From 7ac751e9ad4c074c38fe57090c161c07ac489ce6 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Sun, 22 Aug 2021 19:55:27 -0700 Subject: [PATCH] Implement gradient contents renderer. --- engine/src/flutter/impeller/aiks/canvas.cc | 3 + engine/src/flutter/impeller/aiks/paint.h | 2 + .../compositor/vertex_buffer_builder.h | 6 +- engine/src/flutter/impeller/entity/BUILD.gn | 8 +- .../impeller/entity/content_renderer.h | 39 ++++++++ .../impeller/entity/content_renderer.mm | 37 ++++++++ engine/src/flutter/impeller/entity/contents.h | 61 +++++++++++++ .../src/flutter/impeller/entity/contents.mm | 88 +++++++++++++++++++ engine/src/flutter/impeller/entity/entity.cc | 11 +++ engine/src/flutter/impeller/entity/entity.h | 6 ++ .../impeller/entity/entity_renderer.mm | 23 +---- .../impeller/entity/entity_renderer_impl.h | 6 +- .../impeller/entity/entity_renderer_impl.mm | 17 ++-- .../impeller/entity/gradient_fill.frag | 24 +++++ .../impeller/entity/gradient_fill.vert | 16 ++++ .../entity/{path.vert => solid_fill.vert} | 0 16 files changed, 317 insertions(+), 30 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/content_renderer.h create mode 100644 engine/src/flutter/impeller/entity/content_renderer.mm create mode 100644 engine/src/flutter/impeller/entity/contents.h create mode 100644 engine/src/flutter/impeller/entity/contents.mm create mode 100644 engine/src/flutter/impeller/entity/gradient_fill.frag create mode 100644 engine/src/flutter/impeller/entity/gradient_fill.vert rename engine/src/flutter/impeller/entity/{path.vert => solid_fill.vert} (100%) diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 0701dee386..edfda32088 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -71,10 +71,13 @@ void Canvas::RestoreToCount(size_t count) { void Canvas::DrawPath(Path path, Paint paint) { Color color = paint.color; color.alpha *= paint_stack_.top().color.alpha; + Entity entity; entity.SetTransformation(GetCurrentTransformation()); entity.SetPath(std::move(path)); entity.SetBackgroundColor(color); + entity.SetContents(std::move(paint.contents)); + ops_.emplace_back(std::move(entity)); } diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 6b90aa1c43..c3b947b8d8 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -6,6 +6,7 @@ #include "flutter/fml/macros.h" +#include "impeller/entity/contents.h" #include "impeller/geometry/color.h" namespace impeller { @@ -13,6 +14,7 @@ namespace impeller { struct Paint { Color color; Scalar stroke_width = 0.0; + std::shared_ptr contents; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/vertex_buffer_builder.h b/engine/src/flutter/impeller/compositor/vertex_buffer_builder.h index db2dffdf85..1d5a266902 100644 --- a/engine/src/flutter/impeller/compositor/vertex_buffer_builder.h +++ b/engine/src/flutter/impeller/compositor/vertex_buffer_builder.h @@ -47,10 +47,10 @@ class VertexBufferBuilder { return *this; } - VertexBuffer CreateVertexBuffer(HostBuffer& host) const { + VertexBuffer CreateVertexBuffer(HostBuffer& host_buffer) const { VertexBuffer buffer; - buffer.vertex_buffer = CreateVertexBufferView(host); - buffer.index_buffer = CreateIndexBufferView(host); + buffer.vertex_buffer = CreateVertexBufferView(host_buffer); + buffer.index_buffer = CreateIndexBufferView(host_buffer); buffer.index_count = GetIndexCount(); return buffer; }; diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index fece214786..ef9cc7aa89 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -8,13 +8,19 @@ impeller_shaders("entity_shaders") { name = "entity" shaders = [ - "path.vert", + "gradient_fill.frag", + "gradient_fill.vert", "solid_fill.frag", + "solid_fill.vert", ] } impeller_component("entity") { sources = [ + "content_renderer.h", + "content_renderer.mm", + "contents.h", + "contents.mm", "entity.cc", "entity.h", "entity_renderer.h", diff --git a/engine/src/flutter/impeller/entity/content_renderer.h b/engine/src/flutter/impeller/entity/content_renderer.h new file mode 100644 index 0000000000..6b113d86c5 --- /dev/null +++ b/engine/src/flutter/impeller/entity/content_renderer.h @@ -0,0 +1,39 @@ +// 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 + +#include "flutter/fml/macros.h" +#include "impeller/compositor/pipeline.h" +#include "impeller/entity/gradient_fill.frag.h" +#include "impeller/entity/gradient_fill.vert.h" + +namespace impeller { + +using GradientFillPipeline = + PipelineT; + +class ContentRenderer { + public: + ContentRenderer(std::shared_ptr context); + + ~ContentRenderer(); + + bool IsValid() const; + + std::shared_ptr GetGradientFillPipeline() const; + + std::shared_ptr GetContext() const; + + private: + std::shared_ptr context_; + std::unique_ptr gradient_fill_pipeline_; + bool is_valid_ = false; + + FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/content_renderer.mm b/engine/src/flutter/impeller/entity/content_renderer.mm new file mode 100644 index 0000000000..4ea53ec0c4 --- /dev/null +++ b/engine/src/flutter/impeller/entity/content_renderer.mm @@ -0,0 +1,37 @@ +// 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/content_renderer.h" + +namespace impeller { + +ContentRenderer::ContentRenderer(std::shared_ptr context) + : context_(std::move(context)) { + if (!context_ || !context_->IsValid()) { + return; + } + + gradient_fill_pipeline_ = std::make_unique(*context_); + + is_valid_ = true; +} + +ContentRenderer::~ContentRenderer() = default; + +bool ContentRenderer::IsValid() const { + return is_valid_; +} + +std::shared_ptr ContentRenderer::GetContext() const { + return context_; +} + +std::shared_ptr ContentRenderer::GetGradientFillPipeline() const { + if (!IsValid()) { + return nullptr; + } + return gradient_fill_pipeline_->WaitAndGet(); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents.h b/engine/src/flutter/impeller/entity/contents.h new file mode 100644 index 0000000000..a2a55bf404 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents.h @@ -0,0 +1,61 @@ +// 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 + +#include "flutter/fml/macros.h" +#include "impeller/geometry/color.h" +#include "impeller/geometry/point.h" + +namespace impeller { + +class ContentRenderer; +class Entity; +class Surface; +class RenderPass; + +class Contents { + public: + Contents(); + + ~Contents(); + + virtual bool Render(const ContentRenderer& renderer, + const Entity& entity, + const Surface& surface, + RenderPass& pass) const = 0; + + private: + FML_DISALLOW_COPY_AND_ASSIGN(Contents); +}; + +class LinearGradientContents final : public Contents { + public: + LinearGradientContents(); + + ~LinearGradientContents(); + + // |Contents| + bool Render(const ContentRenderer& renderer, + const Entity& entity, + const Surface& surface, + RenderPass& pass) const override; + + void SetEndPoints(Point start_point, Point end_point); + + void SetColors(std::vector colors); + + const std::vector& GetColors() const; + + private: + Point start_point_; + Point end_point_; + std::vector colors_; + + FML_DISALLOW_COPY_AND_ASSIGN(LinearGradientContents); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents.mm b/engine/src/flutter/impeller/entity/contents.mm new file mode 100644 index 0000000000..9f57215f3b --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents.mm @@ -0,0 +1,88 @@ +// 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.h" + +#include "flutter/fml/logging.h" +#include "impeller/compositor/render_pass.h" +#include "impeller/compositor/surface.h" +#include "impeller/compositor/tessellator.h" +#include "impeller/compositor/vertex_buffer_builder.h" +#include "impeller/entity/content_renderer.h" +#include "impeller/entity/entity.h" + +namespace impeller { + +Contents::Contents() = default; + +Contents::~Contents() = default; + +LinearGradientContents::LinearGradientContents() = default; + +LinearGradientContents::~LinearGradientContents() = default; + +void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) { + start_point_ = start_point; + end_point_ = end_point; +} + +void LinearGradientContents::SetColors(std::vector colors) { + colors_ = std::move(colors); + if (colors_.empty()) { + colors_.push_back(Color::Black()); + colors_.push_back(Color::Black()); + } else if (colors_.size() < 2u) { + colors_.push_back(colors_.back()); + } +} + +const std::vector& LinearGradientContents::GetColors() const { + return colors_; +} + +// |Contents| +bool LinearGradientContents::Render(const ContentRenderer& renderer, + const Entity& entity, + const Surface& surface, + RenderPass& pass) const { + using VS = GradientFillPipeline::VertexShader; + using FS = GradientFillPipeline::FragmentShader; + + auto vertices_builder = VertexBufferBuilder(); + { + auto result = + Tessellator{}.Tessellate(entity.GetPath().SubdivideAdaptively(), + [&vertices_builder](Point point) { + VS::PerVertexData vtx; + vtx.vertices = point; + vertices_builder.AppendVertex(vtx); + }); + if (!result) { + return false; + } + } + + VS::FrameInfo frame_info; + frame_info.mvp = + Matrix::MakeOrthographic(surface.GetSize()) * entity.GetTransformation(); + + FS::GradientInfo gradient_info; + gradient_info.start_point = start_point_; + gradient_info.end_point = end_point_; + gradient_info.start_color = colors_[0]; + gradient_info.end_color = colors_[1]; + + Command cmd; + cmd.label = "LinearGradientFill"; + cmd.pipeline = renderer.GetGradientFillPipeline(); + cmd.BindVertices(vertices_builder.CreateVertexBuffer( + *renderer.GetContext()->GetPermanentsAllocator())); + cmd.primitive_type = PrimitiveType::kTriangle; + FS::BindGradientInfo( + cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info)); + VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); + return pass.RecordCommand(std::move(cmd)); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity.cc b/engine/src/flutter/impeller/entity/entity.cc index 51acf97e5a..186d8e1aac 100644 --- a/engine/src/flutter/impeller/entity/entity.cc +++ b/engine/src/flutter/impeller/entity/entity.cc @@ -58,11 +58,22 @@ bool Entity::IsClip() const { return is_clip_; } +void Entity::SetContents(std::shared_ptr contents) { + contents_ = std::move(contents); +} + +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(); } diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 43dd3f3fdc..ed3942fc8a 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -4,6 +4,7 @@ #pragma once +#include "impeller/entity/contents.h" #include "impeller/geometry/color.h" #include "impeller/geometry/matrix.h" #include "impeller/geometry/path.h" @@ -48,9 +49,14 @@ class Entity { bool HasRenderableContents() const; + void SetContents(std::shared_ptr contents); + + const std::shared_ptr& GetContents() const; + private: Matrix transformation_; Color background_color_; + std::shared_ptr contents_; Path path_; Color stroke_color_; diff --git a/engine/src/flutter/impeller/entity/entity_renderer.mm b/engine/src/flutter/impeller/entity/entity_renderer.mm index 06b0a439ca..a352ce4f4e 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer.mm +++ b/engine/src/flutter/impeller/entity/entity_renderer.mm @@ -30,29 +30,14 @@ bool EntityRenderer::RenderEntities(const Surface& surface, return false; } - size_t success = 0u; - size_t failure = 0u; - size_t skipped = 0u; - for (const auto& entity : entities) { - auto result = renderer_->RenderEntity(surface, onscreen_pass, entity); - switch (result) { - case EntityRendererImpl::RenderResult::kSuccess: - success++; - break; - case EntityRendererImpl::RenderResult::kFailure: - failure++; - break; - case EntityRendererImpl::RenderResult::kSkipped: - skipped++; - break; + if (renderer_->RenderEntity(surface, onscreen_pass, entity) == + EntityRendererImpl::RenderResult::kFailure) { + return false; } } - FML_LOG(ERROR) << "Success " << success << " failure " << failure - << " skipped " << skipped << " total " << entities.size(); - - return failure == 0; + return true; } } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/entity_renderer_impl.h b/engine/src/flutter/impeller/entity/entity_renderer_impl.h index 1bf525227d..1f40114aac 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer_impl.h +++ b/engine/src/flutter/impeller/entity/entity_renderer_impl.h @@ -12,9 +12,10 @@ #include "impeller/compositor/pipeline_builder.h" #include "impeller/compositor/render_pass.h" #include "impeller/compositor/surface.h" +#include "impeller/entity/content_renderer.h" #include "impeller/entity/entity.h" -#include "impeller/entity/path.vert.h" #include "impeller/entity/solid_fill.frag.h" +#include "impeller/entity/solid_fill.vert.h" namespace impeller { @@ -41,10 +42,11 @@ class EntityRendererImpl { private: using SolidFillPipeline = - PipelineT; + 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_renderer_impl.mm b/engine/src/flutter/impeller/entity/entity_renderer_impl.mm index 531fbfc15f..13caea3acb 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer_impl.mm +++ b/engine/src/flutter/impeller/entity/entity_renderer_impl.mm @@ -16,6 +16,11 @@ EntityRendererImpl::EntityRendererImpl(std::shared_ptr context) return; } + content_renderer_ = std::make_unique(context_); + if (!content_renderer_->IsValid()) { + return; + } + solid_fill_pipeline_ = std::make_unique(*context_); is_valid_ = true; @@ -32,13 +37,10 @@ EntityRendererImpl::RenderResult EntityRendererImpl::RenderEntity( RenderPass& pass, const Entity& entity) { if (!entity.HasRenderableContents()) { - if (entity.GetPath().GetBoundingBox().IsZero()) { - FML_LOG(ERROR) << "Skipped because bounds box zero."; - } return RenderResult::kSkipped; } - if (entity.HasContents() && !entity.IsClip()) { + if (entity.HasContents() && !entity.IsClip() && !entity.GetContents()) { using VS = SolidFillPipeline::VertexShader; Command cmd; @@ -50,7 +52,6 @@ EntityRendererImpl::RenderResult EntityRendererImpl::RenderEntity( VertexBufferBuilder vtx_builder; { - TRACE_EVENT0("flutter", "Tesselate"); auto tesselation_result = Tessellator{}.Tessellate( entity.GetPath().SubdivideAdaptively(), [&vtx_builder](auto point) { VS::PerVertexData vtx; @@ -77,6 +78,12 @@ EntityRendererImpl::RenderResult EntityRendererImpl::RenderEntity( if (!pass.RecordCommand(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; diff --git a/engine/src/flutter/impeller/entity/gradient_fill.frag b/engine/src/flutter/impeller/entity/gradient_fill.frag new file mode 100644 index 0000000000..b70cc6b879 --- /dev/null +++ b/engine/src/flutter/impeller/entity/gradient_fill.frag @@ -0,0 +1,24 @@ +// 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. + +uniform GradientInfo { + vec2 start_point; + vec2 end_point; + vec4 start_color; + vec4 end_color; +} gradient_info; + +in vec2 interpolated_vertices; + +out vec4 frag_color; + +void main() { + float len = length(gradient_info.end_point - gradient_info.start_point); + float dot = dot( + interpolated_vertices - gradient_info.start_point, + gradient_info.end_point - gradient_info.start_point + ); + float interp = dot / (len * len); + frag_color = mix(gradient_info.start_color, gradient_info.end_color, interp); +} diff --git a/engine/src/flutter/impeller/entity/gradient_fill.vert b/engine/src/flutter/impeller/entity/gradient_fill.vert new file mode 100644 index 0000000000..f9d0ccdabf --- /dev/null +++ b/engine/src/flutter/impeller/entity/gradient_fill.vert @@ -0,0 +1,16 @@ +// 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. + +uniform FrameInfo { + mat4 mvp; +} frame_info; + +in vec2 vertices; + +out vec2 interpolated_vertices; + +void main() { + gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0); + interpolated_vertices = vertices; +} diff --git a/engine/src/flutter/impeller/entity/path.vert b/engine/src/flutter/impeller/entity/solid_fill.vert similarity index 100% rename from engine/src/flutter/impeller/entity/path.vert rename to engine/src/flutter/impeller/entity/solid_fill.vert