From 61110f934a61f27cb56f7d295dda78bf546b4161 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Mon, 8 Aug 2022 14:58:04 +0800 Subject: [PATCH] [Impeller] Implement sweep gradient (flutter/engine#35124) --- .../ci/licenses_golden/licenses_flutter | 4 + .../flutter/impeller/aiks/aiks_unittests.cc | 33 +++++- engine/src/flutter/impeller/aiks/paint.h | 1 + .../shader_lib/impeller/constants.glsl | 1 + .../display_list/display_list_dispatcher.cc | 22 +++- engine/src/flutter/impeller/entity/BUILD.gn | 8 +- .../entity/contents/content_context.cc | 2 + .../entity/contents/content_context.h | 11 ++ .../contents/radial_gradient_contents.cc | 4 +- .../contents/radial_gradient_contents.h | 2 +- .../contents/sweep_gradient_contents.cc | 107 ++++++++++++++++++ .../entity/contents/sweep_gradient_contents.h | 56 +++++++++ .../entity/shaders/sweep_gradient_fill.frag | 33 ++++++ .../entity/shaders/sweep_gradient_fill.vert | 16 +++ 14 files changed, 289 insertions(+), 11 deletions(-) create mode 100644 engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc create mode 100644 engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h create mode 100644 engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag create mode 100644 engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.vert diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 5343b8072b..2ef7fd3b18 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -581,6 +581,8 @@ 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 FILE: ../../../flutter/impeller/entity/contents/solid_stroke_contents.h +FILE: ../../../flutter/impeller/entity/contents/sweep_gradient_contents.cc +FILE: ../../../flutter/impeller/entity/contents/sweep_gradient_contents.h FILE: ../../../flutter/impeller/entity/contents/text_contents.cc FILE: ../../../flutter/impeller/entity/contents/text_contents.h FILE: ../../../flutter/impeller/entity/contents/texture_contents.cc @@ -635,6 +637,8 @@ FILE: ../../../flutter/impeller/entity/shaders/solid_fill.frag FILE: ../../../flutter/impeller/entity/shaders/solid_fill.vert FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.frag FILE: ../../../flutter/impeller/entity/shaders/solid_stroke.vert +FILE: ../../../flutter/impeller/entity/shaders/sweep_gradient_fill.frag +FILE: ../../../flutter/impeller/entity/shaders/sweep_gradient_fill.vert FILE: ../../../flutter/impeller/entity/shaders/texture_fill.frag FILE: ../../../flutter/impeller/entity/shaders/texture_fill.vert FILE: ../../../flutter/impeller/entity/shaders/vertices.frag diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index edd552f17d..d4fbf8c636 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -216,8 +216,8 @@ TEST_P(AiksTest, CanRenderLinearGradient) { canvas.Translate(offsets[i]); auto contents = std::make_shared(); contents->SetEndPoints({0, 0}, {100, 100}); - std::vector colors = {Color{0.9019, 0.3921, 0.3960, 1.0}, - Color{0.5686, 0.5960, 0.8980, 1.0}}; + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; contents->SetColors(std::move(colors)); contents->SetTileMode(tile_modes[i]); paint.contents = contents; @@ -240,8 +240,33 @@ TEST_P(AiksTest, CanRenderRadialGradient) { canvas.Translate(offsets[i]); auto contents = std::make_shared(); contents->SetCenterAndRadius({50, 50}, 50); - std::vector colors = {Color{0.9019, 0.3921, 0.3960, 1.0}, - Color{0.5686, 0.5960, 0.8980, 1.0}}; + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; + contents->SetColors(std::move(colors)); + contents->SetTileMode(tile_modes[i]); + paint.contents = contents; + canvas.DrawRect({0, 0, 200, 200}, paint); + canvas.Restore(); + } + + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + +TEST_P(AiksTest, CanRenderSweepGradient) { + Canvas canvas; + Paint paint; + std::vector offsets = { + {0, 0, 0}, {0, 300, 0}, {300, 0, 0}, {300, 300, 0}}; + std::vector tile_modes = { + Entity::TileMode::kClamp, Entity::TileMode::kRepeat, + Entity::TileMode::kMirror, Entity::TileMode::kDecal}; + for (int i = 0; i < 4; i++) { + canvas.Save(); + canvas.Translate(offsets[i]); + auto contents = std::make_shared(); + contents->SetCenterAndAngles({50, 50}, Degrees(45), Degrees(135)); + std::vector colors = {Color{0.9568, 0.2627, 0.2118, 1.0}, + Color{0.1294, 0.5882, 0.9529, 1.0}}; contents->SetColors(std::move(colors)); contents->SetTileMode(tile_modes[i]); paint.contents = contents; diff --git a/engine/src/flutter/impeller/aiks/paint.h b/engine/src/flutter/impeller/aiks/paint.h index 2a9f831bd0..8a70d3428e 100644 --- a/engine/src/flutter/impeller/aiks/paint.h +++ b/engine/src/flutter/impeller/aiks/paint.h @@ -12,6 +12,7 @@ #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/contents/sweep_gradient_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/color.h" diff --git a/engine/src/flutter/impeller/compiler/shader_lib/impeller/constants.glsl b/engine/src/flutter/impeller/compiler/shader_lib/impeller/constants.glsl index 5ccc5b298c..b8e2473983 100644 --- a/engine/src/flutter/impeller/compiler/shader_lib/impeller/constants.glsl +++ b/engine/src/flutter/impeller/compiler/shader_lib/impeller/constants.glsl @@ -6,5 +6,6 @@ #define CONSTANTS_GLSL_ const float kEhCloseEnough = 0.000001; +const float k1Over2Pi = 0.1591549430918; #endif 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 9560f1991b..a1d80528d6 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -18,6 +18,7 @@ #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/contents/sweep_gradient_contents.h" #include "impeller/entity/entity.h" #include "impeller/geometry/path.h" #include "impeller/geometry/path_builder.h" @@ -254,7 +255,7 @@ void DisplayListDispatcher::setColorSource( case flutter::DlColorSourceType::kRadialGradient: { const flutter::DlRadialGradientColorSource* radialGradient = source->asRadialGradient(); - FML_CHECK(radialGradient); + FML_DCHECK(radialGradient); auto contents = std::make_shared(); contents->SetCenterAndRadius(ToPoint(radialGradient->center()), radialGradient->radius()); @@ -268,9 +269,26 @@ void DisplayListDispatcher::setColorSource( paint_.contents = std::move(contents); return; } + case flutter::DlColorSourceType::kSweepGradient: { + const flutter::DlSweepGradientColorSource* sweepGradient = + source->asSweepGradient(); + FML_DCHECK(sweepGradient); + auto contents = std::make_shared(); + contents->SetCenterAndAngles(ToPoint(sweepGradient->center()), + Degrees(sweepGradient->start()), + Degrees(sweepGradient->end())); + std::vector colors; + for (auto i = 0; i < sweepGradient->stop_count(); i++) { + colors.emplace_back(ToColor(sweepGradient->colors()[i])); + } + contents->SetColors(std::move(colors)); + contents->SetTileMode( + static_cast(sweepGradient->tile_mode())); + paint_.contents = std::move(contents); + return; + } case flutter::DlColorSourceType::kImage: case flutter::DlColorSourceType::kConicalGradient: - case flutter::DlColorSourceType::kSweepGradient: case flutter::DlColorSourceType::kUnknown: UNIMPLEMENTED; break; diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index 3ebfe3d354..014d11484b 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -36,18 +36,20 @@ impeller_shaders("entity_shaders") { "shaders/glyph_atlas.vert", "shaders/gradient_fill.frag", "shaders/gradient_fill.vert", - "shaders/radial_gradient_fill.vert", "shaders/radial_gradient_fill.frag", + "shaders/radial_gradient_fill.vert", "shaders/rrect_blur.vert", "shaders/rrect_blur.frag", "shaders/solid_fill.frag", "shaders/solid_fill.vert", "shaders/solid_stroke.frag", "shaders/solid_stroke.vert", + "shaders/sweep_gradient_fill.frag", + "shaders/sweep_gradient_fill.vert", "shaders/texture_fill.frag", "shaders/texture_fill.vert", - "shaders/vertices.vert", "shaders/vertices.frag", + "shaders/vertices.vert", ] } @@ -89,6 +91,8 @@ impeller_component("entity") { "contents/solid_color_contents.h", "contents/solid_stroke_contents.cc", "contents/solid_stroke_contents.h", + "contents/sweep_gradient_contents.cc", + "contents/sweep_gradient_contents.h", "contents/text_contents.cc", "contents/text_contents.h", "contents/texture_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 f53bdfcf18..941ed5f451 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -155,6 +155,8 @@ ContentContext::ContentContext(std::shared_ptr context) CreateDefaultPipeline(*context_); radial_gradient_fill_pipelines_[{}] = CreateDefaultPipeline(*context_); + sweep_gradient_fill_pipelines_[{}] = + CreateDefaultPipeline(*context_); rrect_blur_pipelines_[{}] = CreateDefaultPipeline(*context_); texture_blend_pipelines_[{}] = diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index 8f2457ae3a..d31eb9435d 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -48,6 +48,8 @@ #include "impeller/entity/solid_fill.vert.h" #include "impeller/entity/solid_stroke.frag.h" #include "impeller/entity/solid_stroke.vert.h" +#include "impeller/entity/sweep_gradient_fill.frag.h" +#include "impeller/entity/sweep_gradient_fill.vert.h" #include "impeller/entity/texture_fill.frag.h" #include "impeller/entity/texture_fill.vert.h" #include "impeller/entity/vertices.frag.h" @@ -62,6 +64,8 @@ using SolidFillPipeline = PipelineT; using RadialGradientFillPipeline = PipelineT; +using SweepGradientFillPipeline = + PipelineT; using BlendPipeline = PipelineT; using RRectBlurPipeline = PipelineT; @@ -151,6 +155,7 @@ 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); @@ -160,6 +165,11 @@ class ContentContext { return GetPipeline(rrect_blur_pipelines_, opts); } + std::shared_ptr GetSweepGradientFillPipeline( + ContentContextOptions opts) const { + return GetPipeline(sweep_gradient_fill_pipelines_, opts); + } + std::shared_ptr GetSolidFillPipeline( ContentContextOptions opts) const { return GetPipeline(solid_fill_pipelines_, opts); @@ -309,6 +319,7 @@ class ContentContext { mutable Variants gradient_fill_pipelines_; mutable Variants solid_fill_pipelines_; mutable Variants radial_gradient_fill_pipelines_; + mutable Variants sweep_gradient_fill_pipelines_; mutable Variants rrect_blur_pipelines_; mutable Variants texture_blend_pipelines_; mutable Variants texture_pipelines_; diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc index 4ea50e0a1d..e39b622dc4 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -20,8 +20,8 @@ void RadialGradientContents::SetPath(Path path) { path_ = std::move(path); } -void RadialGradientContents::SetCenterAndRadius(Point centre, Scalar radius) { - center_ = centre; +void RadialGradientContents::SetCenterAndRadius(Point center, Scalar radius) { + center_ = center; radius_ = radius; } diff --git a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h index 3e936ae383..7db7cef5ad 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h @@ -33,7 +33,7 @@ class RadialGradientContents final : public PathContents { const Entity& entity, RenderPass& pass) const override; - void SetCenterAndRadius(Point centre, Scalar radius); + void SetCenterAndRadius(Point center, Scalar radius); void SetColors(std::vector colors); diff --git a/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc new file mode 100644 index 0000000000..d3874e1095 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.cc @@ -0,0 +1,107 @@ +// 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 "sweep_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 { + +SweepGradientContents::SweepGradientContents() = default; + +SweepGradientContents::~SweepGradientContents() = default; + +void SweepGradientContents::SetPath(Path path) { + path_ = std::move(path); +} + +void SweepGradientContents::SetCenterAndAngles(Point center, + Degrees start_angle, + Degrees end_angle) { + center_ = center; + Scalar t0 = start_angle.degrees / 360; + Scalar t1 = end_angle.degrees / 360; + FML_DCHECK(t0 < t1); + bias_ = -t0; + scale_ = 1 / (t1 - t0); +} + +void SweepGradientContents::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()); + } +} + +void SweepGradientContents::SetTileMode(Entity::TileMode tile_mode) { + tile_mode_ = tile_mode; +} + +const std::vector& SweepGradientContents::GetColors() const { + return colors_; +} + +std::optional SweepGradientContents::GetCoverage( + const Entity& entity) const { + return path_.GetTransformedBoundingBox(entity.GetTransformation()); +}; + +bool SweepGradientContents::Render(const ContentContext& renderer, + const Entity& entity, + RenderPass& pass) const { + using VS = SweepGradientFillPipeline::VertexShader; + using FS = SweepGradientFillPipeline::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.bias = bias_; + gradient_info.scale = scale_; + gradient_info.start_color = colors_[0].Premultiply(); + gradient_info.end_color = colors_[1].Premultiply(); + gradient_info.tile_mode = static_cast(tile_mode_); + + Command cmd; + cmd.label = "SweepGradientFill"; + cmd.pipeline = renderer.GetSweepGradientFillPipeline( + 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/sweep_gradient_contents.h b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h new file mode 100644 index 0000000000..cfd21249b4 --- /dev/null +++ b/engine/src/flutter/impeller/entity/contents/sweep_gradient_contents.h @@ -0,0 +1,56 @@ +// 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/entity/entity.h" +#include "impeller/geometry/color.h" +#include "impeller/geometry/path.h" +#include "impeller/geometry/point.h" +#include "impeller/geometry/scalar.h" + +namespace impeller { + +class SweepGradientContents final : public PathContents { + public: + SweepGradientContents(); + + ~SweepGradientContents() 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 SetCenterAndAngles(Point center, Degrees start_angle, Degrees end_angle); + + void SetColors(std::vector colors); + + void SetTileMode(Entity::TileMode tile_mode); + + const std::vector& GetColors() const; + + private: + Path path_; + Point center_; + Scalar bias_; + Scalar scale_; + std::vector colors_; + Entity::TileMode tile_mode_; + + FML_DISALLOW_COPY_AND_ASSIGN(SweepGradientContents); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag new file mode 100644 index 0000000000..1be76ba7b4 --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.frag @@ -0,0 +1,33 @@ +// 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 +#include + +uniform GradientInfo { + vec2 center; + float bias; + float scale; + vec4 start_color; + vec4 end_color; + float tile_mode; +} gradient_info; + +in vec2 interpolated_vertices; + +out vec4 frag_color; + +void main() { + vec2 coord = interpolated_vertices - gradient_info.center; + float angle = atan(-coord.y, -coord.x); + + float t = (angle * k1Over2Pi + 0.5 + gradient_info.bias) * gradient_info.scale; + if ((t < 0.0 || t > 1.0) && gradient_info.tile_mode == kTileModeDecal) { + frag_color = vec4(0); + return; + } + + t = IPTileTextureCoords(t, gradient_info.tile_mode); + frag_color = mix(gradient_info.start_color, gradient_info.end_color, t); +} diff --git a/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.vert b/engine/src/flutter/impeller/entity/shaders/sweep_gradient_fill.vert new file mode 100644 index 0000000000..f9d0ccdabf --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/sweep_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; +}