| 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](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 = GetOpacityFactor();
98
99 auto& host_buffer = renderer.GetTransientsBuffer();
100 auto colors = CreateGradientColors(colors_, stops_);
101
102 frag_info.colors_length = colors.size();
103 auto color_buffer =
104 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
105 DefaultUniformAlignment());
106
107 pass.SetCommandLabel("SweepGradientSSBOFill");
108
109 FS::BindFragInfo(
110 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
111 FS::BindColorData(pass, color_buffer);
112
113 return true;
114 });
115 }
116
117 bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
118 const Entity& entity,
119 RenderPass& pass) const {
120 using VS = SweepGradientFillPipeline::VertexShader;
121 using FS = SweepGradientFillPipeline::FragmentShader;
122
123 auto gradient_data = CreateGradientBuffer(colors_, stops_);
124 auto gradient_texture =
125 CreateGradientTexture(gradient_data, renderer.GetContext());
126 if (gradient_texture == nullptr) {
127 return false;
128 }
129
130 VS::FrameInfo frame_info;
131 frame_info.matrix = GetInverseEffectTransform();
132
133 PipelineBuilderCallback pipeline_callback =
134 [&renderer](ContentContextOptions options) {
135 return renderer.GetSweepGradientFillPipeline(options);
136 };
137 return ColorSourceContents::DrawGeometry<VS>(
138 renderer, entity, pass, pipeline_callback, frame_info,
139 [this, &renderer, &gradient_texture](RenderPass& pass) {
140 FS::FragInfo frag_info;
141 frag_info.center = center_;
142 frag_info.bias = bias_;
143 frag_info.scale = scale_;
144 frag_info.texture_sampler_y_coord_scale =
145 gradient_texture->GetYCoordScale();
146 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
147 frag_info.decal_border_color = decal_border_color_;
148 frag_info.alpha = GetOpacityFactor();
149 frag_info.half_texel =
150 Vector2(0.5 / gradient_texture->GetSize().width,
151 0.5 / gradient_texture->GetSize().height);
152
153 SamplerDescriptor sampler_desc;
154 sampler_desc.min_filter = MinMagFilter::kLinear;
155 sampler_desc.mag_filter = MinMagFilter::kLinear;
156
157 pass.SetCommandLabel("SweepGradientFill");
158
159 FS::BindFragInfo(
160 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
161 FS::BindTextureSampler(
162 pass, gradient_texture,
163 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
164 sampler_desc));
165
166 return true;
167 });
168 }
169
170 bool SweepGradientContents::ApplyColorFilter(
171 const ColorFilterProc& color_filter_proc) {
172 for (Color& color : colors_) {
173 color = color_filter_proc(color);
174 }
175 decal_border_color_ = color_filter_proc(decal_border_color_);
176 return true;
177 }
178
179 } // 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:170
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:113
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:15
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:320
impeller::SweepGradientContents::~SweepGradientContents
~SweepGradientContents() override
impeller::SweepGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: sweep_gradient_contents.cc:48
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:94
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::ContentContext::GetSweepGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSweepGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:450
sweep_gradient_contents.h
gradient.h
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::Entity
Definition: entity.h:21
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:38
impeller::MinMagFilter::kLinear
@ kLinear
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:568
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
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::SweepGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: sweep_gradient_contents.cc:44
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:93
impeller::ContentContextOptions
Definition: content_context.h:288
impeller::Capabilities::SupportsSSBO
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
impeller
Definition: aiks_blur_unittests.cc:20
impeller::ContentContext
Definition: content_context.h:392
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:833