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 
53  const Entity& entity,
54  RenderPass& pass) const {
55  if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
56  return RenderSSBO(renderer, entity, pass);
57  }
58  return RenderTexture(renderer, entity, pass);
59 }
60 
61 bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
62  const Entity& entity,
63  RenderPass& pass) const {
66 
67  FS::FragInfo frag_info;
68  frag_info.center = center_;
69  frag_info.radius = radius_;
70  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
71  frag_info.decal_border_color = decal_border_color_;
72  frag_info.alpha = GetOpacityFactor();
73  if (focus_) {
74  frag_info.focus = focus_.value();
75  frag_info.focus_radius = focus_radius_;
76  } else {
77  frag_info.focus = center_;
78  frag_info.focus_radius = 0.0;
79  }
80 
81  auto& host_buffer = pass.GetTransientsBuffer();
82  auto colors = CreateGradientColors(colors_, stops_);
83 
84  frag_info.colors_length = colors.size();
85  auto color_buffer =
86  host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
88 
89  VS::FrameInfo frame_info;
90  frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform();
91  frame_info.matrix = GetInverseEffectTransform();
92 
93  Command cmd;
94  DEBUG_COMMAND_INFO(cmd, "ConicalGradientSSBOFill");
95  cmd.stencil_reference = entity.GetClipDepth();
96 
97  auto geometry_result =
98  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
99  auto options = OptionsFromPassAndEntity(pass, entity);
100  if (geometry_result.prevent_overdraw) {
101  options.stencil_compare = CompareFunction::kEqual;
102  options.stencil_operation = StencilOperation::kIncrementClamp;
103  }
104  options.primitive_type = geometry_result.type;
105  cmd.pipeline = renderer.GetConicalGradientSSBOFillPipeline(options);
106 
107  cmd.BindVertices(std::move(geometry_result.vertex_buffer));
108  FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
109  FS::BindColorData(cmd, color_buffer);
110  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
111 
112  if (!pass.AddCommand(std::move(cmd))) {
113  return false;
114  }
115 
116  if (geometry_result.prevent_overdraw) {
117  auto restore = ClipRestoreContents();
118  restore.SetRestoreCoverage(GetCoverage(entity));
119  return restore.Render(renderer, entity, pass);
120  }
121  return true;
122 }
123 
124 bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
125  const Entity& entity,
126  RenderPass& pass) const {
129 
130  auto gradient_data = CreateGradientBuffer(colors_, stops_);
131  auto gradient_texture =
132  CreateGradientTexture(gradient_data, renderer.GetContext());
133  if (gradient_texture == nullptr) {
134  return false;
135  }
136 
137  FS::FragInfo frag_info;
138  frag_info.center = center_;
139  frag_info.radius = radius_;
140  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
141  frag_info.decal_border_color = decal_border_color_;
142  frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
143  frag_info.alpha = GetOpacityFactor();
144  frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
145  0.5 / gradient_texture->GetSize().height);
146  if (focus_) {
147  frag_info.focus = focus_.value();
148  frag_info.focus_radius = focus_radius_;
149  } else {
150  frag_info.focus = center_;
151  frag_info.focus_radius = 0.0;
152  }
153 
154  auto geometry_result =
155  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
156 
157  VS::FrameInfo frame_info;
158  frame_info.mvp = geometry_result.transform;
159  frame_info.matrix = GetInverseEffectTransform();
160 
161  Command cmd;
162  DEBUG_COMMAND_INFO(cmd, "ConicalGradientFill");
163  cmd.stencil_reference = entity.GetClipDepth();
164 
165  auto options = OptionsFromPassAndEntity(pass, entity);
166  if (geometry_result.prevent_overdraw) {
167  options.stencil_compare = CompareFunction::kEqual;
168  options.stencil_operation = StencilOperation::kIncrementClamp;
169  }
170  options.primitive_type = geometry_result.type;
171  cmd.pipeline = renderer.GetConicalGradientFillPipeline(options);
172 
173  cmd.BindVertices(std::move(geometry_result.vertex_buffer));
174  FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
175  SamplerDescriptor sampler_desc;
176  sampler_desc.min_filter = MinMagFilter::kLinear;
177  sampler_desc.mag_filter = MinMagFilter::kLinear;
178  FS::BindTextureSampler(
179  cmd, gradient_texture,
180  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
181  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
182 
183  if (!pass.AddCommand(std::move(cmd))) {
184  return false;
185  }
186 
187  if (geometry_result.prevent_overdraw) {
188  auto restore = ClipRestoreContents();
189  restore.SetRestoreCoverage(GetCoverage(entity));
190  return restore.Render(renderer, entity, pass);
191  }
192  return true;
193 }
194 
196  const ColorFilterProc& color_filter_proc) {
197  for (Color& color : colors_) {
198  color = color_filter_proc(color);
199  }
200  decal_border_color_ = color_filter_proc(decal_border_color_);
201  return true;
202 }
203 
204 } // 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::ConicalGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: conical_gradient_contents.cc:42
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
entity.h
impeller::Color
Definition: color.h:124
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::ConicalGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: conical_gradient_contents.cc:30
impeller::ConicalGradientContents::SetFocus
void SetFocus(std::optional< Point > focus, Scalar radius)
Definition: conical_gradient_contents.cc:46
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::ConicalGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: conical_gradient_contents.cc:21
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
impeller::ConicalGradientContents::ConicalGradientContents
ConicalGradientContents()
gradient.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
render_pass.h
impeller::ContentContext::GetConicalGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetConicalGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:374
conical_gradient_contents.h
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::MinMagFilter::kLinear
@ kLinear
geometry.h
impeller::ConicalGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: conical_gradient_contents.cc:52
clip_contents.h
impeller::ConicalGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: conical_gradient_contents.cc:34
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::ConicalGradientContents::~ConicalGradientContents
~ConicalGradientContents() override
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::ConicalGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: conical_gradient_contents.cc:38
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::TPoint
Definition: point.h:22
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::ConicalGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: conical_gradient_contents.cc:26
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::ConicalGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: conical_gradient_contents.cc:195
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