blob: 0efea72a86c5954abe45c653a5b6739516e5b2d3 [file] [log] [blame]
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkPathRef_DEFINED
10#define SkPathRef_DEFINED
11
bungeman2c4bd072016-04-08 06:58:51 -070012#include "../private/SkAtomics.h"
bungemana7e9f052016-02-18 08:53:33 -080013#include "../private/SkTDArray.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000014#include "SkMatrix.h"
15#include "SkPoint.h"
caryclarkda707bf2015-11-19 14:47:43 -080016#include "SkRRect.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000017#include "SkRect.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000018#include "SkRefCnt.h"
Ben Wagnerac326622017-07-31 16:57:01 -040019#include "SkTemplates.h"
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000020
robertphillips@google.comca0c8382013-09-26 12:18:23 +000021class SkRBuffer;
22class SkWBuffer;
23
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000024/**
25 * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods
26 * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an
27 * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs
robertphillips@google.com466310d2013-12-03 16:43:54 +000028 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
bungeman6bd52842016-10-27 09:30:08 -070029 * constructor a pointer to a sk_sp<SkPathRef>, which may be updated to point to a new SkPathRef
30 * after the editor's constructor returns.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000031 *
32 * The points and verbs are stored in a single allocation. The points are at the begining of the
33 * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points
34 * and verbs both grow into the middle of the allocation until the meet. To access verb i in the
35 * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first
36 * logical verb or the last verb in memory).
37 */
bsalomon@google.comae09f2d2012-10-03 19:57:01 +000038
mtkleinb47cd4b2016-08-09 12:20:04 -070039class SK_API SkPathRef final : public SkNVRefCnt<SkPathRef> {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000040public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000041 class Editor {
42 public:
bungeman6bd52842016-10-27 09:30:08 -070043 Editor(sk_sp<SkPathRef>* pathRef,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000044 int incReserveVerbs = 0,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000045 int incReservePoints = 0);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000046
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +000047 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000048
49 /**
50 * Returns the array of points.
51 */
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000052 SkPoint* points() { return fPathRef->getPoints(); }
53 const SkPoint* points() const { return fPathRef->points(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000054
55 /**
56 * Gets the ith point. Shortcut for this->points() + i
57 */
58 SkPoint* atPoint(int i) {
59 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
60 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040061 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000062 const SkPoint* atPoint(int i) const {
63 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
64 return this->points() + i;
Mike Kleinfc6c37b2016-09-27 09:34:10 -040065 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000066
67 /**
68 * Adds the verb and allocates space for the number of points indicated by the verb. The
69 * return value is a pointer to where the points for the verb should be written.
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000070 * 'weight' is only used if 'verb' is kConic_Verb
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000071 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000072 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
robertphillips@google.com03087072013-10-02 16:42:21 +000073 SkDEBUGCODE(fPathRef->validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000074 return fPathRef->growForVerb(verb, weight);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000075 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000076
77 /**
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +000078 * Allocates space for multiple instances of a particular verb and the
79 * requisite points & weights.
80 * The return pointer points at the first new point (indexed normally [<i>]).
81 * If 'verb' is kConic_Verb, 'weights' will return a pointer to the
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000082 * space for the conic weights (indexed normally).
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000083 */
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000084 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
85 int numVbs,
86 SkScalar** weights = NULL) {
87 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000088 }
89
90 /**
91 * Resets the path ref to a new verb and point count. The new verbs and points are
92 * uninitialized.
93 */
reed@google.com277c3f82013-05-31 15:17:50 +000094 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
95 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000096 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +000097
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +000098 /**
99 * Gets the path ref that is wrapped in the Editor.
100 */
101 SkPathRef* pathRef() { return fPathRef; }
102
bsalomon78d58d12016-05-27 09:17:04 -0700103 void setIsOval(bool isOval, bool isCCW, unsigned start) {
104 fPathRef->setIsOval(isOval, isCCW, start);
105 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000106
bsalomon78d58d12016-05-27 09:17:04 -0700107 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
108 fPathRef->setIsRRect(isRRect, isCCW, start);
109 }
caryclarkda707bf2015-11-19 14:47:43 -0800110
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000111 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
112
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000113 private:
114 SkPathRef* fPathRef;
115 };
116
caryclarkda707bf2015-11-19 14:47:43 -0800117 class SK_API Iter {
118 public:
119 Iter();
120 Iter(const SkPathRef&);
121
122 void setPathRef(const SkPathRef&);
123
124 /** Return the next verb in this iteration of the path. When all
125 segments have been visited, return kDone_Verb.
126
127 @param pts The points representing the current verb and/or segment
128 This must not be NULL.
129 @return The verb for the current segment
130 */
131 uint8_t next(SkPoint pts[4]);
caryclark2028d7f2015-12-09 14:04:46 -0800132 uint8_t peek() const;
caryclarkda707bf2015-11-19 14:47:43 -0800133
134 SkScalar conicWeight() const { return *fConicWeights; }
135
136 private:
137 const SkPoint* fPts;
138 const uint8_t* fVerbs;
139 const uint8_t* fVerbStop;
140 const SkScalar* fConicWeights;
141 };
142
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000143public:
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000144 /**
145 * Gets a path ref with no verbs or points.
146 */
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000147 static SkPathRef* CreateEmpty();
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000148
149 /**
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000150 * Returns true if all of the points in this path are finite, meaning there
151 * are no infinities and no NaNs.
152 */
153 bool isFinite() const {
154 if (fBoundsIsDirty) {
155 this->computeBounds();
156 }
157 return SkToBool(fIsFinite);
158 }
159
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000160 /**
161 * Returns a mask, where each bit corresponding to a SegmentMask is
162 * set if the path contains 1 or more segments of that type.
163 * Returns 0 for an empty path (no segments).
164 */
165 uint32_t getSegmentMasks() const { return fSegmentMask; }
166
robertphillips@google.com466310d2013-12-03 16:43:54 +0000167 /** Returns true if the path is an oval.
168 *
169 * @param rect returns the bounding rect of this oval. It's a circle
170 * if the height and width are the same.
bsalomon78d58d12016-05-27 09:17:04 -0700171 * @param isCCW is the oval CCW (or CW if false).
172 * @param start indicates where the contour starts on the oval (see
173 * SkPath::addOval for intepretation of the index).
robertphillips@google.com466310d2013-12-03 16:43:54 +0000174 *
175 * @return true if this path is an oval.
176 * Tracking whether a path is an oval is considered an
177 * optimization for performance and so some paths that are in
178 * fact ovals can report false.
179 */
bsalomon78d58d12016-05-27 09:17:04 -0700180 bool isOval(SkRect* rect, bool* isCCW, unsigned* start) const {
181 if (fIsOval) {
182 if (rect) {
183 *rect = this->getBounds();
184 }
185 if (isCCW) {
186 *isCCW = SkToBool(fRRectOrOvalIsCCW);
187 }
188 if (start) {
189 *start = fRRectOrOvalStartIdx;
190 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000191 }
192
193 return SkToBool(fIsOval);
194 }
195
bsalomon78d58d12016-05-27 09:17:04 -0700196 bool isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const {
197 if (fIsRRect) {
198 if (rrect) {
199 *rrect = this->getRRect();
200 }
201 if (isCCW) {
202 *isCCW = SkToBool(fRRectOrOvalIsCCW);
203 }
204 if (start) {
205 *start = fRRectOrOvalStartIdx;
206 }
caryclarkda707bf2015-11-19 14:47:43 -0800207 }
208 return SkToBool(fIsRRect);
209 }
210
211
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000212 bool hasComputedBounds() const {
213 return !fBoundsIsDirty;
214 }
215
216 /** Returns the bounds of the path's points. If the path contains 0 or 1
217 points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
218 Note: this bounds may be larger than the actual shape, since curves
219 do not extend as far as their control points.
220 */
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +0000221 const SkRect& getBounds() const {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000222 if (fBoundsIsDirty) {
223 this->computeBounds();
224 }
225 return fBounds;
226 }
227
caryclarkda707bf2015-11-19 14:47:43 -0800228 SkRRect getRRect() const;
229
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000230 /**
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000231 * Transforms a path ref by a matrix, allocating a new one only if necessary.
232 */
bungeman6bd52842016-10-27 09:30:08 -0700233 static void CreateTransformedCopy(sk_sp<SkPathRef>* dst,
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000234 const SkPathRef& src,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000235 const SkMatrix& matrix);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000236
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000237 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000238
239 /**
240 * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be
241 * repopulated with approximately the same number of verbs and points. A new path ref is created
242 * only if necessary.
243 */
bungeman6bd52842016-10-27 09:30:08 -0700244 static void Rewind(sk_sp<SkPathRef>* pathRef);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000245
mtkleinb47cd4b2016-08-09 12:20:04 -0700246 ~SkPathRef();
robertphillips@google.com03087072013-10-02 16:42:21 +0000247 int countPoints() const { SkDEBUGCODE(this->validate();) return fPointCnt; }
248 int countVerbs() const { SkDEBUGCODE(this->validate();) return fVerbCnt; }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000249 int countWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.count(); }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000250
251 /**
252 * Returns a pointer one beyond the first logical verb (last verb in memory order).
253 */
robertphillips@google.com03087072013-10-02 16:42:21 +0000254 const uint8_t* verbs() const { SkDEBUGCODE(this->validate();) return fVerbs; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000255
256 /**
257 * Returns a const pointer to the first verb in memory (which is the last logical verb).
258 */
259 const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
260
261 /**
262 * Returns a const pointer to the first point.
263 */
robertphillips@google.com03087072013-10-02 16:42:21 +0000264 const SkPoint* points() const { SkDEBUGCODE(this->validate();) return fPoints; }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000265
266 /**
267 * Shortcut for this->points() + this->countPoints()
268 */
269 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
270
robertphillips@google.com03087072013-10-02 16:42:21 +0000271 const SkScalar* conicWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.begin(); }
272 const SkScalar* conicWeightsEnd() const { SkDEBUGCODE(this->validate();) return fConicWeights.end(); }
reed@google.com277c3f82013-05-31 15:17:50 +0000273
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000274 /**
275 * Convenience methods for getting to a verb or point by index.
276 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000277 uint8_t atVerb(int index) const {
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000278 SkASSERT((unsigned) index < (unsigned) fVerbCnt);
279 return this->verbs()[~index];
280 }
281 const SkPoint& atPoint(int index) const {
282 SkASSERT((unsigned) index < (unsigned) fPointCnt);
283 return this->points()[index];
284 }
285
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000286 bool operator== (const SkPathRef& ref) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000287
288 /**
289 * Writes the path points and verbs to a buffer.
290 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000291 void writeToBuffer(SkWBuffer* buffer) const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000292
293 /**
294 * Gets the number of bytes that would be written in writeBuffer()
295 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000296 uint32_t writeSize() const;
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000297
caryclark8e7b19d2016-02-18 04:11:48 -0800298 void interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const;
299
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000300 /**
301 * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the
302 * same ID then they have the same verbs and points. However, two path refs may have the same
303 * contents but different genIDs.
304 */
305 uint32_t genID() const;
306
senorblanco84cd6212015-08-04 10:01:58 -0700307 struct GenIDChangeListener {
308 virtual ~GenIDChangeListener() {}
309 virtual void onChange() = 0;
310 };
311
312 void addGenIDChangeListener(GenIDChangeListener* listener);
313
reed5bcbe912014-12-15 12:28:33 -0800314 SkDEBUGCODE(void validate() const;)
315
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000316private:
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000317 enum SerializationOffsets {
bsalomon78d58d12016-05-27 09:17:04 -0700318 kRRectOrOvalStartIdx_SerializationShift = 28, // requires 3 bits
319 kRRectOrOvalIsCCW_SerializationShift = 27, // requires 1 bit
320 kIsRRect_SerializationShift = 26, // requires 1 bit
321 kIsFinite_SerializationShift = 25, // requires 1 bit
322 kIsOval_SerializationShift = 24, // requires 1 bit
323 kSegmentMask_SerializationShift = 0 // requires 4 bits
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000324 };
325
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000326 SkPathRef() {
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000327 fBoundsIsDirty = true; // this also invalidates fIsFinite
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000328 fPointCnt = 0;
329 fVerbCnt = 0;
330 fVerbs = NULL;
331 fPoints = NULL;
332 fFreeSpace = 0;
333 fGenerationID = kEmptyGenID;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000334 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000335 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800336 fIsRRect = false;
bsalomon78d58d12016-05-27 09:17:04 -0700337 // The next two values don't matter unless fIsOval or fIsRRect are true.
senorblanco9c1d45d2016-07-22 13:51:42 -0700338 fRRectOrOvalIsCCW = false;
339 fRRectOrOvalStartIdx = 0xAC;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000340 SkDEBUGCODE(fEditorsAttached = 0;)
robertphillips@google.com03087072013-10-02 16:42:21 +0000341 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000342 }
343
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000344 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000345
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000346 // Return true if the computed bounds are finite.
347 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
reed91f283b2015-07-28 06:00:50 -0700348 return bounds->setBoundsCheck(ref.points(), ref.countPoints());
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000349 }
350
351 // called, if dirty, by getBounds()
352 void computeBounds() const {
353 SkDEBUGCODE(this->validate();)
Mike Klein0b4cc892014-07-16 17:18:20 -0400354 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
355 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
mtklein5c9c9be2014-12-01 06:59:54 -0800356 SkASSERT(fBoundsIsDirty);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000357
mtklein5c9c9be2014-12-01 06:59:54 -0800358 fIsFinite = ComputePtBounds(&fBounds, *this);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000359 fBoundsIsDirty = false;
360 }
361
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000362 void setBounds(const SkRect& rect) {
363 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
364 fBounds = rect;
365 fBoundsIsDirty = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800366 fIsFinite = fBounds.isFinite();
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000367 }
368
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000369 /** Makes additional room but does not change the counts or change the genID */
370 void incReserve(int additionalVerbs, int additionalPoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000371 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000372 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
373 this->makeSpace(space);
robertphillips@google.com03087072013-10-02 16:42:21 +0000374 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000375 }
376
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000377 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000378 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
reed@google.com277c3f82013-05-31 15:17:50 +0000379 void resetToSize(int verbCount, int pointCount, int conicCount,
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000380 int reserveVerbs = 0, int reservePoints = 0) {
381 SkDEBUGCODE(this->validate();)
382 fBoundsIsDirty = true; // this also invalidates fIsFinite
383 fGenerationID = 0;
384
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000385 fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000386 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800387 fIsRRect = false;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000388
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000389 size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
390 size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
391 size_t minSize = newSize + newReserve;
392
393 ptrdiff_t sizeDelta = this->currSize() - minSize;
394
395 if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
396 sk_free(fPoints);
397 fPoints = NULL;
398 fVerbs = NULL;
399 fFreeSpace = 0;
400 fVerbCnt = 0;
401 fPointCnt = 0;
402 this->makeSpace(minSize);
403 fVerbCnt = verbCount;
404 fPointCnt = pointCount;
405 fFreeSpace -= newSize;
406 } else {
407 fPointCnt = pointCount;
408 fVerbCnt = verbCount;
409 fFreeSpace = this->currSize() - minSize;
410 }
411 fConicWeights.setCount(conicCount);
412 SkDEBUGCODE(this->validate();)
413 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000414
415 /**
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000416 * Increases the verb count by numVbs and point count by the required amount.
417 * The new points are uninitialized. All the new verbs are set to the specified
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000418 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
419 * uninitialized conic weights.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000420 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000421 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000422
423 /**
424 * Increases the verb count 1, records the new verb, and creates room for the requisite number
425 * of additional points. A pointer to the first point is returned. Any new points are
426 * uninitialized.
427 */
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000428 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000429
430 /**
431 * Ensures that the free space available in the path ref is >= size. The verb and point counts
432 * are not changed.
433 */
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000434 void makeSpace(size_t size) {
435 SkDEBUGCODE(this->validate();)
Ben Wagnerac326622017-07-31 16:57:01 -0400436 if (size <= fFreeSpace) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000437 return;
438 }
Ben Wagnerac326622017-07-31 16:57:01 -0400439 size_t growSize = size - fFreeSpace;
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000440 size_t oldSize = this->currSize();
441 // round to next multiple of 8 bytes
442 growSize = (growSize + 7) & ~static_cast<size_t>(7);
443 // we always at least double the allocation
Ben Wagnerac326622017-07-31 16:57:01 -0400444 if (growSize < oldSize) {
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000445 growSize = oldSize;
446 }
447 if (growSize < kMinSize) {
448 growSize = kMinSize;
449 }
Ben Wagnerac326622017-07-31 16:57:01 -0400450 constexpr size_t maxSize = std::numeric_limits<size_t>::max();
451 size_t newSize;
452 if (growSize <= maxSize - oldSize) {
453 newSize = oldSize + growSize;
454 } else {
455 SK_ABORT("Path too big.");
456 }
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000457 // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
458 // encapsulate this.
459 fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
460 size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
Ben Wagnerac326622017-07-31 16:57:01 -0400461 void* newVerbsDst = SkTAddOffset<void>(fPoints, newSize - oldVerbSize);
462 void* oldVerbsSrc = SkTAddOffset<void>(fPoints, oldSize - oldVerbSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000463 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
Ben Wagnerac326622017-07-31 16:57:01 -0400464 fVerbs = SkTAddOffset<uint8_t>(fPoints, newSize);
robertphillips@google.comaaf3e642013-10-02 17:49:50 +0000465 fFreeSpace += growSize;
466 SkDEBUGCODE(this->validate();)
467 }
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000468
469 /**
470 * Private, non-const-ptr version of the public function verbsMemBegin().
471 */
472 uint8_t* verbsMemWritable() {
robertphillips@google.com03087072013-10-02 16:42:21 +0000473 SkDEBUGCODE(this->validate();)
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000474 return fVerbs - fVerbCnt;
475 }
476
477 /**
478 * Gets the total amount of space allocated for verbs, points, and reserve.
479 */
480 size_t currSize() const {
481 return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
482 }
483
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000484 /**
485 * Called the first time someone calls CreateEmpty to actually create the singleton.
486 */
mtklein148ec592014-10-13 13:17:56 -0700487 friend SkPathRef* sk_create_empty_pathref();
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +0000488
bsalomon78d58d12016-05-27 09:17:04 -0700489 void setIsOval(bool isOval, bool isCCW, unsigned start) {
490 fIsOval = isOval;
491 fRRectOrOvalIsCCW = isCCW;
492 fRRectOrOvalStartIdx = start;
493 }
robertphillips@google.com466310d2013-12-03 16:43:54 +0000494
bsalomon78d58d12016-05-27 09:17:04 -0700495 void setIsRRect(bool isRRect, bool isCCW, unsigned start) {
496 fIsRRect = isRRect;
497 fRRectOrOvalIsCCW = isCCW;
498 fRRectOrOvalStartIdx = start;
499 }
caryclarkda707bf2015-11-19 14:47:43 -0800500
501 // called only by the editor. Note that this is not a const function.
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000502 SkPoint* getPoints() {
503 SkDEBUGCODE(this->validate();)
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000504 fIsOval = false;
caryclarkda707bf2015-11-19 14:47:43 -0800505 fIsRRect = false;
506 return fPoints;
507 }
508
509 const SkPoint* getPoints() const {
510 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000511 return fPoints;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000512 }
513
senorblanco84cd6212015-08-04 10:01:58 -0700514 void callGenIDChangeListeners();
515
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000516 enum {
517 kMinSize = 256,
518 };
519
mtklein5c9c9be2014-12-01 06:59:54 -0800520 mutable SkRect fBounds;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000521
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000522 SkPoint* fPoints; // points to begining of the allocation
523 uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
524 int fVerbCnt;
525 int fPointCnt;
526 size_t fFreeSpace; // redundant but saves computation
reed@google.com277c3f82013-05-31 15:17:50 +0000527 SkTDArray<SkScalar> fConicWeights;
528
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000529 enum {
530 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
531 };
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000532 mutable uint32_t fGenerationID;
commit-bot@chromium.orgf48e4752013-06-27 18:39:39 +0000533 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000534
senorblanco84cd6212015-08-04 10:01:58 -0700535 SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned
536
caryclarkda707bf2015-11-19 14:47:43 -0800537 mutable uint8_t fBoundsIsDirty;
538 mutable SkBool8 fIsFinite; // only meaningful if bounds are valid
539
540 SkBool8 fIsOval;
541 SkBool8 fIsRRect;
bsalomon78d58d12016-05-27 09:17:04 -0700542 // Both the circle and rrect special cases have a notion of direction and starting point
543 // The next two variables store that information for either.
544 SkBool8 fRRectOrOvalIsCCW;
545 uint8_t fRRectOrOvalStartIdx;
caryclarkda707bf2015-11-19 14:47:43 -0800546 uint8_t fSegmentMask;
547
robertphillips@google.com0efb21b2013-12-13 19:36:25 +0000548 friend class PathRefTest_Private;
caryclarkda707bf2015-11-19 14:47:43 -0800549 friend class ForceIsRRect_Private; // unit test isRRect
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000550};
551
bsalomon@google.com1dfe88e2012-10-03 13:46:20 +0000552#endif