From e929c03b5425d187690ecf07e7e7355f9d8a1be5 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Wed, 26 Mar 2025 21:30:50 -0700 Subject: [PATCH] [Engine] RSuperellipse.contains that reuses C++ implementation (#164857) This PR implements `RSuperellipse.contains`. Different from the previous attempt https://github.com/flutter/flutter/pull/164517, this PR leverages the existing C++ implementation via FFI. The biggest outcome of this PR is to enable future work that generates RSE paths in the framework. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [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 --- .../geometry/round_superellipse_unittests.cc | 6 +- engine/src/flutter/lib/ui/compositing.dart | 10 +- .../lib/ui/compositing/scene_builder.cc | 4 +- .../lib/ui/compositing/scene_builder.h | 2 +- engine/src/flutter/lib/ui/dart_ui.cc | 2 + engine/src/flutter/lib/ui/geometry.dart | 135 ++++++++++++----- engine/src/flutter/lib/ui/painting.dart | 30 ++-- engine/src/flutter/lib/ui/painting/canvas.cc | 8 +- engine/src/flutter/lib/ui/painting/canvas.h | 4 +- .../flutter/lib/ui/painting/rsuperellipse.cc | 98 ++++++++----- .../flutter/lib/ui/painting/rsuperellipse.h | 63 ++++---- engine/src/flutter/lib/web_ui/lib/canvas.dart | 4 +- .../flutter/lib/web_ui/lib/compositing.dart | 2 +- .../src/flutter/lib/web_ui/lib/geometry.dart | 9 ++ .../lib/src/engine/canvaskit/canvas.dart | 10 +- .../engine/canvaskit/canvaskit_canvas.dart | 16 +-- .../engine/canvaskit/layer_scene_builder.dart | 6 +- .../src/engine/canvaskit/layer_visitor.dart | 4 +- .../lib/web_ui/lib/src/engine/layers.dart | 42 +++--- .../web_ui/lib/src/engine/scene_builder.dart | 4 +- .../src/engine/skwasm/skwasm_impl/canvas.dart | 12 +- .../lib/web_ui/lib/src/engine/validators.dart | 7 +- .../test/engine/scene_builder_utils.dart | 4 +- .../flutter/testing/dart/geometry_test.dart | 136 ++++++++++++++++++ 24 files changed, 441 insertions(+), 177 deletions(-) diff --git a/engine/src/flutter/impeller/geometry/round_superellipse_unittests.cc b/engine/src/flutter/impeller/geometry/round_superellipse_unittests.cc index 2c87d5e4f7..86324be1f4 100644 --- a/engine/src/flutter/impeller/geometry/round_superellipse_unittests.cc +++ b/engine/src/flutter/impeller/geometry/round_superellipse_unittests.cc @@ -549,7 +549,8 @@ TEST(RoundSuperellipseTest, ContractAndRequireRadiiAdjustment) { TEST(RoundSuperellipseTest, NoCornerRoundSuperellipseContains) { Rect bounds = Rect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f); - // RRect of bounds with no corners contains corners just barely + // Rounded superellipses of bounds with no corners contains corners just + // barely. auto no_corners = RoundSuperellipse::MakeRectRadii( bounds, RoundingRadii::MakeRadii({0.0f, 0.0f})); @@ -571,7 +572,8 @@ TEST(RoundSuperellipseTest, NoCornerRoundSuperellipseContains) { TEST(RoundSuperellipseTest, TinyCornerContains) { Rect bounds = Rect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f); - // RRect of bounds with even the tiniest corners does not contain corners + // Rounded superellipses of bounds with even the tiniest corners does not + // contain corners. auto tiny_corners = RoundSuperellipse::MakeRectRadii( bounds, RoundingRadii::MakeRadii({0.01f, 0.01f})); diff --git a/engine/src/flutter/lib/ui/compositing.dart b/engine/src/flutter/lib/ui/compositing.dart index 4701a15477..ea04b57527 100644 --- a/engine/src/flutter/lib/ui/compositing.dart +++ b/engine/src/flutter/lib/ui/compositing.dart @@ -345,7 +345,7 @@ abstract class SceneBuilder { /// See [pop] for details about the operation stack, and [Clip] for different clip modes. /// By default, the clip will be anti-aliased (clip = [Clip.antiAlias]). ClipRSuperellipseEngineLayer pushClipRSuperellipse( - RSuperellipse rse, { + RSuperellipse rsuperellipse, { Clip clipBehavior = Clip.antiAlias, ClipRSuperellipseEngineLayer? oldLayer, }); @@ -747,7 +747,7 @@ base class _NativeSceneBuilder extends NativeFieldWrapperClass1 implements Scene @override ClipRSuperellipseEngineLayer pushClipRSuperellipse( - RSuperellipse rse, { + RSuperellipse rsuperellipse, { Clip clipBehavior = Clip.antiAlias, ClipRSuperellipseEngineLayer? oldLayer, }) { @@ -756,7 +756,7 @@ base class _NativeSceneBuilder extends NativeFieldWrapperClass1 implements Scene final EngineLayer engineLayer = _NativeEngineLayer._(); _pushClipRSuperellipse( engineLayer, - rse._getValue32(), + rsuperellipse._native(), clipBehavior.index, oldLayer?._nativeLayer, ); @@ -765,12 +765,12 @@ base class _NativeSceneBuilder extends NativeFieldWrapperClass1 implements Scene return layer; } - @Native, Handle, Handle, Int32, Handle)>( + @Native, Handle, Pointer, Int32, Handle)>( symbol: 'SceneBuilder::pushClipRSuperellipse', ) external void _pushClipRSuperellipse( EngineLayer layer, - Float32List rrect, + _NativeRSuperellipse rsuperellipseParam, int clipBehavior, EngineLayer? oldLayer, ); diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.cc b/engine/src/flutter/lib/ui/compositing/scene_builder.cc index 0c165ec1b0..bbed17a69a 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.cc +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.cc @@ -104,11 +104,11 @@ void SceneBuilder::pushClipRRect(Dart_Handle layer_handle, void SceneBuilder::pushClipRSuperellipse( Dart_Handle layer_handle, - const RSuperellipse& rse, + const RSuperellipse* rse, int clip_behavior, const fml::RefPtr& old_layer) { auto layer = std::make_shared( - rse.rsuperellipse, static_cast(clip_behavior)); + rse->rsuperellipse(), static_cast(clip_behavior)); PushLayer(layer); EngineLayer::MakeRetained(layer_handle, layer); diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.h b/engine/src/flutter/lib/ui/compositing/scene_builder.h index 55726665c5..3a538744eb 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.h +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.h @@ -61,7 +61,7 @@ class SceneBuilder : public RefCountedDartWrappable { int clip_behavior, const fml::RefPtr& old_layer); void pushClipRSuperellipse(Dart_Handle layer_handle, - const RSuperellipse& rse, + const RSuperellipse* rse, int clip_behavior, const fml::RefPtr& old_layer); void pushClipPath(Dart_Handle layer_handle, diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index 7d1864308c..d8e7f0b4ef 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -78,6 +78,7 @@ typedef CanvasPath Path; V(PathMeasure::Create) \ V(Path::Create) \ V(PictureRecorder::Create) \ + V(RSuperellipse::Create) \ V(SceneBuilder::Create) \ V(SemanticsUpdateBuilder::Create) \ /* Other */ \ @@ -283,6 +284,7 @@ typedef CanvasPath Path; V(Picture, dispose) \ V(Picture, toImage) \ V(Picture, toImageSync) \ + V(RSuperellipse, contains) \ V(SceneBuilder, addPerformanceOverlay) \ V(SceneBuilder, addPicture) \ V(SceneBuilder, addPlatformView) \ diff --git a/engine/src/flutter/lib/ui/geometry.dart b/engine/src/flutter/lib/ui/geometry.dart index ae30eace46..c665e1b3ff 100644 --- a/engine/src/flutter/lib/ui/geometry.dart +++ b/engine/src/flutter/lib/ui/geometry.dart @@ -955,7 +955,8 @@ class Radius { /// A radius with [x] and [y] values set to zero. /// - /// You can use [Radius.zero] with [RRect] to have right-angle corners. + /// You can use [Radius.zero] with [RRect] or [RSuperellipse] to have + /// right-angle corners. static const Radius zero = Radius.circular(0.0); /// Returns this [Radius], with values clamped to the given min and max @@ -1136,23 +1137,6 @@ abstract class _RRectLike> { required double blRadiusY, }); - Float32List _getValue32() { - final Float32List result = Float32List(12); - result[0] = left; - result[1] = top; - result[2] = right; - result[3] = bottom; - result[4] = tlRadiusX; - result[5] = tlRadiusY; - result[6] = trRadiusX; - result[7] = trRadiusY; - result[8] = brRadiusX; - result[9] = brRadiusY; - result[10] = blRadiusX; - result[11] = blRadiusY; - return result; - } - /// The offset of the left edge of this rectangle from the x axis. final double left; @@ -1386,8 +1370,8 @@ abstract class _RRectLike> { /// of the width/height. /// /// Skia already handles RRects with radii that are too large in this way. - /// Therefore, this method is only needed for RRect use cases that require - /// the appropriately scaled radii values. + /// Therefore, this method is only needed for use cases of [RRect] or + /// [RSuperellipse] that require the appropriately scaled radii values. /// /// See the [Skia scaling implementation](https://github.com/google/skia/blob/main/src/core/SkRRect.cpp) /// for more details. @@ -1717,6 +1701,23 @@ class RRect extends _RRectLike { brRadiusY: brRadiusY, ); + Float32List _getValue32() { + final Float32List result = Float32List(12); + result[0] = left; + result[1] = top; + result[2] = right; + result[3] = bottom; + result[4] = tlRadiusX; + result[5] = tlRadiusY; + result[6] = trRadiusX; + result[7] = trRadiusY; + result[8] = brRadiusX; + result[9] = brRadiusY; + result[10] = blRadiusX; + result[11] = blRadiusY; + return result; + } + /// A rounded rectangle with all the values set to zero. static const RRect zero = RRect._raw(); @@ -1806,20 +1807,17 @@ class RRect extends _RRectLike { /// An immutable rounded superellipse. /// -/// A rounded superellipse is a shape similar to a typical rounded rectangle -/// ([RRect]), but with smoother transitions between the straight sides and the -/// rounded corners. It resembles the `RoundedRectangle` shape in SwiftUI with -/// the `.continuous` corner style. +/// A rounded superellipse (not to be confused with a standard superellipse) is +/// a shape formed by replacing the four curved corners of a superellipse with +/// circular arcs. A (standard) superellipse follows the formula x^n + y^n = +/// a^n, and while n > 2 gives it rounded corners, they tend to be too sharp and +/// pronounced. Replacing them with circular arcs makes the shape feel softer +/// and more natural. /// -/// Technically, a canonical rounded superellipse, i.e. one with a uniform -/// corner radius ([RSuperellipse.fromRectAndRadius]), is created by replacing -/// the four corners of a superellipse (also known as a Lamé curve) with -/// circular arcs. A rounded superellipse with non-uniform radii is extended on it -/// by concatenating arc segments and transformation. -/// -/// The corner radius parameters used in this class corresponds to SwiftUI's -/// `cornerRadius` parameter, which is close to, but not exactly equals to, the -/// radius of the corner circles. +/// Visually, a rounded superellipse looks similar to a typical rounded rectangle +/// ([RRect]) but with smoother transitions between the straight edges and +/// corners. It closely matches the `RoundedRectangle` shape in SwiftUI with the +/// `.continuous` corner style. class RSuperellipse extends _RRectLike { /// Construct a rounded rectangle from its left, top, right, and bottom edges, /// and the same radii along its horizontal axis and its vertical axis. @@ -2006,6 +2004,16 @@ class RSuperellipse extends _RRectLike { brRadiusY: brRadiusY, ); + _NativeRSuperellipse _native() { + return _NativeRSuperellipse(this); + } + + /// Whether the point specified by the given offset (which is assumed to be + /// relative to the origin) lies inside the rounded superellipse. + bool contains(Offset point) { + return _native().contains(point); + } + /// A rounded rectangle with all the values set to zero. static const RSuperellipse zero = RSuperellipse._raw(); @@ -2040,6 +2048,67 @@ class RSuperellipse extends _RRectLike { } } +class _NativeRSuperellipse extends NativeFieldWrapperClass1 { + _NativeRSuperellipse(RSuperellipse rsuperellipse) { + _constructor( + rsuperellipse.left, + rsuperellipse.top, + rsuperellipse.right, + rsuperellipse.bottom, + rsuperellipse.tlRadiusX, + rsuperellipse.tlRadiusY, + rsuperellipse.trRadiusX, + rsuperellipse.trRadiusY, + rsuperellipse.brRadiusX, + rsuperellipse.brRadiusY, + rsuperellipse.blRadiusX, + rsuperellipse.blRadiusY, + ); + } + + @Native< + Void Function( + Handle, + Double, + Double, + Double, + Double, + Double, + Double, + Double, + Double, + Double, + Double, + Double, + Double, + ) + >(symbol: 'RSuperellipse::Create') + external void _constructor( + double left, + double top, + double right, + double bottom, + double tlRadiusX, + double tlRadiusY, + double trRadiusX, + double trRadiusY, + double brRadiusX, + double brRadiusY, + double blRadiusX, + double blRadiusY, + ); + + bool contains(Offset point) { + return _contains(point.dx, point.dy); + } + + @Native, Double, Double)>( + symbol: 'RSuperellipse::contains', + isLeaf: true, + ) + external bool _contains(double x, double y); +} + /// A transform consisting of a translation, a rotation, and a uniform scale. /// /// Used by [Canvas.drawAtlas]. This is a more efficient way to represent these diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 70b0dc01c5..d98ae5efef 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -32,8 +32,8 @@ bool _rrectIsValid(RRect rrect) { return true; } -bool _rseIsValid(RSuperellipse rse) { - assert(!rse.hasNaN, 'RSuperellipse argument contained a NaN value.'); +bool _rsuperellipseIsValid(RSuperellipse rsuperellipse) { + assert(!rsuperellipse.hasNaN, 'RSuperellipse argument contained a NaN value.'); return true; } @@ -5894,7 +5894,7 @@ abstract class Canvas { /// If multiple draw commands intersect with the clip boundary, this can result /// in incorrect blending at the clip boundary. See [saveLayer] for a /// discussion of how to address that and some examples of using [clipRSuperellipse]. - void clipRSuperellipse(RSuperellipse rse, {bool doAntiAlias = true}); + void clipRSuperellipse(RSuperellipse rsuperellipse, {bool doAntiAlias = true}); /// Reduces the clip region to the intersection of the current clip and the /// given [Path]. @@ -6024,7 +6024,7 @@ abstract class Canvas { /// /// ![](https://flutter.github.io/assets-for-api-docs/assets/dart-ui/canvas_rsuperellipse.png#gh-light-mode-only) /// ![](https://flutter.github.io/assets-for-api-docs/assets/dart-ui/canvas_rsuperellipse.png#gh-dark-mode-only) - void drawRSuperellipse(RSuperellipse rse, Paint paint); + void drawRSuperellipse(RSuperellipse rsuperellipse, Paint paint); /// Draws an axis-aligned oval that fills the given axis-aligned rectangle /// with the given [Paint]. Whether the oval is filled or stroked (or both) is @@ -6638,13 +6638,13 @@ base class _NativeCanvas extends NativeFieldWrapperClass1 implements Canvas { external void _clipRRect(Float32List rrect, bool doAntiAlias); @override - void clipRSuperellipse(RSuperellipse rse, {bool doAntiAlias = true}) { - assert(_rseIsValid(rse)); - _clipRSuperellipse(rse._getValue32(), doAntiAlias); + void clipRSuperellipse(RSuperellipse rsuperellipse, {bool doAntiAlias = true}) { + assert(_rsuperellipseIsValid(rsuperellipse)); + _clipRSuperellipse(rsuperellipse._native(), doAntiAlias); } - @Native, Handle, Bool)>(symbol: 'Canvas::clipRSuperellipse') - external void _clipRSuperellipse(Float32List rse, bool doAntiAlias); + @Native, Pointer, Bool)>(symbol: 'Canvas::clipRSuperellipse') + external void _clipRSuperellipse(_NativeRSuperellipse rsuperellipse, bool doAntiAlias); @override void clipPath(Path path, {bool doAntiAlias = true}) { @@ -6757,14 +6757,16 @@ base class _NativeCanvas extends NativeFieldWrapperClass1 implements Canvas { ); @override - void drawRSuperellipse(RSuperellipse rse, Paint paint) { - assert(_rseIsValid(rse)); - _drawRSuperellipse(rse._getValue32(), paint._objects, paint._data); + void drawRSuperellipse(RSuperellipse rsuperellipse, Paint paint) { + assert(_rsuperellipseIsValid(rsuperellipse)); + _drawRSuperellipse(rsuperellipse._native(), paint._objects, paint._data); } - @Native, Handle, Handle, Handle)>(symbol: 'Canvas::drawRSuperellipse') + @Native, Pointer, Handle, Handle)>( + symbol: 'Canvas::drawRSuperellipse', + ) external void _drawRSuperellipse( - Float32List rse, + _NativeRSuperellipse rsuperellipse, List? paintObjects, ByteData paintData, ); diff --git a/engine/src/flutter/lib/ui/painting/canvas.cc b/engine/src/flutter/lib/ui/painting/canvas.cc index e4082e9dc9..64d07e8801 100644 --- a/engine/src/flutter/lib/ui/painting/canvas.cc +++ b/engine/src/flutter/lib/ui/painting/canvas.cc @@ -177,9 +177,9 @@ void Canvas::clipRRect(const RRect& rrect, bool doAntiAlias) { } } -void Canvas::clipRSuperellipse(const RSuperellipse& rse, bool doAntiAlias) { +void Canvas::clipRSuperellipse(const RSuperellipse* rse, bool doAntiAlias) { if (display_list_builder_) { - builder()->ClipRoundSuperellipse(rse.rsuperellipse, DlClipOp::kIntersect, + builder()->ClipRoundSuperellipse(rse->rsuperellipse(), DlClipOp::kIntersect, doAntiAlias); } } @@ -302,7 +302,7 @@ void Canvas::drawDRRect(const RRect& outer, } } -void Canvas::drawRSuperellipse(const RSuperellipse& rse, +void Canvas::drawRSuperellipse(const RSuperellipse* rse, Dart_Handle paint_objects, Dart_Handle paint_data) { Paint paint(paint_objects, paint_data); @@ -311,7 +311,7 @@ void Canvas::drawRSuperellipse(const RSuperellipse& rse, if (display_list_builder_) { DlPaint dl_paint; paint.paint(dl_paint, kDrawDRRectFlags, DlTileMode::kDecal); - builder()->DrawRoundSuperellipse(rse.rsuperellipse, dl_paint); + builder()->DrawRoundSuperellipse(rse->rsuperellipse(), dl_paint); } } diff --git a/engine/src/flutter/lib/ui/painting/canvas.h b/engine/src/flutter/lib/ui/painting/canvas.h index e0c885b048..9f892308bd 100644 --- a/engine/src/flutter/lib/ui/painting/canvas.h +++ b/engine/src/flutter/lib/ui/painting/canvas.h @@ -62,7 +62,7 @@ class Canvas : public RefCountedDartWrappable, DisplayListOpFlags { DlClipOp clipOp, bool doAntiAlias = true); void clipRRect(const RRect& rrect, bool doAntiAlias = true); - void clipRSuperellipse(const RSuperellipse& rse, bool doAntiAlias = true); + void clipRSuperellipse(const RSuperellipse* rse, bool doAntiAlias = true); void clipPath(const CanvasPath* path, bool doAntiAlias = true); void getDestinationClipBounds(Dart_Handle rect_handle); void getLocalClipBounds(Dart_Handle rect_handle); @@ -94,7 +94,7 @@ class Canvas : public RefCountedDartWrappable, DisplayListOpFlags { Dart_Handle paint_objects, Dart_Handle paint_data); - void drawRSuperellipse(const RSuperellipse& rse, + void drawRSuperellipse(const RSuperellipse* rse, Dart_Handle paint_objects, Dart_Handle paint_data); diff --git a/engine/src/flutter/lib/ui/painting/rsuperellipse.cc b/engine/src/flutter/lib/ui/painting/rsuperellipse.cc index 6f1865b633..9f193954dc 100644 --- a/engine/src/flutter/lib/ui/painting/rsuperellipse.cc +++ b/engine/src/flutter/lib/ui/painting/rsuperellipse.cc @@ -5,55 +5,83 @@ #include "flutter/lib/ui/painting/rsuperellipse.h" #include "flutter/fml/logging.h" +#include "flutter/lib/ui/floating_point.h" #include "third_party/tonic/logging/dart_error.h" -#include "third_party/tonic/typed_data/typed_list.h" -using flutter::RSuperellipse; +namespace flutter { -namespace tonic { - -// Construct an DlRoundSuperellipse from a Dart RSuperellipse object. -// The Dart RSuperellipse is a Float32List containing -// [left, top, right, bottom, xRadius, yRadius] -RSuperellipse DartConverter::FromDart( - Dart_Handle value) { - Float32List buffer(value); - - RSuperellipse result; - result.is_null = true; - if (buffer.data() == nullptr) { - return result; - } +namespace { +flutter::DlRect BuildBounds(float left, float top, float right, float bottom) { // The Flutter rect may be inverted (upside down, backward, or both) // Historically, Skia would normalize such rects but we will do that - // manually below when we construct the Impeller RoundRect + // manually below when we construct the Impeller Rect flutter::DlRect raw_rect = - flutter::DlRect::MakeLTRB(buffer[0], buffer[1], buffer[2], buffer[3]); + flutter::DlRect::MakeLTRB(left, top, right, bottom); + return raw_rect.GetPositive(); +} +impeller::RoundingRadii BuildRadii(float tl_radius_x, + float tl_radius_y, + float tr_radius_x, + float tr_radius_y, + float br_radius_x, + float br_radius_y, + float bl_radius_x, + float bl_radius_y) { // Flutter has radii in TL,TR,BR,BL (clockwise) order, // but Impeller uses TL,TR,BL,BR (zig-zag) order - impeller::RoundingRadii radii = { - .top_left = flutter::DlSize(buffer[4], buffer[5]), - .top_right = flutter::DlSize(buffer[6], buffer[7]), - .bottom_left = flutter::DlSize(buffer[10], buffer[11]), - .bottom_right = flutter::DlSize(buffer[8], buffer[9]), + return impeller::RoundingRadii{ + .top_left = flutter::DlSize(tl_radius_x, tl_radius_y), + .top_right = flutter::DlSize(tr_radius_x, tr_radius_y), + .bottom_left = flutter::DlSize(bl_radius_x, bl_radius_y), + .bottom_right = flutter::DlSize(br_radius_x, br_radius_y), }; +} +} // namespace - result.rsuperellipse = flutter::DlRoundSuperellipse::MakeRectRadii( - raw_rect.GetPositive(), radii); +IMPLEMENT_WRAPPERTYPEINFO(ui, RSuperellipse); - result.is_null = false; - return result; +void RSuperellipse::Create(Dart_Handle wrapper, + double left, + double top, + double right, + double bottom, + double tl_radius_x, + double tl_radius_y, + double tr_radius_x, + double tr_radius_y, + double br_radius_x, + double br_radius_y, + double bl_radius_x, + double bl_radius_y) { + UIDartState::ThrowIfUIOperationsProhibited(); + auto res = fml::MakeRefCounted( + BuildBounds(SafeNarrow(left), SafeNarrow(top), SafeNarrow(right), + SafeNarrow(bottom)), + BuildRadii(SafeNarrow(tl_radius_x), SafeNarrow(tl_radius_y), + SafeNarrow(tr_radius_x), SafeNarrow(tr_radius_y), + SafeNarrow(br_radius_x), SafeNarrow(br_radius_y), + SafeNarrow(bl_radius_x), SafeNarrow(bl_radius_y))); + res->AssociateWithDartWrapper(wrapper); } -RSuperellipse DartConverter::FromArguments( - Dart_NativeArguments args, - int index, - Dart_Handle& exception) { - Dart_Handle value = Dart_GetNativeArgument(args, index); - FML_DCHECK(!CheckAndHandleError(value)); - return FromDart(value); +RSuperellipse::RSuperellipse(flutter::DlRect bounds, + impeller::RoundingRadii radii) + : bounds_(bounds), radii_(radii) {} + +RSuperellipse::~RSuperellipse() = default; + +flutter::DlRoundSuperellipse RSuperellipse::rsuperellipse() const { + return flutter::DlRoundSuperellipse::MakeRectRadii(bounds_, radii_); } -} // namespace tonic +impeller::RoundSuperellipseParam RSuperellipse::param() const { + return impeller::RoundSuperellipseParam::MakeBoundsRadii(bounds_, radii_); +} + +bool RSuperellipse::contains(double x, double y) { + return param().Contains(DlPoint(SafeNarrow(x), SafeNarrow(y))); +} + +} // namespace flutter diff --git a/engine/src/flutter/lib/ui/painting/rsuperellipse.h b/engine/src/flutter/lib/ui/painting/rsuperellipse.h index 6df8606338..9cf9959e89 100644 --- a/engine/src/flutter/lib/ui/painting/rsuperellipse.h +++ b/engine/src/flutter/lib/ui/painting/rsuperellipse.h @@ -6,40 +6,47 @@ #define FLUTTER_LIB_UI_PAINTING_RSUPERELLIPSE_H_ #include "flutter/display_list/geometry/dl_geometry_types.h" -#include "third_party/dart/runtime/include/dart_api.h" -#include "third_party/tonic/converter/dart_converter.h" +#include "flutter/impeller/geometry/round_superellipse_param.h" +#include "flutter/lib/ui/dart_wrapper.h" +#include "flutter/lib/ui/ui_dart_state.h" +#include "third_party/tonic/typed_data/typed_list.h" namespace flutter { -class RSuperellipse { +class RSuperellipse : public RefCountedDartWrappable { + DEFINE_WRAPPERTYPEINFO(); + FML_FRIEND_MAKE_REF_COUNTED(RSuperellipse); + public: - DlRoundSuperellipse rsuperellipse; - bool is_null; + static void Create(Dart_Handle wrapper, + double left, + double top, + double right, + double bottom, + double tl_radius_x, + double tl_radius_y, + double tr_radius_x, + double tr_radius_y, + double br_radius_x, + double br_radius_y, + double bl_radius_x, + double bl_radius_y); + + ~RSuperellipse() override; + + bool contains(double x, double y); + flutter::DlRoundSuperellipse rsuperellipse() const; + impeller::RoundSuperellipseParam param() const; + + private: + RSuperellipse(flutter::DlRect bounds, impeller::RoundingRadii radii); + + flutter::DlScalar scalar_value(int index) const; + + flutter::DlRect bounds_; + impeller::RoundingRadii radii_; }; } // namespace flutter -namespace tonic { - -template <> -struct DartConverter { - using NativeType = flutter::RSuperellipse; - using FfiType = Dart_Handle; - static constexpr const char* kFfiRepresentation = "Handle"; - static constexpr const char* kDartRepresentation = "Object"; - static constexpr bool kAllowedInLeafCall = false; - - static NativeType FromDart(Dart_Handle handle); - static NativeType FromArguments(Dart_NativeArguments args, - int index, - Dart_Handle& exception); - - static NativeType FromFfi(FfiType val) { return FromDart(val); } - static const char* GetFfiRepresentation() { return kFfiRepresentation; } - static const char* GetDartRepresentation() { return kDartRepresentation; } - static bool AllowedInLeafCall() { return kAllowedInLeafCall; } -}; - -} // namespace tonic - #endif // FLUTTER_LIB_UI_PAINTING_RSUPERELLIPSE_H_ diff --git a/engine/src/flutter/lib/web_ui/lib/canvas.dart b/engine/src/flutter/lib/web_ui/lib/canvas.dart index 0342ef3e06..b588ecf4ff 100644 --- a/engine/src/flutter/lib/web_ui/lib/canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/canvas.dart @@ -68,7 +68,7 @@ abstract class Canvas { Float64List getTransform(); void clipRect(Rect rect, {ClipOp clipOp = ClipOp.intersect, bool doAntiAlias = true}); void clipRRect(RRect rrect, {bool doAntiAlias = true}); - void clipRSuperellipse(RSuperellipse rse, {bool doAntiAlias = true}); + void clipRSuperellipse(RSuperellipse rsuperellipse, {bool doAntiAlias = true}); void clipPath(Path path, {bool doAntiAlias = true}); Rect getLocalClipBounds(); Rect getDestinationClipBounds(); @@ -77,7 +77,7 @@ abstract class Canvas { void drawPaint(Paint paint); void drawRect(Rect rect, Paint paint); void drawRRect(RRect rrect, Paint paint); - void drawRSuperellipse(RSuperellipse rse, Paint paint); + void drawRSuperellipse(RSuperellipse rsuperellipse, Paint paint); void drawDRRect(RRect outer, RRect inner, Paint paint); void drawOval(Rect rect, Paint paint); void drawCircle(Offset c, double radius, Paint paint); diff --git a/engine/src/flutter/lib/web_ui/lib/compositing.dart b/engine/src/flutter/lib/web_ui/lib/compositing.dart index b751ba553d..ea8a1d8b55 100644 --- a/engine/src/flutter/lib/web_ui/lib/compositing.dart +++ b/engine/src/flutter/lib/web_ui/lib/compositing.dart @@ -48,7 +48,7 @@ abstract class SceneBuilder { ClipRRectEngineLayer? oldLayer, }); ClipRSuperellipseEngineLayer pushClipRSuperellipse( - RSuperellipse rse, { + RSuperellipse rsuperellipse, { required Clip clipBehavior, ClipRSuperellipseEngineLayer? oldLayer, }); diff --git a/engine/src/flutter/lib/web_ui/lib/geometry.dart b/engine/src/flutter/lib/web_ui/lib/geometry.dart index 0cb88fe9d3..44122a4d1c 100644 --- a/engine/src/flutter/lib/web_ui/lib/geometry.dart +++ b/engine/src/flutter/lib/web_ui/lib/geometry.dart @@ -1188,6 +1188,15 @@ class RSuperellipse extends _RRectLike { static const RSuperellipse zero = RSuperellipse._raw(); + bool contains(Offset point) { + // Web doesn't support RSuperellipse, but falls back to RRect in all use + // cases. Therefore this `contains` is implemented as RRect. Once Web + // supports RSuperellipse this method should be changed to the correct shape. + // TODO(dkwingsmt): Properly implement the shape on Web instead of + // falling back to RRect. https://github.com/flutter/flutter/issues/163718 + return toApproximateRRect().contains(point); + } + static RSuperellipse? lerp(RSuperellipse? a, RSuperellipse? b, double t) { if (a == null) { if (b == null) { diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart index 474a59c916..7b3a99cc5b 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart @@ -52,10 +52,14 @@ class CkCanvas { skCanvas.clipRRect(toSkRRect(rrect), _clipOpIntersect, doAntiAlias); } - void clipRSuperellipse(ui.RSuperellipse rse, bool doAntiAlias) { - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + void clipRSuperellipse(ui.RSuperellipse rsuperellipse, bool doAntiAlias) { + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 - skCanvas.clipRRect(toSkRRect(rse.toApproximateRRect()), _clipOpIntersect, doAntiAlias); + skCanvas.clipRRect( + toSkRRect(rsuperellipse.toApproximateRRect()), + _clipOpIntersect, + doAntiAlias, + ); } void clipRect(ui.Rect rect, ui.ClipOp clipOp, bool doAntiAlias) { diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_canvas.dart index c1ae75100a..a2af8cc4c8 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_canvas.dart @@ -131,11 +131,11 @@ class CanvasKitCanvas implements ui.Canvas { } @override - void clipRSuperellipse(ui.RSuperellipse rse, {bool doAntiAlias = true}) { - assert(rsuperellipseIsValid(rse)); - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + void clipRSuperellipse(ui.RSuperellipse rsuperellipse, {bool doAntiAlias = true}) { + assert(rsuperellipseIsValid(rsuperellipse)); + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 - _clipRRect(rse.toApproximateRRect(), doAntiAlias); + _clipRRect(rsuperellipse.toApproximateRRect(), doAntiAlias); } @override @@ -208,11 +208,11 @@ class CanvasKitCanvas implements ui.Canvas { } @override - void drawRSuperellipse(ui.RSuperellipse rse, ui.Paint paint) { - assert(rsuperellipseIsValid(rse)); - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + void drawRSuperellipse(ui.RSuperellipse rsuperellipse, ui.Paint paint) { + assert(rsuperellipseIsValid(rsuperellipse)); + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 - _drawRRect(rse.toApproximateRRect(), paint); + _drawRRect(rsuperellipse.toApproximateRRect(), paint); } @override diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_scene_builder.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_scene_builder.dart index dde5038654..998dd94ccf 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_scene_builder.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_scene_builder.dart @@ -128,11 +128,13 @@ class LayerSceneBuilder implements ui.SceneBuilder { @override ClipRSuperellipseEngineLayer pushClipRSuperellipse( - ui.RSuperellipse rse, { + ui.RSuperellipse rsuperellipse, { ui.Clip? clipBehavior, ui.EngineLayer? oldLayer, }) { - return pushLayer(ClipRSuperellipseEngineLayer(rse, clipBehavior)); + return pushLayer( + ClipRSuperellipseEngineLayer(rsuperellipse, clipBehavior), + ); } @override diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_visitor.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_visitor.dart index 7b1dcd8086..9c8641be13 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_visitor.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/layer_visitor.dart @@ -112,7 +112,7 @@ class PrerollVisitor extends LayerVisitor { @override void visitClipRSuperellipse(ClipRSuperellipseEngineLayer clipRSuperellipse) { - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 mutatorsStack.pushClipRRect(clipRSuperellipse.clipRSuperellipse.toApproximateRRect()); final ui.Rect childPaintBounds = prerollChildren(clipRSuperellipse); @@ -571,7 +571,7 @@ class PaintVisitor extends LayerVisitor { assert(clipRSuperellipse.needsPainting); nWayCanvas.save(); - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 nWayCanvas.clipRRect( clipRSuperellipse.clipRSuperellipse.toApproximateRRect(), 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 c91fd8e75b..60c8a119f6 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 @@ -297,20 +297,20 @@ class ClipRSuperellipseLayer with PictureEngineLayer implements ui.ClipRSuperell } class ClipRSuperellipseOperation implements LayerOperation { - const ClipRSuperellipseOperation(this.rse, this.clip); + const ClipRSuperellipseOperation(this.rsuperellipse, this.clip); - final ui.RSuperellipse rse; + final ui.RSuperellipse rsuperellipse; final ui.Clip clip; @override - ui.Rect mapRect(ui.Rect contentRect) => contentRect.intersect(rse.outerRect); + ui.Rect mapRect(ui.Rect contentRect) => contentRect.intersect(rsuperellipse.outerRect); @override void pre(SceneCanvas canvas) { canvas.save(); - canvas.clipRSuperellipse(rse, doAntiAlias: clip != ui.Clip.hardEdge); + canvas.clipRSuperellipse(rsuperellipse, doAntiAlias: clip != ui.Clip.hardEdge); if (clip == ui.Clip.antiAliasWithSaveLayer) { - canvas.saveLayer(rse.outerRect, ui.Paint()); + canvas.saveLayer(rsuperellipse.outerRect, ui.Paint()); } } @@ -324,34 +324,34 @@ class ClipRSuperellipseOperation implements LayerOperation { @override PlatformViewStyling createPlatformViewStyling() { - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 - return PlatformViewStyling(clip: PlatformViewRRectClip(rse.toApproximateRRect())); + return PlatformViewStyling(clip: PlatformViewRRectClip(rsuperellipse.toApproximateRRect())); } @override bool get affectsBackdrop => false; @override - String toString() => 'ClipRSuperellipseOperation(rse: $rse, clip: $clip)'; + String toString() => 'ClipRSuperellipseOperation(rsuperellipse: $rsuperellipse, clip: $clip)'; @override Map get debugJsonDescription { return { 'type': 'clipRSuperEllipse', - 'rse': { - 'left': rse.left, - 'top': rse.top, - 'right': rse.right, - 'bottom': rse.bottom, - 'tlRadiusX': rse.tlRadiusX, - 'tlRadiusY': rse.tlRadiusY, - 'trRadiusX': rse.trRadiusX, - 'trRadiusY': rse.trRadiusY, - 'brRadiusX': rse.brRadiusX, - 'brRadiusY': rse.brRadiusY, - 'blRadiusX': rse.blRadiusX, - 'blRadiusY': rse.blRadiusY, + 'rsuperellipse': { + 'left': rsuperellipse.left, + 'top': rsuperellipse.top, + 'right': rsuperellipse.right, + 'bottom': rsuperellipse.bottom, + 'tlRadiusX': rsuperellipse.tlRadiusX, + 'tlRadiusY': rsuperellipse.tlRadiusY, + 'trRadiusX': rsuperellipse.trRadiusX, + 'trRadiusY': rsuperellipse.trRadiusY, + 'brRadiusX': rsuperellipse.brRadiusX, + 'brRadiusY': rsuperellipse.brRadiusY, + 'blRadiusX': rsuperellipse.blRadiusX, + 'blRadiusY': rsuperellipse.blRadiusY, }, 'clip': clip.name, }; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/scene_builder.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/scene_builder.dart index 45a782cd2d..1f86d8d13a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/scene_builder.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/scene_builder.dart @@ -390,12 +390,12 @@ class EngineSceneBuilder implements ui.SceneBuilder { @override ui.ClipRSuperellipseEngineLayer pushClipRSuperellipse( - ui.RSuperellipse rse, { + ui.RSuperellipse rsuperellipse, { required ui.Clip clipBehavior, ui.ClipRSuperellipseEngineLayer? oldLayer, }) { return pushLayer( - ClipRSuperellipseLayer(ClipRSuperellipseOperation(rse, clipBehavior)), + ClipRSuperellipseLayer(ClipRSuperellipseOperation(rsuperellipse, clipBehavior)), ); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart index fd63b4d561..fee2a4eaf5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart @@ -135,10 +135,10 @@ class SkwasmCanvas implements SceneCanvas { } @override - void clipRSuperellipse(ui.RSuperellipse rse, {bool doAntiAlias = true}) { - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + void clipRSuperellipse(ui.RSuperellipse rsuperellipse, {bool doAntiAlias = true}) { + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 - clipRRect(rse.toApproximateRRect(), doAntiAlias: doAntiAlias); + clipRRect(rsuperellipse.toApproximateRRect(), doAntiAlias: doAntiAlias); } @override @@ -184,10 +184,10 @@ class SkwasmCanvas implements SceneCanvas { } @override - void drawRSuperellipse(ui.RSuperellipse rse, ui.Paint paint) { - // TODO(dkwingsmt): Properly implement clipRSE on Web instead of falling + void drawRSuperellipse(ui.RSuperellipse rsuperellipse, ui.Paint paint) { + // TODO(dkwingsmt): Properly implement RSuperellipse on Web instead of falling // back to RRect. https://github.com/flutter/flutter/issues/163718 - drawRRect(rse.toApproximateRRect(), paint); + drawRRect(rsuperellipse.toApproximateRRect(), paint); } @override diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/validators.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/validators.dart index ba1b72a244..f1a2c7240a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/validators.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/validators.dart @@ -22,9 +22,12 @@ bool rrectIsValid(ui.RRect rrect) { return true; } -bool rsuperellipseIsValid(ui.RSuperellipse rse) { +bool rsuperellipseIsValid(ui.RSuperellipse rsuperellipse) { assert( - !(rse.left.isNaN || rse.right.isNaN || rse.top.isNaN || rse.bottom.isNaN), + !(rsuperellipse.left.isNaN || + rsuperellipse.right.isNaN || + rsuperellipse.top.isNaN || + rsuperellipse.bottom.isNaN), 'RSuperellipse argument contained a NaN value.', ); return true; diff --git a/engine/src/flutter/lib/web_ui/test/engine/scene_builder_utils.dart b/engine/src/flutter/lib/web_ui/test/engine/scene_builder_utils.dart index d17b02b5ce..cbea4cdc86 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/scene_builder_utils.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/scene_builder_utils.dart @@ -90,7 +90,7 @@ class StubSceneCanvas implements SceneCanvas { void clipRRect(ui.RRect rrect, {bool doAntiAlias = true}) {} @override - void clipRSuperellipse(ui.RSuperellipse rse, {bool doAntiAlias = true}) {} + void clipRSuperellipse(ui.RSuperellipse rsuperellipse, {bool doAntiAlias = true}) {} @override void clipRect(ui.Rect rect, {ui.ClipOp clipOp = ui.ClipOp.intersect, bool doAntiAlias = true}) {} @@ -155,7 +155,7 @@ class StubSceneCanvas implements SceneCanvas { void drawRRect(ui.RRect rrect, ui.Paint paint) {} @override - void drawRSuperellipse(ui.RSuperellipse rse, ui.Paint paint) {} + void drawRSuperellipse(ui.RSuperellipse rsuperellipse, ui.Paint paint) {} @override void drawRawAtlas( diff --git a/engine/src/flutter/testing/dart/geometry_test.dart b/engine/src/flutter/testing/dart/geometry_test.dart index 0ea4dd804b..fda880dbb7 100644 --- a/engine/src/flutter/testing/dart/geometry_test.dart +++ b/engine/src/flutter/testing/dart/geometry_test.dart @@ -573,4 +573,140 @@ void main() { expect(rrect.brRadiusX, 0); expect(rrect.brRadiusY, 0); }); + + test('RSuperellipse.contains NoCornerRoundSuperellipseContains', () { + // RSuperellipse of bounds with no corners contains corners just barely. + const RSuperellipse rse = RSuperellipse.fromLTRBXY(-50, -50, 50, 50, 0, 0); + + expect(rse.contains(const Offset(-50, -50)), isTrue); + // Rectangles have half-in, half-out containment so we need + // to be careful about testing containment of right/bottom corners. + expect(rse.contains(const Offset(-50, 49.99)), isTrue); + expect(rse.contains(const Offset(49.99, -50)), isTrue); + expect(rse.contains(const Offset(49.99, 49.99)), isTrue); + expect(rse.contains(const Offset(-50.01, -50)), isFalse); + expect(rse.contains(const Offset(-50, -50.01)), isFalse); + expect(rse.contains(const Offset(-50.01, 50)), isFalse); + expect(rse.contains(const Offset(-50, 50.01)), isFalse); + expect(rse.contains(const Offset(50.01, -50)), isFalse); + expect(rse.contains(const Offset(50, -50.01)), isFalse); + expect(rse.contains(const Offset(50.01, 50)), isFalse); + expect(rse.contains(const Offset(50, 50.01)), isFalse); + }); + + test('RSuperellipse.contains TinyCornerContains', () { + // RSuperellipse of bounds with even the tiniest corners does not contain corners. + const RSuperellipse rse = RSuperellipse.fromLTRBXY(-50, -50, 50, 50, 0.01, 0.01); + + expect(rse.contains(const Offset(-50, -50)), isFalse); + expect(rse.contains(const Offset(-50, 50)), isFalse); + expect(rse.contains(const Offset(50, -50)), isFalse); + expect(rse.contains(const Offset(50, 50)), isFalse); + }); + + test('RSuperellipse.contains UniformSquareContains', () { + const RSuperellipse rse = RSuperellipse.fromLTRBXY(-50, -50, 50, 50, 5.0, 5.0); + + void checkPointAndMirrors(Offset p) { + checkPointWithOffset(rse, Offset(p.dx, p.dy), const Offset(0.02, 0.02)); + checkPointWithOffset(rse, Offset(p.dx, -p.dy), const Offset(0.02, -0.02)); + checkPointWithOffset(rse, Offset(-p.dx, p.dy), const Offset(-0.02, 0.02)); + checkPointWithOffset(rse, Offset(-p.dx, -p.dy), const Offset(-0.02, -0.02)); + } + + checkPointAndMirrors(const Offset(0, 49.995)); // Top + checkPointAndMirrors(const Offset(44.245, 49.95)); // Top curve start + checkPointAndMirrors(const Offset(45.72, 49.87)); // Top joint + checkPointAndMirrors(const Offset(48.53, 48.53)); // Circular arc mid + checkPointAndMirrors(const Offset(49.87, 45.72)); // Right joint + checkPointAndMirrors(const Offset(49.95, 44.245)); // Right curve start + checkPointAndMirrors(const Offset(49.995, 0)); // Right + }); + + test('RSuperellipse.contains UniformEllipticalContains', () { + const RSuperellipse rse = RSuperellipse.fromLTRBXY(-50, -50, 50, 50, 5.0, 10.0); + + void checkPointAndMirrors(Offset p) { + checkPointWithOffset(rse, Offset(p.dx, p.dy), const Offset(0.02, 0.02)); + checkPointWithOffset(rse, Offset(p.dx, -p.dy), const Offset(0.02, -0.02)); + checkPointWithOffset(rse, Offset(-p.dx, p.dy), const Offset(-0.02, 0.02)); + checkPointWithOffset(rse, Offset(-p.dx, -p.dy), const Offset(-0.02, -0.02)); + } + + checkPointAndMirrors(const Offset(0, 49.995)); // Top + checkPointAndMirrors(const Offset(44.245, 49.911)); // Top curve start + checkPointAndMirrors(const Offset(45.72, 49.75)); // Top joint + checkPointAndMirrors(const Offset(48.51, 47.07)); // Circular arc mid + checkPointAndMirrors(const Offset(49.87, 41.44)); // Right joint + checkPointAndMirrors(const Offset(49.95, 38.49)); // Right curve start + checkPointAndMirrors(const Offset(49.995, 0)); // Right + }); + + test('RSuperellipse.contains UniformRectangularContains', () { + // The bounds is not centered at the origin and has unequal height and width. + const RSuperellipse rse = RSuperellipse.fromLTRBXY(0, 0, 50, 100, 23.0, 30.0); + + final Offset center = rse.outerRect.center; + void checkPointAndMirrors(Offset globalPoint) { + final Offset p = globalPoint - center; + checkPointWithOffset(rse, Offset(p.dx, p.dy) + center, const Offset(0.02, 0.02)); + checkPointWithOffset(rse, Offset(p.dx, -p.dy) + center, const Offset(0.02, -0.02)); + checkPointWithOffset(rse, Offset(-p.dx, p.dy) + center, const Offset(-0.02, 0.02)); + checkPointWithOffset(rse, Offset(-p.dx, -p.dy) + center, const Offset(-0.02, -0.02)); + } + + checkPointAndMirrors(const Offset(24.99, 99.99)); // Bottom mid-edge + checkPointAndMirrors(const Offset(29.99, 99.64)); + checkPointAndMirrors(const Offset(34.99, 98.06)); + checkPointAndMirrors(const Offset(39.99, 94.73)); + checkPointAndMirrors(const Offset(44.13, 89.99)); + checkPointAndMirrors(const Offset(48.46, 79.99)); + checkPointAndMirrors(const Offset(49.70, 69.99)); + checkPointAndMirrors(const Offset(49.97, 59.99)); + checkPointAndMirrors(const Offset(49.99, 49.99)); // Right mid-edge + }); + + test('RSuperellipse.contains SlimDiagnalContains', () { + // This shape has large radii on one diagnal and tiny radii on the other, + // resulting in a almond-like shape placed diagnally (NW to SE). + final RSuperellipse rse = RSuperellipse.fromLTRBAndCorners( + -50, + -50, + 50, + 50, + topLeft: const Radius.circular(1.0), + topRight: const Radius.circular(99.0), + bottomLeft: const Radius.circular(99.0), + bottomRight: const Radius.circular(1.0), + ); + + expect(rse.contains(const Offset(0, 0)), isTrue); + expect(rse.contains(const Offset(-49.999, -49.999)), isFalse); + expect(rse.contains(const Offset(-49.999, 49.999)), isFalse); + expect(rse.contains(const Offset(49.999, 49.999)), isFalse); + expect(rse.contains(const Offset(49.999, -49.999)), isFalse); + + // The pointy ends at the NE and SW corners + checkPointWithOffset(rse, const Offset(-49.70, -49.70), const Offset(-0.02, -0.02)); + checkPointWithOffset(rse, const Offset(49.70, 49.70), const Offset(0.02, 0.02)); + + // Checks two points symmetrical to the origin. + void checkDiagnalPoints(Offset p) { + checkPointWithOffset(rse, p, const Offset(0.02, -0.02)); + checkPointWithOffset(rse, Offset(-p.dx, -p.dy), const Offset(-0.02, 0.02)); + } + + // A few other points along the edge + checkDiagnalPoints(const Offset(-40.0, -49.59)); + checkDiagnalPoints(const Offset(-20.0, -45.64)); + checkDiagnalPoints(const Offset(0.0, -37.01)); + checkDiagnalPoints(const Offset(20.0, -21.96)); + checkDiagnalPoints(const Offset(21.05, -20.92)); + checkDiagnalPoints(const Offset(40.0, 5.68)); + }); +} + +void checkPointWithOffset(RSuperellipse rse, Offset inPoint, Offset outwardOffset) { + expect(rse.contains(inPoint), isTrue); + expect(rse.contains(inPoint + outwardOffset), isFalse); }