[Impeller] Implement sweep gradient (flutter/engine#35124)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -216,8 +216,8 @@ TEST_P(AiksTest, CanRenderLinearGradient) {
|
||||
canvas.Translate(offsets[i]);
|
||||
auto contents = std::make_shared<LinearGradientContents>();
|
||||
contents->SetEndPoints({0, 0}, {100, 100});
|
||||
std::vector<Color> colors = {Color{0.9019, 0.3921, 0.3960, 1.0},
|
||||
Color{0.5686, 0.5960, 0.8980, 1.0}};
|
||||
std::vector<Color> 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<RadialGradientContents>();
|
||||
contents->SetCenterAndRadius({50, 50}, 50);
|
||||
std::vector<Color> colors = {Color{0.9019, 0.3921, 0.3960, 1.0},
|
||||
Color{0.5686, 0.5960, 0.8980, 1.0}};
|
||||
std::vector<Color> 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<Vector3> offsets = {
|
||||
{0, 0, 0}, {0, 300, 0}, {300, 0, 0}, {300, 300, 0}};
|
||||
std::vector<Entity::TileMode> 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<SweepGradientContents>();
|
||||
contents->SetCenterAndAngles({50, 50}, Degrees(45), Degrees(135));
|
||||
std::vector<Color> 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;
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
#define CONSTANTS_GLSL_
|
||||
|
||||
const float kEhCloseEnough = 0.000001;
|
||||
const float k1Over2Pi = 0.1591549430918;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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<RadialGradientContents>();
|
||||
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<SweepGradientContents>();
|
||||
contents->SetCenterAndAngles(ToPoint(sweepGradient->center()),
|
||||
Degrees(sweepGradient->start()),
|
||||
Degrees(sweepGradient->end()));
|
||||
std::vector<Color> 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<Entity::TileMode>(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;
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -155,6 +155,8 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
|
||||
CreateDefaultPipeline<SolidFillPipeline>(*context_);
|
||||
radial_gradient_fill_pipelines_[{}] =
|
||||
CreateDefaultPipeline<RadialGradientFillPipeline>(*context_);
|
||||
sweep_gradient_fill_pipelines_[{}] =
|
||||
CreateDefaultPipeline<SweepGradientFillPipeline>(*context_);
|
||||
rrect_blur_pipelines_[{}] =
|
||||
CreateDefaultPipeline<RRectBlurPipeline>(*context_);
|
||||
texture_blend_pipelines_[{}] =
|
||||
|
||||
@@ -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<SolidFillVertexShader, SolidFillFragmentShader>;
|
||||
using RadialGradientFillPipeline =
|
||||
PipelineT<RadialGradientFillVertexShader, RadialGradientFillFragmentShader>;
|
||||
using SweepGradientFillPipeline =
|
||||
PipelineT<SweepGradientFillVertexShader, SweepGradientFillFragmentShader>;
|
||||
using BlendPipeline = PipelineT<BlendVertexShader, BlendFragmentShader>;
|
||||
using RRectBlurPipeline =
|
||||
PipelineT<RrectBlurVertexShader, RrectBlurFragmentShader>;
|
||||
@@ -151,6 +155,7 @@ 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);
|
||||
@@ -160,6 +165,11 @@ class ContentContext {
|
||||
return GetPipeline(rrect_blur_pipelines_, opts);
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> GetSweepGradientFillPipeline(
|
||||
ContentContextOptions opts) const {
|
||||
return GetPipeline(sweep_gradient_fill_pipelines_, opts);
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> GetSolidFillPipeline(
|
||||
ContentContextOptions opts) const {
|
||||
return GetPipeline(solid_fill_pipelines_, opts);
|
||||
@@ -309,6 +319,7 @@ class ContentContext {
|
||||
mutable Variants<GradientFillPipeline> gradient_fill_pipelines_;
|
||||
mutable Variants<SolidFillPipeline> solid_fill_pipelines_;
|
||||
mutable Variants<RadialGradientFillPipeline> radial_gradient_fill_pipelines_;
|
||||
mutable Variants<SweepGradientFillPipeline> sweep_gradient_fill_pipelines_;
|
||||
mutable Variants<RRectBlurPipeline> rrect_blur_pipelines_;
|
||||
mutable Variants<BlendPipeline> texture_blend_pipelines_;
|
||||
mutable Variants<TexturePipeline> texture_pipelines_;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Color> colors);
|
||||
|
||||
|
||||
@@ -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<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());
|
||||
}
|
||||
}
|
||||
|
||||
void SweepGradientContents::SetTileMode(Entity::TileMode tile_mode) {
|
||||
tile_mode_ = tile_mode;
|
||||
}
|
||||
|
||||
const std::vector<Color>& SweepGradientContents::GetColors() const {
|
||||
return colors_;
|
||||
}
|
||||
|
||||
std::optional<Rect> 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<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.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<Scalar>(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
|
||||
@@ -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 <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#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<Rect> 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<Color> colors);
|
||||
|
||||
void SetTileMode(Entity::TileMode tile_mode);
|
||||
|
||||
const std::vector<Color>& GetColors() const;
|
||||
|
||||
private:
|
||||
Path path_;
|
||||
Point center_;
|
||||
Scalar bias_;
|
||||
Scalar scale_;
|
||||
std::vector<Color> colors_;
|
||||
Entity::TileMode tile_mode_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SweepGradientContents);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -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 <impeller/constants.glsl>
|
||||
#include <impeller/texture.glsl>
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user