| 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
5 #include "sweep_gradient_contents.h"
6
7 #include "flutter/fml/logging.h"
8 #include "impeller/entity/contents/clip_contents.h"
9 #include "impeller/entity/contents/content_context.h"
10 #include "impeller/entity/contents/gradient_generator.h"
11 #include "impeller/entity/entity.h"
12 #include "impeller/geometry/gradient.h"
13 #include "impeller/renderer/render_pass.h"
14 #include "impeller/renderer/sampler_library.h"
15
16 namespace impeller {
17
18 SweepGradientContents::SweepGradientContents() = default;
19
20 SweepGradientContents::~SweepGradientContents() = default;
21
22 void SweepGradientContents::SetCenterAndAngles(Point center,
23 Degrees start_angle,
24 Degrees end_angle) {
25 center_ = center;
26 Scalar t0 = start_angle.degrees / 360;
27 Scalar t1 = end_angle.degrees / 360;
28 FML_DCHECK(t0 < t1);
29 bias_ = -t0;
30 scale_ = 1 / (t1 - t0);
31 }
32
33 void SweepGradientContents::SetColors(std::vector<Color> colors) {
34 colors_ = std::move(colors);
35 }
36
37 void SweepGradientContents::SetStops(std::vector<Scalar> stops) {
38 stops_ = std::move(stops);
39 }
40
41 void SweepGradientContents::SetTileMode(Entity::TileMode tile_mode) {
42 tile_mode_ = tile_mode;
43 }
44
45 void SweepGradientContents::SetDither(bool dither) {
46 dither_ = dither;
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
57 bool SweepGradientContents::IsOpaque() const {
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 }
66 return true;
67 }
68
69 bool SweepGradientContents::Render(const ContentContext& renderer,
70 const Entity& entity,
71 RenderPass& pass) const {
72 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
73 return RenderSSBO(renderer, entity, pass);
74 }
75 return RenderTexture(renderer, entity, pass);
76 }
77
78 bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
79 const Entity& entity,
80 RenderPass& pass) const {
81 using VS = SweepGradientSSBOFillPipeline::VertexShader;
82 using FS = SweepGradientSSBOFillPipeline::FragmentShader;
83
84 FS::FragInfo frag_info;
85 frag_info.center = center_;
86 frag_info.bias = bias_;
87 frag_info.scale = scale_;
88 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
89 frag_info.decal_border_color = decal_border_color_;
90 frag_info.alpha = GetOpacityFactor();
91 frag_info.dither = dither_;
92
93 auto& host_buffer = pass.GetTransientsBuffer();
94 auto colors = CreateGradientColors(colors_, stops_);
95
96 frag_info.colors_length = colors.size();
97 auto color_buffer =
98 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
99 DefaultUniformAlignment());
100
101 VS::FrameInfo frame_info;
102 frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
103 entity.GetTransformation();
104 frame_info.matrix = GetInverseEffectTransform();
105
106 Command cmd;
107 DEBUG_COMMAND_INFO(cmd, "SweepGradientSSBOFill");
108 cmd.stencil_reference = entity.GetStencilDepth();
109 auto geometry_result =
110 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
111
112 auto options = OptionsFromPassAndEntity(pass, entity);
113 if (geometry_result.prevent_overdraw) {
114 options.stencil_compare = CompareFunction::kEqual;
115 options.stencil_operation = StencilOperation::kIncrementClamp;
116 }
117 options.primitive_type = geometry_result.type;
118 cmd.pipeline = renderer.GetSweepGradientSSBOFillPipeline(options);
119
120 cmd.BindVertices(geometry_result.vertex_buffer);
121 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
122 FS::BindColorData(cmd, color_buffer);
123 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
124
125 if (!pass.AddCommand(std::move(cmd))) {
126 return false;
127 }
128
129 if (geometry_result.prevent_overdraw) {
130 auto restore = ClipRestoreContents();
131 restore.SetRestoreCoverage(GetCoverage(entity));
132 return restore.Render(renderer, entity, pass);
133 }
134 return true;
135 }
136
137 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
138 const Entity& entity,
139 RenderPass& pass) const {
140 using VS = SweepGradientFillPipeline::VertexShader;
141 using FS = SweepGradientFillPipeline::FragmentShader;
142
143 auto gradient_data = CreateGradientBuffer(colors_, stops_);
144 auto gradient_texture =
145 CreateGradientTexture(gradient_data, renderer.GetContext());
146 if (gradient_texture == nullptr) {
147 return false;
148 }
149
150 FS::FragInfo frag_info;
151 frag_info.center = center_;
152 frag_info.bias = bias_;
153 frag_info.scale = scale_;
154 frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
155 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
156 frag_info.decal_border_color = decal_border_color_;
157 frag_info.alpha = GetOpacityFactor();
158 frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
159 0.5 / gradient_texture->GetSize().height);
160
161 auto geometry_result =
162 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
163
164 VS::FrameInfo frame_info;
165 frame_info.mvp = geometry_result.transform;
166 frame_info.matrix = GetInverseEffectTransform();
167
168 Command cmd;
169 DEBUG_COMMAND_INFO(cmd, "SweepGradientFill");
170 cmd.stencil_reference = entity.GetStencilDepth();
171
172 auto options = OptionsFromPassAndEntity(pass, entity);
173 if (geometry_result.prevent_overdraw) {
174 options.stencil_compare = CompareFunction::kEqual;
175 options.stencil_operation = StencilOperation::kIncrementClamp;
176 }
177 options.primitive_type = geometry_result.type;
178 cmd.pipeline = renderer.GetSweepGradientFillPipeline(options);
179
180 cmd.BindVertices(geometry_result.vertex_buffer);
181 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
182 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
183 SamplerDescriptor sampler_desc;
184 sampler_desc.min_filter = MinMagFilter::kLinear;
185 sampler_desc.mag_filter = MinMagFilter::kLinear;
186 FS::BindTextureSampler(
187 cmd, gradient_texture,
188 renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
189
190 if (!pass.AddCommand(std::move(cmd))) {
191 return false;
192 }
193
194 if (geometry_result.prevent_overdraw) {
195 auto restore = ClipRestoreContents();
196 restore.SetRestoreCoverage(GetCoverage(entity));
197 return restore.Render(renderer, entity, pass);
198 }
199 return true;
200 }
201
202 bool SweepGradientContents::ApplyColorFilter(
203 const ColorFilterProc& color_filter_proc) {
204 for (Color& color : colors_) {
205 color = color_filter_proc(color);
206 }
207 decal_border_color_ = color_filter_proc(decal_border_color_);
208 return true;
209 }
210
211 } // namespace impeller
impeller::SweepGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: sweep_gradient_contents.cc:202
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:99
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::SweepGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: sweep_gradient_contents.cc:41
impeller::Entity::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:93
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
sampler_library.h
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:44
entity.h
impeller::SweepGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: sweep_gradient_contents.cc:33
impeller::SweepGradientContents::SetCenterAndAngles
void SetCenterAndAngles(Point center, Degrees start_angle, Degrees end_angle)
Definition: sweep_gradient_contents.cc:22
impeller::Color
Definition: color.h:122
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::SweepGradientContents::~SweepGradientContents
~SweepGradientContents() override
impeller::SweepGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: sweep_gradient_contents.cc:53
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:91
impeller::Color::alpha
Scalar alpha
Definition: color.h:141
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::ContentContext::GetSweepGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:392
sweep_gradient_contents.h
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
gradient.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:30
impeller::StopData
Definition: gradient_generator.h:31
render_pass.h
impeller::SweepGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: sweep_gradient_contents.cc:37
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:37
impeller::MinMagFilter::kLinear
@ kLinear
clip_contents.h
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::SweepGradientContents::SweepGradientContents
SweepGradientContents()
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::Entity::TileMode
TileMode
Definition: entity.h:40
impeller::SweepGradientContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: sweep_gradient_contents.cc:57
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::SweepGradientContents::SetDither
void SetDither(bool dither)
Definition: sweep_gradient_contents.cc:45
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
content_context.h
impeller::SweepGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: sweep_gradient_contents.cc:69
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:444
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: color_source_contents.cc:45
impeller::TPoint< Scalar >
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:43
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::Degrees
Definition: scalar.h:43
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
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::SweepGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: sweep_gradient_contents.cc:49
impeller::Command::BindVertices
bool BindVertices(const VertexBuffer &buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:103
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:90
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:46
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:37