From cc598ce74a98b96f6abb424564f3ad4cb1c38b9d Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Thu, 21 Apr 2022 15:18:47 -0700 Subject: [PATCH] Untie paths from entities (flutter/engine#145) --- engine/src/flutter/impeller/aiks/canvas.cc | 13 ++--- engine/src/flutter/impeller/aiks/paint.cc | 6 ++- engine/src/flutter/impeller/aiks/paint.h | 5 +- .../impeller/aiks/paint_pass_delegate.cc | 4 ++ engine/src/flutter/impeller/entity/BUILD.gn | 2 - .../entity/contents/clear_contents.cc | 34 ------------ .../impeller/entity/contents/clear_contents.h | 33 ------------ .../impeller/entity/contents/clip_contents.cc | 16 +++++- .../impeller/entity/contents/clip_contents.h | 9 ++++ .../impeller/entity/contents/contents.cc | 5 -- .../impeller/entity/contents/contents.h | 2 +- .../contents/filters/filter_contents.cc | 3 +- .../entity/contents/filters/filter_input.cc | 14 +---- .../contents/linear_gradient_contents.cc | 23 +++++--- .../contents/linear_gradient_contents.h | 7 +++ .../entity/contents/solid_color_contents.cc | 26 +++++++-- .../entity/contents/solid_color_contents.h | 13 ++++- .../entity/contents/solid_stroke_contents.cc | 17 +++--- .../entity/contents/solid_stroke_contents.h | 4 ++ .../entity/contents/texture_contents.cc | 12 ++++- .../entity/contents/texture_contents.h | 8 +++ engine/src/flutter/impeller/entity/entity.cc | 16 ------ engine/src/flutter/impeller/entity/entity.h | 7 --- .../flutter/impeller/entity/entity_pass.cc | 4 -- .../impeller/entity/entity_unittests.cc | 53 +++++++++---------- engine/src/flutter/impeller/geometry/path.cc | 8 +++ engine/src/flutter/impeller/geometry/path.h | 2 + 27 files changed, 171 insertions(+), 175 deletions(-) delete mode 100644 engine/src/flutter/impeller/entity/contents/clear_contents.cc delete mode 100644 engine/src/flutter/impeller/entity/contents/clear_contents.h diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index ada6d50cb0..05bc7c06d0 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -8,7 +8,6 @@ #include "flutter/fml/logging.h" #include "impeller/aiks/paint_pass_delegate.h" -#include "impeller/entity/contents/clear_contents.h" #include "impeller/entity/contents/clip_contents.h" #include "impeller/entity/contents/text_contents.h" #include "impeller/entity/contents/texture_contents.h" @@ -111,10 +110,9 @@ void Canvas::RestoreToCount(size_t count) { void Canvas::DrawPath(Path path, Paint paint) { Entity entity; entity.SetTransformation(GetCurrentTransformation()); - entity.SetPath(std::move(path)); entity.SetStencilDepth(GetStencilDepth()); entity.SetBlendMode(paint.blend_mode); - entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity())); + entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity(std::move(path)))); GetCurrentPass().AddEntity(std::move(entity)); } @@ -124,8 +122,7 @@ void Canvas::DrawPaint(Paint paint) { entity.SetTransformation(GetCurrentTransformation()); entity.SetStencilDepth(GetStencilDepth()); entity.SetBlendMode(paint.blend_mode); - entity.SetContents( - std::make_shared(paint.CreateContentsForEntity())); + entity.SetContents(paint.CreateContentsForEntity({}, true)); GetCurrentPass().AddEntity(std::move(entity)); } @@ -158,11 +155,11 @@ void Canvas::SaveLayer(Paint paint, std::optional bounds) { void Canvas::ClipPath(Path path, Entity::ClipOperation clip_op) { auto contents = std::make_shared(); + contents->SetPath(std::move(path)); contents->SetClipOperation(clip_op); Entity entity; entity.SetTransformation(GetCurrentTransformation()); - entity.SetPath(std::move(path)); entity.SetContents(std::move(contents)); entity.SetStencilDepth(GetStencilDepth()); entity.SetAddsToCoverage(false); @@ -178,7 +175,6 @@ void Canvas::RestoreClip() { entity.SetTransformation(GetCurrentTransformation()); // This path is empty because ClipRestoreContents just generates a quad that // takes up the full render target. - entity.SetPath({}); entity.SetContents(std::make_shared()); entity.SetStencilDepth(GetStencilDepth()); entity.SetAddsToCoverage(false); @@ -234,12 +230,12 @@ void Canvas::DrawImageRect(std::shared_ptr image, } auto contents = std::make_shared(); + contents->SetPath(PathBuilder{}.AddRect(dest).TakePath()); contents->SetTexture(image->GetTexture()); contents->SetSourceRect(source); contents->SetSamplerDescriptor(std::move(sampler)); Entity entity; - entity.SetPath(PathBuilder{}.AddRect(dest).TakePath()); entity.SetBlendMode(paint.blend_mode); entity.SetStencilDepth(GetStencilDepth()); entity.SetContents(paint.WithFilters(contents, false)); @@ -293,7 +289,6 @@ void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) { Entity entity; entity.SetTransformation(GetCurrentTransformation() * Matrix::MakeTranslation(position)); - entity.SetPath({}); entity.SetStencilDepth(GetStencilDepth()); entity.SetBlendMode(paint.blend_mode); entity.SetContents(paint.WithFilters(std::move(text_contents), true)); diff --git a/engine/src/flutter/impeller/aiks/paint.cc b/engine/src/flutter/impeller/aiks/paint.cc index e6afd0f419..b34b6787e5 100644 --- a/engine/src/flutter/impeller/aiks/paint.cc +++ b/engine/src/flutter/impeller/aiks/paint.cc @@ -8,7 +8,8 @@ namespace impeller { -std::shared_ptr Paint::CreateContentsForEntity() const { +std::shared_ptr Paint::CreateContentsForEntity(Path path, + bool cover) const { if (contents) { return contents; } @@ -16,11 +17,14 @@ std::shared_ptr Paint::CreateContentsForEntity() const { switch (style) { case Style::kFill: { auto solid_color = std::make_shared(); + solid_color->SetPath(std::move(path)); solid_color->SetColor(color); + solid_color->SetCover(cover); return solid_color; } case Style::kStroke: { auto solid_stroke = std::make_shared(); + solid_stroke->SetPath(std::move(path)); solid_stroke->SetColor(color); solid_stroke->SetStrokeSize(stroke_width); solid_stroke->SetStrokeMiter(stroke_miter); diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 07af116b2a..a6650de47a 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -36,8 +36,6 @@ struct Paint { std::optional mask_blur; std::shared_ptr contents; - std::shared_ptr CreateContentsForEntity() const; - /// @brief Wrap this paint's configured filters to the given contents. /// @param[in] input The contents to wrap with paint's filters. /// @param[in] is_solid_color Affects mask blurring behavior. If false, use @@ -52,6 +50,9 @@ struct Paint { std::shared_ptr WithFilters( std::shared_ptr input, std::optional is_solid_color = std::nullopt) const; + + std::shared_ptr CreateContentsForEntity(Path path = {}, + bool cover = false) const; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc b/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc index 34791be892..dddb6538e3 100644 --- a/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc +++ b/engine/src/flutter/impeller/aiks/paint_pass_delegate.cc @@ -6,6 +6,7 @@ #include "impeller/entity/contents/contents.h" #include "impeller/entity/contents/texture_contents.h" +#include "impeller/geometry/path_builder.h" namespace impeller { @@ -34,6 +35,9 @@ bool PaintPassDelegate::CanCollapseIntoParentPass() { std::shared_ptr PaintPassDelegate::CreateContentsForSubpassTarget( std::shared_ptr target) { auto contents = std::make_shared(); + contents->SetPath(PathBuilder{} + .AddRect(Rect::MakeSize(Size(target->GetSize()))) + .TakePath()); contents->SetTexture(target); contents->SetSourceRect(Rect::MakeSize(Size(target->GetSize()))); contents->SetOpacity(paint_.color.alpha); diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 3add3ae8c2..4f942c39a2 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -31,8 +31,6 @@ impeller_shaders("entity_shaders") { impeller_component("entity") { sources = [ - "contents/clear_contents.cc", - "contents/clear_contents.h", "contents/clip_contents.cc", "contents/clip_contents.h", "contents/content_context.cc", diff --git a/engine/src/flutter/impeller/entity/contents/clear_contents.cc b/engine/src/flutter/impeller/entity/contents/clear_contents.cc deleted file mode 100644 index 6222244f1b..0000000000 --- a/engine/src/flutter/impeller/entity/contents/clear_contents.cc +++ /dev/null @@ -1,34 +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 "impeller/entity/contents/clear_contents.h" - -#include "impeller/entity/entity.h" -#include "impeller/geometry/path_builder.h" -#include "impeller/renderer/render_pass.h" - -namespace impeller { - -ClearContents::ClearContents(std::shared_ptr contents) - : contents_(std::move(contents)) {} - -ClearContents::~ClearContents() = default; - -// |Contents| -bool ClearContents::Render(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) const { - if (contents_ == nullptr) { - return false; - } - // Instead of an entity that doesn't know its size because the render target - // size was unknown to it at construction time, create a copy but substitute - // the contents with the replacements. - Entity clear_entity = entity; - clear_entity.SetPath( - PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()); - return contents_->Render(renderer, clear_entity, pass); -} - -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/clear_contents.h b/engine/src/flutter/impeller/entity/contents/clear_contents.h deleted file mode 100644 index d151b544ca..0000000000 --- a/engine/src/flutter/impeller/entity/contents/clear_contents.h +++ /dev/null @@ -1,33 +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. - -#pragma once - -#include "flutter/fml/macros.h" -#include "impeller/entity/contents/contents.h" - -namespace impeller { - -//------------------------------------------------------------------------------ -/// @brief Disregard existing contents on the render target and use the -/// provided filter to render to the entire target. -/// -class ClearContents final : public Contents { - public: - ClearContents(std::shared_ptr contents); - - ~ClearContents(); - - // |Contents| - bool Render(const ContentContext& renderer, - const Entity& entity, - RenderPass& pass) const override; - - private: - std::shared_ptr contents_; - - FML_DISALLOW_COPY_AND_ASSIGN(ClearContents); -}; - -} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents.cc b/engine/src/flutter/impeller/entity/contents/clip_contents.cc index df9f89ad0b..532b00c1a7 100644 --- a/engine/src/flutter/impeller/entity/contents/clip_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/clip_contents.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include #include "impeller/geometry/path_builder.h" #include "impeller/renderer/formats.h" #include "impeller/renderer/vertex_buffer_builder.h" @@ -23,10 +24,18 @@ ClipContents::ClipContents() = default; ClipContents::~ClipContents() = default; +void ClipContents::SetPath(Path path) { + path_ = std::move(path); +} + void ClipContents::SetClipOperation(Entity::ClipOperation clip_op) { clip_op_ = clip_op; } +std::optional ClipContents::GetCoverage(const Entity& entity) const { + return path_.GetTransformedBoundingBox(entity.GetTransformation()); +}; + bool ClipContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { @@ -77,7 +86,7 @@ bool ClipContents::Render(const ContentContext& renderer, cmd.pipeline = renderer.GetClipPipeline(options); cmd.BindVertices(SolidColorContents::CreateSolidFillVertices( - entity.GetPath(), pass.GetTransientsBuffer())); + path_, pass.GetTransientsBuffer())); info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * entity.GetTransformation(); @@ -95,6 +104,11 @@ ClipRestoreContents::ClipRestoreContents() = default; ClipRestoreContents::~ClipRestoreContents() = default; +std::optional ClipRestoreContents::GetCoverage( + const Entity& entity) const { + return std::nullopt; +}; + bool ClipRestoreContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { diff --git a/engine/src/flutter/impeller/entity/contents/clip_contents.h b/engine/src/flutter/impeller/entity/contents/clip_contents.h index 722b35776c..9153ebb5c0 100644 --- a/engine/src/flutter/impeller/entity/contents/clip_contents.h +++ b/engine/src/flutter/impeller/entity/contents/clip_contents.h @@ -20,14 +20,20 @@ class ClipContents final : public Contents { ~ClipContents(); + void SetPath(Path path); + void SetClipOperation(Entity::ClipOperation clip_op); + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const override; private: + Path path_; Entity::ClipOperation clip_op_ = Entity::ClipOperation::kIntersect; FML_DISALLOW_COPY_AND_ASSIGN(ClipContents); @@ -39,6 +45,9 @@ class ClipRestoreContents final : public Contents { ~ClipRestoreContents(); + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, diff --git a/engine/src/flutter/impeller/entity/contents/contents.cc b/engine/src/flutter/impeller/entity/contents/contents.cc index 98c5a584dc..d5c95a8cbf 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.cc +++ b/engine/src/flutter/impeller/entity/contents/contents.cc @@ -29,10 +29,6 @@ Contents::Contents() = default; Contents::~Contents() = default; -std::optional Contents::GetCoverage(const Entity& entity) const { - return entity.GetPathCoverage(); -} - std::optional Contents::RenderToSnapshot( const ContentContext& renderer, const Entity& entity) const { @@ -46,7 +42,6 @@ std::optional Contents::RenderToSnapshot( [&contents = *this, &entity, &bounds](const ContentContext& renderer, RenderPass& pass) -> bool { Entity sub_entity; - sub_entity.SetPath(entity.GetPath()); sub_entity.SetBlendMode(Entity::BlendMode::kSourceOver); sub_entity.SetTransformation( Matrix::MakeTranslation(Vector3(-bounds->origin)) * diff --git a/engine/src/flutter/impeller/entity/contents/contents.h b/engine/src/flutter/impeller/entity/contents/contents.h index 9cf59ca377..5f9bf4805c 100644 --- a/engine/src/flutter/impeller/entity/contents/contents.h +++ b/engine/src/flutter/impeller/entity/contents/contents.h @@ -37,7 +37,7 @@ class Contents { RenderPass& pass) const = 0; /// @brief Get the screen space bounding rectangle that this contents affects. - virtual std::optional GetCoverage(const Entity& entity) const; + virtual std::optional GetCoverage(const Entity& entity) const = 0; /// @brief Render this contents to a snapshot, respecting the entity's /// transform, path, stencil depth, and blend mode. 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 4a63da973c..a054aa8038 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_contents.cc @@ -129,11 +129,12 @@ bool FilterContents::Render(const ContentContext& renderer, // Draw the result texture, respecting the transform and clip stack. auto contents = std::make_shared(); + contents->SetPath( + PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath()); contents->SetTexture(snapshot.texture); contents->SetSourceRect(Rect::MakeSize(Size(snapshot.texture->GetSize()))); Entity e; - e.SetPath(PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath()); e.SetBlendMode(entity.GetBlendMode()); e.SetStencilDepth(entity.GetStencilDepth()); return contents->Render(renderer, e, pass); 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 32db85b66e..6719886467 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/filter_input.cc @@ -146,23 +146,13 @@ std::optional TextureFilterInput::GetSnapshot( std::optional TextureFilterInput::GetCoverage( const Entity& entity) const { - auto path_bounds = entity.GetPath().GetBoundingBox(); - if (!path_bounds.has_value()) { - return std::nullopt; - } return Rect::MakeSize(Size(texture_->GetSize())) .TransformBounds(GetTransform(entity)); } Matrix TextureFilterInput::GetLocalTransform(const Entity& entity) const { - // Compute the local transform such that the texture will cover the entity - // path bounding box. - auto path_bounds = entity.GetPath().GetBoundingBox(); - if (!path_bounds.has_value()) { - return Matrix(); - } - return Matrix::MakeTranslation(path_bounds->origin) * - Matrix::MakeScale(Vector2(path_bounds->size) / texture_->GetSize()); + // Compute the local transform such that the texture is centered. + return Matrix::MakeTranslation(-Point(texture_->GetSize()) / 2); } } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc index 092eee4ff1..91e7912117 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc @@ -15,6 +15,10 @@ LinearGradientContents::LinearGradientContents() = default; LinearGradientContents::~LinearGradientContents() = default; +void LinearGradientContents::SetPath(Path path) { + path_ = std::move(path); +} + void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) { start_point_ = start_point; end_point_ = end_point; @@ -34,6 +38,11 @@ const std::vector& LinearGradientContents::GetColors() const { return colors_; } +std::optional LinearGradientContents::GetCoverage( + const Entity& entity) const { + return path_.GetTransformedBoundingBox(entity.GetTransformation()); +}; + bool LinearGradientContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { @@ -42,13 +51,13 @@ bool LinearGradientContents::Render(const ContentContext& renderer, auto vertices_builder = VertexBufferBuilder(); { - auto result = Tessellator{}.Tessellate(entity.GetPath().GetFillType(), - entity.GetPath().CreatePolyline(), - [&vertices_builder](Point point) { - VS::PerVertexData vtx; - vtx.vertices = point; - vertices_builder.AppendVertex(vtx); - }); + auto result = + Tessellator{}.Tessellate(path_.GetFillType(), path_.CreatePolyline(), + [&vertices_builder](Point point) { + VS::PerVertexData vtx; + vtx.vertices = point; + vertices_builder.AppendVertex(vtx); + }); if (!result) { return false; } diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h index 0467f8fb3a..5fafed5f96 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h @@ -11,6 +11,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/contents.h" #include "impeller/geometry/color.h" +#include "impeller/geometry/path.h" #include "impeller/geometry/point.h" namespace impeller { @@ -21,6 +22,11 @@ class LinearGradientContents final : public Contents { ~LinearGradientContents() override; + void SetPath(Path path); + + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, @@ -33,6 +39,7 @@ class LinearGradientContents final : public Contents { const std::vector& GetColors() const; private: + Path path_; Point start_point_; Point end_point_; std::vector colors_; 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 c809f037ca..fce8d7ab7f 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.cc @@ -7,6 +7,7 @@ #include "impeller/entity/contents/content_context.h" #include "impeller/entity/entity.h" #include "impeller/geometry/path.h" +#include "impeller/geometry/path_builder.h" #include "impeller/renderer/render_pass.h" #include "impeller/tessellator/tessellator.h" @@ -24,6 +25,19 @@ const Color& SolidColorContents::GetColor() const { return color_; } +void SolidColorContents::SetPath(Path path) { + path_ = std::move(path); +} + +void SolidColorContents::SetCover(bool cover) { + cover_ = cover; +} + +std::optional SolidColorContents::GetCoverage( + const Entity& entity) const { + return path_.GetTransformedBoundingBox(entity.GetTransformation()); +}; + VertexBuffer SolidColorContents::CreateSolidFillVertices(const Path& path, HostBuffer& buffer) { using VS = SolidFillPipeline::VertexShader; @@ -57,8 +71,12 @@ bool SolidColorContents::Render(const ContentContext& renderer, cmd.pipeline = renderer.GetSolidFillPipeline(OptionsFromPassAndEntity(pass, entity)); cmd.stencil_reference = entity.GetStencilDepth(); - cmd.BindVertices( - CreateSolidFillVertices(entity.GetPath(), pass.GetTransientsBuffer())); + + cmd.BindVertices(CreateSolidFillVertices( + cover_ + ? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath() + : path_, + pass.GetTransientsBuffer())); VS::FrameInfo frame_info; frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * @@ -75,8 +93,10 @@ bool SolidColorContents::Render(const ContentContext& renderer, return true; } -std::unique_ptr SolidColorContents::Make(Color color) { +std::unique_ptr SolidColorContents::Make(Path path, + Color color) { auto contents = std::make_unique(); + contents->SetPath(std::move(path)); contents->SetColor(color); return contents; } diff --git a/engine/src/flutter/impeller/entity/contents/solid_color_contents.h b/engine/src/flutter/impeller/entity/contents/solid_color_contents.h index 8f2551ca2b..470b018051 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_color_contents.h +++ b/engine/src/flutter/impeller/entity/contents/solid_color_contents.h @@ -11,6 +11,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/contents.h" #include "impeller/geometry/color.h" +#include "impeller/geometry/path.h" namespace impeller { @@ -24,21 +25,31 @@ class SolidColorContents final : public Contents { ~SolidColorContents() override; - static std::unique_ptr Make(Color color); + static std::unique_ptr Make(Path path, Color color); static VertexBuffer CreateSolidFillVertices(const Path& path, HostBuffer& buffer); + void SetPath(Path path); + + void SetCover(bool cover); + void SetColor(Color color); const Color& GetColor() const; + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const override; private: + Path path_; + bool cover_ = false; + Color color_; FML_DISALLOW_COPY_AND_ASSIGN(SolidColorContents); diff --git a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc index cee7da9143..1cfbfb9f8b 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.cc @@ -29,12 +29,17 @@ const Color& SolidStrokeContents::GetColor() const { return color_; } +void SolidStrokeContents::SetPath(Path path) { + path_ = std::move(path); +} + std::optional SolidStrokeContents::GetCoverage( const Entity& entity) const { - auto path_coverage = entity.GetPathCoverage(); - if (!path_coverage.has_value()) { + auto path_bounds = path_.GetBoundingBox(); + if (!path_bounds.has_value()) { return std::nullopt; } + auto path_coverage = path_bounds->TransformBounds(entity.GetTransformation()); Scalar max_radius = 0.5; if (cap_ == Cap::kSquare) { @@ -46,9 +51,9 @@ std::optional SolidStrokeContents::GetCoverage( Vector2 max_radius_xy = entity.GetTransformation().TransformDirection( Vector2(max_radius, max_radius) * stroke_size_); - return Rect(path_coverage->origin - max_radius_xy, - Size(path_coverage->size.width + max_radius_xy.x * 2, - path_coverage->size.height + max_radius_xy.y * 2)); + return Rect(path_coverage.origin - max_radius_xy, + Size(path_coverage.size.width + max_radius_xy.x * 2, + path_coverage.size.height + max_radius_xy.y * 2)); } static VertexBuffer CreateSolidStrokeVertices( @@ -197,7 +202,7 @@ bool SolidStrokeContents::Render(const ContentContext& renderer, 5.0 / (stroke_size_ * entity.GetTransformation().GetMaxBasisLength()), 0.0, 0.0); cmd.BindVertices(CreateSolidStrokeVertices( - entity.GetPath(), pass.GetTransientsBuffer(), cap_proc_, join_proc_, + path_, pass.GetTransientsBuffer(), cap_proc_, join_proc_, miter_limit_, smoothing)); VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); VS::BindStrokeInfo(cmd, diff --git a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h index e0d3f3abee..8b38657141 100644 --- a/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h +++ b/engine/src/flutter/impeller/entity/contents/solid_stroke_contents.h @@ -48,6 +48,8 @@ class SolidStrokeContents final : public Contents { ~SolidStrokeContents() override; + void SetPath(Path path); + void SetColor(Color color); const Color& GetColor() const; @@ -77,6 +79,8 @@ class SolidStrokeContents final : public Contents { RenderPass& pass) const override; private: + Path path_; + Color color_; Scalar stroke_size_ = 0.0; Scalar miter_limit_ = 4.0; diff --git a/engine/src/flutter/impeller/entity/contents/texture_contents.cc b/engine/src/flutter/impeller/entity/contents/texture_contents.cc index 77e0a16d06..f66936c6b3 100644 --- a/engine/src/flutter/impeller/entity/contents/texture_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/texture_contents.cc @@ -18,6 +18,10 @@ TextureContents::TextureContents() = default; TextureContents::~TextureContents() = default; +void TextureContents::SetPath(Path path) { + path_ = std::move(path); +} + void TextureContents::SetTexture(std::shared_ptr texture) { texture_ = std::move(texture); } @@ -30,6 +34,10 @@ void TextureContents::SetOpacity(Scalar opacity) { opacity_ = opacity; } +std::optional TextureContents::GetCoverage(const Entity& entity) const { + return path_.GetTransformedBoundingBox(entity.GetTransformation()); +}; + bool TextureContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { @@ -40,7 +48,7 @@ bool TextureContents::Render(const ContentContext& renderer, using VS = TextureFillVertexShader; using FS = TextureFillFragmentShader; - const auto coverage_rect = entity.GetPath().GetBoundingBox(); + const auto coverage_rect = path_.GetBoundingBox(); if (!coverage_rect.has_value()) { return true; @@ -62,7 +70,7 @@ bool TextureContents::Render(const ContentContext& renderer, VertexBufferBuilder vertex_builder; { const auto tess_result = Tessellator{}.Tessellate( - entity.GetPath().GetFillType(), entity.GetPath().CreatePolyline(), + path_.GetFillType(), path_.CreatePolyline(), [this, &vertex_builder, &coverage_rect, &texture_size](Point vtx) { VS::PerVertexData data; data.vertices = vtx; diff --git a/engine/src/flutter/impeller/entity/contents/texture_contents.h b/engine/src/flutter/impeller/entity/contents/texture_contents.h index 7b994a6c06..4b02d88c0b 100644 --- a/engine/src/flutter/impeller/entity/contents/texture_contents.h +++ b/engine/src/flutter/impeller/entity/contents/texture_contents.h @@ -10,6 +10,7 @@ #include "flutter/fml/macros.h" #include "impeller/entity/contents/contents.h" +#include "impeller/geometry/path.h" #include "impeller/renderer/sampler_descriptor.h" namespace impeller { @@ -22,6 +23,8 @@ class TextureContents final : public Contents { ~TextureContents() override; + void SetPath(Path path); + void SetTexture(std::shared_ptr texture); std::shared_ptr GetTexture() const; @@ -36,12 +39,17 @@ class TextureContents final : public Contents { void SetOpacity(Scalar opacity); + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + // |Contents| bool Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const override; public: + Path path_; + std::shared_ptr texture_; SamplerDescriptor sampler_descriptor_ = {}; Rect source_rect_; diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 5412df2a14..920fad3e39 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -24,22 +24,6 @@ void Entity::SetTransformation(const Matrix& transformation) { transformation_ = transformation; } -const Path& Entity::GetPath() const { - return path_; -} - -void Entity::SetPath(Path path) { - path_ = std::move(path); -} - -std::optional Entity::GetPathCoverage() const { - auto bounds = GetPath().GetBoundingBox(); - if (!bounds.has_value()) { - return std::nullopt; - } - return bounds->TransformBounds(GetTransformation()); -} - void Entity::SetAddsToCoverage(bool adds) { adds_to_coverage_ = adds; } diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index b4979bb3ed..8566b99bdd 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -61,12 +61,6 @@ class Entity { void SetTransformation(const Matrix& transformation); - const Path& GetPath() const; - - void SetPath(Path path); - - std::optional GetPathCoverage() const; - void SetAddsToCoverage(bool adds); bool AddsToCoverage() const; @@ -93,7 +87,6 @@ class Entity { Matrix transformation_; std::shared_ptr contents_; BlendMode blend_mode_ = BlendMode::kSourceOver; - Path path_; uint32_t stencil_depth_ = 0u; bool adds_to_coverage_ = true; }; diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index c9f4d4f71f..382cef3a03 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -159,7 +159,6 @@ bool EntityPass::Render(ContentContext& renderer, } const auto subpass_coverage = GetSubpassCoverage(*subpass); - if (!subpass_coverage.has_value()) { continue; } @@ -227,9 +226,6 @@ bool EntityPass::Render(ContentContext& renderer, } Entity entity; - entity.SetPath(PathBuilder{} - .AddRect(Rect::MakeSize(subpass_coverage->size)) - .TakePath()); entity.SetContents(std::move(offscreen_texture_contents)); entity.SetStencilDepth(stencil_depth_); entity.SetBlendMode(subpass->blend_mode_); diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 7401ddb187..ba154b2af9 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -33,8 +33,8 @@ TEST_P(EntityTest, CanCreateEntity) { TEST_P(EntityTest, CanDrawRect) { Entity entity; - entity.SetPath(PathBuilder{}.AddRect({100, 100, 100, 100}).TakePath()); - entity.SetContents(SolidColorContents::Make(Color::Red())); + entity.SetContents(SolidColorContents::Make( + PathBuilder{}.AddRect({100, 100, 100, 100}).TakePath(), Color::Red())); ASSERT_TRUE(OpenPlaygroundHere(entity)); } @@ -49,8 +49,8 @@ TEST_P(EntityTest, ThreeStrokesInOnePath) { .TakePath(); Entity entity; - entity.SetPath(path); auto contents = std::make_unique(); + contents->SetPath(std::move(path)); contents->SetColor(Color::Red()); contents->SetStrokeSize(5.0); entity.SetContents(std::move(contents)); @@ -79,8 +79,8 @@ TEST_P(EntityTest, TriangleInsideASquare) { .TakePath(); Entity entity; - entity.SetPath(path); auto contents = std::make_unique(); + contents->SetPath(std::move(path)); contents->SetColor(Color::Red()); contents->SetStrokeSize(20.0); entity.SetContents(std::move(contents)); @@ -124,6 +124,7 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) { Path path, SolidStrokeContents::Cap cap, SolidStrokeContents::Join join) { auto contents = std::make_unique(); + contents->SetPath(path); contents->SetColor(Color::Red()); contents->SetStrokeSize(width); contents->SetStrokeCap(cap); @@ -131,16 +132,15 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) { contents->SetStrokeMiter(miter_limit); Entity entity; - entity.SetPath(path); entity.SetContents(std::move(contents)); auto coverage = entity.GetCoverage(); if (coverage.has_value()) { auto bounds_contents = std::make_unique(); + bounds_contents->SetPath( + PathBuilder{}.AddRect(entity.GetCoverage().value()).TakePath()); bounds_contents->SetColor(Color::Green().WithAlpha(0.5)); Entity bounds_entity; - bounds_entity.SetPath( - PathBuilder{}.AddRect(entity.GetCoverage().value()).TakePath()); bounds_entity.SetContents(std::move(bounds_contents)); bounds_entity.Render(context, pass); } @@ -242,8 +242,7 @@ TEST_P(EntityTest, CubicCurveTest) { .Close() .TakePath(); Entity entity; - entity.SetPath(path); - entity.SetContents(SolidColorContents::Make(Color::Red())); + entity.SetContents(SolidColorContents::Make(path, Color::Red())); ASSERT_TRUE(OpenPlaygroundHere(entity)); } @@ -470,8 +469,7 @@ TEST_P(EntityTest, CubicCurveAndOverlapTest) { .Close() .TakePath(); Entity entity; - entity.SetPath(path); - entity.SetContents(SolidColorContents::Make(Color::Red())); + entity.SetContents(SolidColorContents::Make(path, Color::Red())); ASSERT_TRUE(OpenPlaygroundHere(entity)); } @@ -656,10 +654,9 @@ TEST_P(EntityTest, BezierCircleScaled) { {97.32499434685802, 34.81799797758954}) .Close() .TakePath(); - entity.SetPath(path); entity.SetTransformation( Matrix::MakeScale({20.0, 20.0, 1.0}).Translate({-80, -15, 0})); - entity.SetContents(SolidColorContents::Make(Color::Red())); + entity.SetContents(SolidColorContents::Make(path, Color::Red())); ASSERT_TRUE(OpenPlaygroundHere(entity)); } @@ -682,7 +679,8 @@ TEST_P(EntityTest, Filters) { {fi_bridge, FilterInput::Make(blend0), fi_bridge, fi_bridge}); Entity entity; - entity.SetPath(PathBuilder{}.AddRect({100, 100, 300, 300}).TakePath()); + entity.SetTransformation(Matrix::MakeTranslation({500, 300}) * + Matrix::MakeScale(Vector2{0.5, 0.5})); entity.SetContents(blend1); return entity.Render(context, pass); }; @@ -717,7 +715,7 @@ TEST_P(EntityTest, GaussianBlurFilter) { static Color bounds_color(0, 1, 0, 0.1); static float offset[2] = {500, 400}; static float rotation = 0; - static float scale[2] = {0.8, 0.8}; + static float scale[2] = {0.5, 0.5}; static float skew[2] = {0, 0}; ImGui::Begin("Controls"); @@ -752,8 +750,6 @@ TEST_P(EntityTest, GaussianBlurFilter) { FilterContents::Sigma{blur_amount[1]}, blur_styles[selected_blur_style]); - ISize input_size = boston->GetSize(); - auto rect = Rect(-Point(input_size) / 2, Size(input_size)); auto ctm = Matrix::MakeTranslation(Vector3(offset[0], offset[1])) * Matrix::MakeRotationZ(Radians(rotation)) * Matrix::MakeScale(Vector2(scale[0], scale[1])) * @@ -762,7 +758,6 @@ TEST_P(EntityTest, GaussianBlurFilter) { auto target_contents = selected_blur_type == 0 ? blur : mask_blur; Entity entity; - entity.SetPath(PathBuilder{}.AddRect(rect).TakePath()); entity.SetContents(target_contents); entity.SetTransformation(ctm); @@ -771,19 +766,23 @@ TEST_P(EntityTest, GaussianBlurFilter) { // Renders a red "cover" rectangle that shows the original position of the // unfiltered input. Entity cover_entity; - cover_entity.SetPath(PathBuilder{}.AddRect(rect).TakePath()); - cover_entity.SetContents(SolidColorContents::Make(cover_color)); + cover_entity.SetContents( + SolidColorContents::Make(PathBuilder{} + .AddRect(Rect(-Point(bridge->GetSize()) / 2, + Size(bridge->GetSize()))) + .TakePath(), + cover_color)); cover_entity.SetTransformation(ctm); cover_entity.Render(context, pass); // Renders a green bounding rect of the target filter. Entity bounds_entity; - bounds_entity.SetPath( + bounds_entity.SetContents(SolidColorContents::Make( PathBuilder{} .AddRect(target_contents->GetCoverage(entity).value()) - .TakePath()); - bounds_entity.SetContents(SolidColorContents::Make(bounds_color)); + .TakePath(), + bounds_color)); bounds_entity.SetTransformation(Matrix()); bounds_entity.Render(context, pass); @@ -803,19 +802,17 @@ TEST_P(EntityTest, SetBlendMode) { TEST_P(EntityTest, ContentsGetBoundsForEmptyPathReturnsNullopt) { Entity entity; entity.SetContents(std::make_shared()); - entity.SetPath({}); ASSERT_FALSE(entity.GetCoverage().has_value()); - ASSERT_FALSE(entity.GetPathCoverage().has_value()); } TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) { { Entity entity; auto contents = std::make_unique(); + contents->SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath()); contents->SetStrokeCap(SolidStrokeContents::Cap::kButt); contents->SetStrokeJoin(SolidStrokeContents::Join::kBevel); contents->SetStrokeSize(4); - entity.SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath()); entity.SetContents(std::move(contents)); auto actual = entity.GetCoverage(); auto expected = Rect::MakeLTRB(-2, -2, 12, 12); @@ -827,10 +824,10 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) { { Entity entity; auto contents = std::make_unique(); + contents->SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath()); contents->SetStrokeCap(SolidStrokeContents::Cap::kSquare); contents->SetStrokeJoin(SolidStrokeContents::Join::kBevel); contents->SetStrokeSize(4); - entity.SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath()); entity.SetContents(std::move(contents)); auto actual = entity.GetCoverage(); auto expected = @@ -843,11 +840,11 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) { { Entity entity; auto contents = std::make_unique(); + contents->SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath()); contents->SetStrokeCap(SolidStrokeContents::Cap::kSquare); contents->SetStrokeJoin(SolidStrokeContents::Join::kMiter); contents->SetStrokeSize(4); contents->SetStrokeMiter(2); - entity.SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath()); entity.SetContents(std::move(contents)); auto actual = entity.GetCoverage(); auto expected = Rect::MakeLTRB(-4, -4, 14, 14); diff --git a/engine/src/flutter/impeller/geometry/path.cc b/engine/src/flutter/impeller/geometry/path.cc index b71996e76d..9e6c52e0ab 100644 --- a/engine/src/flutter/impeller/geometry/path.cc +++ b/engine/src/flutter/impeller/geometry/path.cc @@ -286,6 +286,14 @@ std::optional Path::GetBoundingBox() const { return Rect{min.x, min.y, difference.x, difference.y}; } +std::optional Path::GetTransformedBoundingBox(const Matrix& transform) const { + auto bounds = GetBoundingBox(); + if (!bounds.has_value()) { + return std::nullopt; + } + return bounds->TransformBounds(transform); +} + std::optional> Path::GetMinMaxCoveragePoints() const { if (linears_.empty() && quads_.empty() && cubics_.empty()) { return std::nullopt; diff --git a/engine/src/flutter/impeller/geometry/path.h b/engine/src/flutter/impeller/geometry/path.h index 7add50e178..2faa4e5241 100644 --- a/engine/src/flutter/impeller/geometry/path.h +++ b/engine/src/flutter/impeller/geometry/path.h @@ -121,6 +121,8 @@ class Path { std::optional GetBoundingBox() const; + std::optional GetTransformedBoundingBox(const Matrix& transform) const; + std::optional> GetMinMaxCoveragePoints() const; private: