| Flutter Impeller |

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
5 #include "conical_gradient_contents.h"
6
7 #include "impeller/entity/contents/clip_contents.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/contents/gradient_generator.h"
10 #include "impeller/entity/entity.h"
11 #include "impeller/entity/geometry/geometry.h"
12 #include "impeller/geometry/gradient.h"
13 #include "impeller/renderer/render_pass.h"
14
15 namespace impeller {
16
17 ConicalGradientContents::ConicalGradientContents() = default;
18
19 ConicalGradientContents::~ConicalGradientContents() = default;
20
21 void ConicalGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
22 center_ = center;
23 radius_ = radius;
24 }
25
26 void ConicalGradientContents::SetTileMode(Entity::TileMode tile_mode) {
27 tile_mode_ = tile_mode;
28 }
29
30 void ConicalGradientContents::SetColors(std::vector<Color> colors) {
31 colors_ = std::move(colors);
32 }
33
34 void ConicalGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36 }
37
38 const std::vector<Color>& ConicalGradientContents::GetColors() const {
39 return colors_;
40 }
41
42 const std::vector<Scalar>& ConicalGradientContents::GetStops() const {
43 return stops_;
44 }
45
46 void ConicalGradientContents::SetFocus(std::optional<Point> focus,
47 Scalar radius) {
48 focus_ = focus;
49 focus_radius_ = radius;
50 }
51
52 bool ConicalGradientContents::Render(const ContentContext& renderer,
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
61 bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
62 const Entity& entity,
63 RenderPass& pass) const {
64 using VS = ConicalGradientSSBOFillPipeline::VertexShader;
65 using FS = ConicalGradientSSBOFillPipeline::FragmentShader;
66
67 VS::FrameInfo frame_info;
68 frame_info.matrix = GetInverseEffectTransform();
69
70 PipelineBuilderCallback pipeline_callback =
71 [&renderer](ContentContextOptions options) {
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() *
84 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
85 if (focus_) {
86 frag_info.focus = focus_.value();
87 frag_info.focus_radius = focus_radius_;
88 } else {
89 frag_info.focus = center_;
90 frag_info.focus_radius = 0.0;
91 }
92
93 auto& host_buffer = renderer.GetTransientsBuffer();
94 auto colors = CreateGradientColors(colors_, stops_);
95
96 frag_info.colors_length = colors.size();
97 auto color_buffer =
98 host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
99 DefaultUniformAlignment());
100
101 FS::BindFragInfo(
102 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
103 FS::BindColorData(pass, color_buffer);
104
105 pass.SetCommandLabel("ConicalGradientSSBOFill");
106 return true;
107 });
108 }
109
110 bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
111 const Entity& entity,
112 RenderPass& pass) const {
113 using VS = ConicalGradientFillPipeline::VertexShader;
114 using FS = ConicalGradientFillPipeline::FragmentShader;
115
116 auto gradient_data = CreateGradientBuffer(colors_, stops_);
117 auto gradient_texture =
118 CreateGradientTexture(gradient_data, renderer.GetContext());
119 if (gradient_texture == nullptr) {
120 return false;
121 }
122
123 VS::FrameInfo frame_info;
124 frame_info.matrix = GetInverseEffectTransform();
125
126 PipelineBuilderCallback pipeline_callback =
127 [&renderer](ContentContextOptions options) {
128 return renderer.GetConicalGradientFillPipeline(options);
129 };
130 return ColorSourceContents::DrawGeometry<VS>(
131 renderer, entity, pass, pipeline_callback, frame_info,
132 [this, &renderer, &gradient_texture, &entity](RenderPass& pass) {
133 FS::FragInfo frag_info;
134 frag_info.center = center_;
135 frag_info.radius = radius_;
136 frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
137 frag_info.decal_border_color = decal_border_color_;
138 frag_info.texture_sampler_y_coord_scale =
139 gradient_texture->GetYCoordScale();
140 frag_info.alpha =
141 GetOpacityFactor() *
142 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
143 frag_info.half_texel =
144 Vector2(0.5 / gradient_texture->GetSize().width,
145 0.5 / gradient_texture->GetSize().height);
146 if (focus_) {
147 frag_info.focus = focus_.value();
148 frag_info.focus_radius = focus_radius_;
149 } else {
150 frag_info.focus = center_;
151 frag_info.focus_radius = 0.0;
152 }
153
154 pass.SetCommandLabel("ConicalGradientFill");
155
156 FS::BindFragInfo(
157 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
158 SamplerDescriptor sampler_desc;
159 sampler_desc.min_filter = MinMagFilter::kLinear;
160 sampler_desc.mag_filter = MinMagFilter::kLinear;
161 FS::BindTextureSampler(
162 pass, gradient_texture,
163 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
164 sampler_desc));
165
166 return true;
167 });
168 }
169
170 bool ConicalGradientContents::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::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:108
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::ConicalGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: conical_gradient_contents.cc:42
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:124
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
geometry.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
entity.h
impeller::Color
Definition: color.h:123
impeller::ConicalGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: conical_gradient_contents.cc:30
impeller::ConicalGradientContents::SetFocus
void SetFocus(std::optional< Point > focus, Scalar radius)
Definition: conical_gradient_contents.cc:46
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
impeller::ConicalGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: conical_gradient_contents.cc:21
impeller::ConicalGradientContents::ConicalGradientContents
ConicalGradientContents()
gradient.h
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::Entity
Definition: entity.h:20
render_pass.h
impeller::ContentContext::GetConicalGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetConicalGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:402
conical_gradient_contents.h
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::MinMagFilter::kLinear
@ kLinear
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
impeller::ConicalGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: conical_gradient_contents.cc:52
clip_contents.h
impeller::ConicalGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: conical_gradient_contents.cc:34
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::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::ConicalGradientContents::~ConicalGradientContents
~ConicalGradientContents() override
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
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::ConicalGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: conical_gradient_contents.cc:38
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:554
impeller::TPoint
Definition: point.h:27
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::ConicalGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: conical_gradient_contents.cc:26
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
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:53
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:16
impeller::ContentContextOptions
Definition: content_context.h:254
impeller::Capabilities::SupportsSSBO
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
impeller
Definition: allocation.cc:12
impeller::ConicalGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: conical_gradient_contents.cc:170
impeller::ContentContext
Definition: content_context.h:366
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:753