Skip to content

Commit b5971ca

Browse files
authored
Add ClipPath optimizations to backdrop clips as well (flutter#189118)
<!-- 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 --> - In `clip_path_layer.cc`, `ApplyClip()` has optimizations to convert to more efficient shapes if applicable. This PR extends that behaviour to backdrop clips as well - We previously didn't add the backdrop clip pushes to the mock embedder. This extends the mock embedder, adds coverage for the existing push backdrop clip methods as well as the various cases for backdrop path clip added in this PR Fixes flutter#188971 ## 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. 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/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 061974f commit b5971ca

7 files changed

Lines changed: 146 additions & 1 deletion

File tree

‎engine/src/flutter/flow/layers/clip_path_layer.cc‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include "flutter/flow/layers/clip_path_layer.h"
6+
#include "flutter/display_list/geometry/dl_geometry_types.h"
67

78
namespace flutter {
89

@@ -33,7 +34,20 @@ void ClipPathLayer::ApplyClip(LayerStateStack::MutatorContext& mutator) const {
3334

3435
void ClipPathLayer::PushClipToEmbeddedNativeViewMutatorStack(
3536
ExternalViewEmbedder* view_embedder) const {
36-
view_embedder->PushClipPathToVisitedPlatformViews(clip_shape());
37+
DlRect rect;
38+
if (clip_shape().IsRect(&rect)) {
39+
view_embedder->PushClipRectToVisitedPlatformViews(rect);
40+
} else if (clip_shape().IsOval(&rect)) {
41+
view_embedder->PushClipRRectToVisitedPlatformViews(
42+
DlRoundRect::MakeOval(rect));
43+
} else {
44+
DlRoundRect rrect;
45+
if (clip_shape().IsRoundRect(&rrect)) {
46+
view_embedder->PushClipRRectToVisitedPlatformViews(rrect);
47+
} else {
48+
view_embedder->PushClipPathToVisitedPlatformViews(clip_shape());
49+
}
50+
}
3751
}
3852

3953
} // namespace flutter

‎engine/src/flutter/flow/layers/clip_path_layer_unittests.cc‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,49 @@ TEST_F(ClipPathLayerTest, EmptyClipDoesNotCullPlatformView) {
707707
EXPECT_EQ(embedder.painted_views(), std::vector<int64_t>({view_id}));
708708
}
709709

710+
// Prerolls a ClipPathLayer with a platform view child and returns the clips
711+
// that the layer pushed to the view embedder.
712+
static std::vector<Mutator> PushedClipsForPath(PrerollContext* context,
713+
const DlPath& clip_path) {
714+
auto platform_view = std::make_shared<PlatformViewLayer>(
715+
DlPoint(0.0f, 0.0f), DlSize(8.0f, 8.0f), 42);
716+
auto clip = std::make_shared<ClipPathLayer>(clip_path, Clip::kHardEdge);
717+
clip->Add(platform_view);
718+
719+
MockViewEmbedder embedder;
720+
context->view_embedder = &embedder;
721+
clip->Preroll(context);
722+
context->view_embedder = nullptr;
723+
return embedder.pushed_clips();
724+
}
725+
726+
TEST_F(ClipPathLayerTest, RectPathPushesClipRectToEmbedder) {
727+
const DlRect rect = DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f);
728+
EXPECT_EQ(PushedClipsForPath(preroll_context(), DlPath::MakeRect(rect)),
729+
std::vector({Mutator(rect)}));
730+
}
731+
732+
TEST_F(ClipPathLayerTest, OvalPathPushesClipRRectToEmbedder) {
733+
const DlRect bounds = DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f);
734+
EXPECT_EQ(PushedClipsForPath(preroll_context(), DlPath::MakeOval(bounds)),
735+
std::vector({Mutator(DlRoundRect::MakeOval(bounds))}));
736+
}
737+
738+
TEST_F(ClipPathLayerTest, RoundRectPathPushesClipRRectToEmbedder) {
739+
const DlRoundRect rrect = DlRoundRect::MakeRectXY(
740+
DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f), 3.0f, 3.0f);
741+
EXPECT_EQ(PushedClipsForPath(preroll_context(), DlPath::MakeRoundRect(rrect)),
742+
std::vector({Mutator(rrect)}));
743+
}
744+
745+
TEST_F(ClipPathLayerTest, ComplexPathPushesClipPathToEmbedder) {
746+
const DlRect bounds = DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f);
747+
const DlPath path =
748+
DlPath::MakeRect(bounds) + DlPath::MakeRect(bounds.Expand(-1.0f, -1.0f));
749+
EXPECT_EQ(PushedClipsForPath(preroll_context(), path),
750+
std::vector({Mutator(path)}));
751+
}
752+
710753
} // namespace testing
711754
} // namespace flutter
712755

‎engine/src/flutter/flow/layers/clip_rect_layer_unittests.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,21 @@ TEST_F(ClipRectLayerTest, EmptyClipDoesNotCullPlatformView) {
545545
EXPECT_EQ(embedder.painted_views(), std::vector<int64_t>({view_id}));
546546
}
547547

548+
TEST_F(ClipRectLayerTest, PushesClipRectToEmbedder) {
549+
auto platform_view = std::make_shared<PlatformViewLayer>(
550+
DlPoint(0.0f, 0.0f), DlSize(8.0f, 8.0f), 42);
551+
const DlRect clip_rect = DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f);
552+
auto clip = std::make_shared<ClipRectLayer>(clip_rect, Clip::kHardEdge);
553+
clip->Add(platform_view);
554+
555+
MockViewEmbedder embedder;
556+
preroll_context()->view_embedder = &embedder;
557+
clip->Preroll(preroll_context());
558+
preroll_context()->view_embedder = nullptr;
559+
560+
EXPECT_EQ(embedder.pushed_clips(), std::vector({Mutator(clip_rect)}));
561+
}
562+
548563
} // namespace testing
549564
} // namespace flutter
550565

‎engine/src/flutter/flow/layers/clip_rrect_layer_unittests.cc‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,22 @@ TEST_F(ClipRRectLayerTest, EmptyClipDoesNotCullPlatformView) {
638638
EXPECT_EQ(embedder.painted_views(), std::vector<int64_t>({view_id}));
639639
}
640640

641+
TEST_F(ClipRRectLayerTest, PushesClipRRectToEmbedder) {
642+
auto platform_view = std::make_shared<PlatformViewLayer>(
643+
DlPoint(0.0f, 0.0f), DlSize(8.0f, 8.0f), 42);
644+
const DlRoundRect clip_rrect = DlRoundRect::MakeRectXY(
645+
DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f), 3.0f, 3.0f);
646+
auto clip = std::make_shared<ClipRRectLayer>(clip_rrect, Clip::kHardEdge);
647+
clip->Add(platform_view);
648+
649+
MockViewEmbedder embedder;
650+
preroll_context()->view_embedder = &embedder;
651+
clip->Preroll(preroll_context());
652+
preroll_context()->view_embedder = nullptr;
653+
654+
EXPECT_EQ(embedder.pushed_clips(), std::vector({Mutator(clip_rrect)}));
655+
}
656+
641657
} // namespace testing
642658
} // namespace flutter
643659

‎engine/src/flutter/flow/layers/clip_rsuperellipse_layer_unittests.cc‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,23 @@ TEST_F(ClipRSuperellipseLayerTest, EmptyClipDoesNotCullPlatformView) {
622622
EXPECT_EQ(embedder.painted_views(), std::vector<int64_t>({view_id}));
623623
}
624624

625+
TEST_F(ClipRSuperellipseLayerTest, PushesClipRSuperellipseToEmbedder) {
626+
auto platform_view = std::make_shared<PlatformViewLayer>(
627+
DlPoint(0.0f, 0.0f), DlSize(8.0f, 8.0f), 42);
628+
const DlRoundSuperellipse clip_rse = DlRoundSuperellipse::MakeRectXY(
629+
DlRect::MakeLTRB(2.0f, 2.0f, 12.0f, 12.0f), 3.0f, 3.0f);
630+
auto clip =
631+
std::make_shared<ClipRSuperellipseLayer>(clip_rse, Clip::kHardEdge);
632+
clip->Add(platform_view);
633+
634+
MockViewEmbedder embedder;
635+
preroll_context()->view_embedder = &embedder;
636+
clip->Preroll(preroll_context());
637+
preroll_context()->view_embedder = nullptr;
638+
639+
EXPECT_EQ(embedder.pushed_clips(), std::vector({Mutator(clip_rse)}));
640+
}
641+
625642
} // namespace testing
626643
} // namespace flutter
627644

‎engine/src/flutter/flow/testing/mock_embedder.cc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,29 @@ DlCanvas* MockViewEmbedder::CompositeEmbeddedView(int64_t view_id) {
4747
return canvas;
4848
}
4949

50+
// |ExternalViewEmbedder|
51+
void MockViewEmbedder::PushClipRectToVisitedPlatformViews(
52+
const DlRect& clip_rect) {
53+
pushed_clips_.emplace_back(clip_rect);
54+
}
55+
56+
// |ExternalViewEmbedder|
57+
void MockViewEmbedder::PushClipRRectToVisitedPlatformViews(
58+
const DlRoundRect& clip_rrect) {
59+
pushed_clips_.emplace_back(clip_rrect);
60+
}
61+
62+
// |ExternalViewEmbedder|
63+
void MockViewEmbedder::PushClipRSuperellipseToVisitedPlatformViews(
64+
const DlRoundSuperellipse& clip_rse) {
65+
pushed_clips_.emplace_back(clip_rse);
66+
}
67+
68+
// |ExternalViewEmbedder|
69+
void MockViewEmbedder::PushClipPathToVisitedPlatformViews(
70+
const DlPath& clip_path) {
71+
pushed_clips_.emplace_back(clip_path);
72+
}
73+
5074
} // namespace testing
5175
} // namespace flutter

‎engine/src/flutter/flow/testing/mock_embedder.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,29 @@ class MockViewEmbedder : public ExternalViewEmbedder {
4141
// |ExternalViewEmbedder|
4242
DlCanvas* CompositeEmbeddedView(int64_t view_id) override;
4343

44+
// |ExternalViewEmbedder|
45+
void PushClipRectToVisitedPlatformViews(const DlRect& clip_rect) override;
46+
47+
// |ExternalViewEmbedder|
48+
void PushClipRRectToVisitedPlatformViews(
49+
const DlRoundRect& clip_rrect) override;
50+
51+
// |ExternalViewEmbedder|
52+
void PushClipRSuperellipseToVisitedPlatformViews(
53+
const DlRoundSuperellipse& clip_rse) override;
54+
55+
// |ExternalViewEmbedder|
56+
void PushClipPathToVisitedPlatformViews(const DlPath& clip_path) override;
57+
4458
std::vector<int64_t> prerolled_views() const { return prerolled_views_; }
4559
std::vector<int64_t> painted_views() const { return painted_views_; }
60+
std::vector<Mutator> pushed_clips() const { return pushed_clips_; }
4661

4762
private:
4863
std::deque<DlCanvas*> contexts_;
4964
std::vector<int64_t> prerolled_views_;
5065
std::vector<int64_t> painted_views_;
66+
std::vector<Mutator> pushed_clips_;
5167
};
5268

5369
} // namespace testing

0 commit comments

Comments
 (0)