| 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
15 namespace impeller {
16
17 SweepGradientContents::SweepGradientContents() = default;
18
19 SweepGradientContents::~SweepGradientContents() = default;
20
21 void SweepGradientContents::SetCenterAndAngles(Point center,
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
40 void SweepGradientContents::SetTileMode(Entity::TileMode tile_mode) {
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
52 bool SweepGradientContents::IsOpaque() const {
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 }
61 return true;
62 }
63
64 bool SweepGradientContents::Render(const ContentContext& renderer,
65 const Entity& entity,
66 RenderPass& pass) const {
67 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
68 return RenderSSBO(renderer, entity, pass);
69 }
70 return RenderTexture(renderer, entity, pass);
71 }
72
73 bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
74 const Entity& entity,
75 RenderPass& pass) const {
76 using VS = SweepGradientSSBOFillPipeline::VertexShader;
77 using FS = SweepGradientSSBOFillPipeline::FragmentShader;
78
79 FS::FragInfo frag_info;
80 frag_info.center = center_;
81 frag_info.bias = bias_;
82 frag_info.scale = scale_;
83 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
84 frag_info.decal_border_color = decal_border_color_;
85 frag_info.alpha = GetOpacityFactor();
86
87 auto& host_buffer = pass.GetTransientsBuffer();
88 auto colors = CreateGradientColors(colors_, stops_);
89
90 frag_info.colors_length = colors.size();
91 auto color_buffer =
92 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
93 DefaultUniformAlignment());
94
95 VS::FrameInfo frame_info;
96 frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform();
97 frame_info.matrix = GetInverseEffectTransform();
98
99 Command cmd;
100 DEBUG_COMMAND_INFO(cmd, "SweepGradientSSBOFill");
101 cmd.stencil_reference = entity.GetClipDepth();
102 auto geometry_result =
103 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
104
105 auto options = OptionsFromPassAndEntity(pass, entity);
106 if (geometry_result.prevent_overdraw) {
107 options.stencil_compare = CompareFunction::kEqual;
108 options.stencil_operation = StencilOperation::kIncrementClamp;
109 }
110 options.primitive_type = geometry_result.type;
111 cmd.pipeline = renderer.GetSweepGradientSSBOFillPipeline(options);
112
113 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
114 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
115 FS::BindColorData(cmd, color_buffer);
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 SweepGradientContents::RenderTexture(const ContentContext& renderer,
131 const Entity& entity,
132 RenderPass& pass) const {
133 using VS = SweepGradientFillPipeline::VertexShader;
134 using FS = SweepGradientFillPipeline::FragmentShader;
135
136 auto gradient_data = CreateGradientBuffer(colors_, stops_);
137 auto gradient_texture =
138 CreateGradientTexture(gradient_data, renderer.GetContext());
139 if (gradient_texture == nullptr) {
140 return false;
141 }
142
143 FS::FragInfo frag_info;
144 frag_info.center = center_;
145 frag_info.bias = bias_;
146 frag_info.scale = scale_;
147 frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
148 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
149 frag_info.decal_border_color = decal_border_color_;
150 frag_info.alpha = GetOpacityFactor();
151 frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
152 0.5 / gradient_texture->GetSize().height);
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, "SweepGradientFill");
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.GetSweepGradientFillPipeline(options);
172
173 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
174 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
175 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
176 SamplerDescriptor sampler_desc;
177 sampler_desc.min_filter = MinMagFilter::kLinear;
178 sampler_desc.mag_filter = MinMagFilter::kLinear;
179 FS::BindTextureSampler(
180 cmd, gradient_texture,
181 renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
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
195 bool SweepGradientContents::ApplyColorFilter(
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::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:195
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:92
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::SweepGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: sweep_gradient_contents.cc:40
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:47
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
entity.h
impeller::SweepGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: sweep_gradient_contents.cc:32
impeller::SweepGradientContents::SetCenterAndAngles
void SetCenterAndAngles(Point center, Degrees start_angle, Degrees end_angle)
Definition: sweep_gradient_contents.cc:21
impeller::Color
Definition: color.h:124
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::Entity::TileMode::kDecal
@ kDecal
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::SweepGradientContents::~SweepGradientContents
~SweepGradientContents() override
impeller::SweepGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: sweep_gradient_contents.cc:48
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::ContentContext::GetSweepGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:380
sweep_gradient_contents.h
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
gradient.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::StopData
Definition: gradient_generator.h:32
render_pass.h
impeller::SweepGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: sweep_gradient_contents.cc:36
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
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::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:52
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
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::SweepGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: sweep_gradient_contents.cc:64
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< 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:41
impeller::Degrees
Definition: scalar.h:46
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:44
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
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::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