[Impeller] Implement tile mode for linear gradient and radial gradient (flutter/engine#35087)

This commit is contained in:
ColdPaleLight
2022-08-04 17:21:09 +08:00
committed by GitHub
parent c209f18ed4
commit 9d0dc1078c
10 changed files with 130 additions and 12 deletions

View File

@@ -203,18 +203,51 @@ TEST_P(AiksTest, CanSaveLayerStandalone) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, CanRenderLinearGradient) {
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<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}};
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, 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);
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<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}};
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()));
}

View File

@@ -28,4 +28,28 @@ vec4 IPSampleClampToBorder(sampler2D tex, vec2 uv) {
return texture(tex, uv) * within_bounds;
}
// These values must correspond to the order of the items in the
// 'Entity::TileMode' enum class.
const float kTileModeClamp = 0;
const float kTileModeRepeat = 1;
const float kTileModeMirror = 2;
const float kTileModeDecal = 3;
/// Compute an interpolant value "t".
///
/// The domain appears to be any value and the range is [0 to 1].
float IPTileTextureCoords(float t, float tile_mode) {
if (tile_mode == kTileModeClamp) {
t = clamp(t, 0.0, 1.0);
} else if (tile_mode == kTileModeRepeat) {
t = fract(t);
} else if (tile_mode == kTileModeMirror) {
float t1 = t - 1;
float t2 = t1 - 2 * floor(t1 * 0.5) - 1;
t = abs(t2);
}
return t;
}
#endif

View File

@@ -246,6 +246,7 @@ void DisplayListDispatcher::setColorSource(
colors.emplace_back(ToColor(linear->colors()[i]));
}
contents->SetColors(std::move(colors));
contents->SetTileMode(static_cast<Entity::TileMode>(linear->tile_mode()));
paint_.contents = std::move(contents);
return;
}
@@ -261,6 +262,8 @@ void DisplayListDispatcher::setColorSource(
colors.emplace_back(ToColor(radialGradient->colors()[i]));
}
contents->SetColors(std::move(colors));
contents->SetTileMode(
static_cast<Entity::TileMode>(radialGradient->tile_mode()));
paint_.contents = std::move(contents);
return;
}

View File

@@ -35,6 +35,10 @@ void LinearGradientContents::SetColors(std::vector<Color> colors) {
}
}
void LinearGradientContents::SetTileMode(Entity::TileMode tile_mode) {
tile_mode_ = tile_mode;
}
const std::vector<Color>& LinearGradientContents::GetColors() const {
return colors_;
}
@@ -77,6 +81,7 @@ bool LinearGradientContents::Render(const ContentContext& renderer,
gradient_info.end_point = end_point_;
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 = "LinearGradientFill";

View File

@@ -10,6 +10,7 @@
#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"
@@ -36,6 +37,8 @@ class LinearGradientContents final : public PathContents {
void SetColors(std::vector<Color> colors);
void SetTileMode(Entity::TileMode tile_mode);
const std::vector<Color>& GetColors() const;
private:
@@ -43,6 +46,7 @@ class LinearGradientContents final : public PathContents {
Point start_point_;
Point end_point_;
std::vector<Color> colors_;
Entity::TileMode tile_mode_;
FML_DISALLOW_COPY_AND_ASSIGN(LinearGradientContents);
};

View File

@@ -35,6 +35,10 @@ void RadialGradientContents::SetColors(std::vector<Color> colors) {
}
}
void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) {
tile_mode_ = tile_mode;
}
const std::vector<Color>& RadialGradientContents::GetColors() const {
return colors_;
}
@@ -77,6 +81,7 @@ bool RadialGradientContents::Render(const ContentContext& renderer,
gradient_info.radius = radius_;
gradient_info.center_color = colors_[0].Premultiply();
gradient_info.edge_color = colors_[1].Premultiply();
gradient_info.tile_mode = static_cast<Scalar>(tile_mode_);
Command cmd;
cmd.label = "RadialGradientFill";

View File

@@ -10,6 +10,7 @@
#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"
@@ -36,6 +37,8 @@ class RadialGradientContents final : public PathContents {
void SetColors(std::vector<Color> colors);
void SetTileMode(Entity::TileMode tile_mode);
const std::vector<Color>& GetColors() const;
private:
@@ -43,6 +46,7 @@ class RadialGradientContents final : public PathContents {
Point center_;
Scalar radius_;
std::vector<Color> colors_;
Entity::TileMode tile_mode_;
FML_DISALLOW_COPY_AND_ASSIGN(RadialGradientContents);
};

View File

@@ -62,6 +62,28 @@ class Entity {
kLastAdvancedBlendMode = kLuminosity,
};
/// An enum to define how to repeat, fold, or omit colors outside of the
/// typically defined range of the source of the colors (such as the
/// bounds of an image or the defining geometry of a gradient).
enum class TileMode {
/// Replicate the edge color if the shader draws outside of its original
/// bounds.
kClamp,
/// Repeat the shader's image horizontally and vertically (or both along and
/// perpendicular to a gradient's geometry).
kRepeat,
/// Repeat the shader's image horizontally and vertically, seamlessly
/// alternating mirrored images.
kMirror,
/// Render the shader's image pixels only within its original bounds. If the
/// shader draws outside of its original bounds, transparent black is drawn
/// instead.
kDecal,
};
enum class ClipOperation {
kDifference,
kIntersect,

View File

@@ -2,11 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
uniform GradientInfo {
vec2 start_point;
vec2 end_point;
vec4 start_color;
vec4 end_color;
float tile_mode;
} gradient_info;
in vec2 interpolated_vertices;
@@ -19,6 +22,12 @@ void main() {
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);
float t = dot / (len * len);
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);
}

View File

@@ -2,11 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
uniform GradientInfo {
vec2 center;
float radius;
vec4 center_color;
vec4 edge_color;
float tile_mode;
} gradient_info;
in vec2 interpolated_vertices;
@@ -15,6 +18,12 @@ out vec4 frag_color;
void main() {
float len = length(interpolated_vertices - gradient_info.center);
float t = smoothstep(0.0, gradient_info.radius, len);
float t = len / gradient_info.radius;
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.center_color, gradient_info.edge_color, t);
}