[Impeller] Implement radial gradient (flutter/engine#34875)

This commit is contained in:
magicianA
2022-07-26 06:25:04 +08:00
committed by GitHub
parent 9f99402e3b
commit 1836f9d90c
14 changed files with 274 additions and 5 deletions

View File

@@ -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

View File

@@ -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<RadialGradientContents>();
contents->SetCenterAndRadius({125, 125}, 100);
std::vector<Color> 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;

View File

@@ -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<LinearGradientContents> contents;
std::shared_ptr<PathContents> contents;
Scalar stroke_width = 0.0;
SolidStrokeContents::Cap stroke_cap = SolidStrokeContents::Cap::kButt;

View File

@@ -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<RadialGradientContents>();
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:

View File

@@ -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",

View File

@@ -153,6 +153,8 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
CreateDefaultPipeline<GradientFillPipeline>(*context_);
solid_fill_pipelines_[{}] =
CreateDefaultPipeline<SolidFillPipeline>(*context_);
radial_gradient_fill_pipelines_[{}] =
CreateDefaultPipeline<RadialGradientFillPipeline>(*context_);
texture_blend_pipelines_[{}] =
CreateDefaultPipeline<BlendPipeline>(*context_);
blend_color_pipelines_[{}] =

View File

@@ -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<GradientFillVertexShader, GradientFillFragmentShader>;
using SolidFillPipeline =
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
using RadialGradientFillPipeline =
PipelineT<RadialGradientFillVertexShader, RadialGradientFillFragmentShader>;
using BlendPipeline = PipelineT<BlendVertexShader, BlendFragmentShader>;
using BlendColorPipeline =
PipelineT<AdvancedBlendVertexShader, AdvancedBlendColorFragmentShader>;
@@ -139,6 +143,10 @@ class ContentContext {
ContentContextOptions opts) const {
return GetPipeline(gradient_fill_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetRadialGradientFillPipeline(
ContentContextOptions opts) const {
return GetPipeline(radial_gradient_fill_pipelines_, opts);
}
std::shared_ptr<Pipeline> GetSolidFillPipeline(
ContentContextOptions opts) const {
@@ -284,6 +292,7 @@ class ContentContext {
// map.
mutable Variants<GradientFillPipeline> gradient_fill_pipelines_;
mutable Variants<SolidFillPipeline> solid_fill_pipelines_;
mutable Variants<RadialGradientFillPipeline> radial_gradient_fill_pipelines_;
mutable Variants<BlendPipeline> texture_blend_pipelines_;
mutable Variants<TexturePipeline> texture_pipelines_;
mutable Variants<GaussianBlurPipeline> gaussian_blur_pipelines_;

View File

@@ -9,20 +9,20 @@
#include <vector>
#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<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -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

View File

@@ -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

View File

@@ -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<Color> 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<Color>& RadialGradientContents::GetColors() const {
return colors_;
}
std::optional<Rect> 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<VS::PerVertexData>();
{
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

View File

@@ -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 <functional>
#include <memory>
#include <vector>
#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<Rect> 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<Color> colors);
const std::vector<Color>& GetColors() const;
private:
Path path_;
Point center_;
Scalar radius_;
std::vector<Color> colors_;
FML_DISALLOW_COPY_AND_ASSIGN(RadialGradientContents);
};
} // namespace impeller

View File

@@ -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);
}

View File

@@ -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;
}