Skip to content

Commit b6eaa14

Browse files
[Impeller] Fix dilation input bounds when clipping saveLayers (flutter#195652)
Dilation needs source pixels outside the requested output bounds. Both DisplayList and Impeller inset those bounds instead, so a shape outside the parent clip can disappear even when its dilated result overlaps the clip. Outset the required input bounds in both implementations. Add coverage and pixel regressions for this case, including transformed bounds, and restore the dilation comparison against Skia's reverse bounds. fix issue: flutter#195666 Skia reference: https://fiddle.skia.org/c/86acbc9f1ca0a0a85db41a288e858498 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## 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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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/CONTRIBUTING.ai.md [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 [flutter/website]: https://github.com/flutter/website [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 ca28abf commit b6eaa14

6 files changed

Lines changed: 90 additions & 12 deletions

File tree

‎engine/src/flutter/display_list/effects/dl_image_filter_unittests.cc‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,26 @@ TEST(DisplayListImageFilter, DilateBounds) {
322322
TestBounds(filter, input_bounds, expected_output_bounds);
323323
}
324324

325+
TEST(DisplayListImageFilter, DilateInputBounds) {
326+
DlDilateImageFilter filter(10, 20);
327+
DlIRect input_bounds;
328+
EXPECT_EQ(filter.get_input_device_bounds(DlIRect::MakeLTRB(20, 30, 120, 130),
329+
DlMatrix(), input_bounds),
330+
&input_bounds);
331+
EXPECT_EQ(input_bounds, DlIRect::MakeLTRB(10, 10, 130, 150));
332+
}
333+
334+
TEST(DisplayListImageFilter, DilateInputBoundsWithTransform) {
335+
DlDilateImageFilter filter(10, 20);
336+
DlMatrix transform = DlMatrix::MakeTranslation({100, -200, 0}) *
337+
DlMatrix::MakeScale({-2, 3, 1});
338+
DlIRect input_bounds;
339+
EXPECT_EQ(filter.get_input_device_bounds(DlIRect::MakeLTRB(20, 30, 120, 130),
340+
transform, input_bounds),
341+
&input_bounds);
342+
EXPECT_EQ(input_bounds, DlIRect::MakeLTRB(0, -30, 140, 190));
343+
}
344+
325345
TEST(DisplayListImageFilter, ErodeConstructor) {
326346
DlErodeImageFilter filter(5.0, 6.0);
327347
}
@@ -897,12 +917,10 @@ TEST(DisplayListImageFilter, LocalImageFilterBounds) {
897917
}
898918
}
899919
{
900-
// Test for: Know the outset bounds to get the inset bounds
901-
// Skia have some bounds calculate error of DilateFilter and
902-
// MatrixFilter
920+
// Reverse bounds for MatrixFilter are not equivalent yet.
903921
// Skia issue: https://bugs.chromium.org/p/skia/issues/detail?id=13444
904922
// flutter issue: https://github.com/flutter/flutter/issues/108693
905-
if (i == 2 || i == 3) {
923+
if (i == 3) {
906924
continue;
907925
}
908926
auto outset_bounds = SkIRect::MakeLTRB(20, 20, 80, 80);

‎engine/src/flutter/display_list/effects/image_filters/dl_dilate_image_filter.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ DlIRect* DlDilateImageFilter::get_input_device_bounds(
3232
const DlIRect& output_bounds,
3333
const DlMatrix& ctm,
3434
DlIRect& input_bounds) const {
35-
return inset_device_bounds(output_bounds, radius_x_, radius_y_, ctm,
36-
input_bounds);
35+
return outset_device_bounds(output_bounds, radius_x_, radius_y_, ctm,
36+
input_bounds);
3737
}
3838

3939
bool DlDilateImageFilter::equals_(const DlImageFilter& other) const {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "flutter/display_list/dl_color.h"
1010
#include "flutter/display_list/dl_paint.h"
1111
#include "flutter/display_list/effects/dl_color_filter.h"
12+
#include "flutter/display_list/effects/dl_image_filter.h"
1213
#include "flutter/display_list/geometry/dl_path_builder.h"
1314
#include "flutter/testing/testing.h"
1415

@@ -139,5 +140,22 @@ TEST_P(AiksTest, FramebufferBlendsRespectClips) {
139140
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
140141
}
141142

143+
TEST_P(AiksTest, DilatedContentOutsideClip) {
144+
SetWindowSize(impeller::ISize(160, 100));
145+
DisplayListBuilder builder;
146+
builder.DrawColor(DlColor::kBlack(), DlBlendMode::kSrc);
147+
builder.ClipRect(DlRect::MakeXYWH(20, 20, 100, 60));
148+
149+
DlPaint layer_paint;
150+
layer_paint.setImageFilter(DlImageFilter::MakeDilate(10, 10));
151+
builder.SaveLayer(DlRect::MakeXYWH(0, 0, 160, 100), &layer_paint);
152+
// The rectangle is outside the clip, but its dilated right edge is inside.
153+
builder.DrawRect(DlRect::MakeXYWH(15, 45, 4, 10),
154+
DlPaint().setColor(DlColor::kWhite()));
155+
builder.Restore();
156+
157+
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
158+
}
159+
142160
} // namespace testing
143161
} // namespace impeller

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,8 @@ DirectionalMorphologyFilterContents::GetFilterSourceCoverage(
204204
const Rect& output_limit) const {
205205
auto transformed_vector =
206206
effect_transform.TransformDirection(direction_ * radius_.radius).Abs();
207-
switch (morph_type_) {
208-
case FilterContents::MorphType::kDilate:
209-
return output_limit.Expand(-transformed_vector);
210-
case FilterContents::MorphType::kErode:
211-
return output_limit.Expand(transformed_vector);
212-
}
207+
// Dilation and erosion both sample neighboring source pixels.
208+
return output_limit.Expand(transformed_vector);
213209
}
214210

215211
} // namespace impeller

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,37 @@ class MorphologyFilterContentsTest : public EntityPlayground {
4141

4242
INSTANTIATE_PLAYGROUND_SUITE(MorphologyFilterContentsTest);
4343

44+
TEST(MorphologyFilterContentsTest, DilateSourceCoverage) {
45+
auto contents = FilterContents::MakeMorphology(
46+
FilterInput::Make(Rect()), Radius{10}, Radius{20},
47+
FilterContents::MorphType::kDilate);
48+
49+
EXPECT_EQ(contents->GetSourceCoverage({}, Rect::MakeLTRB(20, 30, 120, 130)),
50+
Rect::MakeLTRB(10, 10, 130, 150));
51+
}
52+
53+
TEST(MorphologyFilterContentsTest, DilateSourceCoverageWithEffectTransform) {
54+
auto contents = FilterContents::MakeMorphology(
55+
FilterInput::Make(Rect()), Radius{10}, Radius{20},
56+
FilterContents::MorphType::kDilate);
57+
Matrix transform =
58+
Matrix::MakeTranslation({100, -200, 0}) * Matrix::MakeScale({-2, 3, 1});
59+
contents->SetEffectTransform(transform);
60+
61+
EXPECT_EQ(
62+
contents->GetSourceCoverage(transform, Rect::MakeLTRB(20, 30, 120, 130)),
63+
Rect::MakeLTRB(0, -30, 140, 190));
64+
}
65+
66+
TEST(MorphologyFilterContentsTest, ErodeSourceCoverage) {
67+
auto contents = FilterContents::MakeMorphology(
68+
FilterInput::Make(Rect()), Radius{10}, Radius{20},
69+
FilterContents::MorphType::kErode);
70+
71+
EXPECT_EQ(contents->GetSourceCoverage({}, Rect::MakeLTRB(20, 30, 120, 130)),
72+
Rect::MakeLTRB(10, 10, 130, 150));
73+
}
74+
4475
TEST_P(MorphologyFilterContentsTest, RenderCoverageMatchesGetCoverage) {
4576
std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
4677
ASSERT_NE(texture, nullptr);

‎engine/src/flutter/impeller/entity/save_layer_utils_unittests.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ TEST(SaveLayerUtilsTest, DisjointCoverageTransformedByImageFilter) {
120120
EXPECT_EQ(coverage.value(), Rect::MakeLTRB(200, 200, 210, 210));
121121
}
122122

123+
TEST(SaveLayerUtilsTest, DilateRetainsContentOutsideCoverageLimit) {
124+
auto image_filter = FilterContents::MakeMorphology(
125+
FilterInput::Make(Rect()), Radius{10}, Radius{10},
126+
FilterContents::MorphType::kDilate);
127+
128+
// The source lies outside the clip, but dilation brings it into view.
129+
Rect content_coverage = Rect::MakeXYWH(15, 45, 4, 10);
130+
auto coverage = ComputeSaveLayerCoverage(
131+
content_coverage, /*effect_transform=*/{},
132+
/*coverage_limit=*/Rect::MakeXYWH(20, 20, 100, 60), image_filter);
133+
134+
ASSERT_TRUE(coverage.has_value());
135+
EXPECT_EQ(coverage.value(), content_coverage);
136+
}
137+
123138
TEST(SaveLayerUtilsTest, DisjointCoveragTransformedByCTM) {
124139
// Coverage disjoint from parent coverage.
125140
Matrix ctm = Matrix::MakeTranslation({-200, -200, 0});

0 commit comments

Comments
 (0)