Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
linear_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
12
13namespace impeller {
14
16
18
19void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) {
20 start_point_ = start_point;
21 end_point_ = end_point;
22}
23
24void LinearGradientContents::SetColors(std::vector<Color> colors) {
25 colors_ = std::move(colors);
26}
27
28void LinearGradientContents::SetStops(std::vector<Scalar> stops) {
29 stops_ = std::move(stops);
30}
31
32const std::vector<Color>& LinearGradientContents::GetColors() const {
33 return colors_;
34}
35
36const std::vector<Scalar>& LinearGradientContents::GetStops() const {
37 return stops_;
38}
39
41 tile_mode_ = tile_mode;
42}
43
45 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
46 return false;
47 }
48 for (auto color : colors_) {
49 if (!color.IsOpaque()) {
50 return false;
51 }
52 }
53 return true;
54}
55
57 const Entity& entity,
58 RenderPass& pass) const {
59 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
60 return RenderSSBO(renderer, entity, pass);
61 }
62 return RenderTexture(renderer, entity, pass);
63}
64
65bool LinearGradientContents::RenderTexture(const ContentContext& renderer,
66 const Entity& entity,
67 RenderPass& pass) const {
70
71 VS::FrameInfo frame_info;
72 frame_info.matrix = GetInverseEffectTransform();
73
74 PipelineBuilderCallback pipeline_callback =
75 [&renderer](ContentContextOptions options) {
76 return renderer.GetLinearGradientFillPipeline(options);
77 };
78 return ColorSourceContents::DrawGeometry<VS>(
79 renderer, entity, pass, pipeline_callback, frame_info,
80 [this, &renderer](RenderPass& pass) {
81 auto gradient_data = CreateGradientBuffer(colors_, stops_);
82 auto gradient_texture =
83 CreateGradientTexture(gradient_data, renderer.GetContext());
84 if (gradient_texture == nullptr) {
85 return false;
86 }
87
88 FS::FragInfo frag_info;
89 frag_info.start_point = start_point_;
90 frag_info.end_point = end_point_;
91 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
92 frag_info.decal_border_color = decal_border_color_;
93 frag_info.texture_sampler_y_coord_scale =
94 gradient_texture->GetYCoordScale();
95 frag_info.alpha = GetOpacityFactor();
96 frag_info.half_texel =
97 Vector2(0.5 / gradient_texture->GetSize().width,
98 0.5 / gradient_texture->GetSize().height);
99
100 pass.SetCommandLabel("LinearGradientFill");
101
102 SamplerDescriptor sampler_desc;
103 sampler_desc.min_filter = MinMagFilter::kLinear;
104 sampler_desc.mag_filter = MinMagFilter::kLinear;
105
106 FS::BindTextureSampler(
107 pass, std::move(gradient_texture),
108 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
109 sampler_desc));
110 FS::BindFragInfo(
111 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
112 return true;
113 });
114}
115
116bool LinearGradientContents::RenderSSBO(const ContentContext& renderer,
117 const Entity& entity,
118 RenderPass& pass) const {
121
122 VS::FrameInfo frame_info;
123 frame_info.matrix = GetInverseEffectTransform();
124
125 PipelineBuilderCallback pipeline_callback =
126 [&renderer](ContentContextOptions options) {
127 return renderer.GetLinearGradientSSBOFillPipeline(options);
128 };
129 return ColorSourceContents::DrawGeometry<VS>(
130 renderer, entity, pass, pipeline_callback, frame_info,
131 [this, &renderer](RenderPass& pass) {
132 FS::FragInfo frag_info;
133 frag_info.start_point = start_point_;
134 frag_info.end_point = end_point_;
135 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
136 frag_info.decal_border_color = decal_border_color_;
137 frag_info.alpha = GetOpacityFactor();
138
139 auto& host_buffer = renderer.GetTransientsBuffer();
140 auto colors = CreateGradientColors(colors_, stops_);
141
142 frag_info.colors_length = colors.size();
143 auto color_buffer =
144 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
146
147 pass.SetCommandLabel("LinearGradientSSBOFill");
148
149 FS::BindFragInfo(
150 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
151 FS::BindColorData(pass, color_buffer);
152
153 return true;
154 });
155}
156
158 const ColorFilterProc& color_filter_proc) {
159 for (Color& color : colors_) {
160 color = color_filter_proc(color);
161 }
162 decal_border_color_ = color_filter_proc(decal_border_color_);
163 return true;
164}
165
166} // namespace impeller
const char * options
SkColor4f color
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.
std::function< Color(Color)> ColorFilterProc
Definition contents.h:38
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
void SetTileMode(Entity::TileMode tile_mode)
const std::vector< Scalar > & GetStops() const
void SetColors(std::vector< Color > colors)
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetEndPoints(Point start_point, Point end_point)
void SetStops(std::vector< Scalar > stops)
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:106
PODArray< SkColor > colors
Definition SkRecords.h:276
Point Vector2
Definition point.h:320
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:15
Scalar alpha
Definition color.h:143