From 5e39c6983119e36b9dbe9907a8cd41d9d5357c69 Mon Sep 17 00:00:00 2001 From: Jackson Gardner Date: Mon, 19 Aug 2024 14:45:07 -0700 Subject: [PATCH] [skwasm] Fix skwasm clip coverage algorithm. (flutter/engine#54572) This previously was basically assuming that two clip regions would be a union but instead it's actually an intersection. Also, for some reason I had forgotten to actually implement `boundingRect` for the rrect clip region. I changed this for a bit more clarity. --- .../lib/web_ui/lib/src/engine/layers.dart | 97 +++++++++---------- .../web_ui/test/ui/platform_view_test.dart | 35 +++++++ 2 files changed, 80 insertions(+), 52 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/layers.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/layers.dart index f082f58b25..6f7a67450f 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/layers.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/layers.dart @@ -543,12 +543,23 @@ class PlatformViewStyling { sealed class PlatformViewClip { PlatformViewClip positioned(PlatformViewPosition position); - ui.Rect get boundingRect; + /// The largest rectangle that is entirely inside the clip region. All + /// inside of this region is unclipped. + ui.Rect get innerRect; - bool covers(ui.Rect rect); + /// The bounding rectangle of the clip region. All content outside of this + /// region is clipped. + ui.Rect get outerRect; ScenePath get toPath; + static bool rectCovers(ui.Rect covering, ui.Rect covered) { + return covering.left <= covered.left && + covering.right >= covered.right && + covering.top <= covered.top && + covering.bottom >= covered.bottom; + } + static PlatformViewClip combine(PlatformViewClip outer, PlatformViewClip inner) { if (outer is PlatformViewNoClip) { return inner; @@ -557,18 +568,17 @@ sealed class PlatformViewClip { return outer; } - if (outer.covers(inner.boundingRect)) { - return outer; - } - - if (inner.covers(outer.boundingRect)) { + if (rectCovers(outer.innerRect, inner.outerRect)) { return inner; } - final ScenePath path = ui.Path() as ScenePath; - path.addPath(outer.toPath, ui.Offset.zero); - path.addPath(inner.toPath, ui.Offset.zero); - return PlatformViewPathClip(path); + if (rectCovers(inner.innerRect, outer.outerRect)) { + return outer; + } + + return PlatformViewPathClip( + ui.Path.combine(ui.PathOperation.intersect, outer.toPath, inner.toPath) as ScenePath + ); } } @@ -583,14 +593,6 @@ class PlatformViewNoClip implements PlatformViewClip { @override ScenePath get toPath => ui.Path() as ScenePath; - @override - ui.Rect get boundingRect => ui.Rect.zero; - - @override - bool covers(ui.Rect rect) { - return false; - } - @override bool operator ==(Object other) { return identical(this, other) || (other.runtimeType == PlatformViewNoClip); @@ -598,6 +600,12 @@ class PlatformViewNoClip implements PlatformViewClip { @override int get hashCode => runtimeType.hashCode; + + @override + ui.Rect get innerRect => ui.Rect.zero; + + @override + ui.Rect get outerRect => ui.Rect.zero; } class PlatformViewRectClip implements PlatformViewClip { @@ -621,17 +629,6 @@ class PlatformViewRectClip implements PlatformViewClip { @override ScenePath get toPath => (ui.Path() as ScenePath)..addRect(rect); - @override - ui.Rect get boundingRect => rect; - - @override - bool covers(ui.Rect other) { - return rect.left <= other.left && - rect.right >= other.right && - rect.top <= other.top && - rect.bottom >= other.bottom; - } - @override bool operator ==(Object other) { if (identical(this, other)) { @@ -642,6 +639,12 @@ class PlatformViewRectClip implements PlatformViewClip { @override int get hashCode => Object.hash(runtimeType, rect); + + @override + ui.Rect get innerRect => rect; + + @override + ui.Rect get outerRect => rect; } class PlatformViewRRectClip implements PlatformViewClip { @@ -665,18 +668,6 @@ class PlatformViewRRectClip implements PlatformViewClip { @override ScenePath get toPath => (ui.Path() as ScenePath)..addRRect(rrect); - @override - ui.Rect get boundingRect => throw UnimplementedError(); - - @override - bool covers(ui.Rect other) { - final ui.Rect rect = rrect.safeInnerRect; - return rect.left <= other.left && - rect.right >= other.right && - rect.top <= other.top && - rect.bottom >= other.bottom; - } - @override bool operator ==(Object other) { if (identical(this, other)) { @@ -687,6 +678,12 @@ class PlatformViewRRectClip implements PlatformViewClip { @override int get hashCode => Object.hash(runtimeType, rrect); + + @override + ui.Rect get innerRect => rrect.safeInnerRect; + + @override + ui.Rect get outerRect => rrect.outerRect; } class PlatformViewPathClip implements PlatformViewClip { @@ -711,16 +708,6 @@ class PlatformViewPathClip implements PlatformViewClip { @override ScenePath get toPath => path; - @override - ui.Rect get boundingRect => path.getBounds(); - - @override - bool covers(ui.Rect rect) { - // There is no way of determining if an arbitrary path fully covers a rectangle - // so we always conservatively return false here. - return false; - } - @override bool operator ==(Object other) { if (identical(this, other)) { @@ -731,6 +718,12 @@ class PlatformViewPathClip implements PlatformViewClip { @override int get hashCode => Object.hash(runtimeType, path); + + @override + ui.Rect get innerRect => ui.Rect.zero; + + @override + ui.Rect get outerRect => path.getBounds(); } class LayerBuilder { diff --git a/engine/src/flutter/lib/web_ui/test/ui/platform_view_test.dart b/engine/src/flutter/lib/web_ui/test/ui/platform_view_test.dart index 144d1e4369..d18b5d8975 100644 --- a/engine/src/flutter/lib/web_ui/test/ui/platform_view_test.dart +++ b/engine/src/flutter/lib/web_ui/test/ui/platform_view_test.dart @@ -298,6 +298,41 @@ Future testMain() async { await matchGoldenFile('platformview_cliprrect.png', region: region); }); + test('platformview covered clip', () async { + await _createPlatformView(1, platformViewType); + + final ui.PictureRecorder recorder = ui.PictureRecorder(); + final ui.Canvas canvas = ui.Canvas(recorder); + canvas.drawCircle( + const ui.Offset(50, 50), + 50, + ui.Paint() + ..style = ui.PaintingStyle.fill + ..color = const ui.Color(0xFFFF0000) + ); + + final ui.SceneBuilder sb = ui.SceneBuilder(); + sb.pushOffset(50, 50); + + // The rrect should completely cover the rect for this test case. + sb.pushClipRRect( + const ui.RRect.fromLTRBXY(50, 50, 110, 110, 5, 10), + clipBehavior: ui.Clip.antiAlias + ); + sb.pushClipRect(const ui.Rect.fromLTRB(60, 60, 100, 100)); + + sb.addPicture(const ui.Offset(50, 50), recorder.endRecording()); + sb.addPlatformView( + 1, + offset: const ui.Offset(75, 75), + width: 50, + height: 50, + ); + await renderScene(sb.build()); + + await matchGoldenFile('platformview_covered_clip.png', region: region); + }); + test('platformview clippath', () async { await _createPlatformView(1, platformViewType);