diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 0e2dac1ecc..10608b797c 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -565,6 +565,10 @@ FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_in FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.cc FILE: ../../../flutter/impeller/entity/contents/linear_gradient_contents.h +FILE: ../../../flutter/impeller/entity/contents/path_contents.cc +FILE: ../../../flutter/impeller/entity/contents/path_contents.h +FILE: ../../../flutter/impeller/entity/contents/radial_gradient_contents.cc +FILE: ../../../flutter/impeller/entity/contents/radial_gradient_contents.h FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.cc FILE: ../../../flutter/impeller/entity/contents/solid_color_contents.h FILE: ../../../flutter/impeller/entity/contents/solid_stroke_contents.cc @@ -614,6 +618,8 @@ FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas.frag FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas.vert FILE: ../../../flutter/impeller/entity/shaders/gradient_fill.frag FILE: ../../../flutter/impeller/entity/shaders/gradient_fill.vert +FILE: ../../../flutter/impeller/entity/shaders/radial_gradient_fill.frag +FILE: ../../../flutter/impeller/entity/shaders/radial_gradient_fill.vert FILE: ../../../flutter/impeller/entity/shaders/solid_fill.frag FILE: ../../../flutter/impeller/entity/shaders/solid_fill.vert FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.frag diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 815a018bc7..488728e1bf 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -203,6 +203,23 @@ TEST_P(AiksTest, CanSaveLayerStandalone) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, CanRenderRadialGradient) { + Canvas canvas; + + Paint paint; + auto contents = std::make_shared(); + contents->SetCenterAndRadius({125, 125}, 100); + std::vector colors = {Color{0.9019, 0.3921, 0.3960, 1.0}, + Color{0.5686, 0.5960, 0.8980, 1.0}}; + contents->SetColors(std::move(colors)); + paint.contents = contents; + + canvas.DrawRect({25, 25, 200, 200}, paint); + canvas.Restore(); + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + TEST_P(AiksTest, CanRenderGroupOpacity) { Canvas canvas; diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 910d934544..edf9b19766 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -10,6 +10,7 @@ #include "impeller/entity/contents/contents.h" #include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/linear_gradient_contents.h" +#include "impeller/entity/contents/radial_gradient_contents.h" #include "impeller/entity/contents/solid_stroke_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/color.h" @@ -30,7 +31,7 @@ struct Paint { }; Color color = Color::Black(); - std::shared_ptr contents; + std::shared_ptr contents; Scalar stroke_width = 0.0; SolidStrokeContents::Cap stroke_cap = SolidStrokeContents::Cap::kButt; diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index 9182ba8a35..ef4514288d 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -15,6 +15,7 @@ #include "impeller/display_list/display_list_image_impeller.h" #include "impeller/entity/contents/filters/filter_contents.h" #include "impeller/entity/contents/linear_gradient_contents.h" +#include "impeller/entity/contents/radial_gradient_contents.h" #include "impeller/entity/contents/solid_stroke_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/path.h" @@ -219,7 +220,16 @@ void DisplayListDispatcher::setColorSource( return; } case flutter::DlColorSourceType::kImage: - case flutter::DlColorSourceType::kRadialGradient: + case flutter::DlColorSourceType::kRadialGradient: { + const flutter::DlRadialGradientColorSource* radialGradient = + source->asRadialGradient(); + FML_CHECK(radialGradient); + auto contents = std::make_shared(); + contents->SetCenterAndRadius(ToPoint(radialGradient->center()), + radialGradient->radius()); + paint_.contents = std::move(contents); + return; + } case flutter::DlColorSourceType::kConicalGradient: case flutter::DlColorSourceType::kSweepGradient: case flutter::DlColorSourceType::kUnknown: diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 51bd1b7405..48dfab0a5c 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -42,6 +42,8 @@ impeller_shaders("entity_shaders") { "shaders/texture_fill.vert", "shaders/vertices.vert", "shaders/vertices.frag", + "shaders/radial_gradient_fill.vert", + "shaders/radial_gradient_fill.frag", ] } @@ -71,6 +73,10 @@ impeller_component("entity") { "contents/filters/inputs/texture_filter_input.h", "contents/linear_gradient_contents.cc", "contents/linear_gradient_contents.h", + "contents/path_contents.cc", + "contents/path_contents.h", + "contents/radial_gradient_contents.cc", + "contents/radial_gradient_contents.h", "contents/solid_color_contents.cc", "contents/solid_color_contents.h", "contents/solid_stroke_contents.cc", diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 8a1a29c701..60c6a062db 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -153,6 +153,8 @@ ContentContext::ContentContext(std::shared_ptr context) CreateDefaultPipeline(*context_); solid_fill_pipelines_[{}] = CreateDefaultPipeline(*context_); + radial_gradient_fill_pipelines_[{}] = + CreateDefaultPipeline(*context_); texture_blend_pipelines_[{}] = CreateDefaultPipeline(*context_); blend_color_pipelines_[{}] = diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index aa3a5e0787..fb627bad17 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -38,6 +38,8 @@ #include "impeller/entity/glyph_atlas.vert.h" #include "impeller/entity/gradient_fill.frag.h" #include "impeller/entity/gradient_fill.vert.h" +#include "impeller/entity/radial_gradient_fill.frag.h" +#include "impeller/entity/radial_gradient_fill.vert.h" #include "impeller/entity/solid_fill.frag.h" #include "impeller/entity/solid_fill.vert.h" #include "impeller/entity/solid_stroke.frag.h" @@ -54,6 +56,8 @@ using GradientFillPipeline = PipelineT; using SolidFillPipeline = PipelineT; +using RadialGradientFillPipeline = + PipelineT; using BlendPipeline = PipelineT; using BlendColorPipeline = PipelineT; @@ -139,6 +143,10 @@ class ContentContext { ContentContextOptions opts) const { return GetPipeline(gradient_fill_pipelines_, opts); } + std::shared_ptr GetRadialGradientFillPipeline( + ContentContextOptions opts) const { + return GetPipeline(radial_gradient_fill_pipelines_, opts); + } std::shared_ptr GetSolidFillPipeline( ContentContextOptions opts) const { @@ -284,6 +292,7 @@ class ContentContext { // map. mutable Variants gradient_fill_pipelines_; mutable Variants solid_fill_pipelines_; + mutable Variants radial_gradient_fill_pipelines_; mutable Variants texture_blend_pipelines_; mutable Variants texture_pipelines_; mutable Variants gaussian_blur_pipelines_; 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 5fafed5f96..e534a9423e 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h @@ -9,20 +9,20 @@ #include #include "flutter/fml/macros.h" -#include "impeller/entity/contents/contents.h" +#include "impeller/entity/contents/path_contents.h" #include "impeller/geometry/color.h" #include "impeller/geometry/path.h" #include "impeller/geometry/point.h" namespace impeller { -class LinearGradientContents final : public Contents { +class LinearGradientContents final : public PathContents { public: LinearGradientContents(); ~LinearGradientContents() override; - void SetPath(Path path); + void SetPath(Path path) override; // |Contents| std::optional GetCoverage(const Entity& entity) const override; diff --git a/engine/src/flutter/impeller/entity/contents/path_contents.cc b/engine/src/flutter/impeller/entity/contents/path_contents.cc new file mode 100644 index 0000000000..afa956a8cd --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/path_contents.cc @@ -0,0 +1,13 @@ +// 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 "path_contents.h" + +namespace impeller { + +PathContents::PathContents() = default; + +PathContents::~PathContents() = default; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/path_contents.h b/engine/src/flutter/impeller/entity/contents/path_contents.h new file mode 100644 index 0000000000..f02430570f --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/path_contents.h @@ -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. + +#pragma once + +#include "flutter/fml/macros.h" +#include "impeller/entity/contents/contents.h" +#include "impeller/geometry/path.h" + +namespace impeller { + +class PathContents : public Contents { + public: + PathContents(); + + ~PathContents() override; + + virtual void SetPath(Path path) = 0; + + FML_DISALLOW_COPY_AND_ASSIGN(PathContents); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc new file mode 100644 index 0000000000..f94fca5116 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -0,0 +1,95 @@ +// 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 "radial_gradient_contents.h" + +#include "flutter/fml/logging.h" +#include "impeller/entity/contents/content_context.h" +#include "impeller/entity/entity.h" +#include "impeller/renderer/render_pass.h" +#include "impeller/tessellator/tessellator.h" + +namespace impeller { + +RadialGradientContents::RadialGradientContents() = default; + +RadialGradientContents::~RadialGradientContents() = default; + +void RadialGradientContents::SetPath(Path path) { + path_ = std::move(path); +} + +void RadialGradientContents::SetCenterAndRadius(Point centre, Scalar radius) { + center_ = centre; + radius_ = radius; +} + +void RadialGradientContents::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& RadialGradientContents::GetColors() const { + return colors_; +} + +std::optional RadialGradientContents::GetCoverage( + const Entity& entity) const { + return path_.GetTransformedBoundingBox(entity.GetTransformation()); +}; + +bool RadialGradientContents::Render(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const { + using VS = RadialGradientFillPipeline::VertexShader; + using FS = RadialGradientFillPipeline::FragmentShader; + + auto vertices_builder = VertexBufferBuilder(); + { + auto result = + Tessellator{}.Tessellate(path_.GetFillType(), path_.CreatePolyline(), + [&vertices_builder](Point point) { + VS::PerVertexData vtx; + vtx.vertices = point; + vertices_builder.AppendVertex(vtx); + }); + + if (result == Tessellator::Result::kInputError) { + return true; + } + if (result == Tessellator::Result::kTessellationError) { + return false; + } + } + + VS::FrameInfo frame_info; + frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * + entity.GetTransformation(); + + FS::GradientInfo gradient_info; + gradient_info.center = center_; + gradient_info.radius = radius_; + gradient_info.center_color = colors_[0].Premultiply(); + gradient_info.edge_color = colors_[1].Premultiply(); + + Command cmd; + cmd.label = "RadialGradientFill"; + cmd.pipeline = renderer.GetRadialGradientFillPipeline( + OptionsFromPassAndEntity(pass, entity)); + cmd.stencil_reference = entity.GetStencilDepth(); + cmd.BindVertices( + vertices_builder.CreateVertexBuffer(pass.GetTransientsBuffer())); + cmd.primitive_type = PrimitiveType::kTriangle; + FS::BindGradientInfo( + cmd, pass.GetTransientsBuffer().EmplaceUniform(gradient_info)); + VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); + return pass.AddCommand(std::move(cmd)); +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h new file mode 100644 index 0000000000..83e096841d --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h @@ -0,0 +1,50 @@ +// 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 +#include + +#include "flutter/fml/macros.h" +#include "impeller/entity/contents/path_contents.h" +#include "impeller/geometry/color.h" +#include "impeller/geometry/path.h" +#include "impeller/geometry/point.h" + +namespace impeller { + +class RadialGradientContents final : public PathContents { + public: + RadialGradientContents(); + + ~RadialGradientContents() override; + + void SetPath(Path path) override; + + // |Contents| + std::optional GetCoverage(const Entity& entity) const override; + + // |Contents| + bool Render(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const override; + + void SetCenterAndRadius(Point centre, Scalar radius); + + void SetColors(std::vector colors); + + const std::vector& GetColors() const; + + private: + Path path_; + Point center_; + Scalar radius_; + std::vector colors_; + + FML_DISALLOW_COPY_AND_ASSIGN(RadialGradientContents); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag new file mode 100644 index 0000000000..5e1dcc7380 --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag @@ -0,0 +1,20 @@ +// 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 center; + float radius; + vec4 center_color; + vec4 edge_color; +} gradient_info; + +in vec2 interpolated_vertices; + +out vec4 frag_color; + +void main() { + float len = length(interpolated_vertices - gradient_info.center); + float t = smoothstep(0.0, gradient_info.radius, len); + frag_color = mix(gradient_info.center_color, gradient_info.edge_color, t); +} diff --git a/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.vert b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.vert new file mode 100644 index 0000000000..f9d0ccdabf --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/radial_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; +}