| Flutter Impeller |

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