From fa740cad04d4efbbda304e6c9f3ab10f8e780239 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 6 Apr 2022 18:39:19 -0700 Subject: [PATCH] Standardize around "coverage" terminology; make coverage optional everywhere (flutter/engine#118) --- .../impeller/entity/contents/contents.cc | 15 ++-- .../impeller/entity/contents/contents.h | 8 +-- .../contents/filters/filter_contents.cc | 42 +++++++---- .../entity/contents/filters/filter_contents.h | 2 +- .../entity/contents/filters/filter_input.cc | 6 +- .../entity/contents/filters/filter_input.h | 2 +- .../filters/gaussian_blur_filter_contents.cc | 69 ++++++++++++------- .../filters/gaussian_blur_filter_contents.h | 2 +- .../impeller/entity/contents/snapshot.cc | 17 +++-- engine/src/flutter/impeller/entity/entity.cc | 8 ++- engine/src/flutter/impeller/entity/entity.h | 2 +- .../impeller/entity/entity_unittests.cc | 11 +-- 12 files changed, 115 insertions(+), 69 deletions(-) diff --git a/engine/src/flutter/impeller/entity/contents/contents.cc b/engine/src/flutter/impeller/entity/contents/contents.cc index 33c489efb1..e18eb7d3e1 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.cc +++ b/engine/src/flutter/impeller/entity/contents/contents.cc @@ -29,24 +29,27 @@ Contents::Contents() = default; Contents::~Contents() = default; -Rect Contents::GetBounds(const Entity& entity) const { - return entity.GetTransformedPathBounds(); +std::optional Contents::GetCoverage(const Entity& entity) const { + return entity.GetPathCoverage(); } std::optional Contents::RenderToTexture( const ContentContext& renderer, const Entity& entity) const { - auto bounds = GetBounds(entity); + auto bounds = GetCoverage(entity); + if (!bounds.has_value()) { + return std::nullopt; + } auto texture = renderer.MakeSubpass( - ISize::Ceil(bounds.size), + ISize::Ceil(bounds->size), [&contents = *this, &entity, &bounds](const ContentContext& renderer, RenderPass& pass) -> bool { Entity sub_entity; sub_entity.SetPath(entity.GetPath()); sub_entity.SetBlendMode(Entity::BlendMode::kSource); sub_entity.SetTransformation( - Matrix::MakeTranslation(Vector3(-bounds.origin)) * + Matrix::MakeTranslation(Vector3(-bounds->origin)) * entity.GetTransformation()); return contents.Render(renderer, sub_entity, pass); }); @@ -55,7 +58,7 @@ std::optional Contents::RenderToTexture( return std::nullopt; } - return Snapshot{.texture = texture, .position = bounds.origin}; + return Snapshot{.texture = texture, .position = bounds->origin}; } } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/contents.h b/engine/src/flutter/impeller/entity/contents/contents.h index ee9b0b0535..d915891b50 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.h +++ b/engine/src/flutter/impeller/entity/contents/contents.h @@ -36,13 +36,13 @@ class Contents { const Entity& entity, RenderPass& pass) const = 0; - /// @brief Get the bounding rectangle that this contents modifies in screen - /// space. - virtual Rect GetBounds(const Entity& entity) const; + /// @brief Get the screen space bounding rectangle that this contents affects. + virtual std::optional GetCoverage(const Entity& entity) const; /// @brief Render this contents to a texture, respecting the entity's /// transform, path, stencil depth, blend mode, etc. - /// The result texture size is always the size of `GetBounds(entity)`. + /// The result texture size is always the size of + /// `GetCoverage(entity)`. virtual std::optional RenderToTexture( const ContentContext& renderer, const Entity& entity) const; diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc index 59a7149fc0..4829403f2d 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -95,6 +95,11 @@ void FilterContents::SetInputs(FilterInput::Vector inputs) { bool FilterContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { + auto filter_coverage = GetCoverage(entity); + if (!filter_coverage.has_value()) { + return true; + } + // Run the filter. auto maybe_snapshot = RenderToTexture(renderer, entity); @@ -110,49 +115,56 @@ bool FilterContents::Render(const ContentContext& renderer, contents->SetSourceRect(Rect::MakeSize(Size(snapshot.texture->GetSize()))); Entity e; - e.SetPath(PathBuilder{}.AddRect(GetBounds(entity)).GetCurrentPath()); + e.SetPath(PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath()); e.SetBlendMode(entity.GetBlendMode()); e.SetStencilDepth(entity.GetStencilDepth()); return contents->Render(renderer, e, pass); } -Rect FilterContents::GetBounds(const Entity& entity) const { - // The default bounds of FilterContents is just the union of its inputs. - // FilterContents implementations may choose to increase the bounds in any - // direction, but it should never +std::optional FilterContents::GetCoverage(const Entity& entity) const { + // The default coverage of FilterContents is just the union of its inputs' + // coverage. FilterContents implementations may choose to adjust this + // coverage depending on the use case. if (inputs_.empty()) { - return Rect(); + return std::nullopt; } - Rect result = inputs_.front()->GetBounds(entity); - for (auto input_i = inputs_.begin() + 1; input_i < inputs_.end(); input_i++) { - result.Union(input_i->get()->GetBounds(entity)); + std::optional result; + for (const auto& input : inputs_) { + auto coverage = input->GetCoverage(entity); + if (!coverage.has_value()) { + continue; + } + if (!result.has_value()) { + result = coverage; + continue; + } + result = result->Union(result.value()); } - return result; } std::optional FilterContents::RenderToTexture( const ContentContext& renderer, const Entity& entity) const { - auto bounds = GetBounds(entity); - if (bounds.IsZero()) { + auto bounds = GetCoverage(entity); + if (!bounds.has_value() || bounds->IsEmpty()) { return std::nullopt; } // Render the filter into a new texture. auto texture = renderer.MakeSubpass( - ISize(GetBounds(entity).size), + ISize(bounds->size), [=](const ContentContext& renderer, RenderPass& pass) -> bool { - return RenderFilter(inputs_, renderer, entity, pass, bounds); + return RenderFilter(inputs_, renderer, entity, pass, bounds.value()); }); if (!texture) { return std::nullopt; } - return Snapshot{.texture = texture, .position = bounds.origin}; + return Snapshot{.texture = texture, .position = bounds->origin}; } } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h index 63c60bcc44..275ea4d52b 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.h @@ -61,7 +61,7 @@ class FilterContents : public Contents { RenderPass& pass) const override; // |Contents| - Rect GetBounds(const Entity& entity) const override; + std::optional GetCoverage(const Entity& entity) const override; // |Contents| virtual std::optional RenderToTexture( diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc b/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc index eaf75a982a..c3585e6cdc 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc @@ -30,17 +30,17 @@ FilterInput::Variant FilterInput::GetInput() const { return input_; } -Rect FilterInput::GetBounds(const Entity& entity) const { +std::optional FilterInput::GetCoverage(const Entity& entity) const { if (snapshot_) { return Rect(snapshot_->position, Size(snapshot_->texture->GetSize())); } if (auto contents = std::get_if>(&input_)) { - return contents->get()->GetBounds(entity); + return contents->get()->GetCoverage(entity); } if (auto texture = std::get_if>(&input_)) { - return entity.GetTransformedPathBounds(); + return entity.GetPathCoverage(); } FML_UNREACHABLE(); diff --git a/engine/src/flutter/impeller/entity/contents/filters/filter_input.h b/engine/src/flutter/impeller/entity/contents/filters/filter_input.h index b96e7203cc..32b440a8f3 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_input.h +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_input.h @@ -42,7 +42,7 @@ class FilterInput final { Variant GetInput() const; - Rect GetBounds(const Entity& entity) const; + std::optional GetCoverage(const Entity& entity) const; std::optional GetSnapshot(const ContentContext& renderer, const Entity& entity) const; 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 34441ddff9..79a77b4d0a 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 @@ -78,38 +78,55 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter( auto& host_buffer = pass.GetTransientsBuffer(); auto input = inputs[0]->GetSnapshot(renderer, entity); - auto input_bounds = inputs[0]->GetBounds(entity); - auto filter_bounds = GetBounds(entity); + if (!input.has_value()) { + return true; + } + + auto input_bounds = inputs[0]->GetCoverage(entity); + if (!input_bounds.has_value() || input_bounds->IsEmpty()) { + return true; + } + auto filter_bounds = GetCoverage(entity); + if (!filter_bounds.has_value() || filter_bounds->IsEmpty()) { + FML_LOG(ERROR) << "The gaussian blur filter coverage is missing or empty " + "even though the filter's input has coverage."; + return false; + } auto transformed_blur = entity.GetTransformation().TransformDirection(blur_vector_); // LTRB Scalar uv[4] = { - (filter_bounds.GetLeft() - input_bounds.GetLeft()) / - input_bounds.size.width, - (filter_bounds.GetTop() - input_bounds.GetTop()) / - input_bounds.size.height, - 1 + (filter_bounds.GetRight() - input_bounds.GetRight()) / - input_bounds.size.width, - 1 + (filter_bounds.GetBottom() - input_bounds.GetBottom()) / - input_bounds.size.height, + (filter_bounds->GetLeft() - input_bounds->GetLeft()) / + input_bounds->size.width, + (filter_bounds->GetTop() - input_bounds->GetTop()) / + input_bounds->size.height, + 1 + (filter_bounds->GetRight() - input_bounds->GetRight()) / + input_bounds->size.width, + 1 + (filter_bounds->GetBottom() - input_bounds->GetBottom()) / + input_bounds->size.height, }; auto source = source_override_ ? source_override_ : inputs[0]; auto source_texture = source->GetSnapshot(renderer, entity); - auto source_bounds = source->GetBounds(entity); + auto source_bounds = source->GetCoverage(entity); + if (!source_texture.has_value() || !source_bounds.has_value() || + source_bounds->IsEmpty()) { + VALIDATION_LOG << "The gaussian blur source override has no coverage."; + return false; + } // LTRB Scalar uv_src[4] = { - (filter_bounds.GetLeft() - source_bounds.GetLeft()) / - source_bounds.size.width, - (filter_bounds.GetTop() - source_bounds.GetTop()) / - source_bounds.size.height, - 1 + (filter_bounds.GetRight() - source_bounds.GetRight()) / - source_bounds.size.width, - 1 + (filter_bounds.GetBottom() - source_bounds.GetBottom()) / - source_bounds.size.height, + (filter_bounds->GetLeft() - source_bounds->GetLeft()) / + source_bounds->size.width, + (filter_bounds->GetTop() - source_bounds->GetTop()) / + source_bounds->size.height, + 1 + (filter_bounds->GetRight() - source_bounds->GetRight()) / + source_bounds->size.width, + 1 + (filter_bounds->GetBottom() - source_bounds->GetBottom()) / + source_bounds->size.height, }; VertexBufferBuilder vtx_builder; @@ -124,7 +141,7 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter( auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer); VS::FrameInfo frame_info; - frame_info.texture_size = Point(input_bounds.size); + frame_info.texture_size = Point(input_bounds->size); frame_info.blur_radius = transformed_blur.GetLength(); frame_info.blur_direction = transformed_blur.Normalize(); frame_info.src_factor = src_color_factor_; @@ -150,13 +167,17 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter( return pass.AddCommand(cmd); } -Rect DirectionalGaussianBlurFilterContents::GetBounds( +std::optional DirectionalGaussianBlurFilterContents::GetCoverage( const Entity& entity) const { - auto bounds = FilterContents::GetBounds(entity); + auto bounds = FilterContents::GetCoverage(entity); + if (!bounds.has_value()) { + return std::nullopt; + } + auto transformed_blur = entity.GetTransformation().TransformDirection(blur_vector_).Abs(); - auto extent = bounds.size + transformed_blur * 2; - return Rect(bounds.origin - transformed_blur, Size(extent.x, extent.y)); + auto extent = bounds->size + transformed_blur * 2; + return Rect(bounds->origin - transformed_blur, Size(extent.x, extent.y)); } } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h index 0b401d2cc0..1ec3a710a4 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h +++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h @@ -25,7 +25,7 @@ class DirectionalGaussianBlurFilterContents final : public FilterContents { void SetSourceOverride(FilterInput::Ref alpha_mask); // |Contents| - Rect GetBounds(const Entity& entity) const override; + std::optional GetCoverage(const Entity& entity) const override; private: // |FilterContents| diff --git a/engine/src/flutter/impeller/entity/contents/snapshot.cc b/engine/src/flutter/impeller/entity/contents/snapshot.cc index 32dd34b5ef..76809d7cb5 100644 --- a/engine/src/flutter/impeller/entity/contents/snapshot.cc +++ b/engine/src/flutter/impeller/entity/contents/snapshot.cc @@ -4,6 +4,8 @@ #include "impeller/entity/contents/snapshot.h" +#include + #include "impeller/entity/contents/content_context.h" #include "impeller/entity/contents/texture_contents.h" @@ -13,12 +15,15 @@ std::optional Snapshot::FromTransformedTexture( const ContentContext& renderer, const Entity& entity, std::shared_ptr texture) { - Rect bounds = entity.GetTransformedPathBounds(); + auto bounds = entity.GetPathCoverage(); + if (!bounds.has_value()) { + return std::nullopt; + } auto result = renderer.MakeSubpass( - ISize(bounds.size), - [&texture, &entity, bounds](const ContentContext& renderer, - RenderPass& pass) -> bool { + ISize(bounds->size), + [&texture, &entity, &bounds](const ContentContext& renderer, + RenderPass& pass) -> bool { TextureContents contents; contents.SetTexture(texture); contents.SetSourceRect(Rect::MakeSize(Size(texture->GetSize()))); @@ -26,7 +31,7 @@ std::optional Snapshot::FromTransformedTexture( sub_entity.SetPath(entity.GetPath()); sub_entity.SetBlendMode(Entity::BlendMode::kSource); sub_entity.SetTransformation( - Matrix::MakeTranslation(Vector3(-bounds.origin)) * + Matrix::MakeTranslation(Vector3(-bounds->origin)) * entity.GetTransformation()); return contents.Render(renderer, sub_entity, pass); }); @@ -34,7 +39,7 @@ std::optional Snapshot::FromTransformedTexture( return std::nullopt; } - return Snapshot{.texture = result, .position = bounds.origin}; + return Snapshot{.texture = result, .position = bounds->origin}; } } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 95d0e66528..5412df2a14 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -4,6 +4,8 @@ #include "impeller/entity/entity.h" +#include + #include "impeller/base/validation.h" #include "impeller/entity/contents/content_context.h" #include "impeller/renderer/render_pass.h" @@ -30,10 +32,10 @@ void Entity::SetPath(Path path) { path_ = std::move(path); } -Rect Entity::GetTransformedPathBounds() const { +std::optional Entity::GetPathCoverage() const { auto bounds = GetPath().GetBoundingBox(); if (!bounds.has_value()) { - return Rect(); + return std::nullopt; } return bounds->TransformBounds(GetTransformation()); } @@ -51,7 +53,7 @@ std::optional Entity::GetCoverage() const { return std::nullopt; } - return contents_->GetBounds(*this); + return contents_->GetCoverage(*this); } void Entity::SetContents(std::shared_ptr contents) { diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 5c60967157..b4979bb3ed 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -65,7 +65,7 @@ class Entity { void SetPath(Path path); - Rect GetTransformedPathBounds() const; + std::optional GetPathCoverage() const; void SetAddsToCoverage(bool adds); diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 31a68a1aea..ff1d07d278 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -5,6 +5,7 @@ #include #include "flutter/testing/testing.h" +#include "impeller/entity/contents/filters/blend_filter_contents.h" #include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/filters/filter_input.h" #include "impeller/entity/contents/solid_color_contents.h" @@ -760,7 +761,9 @@ TEST_F(EntityTest, GaussianBlurFilter) { // Renders a green bounding rect of the target filter. Entity bounds_entity; bounds_entity.SetPath( - PathBuilder{}.AddRect(target_contents->GetBounds(entity)).TakePath()); + PathBuilder{} + .AddRect(target_contents->GetCoverage(entity).value()) + .TakePath()); bounds_entity.SetContents( SolidColorContents::Make(bounds_color.Premultiply())); bounds_entity.SetTransformation(Matrix()); @@ -779,12 +782,12 @@ TEST_F(EntityTest, SetBlendMode) { ASSERT_EQ(entity.GetBlendMode(), Entity::BlendMode::kClear); } -TEST_F(EntityTest, ContentsGetBoundsForEmptyPathReturnsZero) { +TEST_F(EntityTest, ContentsGetBoundsForEmptyPathReturnsNullopt) { Entity entity; entity.SetContents(std::make_shared()); entity.SetPath({}); - ASSERT_TRUE(entity.GetCoverage()->IsZero()); - ASSERT_TRUE(entity.GetTransformedPathBounds().IsZero()); + ASSERT_FALSE(entity.GetCoverage().has_value()); + ASSERT_FALSE(entity.GetPathCoverage().has_value()); } } // namespace testing