From 3928bcf361b669d85ee39c2c47f583549b4300dc Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 2 Feb 2024 15:54:41 -0800 Subject: [PATCH] [Impeller] In advanced blends, unpremultiply and apply src_input_alpha only if there is no foreground color (flutter/engine#50260) See https://github.com/flutter/flutter/issues/142620 --- .../flutter/impeller/aiks/aiks_unittests.cc | 22 +++++++++ .../shaders/blending/advanced_blend.frag | 16 ++++--- engine/src/flutter/impeller/tools/malioc.json | 48 +++++++++---------- engine/src/flutter/testing/run_tests.py | 2 +- 4 files changed, 56 insertions(+), 32 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index b54b460318..2cf7fe8b5a 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -138,6 +138,28 @@ TEST_P(AiksTest, CanRenderColorFilterWithInvertColorsDrawPaint) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, CanRenderAdvancedBlendColorFilterWithSaveLayer) { + Canvas canvas; + + Rect layer_rect = Rect::MakeXYWH(0, 0, 500, 500); + canvas.ClipRect(layer_rect); + + canvas.SaveLayer( + { + .color_filter = ColorFilter::MakeBlend(BlendMode::kDifference, + Color(0, 1, 0, 0.5)), + }, + layer_rect); + + Paint paint; + canvas.DrawPaint({.color = Color::Black()}); + canvas.DrawRect(Rect::MakeXYWH(100, 100, 300, 300), + {.color = Color::White()}); + canvas.Restore(); + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + namespace { bool GenerateMipmap(const std::shared_ptr& context, std::shared_ptr texture, diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag index 0e7f80958c..db444f522a 100644 --- a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend.frag @@ -41,13 +41,15 @@ void main() { v_dst_texture_coords // texture coordinates )); dst *= blend_info.dst_input_alpha; - f16vec4 src = IPHalfUnpremultiply( - blend_info.color_factor > 0.0hf - ? blend_info.color - : Sample(texture_sampler_src, // sampler - v_src_texture_coords // texture coordinates - )); - src *= blend_info.src_input_alpha; + f16vec4 src = blend_info.color_factor > 0.0hf + ? blend_info.color + : IPHalfUnpremultiply(Sample( + texture_sampler_src, // sampler + v_src_texture_coords // texture coordinates + )); + if (blend_info.color_factor == 0.0hf) { + src.a *= blend_info.src_input_alpha; + } f16vec3 blend_result = AdvancedBlend(dst.rgb, src.rgb, int(blend_type)); diff --git a/engine/src/flutter/impeller/tools/malioc.json b/engine/src/flutter/impeller/tools/malioc.json index f63e7632d3..8e1b1ce7ab 100644 --- a/engine/src/flutter/impeller/tools/malioc.json +++ b/engine/src/flutter/impeller/tools/malioc.json @@ -12,7 +12,7 @@ "uses_late_zs_update": false, "variants": { "Main": { - "fp16_arithmetic": 0, + "fp16_arithmetic": 2, "has_stack_spilling": false, "performance": { "longest_path_bound_pipelines": [ @@ -20,8 +20,8 @@ "arith_fma" ], "longest_path_cycles": [ - 0.625, - 0.625, + 0.578125, + 0.578125, 0.21875, 0.125, 0.0, @@ -42,10 +42,10 @@ "arith_fma" ], "shortest_path_cycles": [ - 0.625, - 0.625, - 0.234375, - 0.125, + 0.53125, + 0.53125, + 0.203125, + 0.0625, 0.0, 0.25, 0.25 @@ -55,8 +55,8 @@ "arith_fma" ], "total_cycles": [ - 0.625, - 0.625, + 0.578125, + 0.578125, 0.296875, 0.125, 0.0, @@ -66,8 +66,8 @@ }, "stack_spill_bytes": 0, "thread_occupancy": 100, - "uniform_registers_used": 10, - "work_registers_used": 24 + "uniform_registers_used": 12, + "work_registers_used": 25 } } } @@ -1423,7 +1423,7 @@ "uses_late_zs_update": false, "variants": { "Main": { - "fp16_arithmetic": 0, + "fp16_arithmetic": 2, "has_stack_spilling": false, "performance": { "longest_path_bound_pipelines": [ @@ -1431,8 +1431,8 @@ "arith_fma" ], "longest_path_cycles": [ - 0.625, - 0.625, + 0.578125, + 0.578125, 0.265625, 0.125, 0.0, @@ -1453,10 +1453,10 @@ "arith_fma" ], "shortest_path_cycles": [ - 0.625, - 0.625, - 0.25, - 0.125, + 0.53125, + 0.53125, + 0.21875, + 0.0625, 0.0, 0.25, 0.25 @@ -1466,8 +1466,8 @@ "arith_fma" ], "total_cycles": [ - 0.625, - 0.625, + 0.578125, + 0.578125, 0.34375, 0.125, 0.0, @@ -1477,8 +1477,8 @@ }, "stack_spill_bytes": 0, "thread_occupancy": 100, - "uniform_registers_used": 12, - "work_registers_used": 22 + "uniform_registers_used": 14, + "work_registers_used": 23 } } }, @@ -1495,7 +1495,7 @@ "arithmetic" ], "longest_path_cycles": [ - 3.630000114440918, + 4.289999961853027, 2.0, 2.0 ], @@ -1516,7 +1516,7 @@ "arithmetic" ], "total_cycles": [ - 4.0, + 4.666666507720947, 2.0, 2.0 ] diff --git a/engine/src/flutter/testing/run_tests.py b/engine/src/flutter/testing/run_tests.py index 760dd3be4f..cd96f7d4c8 100755 --- a/engine/src/flutter/testing/run_tests.py +++ b/engine/src/flutter/testing/run_tests.py @@ -44,7 +44,7 @@ FONT_SUBSET_DIR = os.path.join(BUILDROOT_DIR, 'flutter', 'tools', 'font_subset') ENCODING = 'UTF-8' # This number must be updated when adding new golden tests to impeller. -_NUM_EXPECTED_GENERATED_IMPELLER_GOLDEN_FILES = 551 +_NUM_EXPECTED_GENERATED_IMPELLER_GOLDEN_FILES = 554 logger = logging.getLogger(__name__) logger_handler = logging.StreamHandler()