From edb33af0d8ca82782f04081b7c57845b0d6ebaeb Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Tue, 17 May 2022 15:53:43 -0700 Subject: [PATCH] Fix texture alpha (flutter/engine#33437) --- .../src/flutter/impeller/aiks/aiks_unittests.cc | 15 ++++++++------- engine/src/flutter/impeller/aiks/canvas.cc | 1 + .../impeller/entity/shaders/texture_fill.frag | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 2872892b7c..dbe9e60f26 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -469,14 +469,14 @@ TEST_P(AiksTest, PaintBlendModeIsRespected) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } -TEST_P(AiksTest, CanDrawWithAdvancedBlend) { +TEST_P(AiksTest, ColorWheel) { // Compare with https://fiddle.skia.org/c/@BlendModes std::vector blend_mode_names; std::vector blend_mode_values; { const std::vector> blends = { - // Pipeline blends (Porter-Duff/alpha blends) + // Pipeline blends (Porter-Duff alpha compositing) {"Clear", Entity::BlendMode::kClear}, {"Source", Entity::BlendMode::kSource}, {"Destination", Entity::BlendMode::kDestination}, @@ -491,7 +491,7 @@ TEST_P(AiksTest, CanDrawWithAdvancedBlend) { {"Xor", Entity::BlendMode::kXor}, {"Plus", Entity::BlendMode::kPlus}, {"Modulate", Entity::BlendMode::kModulate}, - // Advanced blends (non Porter-Duff/color blends) + // Advanced blends (color component blends) {"Screen", Entity::BlendMode::kScreen}, {"ColorBurn", Entity::BlendMode::kColorBurn}, }; @@ -572,16 +572,17 @@ TEST_P(AiksTest, CanDrawWithAdvancedBlend) { draw_color_wheel(canvas); // Draw 3 circles to a subpass and blend it in. - canvas.SaveLayer({.blend_mode = blend_mode_values[current_blend_index]}); + canvas.SaveLayer({.color = Color::White().WithAlpha(alpha), + .blend_mode = blend_mode_values[current_blend_index]}); { paint.blend_mode = Entity::BlendMode::kPlus; const Scalar x = std::sin(k2Pi / 3); const Scalar y = -std::cos(k2Pi / 3); - paint.color = Color::Red().WithAlpha(alpha); + paint.color = Color::Red(); canvas.DrawCircle(Point(-x, y) * 45, 65, paint); - paint.color = Color::Green().WithAlpha(alpha); + paint.color = Color::Green(); canvas.DrawCircle(Point(0, -1) * 45, 65, paint); - paint.color = Color::Blue().WithAlpha(alpha); + paint.color = Color::Blue(); canvas.DrawCircle(Point(x, y) * 45, 65, paint); } canvas.Restore(); diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 68867021f5..e4e4f7fd3c 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -233,6 +233,7 @@ void Canvas::DrawImageRect(std::shared_ptr image, contents->SetTexture(image->GetTexture()); contents->SetSourceRect(source); contents->SetSamplerDescriptor(std::move(sampler)); + contents->SetOpacity(paint.color.alpha); Entity entity; entity.SetBlendMode(paint.blend_mode); diff --git a/engine/src/flutter/impeller/entity/shaders/texture_fill.frag b/engine/src/flutter/impeller/entity/shaders/texture_fill.frag index 4c7810480d..37e7d308e8 100644 --- a/engine/src/flutter/impeller/entity/shaders/texture_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/texture_fill.frag @@ -11,6 +11,5 @@ out vec4 frag_color; void main() { vec4 sampled = texture(texture_sampler, v_texture_coords); - sampled.w *= v_alpha; - frag_color = sampled; + frag_color = sampled * v_alpha; }