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 
17 namespace {
18 ConicalKind GetConicalKind(Point center,
19  Scalar radius,
20  std::optional<Point> focus,
21  Scalar focus_radius) {
23  if (!focus.has_value() ||
24  center.GetDistance(focus.value()) < kEhCloseEnough) {
25  kind = ConicalKind::kRadial;
26  }
27  if (focus.has_value() && std::fabsf(radius - focus_radius) < kEhCloseEnough) {
28  if (kind == ConicalKind::kRadial) {
30  } else {
31  kind = ConicalKind::kStrip;
32  }
33  }
34  return kind;
35 }
36 
37 } // namespace
38 
40 
42 
44  center_ = center;
45  radius_ = radius;
46 }
47 
49  tile_mode_ = tile_mode;
50 }
51 
52 void ConicalGradientContents::SetColors(std::vector<Color> colors) {
53  colors_ = std::move(colors);
54 }
55 
56 void ConicalGradientContents::SetStops(std::vector<Scalar> stops) {
57  stops_ = std::move(stops);
58 }
59 
60 const std::vector<Color>& ConicalGradientContents::GetColors() const {
61  return colors_;
62 }
63 
64 const std::vector<Scalar>& ConicalGradientContents::GetStops() const {
65  return stops_;
66 }
67 
68 void ConicalGradientContents::SetFocus(std::optional<Point> focus,
69  Scalar radius) {
70  focus_ = focus;
71  focus_radius_ = radius;
72 }
73 
74 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
75 #define UNIFORM_FRAG_INFO(t) \
76  t##GradientUniformFillConicalPipeline::FragmentShader::FragInfo
77 #define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Conical)::colors)
78 #define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_FRAG_INFO(Conical)::stop_pairs)
80 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
81 
83  const Entity& entity,
84  RenderPass& pass) const {
85  if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
86  return RenderSSBO(renderer, entity, pass);
87  }
88  if (colors_.size() <= kMaxUniformGradientStops &&
89  stops_.size() <= kMaxUniformGradientStops) {
90  return RenderUniform(renderer, entity, pass);
91  }
92  return RenderTexture(renderer, entity, pass);
93 }
94 
95 bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
96  const Entity& entity,
97  RenderPass& pass) const {
100 
101  VS::FrameInfo frame_info;
102  frame_info.matrix = GetInverseEffectTransform();
103 
104  ConicalKind kind = GetConicalKind(center_, radius_, focus_, focus_radius_);
105  PipelineBuilderCallback pipeline_callback =
106  [&renderer, kind](ContentContextOptions options) {
107  return renderer.GetConicalGradientSSBOFillPipeline(options, kind);
108  };
109  return ColorSourceContents::DrawGeometry<VS>(
110  renderer, entity, pass, pipeline_callback, frame_info,
111  [this, &renderer, &entity](RenderPass& pass) {
112  FS::FragInfo frag_info;
113  frag_info.center = center_;
114  frag_info.radius = radius_;
115  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
116  frag_info.decal_border_color = decal_border_color_;
117  frag_info.alpha =
118  GetOpacityFactor() *
120  if (focus_) {
121  frag_info.focus = focus_.value();
122  frag_info.focus_radius = focus_radius_;
123  } else {
124  frag_info.focus = center_;
125  frag_info.focus_radius = 0.0;
126  }
127 
128  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
129  auto colors = CreateGradientColors(colors_, stops_);
130 
131  frag_info.colors_length = colors.size();
132  auto color_buffer = data_host_buffer.Emplace(
133  colors.data(), colors.size() * sizeof(StopData),
134  data_host_buffer.GetMinimumUniformAlignment());
135 
136  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
137  FS::BindColorData(pass, color_buffer);
138 
139  pass.SetCommandLabel("ConicalGradientSSBOFill");
140  return true;
141  });
142 }
143 
144 bool ConicalGradientContents::RenderUniform(const ContentContext& renderer,
145  const Entity& entity,
146  RenderPass& pass) const {
149 
150  VS::FrameInfo frame_info;
151  frame_info.matrix = GetInverseEffectTransform();
152 
153  ConicalKind kind = GetConicalKind(center_, radius_, focus_, focus_radius_);
154  PipelineBuilderCallback pipeline_callback =
155  [&renderer, kind](ContentContextOptions options) {
156  return renderer.GetConicalGradientUniformFillPipeline(options, kind);
157  };
158  return ColorSourceContents::DrawGeometry<VS>(
159  renderer, entity, pass, pipeline_callback, frame_info,
160  [this, &renderer, &entity](RenderPass& pass) {
161  FS::FragInfo frag_info;
162  frag_info.center = center_;
163  if (focus_) {
164  frag_info.focus = focus_.value();
165  frag_info.focus_radius = focus_radius_;
166  } else {
167  frag_info.focus = center_;
168  frag_info.focus_radius = 0.0;
169  }
170  frag_info.radius = radius_;
171  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
172  frag_info.alpha =
173  GetOpacityFactor() *
174  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
175  frag_info.colors_length = PopulateUniformGradientColors(
176  colors_, stops_, frag_info.colors, frag_info.stop_pairs);
177  frag_info.decal_border_color = decal_border_color_;
178 
179  pass.SetCommandLabel("ConicalGradientUniformFill");
180 
181  FS::BindFragInfo(
182  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
183 
184  return true;
185  });
186 }
187 
188 bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
189  const Entity& entity,
190  RenderPass& pass) const {
193 
194  auto gradient_data = CreateGradientBuffer(colors_, stops_);
195  auto gradient_texture =
196  CreateGradientTexture(gradient_data, renderer.GetContext());
197  if (gradient_texture == nullptr) {
198  return false;
199  }
200 
201  VS::FrameInfo frame_info;
202  frame_info.matrix = GetInverseEffectTransform();
203 
204  ConicalKind kind = GetConicalKind(center_, radius_, focus_, focus_radius_);
205  PipelineBuilderCallback pipeline_callback =
206  [&renderer, kind](ContentContextOptions options) {
207  return renderer.GetConicalGradientFillPipeline(options, kind);
208  };
209  return ColorSourceContents::DrawGeometry<VS>(
210  renderer, entity, pass, pipeline_callback, frame_info,
211  [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
212  FS::FragInfo frag_info;
213  frag_info.center = center_;
214  frag_info.radius = radius_;
215  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
216  frag_info.decal_border_color = decal_border_color_;
217  frag_info.texture_sampler_y_coord_scale =
218  gradient_texture->GetYCoordScale();
219  frag_info.alpha =
220  GetOpacityFactor() *
221  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
222  frag_info.half_texel =
223  Vector2(0.5 / gradient_texture->GetSize().width,
224  0.5 / gradient_texture->GetSize().height);
225  if (focus_) {
226  frag_info.focus = focus_.value();
227  frag_info.focus_radius = focus_radius_;
228  } else {
229  frag_info.focus = center_;
230  frag_info.focus_radius = 0.0;
231  }
232 
233  pass.SetCommandLabel("ConicalGradientFill");
234 
235  FS::BindFragInfo(
236  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
237  SamplerDescriptor sampler_desc;
238  sampler_desc.min_filter = MinMagFilter::kLinear;
239  sampler_desc.mag_filter = MinMagFilter::kLinear;
240  FS::BindTextureSampler(
241  pass, gradient_texture,
242  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
243  sampler_desc));
244 
245  return true;
246  });
247 }
248 
250  const ColorFilterProc& color_filter_proc) {
251  for (Color& color : colors_) {
252  color = color_filter_proc(color);
253  }
254  decal_border_color_ = color_filter_proc(decal_border_color_);
255  return true;
256 }
257 
258 } // 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)
const Capabilities & GetDeviceCapabilities() const
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
PipelineRef GetConicalGradientSSBOFillPipeline(ContentContextOptions opts, ConicalKind kind) 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:125
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:164
#define UNIFORM_STOP_SIZE
#define UNIFORM_COLOR_SIZE
Point Vector2
Definition: point.h:331
float Scalar
Definition: scalar.h:19
constexpr float kEhCloseEnough
Definition: constants.h:57
TPoint< Scalar > Point
Definition: point.h:327
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
static constexpr uint32_t kMaxUniformGradientStops
Scalar alpha
Definition: color.h:143