Skip to content

Commit 2d424bd

Browse files
authored
Improve non rect platform view rendering (flutter#182662)
<!-- 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 --> Test setup: ```dart return Stack( children: [ Container(color: Colors.blue), // Flutter content behind const Text('Some random text'), ClipRRect( borderRadius: BorderRadius.circular(250), child: const PlatformViewWidget(), // platform view on top - red background with black text on it ), BackdropFilter( filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), child: Center( child: Container( height: 100, width: 100, color: Colors.blueAccent, ), ), // Flutter content infront of the blur ) ], ); ``` <p> Before<br/> <img height="600" alt="image" src="https://github.com/user-attachments/assets/2ea8d99a-73a5-415f-ad63-292f3b9cc12c" /> After<br/> <img height="600" alt="Simulator Screenshot - iPhone 17 Pro - 2026-02-20 at 19 03 15" src="https://github.com/user-attachments/assets/ae515106-6238-474f-9aea-c6589da79bff" /> </p> - Currently the underlay is always clipped by platform view in a rectangular shape. This isn't correct because the platform view can have clips on it such as a rounded rect or a custom path even - The overlay was also always drawn in a rectangular shape - Blurred text can only be part of the underlay because there's no background information in overlay. - When a blured text was in the corner of a backdrop filter containing a platform view, the underlay which contains the blurred text got sliced away and overlay had unblurred text - This PR makes this flow clip aware and handles different clips correctly Definitions: - Underlay: This is the flutter layer drawn below a platform view. When rendering we cut out holes in this layer for where the platform view will be rendered to save on compute - [Overlay](https://docs.google.com/document/d/1V7Jc_RGaknrBBPPBBKB8lT7f3PKhYr8sin35MSMFAf4): This is the flutter layer drawn on top of the area occupied by the platform view. *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* Fixes flutter#150660 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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]. **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 [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 09a7598 commit 2d424bd

6 files changed

Lines changed: 144 additions & 14 deletions

File tree

‎engine/src/flutter/flow/view_slicer.cc‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ std::unordered_map<int64_t, DlRect> SliceViews(
1515
const std::vector<int64_t>& composition_order,
1616
const std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>>&
1717
slices,
18-
const std::unordered_map<int64_t, DlRect>& view_rects) {
18+
const std::unordered_map<int64_t, DlRect>& view_rects,
19+
const std::unordered_set<int64_t>& views_with_underlay_preserved) {
1920
std::unordered_map<int64_t, DlRect> overlay_layers;
2021

2122
auto current_frame_view_count = composition_order.size();
@@ -110,9 +111,17 @@ std::unordered_map<int64_t, DlRect> SliceViews(
110111
if (!full_joined_rect.IsEmpty()) {
111112
overlay_layers.insert({view_id, full_joined_rect});
112113

113-
// Clip the background canvas, so it doesn't contain any of the pixels
114-
// drawn on the overlay layer.
115-
background_canvas->ClipRect(full_joined_rect, DlClipOp::kDifference);
114+
// If overlay has a non-rect clip, we need to preserve the underlay in
115+
// that area to show correctly after overlay is clipped behind the
116+
// platform view
117+
const bool preserve_underlay =
118+
views_with_underlay_preserved.find(view_id) !=
119+
views_with_underlay_preserved.end();
120+
if (!preserve_underlay) {
121+
// Clip the background canvas, so it doesn't contain any of the pixels
122+
// drawn on the overlay layer.
123+
background_canvas->ClipRect(full_joined_rect, DlClipOp::kDifference);
124+
}
116125
}
117126
slice->render_into(background_canvas);
118127
}

‎engine/src/flutter/flow/view_slicer.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
#define FLUTTER_FLOW_VIEW_SLICER_H_
77

88
#include <unordered_map>
9+
#include <unordered_set>
910
#include "display_list/dl_canvas.h"
1011
#include "flow/embedded_views.h"
1112

1213
namespace flutter {
1314

1415
/// @brief Compute the required overlay layers and clip the view slices
1516
/// according to the size and position of the platform views.
17+
/// @param views_with_underlay_preserved The platform view IDs for which we
18+
/// should not subtract the overlap from the background canvas.
1619
std::unordered_map<int64_t, DlRect> SliceViews(
1720
DlCanvas* background_canvas,
1821
const std::vector<int64_t>& composition_order,
1922
const std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>>&
2023
slices,
21-
const std::unordered_map<int64_t, DlRect>& view_rects);
24+
const std::unordered_map<int64_t, DlRect>& view_rects,
25+
const std::unordered_set<int64_t>& views_with_underlay_preserved);
2226

2327
} // namespace flutter
2428

‎engine/src/flutter/flow/view_slicer_unittests.cc‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ namespace flutter {
1212
namespace testing {
1313

1414
namespace {
15+
bool ContainsClipDifferenceRect(const sk_sp<DisplayList>& display_list) {
16+
for (DlIndex i = 0; i < display_list->op_count(); i++) {
17+
if (display_list->GetOpType(i) == DisplayListOpType::kClipDifferenceRect) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
1524
void AddSliceOfSize(
1625
std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>>& slices,
1726
int64_t id,
@@ -34,7 +43,7 @@ TEST(ViewSlicerTest, CanSlicerNonOverlappingViews) {
3443
{1, DlRect::MakeLTRB(50, 50, 60, 60)}};
3544

3645
auto computed_overlays =
37-
SliceViews(&builder, composition_order, slices, view_rects);
46+
SliceViews(&builder, composition_order, slices, view_rects, {});
3847

3948
EXPECT_TRUE(computed_overlays.empty());
4049
}
@@ -50,7 +59,7 @@ TEST(ViewSlicerTest, IgnoresFractionalOverlaps) {
5059
{1, DlRect::MakeLTRB(50.5, 50.5, 100, 100)}};
5160

5261
auto computed_overlays =
53-
SliceViews(&builder, composition_order, slices, view_rects);
62+
SliceViews(&builder, composition_order, slices, view_rects, {});
5463

5564
EXPECT_TRUE(computed_overlays.empty());
5665
}
@@ -66,7 +75,7 @@ TEST(ViewSlicerTest, ComputesOverlapWith1PV) {
6675
{1, DlRect::MakeLTRB(0, 0, 100, 100)}};
6776

6877
auto computed_overlays =
69-
SliceViews(&builder, composition_order, slices, view_rects);
78+
SliceViews(&builder, composition_order, slices, view_rects, {});
7079

7180
EXPECT_EQ(computed_overlays.size(), 1u);
7281
auto overlay = computed_overlays.find(1);
@@ -89,7 +98,7 @@ TEST(ViewSlicerTest, ComputesOverlapWith2PV) {
8998
};
9099

91100
auto computed_overlays =
92-
SliceViews(&builder, composition_order, slices, view_rects);
101+
SliceViews(&builder, composition_order, slices, view_rects, {});
93102

94103
EXPECT_EQ(computed_overlays.size(), 2u);
95104

@@ -123,7 +132,7 @@ TEST(ViewSlicerTest, OverlappingTwoPVs) {
123132
};
124133

125134
auto computed_overlays =
126-
SliceViews(&builder, composition_order, slices, view_rects);
135+
SliceViews(&builder, composition_order, slices, view_rects, {});
127136

128137
EXPECT_EQ(computed_overlays.size(), 1u);
129138

@@ -134,5 +143,33 @@ TEST(ViewSlicerTest, OverlappingTwoPVs) {
134143
EXPECT_EQ(overlay->second, DlRect::MakeLTRB(0, 0, 100, 100));
135144
}
136145

146+
TEST(ViewSlicerTest, PreservesUnderlayForSelectedViews) {
147+
std::vector<int64_t> composition_order = {1};
148+
std::unordered_map<int64_t, DlRect> view_rects = {
149+
{1, DlRect::MakeLTRB(0, 0, 100, 100)}};
150+
151+
std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>>
152+
baseline_slices;
153+
AddSliceOfSize(baseline_slices, 1, DlRect::MakeLTRB(0, 0, 50, 50));
154+
DisplayListBuilder baseline_builder(DlRect::MakeLTRB(0, 0, 100, 100));
155+
auto baseline_overlays = SliceViews(&baseline_builder, composition_order,
156+
baseline_slices, view_rects, {});
157+
EXPECT_EQ(baseline_overlays.size(), 1u);
158+
auto baseline_dl = baseline_builder.Build();
159+
EXPECT_TRUE(ContainsClipDifferenceRect(baseline_dl));
160+
161+
std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>>
162+
preserve_slices;
163+
AddSliceOfSize(preserve_slices, 1, DlRect::MakeLTRB(0, 0, 50, 50));
164+
std::unordered_set<int64_t> views_with_underlay_preserved = {1};
165+
DisplayListBuilder preserve_builder(DlRect::MakeLTRB(0, 0, 100, 100));
166+
auto preserve_overlays =
167+
SliceViews(&preserve_builder, composition_order, preserve_slices,
168+
view_rects, views_with_underlay_preserved);
169+
EXPECT_EQ(preserve_overlays.size(), 1u);
170+
auto preserve_dl = preserve_builder.Build();
171+
EXPECT_FALSE(ContainsClipDifferenceRect(preserve_dl));
172+
}
173+
137174
} // namespace testing
138175
} // namespace flutter

‎engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ void AndroidExternalViewEmbedder::SubmitFlutterView(
8686
SliceViews(frame->Canvas(), //
8787
composition_order_, //
8888
slices_, //
89-
view_rects //
89+
view_rects, //
90+
{} //
9091
);
9192

9293
// Submit the background canvas frame before switching the GL context to

‎engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
9999
SliceViews(frame->Canvas(), //
100100
composition_order_, //
101101
slices_, //
102-
view_rects //
102+
view_rects, //
103+
{} //
103104
);
104105

105106
// If there is no overlay Surface, initialize one on the platform thread. This

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsController.mm‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "impeller/geometry/rounding_radii.h"
88

99
#include "flutter/display_list/effects/image_filters/dl_blur_image_filter.h"
10+
#include "flutter/display_list/geometry/dl_geometry_conversions.h"
1011
#include "flutter/display_list/utils/dl_matrix_clip_tracker.h"
1112
#include "flutter/flow/surface_frame.h"
1213
#include "flutter/flow/view_slicer.h"
@@ -90,6 +91,74 @@ static CGRect GetCGRectFromDlRect(const DlRect& clipDlRect) {
9091
clipDlRect.GetHeight());
9192
}
9293

94+
static bool HasNonRectClipForUnderlayCutout(const flutter::EmbeddedViewParams& params) {
95+
auto iter = params.mutatorsStack().Begin();
96+
while (iter != params.mutatorsStack().End()) {
97+
switch ((*iter)->GetType()) {
98+
case flutter::MutatorType::kClipRRect:
99+
case flutter::MutatorType::kClipRSE:
100+
case flutter::MutatorType::kClipPath:
101+
return true;
102+
default:
103+
break;
104+
}
105+
++iter;
106+
}
107+
return false;
108+
}
109+
110+
// Overlay canvas needs to be clipped to the shape of platform view to ensure
111+
// underlay shows up correctly, so that when there's backdrop filter, the region outside of platform
112+
// view's shape is blurred. See: https://github.com/flutter/flutter/issues/150660
113+
static void ApplyNonRectClipToOverlayCanvas(flutter::DlCanvas* overlay_canvas,
114+
const flutter::EmbeddedViewParams& params) {
115+
flutter::DlMatrix transform;
116+
auto iter = params.mutatorsStack().Begin();
117+
while (iter != params.mutatorsStack().End()) {
118+
switch ((*iter)->GetType()) {
119+
case flutter::MutatorType::kTransform:
120+
transform = transform * (*iter)->GetMatrix();
121+
break;
122+
case flutter::MutatorType::kClipRRect: {
123+
if (transform.IsIdentity()) {
124+
overlay_canvas->ClipRoundRect((*iter)->GetRRect(), flutter::DlClipOp::kIntersect, true);
125+
} else {
126+
auto path = flutter::DlPath::MakeRoundRect((*iter)->GetRRect());
127+
auto transformed_path =
128+
flutter::DlPath(path.GetSkPath().makeTransform(flutter::ToSkMatrix(transform)));
129+
overlay_canvas->ClipPath(transformed_path, flutter::DlClipOp::kIntersect, true);
130+
}
131+
break;
132+
}
133+
case flutter::MutatorType::kClipRSE: {
134+
if (transform.IsIdentity()) {
135+
overlay_canvas->ClipRoundSuperellipse((*iter)->GetRSE(), flutter::DlClipOp::kIntersect,
136+
true);
137+
} else {
138+
auto path = flutter::DlPath::MakeRoundSuperellipse((*iter)->GetRSE());
139+
auto transformed_path =
140+
flutter::DlPath(path.GetSkPath().makeTransform(flutter::ToSkMatrix(transform)));
141+
overlay_canvas->ClipPath(transformed_path, flutter::DlClipOp::kIntersect, true);
142+
}
143+
break;
144+
}
145+
case flutter::MutatorType::kClipPath: {
146+
if (transform.IsIdentity()) {
147+
overlay_canvas->ClipPath((*iter)->GetPath(), flutter::DlClipOp::kIntersect, true);
148+
} else {
149+
auto transformed_path = flutter::DlPath(
150+
(*iter)->GetPath().GetSkPath().makeTransform(flutter::ToSkMatrix(transform)));
151+
overlay_canvas->ClipPath(transformed_path, flutter::DlClipOp::kIntersect, true);
152+
}
153+
break;
154+
}
155+
default:
156+
break;
157+
}
158+
++iter;
159+
}
160+
}
161+
93162
@interface FlutterPlatformViewsController ()
94163

95164
// The pool of reusable view layers. The pool allows to recycle layer in each frame.
@@ -776,13 +845,19 @@ - (BOOL)submitFrame:(std::unique_ptr<flutter::SurfaceFrame>)background_frame
776845
std::vector<std::unique_ptr<flutter::SurfaceFrame>> surfaceFrames;
777846
surfaceFrames.reserve(self.compositionOrder.size());
778847
std::unordered_map<int64_t, DlRect> viewRects;
848+
std::unordered_set<int64_t> viewsWithUnderlayPreserved;
779849

780850
for (int64_t viewId : self.compositionOrder) {
781-
viewRects[viewId] = self.currentCompositionParams[viewId].finalBoundingRect();
851+
const flutter::EmbeddedViewParams& params = self.currentCompositionParams[viewId];
852+
viewRects[viewId] = params.finalBoundingRect();
853+
if (HasNonRectClipForUnderlayCutout(params)) {
854+
viewsWithUnderlayPreserved.insert(viewId);
855+
}
782856
}
783857

784858
std::unordered_map<int64_t, DlRect> overlayLayers =
785-
SliceViews(background_frame->Canvas(), self.compositionOrder, self.slices, viewRects);
859+
SliceViews(background_frame->Canvas(), self.compositionOrder, self.slices, viewRects,
860+
viewsWithUnderlayPreserved);
786861

787862
size_t requiredOverlayLayers = 0;
788863
for (int64_t viewId : self.compositionOrder) {
@@ -818,6 +893,9 @@ - (BOOL)submitFrame:(std::unique_ptr<flutter::SurfaceFrame>)background_frame
818893
int restoreCount = overlayCanvas->GetSaveCount();
819894
overlayCanvas->Save();
820895
overlayCanvas->ClipRect(overlay->second);
896+
if (viewsWithUnderlayPreserved.find(viewId) != viewsWithUnderlayPreserved.end()) {
897+
ApplyNonRectClipToOverlayCanvas(overlayCanvas, self.currentCompositionParams[viewId]);
898+
}
821899
overlayCanvas->Clear(flutter::DlColor::kTransparent());
822900
self.slices[viewId]->render_into(overlayCanvas);
823901
overlayCanvas->RestoreToCount(restoreCount);

0 commit comments

Comments
 (0)