Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
uber_sdf_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
15namespace {
16
17using PipelineBuilderCallback =
18 std::function<PipelineRef(ContentContextOptions)>;
19
20using VS = UberSDFPipeline::VertexShader;
21using FS = UberSDFPipeline::FragmentShader;
22
24 switch (type) {
26 return 0.0f;
28 return 1.0f;
30 return 2.0f;
32 return 3.0f;
34 return 4.0f;
35 }
36}
37
38Scalar ToShaderStrokeJoin(Join join) {
39 switch (join) {
40 case Join::kMiter:
41 return 0.0f;
42 case Join::kBevel:
43 return 1.0f;
44 case Join::kRound:
45 return 2.0f;
46 }
47}
48
49} // namespace
50
51std::unique_ptr<UberSDFContents> UberSDFContents::Make(
53 std::unique_ptr<Geometry> geometry) {
54 return std::unique_ptr<UberSDFContents>(
55 new UberSDFContents(params, std::move(geometry)));
56}
57
58UberSDFContents::UberSDFContents(const UberSDFParameters& params,
59 std::unique_ptr<Geometry> geometry)
60 : params_(params), geometry_(std::move(geometry)) {}
61
63
65 const Entity& entity,
66 RenderPass& pass) const {
67 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
68
69 VS::FrameInfo frame_info;
70 FS::FragInfo frag_info;
71 frag_info.type = ToShaderType(params_.type);
72 frag_info.color =
73 params_.color.WithAlpha(params_.color.alpha * GetOpacityFactor());
74 frag_info.center = params_.center;
75 frag_info.size = params_.size;
76 frag_info.radii =
78 params_.radii.bottom_left.width, params_.radii.top_left.width);
79 frag_info.radii_right =
81 params_.radii.bottom_left.height, params_.radii.top_left.height);
82 frag_info.stroked = params_.stroke ? 1.0f : 0.0f;
83 frag_info.stroke_width = params_.stroke ? params_.stroke->width : 0.0f;
84 frag_info.stroke_join =
85 params_.stroke ? ToShaderStrokeJoin(params_.stroke->join) : 0.0f;
86 frag_info.aa_pixels = UberSDFParameters::kAntialiasPixels;
87 frag_info.superellipse_degree = params_.superellipse_degree;
88 frag_info.superellipse_a = params_.superellipse_a;
89 frag_info.corner_angle_span = params_.corner_angle_span;
90 frag_info.corner_circle_center_top = params_.corner_circle_center_top;
91 frag_info.corner_circle_center_right = params_.corner_circle_center_right;
92 frag_info.superellipse_c = params_.superellipse_c;
93 frag_info.superellipse_scale = params_.superellipse_scale;
94
95 auto geometry_result =
96 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
97
98 PipelineBuilderCallback pipeline_callback =
99 [&renderer](ContentContextOptions options) {
100 return renderer.GetUberSDFPipeline(options);
101 };
102
103 return ColorSourceContents::DrawGeometry<VS>(
104 this, GetGeometry(), renderer, entity, pass, pipeline_callback,
105 frame_info,
106 /*bind_fragment_callback=*/
107 [&frag_info, &data_host_buffer](RenderPass& pass) {
108 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
109 pass.SetCommandLabel("UberSDF");
110 return true;
111 },
112 /*force_stencil=*/false,
113 /*create_geom_callback=*/
114 [geometry_result = std::move(geometry_result)](
115 const ContentContext& renderer, const Entity& entity,
116 RenderPass& pass,
117 const Geometry* geometry) { return geometry_result; });
118}
119
120std::optional<Rect> UberSDFContents::GetCoverage(const Entity& entity) const {
121 return GetGeometry()->GetCoverage(entity.GetTransform());
122}
123
125 return geometry_.get();
126}
127
129 return params_.color;
130}
131
133 const ColorFilterProc& color_filter_proc) {
134 params_.color = color_filter_proc(params_.color);
135 return true;
136}
137
139 const Entity& entity,
140 ISize target_size) const {
141 if (params_.type != UberSDFParameters::Type::kRect) {
142 return std::nullopt;
143 }
144 const Geometry* geometry = GetGeometry();
145 if (geometry == nullptr) {
146 return std::nullopt;
147 }
148 IRect target_rect = IRect::MakeSize(target_size);
149 return geometry->CoversArea(entity.GetTransform(), target_rect)
150 ? GetColor()
151 : std::optional<Color>();
152}
153
154} // namespace impeller
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
PipelineRef GetUberSDFPipeline(ContentContextOptions opts) const
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition entity.cc:46
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
The coverage rectangle of this geometry, transformed by the transform argument.
virtual bool CoversArea(const Matrix &transform, const IRect &rect) const
Determines if this geometry, transformed by the given transform, will completely cover all of the pix...
Definition geometry.cc:136
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:30
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
static std::unique_ptr< UberSDFContents > Make(const UberSDFParameters &params, std::unique_ptr< Geometry > geometry)
std::optional< Color > AsBackgroundColor(const Entity &entity, ISize target_size) const override
Returns a color if this Contents will flood the given target_size with a color. This output color is ...
const Geometry * GetGeometry() const override
Get the geometry that this contents will use to render.
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
const EmbeddedViewParams * params
Join
An enum that describes ways to join two segments of a path.
float Scalar
Definition scalar.h:19
raw_ptr< Pipeline< PipelineDescriptor > > PipelineRef
A raw ptr to a pipeline object.
Definition pipeline.h:89
LinePipeline::FragmentShader FS
std::function< Color(Color)> ColorFilterProc
LinePipeline::VertexShader VS
constexpr ArchiveShaderType ToShaderType(fb::Stage stage)
Definition ref_ptr.h:261
impeller::ShaderType type
Scalar alpha
Definition color.h:143
constexpr Color WithAlpha(Scalar new_alpha) const
Definition color.h:283
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:150
Type height
Definition size.h:29
Type width
Definition size.h:28
Parameters for rendering shapes using the UberSDF shader.
Point superellipse_a
The semi-axes of the top (.x) and right (.y) superellipse segments.
Color color
The color used for filling or stroking the shape.
Point corner_circle_center_right
The center of the right circular arc in a RoundSuperellipse.
Point superellipse_degree
The degrees of the top (.x) and right (.y) octants of a RoundSuperellipse.
Type type
The type of shape to render.
Point center
The center point of the shape in local coordinates.
Type
The type of primitive shape.
Point corner_angle_span
The spans of the top (.x) and right (.y) circular arcs.
static constexpr Scalar kAntialiasPixels
Point superellipse_scale
The scale of the superellipse.
std::optional< StrokeParameters > stroke
The stroke parameters. If std::nullopt, the shape is filled.
Point corner_circle_center_top
The center of the top circular arc in a RoundSuperellipse.
Scalar superellipse_c
The offset of the octants in a RoundSuperellipse.