diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index 2077c00829..841222b675 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -3,7 +3,12 @@ // found in the LICENSE file. #include "impeller/aiks/paint.h" + +#include + #include "impeller/entity/contents/color_source_contents.h" +#include "impeller/entity/contents/filters/color_filter_contents.h" +#include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/solid_color_contents.h" #include "impeller/entity/geometry/geometry.h" @@ -30,6 +35,9 @@ std::shared_ptr Paint::CreateContentsForGeometry( std::shared_ptr geometry) const { auto contents = color_source.GetContents(*this); contents->SetGeometry(std::move(geometry)); + if (mask_blur_descriptor.has_value()) { + return mask_blur_descriptor->CreateMaskBlur(contents); + } return contents; } @@ -40,7 +48,7 @@ std::shared_ptr Paint::WithFilters( ColorSource::Type::kColor); input = WithColorFilter(input, /*absorb_opacity=*/true); input = WithInvertFilter(input); - input = WithMaskBlur(input, is_solid_color_val, Matrix()); + input = WithMaskBlur(input, is_solid_color_val); input = WithImageFilter(input, Matrix(), /*is_subpass=*/false); return input; } @@ -53,13 +61,11 @@ std::shared_ptr Paint::WithFiltersForSubpassTarget( return input; } -std::shared_ptr Paint::WithMaskBlur( - std::shared_ptr input, - bool is_solid_color, - const Matrix& effect_transform) const { +std::shared_ptr Paint::WithMaskBlur(std::shared_ptr input, + bool is_solid_color) const { if (mask_blur_descriptor.has_value()) { - input = mask_blur_descriptor->CreateMaskBlur( - FilterInput::Make(input), is_solid_color, effect_transform); + input = mask_blur_descriptor->CreateMaskBlur(FilterInput::Make(input), + is_solid_color); } return input; } @@ -116,16 +122,47 @@ std::shared_ptr Paint::WithInvertFilter( {FilterInput::Make(std::move(input))}, kColorInversion); } +std::shared_ptr Paint::MaskBlurDescriptor::CreateMaskBlur( + std::shared_ptr color_source_contents) const { + /// 1. Create an opaque white mask of the original geometry. + + auto mask = std::make_shared(); + mask->SetColor(Color::White()); + mask->SetGeometry(color_source_contents->GetGeometry()); + + /// 2. Blur the mask. + + auto blurred_mask = FilterContents::MakeGaussianBlur( + FilterInput::Make(mask), sigma, sigma, style, Entity::TileMode::kDecal, + Matrix()); + + /// 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 + /// local bounds. + + auto expanded_local_bounds = blurred_mask->GetCoverage({}); + if (!expanded_local_bounds.has_value()) { + return nullptr; + } + color_source_contents->SetGeometry( + Geometry::MakeRect(*expanded_local_bounds)); + + /// 4. Composite the color source and mask together. + + return ColorFilterContents::MakeBlend( + BlendMode::kSourceIn, {FilterInput::Make(blurred_mask), + FilterInput::Make(color_source_contents)}); +} + std::shared_ptr Paint::MaskBlurDescriptor::CreateMaskBlur( const FilterInput::Ref& input, - bool is_solid_color, - const Matrix& effect_transform) const { + bool is_solid_color) const { if (is_solid_color) { - return FilterContents::MakeGaussianBlur( - input, sigma, sigma, style, Entity::TileMode::kDecal, effect_transform); + return FilterContents::MakeGaussianBlur(input, sigma, sigma, style, + Entity::TileMode::kDecal, Matrix()); } return FilterContents::MakeBorderMaskBlur(input, sigma, sigma, style, - effect_transform); + Matrix()); } bool Paint::HasColorFilter() const { diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index d14fda6a8f..b1f793905c 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -42,10 +42,12 @@ struct Paint { FilterContents::BlurStyle style; Sigma sigma; + std::shared_ptr CreateMaskBlur( + std::shared_ptr color_source_contents) const; + std::shared_ptr CreateMaskBlur( const FilterInput::Ref& input, - bool is_solid_color, - const Matrix& effect_matrix) const; + bool is_solid_color) const; }; Color color = Color::Black(); @@ -101,8 +103,7 @@ struct Paint { private: std::shared_ptr WithMaskBlur(std::shared_ptr input, - bool is_solid_color, - const Matrix& effect_transform) const; + bool is_solid_color) const; std::shared_ptr WithImageFilter(std::shared_ptr input, const Matrix& effect_transform, diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.h b/engine/src/flutter/impeller/entity/contents/color_source_contents.h index a4f0766e1f..0d0392d6e7 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.h +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.h @@ -41,7 +41,6 @@ class ColorSourceContents : public Contents { Scalar GetOpacity() const; - protected: const std::shared_ptr& GetGeometry() const; private: