Skip to content

Commit e1fee50

Browse files
authored
[Impeller] Reland clipped BackdropFilter square corners fix (flutter#195716)
Fixes flutter#162172 Fixes flutter#190602 Fixes flutter#192992 Fixes flutter#195423 Revised reland of the square corners fix in flutter#191096. Previously, `EntityPassClipStack::RecordRestore` unconditionally popped a single clip replay entry even when the restored scope had skipped a redundant clip or recorded multiple clips, which caused parent rounded clips to get dropped across `BackdropFilter` flips (see my [comment on flutter#190602](flutter#190602 (comment))). `EntityPassClipStack` now tracks `clip_height` on `ReplayResult` so `RecordRestore` only pops entries above `restore_height`. This is the only production code change needed to fix the issue. I structured this PR as two commits to make the diff against the original landing easy to see. Commit 1 is flutter#191096 unmodified, and Commit 2 reverts all of the `Canvas` and `ClipContents` changes back to `master`. In flutter#191096, I gated `should_use_onscreen` on `is_onscreen_` in `Canvas::SaveLayer` to make unit testing easier, which routed `DisplayListToTexture` snapshots through `Canvas::BlitToOnscreen` with an uncleared 4x MSAA depth attachment and caused the `action_sheet_test.dart` alpha flake (see my [comment on flutter#195423](flutter#195423 (comment))). Reverting the `Canvas` changes fixes that flake and removes the clip-depth clamping that @flar flagged in review. Instead of modifying `Canvas` for testability, I replaced the `CreateTestCanvas` tests from Commit 1 with `DisplayListToTexture` pixel-readback tests in `canvas_unittests.cc`, along with unit tests in `clip_stack_unittests.cc` and `aiks_dl_clip_unittests.cc`. I rewrote those tests significantly so they are smaller for the same coverage, and added a test which covers the problem which resulted in the alpha flake behavior. ## 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. <!-- 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 f0add0e commit e1fee50

5 files changed

Lines changed: 117 additions & 16 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flutter/display_list/dl_paint.h"
1111
#include "flutter/display_list/effects/dl_color_filter.h"
1212
#include "flutter/display_list/effects/dl_image_filter.h"
13+
#include "flutter/display_list/geometry/dl_geometry_types.h"
1314
#include "flutter/display_list/geometry/dl_path_builder.h"
1415
#include "flutter/testing/testing.h"
1516

@@ -157,5 +158,35 @@ TEST_P(AiksTest, DilatedContentOutsideClip) {
157158
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
158159
}
159160

161+
TEST_P(AiksTest, CanRenderClippedBackdropFilterWithSuperellipse) {
162+
SetWindowSize({400, 400});
163+
DisplayListBuilder builder;
164+
builder.Scale(GetContentScale().x, GetContentScale().y);
165+
builder.DrawPaint(DlPaint(DlColor::kBlack()));
166+
167+
// Clip to a centered superellipse with large rounded corners.
168+
DlRect rect = DlRect::MakeXYWH(50, 50, 300, 300);
169+
builder.ClipRoundSuperellipse(DlRoundSuperellipse::MakeRectXY(rect, 80, 80));
170+
171+
// Draw backdrop content inside a child DisplayList with a containing
172+
// ClipRect; restoring its scope must preserve the parent superellipse clip.
173+
DisplayListBuilder child_builder;
174+
child_builder.ClipRect(DlRect::MakeXYWH(0, 0, 400, 400));
175+
child_builder.DrawRect(DlRect::MakeXYWH(50, 50, 150, 300),
176+
DlPaint(DlColor::kCornflowerBlue()));
177+
child_builder.DrawRect(DlRect::MakeXYWH(200, 50, 150, 300),
178+
DlPaint(DlColor::kOrange()));
179+
builder.DrawDisplayList(child_builder.Build());
180+
181+
// Apply a BackdropFilter with a translucent white fill across the full rect.
182+
// Its corners remain rounded only if the superellipse clip is replayed.
183+
auto filter = DlImageFilter::MakeBlur(20.0f, 20.0f, DlTileMode::kClamp);
184+
builder.SaveLayer(rect, nullptr, filter.get());
185+
builder.DrawPaint(DlPaint(DlColor::kWhite().withAlpha(160)));
186+
builder.Restore();
187+
188+
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
189+
}
190+
160191
} // namespace testing
161192
} // namespace impeller

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55
#include <unordered_map>
66
#include "flutter/display_list/dl_tile_mode.h"
7-
#include "flutter/display_list/effects/dl_image_filter.h"
8-
#include "flutter/display_list/geometry/dl_geometry_types.h"
97
#include "flutter/testing/testing.h"
108
#include "gtest/gtest.h"
11-
#include "impeller/core/formats.h"
12-
#include "impeller/core/texture_descriptor.h"
139
#include "impeller/display_list/aiks_unittests.h"
1410
#include "impeller/display_list/canvas.h"
1511
#include "impeller/display_list/dl_image_impeller.h"

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,5 +327,69 @@ TEST(EntityPassClipStackTest, ClipAndRestoreWithSubpassesNonAA) {
327327
Rect::MakeLTRB(50, 50, 55, 55));
328328
}
329329

330+
TEST(EntityPassClipStackTest, RestoringChildClipPreservesParentReplayEntities) {
331+
EntityPassClipStack recorder =
332+
EntityPassClipStack(Rect::MakeLTRB(0, 0, 100, 100));
333+
334+
// 1. Record parent clip at height 1.
335+
// Non-axis-aligned (modeling e.g. a rounded rect) so RecordClip does not
336+
// elide it against the initial (0, 0, 100, 100) pass coverage and records a
337+
// replay entity at height 1.
338+
recorder.RecordClip(ClipContents(Rect::MakeLTRB(0, 0, 100, 100),
339+
/*is_axis_aligned_rect=*/false),
340+
Matrix(), {0, 0}, 0, 100, /*is_aa=*/true);
341+
ASSERT_EQ(recorder.GetReplayEntities().size(), 1u);
342+
EXPECT_EQ(recorder.GetReplayEntities()[0].clip_height, 1u);
343+
344+
// 2. Record a redundant child clip at height 2 that skips replay recording.
345+
// Because it is both axis-aligned (no rounded corners cutting inside its
346+
// bounds) and contains the parent's (0, 0, 100, 100) coverage, RecordClip
347+
// pushes coverage at height 2 and returns early without pushing a replay
348+
// entity.
349+
recorder.RecordClip(ClipContents(Rect::MakeLTRB(0, 0, 100, 100),
350+
/*is_axis_aligned_rect=*/true),
351+
Matrix(), {0, 0}, 1, 100, /*is_aa=*/true);
352+
EXPECT_EQ(recorder.GetReplayEntities().size(), 1u);
353+
// Restoring to height 1 must NOT pop the parent replay entity, even though
354+
// this child clip pushed no replay entity of its own.
355+
recorder.RecordRestore({0, 0}, 1);
356+
ASSERT_EQ(recorder.GetReplayEntities().size(), 1u);
357+
EXPECT_EQ(recorder.GetReplayEntities()[0].clip_height, 1u);
358+
359+
// 3. Record a child clip at height 2 that DOES push a replay entity.
360+
// Unlike step 2, this clip is non-axis-aligned and narrower than the parent,
361+
// so RecordClip records a second replay entity at height 2.
362+
recorder.RecordClip(ClipContents(Rect::MakeLTRB(10, 10, 50, 50),
363+
/*is_axis_aligned_rect=*/false),
364+
Matrix(), {0, 0}, 2, 100, /*is_aa=*/true);
365+
ASSERT_EQ(recorder.GetReplayEntities().size(), 2u);
366+
EXPECT_EQ(recorder.GetReplayEntities()[1].clip_height, 2u);
367+
// Restoring to height 1 must pop the child replay entity while preserving the
368+
// parent replay entity.
369+
recorder.RecordRestore({0, 0}, 1);
370+
ASSERT_EQ(recorder.GetReplayEntities().size(), 1u);
371+
EXPECT_EQ(recorder.GetReplayEntities()[0].clip_height, 1u);
372+
373+
// 4. Record two nested rendered clips in a child scope (heights 2 and 3) and
374+
// restore directly to height 1; both child clips must be popped while the
375+
// parent clip remains.
376+
recorder.RecordClip(ClipContents(Rect::MakeLTRB(15, 15, 45, 45),
377+
/*is_axis_aligned_rect=*/false),
378+
Matrix(), {0, 0}, 3, 100, /*is_aa=*/true);
379+
recorder.RecordClip(ClipContents(Rect::MakeLTRB(20, 20, 40, 40),
380+
/*is_axis_aligned_rect=*/false),
381+
Matrix(), {0, 0}, 4, 100, /*is_aa=*/true);
382+
ASSERT_EQ(recorder.GetReplayEntities().size(), 3u);
383+
EXPECT_EQ(recorder.GetReplayEntities()[1].clip_height, 2u);
384+
EXPECT_EQ(recorder.GetReplayEntities()[2].clip_height, 3u);
385+
recorder.RecordRestore({0, 0}, 1);
386+
ASSERT_EQ(recorder.GetReplayEntities().size(), 1u);
387+
EXPECT_EQ(recorder.GetReplayEntities()[0].clip_height, 1u);
388+
389+
// 5. Restore to height 0 (popping parent clip).
390+
recorder.RecordRestore({0, 0}, 0);
391+
EXPECT_TRUE(recorder.GetReplayEntities().empty());
392+
}
393+
330394
} // namespace testing
331395
} // namespace impeller

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,17 @@ EntityPassClipStack::ClipStateResult EntityPassClipStack::RecordRestore(
8383
if (subpass_state.clip_coverage.back().coverage.has_value()) {
8484
FML_DCHECK(next_replay_index_ <=
8585
subpass_state.rendered_clip_entities.size());
86-
// https://github.com/flutter/flutter/issues/162172
87-
// This code is slightly wrong and should be popping more than one clip
88-
// entry.
89-
if (!subpass_state.rendered_clip_entities.empty()) {
86+
// `RecordClip` omits replay entities for axis-aligned intersect rects that
87+
// contain the current clip coverage, and a single `RecordRestore` can
88+
// unwind multiple `RecordClip` calls at once. Popping all entries with
89+
// `clip_height > restore_height` evicts only the restored scopes' clips.
90+
while (!subpass_state.rendered_clip_entities.empty() &&
91+
subpass_state.rendered_clip_entities.back().clip_height >
92+
restore_height) {
9093
subpass_state.rendered_clip_entities.pop_back();
91-
92-
if (next_replay_index_ > subpass_state.rendered_clip_entities.size()) {
93-
next_replay_index_ = subpass_state.rendered_clip_entities.size();
94-
}
94+
}
95+
if (next_replay_index_ > subpass_state.rendered_clip_entities.size()) {
96+
next_replay_index_ = subpass_state.rendered_clip_entities.size();
9597
}
9698
}
9799
return result;
@@ -189,11 +191,15 @@ EntityPassClipStack::ClipStateResult EntityPassClipStack::RecordClip(
189191
FML_DCHECK(next_replay_index_ == subpass_state.rendered_clip_entities.size())
190192
<< "Not all clips have been replayed before appending new clip.";
191193

194+
// Tag with the same `clip_height` as its `ClipCoverageLayer` above so
195+
// `RecordRestore` pops this replay entity if and only if its coverage layer
196+
// is popped.
192197
subpass_state.rendered_clip_entities.push_back(ReplayResult{
193-
.clip_contents = clip_contents, //
194-
.transform = transform, //
195-
.clip_coverage = coverage_value, //
196-
.clip_depth = clip_depth //
198+
.clip_contents = clip_contents, //
199+
.transform = transform, //
200+
.clip_coverage = coverage_value, //
201+
.clip_depth = clip_depth, //
202+
.clip_height = previous_clip_height + 1, //
197203
});
198204
next_replay_index_++;
199205

‎engine/src/flutter/impeller/entity/entity_pass_clip_stack.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class EntityPassClipStack {
2727
Matrix transform;
2828
std::optional<Rect> clip_coverage;
2929
uint32_t clip_depth = 0;
30+
/// The clip stack height at which this clip entity was recorded.
31+
/// Used during restoration to preserve parent clips while popping child
32+
/// scopes.
33+
size_t clip_height = 0;
3034
};
3135

3236
struct ClipStateResult {

0 commit comments

Comments
 (0)