Flutter Impeller
linear_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 
11 #include "impeller/entity/entity.h"
13 
14 namespace impeller {
15 
17 
19 
20 void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) {
21  start_point_ = start_point;
22  end_point_ = end_point;
23 }
24 
25 void LinearGradientContents::SetColors(std::vector<Color> colors) {
26  colors_ = std::move(colors);
27 }
28 
29 void LinearGradientContents::SetStops(std::vector<Scalar> stops) {
30  stops_ = std::move(stops);
31 }
32 
33 const std::vector<Color>& LinearGradientContents::GetColors() const {
34  return colors_;
35 }
36 
37 const std::vector<Scalar>& LinearGradientContents::GetStops() const {
38  return stops_;
39 }
40 
42  tile_mode_ = tile_mode;
43 }
44 
46  if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
47  return false;
48  }
49  for (auto color : colors_) {
50  if (!color.IsOpaque()) {
51  return false;
52  }
53  }
54  return true;
55 }
56 
58  const Entity& entity,
59  RenderPass& pass) const {
60  if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
61  return RenderSSBO(renderer, entity, pass);
62  }
63  return RenderTexture(renderer, entity, pass);
64 }
65 
66 bool LinearGradientContents::RenderTexture(const ContentContext& renderer,
67  const Entity& entity,
68  RenderPass& pass) const {
71 
72  auto gradient_data = CreateGradientBuffer(colors_, stops_);
73  auto gradient_texture =
74  CreateGradientTexture(gradient_data, renderer.GetContext());
75  if (gradient_texture == nullptr) {
76  return false;
77  }
78 
79  FS::FragInfo frag_info;
80  frag_info.start_point = start_point_;
81  frag_info.end_point = end_point_;
82  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
83  frag_info.decal_border_color = decal_border_color_;
84  frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
85  frag_info.alpha = GetOpacityFactor();
86  frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
87  0.5 / gradient_texture->GetSize().height);
88 
89  auto geometry_result =
90  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
91 
92  VS::FrameInfo frame_info;
93  frame_info.mvp = geometry_result.transform;
94  frame_info.matrix = GetInverseEffectTransform();
95 
96  Command cmd;
97  DEBUG_COMMAND_INFO(cmd, "LinearGradientFill");
98  cmd.stencil_reference = entity.GetClipDepth();
99 
100  auto options = OptionsFromPassAndEntity(pass, entity);
101  if (geometry_result.prevent_overdraw) {
102  options.stencil_compare = CompareFunction::kEqual;
103  options.stencil_operation = StencilOperation::kIncrementClamp;
104  }
105  options.primitive_type = geometry_result.type;
106  cmd.pipeline = renderer.GetLinearGradientFillPipeline(options);
107 
108  cmd.BindVertices(std::move(geometry_result.vertex_buffer));
109  FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
110  SamplerDescriptor sampler_desc;
111  sampler_desc.min_filter = MinMagFilter::kLinear;
112  sampler_desc.mag_filter = MinMagFilter::kLinear;
113  FS::BindTextureSampler(
114  cmd, std::move(gradient_texture),
115  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
116  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
117 
118  if (!pass.AddCommand(std::move(cmd))) {
119  return false;
120  }
121 
122  if (geometry_result.prevent_overdraw) {
123  auto restore = ClipRestoreContents();
124  restore.SetRestoreCoverage(GetCoverage(entity));
125  return restore.Render(renderer, entity, pass);
126  }
127  return true;
128 }
129 
130 bool LinearGradientContents::RenderSSBO(const ContentContext& renderer,
131  const Entity& entity,
132  RenderPass& pass) const {
135 
136  FS::FragInfo frag_info;
137  frag_info.start_point = start_point_;
138  frag_info.end_point = end_point_;
139  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
140  frag_info.decal_border_color = decal_border_color_;
141  frag_info.alpha = GetOpacityFactor();
142 
143  auto& host_buffer = pass.GetTransientsBuffer();
144  auto colors = CreateGradientColors(colors_, stops_);
145 
146  frag_info.colors_length = colors.size();
147  auto color_buffer =
148  host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
150 
151  VS::FrameInfo frame_info;
152  frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform();
153  frame_info.matrix = GetInverseEffectTransform();
154 
155  Command cmd;
156  DEBUG_COMMAND_INFO(cmd, "LinearGradientSSBOFill");
157  cmd.stencil_reference = entity.GetClipDepth();
158 
159  auto geometry_result =
160  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
161  auto options = OptionsFromPassAndEntity(pass, entity);
162  if (geometry_result.prevent_overdraw) {
163  options.stencil_compare = CompareFunction::kEqual;
164  options.stencil_operation = StencilOperation::kIncrementClamp;
165  }
166  options.primitive_type = geometry_result.type;
167  cmd.pipeline = renderer.GetLinearGradientSSBOFillPipeline(options);
168 
169  cmd.BindVertices(std::move(geometry_result.vertex_buffer));
170  FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
171  FS::BindColorData(cmd, color_buffer);
172  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
173 
174  if (!pass.AddCommand(std::move(cmd))) {
175  return false;
176  }
177 
178  if (geometry_result.prevent_overdraw) {
179  auto restore = ClipRestoreContents();
180  restore.SetRestoreCoverage(GetCoverage(entity));
181  return restore.Render(renderer, entity, pass);
182  }
183  return true;
184 }
185 
187  const ColorFilterProc& color_filter_proc) {
188  for (Color& color : colors_) {
189  color = color_filter_proc(color);
190  }
191  decal_border_color_ = color_filter_proc(decal_border_color_);
192  return true;
193 }
194 
195 } // namespace impeller
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::ContentContext::GetLinearGradientFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetLinearGradientFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:356
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
entity.h
impeller::Color
Definition: color.h:124
impeller::LinearGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: linear_gradient_contents.cc:186
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::LinearGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: linear_gradient_contents.cc:57
formats.h
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::LinearGradientContents::SetEndPoints
void SetEndPoints(Point start_point, Point end_point)
Definition: linear_gradient_contents.cc:20
impeller::LinearGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: linear_gradient_contents.cc:25
impeller::LinearGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: linear_gradient_contents.cc:41
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
render_pass.h
impeller::LinearGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: linear_gradient_contents.cc:33
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:479
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::MinMagFilter::kLinear
@ kLinear
clip_contents.h
impeller::LinearGradientContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: linear_gradient_contents.cc:45
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::Entity::TileMode
TileMode
Definition: entity.h:40
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
content_context.h
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:483
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: color_source_contents.cc:45
impeller::LinearGradientContents::LinearGradientContents
LinearGradientContents()
impeller::TPoint< Scalar >
impeller::LinearGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: linear_gradient_contents.cc:29
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:41
impeller::LinearGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: linear_gradient_contents.cc:37
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:45
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::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller::Capabilities::SupportsSSBO
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:67
impeller
Definition: aiks_context.cc:10
impeller::LinearGradientContents::~LinearGradientContents
~LinearGradientContents() override
impeller::ContentContext
Definition: content_context.h:332
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:37
linear_gradient_contents.h