| 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 "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/entity/geometry/geometry.h"
13 #include "impeller/geometry/gradient.h"
14 #include "impeller/renderer/render_pass.h"
15 #include "impeller/renderer/sampler_library.h"
16
17 namespace impeller {
18
19 RadialGradientContents::RadialGradientContents() = default;
20
21 RadialGradientContents::~RadialGradientContents() = default;
22
23 void RadialGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
24 center_ = center;
25 radius_ = radius;
26 }
27
28 void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) {
29 tile_mode_ = tile_mode;
30 }
31
32 void RadialGradientContents::SetDither(bool dither) {
33 dither_ = dither;
34 }
35
36 void RadialGradientContents::SetColors(std::vector<Color> colors) {
37 colors_ = std::move(colors);
38 }
39
40 void RadialGradientContents::SetStops(std::vector<Scalar> stops) {
41 stops_ = std::move(stops);
42 }
43
44 const std::vector<Color>& RadialGradientContents::GetColors() const {
45 return colors_;
46 }
47
48 const std::vector<Scalar>& RadialGradientContents::GetStops() const {
49 return stops_;
50 }
51
52 bool RadialGradientContents::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 RadialGradientContents::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 RadialGradientContents::RenderSSBO(const ContentContext& renderer,
74 const Entity& entity,
75 RenderPass& pass) const {
76 using VS = RadialGradientSSBOFillPipeline::VertexShader;
77 using FS = RadialGradientSSBOFillPipeline::FragmentShader;
78
79 FS::FragInfo frag_info;
80 frag_info.center = center_;
81 frag_info.radius = radius_;
82 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
83 frag_info.decal_border_color = decal_border_color_;
84 frag_info.alpha = GetOpacityFactor();
85 frag_info.dither = dither_;
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 = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
97 entity.GetTransformation();
98 frame_info.matrix = GetInverseEffectTransform();
99
100 Command cmd;
101 DEBUG_COMMAND_INFO(cmd, "RadialGradientSSBOFill");
102 cmd.stencil_reference = entity.GetStencilDepth();
103
104 auto geometry_result =
105 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
106 auto options = OptionsFromPassAndEntity(pass, entity);
107 if (geometry_result.prevent_overdraw) {
108 options.stencil_compare = CompareFunction::kEqual;
109 options.stencil_operation = StencilOperation::kIncrementClamp;
110 }
111 options.primitive_type = geometry_result.type;
112 cmd.pipeline = renderer.GetRadialGradientSSBOFillPipeline(options);
113
114 cmd.BindVertices(geometry_result.vertex_buffer);
115 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
116 FS::BindColorData(cmd, color_buffer);
117 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
118
119 if (!pass.AddCommand(std::move(cmd))) {
120 return false;
121 }
122
123 if (geometry_result.prevent_overdraw) {
124 auto restore = ClipRestoreContents();
125 restore.SetRestoreCoverage(GetCoverage(entity));
126 return restore.Render(renderer, entity, pass);
127 }
128 return true;
129 }
130
131 bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
132 const Entity& entity,
133 RenderPass& pass) const {
134 using VS = RadialGradientFillPipeline::VertexShader;
135 using FS = RadialGradientFillPipeline::FragmentShader;
136
137 auto gradient_data = CreateGradientBuffer(colors_, stops_);
138 auto gradient_texture =
139 CreateGradientTexture(gradient_data, renderer.GetContext());
140 if (gradient_texture == nullptr) {
141 return false;
142 }
143
144 FS::FragInfo frag_info;
145 frag_info.center = center_;
146 frag_info.radius = radius_;
147 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
148 frag_info.decal_border_color = decal_border_color_;
149 frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
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, "RadialGradientFill");
163 cmd.stencil_reference = entity.GetStencilDepth();
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.GetRadialGradientFillPipeline(options);
172
173 cmd.BindVertices(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
195 bool RadialGradientContents::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::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::RadialGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: radial_gradient_contents.cc:23
impeller::Entity::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:89
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::RadialGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: radial_gradient_contents.cc:40
entity.h
impeller::RadialGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: radial_gradient_contents.cc:48
impeller::Color
Definition: color.h:122
impeller::RadialGradientContents::~RadialGradientContents
~RadialGradientContents() override
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Vector2
Point Vector2
Definition: point.h:310
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::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:52
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
gradient.h
impeller::RadialGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: radial_gradient_contents.cc:64
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::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:37
impeller::MinMagFilter::kLinear
@ kLinear
impeller::RadialGradientContents::SetDither
void SetDither(bool dither)
Definition: radial_gradient_contents.cc:32
impeller::RadialGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: radial_gradient_contents.cc:44
geometry.h
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::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
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:195
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
content_context.h
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::RadialGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: radial_gradient_contents.cc:28
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::ContentContext::GetRadialGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetRadialGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:380
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
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::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::RadialGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: radial_gradient_contents.cc:36
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