Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
radial_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
14
15namespace impeller {
16
18 : geometry_(geometry) {}
19
21
23 return geometry_;
24}
25
27 center_ = center;
28 radius_ = radius;
29}
30
32 tile_mode_ = tile_mode;
33}
34
35void RadialGradientContents::SetColors(std::vector<Color> colors) {
36 colors_ = std::move(colors);
37}
38
39void RadialGradientContents::SetStops(std::vector<Scalar> stops) {
40 stops_ = std::move(stops);
41}
42
43const std::vector<Color>& RadialGradientContents::GetColors() const {
44 return colors_;
45}
46
47const std::vector<Scalar>& RadialGradientContents::GetStops() const {
48 return stops_;
49}
50
52 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
53 return false;
54 }
55 for (auto color : colors_) {
56 if (!color.IsOpaque()) {
57 return false;
58 }
59 }
61}
62
63#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
64#define UNIFORM_COLORS_INFO(t) \
65 t##GradientUniformFillPipeline::FragmentShader::ColorsInfo
66#define UNIFORM_STOP_PAIRS_INFO(t) \
67 t##GradientUniformFillPipeline::FragmentShader::StopPairsInfo
68#define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_COLORS_INFO(Radial)::colors)
69#define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_STOP_PAIRS_INFO(Radial)::stop_pairs)
72static_assert(sizeof(UNIFORM_COLORS_INFO(Radial)) ==
73 sizeof(UNIFORM_COLORS_INFO(Radial)::colors));
74static_assert(sizeof(UNIFORM_STOP_PAIRS_INFO(Radial)) ==
75 sizeof(UNIFORM_STOP_PAIRS_INFO(Radial)::stop_pairs));
76
78 const Entity& entity,
79 RenderPass& pass) const {
80 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
81 return RenderSSBO(renderer, entity, pass);
82 }
83 if (colors_.size() <= kMaxUniformGradientStops &&
84 stops_.size() <= kMaxUniformGradientStops) {
85 return RenderUniform(renderer, entity, pass);
86 }
87 return RenderTexture(renderer, entity, pass);
88}
89
90bool RadialGradientContents::RenderSSBO(const ContentContext& renderer,
91 const Entity& entity,
92 RenderPass& pass) const {
93 using VS = RadialGradientSSBOFillPipeline::VertexShader;
94 using FS = RadialGradientSSBOFillPipeline::FragmentShader;
95
96 VS::FrameInfo frame_info;
97 frame_info.matrix = GetInverseEffectTransform();
98
99 PipelineBuilderCallback pipeline_callback =
100 [&renderer](ContentContextOptions options) {
101 return renderer.GetRadialGradientSSBOFillPipeline(options);
102 };
103 return ColorSourceContents::DrawGeometry<VS>(
104 renderer, entity, pass, pipeline_callback, frame_info,
105 [this, &renderer, &entity](RenderPass& pass) {
106 FS::FragInfo frag_info;
107 frag_info.center = center_;
108 frag_info.radius = radius_;
109 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
110 frag_info.decal_border_color = decal_border_color_;
111 frag_info.alpha =
114
115 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
116 auto colors = CreateGradientColors(colors_, stops_);
117
118 frag_info.colors_length = colors.size();
119 auto color_buffer = data_host_buffer.Emplace(
120 colors.data(), colors.size() * sizeof(StopData),
121 renderer.GetDeviceCapabilities()
123
124 pass.SetCommandLabel("RadialGradientSSBOFill");
125 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
126 FS::BindColorData(pass, color_buffer);
127
128 return true;
129 });
130}
131
132bool RadialGradientContents::RenderUniform(const ContentContext& renderer,
133 const Entity& entity,
134 RenderPass& pass) const {
135 using VS = RadialGradientUniformFillPipeline::VertexShader;
136 using FS = RadialGradientUniformFillPipeline::FragmentShader;
137
138 VS::FrameInfo frame_info;
139 frame_info.matrix = GetInverseEffectTransform();
140
141 PipelineBuilderCallback pipeline_callback =
142 [&renderer](ContentContextOptions options) {
143 return renderer.GetRadialGradientUniformFillPipeline(options);
144 };
145 return ColorSourceContents::DrawGeometry<VS>(
146 renderer, entity, pass, pipeline_callback, frame_info,
147 [this, &renderer, &entity](RenderPass& pass) {
148 FS::FragInfo frag_info;
149 FS::ColorsInfo colors_info;
150 FS::StopPairsInfo stop_pairs_info;
151
152 frag_info.center = center_;
153 frag_info.radius = radius_;
154 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
155 frag_info.alpha =
157 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
158 frag_info.colors_length = PopulateUniformGradientColors(
159 colors_, stops_, colors_info.colors, stop_pairs_info.stop_pairs);
160 frag_info.decal_border_color = decal_border_color_;
161
162 pass.SetCommandLabel("RadialGradientUniformFill");
163
164 auto& transients_buffer = renderer.GetTransientsDataBuffer();
165 FS::BindFragInfo(pass, transients_buffer.EmplaceUniform(frag_info));
166 FS::BindColorsInfo(pass, transients_buffer.EmplaceUniform(colors_info));
167 FS::BindStopPairsInfo(
168 pass, transients_buffer.EmplaceUniform(stop_pairs_info));
169
170 return true;
171 });
172}
173
174bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
175 const Entity& entity,
176 RenderPass& pass) const {
177 using VS = RadialGradientFillPipeline::VertexShader;
178 using FS = RadialGradientFillPipeline::FragmentShader;
179
180 auto gradient_data = CreateGradientBuffer(colors_, stops_);
181 auto gradient_texture =
182 CreateGradientTexture(gradient_data, renderer.GetContext());
183 if (gradient_texture == nullptr) {
184 return false;
185 }
186
187 VS::FrameInfo frame_info;
188 frame_info.matrix = GetInverseEffectTransform();
189
190 PipelineBuilderCallback pipeline_callback =
191 [&renderer](ContentContextOptions options) {
192 return renderer.GetRadialGradientFillPipeline(options);
193 };
194 return ColorSourceContents::DrawGeometry<VS>(
195 renderer, entity, pass, pipeline_callback, frame_info,
196 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
197 FS::FragInfo frag_info;
198 frag_info.center = center_;
199 frag_info.radius = radius_;
200 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
201 frag_info.decal_border_color = decal_border_color_;
202 frag_info.alpha =
204 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
205 frag_info.half_texel =
206 Vector2(0.5 / gradient_texture->GetSize().width,
207 0.5 / gradient_texture->GetSize().height);
208
209 SamplerDescriptor sampler_desc;
210 sampler_desc.min_filter = MinMagFilter::kLinear;
211 sampler_desc.mag_filter = MinMagFilter::kLinear;
212
213 pass.SetCommandLabel("RadialGradientFill");
214
215 FS::BindFragInfo(
216 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
217 FS::BindTextureSampler(
218 pass, gradient_texture,
219 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
220 sampler_desc));
221
222 return true;
223 });
224}
225
227 const ColorFilterProc& color_filter_proc) {
228 for (Color& color : colors_) {
229 color = color_filter_proc(color);
230 }
231 decal_border_color_ = color_filter_proc(decal_border_color_);
232 return true;
233}
234
235} // namespace impeller
virtual size_t GetMinimumStorageBufferAlignment() const
The minimum alignment of storage buffer value offsets in bytes.
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
const Capabilities & GetDeviceCapabilities() const
PipelineRef GetRadialGradientSSBOFillPipeline(ContentContextOptions opts) const
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition entity.cc:46
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition geometry.h:135
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
void SetTileMode(Entity::TileMode tile_mode)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetCenterAndRadius(Point center, Scalar radius)
const Geometry * GetGeometry() const override
Get the geometry that this contents will use to render.
RadialGradientContents(const Geometry *geometry)
void SetStops(std::vector< Scalar > stops)
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
void SetColors(std::vector< Color > colors)
const std::vector< Scalar > & GetStops() const
const std::vector< Color > & GetColors() const
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:30
#define UNIFORM_STOP_PAIRS_INFO(t)
#define UNIFORM_STOP_SIZE
#define UNIFORM_COLORS_INFO(t)
#define UNIFORM_COLOR_SIZE
Point Vector2
Definition point.h:430
float Scalar
Definition scalar.h:19
LinePipeline::FragmentShader FS
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.
std::function< Color(Color)> ColorFilterProc
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.
LinePipeline::VertexShader VS
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
static constexpr uint32_t kMaxUniformGradientStops
Scalar alpha
Definition color.h:143
A 4x4 matrix using column-major storage.
Definition matrix.h:37