[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.
This commit is contained in:
Brandon DeRosier
2023-08-20 15:03:11 -07:00
committed by GitHub
parent 6f57ec0293
commit d6ab095556
4 changed files with 66 additions and 9 deletions

View File

@@ -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()});

View File

@@ -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<Entity>(&element)) {
entity->IncrementStencilDepth(GetStencilDepth());
entity->SetTransformation(GetCurrentTransformation() *
entity->GetTransformation());
return true;
}
if (auto subpass = std::get_if<std::unique_ptr<EntityPass>>(&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>& image,

View File

@@ -881,6 +881,22 @@ bool EntityPass::OnRender(
return true;
}
void EntityPass::IterateAllElements(
const std::function<bool(Element&)>& iterator) {
if (!iterator) {
return;
}
for (auto& element : elements_) {
if (!iterator(element)) {
return;
}
if (auto subpass = std::get_if<std::unique_ptr<EntityPass>>(&element)) {
subpass->get()->IterateAllElements(iterator);
}
}
}
void EntityPass::IterateAllEntities(
const std::function<bool(Entity&)>& 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);

View File

@@ -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<bool(Element&)>& 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<bool(Entity&)>& 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;