From d6ab09555686193f21ddc037302ab1f7a777ea00 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Sun, 20 Aug 2023 15:03:11 -0700 Subject: [PATCH] [Impeller] Fix clip management for DrawPicture. (flutter/engine#44835) Solves two problems: 1. Fix the debug crash by restoring the clip after the `EntityPass` merge. I initially surrounded the merge with a `Save` and `Restore`, which was a mistake and is a no-op in this situation. This should have no excess impact on performance, since we ignore clips in `EntityPass` when we detect that they won't do anything. 2. Picture subpasses weren't getting their stencil depth adjusted during the merge, which can result in subpass entities potentially receiving a clip depth that's too high. --- .../flutter/impeller/aiks/aiks_unittests.cc | 20 ++++++++++++++ engine/src/flutter/impeller/aiks/canvas.cc | 27 ++++++++++++------- .../flutter/impeller/entity/entity_pass.cc | 20 ++++++++++++++ .../src/flutter/impeller/entity/entity_pass.h | 8 ++++++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index dbd507233d..d1dcadac7f 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -3060,6 +3060,26 @@ TEST_P(AiksTest, DrawPictureWithText) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, DrawPictureClipped) { + Canvas subcanvas; + subcanvas.ClipRRect(Rect::MakeLTRB(100, 100, 400, 400), 15); + subcanvas.DrawPaint({.color = Color::Red()}); + auto picture = subcanvas.EndRecordingAsPicture(); + + Canvas canvas; + canvas.DrawPaint({.color = Color::CornflowerBlue()}); + + // Draw a red RRect via DrawPicture. + canvas.DrawPicture(picture); + + // Draw over the picture with a larger green rectangle, completely covering it + // up. + canvas.ClipRRect(Rect::MakeLTRB(100, 100, 400, 400).Expand(20), 15); + canvas.DrawPaint({.color = Color::Green()}); + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + TEST_P(AiksTest, MatrixSaveLayerFilter) { Canvas canvas; canvas.DrawPaint({.color = Color::Black()}); diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 84cb7544fd..4a4fc7ad4b 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -427,20 +427,29 @@ void Canvas::DrawPicture(const Picture& picture) { return; } - auto save_count = GetSaveCount(); - Save(); - // Clone the base pass and account for the CTM updates. auto pass = picture.pass->Clone(); - pass->IterateAllEntities([&](auto& entity) -> bool { - entity.IncrementStencilDepth(GetStencilDepth()); - entity.SetTransformation(GetCurrentTransformation() * - entity.GetTransformation()); - return true; + + pass->IterateAllElements([&](auto& element) -> bool { + if (auto entity = std::get_if(&element)) { + entity->IncrementStencilDepth(GetStencilDepth()); + entity->SetTransformation(GetCurrentTransformation() * + entity->GetTransformation()); + return true; + } + + if (auto subpass = std::get_if>(&element)) { + subpass->get()->SetStencilDepth(subpass->get()->GetStencilDepth() + + GetStencilDepth()); + return true; + } + + FML_UNREACHABLE(); }); + GetCurrentPass().AddSubpassInline(std::move(pass)); - RestoreToCount(save_count); + RestoreClip(); } void Canvas::DrawImage(const std::shared_ptr& image, diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index 4925c4df45..b0a4b883fb 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -881,6 +881,22 @@ bool EntityPass::OnRender( return true; } +void EntityPass::IterateAllElements( + const std::function& iterator) { + if (!iterator) { + return; + } + + for (auto& element : elements_) { + if (!iterator(element)) { + return; + } + if (auto subpass = std::get_if>(&element)) { + subpass->get()->IterateAllElements(iterator); + } + } +} + void EntityPass::IterateAllEntities( const std::function& iterator) { if (!iterator) { @@ -975,6 +991,10 @@ void EntityPass::SetStencilDepth(size_t stencil_depth) { stencil_depth_ = stencil_depth; } +size_t EntityPass::GetStencilDepth() { + return stencil_depth_; +} + void EntityPass::SetBlendMode(BlendMode blend_mode) { blend_mode_ = blend_mode; flood_clip_ = Entity::IsBlendModeDestructive(blend_mode); diff --git a/engine/src/flutter/impeller/entity/entity_pass.h b/engine/src/flutter/impeller/entity/entity_pass.h index 54eb84de7a..d2dcb06018 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.h +++ b/engine/src/flutter/impeller/entity/entity_pass.h @@ -88,6 +88,12 @@ class EntityPass { bool Render(ContentContext& renderer, const RenderTarget& render_target) const; + /// @brief Iterate all elements (entities and subpasses) in this pass, + /// recursively including elements of child passes. The iteration + /// order is depth-first. Whenever a subpass elements is encountered, + /// it's included in the stream before its children. + void IterateAllElements(const std::function& iterator); + /// @brief Iterate all entities in this pass, recursively including entities /// of child passes. The iteration order is depth-first. void IterateAllEntities(const std::function& iterator); @@ -111,6 +117,8 @@ class EntityPass { void SetStencilDepth(size_t stencil_depth); + size_t GetStencilDepth(); + void SetBlendMode(BlendMode blend_mode); Color GetClearColor(ISize size = ISize::Infinite()) const;