[Impeller] Add generic path for mask blurring (flutter/engine#42308)

Resolves https://github.com/flutter/flutter/issues/127013.

Makes mask blurs work for most color source + geometry combinations.
Crucially, this doesn't interfere with the fast paths for solid color
RRect blurs (used for shadows).

I'll be addressing mask blurs for text in a follow-up.
This commit is contained in:
Brandon DeRosier
2023-05-25 09:31:20 -07:00
committed by GitHub
parent fee0534882
commit 273bb77b61
3 changed files with 54 additions and 17 deletions

View File

@@ -3,7 +3,12 @@
// found in the LICENSE file.
#include "impeller/aiks/paint.h"
#include <memory>
#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<Contents> Paint::CreateContentsForGeometry(
std::shared_ptr<Geometry> 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<Contents> 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<Contents> Paint::WithFiltersForSubpassTarget(
return input;
}
std::shared_ptr<Contents> Paint::WithMaskBlur(
std::shared_ptr<Contents> input,
bool is_solid_color,
const Matrix& effect_transform) const {
std::shared_ptr<Contents> Paint::WithMaskBlur(std::shared_ptr<Contents> 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<Contents> Paint::WithInvertFilter(
{FilterInput::Make(std::move(input))}, kColorInversion);
}
std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
std::shared_ptr<ColorSourceContents> color_source_contents) const {
/// 1. Create an opaque white mask of the original geometry.
auto mask = std::make_shared<SolidColorContents>();
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<FilterContents> 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 {

View File

@@ -42,10 +42,12 @@ struct Paint {
FilterContents::BlurStyle style;
Sigma sigma;
std::shared_ptr<FilterContents> CreateMaskBlur(
std::shared_ptr<ColorSourceContents> color_source_contents) const;
std::shared_ptr<FilterContents> 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<Contents> WithMaskBlur(std::shared_ptr<Contents> input,
bool is_solid_color,
const Matrix& effect_transform) const;
bool is_solid_color) const;
std::shared_ptr<Contents> WithImageFilter(std::shared_ptr<Contents> input,
const Matrix& effect_transform,

View File

@@ -41,7 +41,6 @@ class ColorSourceContents : public Contents {
Scalar GetOpacity() const;
protected:
const std::shared_ptr<Geometry>& GetGeometry() const;
private: