From 7ebba40ad4d8627a64f74b3288185e6184d48b8e Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 22 Oct 2024 14:12:22 -0700 Subject: [PATCH] [Impeller] refactor clip rendering. (flutter/engine#56030) Now that we don't have the entity pass structure, clips can be treated as a kind of structured data and not a generic entity. Doing so lets us simplify some of the clip management and remove some usage of raw pointers. This change also removes the ClipRestoreContents, which are only ever created and then immediately rendered - we can instead replace this with a function. The ClipCoverageStack now has explicit Clip/Restore methods that internally apply the same logic, but without involving the entity object. Redo of https://github.com/flutter/engine/pull/55784 --- .../flutter/ci/licenses_golden/excluded_files | 3 +- .../flutter/impeller/display_list/canvas.cc | 188 +++++++-------- .../flutter/impeller/display_list/canvas.h | 8 +- .../impeller/display_list/dl_dispatcher.cc | 58 +++-- engine/src/flutter/impeller/entity/BUILD.gn | 3 +- ...s_unittests.cc => clip_stack_unittests.cc} | 179 +++++--------- .../impeller/entity/contents/clip_contents.cc | 103 +++----- .../impeller/entity/contents/clip_contents.h | 108 ++++----- .../contents/clip_contents_unittests.cc | 54 ----- .../entity/contents/color_source_contents.h | 6 +- .../impeller/entity/contents/contents.cc | 7 - .../impeller/entity/contents/contents.h | 31 --- .../filters/gaussian_blur_filter_contents.cc | 44 ++-- .../entity/contents/solid_color_contents.cc | 1 - engine/src/flutter/impeller/entity/entity.cc | 11 - engine/src/flutter/impeller/entity/entity.h | 3 - .../impeller/entity/entity_pass_clip_stack.cc | 221 +++++++++--------- .../impeller/entity/entity_pass_clip_stack.h | 28 +-- .../impeller/entity/entity_unittests.cc | 63 ----- 19 files changed, 395 insertions(+), 724 deletions(-) rename engine/src/flutter/impeller/entity/{entity_pass_unittests.cc => clip_stack_unittests.cc} (56%) delete mode 100644 engine/src/flutter/impeller/entity/contents/clip_contents_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index 1009dc88cc..c5c1c44f7f 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -157,7 +157,7 @@ ../../../flutter/impeller/display_list/dl_unittests.cc ../../../flutter/impeller/display_list/skia_conversions_unittests.cc ../../../flutter/impeller/docs -../../../flutter/impeller/entity/contents/clip_contents_unittests.cc +../../../flutter/impeller/entity/clip_stack_unittests.cc ../../../flutter/impeller/entity/contents/filters/blend_filter_contents_unittests.cc ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc ../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc @@ -167,7 +167,6 @@ ../../../flutter/impeller/entity/contents/tiled_texture_contents_unittests.cc ../../../flutter/impeller/entity/draw_order_resolver_unittests.cc ../../../flutter/impeller/entity/entity_pass_target_unittests.cc -../../../flutter/impeller/entity/entity_pass_unittests.cc ../../../flutter/impeller/entity/entity_unittests.cc ../../../flutter/impeller/entity/geometry/geometry_unittests.cc ../../../flutter/impeller/entity/render_target_cache_unittests.cc diff --git a/engine/src/flutter/impeller/display_list/canvas.cc b/engine/src/flutter/impeller/display_list/canvas.cc index abfead73ba..0498fe04ab 100644 --- a/engine/src/flutter/impeller/display_list/canvas.cc +++ b/engine/src/flutter/impeller/display_list/canvas.cc @@ -425,14 +425,14 @@ bool Canvas::AttemptDrawBlurredRRect(const Rect& rect, break; } case FilterContents::BlurStyle::kOuter: { - ClipGeometry(Geometry::MakeRoundRect(rect, corner_radii), - Entity::ClipOperation::kDifference); + RoundRectGeometry geom(rect, corner_radii); + ClipGeometry(geom, Entity::ClipOperation::kDifference); draw_blurred_rrect(); break; } case FilterContents::BlurStyle::kInner: { - ClipGeometry(Geometry::MakeRoundRect(rect, corner_radii), - Entity::ClipOperation::kIntersect); + RoundRectGeometry geom(rect, corner_radii); + ClipGeometry(geom, Entity::ClipOperation::kIntersect); draw_blurred_rrect(); break; } @@ -550,34 +550,84 @@ void Canvas::DrawCircle(const Point& center, } } -void Canvas::ClipGeometry(std::unique_ptr geometry, +void Canvas::ClipGeometry(const Geometry& geometry, Entity::ClipOperation clip_op) { - clip_geometry_.push_back(std::move(geometry)); + if (IsSkipping()) { + return; + } - auto contents = std::make_shared(); - contents->SetGeometry(clip_geometry_.back().get()); - contents->SetClipOperation(clip_op); + // Ideally the clip depth would be greater than the current rendering + // depth because any rendering calls that follow this clip operation will + // pre-increment the depth and then be rendering above our clip depth, + // but that case will be caught by the CHECK in AddRenderEntity above. + // In practice we sometimes have a clip set with no rendering after it + // and in such cases the current depth will equal the clip depth. + // Eventually the DisplayList should optimize these out, but it is hard + // to know if a clip will actually be used in advance of storing it in + // the DisplayList buffer. + // See https://github.com/flutter/flutter/issues/147021 + FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth) + << current_depth_ << " <=? " << transform_stack_.back().clip_depth; - Entity entity; - entity.SetTransform(GetCurrentTransform()); - entity.SetContents(std::move(contents)); + uint32_t clip_depth = transform_stack_.back().clip_depth; - AddClipEntityToCurrentPass(entity); + const Matrix clip_transform = + Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) * + GetCurrentTransform(); + + std::optional clip_coverage = geometry.GetCoverage(clip_transform); + if (!clip_coverage.has_value()) { + return; + } + + ClipContents clip_contents( + clip_coverage.value(), + /*is_axis_aligned_rect=*/geometry.IsAxisAlignedRect() && + GetCurrentTransform().IsTranslationScaleOnly()); + clip_contents.SetClipOperation(clip_op); + + EntityPassClipStack::ClipStateResult clip_state_result = + clip_coverage_stack_.RecordClip(clip_contents, // + clip_transform, // + GetGlobalPassPosition(), // + clip_depth, // + GetClipHeightFloor() // + ); + + if (clip_state_result.clip_did_change) { + // We only need to update the pass scissor if the clip state has changed. + SetClipScissor(clip_coverage_stack_.CurrentClipCoverage(), + *render_passes_.back().inline_pass_context->GetRenderPass(), + GetGlobalPassPosition()); + } ++transform_stack_.back().clip_height; ++transform_stack_.back().num_clips; -} -void Canvas::RestoreClip() { + if (!clip_state_result.should_render) { + return; + } + + // Note: this is a bit of a hack. Its not possible to construct a geometry + // result without begninning the render pass. We should refactor the geometry + // objects so that they only need a reference to the render pass size and/or + // orthographic transform. Entity entity; - entity.SetTransform(GetCurrentTransform()); - // This path is empty because ClipRestoreContents just generates a quad that - // takes up the full render target. - auto clip_restore = std::make_shared(); - clip_restore->SetRestoreHeight(GetClipHeight()); - entity.SetContents(std::move(clip_restore)); + entity.SetTransform(clip_transform); + entity.SetClipDepth(clip_depth); - AddRenderEntityToCurrentPass(entity); + GeometryResult geometry_result = geometry.GetPositionBuffer( + renderer_, // + entity, // + *render_passes_.back().inline_pass_context->GetRenderPass() // + ); + clip_contents.SetGeometry(geometry_result); + clip_coverage_stack_.GetLastReplayResult().clip_contents.SetGeometry( + geometry_result); + + clip_contents.Render( + renderer_, *render_passes_.back().inline_pass_context->GetRenderPass(), + clip_depth); } void Canvas::DrawPoints(std::vector points, @@ -1233,35 +1283,12 @@ bool Canvas::Restore() { transform_stack_.pop_back(); if (num_clips > 0) { - Entity entity; - entity.SetTransform( - Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) * - GetCurrentTransform()); - // This path is empty because ClipRestoreContents just generates a quad that - // takes up the full render target. - auto clip_restore = std::make_shared(); - clip_restore->SetRestoreHeight(GetClipHeight()); - entity.SetContents(std::move(clip_restore)); - - auto current_clip_coverage = clip_coverage_stack_.CurrentClipCoverage(); - if (current_clip_coverage.has_value()) { - // Entity transforms are relative to the current pass position, so we need - // to check clip coverage in the same space. - current_clip_coverage = - current_clip_coverage->Shift(-GetGlobalPassPosition()); - } - - auto clip_coverage = entity.GetClipCoverage(current_clip_coverage); - if (clip_coverage.coverage.has_value()) { - clip_coverage.coverage = - clip_coverage.coverage->Shift(GetGlobalPassPosition()); - } - EntityPassClipStack::ClipStateResult clip_state_result = - clip_coverage_stack_.ApplyClipState(clip_coverage, entity, - GetClipHeightFloor(), - GetGlobalPassPosition()); + clip_coverage_stack_.RecordRestore(GetGlobalPassPosition(), + GetClipHeight()); + // Clip restores are never required with depth based clipping. + FML_DCHECK(!clip_state_result.should_render); if (clip_state_result.clip_did_change) { // We only need to update the pass scissor if the clip state has changed. SetClipScissor( @@ -1270,12 +1297,6 @@ bool Canvas::Restore() { GetGlobalPassPosition() // ); } - - if (!clip_state_result.should_render) { - return true; - } - - entity.Render(renderer_, GetCurrentRenderPass()); } return true; @@ -1490,60 +1511,6 @@ void Canvas::AddRenderEntityToCurrentPass(Entity& entity, bool reuse_depth) { entity.Render(renderer_, *result); } -void Canvas::AddClipEntityToCurrentPass(Entity& entity) { - if (IsSkipping()) { - return; - } - - auto transform = entity.GetTransform(); - entity.SetTransform( - Matrix::MakeTranslation(Vector3(-GetGlobalPassPosition())) * transform); - - // Ideally the clip depth would be greater than the current rendering - // depth because any rendering calls that follow this clip operation will - // pre-increment the depth and then be rendering above our clip depth, - // but that case will be caught by the CHECK in AddRenderEntity above. - // In practice we sometimes have a clip set with no rendering after it - // and in such cases the current depth will equal the clip depth. - // Eventually the DisplayList should optimize these out, but it is hard - // to know if a clip will actually be used in advance of storing it in - // the DisplayList buffer. - // See https://github.com/flutter/flutter/issues/147021 - FML_DCHECK(current_depth_ <= transform_stack_.back().clip_depth) - << current_depth_ << " <=? " << transform_stack_.back().clip_depth; - entity.SetClipDepth(transform_stack_.back().clip_depth); - - auto current_clip_coverage = clip_coverage_stack_.CurrentClipCoverage(); - if (current_clip_coverage.has_value()) { - // Entity transforms are relative to the current pass position, so we need - // to check clip coverage in the same space. - current_clip_coverage = - current_clip_coverage->Shift(-GetGlobalPassPosition()); - } - - auto clip_coverage = entity.GetClipCoverage(current_clip_coverage); - if (clip_coverage.coverage.has_value()) { - clip_coverage.coverage = - clip_coverage.coverage->Shift(GetGlobalPassPosition()); - } - - EntityPassClipStack::ClipStateResult clip_state_result = - clip_coverage_stack_.ApplyClipState( - clip_coverage, entity, GetClipHeightFloor(), GetGlobalPassPosition()); - - if (clip_state_result.clip_did_change) { - // We only need to update the pass scissor if the clip state has changed. - SetClipScissor(clip_coverage_stack_.CurrentClipCoverage(), - GetCurrentRenderPass(), GetGlobalPassPosition()); - } - - if (!clip_state_result.should_render) { - return; - } - - entity.Render(renderer_, GetCurrentRenderPass()); -} - RenderPass& Canvas::GetCurrentRenderPass() const { return *render_passes_.back().inline_pass_context->GetRenderPass(); } @@ -1662,7 +1629,8 @@ std::shared_ptr Canvas::FlipBackdrop(Point global_pass_position, for (const auto& replay : replay_entities) { SetClipScissor(replay.clip_coverage, current_render_pass, global_pass_position); - if (!replay.entity.Render(renderer_, current_render_pass)) { + if (!replay.clip_contents.Render(renderer_, current_render_pass, + replay.clip_depth)) { VALIDATION_LOG << "Failed to render entity for clip restore."; } } diff --git a/engine/src/flutter/impeller/display_list/canvas.h b/engine/src/flutter/impeller/display_list/canvas.h index 0c139af0b3..e0f982f8fa 100644 --- a/engine/src/flutter/impeller/display_list/canvas.h +++ b/engine/src/flutter/impeller/display_list/canvas.h @@ -16,6 +16,7 @@ #include "impeller/core/sampler_descriptor.h" #include "impeller/display_list/paint.h" #include "impeller/entity/contents/atlas_contents.h" +#include "impeller/entity/contents/clip_contents.h" #include "impeller/entity/entity.h" #include "impeller/entity/entity_pass_clip_stack.h" #include "impeller/entity/geometry/geometry.h" @@ -225,8 +226,7 @@ class Canvas { void DrawAtlas(const std::shared_ptr& atlas_contents, const Paint& paint); - void ClipGeometry(std::unique_ptr geometry, - Entity::ClipOperation clip_op); + void ClipGeometry(const Geometry& geometry, Entity::ClipOperation clip_op); void EndReplay(); @@ -327,10 +327,6 @@ class Canvas { void AddRenderEntityToCurrentPass(Entity& entity, bool reuse_depth = false); - void AddClipEntityToCurrentPass(Entity& entity); - - void RestoreClip(); - bool AttemptDrawBlurredRRect(const Rect& rect, Size corner_radii, const Paint& paint); diff --git a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc index 989be7aa21..c8a1bc6ac4 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc @@ -25,7 +25,11 @@ #include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/filters/inputs/filter_input.h" #include "impeller/entity/entity.h" +#include "impeller/entity/geometry/ellipse_geometry.h" +#include "impeller/entity/geometry/fill_path_geometry.h" #include "impeller/entity/geometry/geometry.h" +#include "impeller/entity/geometry/rect_geometry.h" +#include "impeller/entity/geometry/round_rect_geometry.h" #include "impeller/geometry/color.h" #include "impeller/geometry/path.h" #include "impeller/geometry/path_builder.h" @@ -434,7 +438,8 @@ void DlDispatcherBase::clipRect(const DlRect& rect, bool is_aa) { AUTO_DEPTH_WATCHER(0u); - GetCanvas().ClipGeometry(Geometry::MakeRect(rect), ToClipOperation(clip_op)); + RectGeometry geom(rect); + GetCanvas().ClipGeometry(geom, ToClipOperation(clip_op)); } // |flutter::DlOpReceiver| @@ -443,8 +448,8 @@ void DlDispatcherBase::clipOval(const DlRect& bounds, bool is_aa) { AUTO_DEPTH_WATCHER(0u); - GetCanvas().ClipGeometry(Geometry::MakeOval(bounds), - ToClipOperation(clip_op)); + EllipseGeometry geom(bounds); + GetCanvas().ClipGeometry(geom, ToClipOperation(clip_op)); } // |flutter::DlOpReceiver| @@ -455,17 +460,17 @@ void DlDispatcherBase::clipRoundRect(const DlRoundRect& rrect, auto clip_op = ToClipOperation(sk_op); if (rrect.IsRect()) { - GetCanvas().ClipGeometry(Geometry::MakeRect(rrect.GetBounds()), clip_op); + RectGeometry geom(rrect.GetBounds()); + GetCanvas().ClipGeometry(geom, clip_op); } else if (rrect.IsOval()) { - GetCanvas().ClipGeometry(Geometry::MakeOval(rrect.GetBounds()), clip_op); + EllipseGeometry geom(rrect.GetBounds()); + GetCanvas().ClipGeometry(geom, clip_op); } else if (rrect.GetRadii().AreAllCornersSame()) { - GetCanvas().ClipGeometry( - Geometry::MakeRoundRect(rrect.GetBounds(), rrect.GetRadii().top_left), - clip_op); + RoundRectGeometry geom(rrect.GetBounds(), rrect.GetRadii().top_left); + GetCanvas().ClipGeometry(geom, clip_op); } else { - GetCanvas().ClipGeometry( - Geometry::MakeFillPath(PathBuilder{}.AddRoundRect(rrect).TakePath()), - clip_op); + FillPathGeometry geom(PathBuilder{}.AddRoundRect(rrect).TakePath()); + GetCanvas().ClipGeometry(geom, clip_op); } } @@ -477,19 +482,20 @@ void DlDispatcherBase::clipPath(const DlPath& path, ClipOp sk_op, bool is_aa) { DlRect rect; if (path.IsRect(&rect)) { - GetCanvas().ClipGeometry(Geometry::MakeRect(rect), clip_op); + RectGeometry geom(rect); + GetCanvas().ClipGeometry(geom, clip_op); } else if (path.IsOval(&rect)) { - GetCanvas().ClipGeometry(Geometry::MakeOval(rect), clip_op); + EllipseGeometry geom(rect); + GetCanvas().ClipGeometry(geom, clip_op); } else { SkRRect rrect; if (path.IsSkRRect(&rrect) && rrect.isSimple()) { - GetCanvas().ClipGeometry( - Geometry::MakeRoundRect( - skia_conversions::ToRect(rrect.rect()), - skia_conversions::ToSize(rrect.getSimpleRadii())), - clip_op); + RoundRectGeometry geom(skia_conversions::ToRect(rrect.rect()), + skia_conversions::ToSize(rrect.getSimpleRadii())); + GetCanvas().ClipGeometry(geom, clip_op); } else { - GetCanvas().ClipGeometry(Geometry::MakeFillPath(path.GetPath()), clip_op); + FillPathGeometry geom(path.GetPath()); + GetCanvas().ClipGeometry(geom, clip_op); } } } @@ -530,7 +536,8 @@ void DlDispatcherBase::drawDashedLine(const DlPoint& p0, // length is non-positive - drawLine will draw appropriate "dot" // off_length is non-positive - no gaps, drawLine will draw it solid // on_length is negative - invalid dashing - // Note that a 0 length "on" dash will draw "dot"s every "off" distance apart + // Note that a 0 length "on" dash will draw "dot"s every "off" distance + // apart if (length > 0.0f && on_length >= 0.0f && off_length > 0.0f) { Point delta = (p1 - p0) / length; // length > 0 already tested PathBuilder builder; @@ -1139,11 +1146,12 @@ void TextFrameDispatcher::drawDisplayList( display_list->Dispatch(*this); } else if (!local_cull_bounds.IsEmpty()) { IRect cull_rect = IRect::RoundOut(local_cull_bounds); - display_list->Dispatch(*this, SkIRect::MakeLTRB(cull_rect.GetLeft(), // - cull_rect.GetTop(), // - cull_rect.GetRight(), // - cull_rect.GetBottom() // - )); + display_list->Dispatch(*this, + SkIRect::MakeLTRB(cull_rect.GetLeft(), // + cull_rect.GetTop(), // + cull_rect.GetRight(), // + cull_rect.GetBottom() // + )); } } diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 9f966ec4c6..76657a5595 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -235,7 +235,7 @@ impeller_component("entity_unittests") { testonly = true sources = [ - "contents/clip_contents_unittests.cc", + "clip_stack_unittests.cc", "contents/filters/blend_filter_contents_unittests.cc", "contents/filters/gaussian_blur_filter_contents_unittests.cc", "contents/filters/inputs/filter_input_unittests.cc", @@ -244,7 +244,6 @@ impeller_component("entity_unittests") { "contents/tiled_texture_contents_unittests.cc", "draw_order_resolver_unittests.cc", "entity_pass_target_unittests.cc", - "entity_pass_unittests.cc", "entity_playground.cc", "entity_playground.h", "entity_unittests.cc", diff --git a/engine/src/flutter/impeller/entity/entity_pass_unittests.cc b/engine/src/flutter/impeller/entity/clip_stack_unittests.cc similarity index 56% rename from engine/src/flutter/impeller/entity/entity_pass_unittests.cc rename to engine/src/flutter/impeller/entity/clip_stack_unittests.cc index 2c24eab20c..07e80c2a39 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_unittests.cc +++ b/engine/src/flutter/impeller/entity/clip_stack_unittests.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include - #include "flutter/testing/testing.h" #include "gtest/gtest.h" #include "impeller/entity/contents/clip_contents.h" @@ -19,16 +17,20 @@ TEST(EntityPassClipStackTest, CanPushAndPopEntities) { EXPECT_TRUE(recorder.GetReplayEntities().empty()); - Entity entity; - recorder.RecordEntity(entity, Contents::ClipCoverage::Type::kAppend, - Rect::MakeLTRB(0, 0, 100, 100)); + recorder.RecordClip(ClipContents(Rect::MakeLTRB(0, 0, 100, 100), + /*is_axis_aligned_rect=*/false), + Matrix(), {0, 0}, 0, 100); + EXPECT_EQ(recorder.GetReplayEntities().size(), 1u); - recorder.RecordEntity(entity, Contents::ClipCoverage::Type::kAppend, - Rect::MakeLTRB(0, 0, 50, 50)); + recorder.RecordClip( + ClipContents(Rect::MakeLTRB(0, 0, 50, 50), /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 2, 100); + EXPECT_EQ(recorder.GetReplayEntities().size(), 2u); ASSERT_TRUE(recorder.GetReplayEntities()[0].clip_coverage.has_value()); ASSERT_TRUE(recorder.GetReplayEntities()[1].clip_coverage.has_value()); + // NOLINTBEGIN(bugprone-unchecked-optional-access) EXPECT_EQ(recorder.GetReplayEntities()[0].clip_coverage.value(), Rect::MakeLTRB(0, 0, 100, 100)); @@ -36,10 +38,10 @@ TEST(EntityPassClipStackTest, CanPushAndPopEntities) { Rect::MakeLTRB(0, 0, 50, 50)); // NOLINTEND(bugprone-unchecked-optional-access) - recorder.RecordEntity(entity, Contents::ClipCoverage::Type::kRestore, Rect()); + recorder.RecordRestore({0, 0}, 1); EXPECT_EQ(recorder.GetReplayEntities().size(), 1u); - recorder.RecordEntity(entity, Contents::ClipCoverage::Type::kRestore, Rect()); + recorder.RecordRestore({0, 0}, 0); EXPECT_TRUE(recorder.GetReplayEntities().empty()); } @@ -49,46 +51,10 @@ TEST(EntityPassClipStackTest, CanPopEntitiesSafely) { EXPECT_TRUE(recorder.GetReplayEntities().empty()); - Entity entity; - recorder.RecordEntity(entity, Contents::ClipCoverage::Type::kRestore, Rect()); + recorder.RecordRestore({0, 0}, 0); EXPECT_TRUE(recorder.GetReplayEntities().empty()); } -TEST(EntityPassClipStackTest, CanAppendNoChange) { - EntityPassClipStack recorder = - EntityPassClipStack(Rect::MakeLTRB(0, 0, 100, 100)); - - EXPECT_TRUE(recorder.GetReplayEntities().empty()); - - Entity entity; - recorder.RecordEntity(entity, Contents::ClipCoverage::Type::kNoChange, - Rect()); - EXPECT_TRUE(recorder.GetReplayEntities().empty()); -} - -TEST(EntityPassClipStackTest, AppendCoverageNoChange) { - EntityPassClipStack recorder = - EntityPassClipStack(Rect::MakeLTRB(0, 0, 100, 100)); - - EXPECT_EQ(recorder.GetClipCoverageLayers()[0].coverage, - Rect::MakeSize(Size::MakeWH(100, 100))); - EXPECT_EQ(recorder.GetClipCoverageLayers()[0].clip_height, 0u); - - Entity entity; - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kNoChange, - .coverage = std::nullopt, - }, - entity, 0, Point(0, 0)); - EXPECT_TRUE(result.should_render); - EXPECT_FALSE(result.clip_did_change); - - EXPECT_EQ(recorder.GetClipCoverageLayers()[0].coverage, - Rect::MakeSize(Size::MakeWH(100, 100))); - EXPECT_EQ(recorder.GetClipCoverageLayers()[0].clip_height, 0u); -} - TEST(EntityPassClipStackTest, AppendAndRestoreClipCoverage) { EntityPassClipStack recorder = EntityPassClipStack(Rect::MakeLTRB(0, 0, 100, 100)); @@ -97,12 +63,10 @@ TEST(EntityPassClipStackTest, AppendAndRestoreClipCoverage) { // Push a clip. Entity entity; - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(50, 50, 55, 55), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordClip(ClipContents(Rect::MakeLTRB(50, 50, 55, 55), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); @@ -113,15 +77,7 @@ TEST(EntityPassClipStackTest, AppendAndRestoreClipCoverage) { EXPECT_EQ(recorder.GetReplayEntities().size(), 1u); // Restore the clip. - auto restore_clip = std::make_shared(); - restore_clip->SetRestoreHeight(0); - entity.SetContents(std::move(restore_clip)); - recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kRestore, - .coverage = Rect::MakeLTRB(50, 50, 55, 55), - }, - entity, 0, Point(0, 0)); + recorder.RecordRestore({0, 0}, 0); ASSERT_EQ(recorder.GetClipCoverageLayers().size(), 1u); EXPECT_EQ(recorder.GetClipCoverageLayers()[0].coverage, @@ -139,23 +95,18 @@ TEST(EntityPassClipStackTest, AppendLargerClipCoverage) { ASSERT_EQ(recorder.GetClipCoverageLayers().size(), 1u); // Push a clip. - Entity entity; - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(50, 50, 55, 55), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordClip(ClipContents(Rect::MakeLTRB(50, 50, 55, 55), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); + EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); // Push a clip with larger coverage than the previous state. - result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(0, 0, 100, 100), - }, - entity, 0, Point(0, 0)); + result = recorder.RecordClip(ClipContents(Rect::MakeLTRB(0, 0, 100, 100), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 1, 100); EXPECT_FALSE(result.should_render); EXPECT_FALSE(result.clip_did_change); @@ -171,24 +122,18 @@ TEST(EntityPassClipStackTest, ASSERT_EQ(recorder.GetClipCoverageLayers().size(), 1u); // Push a clip. - Entity entity; - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(50, 50, 55, 55), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordClip(ClipContents(Rect::MakeLTRB(50, 50, 55, 55), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); + EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); // Push a clip with larger coverage than the previous state. - result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .is_difference_or_non_square = true, - .coverage = Rect::MakeLTRB(0, 0, 100, 100), - }, - entity, 0, Point(0, 0)); + result = recorder.RecordClip(ClipContents(Rect::MakeLTRB(0, 0, 100, 100), + /*is_axis_aligned_rect=*/false), + Matrix(), {0, 0}, 0, 100); EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); @@ -204,12 +149,11 @@ TEST(EntityPassClipStackTest, AppendDecreasingSizeClipCoverage) { Entity entity; for (auto i = 1; i < 20; i++) { - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(i, i, 100 - i, 100 - i), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordClip(ClipContents(Rect::MakeLTRB(i, i, 100 - i, 100 - i), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); + EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); EXPECT_EQ(recorder.CurrentClipCoverage(), @@ -224,15 +168,13 @@ TEST(EntityPassClipStackTest, AppendIncreasingSizeClipCoverage) { ASSERT_EQ(recorder.GetClipCoverageLayers().size(), 1u); // Push Clips that grow in size. All should be skipped. - Entity entity; for (auto i = 1; i < 20; i++) { - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(0 - i, 0 - i, 100 + i, 100 + i), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = recorder.RecordClip( + ClipContents(Rect::MakeLTRB(0 - i, 0 - i, 100 + i, 100 + i), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); + EXPECT_FALSE(result.should_render); EXPECT_FALSE(result.clip_did_change); EXPECT_EQ(recorder.CurrentClipCoverage(), Rect::MakeLTRB(0, 0, 100, 100)); @@ -246,16 +188,8 @@ TEST(EntityPassClipStackTest, UnbalancedRestore) { ASSERT_EQ(recorder.GetClipCoverageLayers().size(), 1u); // Restore the clip. - Entity entity; - auto restore_clip = std::make_shared(); - restore_clip->SetRestoreHeight(0); - entity.SetContents(std::move(restore_clip)); - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kRestore, - .coverage = Rect::MakeLTRB(50, 50, 55, 55), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordRestore(Point(0, 0), 0); EXPECT_FALSE(result.should_render); EXPECT_FALSE(result.clip_did_change); @@ -273,14 +207,12 @@ TEST(EntityPassClipStackTest, ClipAndRestoreWithSubpasses) { ASSERT_EQ(recorder.GetClipCoverageLayers().size(), 1u); // Push a clip. - Entity entity; { - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(50, 50, 55, 55), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordClip(ClipContents(Rect::MakeLTRB(50, 50, 55, 55), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); + EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); } @@ -298,12 +230,11 @@ TEST(EntityPassClipStackTest, ClipAndRestoreWithSubpasses) { Rect::MakeLTRB(50, 50, 55, 55)); { - EntityPassClipStack::ClipStateResult result = recorder.ApplyClipState( - Contents::ClipCoverage{ - .type = Contents::ClipCoverage::Type::kAppend, - .coverage = Rect::MakeLTRB(54, 54, 55, 55), - }, - entity, 0, Point(0, 0)); + EntityPassClipStack::ClipStateResult result = + recorder.RecordClip(ClipContents(Rect::MakeLTRB(54, 54, 55, 55), + /*is_axis_aligned_rect=*/true), + Matrix(), {0, 0}, 0, 100); + EXPECT_TRUE(result.should_render); EXPECT_TRUE(result.clip_did_change); } diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents.cc b/engine/src/flutter/impeller/entity/contents/clip_contents.cc index ee4535ed70..16e7268750 100644 --- a/engine/src/flutter/impeller/entity/contents/clip_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/clip_contents.cc @@ -16,81 +16,68 @@ namespace impeller { -static Scalar GetShaderClipDepth(const Entity& entity) { +static Scalar GetShaderClipDepth(uint32_t clip_depth) { // Draw the clip at the max of the clip entity's depth slice, so that other // draw calls with this same depth value will be culled even if they have a // perspective transform. - return std::nextafterf(Entity::GetShaderClipDepth(entity.GetClipDepth() + 1), - 0.0f); + return std::nextafterf(Entity::GetShaderClipDepth(clip_depth + 1), 0.0f); } /******************************************************************************* ******* ClipContents ******************************************************************************/ -ClipContents::ClipContents() = default; +ClipContents::ClipContents(Rect coverage_rect, bool is_axis_aligned_rect) + : coverage_rect_(coverage_rect), + is_axis_aligned_rect_(is_axis_aligned_rect) {} ClipContents::~ClipContents() = default; -void ClipContents::SetGeometry(const Geometry* geometry) { - geometry_ = geometry; +void ClipContents::SetGeometry(GeometryResult clip_geometry) { + clip_geometry_ = std::move(clip_geometry); } void ClipContents::SetClipOperation(Entity::ClipOperation clip_op) { clip_op_ = clip_op; } -std::optional ClipContents::GetCoverage(const Entity& entity) const { - return std::nullopt; -}; - -Contents::ClipCoverage ClipContents::GetClipCoverage( - const Entity& entity, +ClipCoverage ClipContents::GetClipCoverage( const std::optional& current_clip_coverage) const { if (!current_clip_coverage.has_value()) { - return {.type = ClipCoverage::Type::kAppend, .coverage = std::nullopt}; + return {.coverage = std::nullopt}; } switch (clip_op_) { case Entity::ClipOperation::kDifference: // This can be optimized further by considering cases when the bounds of // the current stencil will shrink. return { - .type = ClipCoverage::Type::kAppend, // .is_difference_or_non_square = true, // .coverage = current_clip_coverage // }; case Entity::ClipOperation::kIntersect: - if (!geometry_) { - return {.type = ClipCoverage::Type::kAppend, .coverage = std::nullopt}; - } - auto coverage = geometry_->GetCoverage(entity.GetTransform()); - if (!coverage.has_value() || !current_clip_coverage.has_value()) { - return {.type = ClipCoverage::Type::kAppend, .coverage = std::nullopt}; + if (coverage_rect_.IsEmpty() || !current_clip_coverage.has_value()) { + return {.coverage = std::nullopt}; } return { - .type = ClipCoverage::Type::kAppend, // - .is_difference_or_non_square = !geometry_->IsAxisAlignedRect(), // - .coverage = current_clip_coverage->Intersection(coverage.value()), // + .is_difference_or_non_square = !is_axis_aligned_rect_, // + .coverage = current_clip_coverage->Intersection(coverage_rect_), // }; } FML_UNREACHABLE(); } -void ClipContents::SetInheritedOpacity(Scalar opacity) {} - bool ClipContents::Render(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) const { - if (!geometry_) { + RenderPass& pass, + uint32_t clip_depth) const { + if (!clip_geometry_.vertex_buffer) { return true; } using VS = ClipPipeline::VertexShader; VS::FrameInfo info; - info.depth = GetShaderClipDepth(entity); + info.depth = GetShaderClipDepth(clip_depth); - auto geometry_result = geometry_->GetPositionBuffer(renderer, entity, pass); auto options = OptionsFromPass(pass); options.blend_mode = BlendMode::kDestination; @@ -99,9 +86,9 @@ bool ClipContents::Render(const ContentContext& renderer, /// Stencil preparation draw. options.depth_write_enabled = false; - options.primitive_type = geometry_result.type; - pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer)); - switch (geometry_result.mode) { + options.primitive_type = clip_geometry_.type; + pass.SetVertexBuffer(clip_geometry_.vertex_buffer); + switch (clip_geometry_.mode) { case GeometryResult::Mode::kNonZero: pass.SetCommandLabel("Clip stencil preparation (NonZero)"); options.stencil_mode = @@ -121,7 +108,7 @@ bool ClipContents::Render(const ContentContext& renderer, } pass.SetPipeline(renderer.GetClipPipeline(options)); - info.mvp = geometry_result.transform; + info.mvp = clip_geometry_.transform; VS::BindFrameInfo(pass, renderer.GetTransientsBuffer().EmplaceUniform(info)); if (!pass.Draw().ok()) { @@ -143,12 +130,7 @@ bool ClipContents::Render(const ContentContext& renderer, case Entity::ClipOperation::kDifference: pass.SetCommandLabel("Difference Clip"); options.stencil_mode = ContentContextOptions::StencilMode::kCoverCompare; - std::optional maybe_cover_area = - geometry_->GetCoverage(entity.GetTransform()); - if (!maybe_cover_area.has_value()) { - return true; - } - cover_area = maybe_cover_area.value(); + cover_area = coverage_rect_; break; } auto points = cover_area.GetPoints(); @@ -167,39 +149,10 @@ bool ClipContents::Render(const ContentContext& renderer, ******* ClipRestoreContents ******************************************************************************/ -ClipRestoreContents::ClipRestoreContents() = default; - -ClipRestoreContents::~ClipRestoreContents() = default; - -void ClipRestoreContents::SetRestoreHeight(size_t clip_height) { - restore_height_ = clip_height; -} - -size_t ClipRestoreContents::GetRestoreHeight() const { - return restore_height_; -} - -void ClipRestoreContents::SetRestoreCoverage( - std::optional restore_coverage) { - restore_coverage_ = restore_coverage; -} - -std::optional ClipRestoreContents::GetCoverage( - const Entity& entity) const { - return std::nullopt; -}; - -Contents::ClipCoverage ClipRestoreContents::GetClipCoverage( - const Entity& entity, - const std::optional& current_clip_coverage) const { - return {.type = ClipCoverage::Type::kRestore, .coverage = std::nullopt}; -} - -void ClipRestoreContents::SetInheritedOpacity(Scalar opacity) {} - -bool ClipRestoreContents::Render(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) const { +bool RenderClipRestore(const ContentContext& renderer, + RenderPass& pass, + uint32_t clip_depth, + std::optional restore_coverage) { using VS = ClipPipeline::VertexShader; pass.SetCommandLabel("Restore Clip"); @@ -214,7 +167,7 @@ bool ClipRestoreContents::Render(const ContentContext& renderer, // Create a rect that covers either the given restore area, or the whole // render target texture. auto ltrb = - restore_coverage_.value_or(Rect::MakeSize(pass.GetRenderTargetSize())) + restore_coverage.value_or(Rect::MakeSize(pass.GetRenderTargetSize())) .GetLTRB(); std::array vertices = { @@ -227,7 +180,7 @@ bool ClipRestoreContents::Render(const ContentContext& renderer, CreateVertexBuffer(vertices, renderer.GetTransientsBuffer())); VS::FrameInfo info; - info.depth = GetShaderClipDepth(entity); + info.depth = GetShaderClipDepth(clip_depth); info.mvp = pass.GetOrthographicTransform(); VS::BindFrameInfo(pass, renderer.GetTransientsBuffer().EmplaceUniform(info)); diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents.h b/engine/src/flutter/impeller/entity/contents/clip_contents.h index 51e76644f4..8690bcb657 100644 --- a/engine/src/flutter/impeller/entity/contents/clip_contents.h +++ b/engine/src/flutter/impeller/entity/contents/clip_contents.h @@ -11,82 +11,68 @@ namespace impeller { -class ClipContents final : public Contents { +struct ClipCoverage { + // TODO(jonahwilliams): this should probably use the Entity::ClipOperation + // enum, but that has transitive import errors. + bool is_difference_or_non_square = false; + + /// @brief This coverage is the outer coverage of the clip. + /// + /// For example, if the clip is a circular clip, this is the rectangle that + /// contains the circle and not the rectangle that is contained within the + /// circle. This means that we cannot use the coverage alone to determine if + /// a clip can be culled, and instead also use the somewhat hacky + /// "is_difference_or_non_square" field. + std::optional coverage = std::nullopt; +}; + +class ClipContents { public: - ClipContents(); + ClipContents(Rect coverage_rect, bool is_axis_aligned_rect); ~ClipContents(); - void SetGeometry(const Geometry* geometry); + /// @brief Set the pre-tessellated clip geometry. + void SetGeometry(GeometryResult geometry); void SetClipOperation(Entity::ClipOperation clip_op); - // |Contents| - std::optional GetCoverage(const Entity& entity) const override; - - // |Contents| - ClipCoverage GetClipCoverage( - const Entity& entity, - const std::optional& current_clip_coverage) const override; - - // |Contents| - bool Render(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) const override; - - // |Contents| - void SetInheritedOpacity(Scalar opacity) override; - - private: - const Geometry* geometry_ = nullptr; - Entity::ClipOperation clip_op_ = Entity::ClipOperation::kIntersect; - - ClipContents(const ClipContents&) = delete; - - ClipContents& operator=(const ClipContents&) = delete; -}; - -class ClipRestoreContents final : public Contents { - public: - ClipRestoreContents(); - - ~ClipRestoreContents(); - - void SetRestoreHeight(size_t clip_height); - - size_t GetRestoreHeight() const; - - /// @brief The area on the pass texture where this clip restore will be - /// applied. If unset, the entire pass texture will be restored. + //---------------------------------------------------------------------------- + /// @brief Given the current pass space bounding rectangle of the clip + /// buffer, return the expected clip coverage after this draw call. + /// This should only be implemented for contents that may write to the + /// clip buffer. + /// + /// During rendering, coverage coordinates count pixels from the top + /// left corner of the framebuffer. /// - /// @note This rectangle is not transformed by the entity's transform. - void SetRestoreCoverage(std::optional coverage); - - // |Contents| - std::optional GetCoverage(const Entity& entity) const override; - - // |Contents| ClipCoverage GetClipCoverage( - const Entity& entity, - const std::optional& current_clip_coverage) const override; + const std::optional& current_clip_coverage) const; - // |Contents| bool Render(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) const override; - - // |Contents| - void SetInheritedOpacity(Scalar opacity) override; + RenderPass& pass, + uint32_t clip_depth) const; private: - std::optional restore_coverage_; - size_t restore_height_ = 0; - - ClipRestoreContents(const ClipRestoreContents&) = delete; - - ClipRestoreContents& operator=(const ClipRestoreContents&) = delete; + // Pre-tessellated clip geometry. + GeometryResult clip_geometry_; + // Coverage rect of the tessellated geometry. + Rect coverage_rect_; + bool is_axis_aligned_rect_ = false; + Entity::ClipOperation clip_op_ = Entity::ClipOperation::kIntersect; }; +/// @brief Render a restore clip. +/// +/// This is is intended to be used for prevent overdraw mechanism. The clip +/// depth should be the depth of the entity that is currently being drawn, and +/// restore_coverage should be its coverage. If restore_coverage is +/// std::nullopt, the render pass coverage is used instead. +bool RenderClipRestore(const ContentContext& renderer, + RenderPass& pass, + uint32_t clip_depth, + std::optional restore_coverage); + } // namespace impeller #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_CLIP_CONTENTS_H_ diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents_unittests.cc b/engine/src/flutter/impeller/entity/contents/clip_contents_unittests.cc deleted file mode 100644 index 21070a0bc5..0000000000 --- a/engine/src/flutter/impeller/entity/contents/clip_contents_unittests.cc +++ /dev/null @@ -1,54 +0,0 @@ -// 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 -#include - -#include "gtest/gtest.h" - -#include "impeller/entity/contents/clip_contents.h" -#include "impeller/entity/contents/content_context.h" -#include "impeller/entity/contents/contents.h" -#include "impeller/entity/contents/test/recording_render_pass.h" -#include "impeller/entity/entity.h" -#include "impeller/entity/entity_playground.h" -#include "impeller/renderer/render_target.h" - -namespace impeller { -namespace testing { - -using EntityTest = EntityPlayground; - -TEST_P(EntityTest, ClipContentsOptimizesFullScreenIntersectClips) { - // Set up mock environment. - - auto content_context = GetContentContext(); - auto buffer = content_context->GetContext()->CreateCommandBuffer(); - auto render_target = - GetContentContext()->GetRenderTargetCache()->CreateOffscreenMSAA( - *content_context->GetContext(), {100, 100}, - /*mip_count=*/1); - auto render_pass = buffer->CreateRenderPass(render_target); - auto recording_pass = std::make_shared( - render_pass, GetContext(), render_target); - - // Set up clip contents. - - auto contents = std::make_shared(); - contents->SetClipOperation(Entity::ClipOperation::kIntersect); - auto geom = Geometry::MakeCover(); - contents->SetGeometry(geom.get()); - - Entity entity; - entity.SetContents(std::move(contents)); - - // Render the clip contents. - - ASSERT_TRUE(recording_pass->GetCommands().empty()); - ASSERT_TRUE(entity.Render(*content_context, *recording_pass)); - ASSERT_FALSE(recording_pass->GetCommands().empty()); -} - -} // namespace testing -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/color_source_contents.h b/engine/src/flutter/impeller/entity/contents/color_source_contents.h index 52985493f7..2dc1ec37f4 100644 --- a/engine/src/flutter/impeller/entity/contents/color_source_contents.h +++ b/engine/src/flutter/impeller/entity/contents/color_source_contents.h @@ -260,10 +260,8 @@ class ColorSourceContents : public Contents { // was incremented by 1 in order to self-clip. So simply append a clip // restore to clean it up. if (geometry_result.mode == GeometryResult::Mode::kPreventOverdraw) { - auto restore = ClipRestoreContents(); - restore.SetRestoreCoverage(GetCoverage(entity)); - Entity restore_entity = entity.Clone(); - return restore.Render(renderer, restore_entity, pass); + return RenderClipRestore(renderer, pass, entity.GetClipDepth(), + GetCoverage(entity)); } return true; } diff --git a/engine/src/flutter/impeller/entity/contents/contents.cc b/engine/src/flutter/impeller/entity/contents/contents.cc index abebbfc68b..7a07da9d13 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.cc +++ b/engine/src/flutter/impeller/entity/contents/contents.cc @@ -53,13 +53,6 @@ bool Contents::IsOpaque(const Matrix& transform) const { return false; } -Contents::ClipCoverage Contents::GetClipCoverage( - const Entity& entity, - const std::optional& current_clip_coverage) const { - return {.type = ClipCoverage::Type::kNoChange, - .coverage = current_clip_coverage}; -} - std::optional Contents::RenderToSnapshot( const ContentContext& renderer, const Entity& entity, diff --git a/engine/src/flutter/impeller/entity/contents/contents.h b/engine/src/flutter/impeller/entity/contents/contents.h index 1577f3054f..31a8aed38f 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.h +++ b/engine/src/flutter/impeller/entity/contents/contents.h @@ -34,24 +34,6 @@ class Contents { /// unpremultiplied color. using ColorFilterProc = std::function; - struct ClipCoverage { - enum class Type { kNoChange, kAppend, kRestore }; - - Type type = Type::kNoChange; - // TODO(jonahwilliams): this should probably use the Entity::ClipOperation - // enum, but that has transitive import errors. - bool is_difference_or_non_square = false; - - /// @brief This coverage is the outer coverage of the clip. - /// - /// For example, if the clip is a circular clip, this is the rectangle that - /// contains the circle and not the rectangle that is contained within the - /// circle. This means that we cannot use the coverage alone to determine if - /// a clip can be culled, and instead also use the somewhat hacky - /// "is_difference_or_non_square" field. - std::optional coverage = std::nullopt; - }; - using RenderProc = std::function; @@ -100,19 +82,6 @@ class Contents { /// render this contents. virtual bool IsOpaque(const Matrix& transform) const; - //---------------------------------------------------------------------------- - /// @brief Given the current pass space bounding rectangle of the clip - /// buffer, return the expected clip coverage after this draw call. - /// This should only be implemented for contents that may write to the - /// clip buffer. - /// - /// During rendering, coverage coordinates count pixels from the top - /// left corner of the framebuffer. - /// - virtual ClipCoverage GetClipCoverage( - const Entity& entity, - const std::optional& current_clip_coverage) const; - //---------------------------------------------------------------------------- /// @brief Render this contents to a snapshot, respecting the entity's /// transform, path, clip depth, and blend mode. diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc index 3e592636b8..e66c830ac7 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc @@ -512,27 +512,33 @@ Entity ApplyClippedBlurStyle(Entity::ClipOperation clip_operation, const Snapshot& input_snapshot, Entity blur_entity, const Geometry* geometry) { - auto clip_contents = std::make_shared(); - clip_contents->SetClipOperation(clip_operation); - clip_contents->SetGeometry(geometry); - Entity clipper; - clipper.SetContents(clip_contents); - auto restore = std::make_unique(); Matrix entity_transform = entity.GetTransform(); Matrix blur_transform = blur_entity.GetTransform(); - auto renderer = fml::MakeCopyable( - [blur_entity = blur_entity.Clone(), clipper = std::move(clipper), - restore = std::move(restore), entity_transform, - blur_transform](const ContentContext& renderer, const Entity& entity, - RenderPass& pass) mutable { - bool result = true; + + auto renderer = + fml::MakeCopyable([blur_entity = blur_entity.Clone(), clip_operation, + entity_transform, blur_transform, geometry]( + const ContentContext& renderer, + const Entity& entity, RenderPass& pass) mutable { + Entity clipper; clipper.SetClipDepth(entity.GetClipDepth()); clipper.SetTransform(entity.GetTransform() * entity_transform); - result = clipper.Render(renderer, pass) && result; + + auto geom_result = geometry->GetPositionBuffer(renderer, clipper, pass); + + ClipContents clip_contents(geometry->GetCoverage(clipper.GetTransform()) + .value_or(Rect::MakeLTRB(0, 0, 0, 0)), + /*is_axis_aligned_rect=*/false); + clip_contents.SetClipOperation(clip_operation); + clip_contents.SetGeometry(std::move(geom_result)); + + if (!clip_contents.Render(renderer, pass, entity.GetClipDepth())) { + return false; + } blur_entity.SetClipDepth(entity.GetClipDepth()); blur_entity.SetTransform(entity.GetTransform() * blur_transform); - result = blur_entity.Render(renderer, pass) && result; - return result; + + return blur_entity.Render(renderer, pass); }); auto coverage = fml::MakeCopyable([blur_entity = std::move(blur_entity), @@ -582,15 +588,15 @@ Entity ApplyBlurStyle(FilterContents::BlurStyle blur_style, const ContentContext& renderer, const Entity& entity, RenderPass& pass) mutable { - bool result = true; snapshot_entity.SetTransform(entity.GetTransform() * snapshot_transform); snapshot_entity.SetClipDepth(entity.GetClipDepth()); - result = result && snapshot_entity.Render(renderer, pass); + if (!snapshot_entity.Render(renderer, pass)) { + return false; + } blur_entity.SetClipDepth(entity.GetClipDepth()); blur_entity.SetTransform(entity.GetTransform() * blurred_transform); - result = result && blur_entity.Render(renderer, pass); - return result; + return blur_entity.Render(renderer, pass); }), fml::MakeCopyable([blur_entity = blur_entity.Clone(), blurred_transform](const Entity& entity) mutable { diff --git a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc index 6fdd23fa69..9284f01cd6 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc @@ -7,7 +7,6 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity.h" #include "impeller/entity/geometry/geometry.h" -#include "impeller/geometry/path.h" #include "impeller/renderer/render_pass.h" namespace impeller { diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 5f06a309a7..5c7fbddb3d 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -5,12 +5,9 @@ #include "impeller/entity/entity.h" #include -#include #include -#include "impeller/base/validation.h" #include "impeller/entity/contents/content_context.h" -#include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/texture_contents.h" #include "impeller/geometry/color.h" #include "impeller/geometry/vector.h" @@ -72,14 +69,6 @@ std::optional Entity::GetCoverage() const { return contents_->GetCoverage(*this); } -Contents::ClipCoverage Entity::GetClipCoverage( - const std::optional& current_clip_coverage) const { - if (!contents_) { - return {}; - } - return contents_->GetClipCoverage(*this, current_clip_coverage); -} - void Entity::SetContents(std::shared_ptr contents) { contents_ = std::move(contents); } diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 86d2e0b329..dd3cc6ad44 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -92,9 +92,6 @@ class Entity { std::optional GetCoverage() const; - Contents::ClipCoverage GetClipCoverage( - const std::optional& current_clip_coverage) const; - void SetContents(std::shared_ptr contents); const std::shared_ptr& GetContents() const; diff --git a/engine/src/flutter/impeller/entity/entity_pass_clip_stack.cc b/engine/src/flutter/impeller/entity/entity_pass_clip_stack.cc index 1f3749ce3b..baea19e552 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_clip_stack.cc +++ b/engine/src/flutter/impeller/entity/entity_pass_clip_stack.cc @@ -6,7 +6,6 @@ #include "flutter/fml/logging.h" #include "impeller/entity/contents/clip_contents.h" -#include "impeller/entity/entity.h" namespace impeller { @@ -52,128 +51,124 @@ EntityPassClipStack::GetClipCoverageLayers() const { return subpass_state_.back().clip_coverage; } -EntityPassClipStack::ClipStateResult EntityPassClipStack::ApplyClipState( - Contents::ClipCoverage global_clip_coverage, - Entity& entity, - size_t clip_height_floor, - Point global_pass_position) { +EntityPassClipStack::ClipStateResult EntityPassClipStack::RecordRestore( + Point global_pass_position, + size_t restore_height) { ClipStateResult result = {.should_render = false, .clip_did_change = false}; - auto& subpass_state = GetCurrentSubpassState(); - switch (global_clip_coverage.type) { - case Contents::ClipCoverage::Type::kNoChange: - break; - case Contents::ClipCoverage::Type::kAppend: { - auto maybe_coverage = CurrentClipCoverage(); - // Compute the previous clip height. - size_t previous_clip_height = 0; - if (!subpass_state.clip_coverage.empty()) { - previous_clip_height = subpass_state.clip_coverage.back().clip_height; - } else { - // If there is no clip coverage, then the previous clip height is the - // clip height floor. - previous_clip_height = clip_height_floor; - } - - if (!maybe_coverage.has_value()) { - // Running this append op won't impact the clip buffer because the - // whole screen is already being clipped, so skip it. - return result; - } - auto op = maybe_coverage.value(); - - // If the new clip coverage is bigger than the existing coverage for - // intersect clips, we do not need to change the clip region. - if (!global_clip_coverage.is_difference_or_non_square && - global_clip_coverage.coverage.has_value() && - global_clip_coverage.coverage.value().Contains(op)) { - subpass_state.clip_coverage.push_back(ClipCoverageLayer{ - .coverage = op, .clip_height = previous_clip_height + 1}); - - return result; - } - - subpass_state.clip_coverage.push_back( - ClipCoverageLayer{.coverage = global_clip_coverage.coverage, - .clip_height = previous_clip_height + 1}); - result.clip_did_change = true; - - FML_DCHECK(subpass_state.clip_coverage.back().clip_height == - subpass_state.clip_coverage.front().clip_height + - subpass_state.clip_coverage.size() - 1); - - } break; - case Contents::ClipCoverage::Type::kRestore: { - ClipRestoreContents* restore_contents = - reinterpret_cast(entity.GetContents().get()); - size_t restore_height = restore_contents->GetRestoreHeight(); - - if (subpass_state.clip_coverage.back().clip_height <= restore_height) { - // Drop clip restores that will do nothing. - return result; - } - - auto restoration_index = - restore_height - subpass_state.clip_coverage.front().clip_height; - FML_DCHECK(restoration_index < subpass_state.clip_coverage.size()); - - // We only need to restore the area that covers the coverage of the - // clip rect at target height + 1. - std::optional restore_coverage = - (restoration_index + 1 < subpass_state.clip_coverage.size()) - ? subpass_state.clip_coverage[restoration_index + 1].coverage - : std::nullopt; - if (restore_coverage.has_value()) { - // Make the coverage rectangle relative to the current pass. - restore_coverage = restore_coverage->Shift(-global_pass_position); - } - subpass_state.clip_coverage.resize(restoration_index + 1); - result.clip_did_change = true; - - // Skip all clip restores when stencil-then-cover is enabled. - if (subpass_state.clip_coverage.back().coverage.has_value()) { - RecordEntity(entity, global_clip_coverage.type, Rect()); - } - return result; - - } break; + if (subpass_state.clip_coverage.back().clip_height <= restore_height) { + // Drop clip restores that will do nothing. + return result; } - RecordEntity(entity, global_clip_coverage.type, - subpass_state.clip_coverage.back().coverage); + auto restoration_index = + restore_height - subpass_state.clip_coverage.front().clip_height; + FML_DCHECK(restoration_index < subpass_state.clip_coverage.size()); - result.should_render = true; + // We only need to restore the area that covers the coverage of the + // clip rect at target height + 1. + std::optional restore_coverage = + (restoration_index + 1 < subpass_state.clip_coverage.size()) + ? subpass_state.clip_coverage[restoration_index + 1].coverage + : std::nullopt; + if (restore_coverage.has_value()) { + // Make the coverage rectangle relative to the current pass. + restore_coverage = restore_coverage->Shift(-global_pass_position); + } + + subpass_state.clip_coverage.resize(restoration_index + 1); + result.clip_did_change = true; + + if (subpass_state.clip_coverage.back().coverage.has_value()) { + FML_DCHECK(next_replay_index_ <= + subpass_state.rendered_clip_entities.size()); + if (!subpass_state.rendered_clip_entities.empty()) { + subpass_state.rendered_clip_entities.pop_back(); + + if (next_replay_index_ > subpass_state.rendered_clip_entities.size()) { + next_replay_index_ = subpass_state.rendered_clip_entities.size(); + } + } + } return result; } -void EntityPassClipStack::RecordEntity(const Entity& entity, - Contents::ClipCoverage::Type type, - std::optional clip_coverage) { - auto& subpass_state = GetCurrentSubpassState(); - switch (type) { - case Contents::ClipCoverage::Type::kNoChange: - return; - case Contents::ClipCoverage::Type::kAppend: - FML_DCHECK(next_replay_index_ == - subpass_state.rendered_clip_entities.size()) - << "Not all clips have been replayed before appending new clip."; - subpass_state.rendered_clip_entities.push_back( - {.entity = entity.Clone(), .clip_coverage = clip_coverage}); - next_replay_index_++; - break; - case Contents::ClipCoverage::Type::kRestore: - FML_DCHECK(next_replay_index_ <= - subpass_state.rendered_clip_entities.size()); - if (!subpass_state.rendered_clip_entities.empty()) { - subpass_state.rendered_clip_entities.pop_back(); +EntityPassClipStack::ClipStateResult EntityPassClipStack::RecordClip( + const ClipContents& clip_contents, + Matrix transform, + Point global_pass_position, + uint32_t clip_depth, + size_t clip_height_floor) { + ClipStateResult result = {.should_render = false, .clip_did_change = false}; - if (next_replay_index_ > subpass_state.rendered_clip_entities.size()) { - next_replay_index_ = subpass_state.rendered_clip_entities.size(); - } - } - break; + std::optional maybe_clip_coverage = CurrentClipCoverage(); + // Running this append op won't impact the clip buffer because the + // whole screen is already being clipped, so skip it. + if (!maybe_clip_coverage.has_value()) { + return result; } + auto current_clip_coverage = maybe_clip_coverage.value(); + // Entity transforms are relative to the current pass position, so we need + // to check clip coverage in the same space. + current_clip_coverage = current_clip_coverage.Shift(-global_pass_position); + + ClipCoverage clip_coverage = + clip_contents.GetClipCoverage(current_clip_coverage); + if (clip_coverage.coverage.has_value()) { + clip_coverage.coverage = + clip_coverage.coverage->Shift(global_pass_position); + } + + auto& subpass_state = GetCurrentSubpassState(); + + // Compute the previous clip height. + size_t previous_clip_height = 0; + if (!subpass_state.clip_coverage.empty()) { + previous_clip_height = subpass_state.clip_coverage.back().clip_height; + } else { + // If there is no clip coverage, then the previous clip height is the + // clip height floor. + previous_clip_height = clip_height_floor; + } + + // If the new clip coverage is bigger than the existing coverage for + // intersect clips, we do not need to change the clip region. + if (!clip_coverage.is_difference_or_non_square && + clip_coverage.coverage.has_value() && + clip_coverage.coverage.value().Contains(current_clip_coverage)) { + subpass_state.clip_coverage.push_back(ClipCoverageLayer{ + .coverage = current_clip_coverage, // + .clip_height = previous_clip_height + 1 // + }); + + return result; + } + + subpass_state.clip_coverage.push_back(ClipCoverageLayer{ + .coverage = clip_coverage.coverage, // + .clip_height = previous_clip_height + 1 // + + }); + result.clip_did_change = true; + result.should_render = true; + + FML_DCHECK(subpass_state.clip_coverage.back().clip_height == + subpass_state.clip_coverage.front().clip_height + + subpass_state.clip_coverage.size() - 1); + + FML_DCHECK(next_replay_index_ == subpass_state.rendered_clip_entities.size()) + << "Not all clips have been replayed before appending new clip."; + + subpass_state.rendered_clip_entities.push_back(ReplayResult{ + .clip_contents = clip_contents, // + .transform = transform, // + .clip_coverage = clip_coverage.coverage, // + .clip_depth = clip_depth // + }); + next_replay_index_++; + + return result; } EntityPassClipStack::SubpassState& @@ -199,7 +194,7 @@ EntityPassClipStack::GetNextReplayResult(size_t current_clip_depth) { } ReplayResult* next_replay = &subpass_state_.back().rendered_clip_entities[next_replay_index_]; - if (next_replay->entity.GetClipDepth() < current_clip_depth) { + if (next_replay->clip_depth < current_clip_depth) { // The next replay clip doesn't affect the current entity, so don't replay // it yet. return nullptr; diff --git a/engine/src/flutter/impeller/entity/entity_pass_clip_stack.h b/engine/src/flutter/impeller/entity/entity_pass_clip_stack.h index 9ec26b1d11..4c2e08ed0b 100644 --- a/engine/src/flutter/impeller/entity/entity_pass_clip_stack.h +++ b/engine/src/flutter/impeller/entity/entity_pass_clip_stack.h @@ -5,8 +5,7 @@ #ifndef FLUTTER_IMPELLER_ENTITY_ENTITY_PASS_CLIP_STACK_H_ #define FLUTTER_IMPELLER_ENTITY_ENTITY_PASS_CLIP_STACK_H_ -#include "impeller/entity/contents/contents.h" -#include "impeller/entity/entity.h" +#include "impeller/entity/contents/clip_contents.h" #include "impeller/geometry/rect.h" namespace impeller { @@ -24,8 +23,10 @@ struct ClipCoverageLayer { class EntityPassClipStack { public: struct ReplayResult { - Entity entity; + ClipContents clip_contents; + Matrix transform; std::optional clip_coverage; + uint32_t clip_depth = 0; }; struct ClipStateResult { @@ -50,17 +51,18 @@ class EntityPassClipStack { bool HasCoverage() const; - /// @brief Applies the current clip state to an Entity. If the given Entity - /// is a clip operation, then the clip state is updated accordingly. - ClipStateResult ApplyClipState(Contents::ClipCoverage global_clip_coverage, - Entity& entity, - size_t clip_height_floor, - Point global_pass_position); + ClipStateResult RecordClip(const ClipContents& clip_contents, + Matrix transform, + Point global_pass_position, + uint32_t clip_depth, + size_t clip_height_floor); - // Visible for testing. - void RecordEntity(const Entity& entity, - Contents::ClipCoverage::Type type, - std::optional clip_coverage); + ReplayResult& GetLastReplayResult() { + return GetCurrentSubpassState().rendered_clip_entities.back(); + } + + ClipStateResult RecordRestore(Point global_pass_position, + size_t restore_height); const std::vector& GetReplayEntities() const; diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 6804b1ed48..c8b4bb722e 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -1328,69 +1328,6 @@ TEST_P(EntityTest, SolidFillCoverageIsCorrect) { } } -TEST_P(EntityTest, ClipContentsGetClipCoverageIsCorrect) { - // Intersection: No stencil coverage, no geometry. - { - auto clip = std::make_shared(); - clip->SetClipOperation(Entity::ClipOperation::kIntersect); - auto result = clip->GetClipCoverage(Entity{}, Rect{}); - - ASSERT_FALSE(result.coverage.has_value()); - } - - // Intersection: No stencil coverage, with geometry. - { - auto clip = std::make_shared(); - auto geom = Geometry::MakeFillPath( - PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath()); - clip->SetClipOperation(Entity::ClipOperation::kIntersect); - clip->SetGeometry(geom.get()); - auto result = clip->GetClipCoverage(Entity{}, Rect{}); - - ASSERT_FALSE(result.coverage.has_value()); - } - - // Intersection: With stencil coverage, no geometry. - { - auto clip = std::make_shared(); - clip->SetClipOperation(Entity::ClipOperation::kIntersect); - auto result = - clip->GetClipCoverage(Entity{}, Rect::MakeLTRB(0, 0, 100, 100)); - - ASSERT_FALSE(result.coverage.has_value()); - } - - // Intersection: With stencil coverage, with geometry. - { - auto clip = std::make_shared(); - auto geom = Geometry::MakeFillPath( - PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 50, 50)).TakePath()); - clip->SetClipOperation(Entity::ClipOperation::kIntersect); - clip->SetGeometry(geom.get()); - auto result = - clip->GetClipCoverage(Entity{}, Rect::MakeLTRB(0, 0, 100, 100)); - - ASSERT_TRUE(result.coverage.has_value()); - ASSERT_RECT_NEAR(result.coverage.value(), Rect::MakeLTRB(0, 0, 50, 50)); - ASSERT_EQ(result.type, Contents::ClipCoverage::Type::kAppend); - } - - // Difference: With stencil coverage, with geometry. - { - auto clip = std::make_shared(); - auto geom = Geometry::MakeFillPath( - PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 50, 50)).TakePath()); - clip->SetClipOperation(Entity::ClipOperation::kDifference); - clip->SetGeometry(geom.get()); - auto result = - clip->GetClipCoverage(Entity{}, Rect::MakeLTRB(0, 0, 100, 100)); - - ASSERT_TRUE(result.coverage.has_value()); - ASSERT_RECT_NEAR(result.coverage.value(), Rect::MakeLTRB(0, 0, 100, 100)); - ASSERT_EQ(result.type, Contents::ClipCoverage::Type::kAppend); - } -} - TEST_P(EntityTest, RRectShadowTest) { auto callback = [&](ContentContext& context, RenderPass& pass) { static Color color = Color::Red();