[Impeller] implement blur style solid (flutter/engine#50892)

fixes https://github.com/flutter/flutter/issues/134178

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
This commit is contained in:
gaaclarke
2024-03-05 15:41:17 -08:00
committed by GitHub
parent 5b91595d3a
commit 425aeca227
8 changed files with 107 additions and 56 deletions

View File

@@ -644,5 +644,33 @@ TEST_P(AiksTest, GaussianBlurStyleOuter) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, GaussianBlurStyleSolid) {
Canvas canvas;
canvas.Scale(GetContentScale());
canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)});
Paint paint;
paint.color = Color::Green();
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()));
}
} // namespace testing
} // namespace impeller

View File

@@ -111,7 +111,7 @@ static std::optional<Entity> AdvancedBlend(
if (!dst_snapshot.has_value()) {
return std::nullopt;
}
return Entity::FromSnapshot(dst_snapshot, entity.GetBlendMode(),
return Entity::FromSnapshot(dst_snapshot.value(), entity.GetBlendMode(),
entity.GetClipDepth());
}
auto maybe_src_uvs = src_snapshot->GetCoverageUVs(coverage);
@@ -119,7 +119,7 @@ static std::optional<Entity> AdvancedBlend(
if (!dst_snapshot.has_value()) {
return std::nullopt;
}
return Entity::FromSnapshot(dst_snapshot, entity.GetBlendMode(),
return Entity::FromSnapshot(dst_snapshot.value(), entity.GetBlendMode(),
entity.GetClipDepth());
}
src_uvs = maybe_src_uvs.value();
@@ -426,7 +426,7 @@ std::optional<Entity> BlendFilterContents::CreateForegroundPorterDuffBlend(
}
if (blend_mode == BlendMode::kDestination) {
return Entity::FromSnapshot(dst_snapshot, entity.GetBlendMode(),
return Entity::FromSnapshot(dst_snapshot.value(), entity.GetBlendMode(),
entity.GetClipDepth());
}

View File

@@ -204,31 +204,12 @@ int ScaleBlurRadius(Scalar radius, Scalar scalar) {
return static_cast<int>(std::round(radius * scalar));
}
Entity ApplyBlurStyle(FilterContents::BlurStyle blur_style,
const Entity& entity,
const std::shared_ptr<FilterInput>& input,
const Snapshot& input_snapshot,
Entity blur_entity,
const std::shared_ptr<Geometry>& geometry) {
if (blur_style == FilterContents::BlurStyle::kNormal) {
return blur_entity;
}
Entity::ClipOperation clip_operation;
switch (blur_style) {
case FilterContents::BlurStyle::kNormal:
FML_UNREACHABLE();
break;
case FilterContents::BlurStyle::kInner:
clip_operation = Entity::ClipOperation::kIntersect;
break;
case FilterContents::BlurStyle::kOuter:
clip_operation = Entity::ClipOperation::kDifference;
break;
case FilterContents::BlurStyle::kSolid:
FML_DLOG(ERROR) << "Unimplemented blur style";
return blur_entity;
}
Entity ApplyClippedBlurStyle(Entity::ClipOperation clip_operation,
const Entity& entity,
const std::shared_ptr<FilterInput>& input,
const Snapshot& input_snapshot,
Entity blur_entity,
const std::shared_ptr<Geometry>& geometry) {
auto shared_blur_entity = std::make_shared<Entity>(std::move(blur_entity));
shared_blur_entity->SetNewClipDepth(entity.GetNewClipDepth());
auto clipper = std::make_unique<ClipContents>();
@@ -255,6 +236,48 @@ Entity ApplyBlurStyle(FilterContents::BlurStyle blur_style,
}));
return result;
}
Entity ApplyBlurStyle(FilterContents::BlurStyle blur_style,
const Entity& entity,
const std::shared_ptr<FilterInput>& input,
const Snapshot& input_snapshot,
Entity blur_entity,
const std::shared_ptr<Geometry>& geometry) {
switch (blur_style) {
case FilterContents::BlurStyle::kNormal:
return blur_entity;
case FilterContents::BlurStyle::kInner:
return ApplyClippedBlurStyle(Entity::ClipOperation::kIntersect, entity,
input, input_snapshot,
std::move(blur_entity), geometry);
break;
case FilterContents::BlurStyle::kOuter:
return ApplyClippedBlurStyle(Entity::ClipOperation::kDifference, entity,
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<Rect> coverage = blurred.GetCoverage();
result.SetContents(Contents::MakeAnonymous(
fml::MakeCopyable([blurred = std::move(blurred),
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);
}),
fml::MakeCopyable(
[coverage](const Entity& entity) { return coverage; })));
return result;
}
}
}
} // namespace
std::string_view GaussianBlurFilterContents::kNoMipsError =
@@ -499,7 +522,7 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
SamplerDescriptor sampler_desc = MakeSamplerDescriptor(
MinMagFilter::kLinear, SamplerAddressMode::kClampToEdge);
auto blur_output_entity = Entity::FromSnapshot(
Entity blur_output_entity = Entity::FromSnapshot(
Snapshot{.texture = pass3_out.value().GetRenderTargetTexture(),
.transform = input_snapshot->transform *
padding_snapshot_adjustment *
@@ -508,13 +531,9 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
.opacity = input_snapshot->opacity},
entity.GetBlendMode(), entity.GetClipDepth());
if (!blur_output_entity.has_value()) {
return std::nullopt;
}
return ApplyBlurStyle(mask_blur_style_, entity, inputs[0],
input_snapshot.value(),
std::move(blur_output_entity.value()), mask_geometry_);
input_snapshot.value(), std::move(blur_output_entity),
mask_geometry_);
}
Scalar GaussianBlurFilterContents::CalculateBlurRadius(Scalar sigma) {

View File

@@ -37,9 +37,13 @@ std::optional<Entity> LocalMatrixFilterContents::RenderFilter(
const Matrix& effect_transform,
const Rect& coverage,
const std::optional<Rect>& coverage_hint) const {
return Entity::FromSnapshot(
inputs[0]->GetSnapshot("LocalMatrix", renderer, entity),
entity.GetBlendMode(), entity.GetClipDepth());
std::optional<Snapshot> snapshot =
inputs[0]->GetSnapshot("LocalMatrix", renderer, entity);
if (!snapshot.has_value()) {
return std::nullopt;
}
return Entity::FromSnapshot(snapshot.value(), entity.GetBlendMode(),
entity.GetClipDepth());
}
} // namespace impeller

View File

@@ -64,7 +64,10 @@ std::optional<Entity> MatrixFilterContents::RenderFilter(
snapshot->transform;
snapshot->sampler_descriptor = sampler_descriptor_;
return Entity::FromSnapshot(snapshot, entity.GetBlendMode(),
if (!snapshot.has_value()) {
return std::nullopt;
}
return Entity::FromSnapshot(snapshot.value(), entity.GetBlendMode(),
entity.GetClipDepth());
}

View File

@@ -19,26 +19,21 @@
namespace impeller {
std::optional<Entity> Entity::FromSnapshot(
const std::optional<Snapshot>& snapshot,
BlendMode blend_mode,
uint32_t clip_depth) {
if (!snapshot.has_value()) {
return std::nullopt;
}
auto texture_rect = Rect::MakeSize(snapshot->texture->GetSize());
Entity Entity::FromSnapshot(const Snapshot& snapshot,
BlendMode blend_mode,
uint32_t clip_depth) {
auto texture_rect = Rect::MakeSize(snapshot.texture->GetSize());
auto contents = TextureContents::MakeRect(texture_rect);
contents->SetTexture(snapshot->texture);
contents->SetSamplerDescriptor(snapshot->sampler_descriptor);
contents->SetTexture(snapshot.texture);
contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
contents->SetSourceRect(texture_rect);
contents->SetOpacity(snapshot->opacity);
contents->SetOpacity(snapshot.opacity);
Entity entity;
entity.SetBlendMode(blend_mode);
entity.SetClipDepth(clip_depth);
entity.SetTransform(snapshot->transform);
entity.SetTransform(snapshot.transform);
entity.SetContents(contents);
return entity;
}

View File

@@ -62,10 +62,9 @@ class Entity {
};
/// @brief Create an entity that can be used to render a given snapshot.
static std::optional<Entity> FromSnapshot(
const std::optional<Snapshot>& snapshot,
BlendMode blend_mode = BlendMode::kSourceOver,
uint32_t clip_depth = 0);
static Entity FromSnapshot(const Snapshot& snapshot,
BlendMode blend_mode = BlendMode::kSourceOver,
uint32_t clip_depth = 0);
Entity();

View File

@@ -494,6 +494,9 @@ impeller_Play_AiksTest_GaussianBlurStyleInner_Vulkan.png
impeller_Play_AiksTest_GaussianBlurStyleOuter_Metal.png
impeller_Play_AiksTest_GaussianBlurStyleOuter_OpenGLES.png
impeller_Play_AiksTest_GaussianBlurStyleOuter_Vulkan.png
impeller_Play_AiksTest_GaussianBlurStyleSolid_Metal.png
impeller_Play_AiksTest_GaussianBlurStyleSolid_OpenGLES.png
impeller_Play_AiksTest_GaussianBlurStyleSolid_Vulkan.png
impeller_Play_AiksTest_GaussianBlurWithoutDecalSupport_Metal.png
impeller_Play_AiksTest_GradientStrokesRenderCorrectly_Metal.png
impeller_Play_AiksTest_GradientStrokesRenderCorrectly_OpenGLES.png