Flutter Engine
The Flutter Engine
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
6
7#include "flutter/fml/logging.h"
14
15namespace impeller {
16
18
20
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
32void SweepGradientContents::SetColors(std::vector<Color> colors) {
33 colors_ = std::move(colors);
34}
35
36void SweepGradientContents::SetStops(std::vector<Scalar> stops) {
37 stops_ = std::move(stops);
38}
39
41 tile_mode_ = tile_mode;
42}
43
44const std::vector<Color>& SweepGradientContents::GetColors() const {
45 return colors_;
46}
47
48const std::vector<Scalar>& SweepGradientContents::GetStops() const {
49 return stops_;
50}
51
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
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
73bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
74 const Entity& entity,
75 RenderPass& pass) const {
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 =
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),
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
118bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
119 const Entity& entity,
120 RenderPass& pass) const {
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
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
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.
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
void SetTileMode(Entity::TileMode tile_mode)
void SetCenterAndAngles(Point center, Degrees start_angle, Degrees end_angle)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
const std::vector< Color > & GetColors() const
const std::vector< Scalar > & GetStops() const
void SetStops(std::vector< Scalar > stops)
void SetColors(std::vector< Color > colors)
DlColor color
#define FML_DCHECK(condition)
Definition: logging.h:103
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
Scalar degrees
Definition: scalar.h:47