| 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 ConicalGradientContents::ConicalGradientContents() = default;
18
19 ConicalGradientContents::~ConicalGradientContents() = default;
20
21 void ConicalGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
22 center_ = center;
23 radius_ = radius;
24 }
25
26 void ConicalGradientContents::SetTileMode(Entity::TileMode tile_mode) {
27 tile_mode_ = tile_mode;
28 }
29
30 void ConicalGradientContents::SetColors(std::vector<Color> colors) {
31 colors_ = std::move(colors);
32 }
33
34 void ConicalGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36 }
37
38 const std::vector<Color>& ConicalGradientContents::GetColors() const {
39 return colors_;
40 }
41
42 const std::vector<Scalar>& ConicalGradientContents::GetStops() const {
43 return stops_;
44 }
45
46 void ConicalGradientContents::SetFocus(std::optional<Point> focus,
47 Scalar radius) {
48 focus_ = focus;
49 focus_radius_ = radius;
50 }
51
52 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
53 #define UNIFORM_FRAG_INFO(t) \
54 t##GradientUniformFillPipeline::FragmentShader::FragInfo
55 #define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Conical)::colors)
56 #define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Conical)::stop_pairs)
57 static_assert(UNIFORM_COLOR_SIZE == kMaxUniformGradientStops);
58 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
59
60 bool ConicalGradientContents::Render(const ContentContext& renderer,
61 const Entity& entity,
62 RenderPass& pass) const {
63 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
64 return RenderSSBO(renderer, entity, pass);
65 }
66 if (colors_.size() <= kMaxUniformGradientStops &&
67 stops_.size() <= kMaxUniformGradientStops) {
68 return RenderUniform(renderer, entity, pass);
69 }
70 return RenderTexture(renderer, entity, pass);
71 }
72
73 bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
74 const Entity& entity,
75 RenderPass& pass) const {
76 using VS = ConicalGradientSSBOFillPipeline::VertexShader;
77 using FS = ConicalGradientSSBOFillPipeline::FragmentShader;
78
79 VS::FrameInfo frame_info;
80 frame_info.matrix = GetInverseEffectTransform();
81
82 PipelineBuilderCallback pipeline_callback =
83 [&renderer](ContentContextOptions options) {
84 return renderer.GetConicalGradientSSBOFillPipeline(options);
85 };
86 return ColorSourceContents::DrawGeometry<VS>(
87 renderer, entity, pass, pipeline_callback, frame_info,
88 [this, &renderer, &entity](RenderPass& pass) {
89 FS::FragInfo frag_info;
90 frag_info.center = center_;
91 frag_info.radius = radius_;
92 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
93 frag_info.decal_border_color = decal_border_color_;
94 frag_info.alpha =
95 GetOpacityFactor() *
96 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
97 if (focus_) {
98 frag_info.focus = focus_.value();
99 frag_info.focus_radius = focus_radius_;
100 } else {
101 frag_info.focus = center_;
102 frag_info.focus_radius = 0.0;
103 }
104
105 auto& host_buffer = renderer.GetTransientsBuffer();
106 auto colors = CreateGradientColors(colors_, stops_);
107
108 frag_info.colors_length = colors.size();
109 auto color_buffer =
110 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
111 DefaultUniformAlignment());
112
113 FS::BindFragInfo(
114 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
115 FS::BindColorData(pass, color_buffer);
116
117 pass.SetCommandLabel("ConicalGradientSSBOFill");
118 return true;
119 });
120 }
121
122 bool ConicalGradientContents::RenderUniform(const ContentContext& renderer,
123 const Entity& entity,
124 RenderPass& pass) const {
125 using VS = ConicalGradientUniformFillPipeline::VertexShader;
126 using FS = ConicalGradientUniformFillPipeline::FragmentShader;
127
128 VS::FrameInfo frame_info;
129 frame_info.matrix = GetInverseEffectTransform();
130
131 PipelineBuilderCallback pipeline_callback =
132 [&renderer](ContentContextOptions options) {
133 return renderer.GetConicalGradientUniformFillPipeline(options);
134 };
135 return ColorSourceContents::DrawGeometry<VS>(
136 renderer, entity, pass, pipeline_callback, frame_info,
137 [this, &renderer, &entity](RenderPass& pass) {
138 FS::FragInfo frag_info;
139 frag_info.center = center_;
140 if (focus_) {
141 frag_info.focus = focus_.value();
142 frag_info.focus_radius = focus_radius_;
143 } else {
144 frag_info.focus = center_;
145 frag_info.focus_radius = 0.0;
146 }
147 frag_info.radius = radius_;
148 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
149 frag_info.alpha =
150 GetOpacityFactor() *
151 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
152 frag_info.colors_length = PopulateUniformGradientColors(
153 colors_, stops_, frag_info.colors, frag_info.stop_pairs);
154 frag_info.decal_border_color = decal_border_color_;
155
156 pass.SetCommandLabel("ConicalGradientUniformFill");
157
158 FS::BindFragInfo(
159 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
160
161 return true;
162 });
163 }
164
165 bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
166 const Entity& entity,
167 RenderPass& pass) const {
168 using VS = ConicalGradientFillPipeline::VertexShader;
169 using FS = ConicalGradientFillPipeline::FragmentShader;
170
171 auto gradient_data = CreateGradientBuffer(colors_, stops_);
172 auto gradient_texture =
173 CreateGradientTexture(gradient_data, renderer.GetContext());
174 if (gradient_texture == nullptr) {
175 return false;
176 }
177
178 VS::FrameInfo frame_info;
179 frame_info.matrix = GetInverseEffectTransform();
180
181 PipelineBuilderCallback pipeline_callback =
182 [&renderer](ContentContextOptions options) {
183 return renderer.GetConicalGradientFillPipeline(options);
184 };
185 return ColorSourceContents::DrawGeometry<VS>(
186 renderer, entity, pass, pipeline_callback, frame_info,
187 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
188 FS::FragInfo frag_info;
189 frag_info.center = center_;
190 frag_info.radius = radius_;
191 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
192 frag_info.decal_border_color = decal_border_color_;
193 frag_info.texture_sampler_y_coord_scale =
194 gradient_texture->GetYCoordScale();
195 frag_info.alpha =
196 GetOpacityFactor() *
197 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
198 frag_info.half_texel =
199 Vector2(0.5 / gradient_texture->GetSize().width,
200 0.5 / gradient_texture->GetSize().height);
201 if (focus_) {
202 frag_info.focus = focus_.value();
203 frag_info.focus_radius = focus_radius_;
204 } else {
205 frag_info.focus = center_;
206 frag_info.focus_radius = 0.0;
207 }
208
209 pass.SetCommandLabel("ConicalGradientFill");
210
211 FS::BindFragInfo(
212 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
213 SamplerDescriptor sampler_desc;
214 sampler_desc.min_filter = MinMagFilter::kLinear;
215 sampler_desc.mag_filter = MinMagFilter::kLinear;
216 FS::BindTextureSampler(
217 pass, gradient_texture,
218 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
219 sampler_desc));
220
221 return true;
222 });
223 }
224
225 bool ConicalGradientContents::ApplyColorFilter(
226 const ColorFilterProc& color_filter_proc) {
227 for (Color& color : colors_) {
228 color = color_filter_proc(color);
229 }
230 decal_border_color_ = color_filter_proc(decal_border_color_);
231 return true;
232 }
233
234 } // 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:105
impeller::ConicalGradientContents::SetFocus
void SetFocus(std::optional< Point > focus, Scalar radius)
Definition: conical_gradient_contents.cc:46
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:225
impeller::ConicalGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: conical_gradient_contents.cc:34
impeller::ConicalGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: conical_gradient_contents.cc:21
impeller::ConicalGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: conical_gradient_contents.cc:60
impeller::ConicalGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: conical_gradient_contents.cc:42
impeller::ConicalGradientContents::ConicalGradientContents
ConicalGradientContents()
impeller::ConicalGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: conical_gradient_contents.cc:26
impeller::ConicalGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: conical_gradient_contents.cc:38
impeller::ConicalGradientContents::~ConicalGradientContents
~ConicalGradientContents() override
impeller::ConicalGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: conical_gradient_contents.cc:30
impeller::ContentContext
Definition: content_context.h:371
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:866
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:617
impeller::ContentContext::GetConicalGradientSSBOFillPipeline
PipelineRef GetConicalGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:424
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::Entity
Definition: entity.h:20
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
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:115
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:48
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:113
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:114
clip_contents.h
UNIFORM_STOP_SIZE
#define UNIFORM_STOP_SIZE
Definition: conical_gradient_contents.cc:56
UNIFORM_COLOR_SIZE
#define UNIFORM_COLOR_SIZE
Definition: conical_gradient_contents.cc:55
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:18
impeller::VS
GlyphAtlasPipeline::VertexShader VS
Definition: text_contents.cc:25
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:70
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:53
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:16
impeller::FS
GlyphAtlasPipeline::FragmentShader FS
Definition: text_contents.cc:26
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::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::kMaxUniformGradientStops
static constexpr uint32_t kMaxUniformGradientStops
Definition: gradient_generator.h:47
render_pass.h
impeller::Color
Definition: color.h:123
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
impeller::ContentContextOptions
Definition: content_context.h:279
impeller::TPoint< Scalar >