diff --git a/engine/src/flutter/impeller/aiks/picture.cc b/engine/src/flutter/impeller/aiks/picture.cc index 7f00996a09..79230c75f0 100644 --- a/engine/src/flutter/impeller/aiks/picture.cc +++ b/engine/src/flutter/impeller/aiks/picture.cc @@ -5,6 +5,7 @@ #include "impeller/aiks/picture.h" #include +#include #include "impeller/base/validation.h" #include "impeller/entity/entity.h" @@ -14,7 +15,7 @@ namespace impeller { std::optional Picture::Snapshot(AiksContext& context) { - auto coverage = pass->GetElementsCoverage(); + auto coverage = pass->GetElementsCoverage(std::nullopt); if (!coverage.has_value() || coverage->IsEmpty()) { return std::nullopt; } diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index c61b4db7fd..071ac71c6f 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -64,16 +64,21 @@ const std::shared_ptr& EntityPass::GetLazyGlyphAtlas() const { return lazy_glyph_atlas_; } -std::optional EntityPass::GetElementsCoverage() const { +std::optional EntityPass::GetElementsCoverage( + std::optional coverage_clip) const { std::optional result; for (const auto& element : elements_) { std::optional coverage; if (auto entity = std::get_if(&element)) { coverage = entity->GetCoverage(); + + if (coverage.has_value() && coverage_clip.has_value()) { + coverage = coverage->Intersection(coverage_clip.value()); + } } else if (auto subpass = std::get_if>(&element)) { - coverage = GetSubpassCoverage(*subpass->get()); + coverage = GetSubpassCoverage(*subpass->get(), coverage_clip); } else { FML_UNREACHABLE(); } @@ -91,8 +96,9 @@ std::optional EntityPass::GetElementsCoverage() const { } std::optional EntityPass::GetSubpassCoverage( - const EntityPass& subpass) const { - auto entities_coverage = subpass.GetElementsCoverage(); + const EntityPass& subpass, + std::optional coverage_clip) const { + auto entities_coverage = subpass.GetElementsCoverage(coverage_clip); // The entities don't cover anything. There is nothing to do. if (!entities_coverage.has_value()) { return std::nullopt; @@ -143,7 +149,8 @@ bool EntityPass::Render(ContentContext& renderer, "EntityPass", // StorageMode::kDevicePrivate, LoadAction::kClear, StoreAction::kStore, StorageMode::kDevicePrivate, LoadAction::kClear, StoreAction::kStore); - if (!OnRender(renderer, offscreen_target, Point(), Point(), 0)) { + if (!OnRender(renderer, offscreen_target.GetRenderTargetSize(), + offscreen_target, Point(), Point(), 0)) { return false; } @@ -178,13 +185,15 @@ bool EntityPass::Render(ContentContext& renderer, return true; } - return OnRender(renderer, render_target, Point(), Point(), 0); + return OnRender(renderer, render_target.GetRenderTargetSize(), render_target, + Point(), Point(), 0); } EntityPass::EntityResult EntityPass::GetEntityForElement( const EntityPass::Element& element, ContentContext& renderer, InlinePassContext& pass_context, + ISize root_pass_size, Point position, uint32_t pass_depth, size_t stencil_depth_floor) const { @@ -221,8 +230,9 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( if (subpass->delegate_->CanCollapseIntoParentPass() && !subpass->backdrop_filter_proc_.has_value()) { // Directly render into the parent target and move on. - if (!subpass->OnRender(renderer, pass_context.GetRenderTarget(), position, - position, stencil_depth_floor)) { + if (!subpass->OnRender(renderer, root_pass_size, + pass_context.GetRenderTarget(), position, position, + stencil_depth_floor)) { return EntityPass::EntityResult::Failure(); } return EntityPass::EntityResult::Skip(); @@ -241,7 +251,8 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( pass_context.EndPass(); } - auto subpass_coverage = GetSubpassCoverage(*subpass); + auto subpass_coverage = + GetSubpassCoverage(*subpass, Rect::MakeSize(Size(root_pass_size))); if (backdrop_contents) { auto backdrop_coverage = backdrop_contents->GetCoverage(Entity{}); @@ -256,6 +267,11 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( } } + if (subpass_coverage.has_value()) { + subpass_coverage = + subpass_coverage->Intersection(Rect::MakeSize(Size(root_pass_size))); + } + if (!subpass_coverage.has_value()) { return EntityPass::EntityResult::Skip(); } @@ -304,9 +320,9 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( // Stencil textures aren't shared between EntityPasses (as much of the // time they are transient). - if (!subpass->OnRender(renderer, subpass_target, subpass_coverage->origin, - position, ++pass_depth, subpass->stencil_depth_, - backdrop_contents)) { + if (!subpass->OnRender(renderer, root_pass_size, subpass_target, + subpass_coverage->origin, position, ++pass_depth, + subpass->stencil_depth_, backdrop_contents)) { return EntityPass::EntityResult::Failure(); } @@ -327,6 +343,7 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( } bool EntityPass::OnRender(ContentContext& renderer, + ISize root_pass_size, RenderTarget render_target, Point position, Point parent_position, @@ -368,8 +385,8 @@ bool EntityPass::OnRender(ContentContext& renderer, for (const auto& element : elements_) { EntityResult result = - GetEntityForElement(element, renderer, pass_context, position, - pass_depth, stencil_depth_floor); + GetEntityForElement(element, renderer, pass_context, root_pass_size, + position, pass_depth, stencil_depth_floor); switch (result.status) { case EntityResult::kSuccess: diff --git a/engine/src/flutter/impeller/entity/entity_pass.h b/engine/src/flutter/impeller/entity/entity_pass.h index 9954fe5898..621f7b57a3 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.h +++ b/engine/src/flutter/impeller/entity/entity_pass.h @@ -60,9 +60,12 @@ class EntityPass { void SetBackdropFilter(std::optional proc); - std::optional GetSubpassCoverage(const EntityPass& subpass) const; + std::optional GetSubpassCoverage( + const EntityPass& subpass, + std::optional coverage_clip) const; - std::optional GetElementsCoverage() const; + std::optional GetElementsCoverage( + std::optional coverage_clip) const; private: struct EntityResult { @@ -91,11 +94,13 @@ class EntityPass { EntityResult GetEntityForElement(const EntityPass::Element& element, ContentContext& renderer, InlinePassContext& pass_context, + ISize root_pass_size, Point position, uint32_t pass_depth, size_t stencil_depth_floor) const; bool OnRender(ContentContext& renderer, + ISize root_pass_size, RenderTarget render_target, Point position, Point parent_position, diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index d2b7c04759..2b96ce4984 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include +#include #include "flutter/testing/testing.h" #include "impeller/entity/contents/filters/blend_filter_contents.h" @@ -63,36 +64,31 @@ class TestPassDelegate final : public EntityPassDelegate { const std::optional coverage_; }; -TEST_P(EntityTest, EntityPassSubpassCoverageIsCorrect) { +auto CreatePassWithRectPath(Rect rect, std::optional bounds_hint) { + auto subpass = std::make_unique(); + Entity entity; + entity.SetContents(SolidColorContents::Make( + PathBuilder{}.AddRect(rect).TakePath(), Color::Red())); + subpass->AddEntity(entity); + subpass->SetDelegate(std::make_unique(bounds_hint)); + return subpass; +} + +TEST_P(EntityTest, EntityPassCoverageRespectsDelegateBoundsHint) { EntityPass pass; - auto subpass0 = std::make_unique(); - { - Entity entity; - entity.SetContents(SolidColorContents::Make( - PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath(), - Color::Red())); - subpass0->AddEntity(entity); - subpass0->SetDelegate( - std::make_unique(Rect::MakeLTRB(50, 50, 150, 150))); - } + auto subpass0 = CreatePassWithRectPath(Rect::MakeLTRB(0, 0, 100, 100), + Rect::MakeLTRB(50, 50, 150, 150)); + auto subpass1 = CreatePassWithRectPath(Rect::MakeLTRB(500, 500, 1000, 1000), + Rect::MakeLTRB(800, 800, 900, 900)); - auto subpass1 = std::make_unique(); - { - Entity entity; - entity.SetContents(SolidColorContents::Make( - PathBuilder{}.AddRect(Rect::MakeLTRB(500, 500, 1000, 1000)).TakePath(), - Color::Red())); - subpass1->AddEntity(entity); - subpass1->SetDelegate( - std::make_unique(Rect::MakeLTRB(800, 800, 900, 900))); - } - - auto subpass0_coverage = pass.GetSubpassCoverage(*subpass0.get()); + auto subpass0_coverage = + pass.GetSubpassCoverage(*subpass0.get(), std::nullopt); ASSERT_TRUE(subpass0_coverage.has_value()); ASSERT_RECT_NEAR(subpass0_coverage.value(), Rect::MakeLTRB(50, 50, 100, 100)); - auto subpass1_coverage = pass.GetSubpassCoverage(*subpass1.get()); + auto subpass1_coverage = + pass.GetSubpassCoverage(*subpass1.get(), std::nullopt); ASSERT_TRUE(subpass1_coverage.has_value()); ASSERT_RECT_NEAR(subpass1_coverage.value(), Rect::MakeLTRB(800, 800, 900, 900)); @@ -100,11 +96,41 @@ TEST_P(EntityTest, EntityPassSubpassCoverageIsCorrect) { pass.AddSubpass(std::move(subpass0)); pass.AddSubpass(std::move(subpass1)); - auto coverage = pass.GetElementsCoverage(); + auto coverage = pass.GetElementsCoverage(std::nullopt); ASSERT_TRUE(coverage.has_value()); ASSERT_RECT_NEAR(coverage.value(), Rect::MakeLTRB(50, 50, 900, 900)); } +TEST_P(EntityTest, EntityPassCoverageRespectsCoverageLimit) { + // Rect is drawn entirely in negative area. + auto pass = CreatePassWithRectPath(Rect::MakeLTRB(-200, -200, -100, -100), + std::nullopt); + + // Without coverage limit. + { + auto pass_coverage = pass->GetElementsCoverage(std::nullopt); + ASSERT_TRUE(pass_coverage.has_value()); + ASSERT_RECT_NEAR(pass_coverage.value(), + Rect::MakeLTRB(-200, -200, -100, -100)); + } + + // With limit that doesn't overlap. + { + auto pass_coverage = + pass->GetElementsCoverage(Rect::MakeLTRB(0, 0, 100, 100)); + ASSERT_FALSE(pass_coverage.has_value()); + } + + // With limit that partially overlaps. + { + auto pass_coverage = + pass->GetElementsCoverage(Rect::MakeLTRB(-150, -150, 0, 0)); + ASSERT_TRUE(pass_coverage.has_value()); + ASSERT_RECT_NEAR(pass_coverage.value(), + Rect::MakeLTRB(-150, -150, -100, -100)); + } +} + TEST_P(EntityTest, CanDrawRect) { Entity entity; entity.SetTransformation(Matrix::MakeScale(GetContentScale()));