Skip to content

Commit 6f1ed6c

Browse files
authored
Fixes translated bounded blurs (flutter#191615)
fixes flutter#184447 ## before <img width="2048" height="1536" alt="impeller_Play_AiksTest_CanRenderBoundedBlurWithTranslation_Metal" src="https://github.com/user-attachments/assets/1ac32f0f-2861-4692-a5fa-1b0b61feea5e" /> ## after <img width="2048" height="1536" alt="impeller_Play_AiksTest_CanRenderBoundedBlurWithTranslation_Metal" src="https://github.com/user-attachments/assets/ce2e24f3-46e5-4a15-8c55-8dbc716fdbb5" /> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 3512fc4 commit 6f1ed6c

11 files changed

Lines changed: 130 additions & 2 deletions

‎engine/src/flutter/impeller/display_list/aiks_dl_blur_unittests.cc‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,45 @@ TEST_P(AiksTest, CanRenderBoundedBlur) {
409409
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
410410
}
411411

412+
TEST_P(AiksTest, CanRenderBoundedBlurWithTranslation) {
413+
auto image = DlImageImpeller::Make(CreateTextureForFixture("kalimba.jpg"));
414+
415+
DisplayListBuilder builder;
416+
417+
DlPaint paint;
418+
builder.DrawImage(image, DlPoint(0.0, 0.0), DlImageSampling::kNearestNeighbor,
419+
&paint);
420+
421+
DlPaint save_paint;
422+
save_paint.setBlendMode(DlBlendMode::kSrcOver);
423+
424+
// Subcase 1: Simple translation (e.g., scrolled list item).
425+
builder.Save();
426+
builder.Translate(100, 150);
427+
DlRect rect1 = DlRect::MakeXYWH(0, 0, 200, 100);
428+
builder.ClipRect(rect1);
429+
auto backdrop_filter1 =
430+
DlBlurImageFilter::Make(20, 20, DlTileMode::kDecal, /*bounds=*/rect1);
431+
builder.SaveLayer(std::nullopt, &save_paint, backdrop_filter1.get());
432+
builder.Restore();
433+
builder.Restore();
434+
435+
// Subcase 2: Translation with scale and rotation.
436+
builder.Save();
437+
builder.Translate(50, 300);
438+
builder.Scale(1.2, 1.2);
439+
builder.Rotate(5);
440+
DlRect rect2 = DlRect::MakeXYWH(10, 10, 180, 80);
441+
builder.ClipRect(rect2);
442+
auto backdrop_filter2 =
443+
DlBlurImageFilter::Make(15, 15, DlTileMode::kDecal, /*bounds=*/rect2);
444+
builder.SaveLayer(std::nullopt, &save_paint, backdrop_filter2.get());
445+
builder.Restore();
446+
builder.Restore();
447+
448+
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
449+
}
450+
412451
TEST_P(AiksTest, CanRenderClippedBlur) {
413452
DisplayListBuilder builder;
414453
builder.ClipRect(DlRect::MakeXYWH(100, 150, 400, 400));

‎engine/src/flutter/impeller/display_list/aiks_dl_unittests.cc‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,44 @@ TEST_P(AiksTest, MatrixBackdropFilter) {
785785
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
786786
}
787787

788+
TEST_P(AiksTest, MatrixBackdropFilterWithTranslation) {
789+
DisplayListBuilder builder;
790+
791+
DlPaint paint;
792+
paint.setColor(DlColor::kBlack());
793+
builder.DrawPaint(paint);
794+
builder.SaveLayer(std::nullopt, nullptr);
795+
{
796+
DlPaint paint;
797+
paint.setColor(DlColor::kGreen());
798+
paint.setBlendMode(DlBlendMode::kPlus);
799+
800+
DlPaint rect_paint;
801+
rect_paint.setColor(DlColor::kRed());
802+
rect_paint.setStrokeWidth(4);
803+
rect_paint.setDrawStyle(DlDrawStyle::kStroke);
804+
builder.DrawRect(DlRect::MakeLTRB(0, 0, 300, 300), rect_paint);
805+
builder.DrawCircle(DlPoint(200, 200), 100, paint);
806+
807+
builder.Save();
808+
builder.Translate(300, 300);
809+
builder.ClipRect(DlRect::MakeLTRB(0, 0, 200, 200));
810+
811+
DlMatrix matrix = DlMatrix::MakeTranslation({(200 + 100 * k1OverSqrt2),
812+
(200 + 100 * k1OverSqrt2)}) *
813+
DlMatrix::MakeScale({0.5, 0.5, 1}) *
814+
DlMatrix::MakeTranslation({-200, -200});
815+
auto backdrop_filter =
816+
DlImageFilter::MakeMatrix(matrix, DlImageSampling::kLinear);
817+
builder.SaveLayer(std::nullopt, nullptr, backdrop_filter.get());
818+
builder.Restore();
819+
builder.Restore();
820+
}
821+
builder.Restore();
822+
823+
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
824+
}
825+
788826
TEST_P(AiksTest, MatrixSaveLayerFilter) {
789827
DisplayListBuilder builder;
790828

‎engine/src/flutter/impeller/display_list/canvas.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,8 @@ void Canvas::SaveLayer(const Paint& paint,
18861886
FilterInput::Make(std::move(input_texture)));
18871887
backdrop_filter_contents->SetEffectTransform(
18881888
transform_stack_.back().transform.Basis());
1889+
backdrop_filter_contents->SetLocalToPassTransform(
1890+
transform_stack_.back().transform);
18891891
backdrop_filter_contents->SetRenderingMode(
18901892
transform_stack_.back().transform.HasTranslation()
18911893
? Entity::RenderingMode::kSubpassPrependSnapshotTransform

‎engine/src/flutter/impeller/entity/contents/filters/border_mask_blur_filter_contents.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
8282
/// Create AnonymousContents for rendering.
8383
///
8484

85-
auto sigma = effect_transform * Vector2(sigma_x_.sigma, sigma_y_.sigma);
85+
auto sigma = effect_transform.TransformDirection(
86+
Vector2(sigma_x_.sigma, sigma_y_.sigma));
8687
RenderProc render_proc = [coverage, input_snapshot, input_uvs = input_uvs,
8788
src_color_factor = src_color_factor_,
8889
inner_blur_factor = inner_blur_factor_,

‎engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ void FilterContents::SetEffectTransform(const Matrix& effect_transform) {
146146
}
147147
}
148148

149+
void FilterContents::SetLocalToPassTransform(
150+
const Matrix& local_to_pass_transform) {
151+
local_to_pass_transform_ = local_to_pass_transform;
152+
153+
for (auto& input : inputs_) {
154+
input->SetLocalToPassTransform(local_to_pass_transform);
155+
}
156+
}
157+
149158
bool FilterContents::Render(const ContentContext& renderer,
150159
const Entity& entity,
151160
RenderPass& pass) const {

‎engine/src/flutter/impeller/entity/contents/filters/filter_contents.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ class FilterContents : public Contents {
106106
/// Entity being rendered.
107107
void SetEffectTransform(const Matrix& effect_transform);
108108

109+
/// @brief Sets the transform used for transforming local canvas geometry
110+
/// (such as `DlBlurImageFilter::bounds_`) into subpass coordinates.
111+
///
112+
/// In subpass and backdrop filter rendering scenarios, the effect
113+
/// itself (such as blur sigma or matrix filter transformations)
114+
/// operates on the screen-space backdrop and typically only requires
115+
/// the scale and rotation of the canvas CTM (via
116+
/// `SetEffectTransform`) to avoid shifting or double-translating the
117+
/// effect.
118+
///
119+
/// However, local geometry defined in widget/canvas space (such as a
120+
/// bounded blur rectangle) requires the full current transformation
121+
/// matrix including translation to be mapped accurately to the
122+
/// backdrop texture space (e.g. when rendering bounded blurs inside
123+
/// scrolling views).
124+
///
125+
/// @see `SetEffectTransform`
126+
void SetLocalToPassTransform(const Matrix& local_to_pass_transform);
127+
128+
const Matrix& GetLocalToPassTransform() const {
129+
return local_to_pass_transform_;
130+
}
131+
109132
/// @brief Create an Entity that renders this filter's output.
110133
std::optional<Entity> GetEntity(
111134
const ContentContext& renderer,
@@ -228,6 +251,7 @@ class FilterContents : public Contents {
228251

229252
FilterInput::Vector inputs_;
230253
Matrix effect_transform_ = Matrix();
254+
Matrix local_to_pass_transform_ = Matrix();
231255

232256
FilterContents(const FilterContents&) = delete;
233257

‎engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,8 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
847847

848848
std::optional<Quad> source_bounds;
849849
if (bounds_.has_value()) {
850-
Matrix transform = snapshot_entity.GetTransform() * effect_transform;
850+
Matrix transform =
851+
snapshot_entity.GetTransform() * GetLocalToPassTransform();
851852
source_bounds = bounds_->GetTransformedPoints(transform);
852853
}
853854

‎engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ void FilterContentsFilterInput::SetEffectTransform(const Matrix& matrix) {
5858
filter_->SetEffectTransform(matrix);
5959
}
6060

61+
void FilterContentsFilterInput::SetLocalToPassTransform(const Matrix& matrix) {
62+
filter_->SetLocalToPassTransform(matrix);
63+
}
64+
6165
void FilterContentsFilterInput::SetRenderingMode(
6266
Entity::RenderingMode rendering_mode) {
6367
filter_->SetRenderingMode(rendering_mode);

‎engine/src/flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class FilterContentsFilterInput final : public FilterInput {
3737
// |FilterInput|
3838
virtual void SetEffectTransform(const Matrix& matrix) override;
3939

40+
// |FilterInput|
41+
virtual void SetLocalToPassTransform(const Matrix& matrix) override;
42+
4043
// |FilterInput|
4144
virtual void SetRenderingMode(Entity::RenderingMode rendering_mode) override;
4245

‎engine/src/flutter/impeller/entity/contents/filters/inputs/filter_input.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ FilterInput::~FilterInput() = default;
8080

8181
void FilterInput::SetEffectTransform(const Matrix& matrix) {}
8282

83+
void FilterInput::SetLocalToPassTransform(const Matrix& matrix) {}
84+
8385
void FilterInput::SetRenderingMode(Entity::RenderingMode rendering_mode) {}
8486

8587
} // namespace impeller

0 commit comments

Comments
 (0)