diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 4d0e29b478..55e5a9f1e6 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -203,18 +203,51 @@ TEST_P(AiksTest, CanSaveLayerStandalone) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, CanRenderLinearGradient) { + 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->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}}; + 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(); - 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); + 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->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}}; + 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())); } diff --git a/engine/src/flutter/impeller/compiler/shader_lib/impeller/texture.glsl b/engine/src/flutter/impeller/compiler/shader_lib/impeller/texture.glsl index 5d24983771..9ef001303b 100644 --- a/engine/src/flutter/impeller/compiler/shader_lib/impeller/texture.glsl +++ b/engine/src/flutter/impeller/compiler/shader_lib/impeller/texture.glsl @@ -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 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 b78cffd2aa..550e9d4816 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -246,6 +246,7 @@ void DisplayListDispatcher::setColorSource( colors.emplace_back(ToColor(linear->colors()[i])); } contents->SetColors(std::move(colors)); + contents->SetTileMode(static_cast(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(radialGradient->tile_mode())); paint_.contents = std::move(contents); return; } diff --git a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc index d4188efbec..374e990d4e 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.cc @@ -35,6 +35,10 @@ void LinearGradientContents::SetColors(std::vector colors) { } } +void LinearGradientContents::SetTileMode(Entity::TileMode tile_mode) { + tile_mode_ = tile_mode; +} + const std::vector& 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(tile_mode_); Command cmd; cmd.label = "LinearGradientFill"; 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 e534a9423e..71605fdd0f 100644 --- a/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/linear_gradient_contents.h @@ -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 colors); + void SetTileMode(Entity::TileMode tile_mode); + const std::vector& GetColors() const; private: @@ -43,6 +46,7 @@ class LinearGradientContents final : public PathContents { Point start_point_; Point end_point_; std::vector colors_; + Entity::TileMode tile_mode_; FML_DISALLOW_COPY_AND_ASSIGN(LinearGradientContents); }; 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 f94fca5116..4ea50e0a1d 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.cc @@ -35,6 +35,10 @@ void RadialGradientContents::SetColors(std::vector colors) { } } +void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) { + tile_mode_ = tile_mode; +} + const std::vector& 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(tile_mode_); Command cmd; cmd.label = "RadialGradientFill"; 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 83e096841d..3e936ae383 100644 --- a/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h +++ b/engine/src/flutter/impeller/entity/contents/radial_gradient_contents.h @@ -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 colors); + void SetTileMode(Entity::TileMode tile_mode); + const std::vector& GetColors() const; private: @@ -43,6 +46,7 @@ class RadialGradientContents final : public PathContents { Point center_; Scalar radius_; std::vector colors_; + Entity::TileMode tile_mode_; FML_DISALLOW_COPY_AND_ASSIGN(RadialGradientContents); }; diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index 7c83f0f2da..b79ce60699 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -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, diff --git a/engine/src/flutter/impeller/entity/shaders/gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/gradient_fill.frag index b70cc6b879..858c56b83b 100644 --- a/engine/src/flutter/impeller/entity/shaders/gradient_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/gradient_fill.frag @@ -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 + 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); } diff --git a/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag index 5e1dcc7380..1e6f46dace 100644 --- a/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag +++ b/engine/src/flutter/impeller/entity/shaders/radial_gradient_fill.frag @@ -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 + 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); }