Flutter Engine
The Flutter Engine
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
6
14
15namespace impeller {
16
18
20
22 center_ = center;
23 radius_ = radius;
24}
25
27 tile_mode_ = tile_mode;
28}
29
31 colors_ = std::move(colors);
32}
33
34void ConicalGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36}
37
38const std::vector<Color>& ConicalGradientContents::GetColors() const {
39 return colors_;
40}
41
42const std::vector<Scalar>& ConicalGradientContents::GetStops() const {
43 return stops_;
44}
45
46void ConicalGradientContents::SetFocus(std::optional<Point> focus,
47 Scalar radius) {
48 focus_ = focus;
49 focus_radius_ = radius;
50}
51
53 const Entity& entity,
54 RenderPass& pass) const {
55 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
56 return RenderSSBO(renderer, entity, pass);
57 }
58 return RenderTexture(renderer, entity, pass);
59}
60
61bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
62 const Entity& entity,
63 RenderPass& pass) const {
66
67 VS::FrameInfo frame_info;
68 frame_info.matrix = GetInverseEffectTransform();
69
70 PipelineBuilderCallback pipeline_callback =
72 return renderer.GetConicalGradientSSBOFillPipeline(options);
73 };
74 return ColorSourceContents::DrawGeometry<VS>(
75 renderer, entity, pass, pipeline_callback, frame_info,
76 [this, &renderer, &entity](RenderPass& pass) {
77 FS::FragInfo frag_info;
78 frag_info.center = center_;
79 frag_info.radius = radius_;
80 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
81 frag_info.decal_border_color = decal_border_color_;
82 frag_info.alpha =
83 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
84 if (focus_) {
85 frag_info.focus = focus_.value();
86 frag_info.focus_radius = focus_radius_;
87 } else {
88 frag_info.focus = center_;
89 frag_info.focus_radius = 0.0;
90 }
91
92 auto& host_buffer = renderer.GetTransientsBuffer();
93 auto colors = CreateGradientColors(colors_, stops_);
94
95 frag_info.colors_length = colors.size();
96 auto color_buffer =
97 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
99
100 FS::BindFragInfo(
101 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
102 FS::BindColorData(pass, color_buffer);
103
104 pass.SetCommandLabel("ConicalGradientSSBOFill");
105 return true;
106 });
107}
108
109bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
110 const Entity& entity,
111 RenderPass& pass) const {
114
115 auto gradient_data = CreateGradientBuffer(colors_, stops_);
116 auto gradient_texture =
117 CreateGradientTexture(gradient_data, renderer.GetContext());
118 if (gradient_texture == nullptr) {
119 return false;
120 }
121
122 VS::FrameInfo frame_info;
123 frame_info.matrix = GetInverseEffectTransform();
124
125 PipelineBuilderCallback pipeline_callback =
126 [&renderer](ContentContextOptions options) {
127 return renderer.GetConicalGradientFillPipeline(options);
128 };
129 return ColorSourceContents::DrawGeometry<VS>(
130 renderer, entity, pass, pipeline_callback, frame_info,
131 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
132 FS::FragInfo frag_info;
133 frag_info.center = center_;
134 frag_info.radius = radius_;
135 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
136 frag_info.decal_border_color = decal_border_color_;
137 frag_info.texture_sampler_y_coord_scale =
138 gradient_texture->GetYCoordScale();
139 frag_info.alpha =
140 GetOpacityFactor() * GetGeometry()->ComputeAlphaCoverage(entity);
141 frag_info.half_texel =
142 Vector2(0.5 / gradient_texture->GetSize().width,
143 0.5 / gradient_texture->GetSize().height);
144 if (focus_) {
145 frag_info.focus = focus_.value();
146 frag_info.focus_radius = focus_radius_;
147 } else {
148 frag_info.focus = center_;
149 frag_info.focus_radius = 0.0;
150 }
151
152 pass.SetCommandLabel("ConicalGradientFill");
153
154 FS::BindFragInfo(
155 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
156 SamplerDescriptor sampler_desc;
157 sampler_desc.min_filter = MinMagFilter::kLinear;
158 sampler_desc.mag_filter = MinMagFilter::kLinear;
159 FS::BindTextureSampler(
160 pass, gradient_texture,
161 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
162 sampler_desc));
163
164 return true;
165 });
166}
167
169 const ColorFilterProc& color_filter_proc) {
170 for (Color& color : colors_) {
171 color = color_filter_proc(color);
172 }
173 decal_border_color_ = color_filter_proc(decal_border_color_);
174 return true;
175}
176
177} // namespace impeller
const char * options
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
void SetFocus(std::optional< Point > focus, Scalar radius)
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
void SetStops(std::vector< Scalar > stops)
void SetCenterAndRadius(Point center, Scalar radius)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
const std::vector< Scalar > & GetStops() const
void SetTileMode(Entity::TileMode tile_mode)
const std::vector< Color > & GetColors() const
void SetColors(std::vector< Color > colors)
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
FragmentShader_ FragmentShader
Definition: pipeline.h:107
DlColor color
PODArray< SkColor > colors
Definition: SkRecords.h:276
Point Vector2
Definition: point.h:326
float Scalar
Definition: scalar.h:18
SolidFillVertexShader VS
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.
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.
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
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
Scalar alpha
Definition: color.h:143