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 
20 
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 
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 
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  }
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)
70 static_assert(UNIFORM_STOP_SIZE == kMaxUniformGradientStops / 2);
71 
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 {
90 
91  VS::FrameInfo frame_info;
92  frame_info.matrix = GetInverseEffectTransform();
93  VS::BindFrameInfo(pass,
94  renderer.GetTransientsBuffer().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() *
112 
113  auto& host_buffer = renderer.GetTransientsBuffer();
114  auto colors = CreateGradientColors(colors_, stops_);
115 
116  frag_info.colors_length = colors.size();
117  auto color_buffer =
118  host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
119  host_buffer.GetMinimumUniformAlignment());
120 
121  pass.SetCommandLabel("SweepGradientSSBOFill");
122 
123  FS::BindFragInfo(
124  pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
125  FS::BindColorData(pass, color_buffer);
126 
127  return true;
128  });
129 }
130 
131 bool SweepGradientContents::RenderUniform(const ContentContext& renderer,
132  const Entity& entity,
133  RenderPass& pass) const {
136 
137  VS::FrameInfo frame_info;
138  frame_info.matrix = GetInverseEffectTransform();
139  VS::BindFrameInfo(pass,
140  renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
141 
142  PipelineBuilderCallback pipeline_callback =
143  [&renderer](ContentContextOptions options) {
144  return renderer.GetSweepGradientUniformFillPipeline(options);
145  };
146  return ColorSourceContents::DrawGeometry<VS>(
147  renderer, entity, pass, pipeline_callback, frame_info,
148  [this, &renderer, &entity](RenderPass& pass) {
149  FS::FragInfo frag_info;
150  frag_info.center = center_;
151  frag_info.bias = bias_;
152  frag_info.scale = scale_;
153  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
154  frag_info.alpha =
155  GetOpacityFactor() *
156  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
157  frag_info.colors_length = PopulateUniformGradientColors(
158  colors_, stops_, frag_info.colors, frag_info.stop_pairs);
159 
160  frag_info.decal_border_color = decal_border_color_;
161 
162  pass.SetCommandLabel("SweepGradientUniformFill");
163 
164  FS::BindFragInfo(
165  pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
166 
167  return true;
168  });
169 }
170 
171 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
172  const Entity& entity,
173  RenderPass& pass) const {
176 
177  auto gradient_data = CreateGradientBuffer(colors_, stops_);
178  auto gradient_texture =
179  CreateGradientTexture(gradient_data, renderer.GetContext());
180  if (gradient_texture == nullptr) {
181  return false;
182  }
183 
184  VS::FrameInfo frame_info;
185  frame_info.matrix = GetInverseEffectTransform();
186 
187  PipelineBuilderCallback pipeline_callback =
188  [&renderer](ContentContextOptions options) {
189  return renderer.GetSweepGradientFillPipeline(options);
190  };
191  return ColorSourceContents::DrawGeometry<VS>(
192  renderer, entity, pass, pipeline_callback, frame_info,
193  [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
194  FS::FragInfo frag_info;
195  frag_info.center = center_;
196  frag_info.bias = bias_;
197  frag_info.scale = scale_;
198  frag_info.texture_sampler_y_coord_scale =
199  gradient_texture->GetYCoordScale();
200  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
201  frag_info.decal_border_color = decal_border_color_;
202  frag_info.alpha =
203  GetOpacityFactor() *
204  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
205  frag_info.half_texel =
206  Vector2(0.5 / gradient_texture->GetSize().width,
207  0.5 / gradient_texture->GetSize().height);
208 
209  SamplerDescriptor sampler_desc;
210  sampler_desc.min_filter = MinMagFilter::kLinear;
211  sampler_desc.mag_filter = MinMagFilter::kLinear;
212 
213  pass.SetCommandLabel("SweepGradientFill");
214 
215  FS::BindFragInfo(
216  pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
217  FS::BindTextureSampler(
218  pass, gradient_texture,
219  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
220  sampler_desc));
221 
222  return true;
223  });
224 }
225 
227  const ColorFilterProc& color_filter_proc) {
228  for (Color& color : colors_) {
229  color = color_filter_proc(color);
230  }
231  decal_border_color_ = color_filter_proc(decal_border_color_);
232  return true;
233 }
234 
235 } // 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.
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
Set the inverted effect transform for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
const Capabilities & GetDeviceCapabilities() const
PipelineRef GetSweepGradientSSBOFillPipeline(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:125
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:164
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.
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)
void SetColors(std::vector< Color > colors)
Point Vector2
Definition: point.h:331
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:77
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
#define UNIFORM_STOP_SIZE
#define UNIFORM_COLOR_SIZE