| 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 Matrix& transform) 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 !AppliesAlphaForStrokeCoverage(transform);
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 VS::FrameInfo frame_info;
80 frame_info.matrix = GetInverseEffectTransform();
81 VS::BindFrameInfo(pass,
82 renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
83
84 PipelineBuilderCallback pipeline_callback =
85 [&renderer](ContentContextOptions options) {
86 return renderer.GetSweepGradientSSBOFillPipeline(options);
87 };
88 return ColorSourceContents::DrawGeometry<VS>(
89 renderer, entity, pass, pipeline_callback, frame_info,
90 [this, &renderer, &entity](RenderPass& pass) {
91 FS::FragInfo frag_info;
92 frag_info.center = center_;
93 frag_info.bias = bias_;
94 frag_info.scale = scale_;
95 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
96 frag_info.decal_border_color = decal_border_color_;
97 frag_info.alpha =
98 GetOpacityFactor() *
99 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
100
101 auto& host_buffer = renderer.GetTransientsBuffer();
102 auto colors = CreateGradientColors(colors_, stops_);
103
104 frag_info.colors_length = colors.size();
105 auto color_buffer =
106 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
107 DefaultUniformAlignment());
108
109 pass.SetCommandLabel("SweepGradientSSBOFill");
110
111 FS::BindFragInfo(
112 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
113 FS::BindColorData(pass, color_buffer);
114
115 return true;
116 });
117 }
118
119 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
120 const Entity& entity,
121 RenderPass& pass) const {
122 using VS = SweepGradientFillPipeline::VertexShader;
123 using FS = SweepGradientFillPipeline::FragmentShader;
124
125 auto gradient_data = CreateGradientBuffer(colors_, stops_);
126 auto gradient_texture =
127 CreateGradientTexture(gradient_data, renderer.GetContext());
128 if (gradient_texture == nullptr) {
129 return false;
130 }
131
132 VS::FrameInfo frame_info;
133 frame_info.matrix = GetInverseEffectTransform();
134
135 PipelineBuilderCallback pipeline_callback =
136 [&renderer](ContentContextOptions options) {
137 return renderer.GetSweepGradientFillPipeline(options);
138 };
139 return ColorSourceContents::DrawGeometry<VS>(
140 renderer, entity, pass, pipeline_callback, frame_info,
141 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
142 FS::FragInfo frag_info;
143 frag_info.center = center_;
144 frag_info.bias = bias_;
145 frag_info.scale = scale_;
146 frag_info.texture_sampler_y_coord_scale =
147 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 =
151 GetOpacityFactor() *
152 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
153 frag_info.half_texel =
154 Vector2(0.5 / gradient_texture->GetSize().width,
155 0.5 / gradient_texture->GetSize().height);
156
157 SamplerDescriptor sampler_desc;
158 sampler_desc.min_filter = MinMagFilter::kLinear;
159 sampler_desc.mag_filter = MinMagFilter::kLinear;
160
161 pass.SetCommandLabel("SweepGradientFill");
162
163 FS::BindFragInfo(
164 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
165 FS::BindTextureSampler(
166 pass, gradient_texture,
167 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
168 sampler_desc));
169
170 return true;
171 });
172 }
173
174 bool SweepGradientContents::ApplyColorFilter(
175 const ColorFilterProc& color_filter_proc) {
176 for (Color& color : colors_) {
177 color = color_filter_proc(color);
178 }
179 decal_border_color_ = color_filter_proc(decal_border_color_);
180 return true;
181 }
182
183 } // 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:174
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:108
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::SweepGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: sweep_gradient_contents.cc:40
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:124
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::Degrees::degrees
Scalar degrees
Definition: scalar.h:52
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
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:123
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::SweepGradientContents::~SweepGradientContents
~SweepGradientContents() override
impeller::SweepGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: sweep_gradient_contents.cc:48
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
impeller::ContentContext::GetSweepGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:408
sweep_gradient_contents.h
gradient.h
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::Entity
Definition: entity.h:20
render_pass.h
impeller::SweepGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: sweep_gradient_contents.cc:36
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::MinMagFilter::kLinear
@ kLinear
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
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:42
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
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:554
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:50
impeller::Degrees
Definition: scalar.h:51
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
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:53
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:16
impeller::SweepGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: sweep_gradient_contents.cc:44
impeller::ColorSourceContents::AppliesAlphaForStrokeCoverage
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
Definition: color_source_contents.cc:53
impeller::ContentContextOptions
Definition: content_context.h:254
impeller::Capabilities::SupportsSSBO
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
impeller
Definition: allocation.cc:12
impeller::ContentContext
Definition: content_context.h:366
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:36
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
impeller::SweepGradientContents::IsOpaque
bool IsOpaque(const Matrix &transform) 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