From 06027becb6b3d717b37639624d5678b7d6f240b0 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 11 Mar 2024 17:05:19 -0700 Subject: [PATCH] [Impeller] implements blur styles for gradients (flutter/engine#51264) fixes https://github.com/flutter/flutter/issues/144449 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../impeller/aiks/aiks_blur_unittests.cc | 99 +++++++++++++++++++ engine/src/flutter/impeller/aiks/paint.cc | 3 +- .../filters/gaussian_blur_filter_contents.cc | 72 +++++++++----- .../testing/impeller_golden_tests_output.txt | 9 ++ 4 files changed, 155 insertions(+), 28 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_blur_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_blur_unittests.cc index 34a7b7c112..9ade568952 100644 --- a/engine/src/flutter/impeller/aiks/aiks_blur_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_blur_unittests.cc @@ -755,6 +755,105 @@ TEST_P(AiksTest, GaussianBlurAnimatedBackdrop) { ASSERT_TRUE(OpenPlaygroundHere(callback)); } +TEST_P(AiksTest, GaussianBlurStyleInnerGradient) { + Canvas canvas; + canvas.Scale(GetContentScale()); + + canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)}); + + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.7568, 0.2627, 0.2118, 1.0}}; + std::vector stops = {0.0, 1.0}; + + Paint paint; + paint.color_source = ColorSource::MakeLinearGradient( + {0, 0}, {200, 200}, std::move(colors), std::move(stops), + Entity::TileMode::kMirror, {}); + paint.mask_blur_descriptor = Paint::MaskBlurDescriptor{ + .style = FilterContents::BlurStyle::kInner, + .sigma = Sigma(30), + }; + canvas.DrawPath(PathBuilder() + .MoveTo({200, 200}) + .LineTo({300, 400}) + .LineTo({100, 400}) + .Close() + .TakePath(), + paint); + + // Draw another thing to make sure the clip area is reset. + Paint red; + red.color = Color::Red(); + canvas.DrawRect(Rect::MakeXYWH(0, 0, 200, 200), red); + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + +TEST_P(AiksTest, GaussianBlurStyleSolidGradient) { + Canvas canvas; + canvas.Scale(GetContentScale()); + + canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)}); + + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.7568, 0.2627, 0.2118, 1.0}}; + std::vector stops = {0.0, 1.0}; + + Paint paint; + paint.color_source = ColorSource::MakeLinearGradient( + {0, 0}, {200, 200}, std::move(colors), std::move(stops), + Entity::TileMode::kMirror, {}); + paint.mask_blur_descriptor = Paint::MaskBlurDescriptor{ + .style = FilterContents::BlurStyle::kSolid, + .sigma = Sigma(30), + }; + canvas.DrawPath(PathBuilder() + .MoveTo({200, 200}) + .LineTo({300, 400}) + .LineTo({100, 400}) + .Close() + .TakePath(), + paint); + + // Draw another thing to make sure the clip area is reset. + Paint red; + red.color = Color::Red(); + canvas.DrawRect(Rect::MakeXYWH(0, 0, 200, 200), red); + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + +TEST_P(AiksTest, GaussianBlurStyleOuterGradient) { + Canvas canvas; + canvas.Scale(GetContentScale()); + + canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)}); + + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.7568, 0.2627, 0.2118, 1.0}}; + std::vector stops = {0.0, 1.0}; + + Paint paint; + paint.color_source = ColorSource::MakeLinearGradient( + {0, 0}, {200, 200}, std::move(colors), std::move(stops), + Entity::TileMode::kMirror, {}); + paint.mask_blur_descriptor = Paint::MaskBlurDescriptor{ + .style = FilterContents::BlurStyle::kOuter, + .sigma = Sigma(30), + }; + canvas.DrawPath(PathBuilder() + .MoveTo({200, 200}) + .LineTo({300, 400}) + .LineTo({100, 400}) + .Close() + .TakePath(), + paint); + + // Draw another thing to make sure the clip area is reset. + Paint red; + red.color = Color::Red(); + canvas.DrawRect(Rect::MakeXYWH(0, 0, 200, 200), red); + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + TEST_P(AiksTest, GaussianBlurStyleInner) { Canvas canvas; canvas.Scale(GetContentScale()); diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index 31043a1485..dc2c3e9be2 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -170,7 +170,8 @@ std::shared_ptr Paint::MaskBlurDescriptor::CreateMaskBlur( /// 2. Blur the mask. auto blurred_mask = FilterContents::MakeGaussianBlur( - FilterInput::Make(mask), sigma, sigma, Entity::TileMode::kDecal, style); + FilterInput::Make(mask), sigma, sigma, Entity::TileMode::kDecal, style, + color_source_contents->GetGeometry()); /// 3. Replace the geometry of the original color source with a rectangle that /// covers the full region of the blurred mask. Note that geometry is in diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index 46513f3b4b..c116b3d517 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -210,30 +210,39 @@ Entity ApplyClippedBlurStyle(Entity::ClipOperation clip_operation, const Snapshot& input_snapshot, Entity blur_entity, const std::shared_ptr& geometry) { - auto shared_blur_entity = std::make_shared(std::move(blur_entity)); - shared_blur_entity->SetNewClipDepth(entity.GetNewClipDepth()); - auto clipper = std::make_unique(); - clipper->SetClipOperation(clip_operation); - clipper->SetGeometry(geometry); + auto clip_contents = std::make_shared(); + clip_contents->SetClipOperation(clip_operation); + clip_contents->SetGeometry(geometry); + Entity clipper; + clipper.SetContents(clip_contents); auto restore = std::make_unique(); - Entity result; - result.SetTransform(entity.GetTransform()); - result.SetContents(Contents::MakeAnonymous( - fml::MakeCopyable([shared_blur_entity, clipper = std::move(clipper), - restore = std::move(restore)]( - const ContentContext& renderer, - const Entity& entity, RenderPass& pass) mutable { + Matrix entity_transform = entity.GetTransform(); + Matrix blur_transform = blur_entity.GetTransform(); + auto renderer = fml::MakeCopyable( + [blur_entity = blur_entity.Clone(), clipper = std::move(clipper), + restore = std::move(restore), entity_transform, + blur_transform](const ContentContext& renderer, const Entity& entity, + RenderPass& pass) mutable { bool result = true; - result = clipper->Render(renderer, entity, pass) && result; - result = shared_blur_entity->Render(renderer, pass) && result; + clipper.SetNewClipDepth(entity.GetNewClipDepth()); + clipper.SetTransform(entity.GetTransform() * entity_transform); + result = clipper.Render(renderer, pass) && result; + blur_entity.SetNewClipDepth(entity.GetNewClipDepth()); + blur_entity.SetTransform(entity.GetTransform() * blur_transform); + result = blur_entity.Render(renderer, pass) && result; if constexpr (!ContentContext::kEnableStencilThenCover) { result = restore->Render(renderer, entity, pass) && result; } return result; - }), - [shared_blur_entity](const Entity& entity) { - return shared_blur_entity->GetCoverage(); - })); + }); + auto coverage = + fml::MakeCopyable([blur_entity = std::move(blur_entity), + blur_transform](const Entity& entity) mutable { + blur_entity.SetTransform(entity.GetTransform() * blur_transform); + return blur_entity.GetCoverage(); + }); + Entity result; + result.SetContents(Contents::MakeAnonymous(renderer, coverage)); return result; } @@ -256,24 +265,33 @@ Entity ApplyBlurStyle(FilterContents::BlurStyle blur_style, input, input_snapshot, std::move(blur_entity), geometry); case FilterContents::BlurStyle::kSolid: { - Entity blurred = ApplyClippedBlurStyle(Entity::ClipOperation::kIntersect, - entity, input, input_snapshot, - std::move(blur_entity), geometry); Entity snapshot_entity = Entity::FromSnapshot( input_snapshot, entity.GetBlendMode(), entity.GetClipDepth()); Entity result; - std::optional coverage = blurred.GetCoverage(); + Matrix blurred_transform = blur_entity.GetTransform(); + Matrix snapshot_transform = snapshot_entity.GetTransform(); result.SetContents(Contents::MakeAnonymous( - fml::MakeCopyable([blurred = std::move(blurred), + fml::MakeCopyable([blur_entity = blur_entity.Clone(), + blurred_transform, snapshot_transform, snapshot_entity = std::move(snapshot_entity)]( const ContentContext& renderer, const Entity& entity, RenderPass& pass) mutable { - return blurred.Render(renderer, pass) && - snapshot_entity.Render(renderer, pass); + bool result = true; + blur_entity.SetNewClipDepth(entity.GetNewClipDepth()); + blur_entity.SetTransform(entity.GetTransform() * blurred_transform); + result = result && blur_entity.Render(renderer, pass); + snapshot_entity.SetTransform(entity.GetTransform() * + snapshot_transform); + snapshot_entity.SetNewClipDepth(entity.GetNewClipDepth()); + result = result && snapshot_entity.Render(renderer, pass); + return result; }), - fml::MakeCopyable( - [coverage](const Entity& entity) { return coverage; }))); + fml::MakeCopyable([blur_entity = blur_entity.Clone(), + blurred_transform](const Entity& entity) mutable { + blur_entity.SetTransform(entity.GetTransform() * blurred_transform); + return blur_entity.GetCoverage(); + }))); return result; } } diff --git a/engine/src/flutter/testing/impeller_golden_tests_output.txt b/engine/src/flutter/testing/impeller_golden_tests_output.txt index 7da59594a0..f48cecd4b7 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -488,12 +488,21 @@ impeller_Play_AiksTest_GaussianBlurRotatedAndClipped_Vulkan.png impeller_Play_AiksTest_GaussianBlurScaledAndClipped_Metal.png impeller_Play_AiksTest_GaussianBlurScaledAndClipped_OpenGLES.png impeller_Play_AiksTest_GaussianBlurScaledAndClipped_Vulkan.png +impeller_Play_AiksTest_GaussianBlurStyleInnerGradient_Metal.png +impeller_Play_AiksTest_GaussianBlurStyleInnerGradient_OpenGLES.png +impeller_Play_AiksTest_GaussianBlurStyleInnerGradient_Vulkan.png impeller_Play_AiksTest_GaussianBlurStyleInner_Metal.png impeller_Play_AiksTest_GaussianBlurStyleInner_OpenGLES.png impeller_Play_AiksTest_GaussianBlurStyleInner_Vulkan.png +impeller_Play_AiksTest_GaussianBlurStyleOuterGradient_Metal.png +impeller_Play_AiksTest_GaussianBlurStyleOuterGradient_OpenGLES.png +impeller_Play_AiksTest_GaussianBlurStyleOuterGradient_Vulkan.png impeller_Play_AiksTest_GaussianBlurStyleOuter_Metal.png impeller_Play_AiksTest_GaussianBlurStyleOuter_OpenGLES.png impeller_Play_AiksTest_GaussianBlurStyleOuter_Vulkan.png +impeller_Play_AiksTest_GaussianBlurStyleSolidGradient_Metal.png +impeller_Play_AiksTest_GaussianBlurStyleSolidGradient_OpenGLES.png +impeller_Play_AiksTest_GaussianBlurStyleSolidGradient_Vulkan.png impeller_Play_AiksTest_GaussianBlurStyleSolid_Metal.png impeller_Play_AiksTest_GaussianBlurStyleSolid_OpenGLES.png impeller_Play_AiksTest_GaussianBlurStyleSolid_Vulkan.png