Debian Codegraph @

src/core/SkPathData.h

raw
1 /*
2 * Copyright 2025 Google LLC.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkPathData_DEFINED
9 #define SkPathData_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPathTypes.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSpan.h"
18
19 #include "include/private/SkIDChangeListener.h"
20 #include "include/private/SkPathRef.h"
21 #include "src/core/SkPathEnums.h"
22
23 #include <array>
24 #include <atomic>
25 #include <cstddef>
26 #include <cstdint>
27 #include <optional>
28
29 struct SkPathRaw;
30
31 /*
32 * Immutable container for path geometry data: points, verbs, bounds, segment masks.
33 *
34 * All of the public factories check for valid input
35 * - valid verb sequence
36 * - corresponding # points
37 * - finite point and conic values
38 *
39 * If any of these checks fail, null is returned.
40 *
41 * A valid sequence of verbs (and corresponding points/conics) is:
42 *
43 * Any number of contours (0 or more)
44 * ... Each contour must begin with a single Move verb
45 * ... followed by any number of segments: lines, quads, conics, cubics (0 or more)
46 * ... followed by 0 or 1 Close verb
47 *
48 * A minor exception to these rules, is that the last contour may end with a single
49 * Move verb -- this will be ignored in the resulting PathData.
50 *
51 * Given a valid verb sequence, there must be the corresponding number of points and
52 * conics to match. If there are more or fewer, null is returned.
53 *
54 * Verb points conic_weights
55 * -----------------------------
56 * Move 1 0
57 * Line 1 0
58 * Quad 2 0
59 * Conic 2 1
60 * Cubic 3 0
61 * Close 0 0
62 */
63 class SkPathData : public SkNVRefCnt<SkPathData> {
64 public:
65 ~SkPathData();
66
67 /*
68 * Returns an empty pathdata.
69 *
70 * Since this is immutable, it may return the same object each time it is called.
71 */
72 static sk_sp<SkPathData> Empty();
73
74 /*
75 * Return SkPathData with a copy of these buffers, or nullptr if they are illegal.
76 * Illegal = non-finite, or non-sensical verb sequences
77 */
78 static sk_sp<SkPathData> Make(SkSpan<const SkPoint> pts,
79 SkSpan<const SkPathVerb> verbs,
80 SkSpan<const float> conics = {});
81
82 /*
83 * Attempt to transform src by the matrix. On success, return a new SkPathData
84 * with the result, else return {}.
85 */
86 static sk_sp<SkPathData> MakeTransform(const SkPathRaw& src, const SkMatrix&);
87
88 /*
89 * When a factory takes a startIndex, this refers to the position of the first point
90 * when constructing one of our simple shapes: rect, oval, rrect.
91 * The index is the same as that passed to the equivalent factories in SkPath
92 * and in the associated addRect/Oval/RRect methods on SkPathBuilder.
93 */
94 static sk_sp<SkPathData> Rect(const SkRect&,
95 SkPathDirection = SkPathDirection::kDefault,
96 unsigned startIndex = 0);
97 static sk_sp<SkPathData> Oval(const SkRect&,
98 SkPathDirection = SkPathDirection::kDefault,
99 unsigned startIndex = 1);
100
101 static sk_sp<SkPathData> RRect(const SkRRect&, SkPathDirection, unsigned startIndex);
102 static sk_sp<SkPathData> RRect(const SkRRect& rrect,
103 SkPathDirection dir = SkPathDirection::kDefault) {
104 return RRect(rrect, dir, dir == SkPathDirection::kCW ? 6 : 7);
105 }
106 static sk_sp<SkPathData> Polygon(SkSpan<const SkPoint> pts, bool isClosed);
107 static sk_sp<SkPathData> Line(SkPoint a, SkPoint b) {
108 return Polygon({{a, b}}, false);
109 }
110
111 friend bool operator==(const SkPathData& a, const SkPathData& b);
112 friend bool operator!=(const SkPathData& a, const SkPathData& b) {
113 return !(a == b);
114 }
115
116 SkSpan<const SkPoint> points() const { return fPoints; }
117 SkSpan<const SkPathVerb> verbs() const { return fVerbs; }
118 SkSpan<const float> conics() const { return fConics; }
119 const SkRect& bounds() const { return fBounds; }
120 uint8_t segmentMask() const { return fSegmentMask; }
121
122 // Will never be zero, has the low-2 bits always zero (to store filltype)
123 uint32_t uniqueID() const { return fUniqueID; }
124
125 SkPathRaw raw(SkPathFillType, SkResolveConvexity) const;
126
127 /**
128 * Return true if the path contains no points or verbs
129 */
130 bool empty() const { return fVerbs.empty(); }
131
132 SkRect computeTightBounds() const;
133
134 /**
135 * Returns true if the pathdata is convex.
136 * Note: if necessary, it will first compute the convexity (and cache it).
137 */
138 bool isConvex() const;
139
140 /**
141 * Returns two points if SkPath contains only one line. If not, return {}.
142 */
143 std::optional<std::array<SkPoint, 2>> asLine() const;
144
145 /**
146 * If the pathdata is recognized as a rect, return it and its direction and open/closed.
147 * If not, return {}
148 */
149 std::optional<SkPathRectInfo> asRect() const;
150
151 /**
152 * If the path is recognized as an oval, return its bounds. If not, return {}.
153 */
154 std::optional<SkPathOvalInfo> asOval() const;
155
156 /**
157 * If the path is recognized as a round-rect, return it. If not, return {}.
158 */
159 std::optional<SkPathRRectInfo> asRRect() const;
160
161 /**
162 * Attempt to transform the pathdata by the matrix. If this succeeds, return a new
163 * pathdata object: note, this may have different verbs / number-of-points, if the
164 * matrix contained perspective.
165 *
166 * If the matrix has no effect on the coordinates (e.g. it is identity), then this may
167 * return a ref to the same pathdata object.
168 *
169 * If the result of applying the matrix creates any non-finite coordinates, this returns
170 * nullptr.
171 */
172 sk_sp<SkPathData> makeTransform(const SkMatrix&) const;
173 sk_sp<SkPathData> makeOffset(SkVector) const;
174
175 bool contains(SkPoint, SkPathFillType) const;
176
177 void addGenIDChangeListener(sk_sp<SkIDChangeListener>) const;
178 int genIDChangeListenerCount() const { return fGenIDChangeListeners.count(); }
179
180 private:
181 friend class SkNVRefCnt<SkPathData>;
182 friend class SkPathPriv;
183 friend class SkPath;
184 friend class SkPathBuilder;
185
186 // notify these in our destructor
187 mutable SkIDChangeListener::List fGenIDChangeListeners;
188
189 SkSpan<SkPoint> fPoints;
190 SkSpan<float> fConics;
191 SkSpan<SkPathVerb> fVerbs;
192 SkRect fBounds;
193
194 uint32_t fUniqueID; // never 0
195
196 /*
197 * Convexity can be slow to compute, and (in theory) it can't always survive a matrix
198 * transform (due to numeric instability). Therefore we will lazily compute it as
199 * requested. Since we are technically always immutable, we have to store this field
200 * in an atomic.
201 */
202 mutable std::atomic<uint8_t> fConvexity; // SkPathConvexity
203 uint8_t fSegmentMask; // SkPathSegmentMask
204 SkPathIsAType fType;
205 SkPathIsAData fIsA {};
206
207 //
208 // Memory layout after this (assuming we're not empty)
209 //
210 // [point data]
211 // [conic data]
212 // [verb data]
213 //
214
215 SkPathData(size_t npts, size_t nvbs, size_t ncns);
216
217 // Ensure the unsized delete is called (since we're manually allocating the storage)
218 void operator delete(void* p);
219
220 // internal finisher when building a PathData.
221 // If the optional value is not present, it will be computed (else checked in debug mode).
222 //
223 // In particular, if bounds is not provided, it will be computed, and if it proves
224 // to be non-finite, false will be returned.
225 bool finishInit(std::optional<SkRect> bounds, std::optional<uint8_t> segmentMask);
226
227 // If we know we're a special shape, call this after the normal initialization
228 void setupIsA(SkPathIsAType, SkPathDirection dir, unsigned startIndex);
229
230 SkPathConvexity getConvexityOrUnknown() const; // may return kUnknown
231 SkPathConvexity getResolvedConvexity() const; // never returns kUnknown
232 void setConvexity(SkPathConvexity) const; // const -- but convexity is mutable
233
234 static SkPathData* PeekEmptySingleton();
235
236 static sk_sp<SkPathData> Alloc(size_t npts, size_t nvbs, size_t ncns);
237
238 static sk_sp<SkPathData> MakeNoCheck(SkSpan<const SkPoint> pts,
239 SkSpan<const SkPathVerb> verbs,
240 SkSpan<const float> conics,
241 std::optional<SkRect> bounds,
242 std::optional<unsigned> segmentMask);
243 static sk_sp<SkPathData> MakeNoCheck(const SkPathRaw&);
244 };
245
246 #endif