blob: 49cc28fb51faf036068cc67ac4b122ebc8e8aac3 [file] [log] [blame]
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +00001/*
2 * Copyright 2012 Google Inc.
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 SkPathRef_DEFINED
9#define SkPathRef_DEFINED
10
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "SkAtomics.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000012#include "SkMatrix.h"
Mike Klein0191ed82018-09-17 17:29:39 -040013#include "SkMutex.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000014#include "SkPoint.h"
caryclarkda707bf2015-11-19 14:47:43 -080015#include "SkRRect.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000016#include "SkRect.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000017#include "SkRefCnt.h"
Chris Daltonf30e4932018-10-18 18:56:59 -060018#include "SkTArray.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkTDArray.h"
Ben Wagnerac326622017-07-31 16:57:01 -040020#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040021#include "SkTo.h"
22
Mike Kleinbf45c702018-06-11 11:56:57 -040023#include <limits>
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000024
robertphillips@google.comca0c8382013-09-26 12:18:23 +000025class SkRBuffer;
26class SkWBuffer;
27
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000028/**
29 * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods
30 * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an
31 * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs
robertphillips@google.com466310d2013-12-03 16:43:54 +000032 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
bungeman6bd52842016-10-27 09:30:08 -070033 * constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef
34 * after the editor's constructor returns.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000035 *
36 * The points and verbs are stored in a single allocation. The points are at the begining of the
37 * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points
38 * and verbs both grow into the middle of the allocation until the meet. To access verb i in the
39 * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first
40 * logical verb or the last verb in memory).
41 */
bsalomon@google.comae09f2d2012-10-03 19:57:01 +000042
mtkleinb47cd4b2016-08-09 12:20:04 -070043class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000044public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000045 class Editor {
46 public:
bungeman6bd52842016-10-27 09:30:08 -070047 Editor(sk_sp<SkPathRef>* pathRef,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000048 int incReserveVerbs = 0,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000049 int incReservePoints = 0);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000050
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +000051 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000052
53 /**
54 * Returns the array of points.
55 */
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000056 SkPoint* points() { return fPathRef->getPoints(); }
57 const SkPoint* points() const { return fPathRef->points(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000058
59 /**
60 * Gets the ith point. Shortcut for this->points() + i
61 */
62 SkPoint* atPoint(int i) {
63 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
64 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040065 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000066 const SkPoint* atPoint(int i) const {
67 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
68 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040069 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000070
71 /**
72 * Adds the verb and allocates space for the number of points indicated by the verb. The
73 * return value is a pointer to where the points for the verb should be written.
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000074 * 'weight' is only used if 'verb' is kConic_Verb
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000075 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000076 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
robertphillips@google.com03087072013-10-02 16:42:21 +000077 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000078 return fPathRef->growForVerb(verb, weight);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000079 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000080
81 /**
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000082 * Allocates space for multiple instances of a particular verb and the
83 * requisite points & weights.
84 * The return pointer points at the first new point (indexed normally [<i>]).
85 * If 'verb' is kConic_Verb, 'weights' will return a pointer to the
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000086 * space for the conic weights (indexed normally).
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000087 */
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000088 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
89 int numVbs,
Ben Wagnera93a14a2017-08-28 10:34:05 -040090 SkScalar** weights = nullptr) {
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000091 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000092 }
93
94 /**
95 * Resets the path ref to a new verb and point count. The new verbs and points are
96 * uninitialized.
97 */
reed@google.com277c3f82013-05-31 15:17:50 +000098 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
99 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000100 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000101
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000102 /**
103 * Gets the path ref that is wrapped in the Editor.
104 */
105 SkPathRef* pathRef() { return fPathRef; }
106
bsalomon78d58d12016-05-27 09:17:04 -0700107 void setIsOval(bool isOval, bool isCCW, unsigned start) {
108 fPathRef->setIsOval(isOval, isCCW, start);
109 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000110
bsalomon78d58d12016-05-27 09:17:04 -0700111 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
112 fPathRef->setIsRRect(isRRect, isCCW, start);
113 }
caryclarkda707bf2015-11-19 14:47:43 -0800114
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000115 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
116
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000117 private:
118 SkPathRef* fPathRef;
119 };
120
caryclarkda707bf2015-11-19 14:47:43 -0800121 class SK_API Iter {
122 public:
123 Iter();
124 Iter(const SkPathRef&);
125
126 void setPathRef(const SkPathRef&);
127
128 /** Return the next verb in this iteration of the path. When all
129 segments have been visited, return kDone_Verb.
130
Mike Klein1170a552017-09-08 15:00:25 -0400131 If any point in the path is non-finite, return kDone_Verb immediately.
132
caryclarkda707bf2015-11-19 14:47:43 -0800133 @param pts The points representing the current verb and/or segment
134 This must not be NULL.
135 @return The verb for the current segment
136 */
137 uint8_t next(SkPoint pts[4]);
caryclark2028d7f2015-12-09 14:04:46 -0800138 uint8_t peek() const;
caryclarkda707bf2015-11-19 14:47:43 -0800139
140 SkScalar conicWeight() const { return *fConicWeights; }
141
142 private:
143 const SkPoint* fPts;
144 const uint8_t* fVerbs;
145 const uint8_t* fVerbStop;
146 const SkScalar* fConicWeights;
147 };
148
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000149public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000150 /**
151 * Gets a path ref with no verbs or points.
152 */
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000153 static SkPathRef* CreateEmpty();
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000154
155 /**
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000156 * Returns true if all of the points in this path are finite, meaning there
157 * are no infinities and no NaNs.
158 */
159 bool isFinite() const {
160 if (fBoundsIsDirty) {
161 this->computeBounds();
162 }
163 return SkToBool(fIsFinite);
164 }
165
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000166 /**
167 * Returns a mask, where each bit corresponding to a SegmentMask is
168 * set if the path contains 1 or more segments of that type.
169 * Returns 0 for an empty path (no segments).
170 */
171 uint32_t getSegmentMasks() const { return fSegmentMask; }
172
robertphillips@google.com466310d2013-12-03 16:43:54 +0000173 /** Returns true if the path is an oval.
174 *
175 * @param rect returns the bounding rect of this oval. It's a circle
176 * if the height and width are the same.
bsalomon78d58d12016-05-27 09:17:04 -0700177 * @param isCCW is the oval CCW (or CW if false).
178 * @param start indicates where the contour starts on the oval (see
179 * SkPath::addOval for intepretation of the index).
robertphillips@google.com466310d2013-12-03 16:43:54 +0000180 *
181 * @return true if this path is an oval.
182 * Tracking whether a path is an oval is considered an
183 * optimization for performance and so some paths that are in
184 * fact ovals can report false.
185 */
bsalomon78d58d12016-05-27 09:17:04 -0700186 bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const {
187 if (fIsOval) {
188 if (rect) {
189 *rect = this->getBounds();
190 }
191 if (isCCW) {
192 *isCCW = SkToBool(fRRectOrOvalIsCCW);
193 }
194 if (start) {
195 *start = fRRectOrOvalStartIdx;
196 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000197 }
198
199 return SkToBool(fIsOval);
200 }
201
bsalomon78d58d12016-05-27 09:17:04 -0700202 bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const {
203 if (fIsRRect) {
204 if (rrect) {
205 *rrect = this->getRRect();
206 }
207 if (isCCW) {
208 *isCCW = SkToBool(fRRectOrOvalIsCCW);
209 }
210 if (start) {
211 *start = fRRectOrOvalStartIdx;
212 }
caryclarkda707bf2015-11-19 14:47:43 -0800213 }
214 return SkToBool(fIsRRect);
215 }
216
217
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000218 bool hasComputedBounds() const {
219 return !fBoundsIsDirty;
220 }
221
222 /** Returns the bounds of the path's points. If the path contains 0 or 1
223 points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
224 Note: this bounds may be larger than the actual shape, since curves
225 do not extend as far as their control points.
226 */
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000227 const SkRect& getBounds() const {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000228 if (fBoundsIsDirty) {
229 this->computeBounds();
230 }
231 return fBounds;
232 }
233
caryclarkda707bf2015-11-19 14:47:43 -0800234 SkRRect getRRect() const;
235
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000236 /**
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000237 * Transforms a path ref by a matrix, allocating a new one only if necessary.
238 */
bungeman6bd52842016-10-27 09:30:08 -0700239 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000240 const SkPathRef& src,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000241 const SkMatrix& matrix);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000242
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000243 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000244
245 /**
246 * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be
247 * repopulated with approximately the same number of verbs and points. A new path ref is created
248 * only if necessary.
249 */
bungeman6bd52842016-10-27 09:30:08 -0700250 static void Rewind(sk_sp<SkPathRef>* pathRef);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000251
mtkleinb47cd4b2016-08-09 12:20:04 -0700252 ~SkPathRef();
Brian Osman03776fc2017-08-15 16:08:48 -0400253 int countPoints() const { return fPointCnt; }
254 int countVerbs() const { return fVerbCnt; }
255 int countWeights() const { return fConicWeights.count(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000256
257 /**
258 * Returns a pointer one beyond the first logical verb (last verb in memory order).
259 */
Brian Osman03776fc2017-08-15 16:08:48 -0400260 const uint8_t* verbs() const { return fVerbs; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000261
262 /**
263 * Returns a const pointer to the first verb in memory (which is the last logical verb).
264 */
265 const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
266
267 /**
268 * Returns a const pointer to the first point.
269 */
Brian Osman03776fc2017-08-15 16:08:48 -0400270 const SkPoint* points() const { return fPoints; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000271
272 /**
273 * Shortcut for this->points() + this->countPoints()
274 */
275 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
276
Brian Osman03776fc2017-08-15 16:08:48 -0400277 const SkScalar* conicWeights() const { return fConicWeights.begin(); }
278 const SkScalar* conicWeightsEnd() const { return fConicWeights.end(); }
reed@google.com277c3f82013-05-31 15:17:50 +0000279
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000280 /**
281 * Convenience methods for getting to a verb or point by index.
282 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000283 uint8_t atVerb(int index) const {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000284 SkASSERT((unsigned) index < (unsigned) fVerbCnt);
285 return this->verbs()[~index];
286 }
287 const SkPoint& atPoint(int index) const {
288 SkASSERT((unsigned) index < (unsigned) fPointCnt);
289 return this->points()[index];
290 }
291
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000292 bool operator== (const SkPathRef& ref) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000293
294 /**
295 * Writes the path points and verbs to a buffer.
296 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000297 void writeToBuffer(SkWBuffer* buffer) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000298
299 /**
300 * Gets the number of bytes that would be written in writeBuffer()
301 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000302 uint32_t writeSize() const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000303
caryclark8e7b19d2016-02-18 04:11:48 -0800304 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
305
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000306 /**
307 * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the
308 * same ID then they have the same verbs and points. However, two path refs may have the same
309 * contents but different genIDs.
310 */
311 uint32_t genID() const;
312
Chris Daltonf30e4932018-10-18 18:56:59 -0600313 class GenIDChangeListener : public SkRefCnt {
314 public:
315 GenIDChangeListener() : fShouldUnregisterFromPath(false) {}
senorblanco84cd6212015-08-04 10:01:58 -0700316 virtual ~GenIDChangeListener() {}
Chris Daltonf30e4932018-10-18 18:56:59 -0600317
senorblanco84cd6212015-08-04 10:01:58 -0700318 virtual void onChange() = 0;
Chris Daltonf30e4932018-10-18 18:56:59 -0600319
320 // The caller can use this method to notify the path that it no longer needs to listen. Once
321 // called, the path will remove this listener from the list at some future point.
322 void markShouldUnregisterFromPath() {
323 fShouldUnregisterFromPath.store(true, std::memory_order_relaxed);
324 }
325 bool shouldUnregisterFromPath() {
326 return fShouldUnregisterFromPath.load(std::memory_order_relaxed);
327 }
328
329 private:
330 mutable std::atomic<bool> fShouldUnregisterFromPath;
senorblanco84cd6212015-08-04 10:01:58 -0700331 };
332
Mike Klein0191ed82018-09-17 17:29:39 -0400333 void addGenIDChangeListener(sk_sp<GenIDChangeListener>); // Threadsafe.
senorblanco84cd6212015-08-04 10:01:58 -0700334
Adrienne Walkerad8da8e2017-08-10 12:16:37 -0700335 bool isValid() const;
336 SkDEBUGCODE(void validate() const { SkASSERT(this->isValid()); } )
reed5bcbe912014-12-15 12:28:33 -0800337
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000338private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000339 enum SerializationOffsets {
Brian Salomonb7d42e32017-09-21 16:59:17 -0400340 kLegacyRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits, ignored.
341 kLegacyRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit, ignored.
342 kLegacyIsRRect_SerializationShift = 26, // requires 1 bit, ignored.
343 kIsFinite_SerializationShift = 25, // requires 1 bit
344 kLegacyIsOval_SerializationShift = 24, // requires 1 bit, ignored.
Mike Reeda3d9e212018-02-20 09:48:13 -0500345 kSegmentMask_SerializationShift = 0 // requires 4 bits (deprecated)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000346 };
347
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000348 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000349 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000350 fPointCnt = 0;
351 fVerbCnt = 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -0400352 fVerbs = nullptr;
353 fPoints = nullptr;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000354 fFreeSpace = 0;
355 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000356 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000357 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800358 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700359 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700360 fRRectOrOvalIsCCW = false;
361 fRRectOrOvalStartIdx = 0xAC;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000362 SkDEBUGCODE(fEditorsAttached = 0;)
robertphillips@google.com03087072013-10-02 16:42:21 +0000363 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000364 }
365
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000366 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000367
Mike Reeda3d9e212018-02-20 09:48:13 -0500368 // Doesn't read fSegmentMask, but (re)computes it from the verbs array
369 unsigned computeSegmentMask() const;
370
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000371 // Return true if the computed bounds are finite.
372 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700373 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000374 }
375
376 // called, if dirty, by getBounds()
377 void computeBounds() const {
378 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400379 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
380 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800381 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000382
mtklein5c9c9be2014-12-01 06:59:54 -0800383 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000384 fBoundsIsDirty = false;
385 }
386
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000387 void setBounds(const SkRect& rect) {
388 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
389 fBounds = rect;
390 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800391 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000392 }
393
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000394 /** Makes additional room but does not change the counts or change the genID */
395 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000396 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000397 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
398 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000399 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000400 }
401
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000402 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000403 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000404 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000405 int reserveVerbs = 0, int reservePoints = 0) {
406 SkDEBUGCODE(this->validate();)
407 fBoundsIsDirty = true; // this also invalidates fIsFinite
408 fGenerationID = 0;
409
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000410 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000411 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800412 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000413
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000414 size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
415 size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
416 size_t minSize = newSize + newReserve;
417
418 ptrdiff_t sizeDelta = this->currSize() - minSize;
419
420 if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
421 sk_free(fPoints);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400422 fPoints = nullptr;
423 fVerbs = nullptr;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000424 fFreeSpace = 0;
425 fVerbCnt = 0;
426 fPointCnt = 0;
Florin Malita3d413c52018-09-11 14:01:42 -0400427 this->makeSpace(minSize, true);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000428 fVerbCnt = verbCount;
429 fPointCnt = pointCount;
430 fFreeSpace -= newSize;
431 } else {
432 fPointCnt = pointCount;
433 fVerbCnt = verbCount;
434 fFreeSpace = this->currSize() - minSize;
435 }
436 fConicWeights.setCount(conicCount);
437 SkDEBUGCODE(this->validate();)
438 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000439
440 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000441 * Increases the verb count by numVbs and point count by the required amount.
442 * The new points are uninitialized. All the new verbs are set to the specified
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000443 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
444 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000445 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000446 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000447
448 /**
449 * Increases the verb count 1, records the new verb, and creates room for the requisite number
450 * of additional points. A pointer to the first point is returned. Any new points are
451 * uninitialized.
452 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000453 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000454
455 /**
456 * Ensures that the free space available in the path ref is >= size. The verb and point counts
Florin Malita3d413c52018-09-11 14:01:42 -0400457 * are not changed. May allocate extra capacity, unless |exact| is true.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000458 */
Florin Malita3d413c52018-09-11 14:01:42 -0400459 void makeSpace(size_t size, bool exact = false) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000460 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400461 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000462 return;
463 }
Ben Wagnerac326622017-07-31 16:57:01 -0400464 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000465 size_t oldSize = this->currSize();
Florin Malita3d413c52018-09-11 14:01:42 -0400466
467 if (!exact) {
468 // round to next multiple of 8 bytes
469 growSize = (growSize + 7) & ~static_cast<size_t>(7);
470 // we always at least double the allocation
471 if (growSize < oldSize) {
472 growSize = oldSize;
473 }
474 if (growSize < kMinSize) {
475 growSize = kMinSize;
476 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000477 }
Florin Malita3d413c52018-09-11 14:01:42 -0400478
Ben Wagnerac326622017-07-31 16:57:01 -0400479 constexpr size_t maxSize = std::numeric_limits<size_t>::max();
480 size_t newSize;
481 if (growSize <= maxSize - oldSize) {
482 newSize = oldSize + growSize;
483 } else {
484 SK_ABORT("Path too big.");
485 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000486 // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
487 // encapsulate this.
488 fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
489 size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
Ben Wagnerac326622017-07-31 16:57:01 -0400490 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
491 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000492 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400493 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000494 fFreeSpace += growSize;
495 SkDEBUGCODE(this->validate();)
496 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000497
498 /**
499 * Private, non-const-ptr version of the public function verbsMemBegin().
500 */
501 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000502 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000503 return fVerbs - fVerbCnt;
504 }
505
506 /**
507 * Gets the total amount of space allocated for verbs, points, and reserve.
508 */
509 size_t currSize() const {
510 return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
511 }
512
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000513 /**
514 * Called the first time someone calls CreateEmpty to actually create the singleton.
515 */
mtklein148ec592014-10-13 13:17:56 -0700516 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000517
bsalomon78d58d12016-05-27 09:17:04 -0700518 void setIsOval(bool isOval, bool isCCW, unsigned start) {
519 fIsOval = isOval;
520 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200521 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700522 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000523
bsalomon78d58d12016-05-27 09:17:04 -0700524 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
525 fIsRRect = isRRect;
526 fRRectOrOvalIsCCW = isCCW;
Kaloyan Donevdfffb392018-03-20 10:38:31 +0200527 fRRectOrOvalStartIdx = SkToU8(start);
bsalomon78d58d12016-05-27 09:17:04 -0700528 }
caryclarkda707bf2015-11-19 14:47:43 -0800529
530 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000531 SkPoint* getPoints() {
532 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000533 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800534 fIsRRect = false;
535 return fPoints;
536 }
537
538 const SkPoint* getPoints() const {
539 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000540 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000541 }
542
senorblanco84cd6212015-08-04 10:01:58 -0700543 void callGenIDChangeListeners();
544
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000545 enum {
546 kMinSize = 256,
547 };
548
mtklein5c9c9be2014-12-01 06:59:54 -0800549 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000550
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000551 SkPoint* fPoints; // points to begining of the allocation
552 uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
553 int fVerbCnt;
554 int fPointCnt;
555 size_t fFreeSpace; // redundant but saves computation
reed@google.com277c3f82013-05-31 15:17:50 +0000556 SkTDArray<SkScalar> fConicWeights;
557
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000558 enum {
559 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
560 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000561 mutable uint32_t fGenerationID;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000562 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000563
Chris Daltonf30e4932018-10-18 18:56:59 -0600564 SkMutex fGenIDChangeListenersMutex;
565 SkTArray<sk_sp<GenIDChangeListener>> fGenIDChangeListeners;
senorblanco84cd6212015-08-04 10:01:58 -0700566
caryclarkda707bf2015-11-19 14:47:43 -0800567 mutable uint8_t fBoundsIsDirty;
Ben Wagnercee46e52018-06-12 16:30:29 -0400568 mutable bool fIsFinite; // only meaningful if bounds are valid
caryclarkda707bf2015-11-19 14:47:43 -0800569
Ben Wagnercee46e52018-06-12 16:30:29 -0400570 bool fIsOval;
571 bool fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700572 // Both the circle and rrect special cases have a notion of direction and starting point
573 // The next two variables store that information for either.
Ben Wagnercee46e52018-06-12 16:30:29 -0400574 bool fRRectOrOvalIsCCW;
bsalomon78d58d12016-05-27 09:17:04 -0700575 uint8_t fRRectOrOvalStartIdx;
caryclarkda707bf2015-11-19 14:47:43 -0800576 uint8_t fSegmentMask;
577
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000578 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800579 friend class ForceIsRRect_Private; // unit test isRRect
Florin Malita3d413c52018-09-11 14:01:42 -0400580 friend class SkPath;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000581};
582
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000583#endif