Skip to content

Commit b04c8a7

Browse files
committed
Fix square corner issue
- fix clip replay pop behavior - fix depth max mismatch - add tests
1 parent 5b9af9a commit b04c8a7

9 files changed

Lines changed: 307 additions & 35 deletions

File tree

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,11 @@ void Canvas::Save(uint32_t total_content_depth) {
16961696

16971697
auto entry = CanvasStackEntry{};
16981698
entry.transform = transform_stack_.back().transform;
1699-
entry.clip_depth = current_depth_ + total_content_depth;
1699+
entry.clip_depth =
1700+
(total_content_depth == kMaxDepth)
1701+
? transform_stack_.back().clip_depth
1702+
: std::min<uint32_t>(current_depth_ + total_content_depth,
1703+
transform_stack_.back().clip_depth);
17001704
entry.distributed_opacity = transform_stack_.back().distributed_opacity;
17011705
FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
17021706
<< entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
@@ -1861,9 +1865,9 @@ void Canvas::SaveLayer(const Paint& paint,
18611865
// 1. The device supports framebuffer fetch
18621866
// 2. There are no more backdrop filters
18631867
// 3. The current render pass is for the onscreen pass.
1864-
const bool should_use_onscreen =
1868+
const bool should_use_onscreen = override_should_use_onscreen_.value_or(
18651869
renderer_.GetDeviceCapabilities().SupportsFramebufferFetch() &&
1866-
backdrop_count_ == 0 && render_passes_.size() == 1u;
1870+
backdrop_count_ == 0 && render_passes_.size() == 1u);
18671871
input_texture = FlipBackdrop(
18681872
GetGlobalPassPosition(), //
18691873
/*should_remove_texture=*/will_cache_backdrop_texture, //
@@ -1946,7 +1950,11 @@ void Canvas::SaveLayer(const Paint& paint,
19461950

19471951
CanvasStackEntry entry;
19481952
entry.transform = transform_stack_.back().transform;
1949-
entry.clip_depth = current_depth_ + total_content_depth;
1953+
entry.clip_depth =
1954+
(total_content_depth == kMaxDepth)
1955+
? transform_stack_.back().clip_depth
1956+
: std::min<uint32_t>(current_depth_ + total_content_depth,
1957+
transform_stack_.back().clip_depth);
19501958
FML_DCHECK(entry.clip_depth <= transform_stack_.back().clip_depth)
19511959
<< entry.clip_depth << " <=? " << transform_stack_.back().clip_depth
19521960
<< " after allocating " << total_content_depth;
@@ -1994,9 +2002,12 @@ bool Canvas::Restore() {
19942002
// to be overly conservative, but we need to jump the depth to
19952003
// the clip depth so that the next rendering op will get a
19962004
// larger depth (it will pre-increment the current_depth_ value).
1997-
FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
1998-
<< current_depth_ << " <=? " << transform_stack_.back().clip_depth;
1999-
current_depth_ = transform_stack_.back().clip_depth;
2005+
if (transform_stack_.back().num_clips > 0 &&
2006+
transform_stack_.back().clip_depth < kMaxDepth) {
2007+
FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth)
2008+
<< current_depth_ << " <=? " << transform_stack_.back().clip_depth;
2009+
current_depth_ = transform_stack_.back().clip_depth;
2010+
}
20002011

20012012
if (IsSkipping()) {
20022013
transform_stack_.pop_back();
@@ -2087,6 +2098,13 @@ bool Canvas::Restore() {
20872098
);
20882099
clip_coverage_stack_.PopSubpass();
20892100
transform_stack_.pop_back();
2101+
if (!transform_stack_.empty()) {
2102+
CanvasStackEntry& entry = transform_stack_.back();
2103+
if (entry.pre_split_clip_depth.has_value()) {
2104+
entry.clip_depth = entry.pre_split_clip_depth.value();
2105+
entry.pre_split_clip_depth = std::nullopt;
2106+
}
2107+
}
20902108

20912109
// We don't need to restore clips if a saveLayer was performed, as the clip
20922110
// state is per render target, and no more rendering operations will be
@@ -2096,6 +2114,13 @@ bool Canvas::Restore() {
20962114

20972115
size_t num_clips = transform_stack_.back().num_clips;
20982116
transform_stack_.pop_back();
2117+
if (!transform_stack_.empty()) {
2118+
CanvasStackEntry& entry = transform_stack_.back();
2119+
if (entry.pre_split_clip_depth.has_value()) {
2120+
entry.clip_depth = entry.pre_split_clip_depth.value();
2121+
entry.pre_split_clip_depth = std::nullopt;
2122+
}
2123+
}
20992124

21002125
if (num_clips > 0) {
21012126
EntityPassClipStack::ClipStateResult clip_state_result =
@@ -2540,20 +2565,19 @@ std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
25402565
return nullptr;
25412566
}
25422567

2543-
// Restore any clips that were recorded before the backdrop filter was
2544-
// applied.
25452568
auto& replay_entities = clip_coverage_stack_.GetReplayEntities();
2546-
uint64_t current_depth =
2547-
post_depth_increment ? current_depth_ - 1 : current_depth_;
2548-
for (const auto& replay : replay_entities) {
2549-
if (replay.clip_depth <= current_depth) {
2550-
continue;
2569+
current_depth_ = 0;
2570+
for (auto& entry : transform_stack_) {
2571+
if (!entry.pre_split_clip_depth.has_value()) {
2572+
entry.pre_split_clip_depth = entry.clip_depth;
25512573
}
2552-
2574+
entry.clip_depth = kMaxDepth;
2575+
}
2576+
for (const auto& replay : replay_entities) {
25532577
SetClipScissor(replay.clip_coverage, current_render_pass,
25542578
global_pass_position);
2555-
if (!replay.clip_contents.Render(renderer_, current_render_pass,
2556-
replay.clip_depth)) {
2579+
if (!replay.clip_contents.Render(renderer_, current_render_pass, kMaxDepth,
2580+
replay.transform)) {
25572581
VALIDATION_LOG << "Failed to render entity for clip restore.";
25582582
}
25592583
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ struct CanvasStackEntry {
5656
size_t num_clips = 0u;
5757
Scalar distributed_opacity = 1.0f;
5858
Entity::RenderingMode rendering_mode = Entity::RenderingMode::kDirect;
59+
// The clip depth before an onscreen FlipBackdrop pass split, if any.
60+
std::optional<uint32_t> pre_split_clip_depth;
5961
// Whether all entities in the current save should be skipped.
6062
bool skipping = false;
6163
// Whether subpass coverage was rounded out to pixel coverage, or if false
@@ -118,7 +120,7 @@ class LazyRenderingConfig {
118120

119121
class Canvas {
120122
public:
121-
static constexpr uint32_t kMaxDepth = 1 << 24;
123+
static constexpr uint32_t kMaxDepth = 262143;
122124

123125
Canvas(ContentContext& renderer,
124126
const RenderTarget& render_target,
@@ -283,6 +285,17 @@ class Canvas {
283285
/// Visible for testing.
284286
static bool IsCompatibleWithSDFRendering(const Paint& paint);
285287

288+
/// Visible for testing.
289+
static void SetOverrideShouldUseOnscreenForTesting(
290+
std::optional<bool> override_value) {
291+
override_should_use_onscreen_ = override_value;
292+
}
293+
294+
private:
295+
inline static thread_local std::optional<bool> override_should_use_onscreen_;
296+
297+
public:
298+
286299
private:
287300
class BlurShape {
288301
public:

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,5 +557,107 @@ TEST(CanvasTest, AntialiasedPaintCompatibleWithSDFRendering) {
557557
EXPECT_TRUE(Canvas::IsCompatibleWithSDFRendering(paint));
558558
}
559559

560+
TEST_P(AiksTest, ClipDepthMaintainedAcrossBackdropFilterAndLayers) {
561+
ContentContext& context = GetContentContext();
562+
if (!context.GetDeviceCapabilities().SupportsFramebufferFetch()) {
563+
GTEST_SKIP() << "Test requires device with framebuffer fetch";
564+
}
565+
566+
struct ScopedOnscreenOverride {
567+
ScopedOnscreenOverride() {
568+
Canvas::SetOverrideShouldUseOnscreenForTesting(true);
569+
}
570+
~ScopedOnscreenOverride() {
571+
Canvas::SetOverrideShouldUseOnscreenForTesting(std::nullopt);
572+
}
573+
} scoped_override;
574+
575+
auto canvas = CreateTestCanvas(context, Rect::MakeLTRB(0, 0, 800, 600),
576+
/*requires_readback=*/true);
577+
578+
// 1. Root route saves and applies ClipRoundSuperellipse.
579+
canvas->Save(/*total_content_depth=*/20);
580+
Rect sheet_rect = Rect::MakeXYWH(0, 42, 400, 500);
581+
canvas->ClipGeometry(*Geometry::MakeRoundSuperellipse(sheet_rect, 12.0f),
582+
Entity::ClipOperation::kIntersect);
583+
584+
uint64_t initial_clip_depth = canvas->GetMaxOpDepth();
585+
586+
// 2. Child layer (body Ink items) renders and restores.
587+
canvas->Save(/*total_content_depth=*/5);
588+
for (int i = 0; i < 5; i++) {
589+
canvas->DrawRect(Rect::MakeXYWH(0, i * 50, 400, 45),
590+
Paint{.color = Color::Azure()});
591+
}
592+
canvas->Restore();
593+
594+
// 3. Child layer (_glassBar BackdropFilter) renders and restores.
595+
auto blur = flutter::DlImageFilter::MakeBlur(12.0f, 12.0f,
596+
flutter::DlTileMode::kClamp);
597+
Rect glass_rect = Rect::MakeXYWH(20, 350, 360, 60);
598+
canvas->SaveLayer(Paint{}, glass_rect, blur.get(),
599+
ContentBoundsPromise::kContainsContents,
600+
/*total_content_depth=*/2);
601+
canvas->DrawRect(glass_rect, Paint{.color = Color::Orange()});
602+
canvas->Restore();
603+
604+
// 4. Next layer (AppBar header) renders.
605+
// Invariant: Subsequent draw operations must NOT exceed the active clip's
606+
// depth, and depth must stay within the precision bounds of kMaxDepth.
607+
EXPECT_LE(initial_clip_depth, Canvas::kMaxDepth);
608+
canvas->Save(/*total_content_depth=*/2);
609+
canvas->DrawRect(Rect::MakeXYWH(0, 0, 400, 80),
610+
Paint{.color = Color::White()});
611+
EXPECT_LE(canvas->GetOpDepth(), initial_clip_depth);
612+
EXPECT_LE(canvas->GetOpDepth(), Canvas::kMaxDepth);
613+
canvas->Restore();
614+
615+
canvas->Restore();
616+
}
617+
618+
TEST_P(AiksTest, ParentClipDepthBudgetPreservedAfterBackdropLayerRestore) {
619+
ContentContext& context = GetContentContext();
620+
if (!context.GetDeviceCapabilities().SupportsFramebufferFetch()) {
621+
GTEST_SKIP() << "Test requires device with framebuffer fetch";
622+
}
623+
624+
struct ScopedOnscreenOverride {
625+
ScopedOnscreenOverride() {
626+
Canvas::SetOverrideShouldUseOnscreenForTesting(true);
627+
}
628+
~ScopedOnscreenOverride() {
629+
Canvas::SetOverrideShouldUseOnscreenForTesting(std::nullopt);
630+
}
631+
} scoped_override;
632+
633+
auto canvas = CreateTestCanvas(context, Rect::MakeLTRB(0, 0, 800, 600),
634+
/*requires_readback=*/true);
635+
636+
// 1. Root pass with finite clip depth budget.
637+
canvas->Save(/*total_content_depth=*/50);
638+
Rect sheet_rect = Rect::MakeXYWH(0, 42, 400, 500);
639+
canvas->ClipGeometry(*Geometry::MakeRoundSuperellipse(sheet_rect, 12.0f),
640+
Entity::ClipOperation::kIntersect);
641+
642+
uint64_t parent_clip_depth = canvas->GetMaxOpDepth();
643+
EXPECT_LT(parent_clip_depth, Canvas::kMaxDepth);
644+
645+
// 2. BackdropFilter save layer executes FlipBackdrop.
646+
auto blur = flutter::DlImageFilter::MakeBlur(12.0f, 12.0f,
647+
flutter::DlTileMode::kClamp);
648+
Rect glass_rect = Rect::MakeXYWH(20, 350, 360, 60);
649+
canvas->SaveLayer(Paint{}, glass_rect, blur.get(),
650+
ContentBoundsPromise::kContainsContents,
651+
/*total_content_depth=*/2);
652+
canvas->DrawRect(glass_rect, Paint{.color = Color::Orange()});
653+
canvas->Restore();
654+
655+
// 3. Verify that parent scope clip_depth was NOT permanently overwritten with
656+
// kMaxDepth.
657+
EXPECT_EQ(canvas->GetMaxOpDepth(), parent_clip_depth);
658+
659+
canvas->Restore();
660+
}
661+
560662
} // namespace testing
561663
} // namespace impeller

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "display_list/geometry/dl_geometry_types.h"
1212
#include "display_list/geometry/dl_path_builder.h"
1313
#include "flutter/display_list/dl_builder.h"
14+
#include "flutter/impeller/display_list/canvas.h"
1415
#include "flutter/impeller/display_list/testing/render_text_in_canvas.h"
1516
#include "flutter/impeller/display_list/testing/rmse.h"
1617
#include "flutter/testing/testing.h"
@@ -650,5 +651,89 @@ TEST_P(DlGoldenTest, SubpixelScaledTranslated) {
650651
EXPECT_EQ(intensity[4].x - intensity[0].x, 1);
651652
}
652653

654+
TEST_P(DlGoldenTest, CanRenderClippedBackdropFilterWithSuperellipse) {
655+
struct ScopedOnscreenOverride {
656+
ScopedOnscreenOverride() {
657+
impeller::Canvas::SetOverrideShouldUseOnscreenForTesting(true);
658+
}
659+
~ScopedOnscreenOverride() {
660+
impeller::Canvas::SetOverrideShouldUseOnscreenForTesting(std::nullopt);
661+
}
662+
} scoped_override;
663+
664+
DisplayListBuilder root_builder;
665+
666+
root_builder.Scale(GetContentScale().x, GetContentScale().y);
667+
668+
// 1. Draw a dark background on the root pass.
669+
DlPaint bg_paint;
670+
bg_paint.setColor(DlColor::kBlack());
671+
root_builder.DrawPaint(bg_paint);
672+
673+
// 2. Wrap in TransformLayer (matching CupertinoSheetTransition scale +
674+
// slide).
675+
root_builder.Save();
676+
root_builder.Scale(0.9f, 0.9f);
677+
root_builder.Translate(33.4f, 3.5f);
678+
679+
// 3. Apply ClipRoundSuperellipse with top-only rounded corners (matching
680+
// CupertinoSheet).
681+
DlRect sheet_rect = DlRect::MakeXYWH(0, 42, 400, 500);
682+
DlRoundingRadii top_radii = {
683+
.top_left = {12, 12},
684+
.top_right = {12, 12},
685+
.bottom_left = {0, 0},
686+
.bottom_right = {0, 0},
687+
};
688+
DlRoundSuperellipse clip_rse =
689+
DlRoundSuperellipse::MakeRectRadii(sheet_rect, top_radii);
690+
root_builder.ClipRoundSuperellipse(clip_rse, DlClipOp::kIntersect);
691+
692+
// 4. Wrap in OpacityLayer offset (matching CupertinoSheetRoute offset: (0,
693+
// 42)).
694+
root_builder.Save();
695+
root_builder.Translate(0.0f, 42.0f);
696+
697+
// 5. Layer 1: DisplayListLayer for sheet body with Ink features.
698+
for (int i = 0; i < 5; i++) {
699+
DisplayListBuilder item_builder;
700+
DlPaint ink_paint;
701+
ink_paint.setColor(DlColor::RGBA(0.0f, 0.0f, 0.0f, 0.07f));
702+
item_builder.DrawRect(DlRect::MakeXYWH(0, i * 50, 400, 45), ink_paint);
703+
root_builder.DrawDisplayList(item_builder.Build());
704+
}
705+
706+
// 6. Layer 2: BackdropFilterLayer (_glassBar).
707+
root_builder.Save();
708+
DlRect glass_rect = DlRect::MakeXYWH(20, 350, 360, 60);
709+
DlRoundRect glass_rrect = DlRoundRect::MakeRectXY(glass_rect, 14, 14);
710+
root_builder.ClipRoundRect(glass_rrect, DlClipOp::kIntersect);
711+
712+
DlPaint save_paint;
713+
auto backdrop_filter =
714+
DlImageFilter::MakeBlur(12.0f, 12.0f, DlTileMode::kClamp);
715+
root_builder.SaveLayer(glass_rect, &save_paint, backdrop_filter.get());
716+
DisplayListBuilder glass_builder;
717+
DlPaint orange_paint;
718+
orange_paint.setColor(DlColor::RGBA(1.0f, 0.6f, 0.0f, 0.6f));
719+
glass_builder.DrawRect(glass_rect, orange_paint);
720+
root_builder.DrawDisplayList(glass_builder.Build());
721+
root_builder.Restore(); // Restore SaveLayer
722+
root_builder.Restore(); // Restore glass clip
723+
724+
// 7. Layer 3: DisplayListLayer for Scaffold AppBar / header drawn AFTER
725+
// backdrop filter.
726+
DisplayListBuilder appbar_builder;
727+
DlPaint white_paint;
728+
white_paint.setColor(DlColor::kWhite());
729+
appbar_builder.DrawRect(DlRect::MakeXYWH(0, 0, 400, 80), white_paint);
730+
root_builder.DrawDisplayList(appbar_builder.Build());
731+
732+
root_builder.Restore(); // Restore OpacityLayer offset
733+
root_builder.Restore(); // Restore TransformLayer
734+
735+
ASSERT_TRUE(OpenPlaygroundHere(root_builder.Build()));
736+
}
737+
653738
} // namespace testing
654739
} // namespace flutter

0 commit comments

Comments
 (0)