| 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 {
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 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() * GetGeometry()->ComputeAlphaCoverage(entity);
99
100 auto& host_buffer = renderer.GetTransientsBuffer();
101 auto colors = CreateGradientColors(colors_, stops_);
102
103 frag_info.colors_length = colors.size();
104 auto color_buffer =
105 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
106 DefaultUniformAlignment());
107
108 pass.SetCommandLabel("SweepGradientSSBOFill");
109
110 FS::BindFragInfo(
111 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
112 FS::BindColorData(pass, color_buffer);
113
114 return true;
115 });
116 }
117
118 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
119 const Entity& entity,
120 RenderPass& pass) const {
121 using VS = SweepGradientFillPipeline::VertexShader;
122 using FS = SweepGradientFillPipeline::FragmentShader;
123
124 auto gradient_data = CreateGradientBuffer(colors_, stops_);
125 auto gradient_texture =
126 CreateGradientTexture(gradient_data, renderer.GetContext());
127 if (gradient_texture == nullptr) {
128 return false;
129 }
130
131 VS::FrameInfo frame_info;
132 frame_info.matrix = GetInverseEffectTransform();
133
134 PipelineBuilderCallback pipeline_callback =
135 [&renderer](ContentContextOptions options) {
136 return renderer.GetSweepGradientFillPipeline(options);
137 };
138 return ColorSourceContents::DrawGeometry<VS>(
139 renderer, entity, pass, pipeline_callback, frame_info,
140 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
141 FS::FragInfo frag_info;
142 frag_info.center = center_;
143 frag_info.bias = bias_;
144 frag_info.scale = scale_;
145 frag_info.texture_sampler_y_coord_scale =
146 gradient_texture->GetYCoordScale();
147 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
148 frag_info.decal_border_color = decal_border_color_;
149 frag_info.alpha =
150 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
151 frag_info.half_texel =
152 Vector2(0.5 / gradient_texture->GetSize().width,
153 0.5 / gradient_texture->GetSize().height);
154
155 SamplerDescriptor sampler_desc;
156 sampler_desc.min_filter = MinMagFilter::kLinear;
157 sampler_desc.mag_filter = MinMagFilter::kLinear;
158
159 pass.SetCommandLabel("SweepGradientFill");
160
161 FS::BindFragInfo(
162 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
163 FS::BindTextureSampler(
164 pass, gradient_texture,
165 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
166 sampler_desc));
167
168 return true;
169 });
170 }
171
172 bool SweepGradientContents::ApplyColorFilter(
173 const ColorFilterProc& color_filter_proc) {
174 for (Color& color : colors_) {
175 color = color_filter_proc(color);
176 }
177 decal_border_color_ = color_filter_proc(decal_border_color_);
178 return true;
179 }
180
181 } // 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:172
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:114
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
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: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:124
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Vector2
Point Vector2
Definition: point.h:326
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: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:20
impeller::ContentContext::GetSweepGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:412
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
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::SweepGradientContents::IsOpaque
bool IsOpaque() 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
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:557
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:46
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
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::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: aiks_blend_unittests.cc:18
impeller::ContentContext
Definition: content_context.h:366
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:752