Flutter Engine
The Flutter Engine
linear_gradient_contents.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
15
16namespace impeller {
17
19
21
22void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) {
23 start_point_ = start_point;
24 end_point_ = end_point;
25}
26
27void LinearGradientContents::SetColors(std::vector<Color> colors) {
28 colors_ = std::move(colors);
29}
30
31void LinearGradientContents::SetStops(std::vector<Scalar> stops) {
32 stops_ = std::move(stops);
33}
34
35const std::vector<Color>& LinearGradientContents::GetColors() const {
36 return colors_;
37}
38
39const std::vector<Scalar>& LinearGradientContents::GetStops() const {
40 return stops_;
41}
42
44 tile_mode_ = tile_mode;
45}
46
48 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
49 return false;
50 }
51 for (auto color : colors_) {
52 if (!color.IsOpaque()) {
53 return false;
54 }
55 }
56 return true;
57}
58
59bool LinearGradientContents::CanApplyFastGradient() const {
60 if (!GetInverseEffectTransform().IsIdentity()) {
61 return false;
62 }
63 std::optional<Rect> maybe_rect = GetGeometry()->GetCoverage(Matrix());
64 if (!maybe_rect.has_value()) {
65 return false;
66 }
67 Rect rect = maybe_rect.value();
68
69 if (ScalarNearlyEqual(start_point_.x, end_point_.x)) {
70 // Sort start and end to make on-rect comparisons easier.
71 Point start = (start_point_.y < end_point_.y) ? start_point_ : end_point_;
72 Point end = (start_point_.y < end_point_.y) ? end_point_ : start_point_;
73 // The exact x positon doesn't matter for a vertical gradient, but the y
74 // position must be nearly on the rectangle.
75 if (ScalarNearlyEqual(start.y, rect.GetTop()) &&
76 ScalarNearlyEqual(end.y, rect.GetBottom())) {
77 return true;
78 }
79 return false;
80 }
81
82 if (ScalarNearlyEqual(start_point_.y, end_point_.y)) {
83 // Sort start and end to make on-rect comparisons easier.
84 Point start = (start_point_.x < end_point_.x) ? start_point_ : end_point_;
85 Point end = (start_point_.x < end_point_.x) ? end_point_ : start_point_;
86 // The exact y positon doesn't matter for a horizontal gradient, but the x
87 // position must be nearly on the rectangle.
88 if (ScalarNearlyEqual(start.x, rect.GetLeft()) &&
89 ScalarNearlyEqual(end.x, rect.GetRight())) {
90 return true;
91 }
92 return false;
93 }
94
95 return false;
96}
97
98// A much faster (in terms of ALU) linear gradient that uses vertex
99// interpolation to perform all color computation. Requires that the geometry of
100// the gradient is divided into regions based on the stop values.
101// Currently restricted to rect geometry where the start and end points are
102// perfectly horizontal/vertical, but could easily be expanded to StC cases
103// provided that the start/end are on or outside of the coverage rect.
104bool LinearGradientContents::FastLinearGradient(const ContentContext& renderer,
105 const Entity& entity,
106 RenderPass& pass) const {
109
110 Geometry& geometry = *GetGeometry();
111 bool force_stencil = !geometry.IsAxisAlignedRect();
112
113 auto geom_callback = [&](const ContentContext& renderer, const Entity& entity,
114 RenderPass& pass,
115 const Geometry& geometry) -> GeometryResult {
116 // We already know this is an axis aligned rectangle, so the coverage will
117 // be approximately the same as the geometry. For non axis-algined
118 // rectangles, we can force stencil then cover (not done here). We give an
119 // identity transform to avoid double transforming the gradient.
120 std::optional<Rect> maybe_rect = geometry.GetCoverage(Matrix());
121 if (!maybe_rect.has_value()) {
122 return {};
123 }
124 Rect rect = maybe_rect.value();
125 bool horizontal_axis = start_point_.y == end_point_.y;
126
127 // Compute the locations of each breakpoint along the primary axis, then
128 // create a rectangle that joins each segment. There will be two triangles
129 // between each pair of points.
130 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
131 vtx_builder.Reserve(6 * (stops_.size() - 1));
132 Point prev = start_point_;
133 for (auto i = 1u; i < stops_.size(); i++) {
134 Scalar t = stops_[i];
135 Point current = (1.0 - t) * start_point_ + t * end_point_;
136 Rect section = horizontal_axis
137 ? Rect::MakeXYWH(prev.x, rect.GetY(),
138 current.x - prev.x, rect.GetHeight())
139
140 : Rect::MakeXYWH(rect.GetX(), prev.y, rect.GetWidth(),
141 current.y - prev.y);
142 vtx_builder.AddVertices({
143 {section.GetLeftTop(), colors_[i - 1]},
144 {section.GetRightTop(),
145 horizontal_axis ? colors_[i] : colors_[i - 1]},
146 {section.GetLeftBottom(),
147 horizontal_axis ? colors_[i - 1] : colors_[i]},
148 {section.GetRightTop(),
149 horizontal_axis ? colors_[i] : colors_[i - 1]},
150 {section.GetLeftBottom(),
151 horizontal_axis ? colors_[i - 1] : colors_[i]},
152 {section.GetRightBottom(), colors_[i]},
153 });
154 prev = current;
155 }
156 return GeometryResult{
158 .vertex_buffer =
159 vtx_builder.CreateVertexBuffer(renderer.GetTransientsBuffer()),
160 .transform = entity.GetShaderTransform(pass),
161 };
162 };
163
164 pass.SetLabel("LinearGradient");
165
166 VS::FrameInfo frame_info;
167
168 PipelineBuilderCallback pipeline_callback =
169 [&renderer](ContentContextOptions options) {
170 return renderer.GetFastGradientPipeline(options);
171 };
172 return ColorSourceContents::DrawGeometry<VS>(
173 renderer, entity, pass, pipeline_callback, frame_info,
174 [this, &renderer, &entity](RenderPass& pass) {
175 auto& host_buffer = renderer.GetTransientsBuffer();
176
177 FS::FragInfo frag_info;
178 frag_info.alpha =
179 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
180
181 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
182
183 return true;
184 },
185 /*force_stencil=*/force_stencil, geom_callback);
186}
187
189 const Entity& entity,
190 RenderPass& pass) const {
191 // TODO(148651): The fast path is overly restrictive, following the design in
192 // https://github.com/flutter/flutter/issues/148651 support for more cases can
193 // be gradually added.
194 if (CanApplyFastGradient()) {
195 return FastLinearGradient(renderer, entity, pass);
196 }
197 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
198 return RenderSSBO(renderer, entity, pass);
199 }
200 return RenderTexture(renderer, entity, pass);
201}
202
203bool LinearGradientContents::RenderTexture(const ContentContext& renderer,
204 const Entity& entity,
205 RenderPass& pass) const {
208
209 VS::FrameInfo frame_info;
210 frame_info.matrix = GetInverseEffectTransform();
211
212 PipelineBuilderCallback pipeline_callback =
214 return renderer.GetLinearGradientFillPipeline(options);
215 };
216 return ColorSourceContents::DrawGeometry<VS>(
217 renderer, entity, pass, pipeline_callback, frame_info,
218 [this, &renderer, &entity](RenderPass& pass) {
219 auto gradient_data = CreateGradientBuffer(colors_, stops_);
220 auto gradient_texture =
221 CreateGradientTexture(gradient_data, renderer.GetContext());
222 if (gradient_texture == nullptr) {
223 return false;
224 }
225
226 FS::FragInfo frag_info;
227 frag_info.start_point = start_point_;
228 frag_info.end_point = end_point_;
229 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
230 frag_info.decal_border_color = decal_border_color_;
231 frag_info.texture_sampler_y_coord_scale =
232 gradient_texture->GetYCoordScale();
233 frag_info.alpha =
234 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
235 ;
236 frag_info.half_texel =
237 Vector2(0.5 / gradient_texture->GetSize().width,
238 0.5 / gradient_texture->GetSize().height);
239
240 pass.SetCommandLabel("LinearGradientFill");
241
242 SamplerDescriptor sampler_desc;
243 sampler_desc.min_filter = MinMagFilter::kLinear;
244 sampler_desc.mag_filter = MinMagFilter::kLinear;
245
246 FS::BindTextureSampler(
247 pass, std::move(gradient_texture),
248 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
249 sampler_desc));
250 FS::BindFragInfo(
251 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
252 return true;
253 });
254}
255
256namespace {
257Scalar CalculateInverseDotStartToEnd(Point start_point, Point end_point) {
258 Point start_to_end = end_point - start_point;
259 Scalar dot =
260 (start_to_end.x * start_to_end.x + start_to_end.y * start_to_end.y);
261 return dot == 0.0f ? 0.0f : 1.0f / dot;
262}
263} // namespace
264
265bool LinearGradientContents::RenderSSBO(const ContentContext& renderer,
266 const Entity& entity,
267 RenderPass& pass) const {
270
271 VS::FrameInfo frame_info;
272 frame_info.matrix = GetInverseEffectTransform();
273
274 PipelineBuilderCallback pipeline_callback =
275 [&renderer](ContentContextOptions options) {
276 return renderer.GetLinearGradientSSBOFillPipeline(options);
277 };
278 return ColorSourceContents::DrawGeometry<VS>(
279 renderer, entity, pass, pipeline_callback, frame_info,
280 [this, &renderer, &entity](RenderPass& pass) {
281 FS::FragInfo frag_info;
282 frag_info.start_point = start_point_;
283 frag_info.end_point = end_point_;
284 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
285 frag_info.decal_border_color = decal_border_color_;
286 frag_info.alpha =
287 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
288 frag_info.start_to_end = end_point_ - start_point_;
289 frag_info.inverse_dot_start_to_end =
290 CalculateInverseDotStartToEnd(start_point_, end_point_);
291
292 auto& host_buffer = renderer.GetTransientsBuffer();
293 auto colors = CreateGradientColors(colors_, stops_);
294
295 frag_info.colors_length = colors.size();
296 auto color_buffer =
297 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
299
300 pass.SetCommandLabel("LinearGradientSSBOFill");
301
302 FS::BindFragInfo(
303 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
304 FS::BindColorData(pass, color_buffer);
305
306 return true;
307 });
308}
309
311 const ColorFilterProc& color_filter_proc) {
312 for (Color& color : colors_) {
313 color = color_filter_proc(color);
314 }
315 decal_border_color_ = color_filter_proc(decal_border_color_);
316 return true;
317}
318
319} // namespace impeller
const char * options
static float prev(float f)
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
const std::vector< Color > & GetColors() const
void SetTileMode(Entity::TileMode tile_mode)
const std::vector< Scalar > & GetStops() const
void SetColors(std::vector< Color > colors)
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetEndPoints(Point start_point, Point end_point)
void SetStops(std::vector< Scalar > stops)
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
FragmentShader_ FragmentShader
Definition: pipeline.h:107
DlColor color
glong glong end
double y
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
Point Vector2
Definition: point.h:326
float Scalar
Definition: scalar.h:18
TRect< Scalar > Rect
Definition: rect.h:769
SolidFillVertexShader VS
TPoint< Scalar > Point
Definition: point.h:322
std::vector< StopData > CreateGradientColors(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the color and stop data for a gradient.
std::shared_ptr< Texture > CreateGradientTexture(const GradientData &gradient_data, const std::shared_ptr< impeller::Context > &context)
Create a host visible texture that contains the gradient defined by the provided gradient data.
GradientData CreateGradientBuffer(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the interpolated color bytes for the linear gradient described by colors and s...
Definition: gradient.cc:20
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:30
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
SK_API sk_sp< PrecompileColorFilter > Matrix()
SINT T dot(const Vec< N, T > &a, const Vec< N, T > &b)
Definition: SkVx.h:964
Scalar alpha
Definition: color.h:143
static constexpr TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136