From 716c6afba4ab1c56e0aadd703ebcb3e531681ed1 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Mon, 24 Jul 2023 17:02:10 -0700 Subject: [PATCH] [Impeller] Skip rect clips that do nothing. (flutter/engine#43948) Resolves https://github.com/flutter/flutter/issues/131100 * Skip rect clips that we know won't further restrict the clip shape. * Don't append clips to enforce subpass bounds. We currently don't collapse passes when subpass clips are added anyhow, so just a restriction to subpass collapsing in order to keep the current behavior intact. * Small refactor to reduce confusion: Just place the subpass bounds limit into `EntityPass` itself instead of the delegate. Built on https://github.com/flutter/engine/pull/43946 (because iOS currently fails validation without it). Gallery home screen before/after: ![compare](https://github.com/flutter/engine/assets/919017/34608633-6b3b-4979-be2b-f2685b32168f) --- .../flutter/ci/licenses_golden/excluded_files | 1 + .../flutter/impeller/aiks/aiks_unittests.cc | 32 ++++++++++++-- engine/src/flutter/impeller/aiks/canvas.cc | 42 ++++++++++++------- .../impeller/aiks/paint_pass_delegate.cc | 24 ++++------- .../impeller/aiks/paint_pass_delegate.h | 12 +----- engine/src/flutter/impeller/entity/BUILD.gn | 1 + .../flutter/impeller/entity/entity_pass.cc | 24 +++++------ .../src/flutter/impeller/entity/entity_pass.h | 14 +++++++ .../impeller/entity/entity_pass_delegate.cc | 3 -- .../impeller/entity/entity_pass_delegate.h | 2 - .../impeller/entity/entity_unittests.cc | 12 ++---- .../entity/geometry/fill_path_geometry.cc | 16 ++++++- .../entity/geometry/fill_path_geometry.h | 10 ++++- .../impeller/entity/geometry/geometry.cc | 9 +++- .../impeller/entity/geometry/geometry.h | 16 +++++-- .../entity/geometry/geometry_unittests.cc | 40 ++++++++++++++++++ 16 files changed, 182 insertions(+), 76 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/geometry/geometry_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index f5218536f7..e687849977 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -132,6 +132,7 @@ ../../../flutter/impeller/docs ../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc ../../../flutter/impeller/entity/entity_unittests.cc +../../../flutter/impeller/entity/geometry/geometry_unittests.cc ../../../flutter/impeller/fixtures ../../../flutter/impeller/geometry/README.md ../../../flutter/impeller/geometry/geometry_unittests.cc diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 888617e7fc..49975b2e8b 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -1979,7 +1979,7 @@ TEST_P(AiksTest, OpacityPeepHoleApplicationTest) { ColorFilter::MakeBlend(BlendMode::kSourceOver, Color::Blue()); // Paint has color filter, can't elide. - auto delegate = std::make_shared(paint, rect); + auto delegate = std::make_shared(paint); ASSERT_FALSE(delegate->CanCollapseIntoParentPass(entity_pass.get())); paint.color_filter = nullptr; @@ -1991,14 +1991,14 @@ TEST_P(AiksTest, OpacityPeepHoleApplicationTest) { }; // Paint has image filter, can't elide. - delegate = std::make_shared(paint, rect); + delegate = std::make_shared(paint); ASSERT_FALSE(delegate->CanCollapseIntoParentPass(entity_pass.get())); paint.image_filter = nullptr; paint.color = Color::Red(); // Paint has no alpha, can't elide; - delegate = std::make_shared(paint, rect); + delegate = std::make_shared(paint); ASSERT_FALSE(delegate->CanCollapseIntoParentPass(entity_pass.get())); // Positive test. @@ -2008,7 +2008,7 @@ TEST_P(AiksTest, OpacityPeepHoleApplicationTest) { entity_pass->AddEntity(entity); paint.color = Color::Red().WithAlpha(0.5); - delegate = std::make_shared(paint, rect); + delegate = std::make_shared(paint); ASSERT_TRUE(delegate->CanCollapseIntoParentPass(entity_pass.get())); } @@ -2114,6 +2114,30 @@ TEST_P(AiksTest, DrawRectAbsorbsClearsNegative) { ASSERT_EQ(render_pass->GetCommands().size(), 2llu); } +TEST_P(AiksTest, ClipRectElidesNoOpClips) { + Canvas canvas(Rect(0, 0, 100, 100)); + canvas.ClipRect(Rect(0, 0, 100, 100)); + canvas.ClipRect(Rect(-100, -100, 300, 300)); + canvas.DrawPaint({.color = Color::Red(), .blend_mode = BlendMode::kSource}); + canvas.DrawPaint({.color = Color::CornflowerBlue().WithAlpha(0.75), + .blend_mode = BlendMode::kSourceOver}); + + Picture picture = canvas.EndRecordingAsPicture(); + auto expected = Color::Red().Blend(Color::CornflowerBlue().WithAlpha(0.75), + BlendMode::kSourceOver); + ASSERT_EQ(picture.pass->GetClearColor(), expected); + + std::shared_ptr spy = ContextSpy::Make(); + std::shared_ptr real_context = GetContext(); + std::shared_ptr mock_context = spy->MakeContext(real_context); + AiksContext renderer(mock_context); + std::shared_ptr image = picture.ToImage(renderer, {300, 300}); + + ASSERT_EQ(spy->render_passes_.size(), 1llu); + std::shared_ptr render_pass = spy->render_passes_[0]; + ASSERT_EQ(render_pass->GetCommands().size(), 0llu); +} + TEST_P(AiksTest, CollapsedDrawPaintInSubpass) { Canvas canvas; canvas.DrawPaint( diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 9e9b89e25a..84cb7544fd 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -280,7 +280,16 @@ void Canvas::ClipPath(const Path& path, Entity::ClipOperation clip_op) { } void Canvas::ClipRect(const Rect& rect, Entity::ClipOperation clip_op) { - ClipGeometry(Geometry::MakeRect(rect), clip_op); + auto geometry = Geometry::MakeRect(rect); + auto& cull_rect = xformation_stack_.back().cull_rect; + if (clip_op == Entity::ClipOperation::kIntersect && // + cull_rect.has_value() && // + geometry->CoversArea(xformation_stack_.back().xformation, *cull_rect) // + ) { + return; // This clip will do nothing, so skip it. + } + + ClipGeometry(std::move(geometry), clip_op); switch (clip_op) { case Entity::ClipOperation::kIntersect: IntersectCulling(rect); @@ -298,7 +307,21 @@ void Canvas::ClipRRect(const Rect& rect, .SetConvexity(Convexity::kConvex) .AddRoundedRect(rect, corner_radius) .TakePath(); - ClipGeometry(Geometry::MakeFillPath(path), clip_op); + + std::optional inner_rect = (corner_radius * 2 < rect.size.width && + corner_radius * 2 < rect.size.height) + ? rect.Expand(-corner_radius) + : std::make_optional(); + auto geometry = Geometry::MakeFillPath(path, inner_rect); + auto& cull_rect = xformation_stack_.back().cull_rect; + if (clip_op == Entity::ClipOperation::kIntersect && // + cull_rect.has_value() && // + geometry->CoversArea(xformation_stack_.back().xformation, *cull_rect) // + ) { + return; // This clip will do nothing, so skip it. + } + + ClipGeometry(std::move(geometry), clip_op); switch (clip_op) { case Entity::ClipOperation::kIntersect: IntersectCulling(rect); @@ -491,23 +514,14 @@ void Canvas::SaveLayer(const Paint& paint, Save(true, paint.blend_mode, backdrop_filter); auto& new_layer_pass = GetCurrentPass(); + new_layer_pass.SetBoundsLimit(bounds); // Only apply opacity peephole on default blending. if (paint.blend_mode == BlendMode::kSourceOver) { new_layer_pass.SetDelegate( - std::make_unique(paint, bounds)); + std::make_unique(paint)); } else { - new_layer_pass.SetDelegate( - std::make_unique(paint, bounds)); - } - - if (bounds.has_value() && !backdrop_filter) { - // Render target switches due to a save layer can be elided. In such cases - // where passes are collapsed into their parent, the clipping effect to - // the size of the render target that would have been allocated will be - // absent. Explicitly add back a clip to reproduce that behavior. Since - // clips never require a render target switch, this is a cheap operation. - ClipRect(bounds.value()); + new_layer_pass.SetDelegate(std::make_unique(paint)); } } diff --git a/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc b/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc index 8f6867b379..382ca3457c 100644 --- a/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc +++ b/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc @@ -17,17 +17,11 @@ namespace impeller { /// PaintPassDelegate /// ---------------------------------------------- -PaintPassDelegate::PaintPassDelegate(Paint paint, std::optional coverage) - : paint_(std::move(paint)), coverage_(coverage) {} +PaintPassDelegate::PaintPassDelegate(Paint paint) : paint_(std::move(paint)) {} // |EntityPassDelgate| PaintPassDelegate::~PaintPassDelegate() = default; -// |EntityPassDelgate| -std::optional PaintPassDelegate::GetCoverageRect() { - return coverage_; -} - // |EntityPassDelgate| bool PaintPassDelegate::CanElide() { return paint_.blend_mode == BlendMode::kDestination; @@ -56,19 +50,12 @@ std::shared_ptr PaintPassDelegate::CreateContentsForSubpassTarget( /// OpacityPeepholePassDelegate /// ---------------------------------------------- -OpacityPeepholePassDelegate::OpacityPeepholePassDelegate( - Paint paint, - std::optional coverage) - : paint_(std::move(paint)), coverage_(coverage) {} +OpacityPeepholePassDelegate::OpacityPeepholePassDelegate(Paint paint) + : paint_(std::move(paint)) {} // |EntityPassDelgate| OpacityPeepholePassDelegate::~OpacityPeepholePassDelegate() = default; -// |EntityPassDelgate| -std::optional OpacityPeepholePassDelegate::GetCoverageRect() { - return coverage_; -} - // |EntityPassDelgate| bool OpacityPeepholePassDelegate::CanElide() { return paint_.blend_mode == BlendMode::kDestination; @@ -77,6 +64,11 @@ bool OpacityPeepholePassDelegate::CanElide() { // |EntityPassDelgate| bool OpacityPeepholePassDelegate::CanCollapseIntoParentPass( EntityPass* entity_pass) { + // Passes with absorbed clips can not be safely collapsed. + if (entity_pass->GetBoundsLimit().has_value()) { + return false; + } + // OpacityPeepholePassDelegate will only get used if the pass's blend mode is // SourceOver, so no need to check here. if (paint_.color.alpha <= 0.0 || paint_.color.alpha >= 1.0 || diff --git a/engine/src/flutter/impeller/aiks/paint_pass_delegate.h b/engine/src/flutter/impeller/aiks/paint_pass_delegate.h index 9217943c8a..967b59ea81 100644 --- a/engine/src/flutter/impeller/aiks/paint_pass_delegate.h +++ b/engine/src/flutter/impeller/aiks/paint_pass_delegate.h @@ -16,14 +16,11 @@ class EntityPass; class PaintPassDelegate final : public EntityPassDelegate { public: - PaintPassDelegate(Paint paint, std::optional coverage); + explicit PaintPassDelegate(Paint paint); // |EntityPassDelgate| ~PaintPassDelegate() override; - // |EntityPassDelegate| - std::optional GetCoverageRect() override; - // |EntityPassDelgate| bool CanElide() override; @@ -37,7 +34,6 @@ class PaintPassDelegate final : public EntityPassDelegate { private: const Paint paint_; - const std::optional coverage_; FML_DISALLOW_COPY_AND_ASSIGN(PaintPassDelegate); }; @@ -49,14 +45,11 @@ class PaintPassDelegate final : public EntityPassDelegate { /// cannot forward to child subpass delegates. class OpacityPeepholePassDelegate final : public EntityPassDelegate { public: - OpacityPeepholePassDelegate(Paint paint, std::optional coverage); + explicit OpacityPeepholePassDelegate(Paint paint); // |EntityPassDelgate| ~OpacityPeepholePassDelegate() override; - // |EntityPassDelegate| - std::optional GetCoverageRect() override; - // |EntityPassDelgate| bool CanElide() override; @@ -70,7 +63,6 @@ class OpacityPeepholePassDelegate final : public EntityPassDelegate { private: const Paint paint_; - const std::optional coverage_; FML_DISALLOW_COPY_AND_ASSIGN(OpacityPeepholePassDelegate); }; diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index bdc3c08072..8495a37a60 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -270,6 +270,7 @@ impeller_component("entity_unittests") { "entity_playground.cc", "entity_playground.h", "entity_unittests.cc", + "geometry/geometry_unittests.cc", ] deps = [ diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index bbd20ec6ef..8303a4152c 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -62,6 +62,14 @@ void EntityPass::SetDelegate(std::unique_ptr delegate) { delegate_ = std::move(delegate); } +void EntityPass::SetBoundsLimit(std::optional bounds_limit) { + bounds_limit_ = bounds_limit; +} + +std::optional EntityPass::GetBoundsLimit() const { + return bounds_limit_; +} + void EntityPass::AddEntity(Entity entity) { if (entity.GetBlendMode() == BlendMode::kSourceOver && entity.GetContents()->IsOpaque()) { @@ -129,20 +137,12 @@ std::optional EntityPass::GetSubpassCoverage( return std::nullopt; } - // The delegates don't have an opinion on what the entity coverage has to be. - // Just use that as-is. - auto delegate_coverage = subpass.delegate_->GetCoverageRect(); - if (!delegate_coverage.has_value()) { + if (!subpass.bounds_limit_.has_value()) { return entities_coverage; } - // The delegate coverage hint is in given in local space, so apply the subpass - // transformation. - delegate_coverage = delegate_coverage->TransformBounds(subpass.xformation_); - - // If the delegate tells us the coverage is smaller than it needs to be, then - // great. OTOH, if the delegate is being wasteful, limit coverage to what is - // actually needed. - return entities_coverage->Intersection(delegate_coverage.value()); + auto user_bounds_coverage = + subpass.bounds_limit_->TransformBounds(subpass.xformation_); + return entities_coverage->Intersection(user_bounds_coverage); } EntityPass* EntityPass::GetSuperpass() const { diff --git a/engine/src/flutter/impeller/entity/entity_pass.h b/engine/src/flutter/impeller/entity/entity_pass.h index d90abfb99e..54eb84de7a 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.h +++ b/engine/src/flutter/impeller/entity/entity_pass.h @@ -54,6 +54,19 @@ class EntityPass { void SetDelegate(std::unique_ptr delgate); + /// @brief Set the bounds limit, which is provided by the user when creating + /// a SaveLayer. This is a hint that allows the user to communicate + /// that it's OK to not render content outside of the bounds. + /// + /// For consistency with Skia, we effectively treat this like a + /// rectangle clip by forcing the subpass texture size to never exceed + /// it. + void SetBoundsLimit(std::optional bounds_limit); + + /// @brief Get the bounds limit, which is provided by the user when creating + /// a SaveLayer. + std::optional GetBoundsLimit() const; + size_t GetSubpassesDepth() const; std::unique_ptr Clone() const; @@ -223,6 +236,7 @@ class EntityPass { BlendMode blend_mode_ = BlendMode::kSourceOver; bool flood_clip_ = false; bool enable_offscreen_debug_checkerboard_ = false; + std::optional bounds_limit_; /// These values are incremented whenever something is added to the pass that /// requires reading from the backdrop texture. Currently, this can happen in diff --git a/engine/src/flutter/impeller/entity/entity_pass_delegate.cc b/engine/src/flutter/impeller/entity/entity_pass_delegate.cc index eccd880086..b996995b57 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_delegate.cc +++ b/engine/src/flutter/impeller/entity/entity_pass_delegate.cc @@ -18,9 +18,6 @@ class DefaultEntityPassDelegate final : public EntityPassDelegate { // |EntityPassDelegate| ~DefaultEntityPassDelegate() override = default; - // |EntityPassDelegate| - std::optional GetCoverageRect() override { return std::nullopt; } - // |EntityPassDelegate| bool CanElide() override { return false; } diff --git a/engine/src/flutter/impeller/entity/entity_pass_delegate.h b/engine/src/flutter/impeller/entity/entity_pass_delegate.h index a8ee633b2d..fbc7f97be2 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_delegate.h +++ b/engine/src/flutter/impeller/entity/entity_pass_delegate.h @@ -20,8 +20,6 @@ class EntityPassDelegate { EntityPassDelegate(); - virtual std::optional GetCoverageRect() = 0; - virtual ~EntityPassDelegate(); virtual bool CanElide() = 0; diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index b8c4975c13..f64463694e 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -70,15 +70,11 @@ TEST_P(EntityTest, CanCreateEntity) { class TestPassDelegate final : public EntityPassDelegate { public: - explicit TestPassDelegate(std::optional coverage, bool collapse = false) - : coverage_(coverage), collapse_(collapse) {} + explicit TestPassDelegate(bool collapse = false) : collapse_(collapse) {} // |EntityPassDelegate| ~TestPassDelegate() override = default; - // |EntityPassDelegate| - std::optional GetCoverageRect() override { return coverage_; } - // |EntityPassDelgate| bool CanElide() override { return false; } @@ -107,12 +103,12 @@ auto CreatePassWithRectPath(Rect rect, entity.SetContents(SolidColorContents::Make( PathBuilder{}.AddRect(rect).TakePath(), Color::Red())); subpass->AddEntity(entity); - subpass->SetDelegate( - std::make_unique(bounds_hint, collapse)); + subpass->SetDelegate(std::make_unique(collapse)); + subpass->SetBoundsLimit(bounds_hint); return subpass; } -TEST_P(EntityTest, EntityPassCoverageRespectsDelegateBoundsHint) { +TEST_P(EntityTest, EntityPassRespectsSubpassBoundsLimit) { EntityPass pass; auto subpass0 = CreatePassWithRectPath(Rect::MakeLTRB(0, 0, 100, 100), diff --git a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc index e6104d2dae..b608ed0428 100644 --- a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc +++ b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.cc @@ -6,7 +6,9 @@ namespace impeller { -FillPathGeometry::FillPathGeometry(const Path& path) : path_(path) {} +FillPathGeometry::FillPathGeometry(const Path& path, + std::optional inner_rect) + : path_(path), inner_rect_(inner_rect) {} FillPathGeometry::~FillPathGeometry() = default; @@ -147,4 +149,16 @@ std::optional FillPathGeometry::GetCoverage( return path_.GetTransformedBoundingBox(transform); } +bool FillPathGeometry::CoversArea(const Matrix& transform, + const Rect& rect) const { + if (!inner_rect_.has_value()) { + return false; + } + if (!transform.IsTranslationScaleOnly()) { + return false; + } + Rect coverage = inner_rect_->TransformBounds(transform); + return coverage.Contains(rect); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h index 087505e87e..63cd4ab535 100644 --- a/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h +++ b/engine/src/flutter/impeller/entity/geometry/fill_path_geometry.h @@ -4,17 +4,24 @@ #pragma once +#include + #include "impeller/entity/geometry/geometry.h" +#include "impeller/geometry/rect.h" namespace impeller { /// @brief A geometry that is created from a filled path object. class FillPathGeometry : public Geometry { public: - explicit FillPathGeometry(const Path& path); + explicit FillPathGeometry(const Path& path, + std::optional inner_rect = std::nullopt); ~FillPathGeometry(); + // |Geometry| + bool CoversArea(const Matrix& transform, const Rect& rect) const override; + private: // |Geometry| GeometryResult GetPositionBuffer(const ContentContext& renderer, @@ -35,6 +42,7 @@ class FillPathGeometry : public Geometry { RenderPass& pass) override; Path path_; + std::optional inner_rect_; FML_DISALLOW_COPY_AND_ASSIGN(FillPathGeometry); }; diff --git a/engine/src/flutter/impeller/entity/geometry/geometry.cc b/engine/src/flutter/impeller/entity/geometry/geometry.cc index a252af0c46..36267c51c2 100644 --- a/engine/src/flutter/impeller/entity/geometry/geometry.cc +++ b/engine/src/flutter/impeller/entity/geometry/geometry.cc @@ -4,11 +4,14 @@ #include "impeller/entity/geometry/geometry.h" +#include + #include "impeller/entity/geometry/cover_geometry.h" #include "impeller/entity/geometry/fill_path_geometry.h" #include "impeller/entity/geometry/point_field_geometry.h" #include "impeller/entity/geometry/rect_geometry.h" #include "impeller/entity/geometry/stroke_path_geometry.h" +#include "impeller/geometry/rect.h" namespace impeller { @@ -107,8 +110,10 @@ GeometryResult Geometry::GetPositionUVBuffer(Rect texture_coverage, return {}; } -std::unique_ptr Geometry::MakeFillPath(const Path& path) { - return std::make_unique(path); +std::unique_ptr Geometry::MakeFillPath( + const Path& path, + std::optional inner_rect) { + return std::make_unique(path, inner_rect); } std::unique_ptr Geometry::MakePointField(std::vector points, diff --git a/engine/src/flutter/impeller/entity/geometry/geometry.h b/engine/src/flutter/impeller/entity/geometry/geometry.h index 0f80e5456b..6a8861d120 100644 --- a/engine/src/flutter/impeller/entity/geometry/geometry.h +++ b/engine/src/flutter/impeller/entity/geometry/geometry.h @@ -57,7 +57,9 @@ class Geometry { virtual ~Geometry(); - static std::unique_ptr MakeFillPath(const Path& path); + static std::unique_ptr MakeFillPath( + const Path& path, + std::optional inner_rect = std::nullopt); static std::unique_ptr MakeStrokePath( const Path& path, @@ -88,8 +90,16 @@ class Geometry { virtual std::optional GetCoverage(const Matrix& transform) const = 0; - /// @return `true` if this geometry will completely cover all fragments in - /// `rect` when the `transform` is applied to it. + /// @brief Determines if this geometry, transformed by the given + /// `transform`, will completely cover all surface area of the given + /// `rect`. + /// + /// This is a conservative estimate useful for certain + /// optimizations. + /// + /// @returns `true` if the transformed geometry is guaranteed to cover the + /// given `rect`. May return `false` in many undetected cases where + /// the transformed geometry does in fact cover the `rect`. virtual bool CoversArea(const Matrix& transform, const Rect& rect) const; }; diff --git a/engine/src/flutter/impeller/entity/geometry/geometry_unittests.cc b/engine/src/flutter/impeller/entity/geometry/geometry_unittests.cc new file mode 100644 index 0000000000..55f958ac9e --- /dev/null +++ b/engine/src/flutter/impeller/entity/geometry/geometry_unittests.cc @@ -0,0 +1,40 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/testing/testing.h" +#include "impeller/entity/geometry/geometry.h" +#include "impeller/geometry/path_builder.h" + +namespace impeller { +namespace testing { + +TEST(EntityGeometryTest, RectGeometryCoversArea) { + auto geometry = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 100, 100)); + ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(0, 0, 100, 100))); + ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(-1, 0, 100, 100))); + ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(1, 1, 100, 100))); + ASSERT_TRUE(geometry->CoversArea({}, Rect())); +} + +TEST(EntityGeometryTest, FillPathGeometryCoversArea) { + auto path = PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath(); + auto geometry = Geometry::MakeFillPath( + path, /* inner rect */ Rect::MakeLTRB(0, 0, 100, 100)); + ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(0, 0, 100, 100))); + ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(-1, 0, 100, 100))); + ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(1, 1, 100, 100))); + ASSERT_TRUE(geometry->CoversArea({}, Rect())); +} + +TEST(EntityGeometryTest, FillPathGeometryCoversAreaNoInnerRect) { + auto path = PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath(); + auto geometry = Geometry::MakeFillPath(path); + ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(0, 0, 100, 100))); + ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(-1, 0, 100, 100))); + ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(1, 1, 100, 100))); + ASSERT_FALSE(geometry->CoversArea({}, Rect())); +} + +} // namespace testing +} // namespace impeller