| Flutter Impeller |

sweep_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 "sweep_gradient_contents.h"
6
7 #include "flutter/fml/logging.h"
8 #include "impeller/entity/contents/clip_contents.h"
9 #include "impeller/entity/contents/content_context.h"
10 #include "impeller/entity/contents/gradient_generator.h"
11 #include "impeller/entity/entity.h"
12 #include "impeller/geometry/gradient.h"
13 #include "impeller/renderer/render_pass.h"
14
15 namespace impeller {
16
17 SweepGradientContents::SweepGradientContents() = default;
18
19 SweepGradientContents::~SweepGradientContents() = default;
20
21 void SweepGradientContents::SetCenterAndAngles(Point center,
22 Degrees start_angle,
23 Degrees end_angle) {
24 center_ = center;
25 Scalar t0 = start_angle.degrees / 360;
26 Scalar t1 = end_angle.degrees / 360;
27 FML_DCHECK(t0 < t1);
28 bias_ = -t0;
29 scale_ = 1 / (t1 - t0);
30 }
31
32 void SweepGradientContents::SetColors(std::vector<Color> colors) {
33 colors_ = std::move(colors);
34 }
35
36 void SweepGradientContents::SetStops(std::vector<Scalar> stops) {
37 stops_ = std::move(stops);
38 }
39
40 void SweepGradientContents::SetTileMode(Entity::TileMode tile_mode) {
41 tile_mode_ = tile_mode;
42 }
43
44 const std::vector<Color>& SweepGradientContents::GetColors() const {
45 return colors_;
46 }
47
48 const std::vector<Scalar>& SweepGradientContents::GetStops() const {
49 return stops_;
50 }
51
52 bool SweepGradientContents::IsOpaque(const Matrix& transform) const {
53 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
54 return false;
55 }
56 for (auto color : colors_) {
57 if (!color.IsOpaque()) {
58 return false;
59 }
60 }
61 return !AppliesAlphaForStrokeCoverage(transform);
62 }
63
64 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
65 #define UNIFORM_FRAG_INFO(t) \
66 t##GradientUniformFillPipeline::FragmentShader::FragInfo
67 #define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Sweep)::colors)
68 #define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Sweep)::stop_pairs)
69 static_assert(UNIFORM_COLOR_SIZE == kMaxUniformGradientStops);
70 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
71
72 bool SweepGradientContents::Render(const ContentContext& renderer,
73 const Entity& entity,
74 RenderPass& pass) const {
75 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
76 return RenderSSBO(renderer, entity, pass);
77 }
78 if (colors_.size() <= kMaxUniformGradientStops &&
79 stops_.size() <= kMaxUniformGradientStops) {
80 return RenderUniform(renderer, entity, pass);
81 }
82 return RenderTexture(renderer, entity, pass);
83 }
84
85 bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
86 const Entity& entity,
87 RenderPass& pass) const {
88 using VS = SweepGradientSSBOFillPipeline::VertexShader;
89 using FS = SweepGradientSSBOFillPipeline::FragmentShader;
90
91 VS::FrameInfo frame_info;
92 frame_info.matrix = GetInverseEffectTransform();
93 VS::BindFrameInfo(
94 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
95
96 PipelineBuilderCallback pipeline_callback =
97 [&renderer](ContentContextOptions options) {
98 return renderer.GetSweepGradientSSBOFillPipeline(options);
99 };
100 return ColorSourceContents::DrawGeometry<VS>(
101 renderer, entity, pass, pipeline_callback, frame_info,
102 [this, &renderer, &entity](RenderPass& pass) {
103 FS::FragInfo frag_info;
104 frag_info.center = center_;
105 frag_info.bias = bias_;
106 frag_info.scale = scale_;
107 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
108 frag_info.decal_border_color = decal_border_color_;
109 frag_info.alpha =
110 GetOpacityFactor() *
111 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
112
113 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
114 auto colors = CreateGradientColors(colors_, stops_);
115
116 frag_info.colors_length = colors.size();
117 auto color_buffer = data_host_buffer.Emplace(
118 colors.data(), colors.size() * sizeof(StopData),
119 renderer.GetDeviceCapabilities()
120 .GetMinimumStorageBufferAlignment());
121
122 pass.SetCommandLabel("SweepGradientSSBOFill");
123
124 FS::BindFragInfo(
125 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
126 FS::BindColorData(pass, color_buffer);
127
128 return true;
129 });
130 }
131
132 bool SweepGradientContents::RenderUniform(const ContentContext& renderer,
133 const Entity& entity,
134 RenderPass& pass) const {
135 using VS = SweepGradientUniformFillPipeline::VertexShader;
136 using FS = SweepGradientUniformFillPipeline::FragmentShader;
137
138 VS::FrameInfo frame_info;
139 frame_info.matrix = GetInverseEffectTransform();
140 VS::BindFrameInfo(
141 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
142
143 PipelineBuilderCallback pipeline_callback =
144 [&renderer](ContentContextOptions options) {
145 return renderer.GetSweepGradientUniformFillPipeline(options);
146 };
147 return ColorSourceContents::DrawGeometry<VS>(
148 renderer, entity, pass, pipeline_callback, frame_info,
149 [this, &renderer, &entity](RenderPass& pass) {
150 FS::FragInfo frag_info;
151 frag_info.center = center_;
152 frag_info.bias = bias_;
153 frag_info.scale = scale_;
154 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
155 frag_info.alpha =
156 GetOpacityFactor() *
157 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
158 frag_info.colors_length = PopulateUniformGradientColors(
159 colors_, stops_, frag_info.colors, frag_info.stop_pairs);
160
161 frag_info.decal_border_color = decal_border_color_;
162
163 pass.SetCommandLabel("SweepGradientUniformFill");
164
165 FS::BindFragInfo(
166 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
167
168 return true;
169 });
170 }
171
172 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
173 const Entity& entity,
174 RenderPass& pass) const {
175 using VS = SweepGradientFillPipeline::VertexShader;
176 using FS = SweepGradientFillPipeline::FragmentShader;
177
178 auto gradient_data = CreateGradientBuffer(colors_, stops_);
179 auto gradient_texture =
180 CreateGradientTexture(gradient_data, renderer.GetContext());
181 if (gradient_texture == nullptr) {
182 return false;
183 }
184
185 VS::FrameInfo frame_info;
186 frame_info.matrix = GetInverseEffectTransform();
187
188 PipelineBuilderCallback pipeline_callback =
189 [&renderer](ContentContextOptions options) {
190 return renderer.GetSweepGradientFillPipeline(options);
191 };
192 return ColorSourceContents::DrawGeometry<VS>(
193 renderer, entity, pass, pipeline_callback, frame_info,
194 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
195 FS::FragInfo frag_info;
196 frag_info.center = center_;
197 frag_info.bias = bias_;
198 frag_info.scale = scale_;
199 frag_info.texture_sampler_y_coord_scale =
200 gradient_texture->GetYCoordScale();
201 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
202 frag_info.decal_border_color = decal_border_color_;
203 frag_info.alpha =
204 GetOpacityFactor() *
205 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
206 frag_info.half_texel =
207 Vector2(0.5 / gradient_texture->GetSize().width,
208 0.5 / gradient_texture->GetSize().height);
209
210 SamplerDescriptor sampler_desc;
211 sampler_desc.min_filter = MinMagFilter::kLinear;
212 sampler_desc.mag_filter = MinMagFilter::kLinear;
213
214 pass.SetCommandLabel("SweepGradientFill");
215
216 FS::BindFragInfo(
217 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
218 FS::BindTextureSampler(
219 pass, gradient_texture,
220 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
221 sampler_desc));
222
223 return true;
224 });
225 }
226
227 bool SweepGradientContents::ApplyColorFilter(
228 const ColorFilterProc& color_filter_proc) {
229 for (Color& color : colors_) {
230 color = color_filter_proc(color);
231 }
232 decal_border_color_ = color_filter_proc(decal_border_color_);
233 return true;
234 }
235
236 } // namespace impeller
impeller::Capabilities::GetMinimumStorageBufferAlignment
virtual size_t GetMinimumStorageBufferAlignment() const
The minimum alignment of storage buffer value offsets in bytes.
Definition: capabilities.cc:14
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::AppliesAlphaForStrokeCoverage
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
Definition: color_source_contents.cc:53
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::ContentContext
Definition: content_context.h:122
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:973
impeller::ContentContext::GetSweepGradientSSBOFillPipeline
PipelineRef GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.cc:1102
impeller::ContentContext::GetTransientsDataBuffer
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
Definition: content_context.h:299
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::TileMode::kDecal
@ kDecal
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
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
impeller::SweepGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: sweep_gradient_contents.cc:40
impeller::SweepGradientContents::SweepGradientContents
SweepGradientContents()
impeller::SweepGradientContents::SetCenterAndAngles
void SetCenterAndAngles(Point center, Degrees start_angle, Degrees end_angle)
Definition: sweep_gradient_contents.cc:21
impeller::SweepGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: sweep_gradient_contents.cc:72
impeller::SweepGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: sweep_gradient_contents.cc:227
impeller::SweepGradientContents::IsOpaque
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: sweep_gradient_contents.cc:52
impeller::SweepGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: sweep_gradient_contents.cc:44
impeller::SweepGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: sweep_gradient_contents.cc:48
impeller::SweepGradientContents::~SweepGradientContents
~SweepGradientContents() override
impeller::SweepGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: sweep_gradient_contents.cc:36
impeller::SweepGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: sweep_gradient_contents.cc:32
clip_contents.h
content_context.h
entity.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:312
gradient.h
gradient_generator.h
impeller
Definition: allocation.cc:12
impeller::Vector2
Point Vector2
Definition: point.h:429
impeller::Scalar
float Scalar
Definition: scalar.h:19
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::Degrees
Definition: scalar.h:66
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:67
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TPoint< Scalar >
UNIFORM_STOP_SIZE
#define UNIFORM_STOP_SIZE
Definition: sweep_gradient_contents.cc:68
UNIFORM_COLOR_SIZE
#define UNIFORM_COLOR_SIZE
Definition: sweep_gradient_contents.cc:67
sweep_gradient_contents.h