Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
radial_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
30void RadialGradientContents::SetColors(std::vector<Color> colors) {
31 colors_ = std::move(colors);
32}
33
34void RadialGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36}
37
38const std::vector<Color>& RadialGradientContents::GetColors() const {
39 return colors_;
40}
41
42const std::vector<Scalar>& RadialGradientContents::GetStops() const {
43 return stops_;
44}
45
47 if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
48 return false;
49 }
50 for (auto color : colors_) {
51 if (!color.IsOpaque()) {
52 return false;
53 }
54 }
55 return true;
56}
57
59 const Entity& entity,
60 RenderPass& pass) const {
61 if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
62 return RenderSSBO(renderer, entity, pass);
63 }
64 return RenderTexture(renderer, entity, pass);
65}
66
67bool RadialGradientContents::RenderSSBO(const ContentContext& renderer,
68 const Entity& entity,
69 RenderPass& pass) const {
72
73 VS::FrameInfo frame_info;
74 frame_info.matrix = GetInverseEffectTransform();
75
76 PipelineBuilderCallback pipeline_callback =
77 [&renderer](ContentContextOptions options) {
78 return renderer.GetRadialGradientSSBOFillPipeline(options);
79 };
80 return ColorSourceContents::DrawGeometry<VS>(
81 renderer, entity, pass, pipeline_callback, frame_info,
82 [this, &renderer](RenderPass& pass) {
83 FS::FragInfo frag_info;
84 frag_info.center = center_;
85 frag_info.radius = radius_;
86 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
87 frag_info.decal_border_color = decal_border_color_;
88 frag_info.alpha = GetOpacityFactor();
89
90 auto& host_buffer = renderer.GetTransientsBuffer();
91 auto colors = CreateGradientColors(colors_, stops_);
92
93 frag_info.colors_length = colors.size();
94 auto color_buffer =
95 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
97
98 pass.SetCommandLabel("RadialGradientSSBOFill");
99 FS::BindFragInfo(
100 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
101 FS::BindColorData(pass, color_buffer);
102
103 return true;
104 });
105}
106
107bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
108 const Entity& entity,
109 RenderPass& pass) const {
112
113 auto gradient_data = CreateGradientBuffer(colors_, stops_);
114 auto gradient_texture =
115 CreateGradientTexture(gradient_data, renderer.GetContext());
116 if (gradient_texture == nullptr) {
117 return false;
118 }
119
120 VS::FrameInfo frame_info;
121 frame_info.matrix = GetInverseEffectTransform();
122
123 PipelineBuilderCallback pipeline_callback =
124 [&renderer](ContentContextOptions options) {
125 return renderer.GetRadialGradientFillPipeline(options);
126 };
127 return ColorSourceContents::DrawGeometry<VS>(
128 renderer, entity, pass, pipeline_callback, frame_info,
129 [this, &renderer, &gradient_texture](RenderPass& pass) {
130 FS::FragInfo frag_info;
131 frag_info.center = center_;
132 frag_info.radius = radius_;
133 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
134 frag_info.decal_border_color = decal_border_color_;
135 frag_info.texture_sampler_y_coord_scale =
136 gradient_texture->GetYCoordScale();
137 frag_info.alpha = GetOpacityFactor();
138 frag_info.half_texel =
139 Vector2(0.5 / gradient_texture->GetSize().width,
140 0.5 / gradient_texture->GetSize().height);
141
142 SamplerDescriptor sampler_desc;
143 sampler_desc.min_filter = MinMagFilter::kLinear;
144 sampler_desc.mag_filter = MinMagFilter::kLinear;
145
146 pass.SetCommandLabel("RadialGradientFill");
147
148 FS::BindFragInfo(
149 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
150 FS::BindTextureSampler(
151 pass, gradient_texture,
152 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
153 sampler_desc));
154
155 return true;
156 });
157}
158
160 const ColorFilterProc& color_filter_proc) {
161 for (Color& color : colors_) {
162 color = color_filter_proc(color);
163 }
164 decal_border_color_ = color_filter_proc(decal_border_color_);
165 return true;
166}
167
168} // namespace impeller
const char * options
SkColor4f color
static SkScalar center(float pos0, float pos1)
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
void SetTileMode(Entity::TileMode tile_mode)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetCenterAndRadius(Point center, Scalar radius)
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
void SetStops(std::vector< Scalar > stops)
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
void SetColors(std::vector< Color > colors)
const std::vector< Scalar > & GetStops() const
const std::vector< Color > & GetColors() const
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
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