From b3b83c6bd385467d2794da907a6d8c58d64d2f0e Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 3 Mar 2023 13:22:04 -0800 Subject: [PATCH] [Impeller] fix subpass collapse pass ending logic (flutter/engine#40057) [Impeller] fix subpass collapse pass ending logic --- .../flutter/impeller/entity/entity_pass.cc | 29 ++++++++------- .../src/flutter/impeller/entity/entity_pass.h | 19 +++++----- .../impeller/entity/entity_playground.cc | 16 +++++++++ .../impeller/entity/entity_playground.h | 4 +++ .../impeller/entity/entity_unittests.cc | 35 ++++++++++++++++--- .../impeller/entity/inline_pass_context.cc | 19 +++++++--- .../impeller/entity/inline_pass_context.h | 10 ++++-- 7 files changed, 97 insertions(+), 35 deletions(-) diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index 31ff319661..23e7f86108 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -294,12 +294,13 @@ EntityPass::EntityResult EntityPass::GetEntityForElement( return EntityPass::EntityResult::Skip(); } - if (subpass->delegate_->CanCollapseIntoParentPass() && - !subpass->backdrop_filter_proc_.has_value()) { + if (!subpass->backdrop_filter_proc_.has_value() && + subpass->delegate_->CanCollapseIntoParentPass()) { // Directly render into the parent target and move on. if (!subpass->OnRender(renderer, root_pass_size, pass_context.GetRenderTarget(), position, position, - stencil_depth_floor)) { + pass_depth, stencil_depth_, nullptr, + pass_context.GetRenderPass(pass_depth))) { return EntityPass::EntityResult::Failure(); } return EntityPass::EntityResult::Skip(); @@ -405,20 +406,22 @@ struct StencilLayer { size_t stencil_depth; }; -bool EntityPass::OnRender( - ContentContext& renderer, - ISize root_pass_size, - const RenderTarget& render_target, - Point position, - Point parent_position, - uint32_t pass_depth, - size_t stencil_depth_floor, - std::shared_ptr backdrop_filter_contents) const { +bool EntityPass::OnRender(ContentContext& renderer, + ISize root_pass_size, + const RenderTarget& render_target, + Point position, + Point parent_position, + uint32_t pass_depth, + size_t stencil_depth_floor, + std::shared_ptr backdrop_filter_contents, + std::optional + collapsed_parent_pass) const { TRACE_EVENT0("impeller", "EntityPass::OnRender"); auto context = renderer.GetContext(); InlinePassContext pass_context(context, render_target, - reads_from_pass_texture_); + reads_from_pass_texture_, + std::move(collapsed_parent_pass)); if (!pass_context.IsValid()) { return false; } diff --git a/engine/src/flutter/impeller/entity/entity_pass.h b/engine/src/flutter/impeller/entity/entity_pass.h index 5ba236d4e4..d15beab319 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.h +++ b/engine/src/flutter/impeller/entity/entity_pass.h @@ -99,15 +99,16 @@ class EntityPass { uint32_t pass_depth, size_t stencil_depth_floor) const; - bool OnRender( - ContentContext& renderer, - ISize root_pass_size, - const RenderTarget& render_target, - Point position, - Point parent_position, - uint32_t pass_depth, - size_t stencil_depth_floor = 0, - std::shared_ptr backdrop_filter_contents = nullptr) const; + bool OnRender(ContentContext& renderer, + ISize root_pass_size, + const RenderTarget& render_target, + Point position, + Point parent_position, + uint32_t pass_depth, + size_t stencil_depth_floor = 0, + std::shared_ptr backdrop_filter_contents = nullptr, + std::optional + collapsed_parent_pass = std::nullopt) const; std::vector elements_; diff --git a/engine/src/flutter/impeller/entity/entity_playground.cc b/engine/src/flutter/impeller/entity/entity_playground.cc index 9fb8e92c09..43f8f40dee 100644 --- a/engine/src/flutter/impeller/entity/entity_playground.cc +++ b/engine/src/flutter/impeller/entity/entity_playground.cc @@ -14,6 +14,22 @@ EntityPlayground::EntityPlayground() = default; EntityPlayground::~EntityPlayground() = default; +bool EntityPlayground::OpenPlaygroundHere(EntityPass& entity_pass) { + if (!Playground::is_enabled()) { + return true; + } + + ContentContext content_context(GetContext()); + if (!content_context.IsValid()) { + return false; + } + + auto callback = [&](RenderTarget& render_target) -> bool { + return entity_pass.Render(content_context, render_target); + }; + return Playground::OpenPlaygroundHere(callback); +} + bool EntityPlayground::OpenPlaygroundHere(Entity entity) { if (!Playground::is_enabled()) { return true; diff --git a/engine/src/flutter/impeller/entity/entity_playground.h b/engine/src/flutter/impeller/entity/entity_playground.h index c66e588fba..a2f0587649 100644 --- a/engine/src/flutter/impeller/entity/entity_playground.h +++ b/engine/src/flutter/impeller/entity/entity_playground.h @@ -7,6 +7,8 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity.h" +#include "impeller/entity/entity_pass.h" + #include "impeller/playground/playground_test.h" namespace impeller { @@ -22,6 +24,8 @@ class EntityPlayground : public PlaygroundTest { bool OpenPlaygroundHere(Entity entity); + bool OpenPlaygroundHere(EntityPass& entity_pass); + bool OpenPlaygroundHere(EntityPlaygroundCallback callback); private: diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index b018ccecad..8a96888424 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -61,8 +61,8 @@ TEST_P(EntityTest, CanCreateEntity) { class TestPassDelegate final : public EntityPassDelegate { public: - explicit TestPassDelegate(std::optional coverage) - : coverage_(coverage) {} + explicit TestPassDelegate(std::optional coverage, bool collapse = false) + : coverage_(coverage), collapse_(collapse) {} // |EntityPassDelegate| ~TestPassDelegate() override = default; @@ -74,7 +74,7 @@ class TestPassDelegate final : public EntityPassDelegate { bool CanElide() override { return false; } // |EntityPassDelgate| - bool CanCollapseIntoParentPass() override { return false; } + bool CanCollapseIntoParentPass() override { return collapse_; } // |EntityPassDelgate| std::shared_ptr CreateContentsForSubpassTarget( @@ -85,15 +85,19 @@ class TestPassDelegate final : public EntityPassDelegate { private: const std::optional coverage_; + const bool collapse_; }; -auto CreatePassWithRectPath(Rect rect, std::optional bounds_hint) { +auto CreatePassWithRectPath(Rect rect, + std::optional bounds_hint, + bool collapse = false) { 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)); + subpass->SetDelegate( + std::make_unique(bounds_hint, collapse)); return subpass; } @@ -124,6 +128,27 @@ TEST_P(EntityTest, EntityPassCoverageRespectsDelegateBoundsHint) { ASSERT_RECT_NEAR(coverage.value(), Rect::MakeLTRB(50, 50, 900, 900)); } +TEST_P(EntityTest, EntityPassCanMergeSubpassIntoParent) { + // Both a red and a blue box should appear if the pass merging has worked + // correctly. + + EntityPass pass; + auto subpass = CreatePassWithRectPath(Rect::MakeLTRB(0, 0, 100, 100), + Rect::MakeLTRB(50, 50, 150, 150), true); + pass.AddSubpass(std::move(subpass)); + + Entity entity; + entity.SetTransformation(Matrix::MakeScale(GetContentScale())); + auto contents = std::make_unique(); + contents->SetGeometry(Geometry::MakeRect(Rect::MakeLTRB(100, 100, 200, 200))); + contents->SetColor(Color::Blue()); + entity.SetContents(std::move(contents)); + + pass.AddEntity(entity); + + ASSERT_TRUE(OpenPlaygroundHere(pass)); +} + TEST_P(EntityTest, EntityPassCoverageRespectsCoverageLimit) { // Rect is drawn entirely in negative area. auto pass = CreatePassWithRectPath(Rect::MakeLTRB(-200, -200, -100, -100), diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.cc b/engine/src/flutter/impeller/entity/inline_pass_context.cc index c5997802ee..ed085eda2e 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.cc +++ b/engine/src/flutter/impeller/entity/inline_pass_context.cc @@ -13,15 +13,24 @@ namespace impeller { -InlinePassContext::InlinePassContext(std::shared_ptr context, - const RenderTarget& render_target, - uint32_t pass_texture_reads) +InlinePassContext::InlinePassContext( + std::shared_ptr context, + const RenderTarget& render_target, + uint32_t pass_texture_reads, + std::optional collapsed_parent_pass) : context_(std::move(context)), render_target_(render_target), - total_pass_reads_(pass_texture_reads) {} + total_pass_reads_(pass_texture_reads), + is_collapsed_(collapsed_parent_pass.has_value()) { + if (collapsed_parent_pass.has_value()) { + pass_ = collapsed_parent_pass.value().pass; + } +} InlinePassContext::~InlinePassContext() { - EndPass(); + if (!is_collapsed_) { + EndPass(); + } } bool InlinePassContext::IsValid() const { diff --git a/engine/src/flutter/impeller/entity/inline_pass_context.h b/engine/src/flutter/impeller/entity/inline_pass_context.h index ee393bda6c..d7a352afd3 100644 --- a/engine/src/flutter/impeller/entity/inline_pass_context.h +++ b/engine/src/flutter/impeller/entity/inline_pass_context.h @@ -17,9 +17,11 @@ class InlinePassContext { std::shared_ptr backdrop_texture; }; - InlinePassContext(std::shared_ptr context, - const RenderTarget& render_target, - uint32_t pass_texture_reads); + InlinePassContext( + std::shared_ptr context, + const RenderTarget& render_target, + uint32_t pass_texture_reads, + std::optional collapsed_parent_pass = std::nullopt); ~InlinePassContext(); bool IsValid() const; @@ -38,6 +40,8 @@ class InlinePassContext { std::shared_ptr pass_; uint32_t pass_count_ = 0; uint32_t total_pass_reads_ = 0; + // Whether this context is collapsed into a parent entity pass. + bool is_collapsed_ = false; FML_DISALLOW_COPY_AND_ASSIGN(InlinePassContext); };