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 
6 
7 #include "flutter/fml/logging.h"
11 #include "impeller/entity/entity.h"
14 
15 namespace impeller {
16 
18  : geometry_(geometry) {}
19 
21 
23  return geometry_;
24 }
25 
27  Degrees start_angle,
28  Degrees end_angle) {
29  center_ = center;
30  Scalar t0 = start_angle.degrees / 360;
31  Scalar t1 = end_angle.degrees / 360;
32  FML_DCHECK(t0 < t1);
33  bias_ = -t0;
34  scale_ = 1 / (t1 - t0);
35 }
36 
37 void SweepGradientContents::SetColors(std::vector<Color> colors) {
38  colors_ = std::move(colors);
39 }
40 
41 void SweepGradientContents::SetStops(std::vector<Scalar> stops) {
42  stops_ = std::move(stops);
43 }
44 
46  tile_mode_ = tile_mode;
47 }
48 
49 const std::vector<Color>& SweepGradientContents::GetColors() const {
50  return colors_;
51 }
52 
53 const std::vector<Scalar>& SweepGradientContents::GetStops() const {
54  return stops_;
55 }
56 
58  if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
59  return false;
60  }
61  for (auto color : colors_) {
62  if (!color.IsOpaque()) {
63  return false;
64  }
65  }
67 }
68 
69 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
70 #define UNIFORM_COLORS_INFO(t) \
71  t##GradientUniformFillPipeline::FragmentShader::ColorsInfo
72 #define UNIFORM_STOP_PAIRS_INFO(t) \
73  t##GradientUniformFillPipeline::FragmentShader::StopPairsInfo
74 #define UNIFORM_COLOR_SIZE ARRAY_LEN(UNIFORM_COLORS_INFO(Sweep)::colors)
75 #define UNIFORM_STOP_SIZE ARRAY_LEN(UNIFORM_STOP_PAIRS_INFO(Sweep)::stop_pairs)
77 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
78 static_assert(sizeof(UNIFORM_COLORS_INFO(Sweep)) ==
79  sizeof(UNIFORM_COLORS_INFO(Sweep)::colors));
80 static_assert(sizeof(UNIFORM_STOP_PAIRS_INFO(Sweep)) ==
81  sizeof(UNIFORM_STOP_PAIRS_INFO(Sweep)::stop_pairs));
82 
84  const Entity& entity,
85  RenderPass& pass) const {
86  if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
87  return RenderSSBO(renderer, entity, pass);
88  }
89  if (colors_.size() <= kMaxUniformGradientStops &&
90  stops_.size() <= kMaxUniformGradientStops) {
91  return RenderUniform(renderer, entity, pass);
92  }
93  return RenderTexture(renderer, entity, pass);
94 }
95 
96 bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
97  const Entity& entity,
98  RenderPass& pass) const {
101 
102  VS::FrameInfo frame_info;
103  frame_info.matrix = GetInverseEffectTransform();
104  VS::BindFrameInfo(
105  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
106 
107  PipelineBuilderCallback pipeline_callback =
108  [&renderer](ContentContextOptions options) {
109  return renderer.GetSweepGradientSSBOFillPipeline(options);
110  };
111  return ColorSourceContents::DrawGeometry<VS>(
112  renderer, entity, pass, pipeline_callback, frame_info,
113  [this, &renderer, &entity](RenderPass& pass) {
114  FS::FragInfo frag_info;
115  frag_info.center = center_;
116  frag_info.bias = bias_;
117  frag_info.scale = scale_;
118  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
119  frag_info.decal_border_color = decal_border_color_;
120  frag_info.alpha =
121  GetOpacityFactor() *
123 
124  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
125  auto colors = CreateGradientColors(colors_, stops_);
126 
127  frag_info.colors_length = colors.size();
128  auto color_buffer = data_host_buffer.Emplace(
129  colors.data(), colors.size() * sizeof(StopData),
130  renderer.GetDeviceCapabilities()
132 
133  pass.SetCommandLabel("SweepGradientSSBOFill");
134 
135  FS::BindFragInfo(
136  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
137  FS::BindColorData(pass, color_buffer);
138 
139  return true;
140  });
141 }
142 
143 bool SweepGradientContents::RenderUniform(const ContentContext& renderer,
144  const Entity& entity,
145  RenderPass& pass) const {
148 
149  VS::FrameInfo frame_info;
150  frame_info.matrix = GetInverseEffectTransform();
151  VS::BindFrameInfo(
152  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
153 
154  PipelineBuilderCallback pipeline_callback =
155  [&renderer](ContentContextOptions options) {
156  return renderer.GetSweepGradientUniformFillPipeline(options);
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  FS::ColorsInfo colors_info;
163  FS::StopPairsInfo stop_pairs_info;
164 
165  frag_info.center = center_;
166  frag_info.bias = bias_;
167  frag_info.scale = scale_;
168  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
169  frag_info.alpha =
170  GetOpacityFactor() *
171  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
172  frag_info.colors_length = PopulateUniformGradientColors(
173  colors_, stops_, colors_info.colors, stop_pairs_info.stop_pairs);
174 
175  frag_info.decal_border_color = decal_border_color_;
176 
177  pass.SetCommandLabel("SweepGradientUniformFill");
178 
179  auto& transients_buffer = renderer.GetTransientsDataBuffer();
180  FS::BindFragInfo(pass, transients_buffer.EmplaceUniform(frag_info));
181  FS::BindColorsInfo(pass, transients_buffer.EmplaceUniform(colors_info));
182  FS::BindStopPairsInfo(
183  pass, transients_buffer.EmplaceUniform(stop_pairs_info));
184 
185  return true;
186  });
187 }
188 
189 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
190  const Entity& entity,
191  RenderPass& pass) const {
194 
195  auto gradient_data = CreateGradientBuffer(colors_, stops_);
196  auto gradient_texture =
197  CreateGradientTexture(gradient_data, renderer.GetContext());
198  if (gradient_texture == nullptr) {
199  return false;
200  }
201 
202  VS::FrameInfo frame_info;
203  frame_info.matrix = GetInverseEffectTransform();
204 
205  PipelineBuilderCallback pipeline_callback =
206  [&renderer](ContentContextOptions options) {
207  return renderer.GetSweepGradientFillPipeline(options);
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.bias = bias_;
215  frag_info.scale = scale_;
216  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
217  frag_info.decal_border_color = decal_border_color_;
218  frag_info.alpha =
219  GetOpacityFactor() *
220  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
221  frag_info.half_texel =
222  Vector2(0.5 / gradient_texture->GetSize().width,
223  0.5 / gradient_texture->GetSize().height);
224 
225  SamplerDescriptor sampler_desc;
226  sampler_desc.min_filter = MinMagFilter::kLinear;
227  sampler_desc.mag_filter = MinMagFilter::kLinear;
228 
229  pass.SetCommandLabel("SweepGradientFill");
230 
231  FS::BindFragInfo(
232  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
233  FS::BindTextureSampler(
234  pass, gradient_texture,
235  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
236  sampler_desc));
237 
238  return true;
239  });
240 }
241 
243  const ColorFilterProc& color_filter_proc) {
244  for (Color& color : colors_) {
245  color = color_filter_proc(color);
246  }
247  decal_border_color_ = color_filter_proc(decal_border_color_);
248  return true;
249 }
250 
251 } // namespace impeller
virtual size_t GetMinimumStorageBufferAlignment() const
The minimum alignment of storage buffer value offsets in bytes.
Definition: capabilities.cc:14
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
const Matrix & GetInverseEffectTransform() const
Get the inverted effect transform for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
const Capabilities & GetDeviceCapabilities() const
PipelineRef GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:135
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:47
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:169
void SetTileMode(Entity::TileMode tile_mode)
void SetCenterAndAngles(Point center, Degrees start_angle, Degrees end_angle)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
const Geometry * GetGeometry() const override
Get the geometry that this contents will use to render.
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
const std::vector< Color > & GetColors() const
const std::vector< Scalar > & GetStops() const
void SetStops(std::vector< Scalar > stops)
SweepGradientContents(const Geometry *geometry)
void SetColors(std::vector< Color > colors)
Point Vector2
Definition: point.h:430
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
static constexpr uint32_t kMaxUniformGradientStops
Scalar alpha
Definition: color.h:143
Scalar degrees
Definition: scalar.h:67
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
#define UNIFORM_STOP_PAIRS_INFO(t)
#define UNIFORM_STOP_SIZE
#define UNIFORM_COLORS_INFO(t)
#define UNIFORM_COLOR_SIZE