Flutter Engine
 
Loading...
Searching...
No Matches
radial_gradient_contents.cc File Reference

Go to the source code of this file.

Namespaces

namespace  impeller
 

Macros

#define ARRAY_LEN(a)   (sizeof(a) / sizeof(a[0]))
 
#define UNIFORM_FRAG_INFO(t)    t##GradientUniformFillPipeline::FragmentShader::FragInfo
 
#define UNIFORM_COLOR_SIZE   ARRAY_LEN(UNIFORM_FRAG_INFO(Radial)::colors)
 
#define UNIFORM_STOP_SIZE   ARRAY_LEN(UNIFORM_FRAG_INFO(Radial)::stop_pairs)
 

Macro Definition Documentation

◆ ARRAY_LEN

#define ARRAY_LEN (   a)    (sizeof(a) / sizeof(a[0]))

Definition at line 58 of file radial_gradient_contents.cc.

◆ UNIFORM_COLOR_SIZE

#define UNIFORM_COLOR_SIZE   ARRAY_LEN(UNIFORM_FRAG_INFO(Radial)::colors)

Definition at line 61 of file radial_gradient_contents.cc.

◆ UNIFORM_FRAG_INFO

#define UNIFORM_FRAG_INFO (   t)     t##GradientUniformFillPipeline::FragmentShader::FragInfo

Definition at line 59 of file radial_gradient_contents.cc.

68 {
69 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
70 return RenderSSBO(renderer, entity, pass);
71 }
72 if (colors_.size() <= kMaxUniformGradientStops &&
73 stops_.size() <= kMaxUniformGradientStops) {
74 return RenderUniform(renderer, entity, pass);
75 }
76 return RenderTexture(renderer, entity, pass);
77}
78
79bool RadialGradientContents::RenderSSBO(const ContentContext& renderer,
80 const Entity& entity,
81 RenderPass& pass) const {
82 using VS = RadialGradientSSBOFillPipeline::VertexShader;
83 using FS = RadialGradientSSBOFillPipeline::FragmentShader;
84
85 VS::FrameInfo frame_info;
86 frame_info.matrix = GetInverseEffectTransform();
87
88 PipelineBuilderCallback pipeline_callback =
89 [&renderer](ContentContextOptions options) {
90 return renderer.GetRadialGradientSSBOFillPipeline(options);
91 };
92 return ColorSourceContents::DrawGeometry<VS>(
93 renderer, entity, pass, pipeline_callback, frame_info,
94 [this, &renderer, &entity](RenderPass& pass) {
95 FS::FragInfo frag_info;
96 frag_info.center = center_;
97 frag_info.radius = radius_;
98 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
99 frag_info.decal_border_color = decal_border_color_;
100 frag_info.alpha =
101 GetOpacityFactor() *
102 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
103
104 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
105 auto colors = CreateGradientColors(colors_, stops_);
106
107 frag_info.colors_length = colors.size();
108 auto color_buffer = data_host_buffer.Emplace(
109 colors.data(), colors.size() * sizeof(StopData),
110 renderer.GetDeviceCapabilities()
111 .GetMinimumStorageBufferAlignment());
112
113 pass.SetCommandLabel("RadialGradientSSBOFill");
114 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
115 FS::BindColorData(pass, color_buffer);
116
117 return true;
118 });
119}
120
121bool RadialGradientContents::RenderUniform(const ContentContext& renderer,
122 const Entity& entity,
123 RenderPass& pass) const {
124 using VS = RadialGradientUniformFillPipeline::VertexShader;
125 using FS = RadialGradientUniformFillPipeline::FragmentShader;
126
127 VS::FrameInfo frame_info;
128 frame_info.matrix = GetInverseEffectTransform();
129
130 PipelineBuilderCallback pipeline_callback =
131 [&renderer](ContentContextOptions options) {
132 return renderer.GetRadialGradientUniformFillPipeline(options);
133 };
134 return ColorSourceContents::DrawGeometry<VS>(
135 renderer, entity, pass, pipeline_callback, frame_info,
136 [this, &renderer, &entity](RenderPass& pass) {
137 FS::FragInfo frag_info;
138 frag_info.center = center_;
139 frag_info.radius = radius_;
140 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
141 frag_info.alpha =
142 GetOpacityFactor() *
143 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
144 frag_info.colors_length = PopulateUniformGradientColors(
145 colors_, stops_, frag_info.colors, frag_info.stop_pairs);
146 frag_info.decal_border_color = decal_border_color_;
147
148 pass.SetCommandLabel("RadialGradientUniformFill");
149
150 FS::BindFragInfo(
151 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
152
153 return true;
154 });
155}
156
157bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
158 const Entity& entity,
159 RenderPass& pass) const {
160 using VS = RadialGradientFillPipeline::VertexShader;
161 using FS = RadialGradientFillPipeline::FragmentShader;
162
163 auto gradient_data = CreateGradientBuffer(colors_, stops_);
164 auto gradient_texture =
165 CreateGradientTexture(gradient_data, renderer.GetContext());
166 if (gradient_texture == nullptr) {
167 return false;
168 }
169
170 VS::FrameInfo frame_info;
171 frame_info.matrix = GetInverseEffectTransform();
172
173 PipelineBuilderCallback pipeline_callback =
174 [&renderer](ContentContextOptions options) {
175 return renderer.GetRadialGradientFillPipeline(options);
176 };
177 return ColorSourceContents::DrawGeometry<VS>(
178 renderer, entity, pass, pipeline_callback, frame_info,
179 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
180 FS::FragInfo frag_info;
181 frag_info.center = center_;
182 frag_info.radius = radius_;
183 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
184 frag_info.decal_border_color = decal_border_color_;
185 frag_info.texture_sampler_y_coord_scale =
186 gradient_texture->GetYCoordScale();
187 frag_info.alpha =
188 GetOpacityFactor() *
189 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
190 frag_info.half_texel =
191 Vector2(0.5 / gradient_texture->GetSize().width,
192 0.5 / gradient_texture->GetSize().height);
193
194 SamplerDescriptor sampler_desc;
195 sampler_desc.min_filter = MinMagFilter::kLinear;
196 sampler_desc.mag_filter = MinMagFilter::kLinear;
197
198 pass.SetCommandLabel("RadialGradientFill");
199
200 FS::BindFragInfo(
201 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
202 FS::BindTextureSampler(
203 pass, gradient_texture,
204 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
205 sampler_desc));
206
207 return true;
208 });
209}
210
211bool RadialGradientContents::ApplyColorFilter(
212 const ColorFilterProc& color_filter_proc) {
213 for (Color& color : colors_) {
214 color = color_filter_proc(color);
215 }
216 decal_border_color_ = color_filter_proc(decal_border_color_);
217 return true;
218}
219
220} // namespace impeller
Point Vector2
Definition point.h:331
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::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

◆ UNIFORM_STOP_SIZE

#define UNIFORM_STOP_SIZE   ARRAY_LEN(UNIFORM_FRAG_INFO(Radial)::stop_pairs)

Definition at line 62 of file radial_gradient_contents.cc.