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