Skip to content

Commit 189ce17

Browse files
committed
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 8fd3e33 commit 189ce17

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
@@ -203,6 +203,7 @@ template("aiks_unittests_component") {
203203
"//flutter/impeller/geometry:geometry_asserts",
204204
"//flutter/impeller/golden_tests:golden_playground_test",
205205
"//flutter/impeller/playground:playground_test",
206+
"//flutter/impeller/renderer/testing:mocks",
206207
"//flutter/testing:testing_lib",
207208
"//flutter/txt",
208209
]

‎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
@@ -1780,11 +1780,9 @@ void Canvas::Save(uint32_t total_content_depth) {
17801780

17811781
auto entry = CanvasStackEntry{};
17821782
entry.transform = transform_stack_.back().transform;
1783-
entry.clip_depth = current_depth_ + total_content_depth;
1783+
entry.clip_depth = std::min<uint32_t>(current_depth_ + total_content_depth,
1784+
transform_stack_.back().clip_depth);
17841785
entry.distributed_opacity = transform_stack_.back().distributed_opacity;
1785-
FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
1786-
<< entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
1787-
<< " after allocating " << total_content_depth;
17881786
entry.clip_height = transform_stack_.back().clip_height;
17891787
entry.rendering_mode = Entity::RenderingMode::kDirect;
17901788
transform_stack_.push_back(entry);
@@ -1947,7 +1945,7 @@ void Canvas::SaveLayer(const Paint& paint,
19471945
// 3. The current render pass is for the onscreen pass.
19481946
const bool should_use_onscreen =
19491947
renderer_.GetDeviceCapabilities().SupportsFramebufferFetch() &&
1950-
backdrop_count_ == 0 && render_passes_.size() == 1u;
1948+
backdrop_count_ == 0 && render_passes_.size() == 1u && is_onscreen_;
19511949
input_texture = FlipBackdrop(
19521950
GetGlobalPassPosition(), //
19531951
/*should_remove_texture=*/will_cache_backdrop_texture, //
@@ -2033,10 +2031,8 @@ void Canvas::SaveLayer(const Paint& paint,
20332031

20342032
CanvasStackEntry entry;
20352033
entry.transform = transform_stack_.back().transform;
2036-
entry.clip_depth = current_depth_ + total_content_depth;
2037-
FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
2038-
<< entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
2039-
<< " after allocating " << total_content_depth;
2034+
entry.clip_depth = std::min<uint32_t>(current_depth_ + total_content_depth,
2035+
transform_stack_.back().clip_depth);
20402036
entry.clip_height = transform_stack_.back().clip_height;
20412037
entry.rendering_mode = Entity::RenderingMode::kSubpassAppendSnapshotTransform;
20422038
entry.did_round_out = did_round_out;
@@ -2081,9 +2077,16 @@ bool Canvas::Restore() {
20812077
// to be overly conservative, but we need to jump the depth to
20822078
// the clip depth so that the next rendering op will get a
20832079
// larger depth (it will pre-increment the current_depth_ value).
2084-
FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
2085-
<< current_depth_ << " <=? " << transform_stack_.back().clip_depth;
2086-
current_depth_ = transform_stack_.back().clip_depth;
2080+
//
2081+
// Only advance depth if clips were recorded and the allocated clip depth
2082+
// was finite. This prevents premature exhaustion of the parent pass depth
2083+
// budget.
2084+
if (transform_stack_.back().num_clips > 0 &&
2085+
transform_stack_.back().clip_depth < kMaxDepth) {
2086+
FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
2087+
<< current_depth_ << " <=? " << transform_stack_.back().clip_depth;
2088+
current_depth_ = transform_stack_.back().clip_depth;
2089+
}
20872090

20882091
if (IsSkipping()) {
20892092
transform_stack_.pop_back();
@@ -2645,8 +2648,6 @@ std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
26452648
}
26462649
}
26472650

2648-
// Restore any clips that were recorded before the backdrop filter was
2649-
// applied.
26502651
auto& replay_entities = clip_coverage_stack_.GetReplayEntities();
26512652
uint64_t current_depth =
26522653
post_depth_increment ? current_depth_ - 1 : current_depth_;
@@ -2658,7 +2659,7 @@ std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
26582659
SetClipScissor(replay.clip_coverage, current_render_pass,
26592660
global_pass_position);
26602661
if (!replay.clip_contents.Render(renderer_, current_render_pass,
2661-
replay.clip_depth)) {
2662+
replay.clip_depth, replay.transform)) {
26622663
VALIDATION_LOG << "Failed to render entity for clip restore.";
26632664
}
26642665
}

‎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)