Flutter Engine
The Flutter Engine
path.h
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#ifndef FLUTTER_IMPELLER_GEOMETRY_PATH_H_
6#define FLUTTER_IMPELLER_GEOMETRY_PATH_H_
7
8#include <functional>
9#include <optional>
10#include <tuple>
11#include <vector>
12
15
16namespace impeller {
17
18enum class Cap {
19 kButt,
20 kRound,
21 kSquare,
22};
23
24enum class Join {
25 kMiter,
26 kRound,
27 kBevel,
28};
29
30enum class FillType {
31 kNonZero, // The default winding order.
32 kOdd,
33};
34
35enum class Convexity {
36 kUnknown,
37 kConvex,
38};
39
40//------------------------------------------------------------------------------
41/// @brief Paths are lightweight objects that describe a collection of
42/// linear, quadratic, or cubic segments. These segments may be
43/// broken up by move commands, which are effectively linear
44/// commands that pick up the pen rather than continuing to draw.
45///
46/// All shapes supported by Impeller are paths either directly or
47/// via approximation (in the case of circles).
48///
49/// Paths are externally immutable once created, Creating paths must
50/// be done using a path builder.
51///
52class Path {
53 public:
54 enum class ComponentType {
55 kLinear,
57 kCubic,
58 kContour,
59 };
60
62 struct Component {
64 /// Denotes whether this component is a curve.
65 ///
66 /// This is set to true when this component is generated from
67 /// QuadraticComponent or CubicPathComponent.
69 };
70 /// Index that denotes the first point of this contour.
72
73 /// Denotes whether the last point of this contour is connected to the first
74 /// point of this contour or not.
76
77 /// The direction of the contour's start cap.
79 /// The direction of the contour's end cap.
81
82 /// Distinct components in this contour.
83 ///
84 /// If this contour is generated from multiple path components, each
85 /// path component forms a component in this vector.
86 std::vector<Component> components;
87 };
88
89 /// One or more contours represented as a series of points and indices in
90 /// the point vector representing the start of a new contour.
91 ///
92 /// Polylines are ephemeral and meant to be used by the tessellator. They do
93 /// not allocate their own point vectors to allow for optimizations around
94 /// allocation and reuse of arenas.
95 struct Polyline {
96 /// The signature of a method called when it is safe to reclaim the point
97 /// buffer provided to the constructor of this object.
98 using PointBufferPtr = std::unique_ptr<std::vector<Point>>;
100
101 /// The buffer will be cleared and returned at the destruction of this
102 /// polyline.
104
105 Polyline(Polyline&& other);
106 ~Polyline();
107
108 /// Points in the polyline, which may represent multiple contours specified
109 /// by indices in |contours|.
111
112 Point& GetPoint(size_t index) const { return (*points)[index]; }
113
114 /// Contours are disconnected pieces of a polyline, such as when a MoveTo
115 /// was issued on a PathBuilder.
116 std::vector<PolylineContour> contours;
117
118 /// Convenience method to compute the start (inclusive) and end (exclusive)
119 /// point of the given contour index.
120 ///
121 /// The contour_index parameter is clamped to contours.size().
122 std::tuple<size_t, size_t> GetContourPointBounds(
123 size_t contour_index) const;
124
125 private:
126 ReclaimPointBufferCallback reclaim_points_;
127 };
128
129 Path();
130
132
133 size_t GetComponentCount(std::optional<ComponentType> type = {}) const;
134
135 FillType GetFillType() const;
136
137 bool IsConvex() const;
138
139 bool IsEmpty() const;
140
141 template <class T>
142 using Applier = std::function<void(size_t index, const T& component)>;
144 const Applier<LinearPathComponent>& linear_applier,
145 const Applier<QuadraticPathComponent>& quad_applier,
146 const Applier<CubicPathComponent>& cubic_applier,
147 const Applier<ContourComponent>& contour_applier) const;
148
149 bool GetLinearComponentAtIndex(size_t index,
151
152 bool GetQuadraticComponentAtIndex(size_t index,
154
155 bool GetCubicComponentAtIndex(size_t index, CubicPathComponent& cubic) const;
156
157 bool GetContourComponentAtIndex(size_t index,
159
160 /// Callers must provide the scale factor for how this path will be
161 /// transformed.
162 ///
163 /// It is suitable to use the max basis length of the matrix used to transform
164 /// the path. If the provided scale is 0, curves will revert to straight
165 /// lines.
168 Polyline::PointBufferPtr point_buffer =
169 std::make_unique<std::vector<Point>>(),
170 Polyline::ReclaimPointBufferCallback reclaim = nullptr) const;
171
172 std::optional<Rect> GetBoundingBox() const;
173
174 std::optional<Rect> GetTransformedBoundingBox(const Matrix& transform) const;
175
176 /// Generate a polyline into the temporary storage held by the [writer].
177 ///
178 /// It is suitable to use the max basis length of the matrix used to transform
179 /// the path. If the provided scale is 0, curves will revert to straight
180 /// lines.
181 void WritePolyline(Scalar scale, VertexWriter& writer) const;
182
183 private:
184 friend class PathBuilder;
185
186 struct ComponentIndexPair {
188 size_t index = 0;
189
190 ComponentIndexPair() {}
191
192 ComponentIndexPair(ComponentType a_type, size_t a_index)
193 : type(a_type), index(a_index) {}
194 };
195
196 // All of the data for the path is stored in this structure which is
197 // held by a shared_ptr. Since they all share the structure, the
198 // copy constructor for Path is very cheap and we don't need to deal
199 // with shared pointers for Path fields and method arguments.
200 //
201 // PathBuilder also uses this structure to accumulate the path data
202 // but the Path constructor used in |TakePath()| will clone the
203 // structure to prevent sharing and future modifications within the
204 // builder from affecting the existing taken paths.
205 struct Data {
206 Data() = default;
207
208 Data(Data&& other) = default;
209
210 Data(const Data& other) = default;
211
212 ~Data() = default;
213
215 Convexity convexity = Convexity::kUnknown;
216 std::vector<ComponentIndexPair> components;
217 std::vector<Point> points;
218 std::vector<ContourComponent> contours;
219
220 std::optional<Rect> bounds;
221 };
222
223 explicit Path(Data data);
224
225 std::shared_ptr<const Data> data_;
226};
227
228static_assert(sizeof(Path) == sizeof(std::shared_ptr<struct Anonymous>));
229
230} // namespace impeller
231
232#endif // FLUTTER_IMPELLER_GEOMETRY_PATH_H_
static const int points[]
GLenum type
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:52
FillType GetFillType() const
Definition: path.cc:51
size_t GetComponentCount(std::optional< ComponentType > type={}) const
Definition: path.cc:34
bool GetContourComponentAtIndex(size_t index, ContourComponent &contour) const
Definition: path.cc:229
void EnumerateComponents(const Applier< LinearPathComponent > &linear_applier, const Applier< QuadraticPathComponent > &quad_applier, const Applier< CubicPathComponent > &cubic_applier, const Applier< ContourComponent > &contour_applier) const
Definition: path.cc:63
std::optional< Rect > GetTransformedBoundingBox(const Matrix &transform) const
Definition: path.cc:409
std::function< void(size_t index, const T &component)> Applier
Definition: path.h:142
bool IsConvex() const
Definition: path.cc:55
bool GetLinearComponentAtIndex(size_t index, LinearPathComponent &linear) const
Definition: path.cc:172
void WritePolyline(Scalar scale, VertexWriter &writer) const
Definition: path.cc:106
bool IsEmpty() const
Definition: path.cc:59
bool GetQuadraticComponentAtIndex(size_t index, QuadraticPathComponent &quadratic) const
Definition: path.cc:190
bool GetCubicComponentAtIndex(size_t index, CubicPathComponent &cubic) const
Definition: path.cc:210
Polyline CreatePolyline(Scalar scale, Polyline::PointBufferPtr point_buffer=std::make_unique< std::vector< Point > >(), Polyline::ReclaimPointBufferCallback reclaim=nullptr) const
Definition: path.cc:264
std::optional< Rect > GetBoundingBox() const
Definition: path.cc:405
An interface for generating a multi contour polyline as a triangle strip.
Dart_NativeFunction function
Definition: fuchsia.cc:51
Optional< SkRect > bounds
Definition: SkRecords.h:189
struct PathData * Data(SkPath *path)
Definition: path_ops.cc:52
CanvasPath Path
Definition: dart_ui.cc:58
Join
Definition: path.h:24
float Scalar
Definition: scalar.h:18
FillType
Definition: path.h:30
Convexity
Definition: path.h:35
Cap
Definition: path.h:18
AI float quadratic(float precision, const SkPoint pts[], const VectorXform &vectorXform=VectorXform())
Definition: WangsFormula.h:156
AI float cubic(float precision, const SkPoint pts[], const VectorXform &vectorXform=VectorXform())
Definition: WangsFormula.h:195
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
#define T
Definition: precompiler.cc:65
const Scalar scale
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
Vector2 start_direction
The direction of the contour's start cap.
Definition: path.h:78
size_t start_index
Index that denotes the first point of this contour.
Definition: path.h:71
std::vector< Component > components
Definition: path.h:86
Vector2 end_direction
The direction of the contour's end cap.
Definition: path.h:80
Polyline(PointBufferPtr point_buffer, ReclaimPointBufferCallback reclaim)
Definition: path.cc:245
std::vector< PolylineContour > contours
Definition: path.h:116
Point & GetPoint(size_t index) const
Definition: path.h:112
std::function< void(PointBufferPtr)> ReclaimPointBufferCallback
Definition: path.h:99
std::unique_ptr< std::vector< Point > > PointBufferPtr
Definition: path.h:98
PointBufferPtr points
Definition: path.h:110
std::tuple< size_t, size_t > GetContourPointBounds(size_t contour_index) const
Definition: path.cc:22
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)