| Flutter Impeller |

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
5 #include "radial_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 RadialGradientContents::RadialGradientContents() = default;
18
19 RadialGradientContents::~RadialGradientContents() = default;
20
21 void RadialGradientContents::SetCenterAndRadius(Point center, Scalar radius) {
22 center_ = center;
23 radius_ = radius;
24 }
25
26 void RadialGradientContents::SetTileMode(Entity::TileMode tile_mode) {
27 tile_mode_ = tile_mode;
28 }
29
30 void RadialGradientContents::SetColors(std::vector<Color> colors) {
31 colors_ = std::move(colors);
32 }
33
34 void RadialGradientContents::SetStops(std::vector<Scalar> stops) {
35 stops_ = std::move(stops);
36 }
37
38 const std::vector<Color>& RadialGradientContents::GetColors() const {
39 return colors_;
40 }
41
42 const std::vector<Scalar>& RadialGradientContents::GetStops() const {
43 return stops_;
44 }
45
46 bool RadialGradientContents::IsOpaque(const Matrix& transform) const {
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 !AppliesAlphaForStrokeCoverage(transform);
56 }
57
58 bool RadialGradientContents::Render(const ContentContext& renderer,
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
67 bool RadialGradientContents::RenderSSBO(const ContentContext& renderer,
68 const Entity& entity,
69 RenderPass& pass) const {
70 using VS = RadialGradientSSBOFillPipeline::VertexShader;
71 using FS = RadialGradientSSBOFillPipeline::FragmentShader;
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, &entity](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 =
89 GetOpacityFactor() *
90 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
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),
98 DefaultUniformAlignment());
99
100 pass.SetCommandLabel("RadialGradientSSBOFill");
101 FS::BindFragInfo(
102 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
103 FS::BindColorData(pass, color_buffer);
104
105 return true;
106 });
107 }
108
109 bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
110 const Entity& entity,
111 RenderPass& pass) const {
112 using VS = RadialGradientFillPipeline::VertexShader;
113 using FS = RadialGradientFillPipeline::FragmentShader;
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.GetRadialGradientFillPipeline(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() *
141 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
142 frag_info.half_texel =
143 Vector2(0.5 / gradient_texture->GetSize().width,
144 0.5 / gradient_texture->GetSize().height);
145
146 SamplerDescriptor sampler_desc;
147 sampler_desc.min_filter = MinMagFilter::kLinear;
148 sampler_desc.mag_filter = MinMagFilter::kLinear;
149
150 pass.SetCommandLabel("RadialGradientFill");
151
152 FS::BindFragInfo(
153 pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
154 FS::BindTextureSampler(
155 pass, gradient_texture,
156 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
157 sampler_desc));
158
159 return true;
160 });
161 }
162
163 bool RadialGradientContents::ApplyColorFilter(
164 const ColorFilterProc& color_filter_proc) {
165 for (Color& color : colors_) {
166 color = color_filter_proc(color);
167 }
168 decal_border_color_ = color_filter_proc(decal_border_color_);
169 return true;
170 }
171
172 } // 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::RadialGradientContents::SetCenterAndRadius
void SetCenterAndRadius(Point center, Scalar radius)
Definition: radial_gradient_contents.cc:21
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
impeller::RadialGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: radial_gradient_contents.cc:34
entity.h
impeller::RadialGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: radial_gradient_contents.cc:42
impeller::Color
Definition: color.h:123
impeller::RadialGradientContents::~RadialGradientContents
~RadialGradientContents() override
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
gradient.h
impeller::RadialGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: radial_gradient_contents.cc:58
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::Entity
Definition: entity.h:20
render_pass.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::MinMagFilter::kLinear
@ kLinear
impeller::RadialGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: radial_gradient_contents.cc:38
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
clip_contents.h
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::RadialGradientContents::RadialGradientContents
RadialGradientContents()
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::RadialGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: radial_gradient_contents.cc:163
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::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:554
impeller::RadialGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: radial_gradient_contents.cc:26
impeller::TPoint< Scalar >
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::ContentContext::GetRadialGradientSSBOFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetRadialGradientSSBOFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:396
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
radial_gradient_contents.h
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::ColorSourceContents::AppliesAlphaForStrokeCoverage
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
Definition: color_source_contents.cc:53
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::RadialGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: radial_gradient_contents.cc:30
impeller
Definition: allocation.cc:12
impeller::RadialGradientContents::IsOpaque
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: radial_gradient_contents.cc:46
impeller::ContentContext
Definition: content_context.h:366
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
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