diff --git a/engine/src/flutter/impeller/.clang-format b/engine/src/flutter/impeller/.clang-format index 6fdf1dc888..1b8a1bdd7d 100644 --- a/engine/src/flutter/impeller/.clang-format +++ b/engine/src/flutter/impeller/.clang-format @@ -1,8 +1,5 @@ # Defines the Chromium style for automatic reformatting. # http://clang.llvm.org/docs/ClangFormatStyleOptions.html BasedOnStyle: Chromium -# This defaults to 'Auto'. Explicitly set it for a while, so that -# 'vector >' in existing files gets formatted to -# 'vector>'. ('Auto' means that clang-format will only use -# 'int>>' if the file already contains at least one such instance.) -Standard: Cpp11 +Standard: c++17 +EmptyLineBeforeAccessModifier: Always diff --git a/engine/src/flutter/impeller/entity/content_renderer.cc b/engine/src/flutter/impeller/entity/content_renderer.cc index 4ea53ec0c4..d798d62f54 100644 --- a/engine/src/flutter/impeller/entity/content_renderer.cc +++ b/engine/src/flutter/impeller/entity/content_renderer.cc @@ -13,6 +13,7 @@ ContentRenderer::ContentRenderer(std::shared_ptr context) } gradient_fill_pipeline_ = std::make_unique(*context_); + solid_fill_pipeline_ = std::make_unique(*context_); is_valid_ = true; } @@ -34,4 +35,12 @@ std::shared_ptr ContentRenderer::GetGradientFillPipeline() const { return gradient_fill_pipeline_->WaitAndGet(); } +std::shared_ptr ContentRenderer::GetSolidFillPipeline() const { + if (!IsValid()) { + return nullptr; + } + + return solid_fill_pipeline_->WaitAndGet(); +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/content_renderer.h b/engine/src/flutter/impeller/entity/content_renderer.h index 671504a298..77869bbcf3 100644 --- a/engine/src/flutter/impeller/entity/content_renderer.h +++ b/engine/src/flutter/impeller/entity/content_renderer.h @@ -9,12 +9,16 @@ #include "flutter/fml/macros.h" #include "flutter/impeller/entity/gradient_fill.frag.h" #include "flutter/impeller/entity/gradient_fill.vert.h" +#include "flutter/impeller/entity/solid_fill.frag.h" +#include "flutter/impeller/entity/solid_fill.vert.h" #include "impeller/renderer/pipeline.h" namespace impeller { using GradientFillPipeline = PipelineT; +using SolidFillPipeline = + PipelineT; class ContentRenderer { public: @@ -26,11 +30,14 @@ class ContentRenderer { std::shared_ptr GetGradientFillPipeline() const; + std::shared_ptr GetSolidFillPipeline() const; + std::shared_ptr GetContext() const; private: std::shared_ptr context_; std::unique_ptr gradient_fill_pipeline_; + std::unique_ptr solid_fill_pipeline_; bool is_valid_ = false; FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer); diff --git a/engine/src/flutter/impeller/entity/contents.cc b/engine/src/flutter/impeller/entity/contents.cc index 41f468aa44..dc2db4a477 100644 --- a/engine/src/flutter/impeller/entity/contents.cc +++ b/engine/src/flutter/impeller/entity/contents.cc @@ -14,10 +14,18 @@ namespace impeller { +/******************************************************************************* + ******* Contents + ******************************************************************************/ + Contents::Contents() = default; Contents::~Contents() = default; +/******************************************************************************* + ******* Linear Gradient Contents + ******************************************************************************/ + LinearGradientContents::LinearGradientContents() = default; LinearGradientContents::~LinearGradientContents() = default; @@ -41,7 +49,6 @@ const std::vector& LinearGradientContents::GetColors() const { return colors_; } -// |Contents| bool LinearGradientContents::Render(const ContentRenderer& renderer, const Entity& entity, const Surface& surface, @@ -84,4 +91,72 @@ bool LinearGradientContents::Render(const ContentRenderer& renderer, return pass.AddCommand(std::move(cmd)); } +/******************************************************************************* + ******* SolidColorContents + ******************************************************************************/ + +void SolidColorContents::SetColor(Color color) { + color_ = color; +} + +const Color& SolidColorContents::GetColor() const { + return color_; +} + +bool SolidColorContents::Render(const ContentRenderer& renderer, + const Entity& entity, + const Surface& surface, + RenderPass& pass) const { + using VS = SolidFillPipeline::VertexShader; + + Command cmd; + cmd.label = "SolidFill"; + cmd.pipeline = renderer.GetSolidFillPipeline(); + if (cmd.pipeline == nullptr) { + return false; + } + + VertexBufferBuilder vtx_builder; + { + auto tesselation_result = Tessellator{}.Tessellate( + entity.GetPath().CreatePolyline(), [&vtx_builder](auto point) { + VS::PerVertexData vtx; + vtx.vertices = point; + vtx_builder.AppendVertex(vtx); + }); + if (!tesselation_result) { + return false; + } + } + + cmd.BindVertices(vtx_builder.CreateVertexBuffer( + *renderer.GetContext()->GetPermanentsAllocator())); + + VS::FrameInfo frame_info; + frame_info.mvp = + Matrix::MakeOrthographic(surface.GetSize()) * entity.GetTransformation(); + frame_info.color = entity.GetBackgroundColor(); + VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); + + cmd.primitive_type = PrimitiveType::kTriangle; + + if (!pass.AddCommand(std::move(cmd))) { + return false; + } + + return true; +} + +/******************************************************************************* + ******* SolidStrokeContents + ******************************************************************************/ + +void SolidStrokeContents::SetColor(Color color) { + color_ = color; +} + +const Color& SolidStrokeContents::GetColor() const { + return color_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents.h b/engine/src/flutter/impeller/entity/contents.h index a2a55bf404..0c04e5474c 100644 --- a/engine/src/flutter/impeller/entity/contents.h +++ b/engine/src/flutter/impeller/entity/contents.h @@ -21,7 +21,7 @@ class Contents { public: Contents(); - ~Contents(); + virtual ~Contents(); virtual bool Render(const ContentRenderer& renderer, const Entity& entity, @@ -36,7 +36,7 @@ class LinearGradientContents final : public Contents { public: LinearGradientContents(); - ~LinearGradientContents(); + ~LinearGradientContents() override; // |Contents| bool Render(const ContentRenderer& renderer, @@ -58,4 +58,53 @@ class LinearGradientContents final : public Contents { FML_DISALLOW_COPY_AND_ASSIGN(LinearGradientContents); }; +class SolidColorContents final : public Contents { + public: + SolidColorContents(); + + ~SolidColorContents() override; + + void SetColor(Color color); + + const Color& GetColor() const; + + // |Contents| + bool Render(const ContentRenderer& renderer, + const Entity& entity, + const Surface& surface, + RenderPass& pass) const override; + + private: + Color color_; + + FML_DISALLOW_COPY_AND_ASSIGN(SolidColorContents); +}; + +class SolidStrokeContents final : public Contents { + public: + SolidStrokeContents(); + + ~SolidStrokeContents(); + + void SetColor(Color color); + + const Color& GetColor() const; + + void SetStrokeSize(Scalar size) { stroke_size_ = size; } + + Scalar GetStrokeSize() const { return stroke_size_; } + + // |Contents| + bool Render(const ContentRenderer& renderer, + const Entity& entity, + const Surface& surface, + RenderPass& pass) const override; + + private: + Color color_; + Scalar stroke_size_ = 0.0; + + FML_DISALLOW_COPY_AND_ASSIGN(SolidStrokeContents); +}; + } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 186d8e1aac..bc8d81aae4 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -66,37 +66,4 @@ const std::shared_ptr& Entity::GetContents() const { return contents_; } -bool Entity::HasStroke() const { - return stroke_size_ > 0.0 && !stroke_color_.IsTransparent(); -} - -bool Entity::HasContents() const { - if (contents_) { - return true; - } - return !background_color_.IsTransparent(); -} - -bool Entity::HasRenderableContents() const { - const bool has_empty_path = path_.GetBoundingBox().IsZero(); - - if (has_empty_path) { - return false; - } - - if (IsClip()) { - return true; - } - - if (HasStroke()) { - return true; - } - - if (HasContents()) { - return true; - } - - return false; -} - } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index ed3942fc8a..bb40679270 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -43,12 +43,6 @@ class Entity { bool IsClip() const; - bool HasStroke() const; - - bool HasContents() const; - - bool HasRenderableContents() const; - void SetContents(std::shared_ptr contents); const std::shared_ptr& GetContents() const; diff --git a/engine/src/flutter/impeller/entity/entity_renderer.cc b/engine/src/flutter/impeller/entity/entity_renderer.cc index a15846b7bb..77e1369754 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer.cc +++ b/engine/src/flutter/impeller/entity/entity_renderer.cc @@ -5,8 +5,7 @@ #include "flutter/impeller/entity/entity_renderer.h" #include "flutter/fml/trace_event.h" -#include "impeller/renderer/tessellator.h" -#include "impeller/renderer/vertex_buffer_builder.h" +#include "impeller/entity/content_renderer.h" namespace impeller { @@ -21,8 +20,6 @@ EntityRenderer::EntityRenderer(std::shared_ptr context) return; } - solid_fill_pipeline_ = std::make_unique(*context_); - is_valid_ = true; } @@ -40,8 +37,8 @@ bool EntityRenderer::RenderEntities(const Surface& surface, } for (const auto& entity : entities) { - if (RenderEntity(surface, onscreen_pass, entity) == - EntityRenderer::RenderResult::kFailure) { + if (auto contents = entity.GetContents(); + !contents->Render(*content_renderer_, entity, surface, onscreen_pass)) { return false; } } @@ -49,61 +46,4 @@ bool EntityRenderer::RenderEntities(const Surface& surface, return true; } -EntityRenderer::RenderResult EntityRenderer::RenderEntity( - const Surface& surface, - RenderPass& pass, - const Entity& entity) { - if (!entity.HasRenderableContents()) { - return RenderResult::kSkipped; - } - - if (entity.HasContents() && !entity.IsClip() && !entity.GetContents()) { - using VS = SolidFillPipeline::VertexShader; - - Command cmd; - cmd.label = "SolidFill"; - cmd.pipeline = solid_fill_pipeline_->WaitAndGet(); - if (cmd.pipeline == nullptr) { - return RenderResult::kFailure; - } - - VertexBufferBuilder vtx_builder; - { - auto tesselation_result = Tessellator{}.Tessellate( - entity.GetPath().CreatePolyline(), [&vtx_builder](auto point) { - VS::PerVertexData vtx; - vtx.vertices = point; - vtx_builder.AppendVertex(vtx); - }); - if (!tesselation_result) { - return RenderResult::kFailure; - } - } - - cmd.BindVertices( - vtx_builder.CreateVertexBuffer(*context_->GetPermanentsAllocator())); - - VS::FrameInfo frame_info; - frame_info.mvp = Matrix::MakeOrthographic(surface.GetSize()) * - entity.GetTransformation(); - frame_info.color = entity.GetBackgroundColor(); - VS::BindFrameInfo(cmd, - pass.GetTransientsBuffer().EmplaceUniform(frame_info)); - - cmd.primitive_type = PrimitiveType::kTriangle; - - if (!pass.AddCommand(std::move(cmd))) { - return RenderResult::kFailure; - } - } else if (entity.GetContents()) { - auto result = - entity.GetContents()->Render(*content_renderer_, entity, surface, pass); - if (!result) { - return RenderResult::kFailure; - } - } - - return RenderResult::kSuccess; -} - } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity_renderer.h b/engine/src/flutter/impeller/entity/entity_renderer.h index caa670dc9c..1a0160cc1d 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer.h +++ b/engine/src/flutter/impeller/entity/entity_renderer.h @@ -7,18 +7,14 @@ #include #include "flutter/fml/macros.h" -#include "flutter/impeller/entity/solid_fill.frag.h" -#include "flutter/impeller/entity/solid_fill.vert.h" -#include "impeller/entity/content_renderer.h" #include "impeller/entity/entity.h" #include "impeller/renderer/context.h" -#include "impeller/renderer/pipeline.h" -#include "impeller/renderer/pipeline_builder.h" -#include "impeller/renderer/render_pass.h" #include "impeller/renderer/surface.h" namespace impeller { +class ContentRenderer; + class EntityRenderer { public: EntityRenderer(std::shared_ptr context); @@ -31,22 +27,8 @@ class EntityRenderer { RenderPass& onscreen_pass, const std::vector& entities); - enum class RenderResult { - kSkipped, - kSuccess, - kFailure, - }; - - [[nodiscard]] RenderResult RenderEntity(const Surface& surface, - RenderPass& onscreen_pass, - const Entity& entities); - private: - using SolidFillPipeline = - PipelineT; - std::shared_ptr context_; - std::unique_ptr solid_fill_pipeline_; std::unique_ptr content_renderer_; bool is_valid_ = false; diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index 939d907173..9a82998fc2 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -3,11 +3,24 @@ // found in the LICENSE file. #include "flutter/testing/testing.h" +#include "impeller/entity/entity.h" +#include "impeller/geometry/path_builder.h" +#include "impeller/playground/playground.h" namespace impeller { namespace testing { -// +using EntityTest = Playground; + +TEST_F(EntityTest, CanCreateEntity) { + Entity entity; + ASSERT_TRUE(entity.GetTransformation().IsIdentity()); +} + +TEST_F(EntityTest, CanDrawRect) { + Entity entity; + entity.SetPath(PathBuilder{}.AddRect({100, 100, 100, 100}).CreatePath()); +} } // namespace testing } // namespace impeller