| Flutter Impeller |

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
5 #include "radial_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 RadialGradientContents::RadialGradientContents() = default;
18
19 RadialGradientContents::~RadialGradientContents() = default;
20
21 void RadialGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
22 center_ = center;
23 radius_ = radius;
24 }
25
26 void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) {
27 tile_mode_ = tile_mode;
28 }
29
30 void RadialGradientContents::SetColors(std::vector<Color> colors) {
31 colors_ = std::move(colors);
32 }
33
34 void RadialGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36 }
37
38 const std::vector<Color>& RadialGradientContents::GetColors() const {
39 return colors_;
40 }
41
42 const std::vector<Scalar>& RadialGradientContents::GetStops() const {
43 return stops_;
44 }
45
46 bool RadialGradientContents::IsOpaque(const Matrix& transform) const {
47 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
48 return false;
49 }
50 for (auto color : colors_) {
51 if (!color.IsOpaque()) {
52 return false;
53 }
54 }
55 return !AppliesAlphaForStrokeCoverage(transform);
56 }
57
58 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
59 #define UNIFORM_FRAG_INFO(t) \
60 t##GradientUniformFillPipeline::FragmentShader::FragInfo
61 #define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Radial)::colors)
62 #define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Radial)::stop_pairs)
63 static_assert(UNIFORM_COLOR_SIZE == kMaxUniformGradientStops);
64 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
65
66 bool RadialGradientContents::Render(const ContentContext& renderer,
67 const Entity& entity,
68 RenderPass& pass) const {
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
79 bool 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 data_host_buffer.GetMinimumUniformAlignment());
111
112 pass.SetCommandLabel("RadialGradientSSBOFill");
113 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
114 FS::BindColorData(pass, color_buffer);
115
116 return true;
117 });
118 }
119
120 bool RadialGradientContents::RenderUniform(const ContentContext& renderer,
121 const Entity& entity,
122 RenderPass& pass) const {
123 using VS = RadialGradientUniformFillPipeline::VertexShader;
124 using FS = RadialGradientUniformFillPipeline::FragmentShader;
125
126 VS::FrameInfo frame_info;
127 frame_info.matrix = GetInverseEffectTransform();
128
129 PipelineBuilderCallback pipeline_callback =
130 [&renderer](ContentContextOptions options) {
131 return renderer.GetRadialGradientUniformFillPipeline(options);
132 };
133 return ColorSourceContents::DrawGeometry<VS>(
134 renderer, entity, pass, pipeline_callback, frame_info,
135 [this, &renderer, &entity](RenderPass& pass) {
136 FS::FragInfo frag_info;
137 frag_info.center = center_;
138 frag_info.radius = radius_;
139 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
140 frag_info.alpha =
141 GetOpacityFactor() *
142 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
143 frag_info.colors_length = PopulateUniformGradientColors(
144 colors_, stops_, frag_info.colors, frag_info.stop_pairs);
145 frag_info.decal_border_color = decal_border_color_;
146
147 pass.SetCommandLabel("RadialGradientUniformFill");
148
149 FS::BindFragInfo(
150 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
151
152 return true;
153 });
154 }
155
156 bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
157 const Entity& entity,
158 RenderPass& pass) const {
159 using VS = RadialGradientFillPipeline::VertexShader;
160 using FS = RadialGradientFillPipeline::FragmentShader;
161
162 auto gradient_data = CreateGradientBuffer(colors_, stops_);
163 auto gradient_texture =
164 CreateGradientTexture(gradient_data, renderer.GetContext());
165 if (gradient_texture == nullptr) {
166 return false;
167 }
168
169 VS::FrameInfo frame_info;
170 frame_info.matrix = GetInverseEffectTransform();
171
172 PipelineBuilderCallback pipeline_callback =
173 [&renderer](ContentContextOptions options) {
174 return renderer.GetRadialGradientFillPipeline(options);
175 };
176 return ColorSourceContents::DrawGeometry<VS>(
177 renderer, entity, pass, pipeline_callback, frame_info,
178 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
179 FS::FragInfo frag_info;
180 frag_info.center = center_;
181 frag_info.radius = radius_;
182 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
183 frag_info.decal_border_color = decal_border_color_;
184 frag_info.texture_sampler_y_coord_scale =
185 gradient_texture->GetYCoordScale();
186 frag_info.alpha =
187 GetOpacityFactor() *
188 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
189 frag_info.half_texel =
190 Vector2(0.5 / gradient_texture->GetSize().width,
191 0.5 / gradient_texture->GetSize().height);
192
193 SamplerDescriptor sampler_desc;
194 sampler_desc.min_filter = MinMagFilter::kLinear;
195 sampler_desc.mag_filter = MinMagFilter::kLinear;
196
197 pass.SetCommandLabel("RadialGradientFill");
198
199 FS::BindFragInfo(
200 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
201 FS::BindTextureSampler(
202 pass, gradient_texture,
203 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
204 sampler_desc));
205
206 return true;
207 });
208 }
209
210 bool RadialGradientContents::ApplyColorFilter(
211 const ColorFilterProc& color_filter_proc) {
212 for (Color& color : colors_) {
213 color = color_filter_proc(color);
214 }
215 decal_border_color_ = color_filter_proc(decal_border_color_);
216 return true;
217 }
218
219 } // 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::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:138
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:954
impeller::ContentContext::GetRadialGradientSSBOFillPipeline
PipelineRef GetRadialGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.cc:1039
impeller::ContentContext::GetTransientsDataBuffer
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
Definition: content_context.h:310
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:44
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125
impeller::RadialGradientContents::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: radial_gradient_contents.cc:46
impeller::RadialGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: radial_gradient_contents.cc:26
impeller::RadialGradientContents::~RadialGradientContents
~RadialGradientContents() override
impeller::RadialGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: radial_gradient_contents.cc:66
impeller::RadialGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: radial_gradient_contents.cc:21
impeller::RadialGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: radial_gradient_contents.cc:34
impeller::RadialGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: radial_gradient_contents.cc:210
impeller::RadialGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: radial_gradient_contents.cc:30
impeller::RadialGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: radial_gradient_contents.cc:42
impeller::RadialGradientContents::RadialGradientContents
RadialGradientContents()
impeller::RadialGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: radial_gradient_contents.cc:38
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
content_context.h
entity.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
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::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
UNIFORM_STOP_SIZE
#define UNIFORM_STOP_SIZE
Definition: radial_gradient_contents.cc:62
UNIFORM_COLOR_SIZE
#define UNIFORM_COLOR_SIZE
Definition: radial_gradient_contents.cc:61
radial_gradient_contents.h
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::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TPoint< Scalar >