Skip to content

Commit 5544f00

Browse files
authored
Fix square corner issue (flutter#191096)
Fixes flutter#190602, see my [comment](flutter#190602 (comment)) on that issue for details about the problems I identified. Also fixes flutter#162172 ## What changed When a route contained rounded clips (such as `CupertinoSheetRoute`), intermediate child ink features, and a backdrop filter (such as a blurred app bar), the clip stack replay logic prematurely popped parent clips and exceeded the depth buffer budget across render pass splits, causing subsequent header draws to render with 90° square corners. This PR addresses both mechanisms: First, `EntityPassClipStack::RecordRestore` was popping replay entities unconditionally whenever any clip was restored. In a sheet containing ink decorations (like list item ripple effects), child clip restores at deeper heights would prematurely discard the root parent clip. We now track `clip_height` in `ReplayResult` so that restorations only pop entities when `clip_height > restore_height`, preserving parent clips across sibling restores. When replaying those clips in `ClipContents::Render`, we also supply the entity's model matrix to properly recompute the MVP matrix against the new pass's orthographic projection instead of reusing the stale matrix from the prior pass. Second, the pass depth budget was exhausting across backdrop splits. `Canvas::kMaxDepth` (`1 << 24`) was mismatched with `Entity::kDepthEpsilon` (`1.0 / 262144.0`, or `2^-18`), allowing integer depths to exceed the 18-bit precision of the depth buffer. Aligning `Canvas::kMaxDepth` to `262,143` and capping depth allocations in `Save`/`SaveLayer` to `min(current_depth_ + total_content_depth, parent_clip_depth)` keeps clip replay and subsequent draw operations within valid precision bounds. In `Canvas::Restore`, we also prevent unconditional depth jumps to `kMaxDepth` for unclipped saves, preserving the allocated budget for subsequent sibling draws. ## Testing Reproducing this interaction in automated tests required testing the onscreen pass-splitting branch (`IsOnscreen()`), which normal offscreen unit tests bypass. We added a `ScopedOnscreenOverride` test hook in `Canvas` and constructed a multi-layer display list recreating the route clip, child ink items, blurred backdrop bar, and trailing app bar: * `clip_stack_unittests.cc`: Added `EntityPassClipStackTest.RestoringChildClipPreservesParentReplayEntities` to isolate parent clip retention across sibling restores. * `canvas_unittests.cc`: Added `AiksTest.ClipDepthMaintainedAcrossBackdropFilterAndLayers` to verify depth invariants and precision bounds (`<= kMaxDepth`), and `AiksTest.ParentClipDepthBudgetPreservedAfterBackdropLayerRestore` to verify parent depth budget restoration. * `dl_golden_unittests.cc` (`impeller_golden_tests`): Added `DlGoldenTest.CanRenderClippedBackdropFilterWithSuperellipse` for pixel-accurate golden image verification in CI against Skia Gold. ## 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. <!-- 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 a1498f2 commit 5544f00

10 files changed

Lines changed: 342 additions & 42 deletions

File tree

‎engine/src/flutter/impeller/display_list/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ template("aiks_unittests_component") {
196196
"//flutter/impeller/geometry:geometry_asserts",
197197
"//flutter/impeller/golden_tests:golden_playground_test",
198198
"//flutter/impeller/playground:playground_test",
199+
"//flutter/impeller/renderer/testing:mocks",
199200
"//flutter/testing:testing_lib",
200201
"//flutter/txt",
201202
]

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
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"
13+
#include "flutter/display_list/geometry/dl_geometry_types.h"
1214
#include "flutter/display_list/geometry/dl_path_builder.h"
1315
#include "flutter/testing/testing.h"
1416

@@ -139,5 +141,80 @@ TEST_P(AiksTest, FramebufferBlendsRespectClips) {
139141
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
140142
}
141143

144+
TEST_P(AiksTest, CanRenderClippedBackdropFilterWithSuperellipse) {
145+
DisplayListBuilder root_builder;
146+
147+
root_builder.Scale(GetContentScale().x, GetContentScale().y);
148+
149+
// 1. Draw a dark background on the root pass.
150+
DlPaint bg_paint;
151+
bg_paint.setColor(DlColor::kBlack());
152+
root_builder.DrawPaint(bg_paint);
153+
154+
// 2. Wrap in TransformLayer (matching CupertinoSheetTransition scale +
155+
// slide).
156+
root_builder.Save();
157+
root_builder.Scale(0.9f, 0.9f);
158+
root_builder.Translate(33.4f, 3.5f);
159+
160+
// 3. Apply ClipRoundSuperellipse with top-only rounded corners (matching
161+
// CupertinoSheet).
162+
DlRect sheet_rect = DlRect::MakeXYWH(0, 42, 400, 500);
163+
DlRoundingRadii top_radii = {
164+
.top_left = {12, 12},
165+
.top_right = {12, 12},
166+
.bottom_left = {0, 0},
167+
.bottom_right = {0, 0},
168+
};
169+
DlRoundSuperellipse clip_rse =
170+
DlRoundSuperellipse::MakeRectRadii(sheet_rect, top_radii);
171+
root_builder.ClipRoundSuperellipse(clip_rse, DlClipOp::kIntersect);
172+
173+
// 4. Wrap in OpacityLayer offset (matching CupertinoSheetRoute offset: (0,
174+
// 42)).
175+
root_builder.Save();
176+
root_builder.Translate(0.0f, 42.0f);
177+
178+
// 5. Layer 1: DisplayListLayer for sheet body with Ink features.
179+
for (int i = 0; i < 5; i++) {
180+
DisplayListBuilder item_builder;
181+
DlPaint ink_paint;
182+
ink_paint.setColor(DlColor::RGBA(0.0f, 0.0f, 0.0f, 0.07f));
183+
item_builder.DrawRect(DlRect::MakeXYWH(0, i * 50, 400, 45), ink_paint);
184+
root_builder.DrawDisplayList(item_builder.Build());
185+
}
186+
187+
// 6. Layer 2: BackdropFilterLayer (_glassBar).
188+
root_builder.Save();
189+
DlRect glass_rect = DlRect::MakeXYWH(20, 350, 360, 60);
190+
DlRoundRect glass_rrect = DlRoundRect::MakeRectXY(glass_rect, 14, 14);
191+
root_builder.ClipRoundRect(glass_rrect, DlClipOp::kIntersect);
192+
193+
DlPaint save_paint;
194+
auto backdrop_filter =
195+
DlImageFilter::MakeBlur(12.0f, 12.0f, DlTileMode::kClamp);
196+
root_builder.SaveLayer(glass_rect, &save_paint, backdrop_filter.get());
197+
DisplayListBuilder glass_builder;
198+
DlPaint orange_paint;
199+
orange_paint.setColor(DlColor::RGBA(1.0f, 0.6f, 0.0f, 0.6f));
200+
glass_builder.DrawRect(glass_rect, orange_paint);
201+
root_builder.DrawDisplayList(glass_builder.Build());
202+
root_builder.Restore(); // Restore SaveLayer
203+
root_builder.Restore(); // Restore glass clip
204+
205+
// 7. Layer 3: DisplayListLayer for Scaffold AppBar / header drawn AFTER
206+
// backdrop filter.
207+
DisplayListBuilder appbar_builder;
208+
DlPaint white_paint;
209+
white_paint.setColor(DlColor::kWhite());
210+
appbar_builder.DrawRect(DlRect::MakeXYWH(0, 0, 400, 80), white_paint);
211+
root_builder.DrawDisplayList(appbar_builder.Build());
212+
213+
root_builder.Restore(); // Restore OpacityLayer offset
214+
root_builder.Restore(); // Restore TransformLayer
215+
216+
ASSERT_TRUE(OpenPlaygroundHere(root_builder.Build()));
217+
}
218+
142219
} // namespace testing
143220
} // namespace impeller

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,11 +1775,9 @@ void Canvas::Save(uint32_t total_content_depth) {
17751775

17761776
auto entry = CanvasStackEntry{};
17771777
entry.transform = transform_stack_.back().transform;
1778-
entry.clip_depth = current_depth_ + total_content_depth;
1778+
entry.clip_depth = std::min<uint32_t>(current_depth_ + total_content_depth,
1779+
transform_stack_.back().clip_depth);
17791780
entry.distributed_opacity = transform_stack_.back().distributed_opacity;
1780-
FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
1781-
<< entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
1782-
<< " after allocating " << total_content_depth;
17831781
entry.clip_height = transform_stack_.back().clip_height;
17841782
entry.rendering_mode = Entity::RenderingMode::kDirect;
17851783
transform_stack_.push_back(entry);
@@ -1942,7 +1940,7 @@ void Canvas::SaveLayer(const Paint& paint,
19421940
// 3. The current render pass is for the onscreen pass.
19431941
const bool should_use_onscreen =
19441942
renderer_.GetDeviceCapabilities().SupportsFramebufferFetch() &&
1945-
backdrop_count_ == 0 && render_passes_.size() == 1u;
1943+
backdrop_count_ == 0 && render_passes_.size() == 1u && is_onscreen_;
19461944
input_texture = FlipBackdrop(
19471945
GetGlobalPassPosition(), //
19481946
/*should_remove_texture=*/will_cache_backdrop_texture, //
@@ -2028,10 +2026,8 @@ void Canvas::SaveLayer(const Paint& paint,
20282026

20292027
CanvasStackEntry entry;
20302028
entry.transform = transform_stack_.back().transform;
2031-
entry.clip_depth = current_depth_ + total_content_depth;
2032-
FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
2033-
<< entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
2034-
<< " after allocating " << total_content_depth;
2029+
entry.clip_depth = std::min<uint32_t>(current_depth_ + total_content_depth,
2030+
transform_stack_.back().clip_depth);
20352031
entry.clip_height = transform_stack_.back().clip_height;
20362032
entry.rendering_mode = Entity::RenderingMode::kSubpassAppendSnapshotTransform;
20372033
entry.did_round_out = did_round_out;
@@ -2076,9 +2072,16 @@ bool Canvas::Restore() {
20762072
// to be overly conservative, but we need to jump the depth to
20772073
// the clip depth so that the next rendering op will get a
20782074
// larger depth (it will pre-increment the current_depth_ value).
2079-
FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
2080-
<< current_depth_ << " <=? " << transform_stack_.back().clip_depth;
2081-
current_depth_ = transform_stack_.back().clip_depth;
2075+
//
2076+
// Only advance depth if clips were recorded and the allocated clip depth
2077+
// was finite. This prevents premature exhaustion of the parent pass depth
2078+
// budget.
2079+
if (transform_stack_.back().num_clips > 0 &&
2080+
transform_stack_.back().clip_depth < kMaxDepth) {
2081+
FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
2082+
<< current_depth_ << " <=? " << transform_stack_.back().clip_depth;
2083+
current_depth_ = transform_stack_.back().clip_depth;
2084+
}
20822085

20832086
if (IsSkipping()) {
20842087
transform_stack_.pop_back();
@@ -2628,8 +2631,6 @@ std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
26282631
return nullptr;
26292632
}
26302633

2631-
// Restore any clips that were recorded before the backdrop filter was
2632-
// applied.
26332634
auto& replay_entities = clip_coverage_stack_.GetReplayEntities();
26342635
uint64_t current_depth =
26352636
post_depth_increment ? current_depth_ - 1 : current_depth_;
@@ -2641,7 +2642,7 @@ std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
26412642
SetClipScissor(replay.clip_coverage, current_render_pass,
26422643
global_pass_position);
26432644
if (!replay.clip_contents.Render(renderer_, current_render_pass,
2644-
replay.clip_depth)) {
2645+
replay.clip_depth, replay.transform)) {
26452646
VALIDATION_LOG << "Failed to render entity for clip restore.";
26462647
}
26472648
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ class LazyRenderingConfig {
121121

122122
class Canvas {
123123
public:
124-
static constexpr uint32_t kMaxDepth = 1 << 24;
124+
/// Maximum discrete integer depth level within the 18-bit depth precision
125+
/// limit defined by `Entity::kDepthEpsilon`.
126+
static constexpr uint32_t kMaxDepth = (1 << 18) - 1;
125127

126128
Canvas(ContentContext& renderer,
127129
const RenderTarget& render_target,

0 commit comments

Comments
 (0)