| 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
5 #include "conical_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 ConicalGradientContents::ConicalGradientContents() = default;
20
21 ConicalGradientContents::~ConicalGradientContents() = default;
22
23 void ConicalGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
24 center_ = center;
25 radius_ = radius;
26 }
27
28 void ConicalGradientContents::SetTileMode(Entity::TileMode tile_mode) {
29 tile_mode_ = tile_mode;
30 }
31
32 void ConicalGradientContents::SetDither(bool dither) {
33 dither_ = dither;
34 }
35
36 void ConicalGradientContents::SetColors(std::vector<Color> colors) {
37 colors_ = std::move(colors);
38 }
39
40 void ConicalGradientContents::SetStops(std::vector<Scalar> stops) {
41 stops_ = std::move(stops);
42 }
43
44 const std::vector<Color>& ConicalGradientContents::GetColors() const {
45 return colors_;
46 }
47
48 const std::vector<Scalar>& ConicalGradientContents::GetStops() const {
49 return stops_;
50 }
51
52 void ConicalGradientContents::SetFocus(std::optional<Point> focus,
53 Scalar radius) {
54 focus_ = focus;
55 focus_radius_ = radius;
56 }
57
58 bool ConicalGradientContents::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 ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
68 const Entity& entity,
69 RenderPass& pass) const {
70 using VS = ConicalGradientSSBOFillPipeline::VertexShader;
71 using FS = ConicalGradientSSBOFillPipeline::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 frag_info.dither = dither_;
80 if (focus_) {
81 frag_info.focus = focus_.value();
82 frag_info.focus_radius = focus_radius_;
83 } else {
84 frag_info.focus = center_;
85 frag_info.focus_radius = 0.0;
86 }
87
88 auto& host_buffer = pass.GetTransientsBuffer();
89 auto colors = CreateGradientColors(colors_, stops_);
90
91 frag_info.colors_length = colors.size();
92 auto color_buffer =
93 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
94 DefaultUniformAlignment());
95
96 VS::FrameInfo frame_info;
97 frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
98 entity.GetTransformation();
99 frame_info.matrix = GetInverseEffectTransform();
100
101 Command cmd;
102 DEBUG_COMMAND_INFO(cmd, "ConicalGradientSSBOFill");
103 cmd.stencil_reference = entity.GetStencilDepth();
104
105 auto geometry_result =
106 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
107 auto options = OptionsFromPassAndEntity(pass, entity);
108 if (geometry_result.prevent_overdraw) {
109 options.stencil_compare = CompareFunction::kEqual;
110 options.stencil_operation = StencilOperation::kIncrementClamp;
111 }
112 options.primitive_type = geometry_result.type;
113 cmd.pipeline = renderer.GetConicalGradientSSBOFillPipeline(options);
114
115 cmd.BindVertices(geometry_result.vertex_buffer);
116 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
117 FS::BindColorData(cmd, color_buffer);
118 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
119
120 if (!pass.AddCommand(std::move(cmd))) {
121 return false;
122 }
123
124 if (geometry_result.prevent_overdraw) {
125 auto restore = ClipRestoreContents();
126 restore.SetRestoreCoverage(GetCoverage(entity));
127 return restore.Render(renderer, entity, pass);
128 }
129 return true;
130 }
131
132 bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
133 const Entity& entity,
134 RenderPass& pass) const {
135 using VS = ConicalGradientFillPipeline::VertexShader;
136 using FS = ConicalGradientFillPipeline::FragmentShader;
137
138 auto gradient_data = CreateGradientBuffer(colors_, stops_);
139 auto gradient_texture =
140 CreateGradientTexture(gradient_data, renderer.GetContext());
141 if (gradient_texture == nullptr) {
142 return false;
143 }
144
145 FS::FragInfo frag_info;
146 frag_info.center = center_;
147 frag_info.radius = radius_;
148 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
149 frag_info.decal_border_color = decal_border_color_;
150 frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
151 frag_info.alpha = GetOpacityFactor();
152 frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
153 0.5 / gradient_texture->GetSize().height);
154 if (focus_) {
155 frag_info.focus = focus_.value();
156 frag_info.focus_radius = focus_radius_;
157 } else {
158 frag_info.focus = center_;
159 frag_info.focus_radius = 0.0;
160 }
161
162 auto geometry_result =
163 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
164
165 VS::FrameInfo frame_info;
166 frame_info.mvp = geometry_result.transform;
167 frame_info.matrix = GetInverseEffectTransform();
168
169 Command cmd;
170 DEBUG_COMMAND_INFO(cmd, "ConicalGradientFill");
171 cmd.stencil_reference = entity.GetStencilDepth();
172
173 auto options = OptionsFromPassAndEntity(pass, entity);
174 if (geometry_result.prevent_overdraw) {
175 options.stencil_compare = CompareFunction::kEqual;
176 options.stencil_operation = StencilOperation::kIncrementClamp;
177 }
178 options.primitive_type = geometry_result.type;
179 cmd.pipeline = renderer.GetConicalGradientFillPipeline(options);
180
181 cmd.BindVertices(geometry_result.vertex_buffer);
182 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_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 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
190
191 if (!pass.AddCommand(std::move(cmd))) {
192 return false;
193 }
194
195 if (geometry_result.prevent_overdraw) {
196 auto restore = ClipRestoreContents();
197 restore.SetRestoreCoverage(GetCoverage(entity));
198 return restore.Render(renderer, entity, pass);
199 }
200 return true;
201 }
202
203 bool ConicalGradientContents::ApplyColorFilter(
204 const ColorFilterProc& color_filter_proc) {
205 for (Color& color : colors_) {
206 color = color_filter_proc(color);
207 }
208 decal_border_color_ = color_filter_proc(decal_border_color_);
209 return true;
210 }
211
212 } // 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:31
impeller::ConicalGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: conical_gradient_contents.cc:48
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
entity.h
impeller::Color
Definition: color.h:122
impeller::ConicalGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: conical_gradient_contents.cc:36
impeller::ConicalGradientContents::SetFocus
void SetFocus(std::optional< Point > focus, Scalar radius)
Definition: conical_gradient_contents.cc:52
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::ConicalGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: conical_gradient_contents.cc:23
impeller::ConicalGradientContents::ConicalGradientContents
ConicalGradientContents()
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
render_pass.h
impeller::ContentContext::GetConicalGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetConicalGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:386
conical_gradient_contents.h
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:37
impeller::MinMagFilter::kLinear
@ kLinear
impeller::ConicalGradientContents::SetDither
void SetDither(bool dither)
Definition: conical_gradient_contents.cc:32
geometry.h
impeller::ConicalGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: conical_gradient_contents.cc:58
clip_contents.h
impeller::ConicalGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: conical_gradient_contents.cc:40
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::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:27
content_context.h
impeller::ConicalGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: conical_gradient_contents.cc:44
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
Definition: point.h:20
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::ConicalGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: conical_gradient_contents.cc:28
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::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::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:203
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