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 
6 
10 #include "impeller/entity/entity.h"
14 
15 namespace impeller {
16 
18 
20 
22  center_ = center;
23  radius_ = radius;
24 }
25 
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)
58 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
59 
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 {
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 =
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),
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 {
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 {
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 
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
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
void SetFocus(std::optional< Point > focus, Scalar radius)
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
void SetStops(std::vector< Scalar > stops)
void SetCenterAndRadius(Point center, Scalar radius)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
const std::vector< Scalar > & GetStops() const
void SetTileMode(Entity::TileMode tile_mode)
const std::vector< Color > & GetColors() const
void SetColors(std::vector< Color > colors)
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
const Capabilities & GetDeviceCapabilities() const
PipelineRef GetConicalGradientSSBOFillPipeline(ContentContextOptions opts) const
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:115
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:48
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
FragmentShader_ FragmentShader
Definition: pipeline.h:114
#define UNIFORM_STOP_SIZE
#define UNIFORM_COLOR_SIZE
Point Vector2
Definition: point.h:331
float Scalar
Definition: scalar.h:18
GlyphAtlasPipeline::VertexShader VS
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.
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.
GlyphAtlasPipeline::FragmentShader FS
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
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
static constexpr uint32_t kMaxUniformGradientStops
Scalar alpha
Definition: color.h:142