| Flutter Impeller |

radial_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 "radial_gradient_contents.h"
6
7 #include "impeller/entity/contents/clip_contents.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/contents/gradient_generator.h"
10 #include "impeller/entity/entity.h"
11 #include "impeller/entity/geometry/geometry.h"
12 #include "impeller/geometry/gradient.h"
13 #include "impeller/renderer/render_pass.h"
14
15 namespace impeller {
16
17 RadialGradientContents::RadialGradientContents() = default;
18
19 RadialGradientContents::~RadialGradientContents() = default;
20
21 void RadialGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
22 center_ = center;
23 radius_ = radius;
24 }
25
26 void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) {
27 tile_mode_ = tile_mode;
28 }
29
30 void RadialGradientContents::SetColors(std::vector<Color> colors) {
31 colors_ = std::move(colors);
32 }
33
34 void RadialGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36 }
37
38 const std::vector<Color>& RadialGradientContents::GetColors() const {
39 return colors_;
40 }
41
42 const std::vector<Scalar>& RadialGradientContents::GetStops() const {
43 return stops_;
44 }
45
46 bool RadialGradientContents::IsOpaque() const {
47 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
48 return false;
49 }
50 for (auto color : colors_) {
51 if (!color.IsOpaque()) {
52 return false;
53 }
54 }
55 return true;
56 }
57
58 bool RadialGradientContents::Render(const ContentContext& renderer,
59 const Entity& entity,
60 RenderPass& pass) const {
61 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
62 return RenderSSBO(renderer, entity, pass);
63 }
64 return RenderTexture(renderer, entity, pass);
65 }
66
67 bool RadialGradientContents::RenderSSBO(const ContentContext& renderer,
68 const Entity& entity,
69 RenderPass& pass) const {
70 using VS = RadialGradientSSBOFillPipeline::VertexShader;
71 using FS = RadialGradientSSBOFillPipeline::FragmentShader;
72
73 FS::FragInfo frag_info;
74 frag_info.center = center_;
75 frag_info.radius = radius_;
76 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
77 frag_info.decal_border_color = decal_border_color_;
78 frag_info.alpha = GetOpacityFactor();
79
80 auto& host_buffer = pass.GetTransientsBuffer();
81 auto colors = CreateGradientColors(colors_, stops_);
82
83 frag_info.colors_length = colors.size();
84 auto color_buffer =
85 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
86 DefaultUniformAlignment());
87
88 VS::FrameInfo frame_info;
89 frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform();
90 frame_info.matrix = GetInverseEffectTransform();
91
92 Command cmd;
93 DEBUG_COMMAND_INFO(cmd, "RadialGradientSSBOFill");
94 cmd.stencil_reference = entity.GetClipDepth();
95
96 auto geometry_result =
97 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
98 auto options = OptionsFromPassAndEntity(pass, entity);
99 if (geometry_result.prevent_overdraw) {
100 options.stencil_compare = CompareFunction::kEqual;
101 options.stencil_operation = StencilOperation::kIncrementClamp;
102 }
103 options.primitive_type = geometry_result.type;
104 cmd.pipeline = renderer.GetRadialGradientSSBOFillPipeline(options);
105
106 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
107 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
108 FS::BindColorData(cmd, color_buffer);
109 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
110
111 if (!pass.AddCommand(std::move(cmd))) {
112 return false;
113 }
114
115 if (geometry_result.prevent_overdraw) {
116 auto restore = ClipRestoreContents();
117 restore.SetRestoreCoverage(GetCoverage(entity));
118 return restore.Render(renderer, entity, pass);
119 }
120 return true;
121 }
122
123 bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
124 const Entity& entity,
125 RenderPass& pass) const {
126 using VS = RadialGradientFillPipeline::VertexShader;
127 using FS = RadialGradientFillPipeline::FragmentShader;
128
129 auto gradient_data = CreateGradientBuffer(colors_, stops_);
130 auto gradient_texture =
131 CreateGradientTexture(gradient_data, renderer.GetContext());
132 if (gradient_texture == nullptr) {
133 return false;
134 }
135
136 FS::FragInfo frag_info;
137 frag_info.center = center_;
138 frag_info.radius = radius_;
139 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
140 frag_info.decal_border_color = decal_border_color_;
141 frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
142 frag_info.alpha = GetOpacityFactor();
143 frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
144 0.5 / gradient_texture->GetSize().height);
145
146 auto geometry_result =
147 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
148
149 VS::FrameInfo frame_info;
150 frame_info.mvp = geometry_result.transform;
151 frame_info.matrix = GetInverseEffectTransform();
152
153 Command cmd;
154 DEBUG_COMMAND_INFO(cmd, "RadialGradientFill");
155 cmd.stencil_reference = entity.GetClipDepth();
156
157 auto options = OptionsFromPassAndEntity(pass, entity);
158 if (geometry_result.prevent_overdraw) {
159 options.stencil_compare = CompareFunction::kEqual;
160 options.stencil_operation = StencilOperation::kIncrementClamp;
161 }
162 options.primitive_type = geometry_result.type;
163 cmd.pipeline = renderer.GetRadialGradientFillPipeline(options);
164
165 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
166 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
167 SamplerDescriptor sampler_desc;
168 sampler_desc.min_filter = MinMagFilter::kLinear;
169 sampler_desc.mag_filter = MinMagFilter::kLinear;
170 FS::BindTextureSampler(
171 cmd, gradient_texture,
172 renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
173 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
174
175 if (!pass.AddCommand(std::move(cmd))) {
176 return false;
177 }
178
179 if (geometry_result.prevent_overdraw) {
180 auto restore = ClipRestoreContents();
181 restore.SetRestoreCoverage(GetCoverage(entity));
182 return restore.Render(renderer, entity, pass);
183 }
184 return true;
185 }
186
187 bool RadialGradientContents::ApplyColorFilter(
188 const ColorFilterProc& color_filter_proc) {
189 for (Color& color : colors_) {
190 color = color_filter_proc(color);
191 }
192 decal_border_color_ = color_filter_proc(decal_border_color_);
193 return true;
194 }
195
196 } // namespace impeller
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::RadialGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: radial_gradient_contents.cc:21
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
impeller::RadialGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: radial_gradient_contents.cc:34
entity.h
impeller::RadialGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: radial_gradient_contents.cc:42
impeller::Color
Definition: color.h:124
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::RadialGradientContents::~RadialGradientContents
~RadialGradientContents() override
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::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::RadialGradientContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: radial_gradient_contents.cc:46
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
gradient.h
impeller::RadialGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: radial_gradient_contents.cc:58
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::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::MinMagFilter::kLinear
@ kLinear
impeller::RadialGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: radial_gradient_contents.cc:38
geometry.h
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::Entity::TileMode
TileMode
Definition: entity.h:40
impeller::RadialGradientContents::RadialGradientContents
RadialGradientContents()
impeller::RadialGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: radial_gradient_contents.cc:187
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::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::RadialGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: radial_gradient_contents.cc:26
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::ContentContext::GetRadialGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetRadialGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:368
radial_gradient_contents.h
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::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::RadialGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: radial_gradient_contents.cc:30
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