| Flutter Impeller |

conical_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
5 #include "conical_gradient_contents.h"
6
7 #include "impeller/entity/contents/clip_contents.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/contents/gradient_generator.h"
10 #include "impeller/entity/entity.h"
11 #include "impeller/entity/geometry/geometry.h"
12 #include "impeller/geometry/gradient.h"
13 #include "impeller/renderer/render_pass.h"
14
15 namespace impeller {
16
17 namespace {
18 ConicalKind GetConicalKind(Point center,
19 Scalar radius,
20 std::optional<Point> focus,
21 Scalar focus_radius) {
22 ConicalKind kind = ConicalKind::kConical;
23 if (!focus.has_value() ||
24 center.GetDistance(focus.value()) < kEhCloseEnough) {
25 kind = ConicalKind::kRadial;
26 }
27 if (focus.has_value() && std::fabsf(radius - focus_radius) < kEhCloseEnough) {
28 if (kind == ConicalKind::kRadial) {
29 kind = ConicalKind::kStripAndRadial;
30 } else {
31 kind = ConicalKind::kStrip;
32 }
33 }
34 return kind;
35 }
36
37 } // namespace
38
39 ConicalGradientContents::ConicalGradientContents() = default;
40
41 ConicalGradientContents::~ConicalGradientContents() = default;
42
43 void ConicalGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
44 center_ = center;
45 radius_ = radius;
46 }
47
48 void ConicalGradientContents::SetTileMode(Entity::TileMode tile_mode) {
49 tile_mode_ = tile_mode;
50 }
51
52 void ConicalGradientContents::SetColors(std::vector<Color> colors) {
53 colors_ = std::move(colors);
54 }
55
56 void ConicalGradientContents::SetStops(std::vector<Scalar> stops) {
57 stops_ = std::move(stops);
58 }
59
60 const std::vector<Color>& ConicalGradientContents::GetColors() const {
61 return colors_;
62 }
63
64 const std::vector<Scalar>& ConicalGradientContents::GetStops() const {
65 return stops_;
66 }
67
68 void ConicalGradientContents::SetFocus(std::optional<Point> focus,
69 Scalar radius) {
70 focus_ = focus;
71 focus_radius_ = radius;
72 }
73
74 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
75 #define UNIFORM_FRAG_INFO(t) \
76 t##GradientUniformFillConicalPipeline::FragmentShader::FragInfo
77 #define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Conical)::colors)
78 #define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Conical)::stop_pairs)
79 static_assert(UNIFORM_COLOR_SIZE == kMaxUniformGradientStops);
80 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
81
82 bool ConicalGradientContents::Render(const ContentContext& renderer,
83 const Entity& entity,
84 RenderPass& pass) const {
85 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
86 return RenderSSBO(renderer, entity, pass);
87 }
88 if (colors_.size() <= kMaxUniformGradientStops &&
89 stops_.size() <= kMaxUniformGradientStops) {
90 return RenderUniform(renderer, entity, pass);
91 }
92 return RenderTexture(renderer, entity, pass);
93 }
94
95 bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
96 const Entity& entity,
97 RenderPass& pass) const {
98 using VS = ConicalGradientSSBOFillPipeline::VertexShader;
99 using FS = ConicalGradientSSBOFillPipeline::FragmentShader;
100
101 VS::FrameInfo frame_info;
102 frame_info.matrix = GetInverseEffectTransform();
103
104 ConicalKind kind = GetConicalKind(center_, radius_, focus_, focus_radius_);
105 PipelineBuilderCallback pipeline_callback =
106 [&renderer, kind](ContentContextOptions options) {
107 return renderer.GetConicalGradientSSBOFillPipeline(options, kind);
108 };
109 return ColorSourceContents::DrawGeometry<VS>(
110 renderer, entity, pass, pipeline_callback, frame_info,
111 [this, &renderer, &entity](RenderPass& pass) {
112 FS::FragInfo frag_info;
113 frag_info.center = center_;
114 frag_info.radius = radius_;
115 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
116 frag_info.decal_border_color = decal_border_color_;
117 frag_info.alpha =
118 GetOpacityFactor() *
119 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
120 if (focus_) {
121 frag_info.focus = focus_.value();
122 frag_info.focus_radius = focus_radius_;
123 } else {
124 frag_info.focus = center_;
125 frag_info.focus_radius = 0.0;
126 }
127
128 auto& host_buffer = renderer.GetTransientsBuffer();
129 auto colors = CreateGradientColors(colors_, stops_);
130
131 frag_info.colors_length = colors.size();
132 auto color_buffer =
133 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
134 host_buffer.GetMinimumUniformAlignment());
135
136 FS::BindFragInfo(
137 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
138 FS::BindColorData(pass, color_buffer);
139
140 pass.SetCommandLabel("ConicalGradientSSBOFill");
141 return true;
142 });
143 }
144
145 bool ConicalGradientContents::RenderUniform(const ContentContext& renderer,
146 const Entity& entity,
147 RenderPass& pass) const {
148 using VS = ConicalGradientUniformFillConicalPipeline::VertexShader;
149 using FS = ConicalGradientUniformFillConicalPipeline::FragmentShader;
150
151 VS::FrameInfo frame_info;
152 frame_info.matrix = GetInverseEffectTransform();
153
154 ConicalKind kind = GetConicalKind(center_, radius_, focus_, focus_radius_);
155 PipelineBuilderCallback pipeline_callback =
156 [&renderer, kind](ContentContextOptions options) {
157 return renderer.GetConicalGradientUniformFillPipeline(options, kind);
158 };
159 return ColorSourceContents::DrawGeometry<VS>(
160 renderer, entity, pass, pipeline_callback, frame_info,
161 [this, &renderer, &entity](RenderPass& pass) {
162 FS::FragInfo frag_info;
163 frag_info.center = center_;
164 if (focus_) {
165 frag_info.focus = focus_.value();
166 frag_info.focus_radius = focus_radius_;
167 } else {
168 frag_info.focus = center_;
169 frag_info.focus_radius = 0.0;
170 }
171 frag_info.radius = radius_;
172 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
173 frag_info.alpha =
174 GetOpacityFactor() *
175 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
176 frag_info.colors_length = PopulateUniformGradientColors(
177 colors_, stops_, frag_info.colors, frag_info.stop_pairs);
178 frag_info.decal_border_color = decal_border_color_;
179
180 pass.SetCommandLabel("ConicalGradientUniformFill");
181
182 FS::BindFragInfo(
183 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
184
185 return true;
186 });
187 }
188
189 bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
190 const Entity& entity,
191 RenderPass& pass) const {
192 using VS = ConicalGradientFillConicalPipeline::VertexShader;
193 using FS = ConicalGradientFillConicalPipeline::FragmentShader;
194
195 auto gradient_data = CreateGradientBuffer(colors_, stops_);
196 auto gradient_texture =
197 CreateGradientTexture(gradient_data, renderer.GetContext());
198 if (gradient_texture == nullptr) {
199 return false;
200 }
201
202 VS::FrameInfo frame_info;
203 frame_info.matrix = GetInverseEffectTransform();
204
205 ConicalKind kind = GetConicalKind(center_, radius_, focus_, focus_radius_);
206 PipelineBuilderCallback pipeline_callback =
207 [&renderer, kind](ContentContextOptions options) {
208 return renderer.GetConicalGradientFillPipeline(options, kind);
209 };
210 return ColorSourceContents::DrawGeometry<VS>(
211 renderer, entity, pass, pipeline_callback, frame_info,
212 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
213 FS::FragInfo frag_info;
214 frag_info.center = center_;
215 frag_info.radius = radius_;
216 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
217 frag_info.decal_border_color = decal_border_color_;
218 frag_info.texture_sampler_y_coord_scale =
219 gradient_texture->GetYCoordScale();
220 frag_info.alpha =
221 GetOpacityFactor() *
222 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
223 frag_info.half_texel =
224 Vector2(0.5 / gradient_texture->GetSize().width,
225 0.5 / gradient_texture->GetSize().height);
226 if (focus_) {
227 frag_info.focus = focus_.value();
228 frag_info.focus_radius = focus_radius_;
229 } else {
230 frag_info.focus = center_;
231 frag_info.focus_radius = 0.0;
232 }
233
234 pass.SetCommandLabel("ConicalGradientFill");
235
236 FS::BindFragInfo(
237 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
238 SamplerDescriptor sampler_desc;
239 sampler_desc.min_filter = MinMagFilter::kLinear;
240 sampler_desc.mag_filter = MinMagFilter::kLinear;
241 FS::BindTextureSampler(
242 pass, gradient_texture,
243 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
244 sampler_desc));
245
246 return true;
247 });
248 }
249
250 bool ConicalGradientContents::ApplyColorFilter(
251 const ColorFilterProc& color_filter_proc) {
252 for (Color& color : colors_) {
253 color = color_filter_proc(color);
254 }
255 decal_border_color_ = color_filter_proc(decal_border_color_);
256 return true;
257 }
258
259 } // namespace impeller
impeller::Capabilities::SupportsSSBO
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:36
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:42
impeller::ConicalGradientContents::SetFocus
void SetFocus(std::optional< Point > focus, Scalar radius)
Definition: conical_gradient_contents.cc:68
impeller::ConicalGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: conical_gradient_contents.cc:250
impeller::ConicalGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: conical_gradient_contents.cc:56
impeller::ConicalGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: conical_gradient_contents.cc:43
impeller::ConicalGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: conical_gradient_contents.cc:82
impeller::ConicalGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: conical_gradient_contents.cc:64
impeller::ConicalGradientContents::ConicalGradientContents
ConicalGradientContents()
impeller::ConicalGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: conical_gradient_contents.cc:48
impeller::ConicalGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: conical_gradient_contents.cc:60
impeller::ConicalGradientContents::~ConicalGradientContents
~ConicalGradientContents() override
impeller::ConicalGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: conical_gradient_contents.cc:52
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:297
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:943
impeller::ContentContext::GetConicalGradientSSBOFillPipeline
PipelineRef GetConicalGradientSSBOFillPipeline(ContentContextOptions opts, ConicalKind kind) const
Definition: content_context.cc:1042
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::Entity
Definition: entity.h:26
impeller::Entity::TileMode
TileMode
Definition: entity.h:48
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:47
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:163
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:164
clip_contents.h
UNIFORM_STOP_SIZE
#define UNIFORM_STOP_SIZE
Definition: conical_gradient_contents.cc:78
UNIFORM_COLOR_SIZE
#define UNIFORM_COLOR_SIZE
Definition: conical_gradient_contents.cc:77
conical_gradient_contents.h
content_context.h
entity.h
geometry.h
gradient.h
gradient_generator.h
impeller
Definition: allocation.cc:12
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:57
impeller::ConicalKind
ConicalKind
Definition: content_context.h:128
impeller::kStripAndRadial
@ kStripAndRadial
Definition: content_context.h:132
impeller::kStrip
@ kStrip
Definition: content_context.h:131
impeller::kConical
@ kConical
Definition: content_context.h:129
impeller::kRadial
@ kRadial
Definition: content_context.h:130
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::FS
LinePipeline::FragmentShader FS
Definition: line_contents.cc:14
impeller::PopulateUniformGradientColors
int PopulateUniformGradientColors(const std::vector< Color > &colors, const std::vector< Scalar > &stops, Vector4 frag_info_colors[kMaxUniformGradientStops], Vector4 frag_info_stop_pairs[kMaxUniformGradientStops/2])
Populate 2 arrays with the colors and stop data for a gradient.
Definition: gradient_generator.cc:51
impeller::CreateGradientColors
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.
Definition: gradient_generator.cc:34
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
impeller::CreateGradientTexture
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.
Definition: gradient_generator.cc:17
impeller::CreateGradientBuffer
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
impeller::MinMagFilter::kLinear
@ kLinear
impeller::kMaxUniformGradientStops
static constexpr uint32_t kMaxUniformGradientStops
Definition: gradient_generator.h:47
render_pass.h
impeller::Color
Definition: color.h:124
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::ContentContextOptions
Definition: content_context.h:40
impeller::TPoint< Scalar >