3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #ifndef SkPathRef_DEFINED
10 #define SkPathRef_DEFINED
12 #include "SkDynamicAnnotations.h"
17 #include "SkTDArray.h"
18 #include <stddef.h> // ptrdiff_t
24 * Holds the path verbs and points. It is versioned by a generation ID. None of its public methods
25 * modify the contents. To modify or append to the verbs/points wrap the SkPathRef in an
26 * SkPathRef::Editor object. Installing the editor resets the generation ID. It also performs
27 * copy-on-write if the SkPathRef is shared by multiple SkPaths. The caller passes the Editor's
28 * constructor a SkAutoTUnref, which may be updated to point to a new SkPathRef after the editor's
29 * constructor returns.
31 * The points and verbs are stored in a single allocation. The points are at the begining of the
32 * allocation while the verbs are stored at end of the allocation, in reverse order. Thus the points
33 * and verbs both grow into the middle of the allocation until the meet. To access verb i in the
34 * verb array use ref.verbs()[~i] (because verbs() returns a pointer just beyond the first
35 * logical verb or the last verb in memory).
38 class SK_API SkPathRef : public ::SkRefCnt {
40 SK_DECLARE_INST_COUNT(SkPathRef);
44 Editor(SkAutoTUnref<SkPathRef>* pathRef,
45 int incReserveVerbs = 0,
46 int incReservePoints = 0);
48 ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
51 * Returns the array of points.
53 SkPoint* points() { return fPathRef->getPoints(); }
54 const SkPoint* points() const { return fPathRef->points(); }
57 * Gets the ith point. Shortcut for this->points() + i
59 SkPoint* atPoint(int i) {
60 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
61 return this->points() + i;
63 const SkPoint* atPoint(int i) const {
64 SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
65 return this->points() + i;
69 * Adds the verb and allocates space for the number of points indicated by the verb. The
70 * return value is a pointer to where the points for the verb should be written.
71 * 'weight' is only used if 'verb' is kConic_Verb
73 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
74 SkDEBUGCODE(fPathRef->validate();)
75 return fPathRef->growForVerb(verb, weight);
79 * Allocates space for multiple instances of a particular verb and the
80 * requisite points & weights.
81 * The return pointer points at the first new point (indexed normally [<i>]).
82 * If 'verb' is kConic_Verb, 'weights' will return a pointer to the
83 * space for the conic weights (indexed normally).
85 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
87 SkScalar** weights = NULL) {
88 return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
92 * Resets the path ref to a new verb and point count. The new verbs and points are
95 void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
96 fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
100 * Gets the path ref that is wrapped in the Editor.
102 SkPathRef* pathRef() { return fPathRef; }
104 void setIsOval(bool isOval) { fPathRef->setIsOval(isOval); }
106 void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
114 * Gets a path ref with no verbs or points.
116 static SkPathRef* CreateEmpty();
119 * Returns true if all of the points in this path are finite, meaning there
120 * are no infinities and no NaNs.
122 bool isFinite() const {
123 if (fBoundsIsDirty) {
124 this->computeBounds();
126 return SkToBool(fIsFinite);
130 * Returns a mask, where each bit corresponding to a SegmentMask is
131 * set if the path contains 1 or more segments of that type.
132 * Returns 0 for an empty path (no segments).
134 uint32_t getSegmentMasks() const { return fSegmentMask; }
136 /** Returns true if the path is an oval.
138 * @param rect returns the bounding rect of this oval. It's a circle
139 * if the height and width are the same.
141 * @return true if this path is an oval.
142 * Tracking whether a path is an oval is considered an
143 * optimization for performance and so some paths that are in
144 * fact ovals can report false.
146 bool isOval(SkRect* rect) const {
147 if (fIsOval && rect) {
151 return SkToBool(fIsOval);
154 bool hasComputedBounds() const {
155 return !fBoundsIsDirty;
158 /** Returns the bounds of the path's points. If the path contains 0 or 1
159 points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
160 Note: this bounds may be larger than the actual shape, since curves
161 do not extend as far as their control points.
163 const SkRect& getBounds() const {
164 if (fBoundsIsDirty) {
165 this->computeBounds();
171 * Transforms a path ref by a matrix, allocating a new one only if necessary.
173 static void CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
174 const SkPathRef& src,
175 const SkMatrix& matrix);
177 static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
180 * Rollsback a path ref to zero verbs and points with the assumption that the path ref will be
181 * repopulated with approximately the same number of verbs and points. A new path ref is created
184 static void Rewind(SkAutoTUnref<SkPathRef>* pathRef);
186 virtual ~SkPathRef() {
187 SkDEBUGCODE(this->validate();)
190 SkDEBUGCODE(fPoints = NULL;)
191 SkDEBUGCODE(fVerbs = NULL;)
192 SkDEBUGCODE(fVerbCnt = 0x9999999;)
193 SkDEBUGCODE(fPointCnt = 0xAAAAAAA;)
194 SkDEBUGCODE(fPointCnt = 0xBBBBBBB;)
195 SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;)
196 SkDEBUGCODE(fEditorsAttached = 0x7777777;)
199 int countPoints() const { SkDEBUGCODE(this->validate();) return fPointCnt; }
200 int countVerbs() const { SkDEBUGCODE(this->validate();) return fVerbCnt; }
201 int countWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.count(); }
204 * Returns a pointer one beyond the first logical verb (last verb in memory order).
206 const uint8_t* verbs() const { SkDEBUGCODE(this->validate();) return fVerbs; }
209 * Returns a const pointer to the first verb in memory (which is the last logical verb).
211 const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
214 * Returns a const pointer to the first point.
216 const SkPoint* points() const { SkDEBUGCODE(this->validate();) return fPoints; }
219 * Shortcut for this->points() + this->countPoints()
221 const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
223 const SkScalar* conicWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.begin(); }
224 const SkScalar* conicWeightsEnd() const { SkDEBUGCODE(this->validate();) return fConicWeights.end(); }
227 * Convenience methods for getting to a verb or point by index.
229 uint8_t atVerb(int index) const {
230 SkASSERT((unsigned) index < (unsigned) fVerbCnt);
231 return this->verbs()[~index];
233 const SkPoint& atPoint(int index) const {
234 SkASSERT((unsigned) index < (unsigned) fPointCnt);
235 return this->points()[index];
238 bool operator== (const SkPathRef& ref) const;
241 * Writes the path points and verbs to a buffer.
243 void writeToBuffer(SkWBuffer* buffer) const;
246 * Gets the number of bytes that would be written in writeBuffer()
248 uint32_t writeSize() const;
251 * Gets an ID that uniquely identifies the contents of the path ref. If two path refs have the
252 * same ID then they have the same verbs and points. However, two path refs may have the same
253 * contents but different genIDs.
255 uint32_t genID() const;
258 enum SerializationOffsets {
259 kIsFinite_SerializationShift = 25, // requires 1 bit
260 kIsOval_SerializationShift = 24, // requires 1 bit
261 kSegmentMask_SerializationShift = 0 // requires 4 bits
265 fBoundsIsDirty = true; // this also invalidates fIsFinite
271 fGenerationID = kEmptyGenID;
274 SkDEBUGCODE(fEditorsAttached = 0;)
275 SkDEBUGCODE(this->validate();)
278 void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
280 // Return true if the computed bounds are finite.
281 static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
282 int count = ref.countPoints();
283 if (count <= 1) { // we ignore just 1 point (moveto)
285 return count ? ref.points()->isFinite() : true;
287 return bounds->setBoundsCheck(ref.points(), count);
291 // called, if dirty, by getBounds()
292 void computeBounds() const {
293 SkDEBUGCODE(this->validate();)
294 // TODO(mtklein): remove fBoundsIsDirty and fIsFinite,
295 // using an inverted rect instead of fBoundsIsDirty and always recalculating fIsFinite.
296 //SkASSERT(fBoundsIsDirty);
298 fIsFinite = ComputePtBounds(fBounds.get(), *this);
299 fBoundsIsDirty = false;
302 void setBounds(const SkRect& rect) {
303 SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
305 fBoundsIsDirty = false;
306 fIsFinite = fBounds->isFinite();
309 /** Makes additional room but does not change the counts or change the genID */
310 void incReserve(int additionalVerbs, int additionalPoints) {
311 SkDEBUGCODE(this->validate();)
312 size_t space = additionalVerbs * sizeof(uint8_t) + additionalPoints * sizeof (SkPoint);
313 this->makeSpace(space);
314 SkDEBUGCODE(this->validate();)
317 /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
318 * allocates space for reserveVerb additional verbs and reservePoints additional points.*/
319 void resetToSize(int verbCount, int pointCount, int conicCount,
320 int reserveVerbs = 0, int reservePoints = 0) {
321 SkDEBUGCODE(this->validate();)
322 fBoundsIsDirty = true; // this also invalidates fIsFinite
328 size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
329 size_t newReserve = sizeof(uint8_t) * reserveVerbs + sizeof(SkPoint) * reservePoints;
330 size_t minSize = newSize + newReserve;
332 ptrdiff_t sizeDelta = this->currSize() - minSize;
334 if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
341 this->makeSpace(minSize);
342 fVerbCnt = verbCount;
343 fPointCnt = pointCount;
344 fFreeSpace -= newSize;
346 fPointCnt = pointCount;
347 fVerbCnt = verbCount;
348 fFreeSpace = this->currSize() - minSize;
350 fConicWeights.setCount(conicCount);
351 SkDEBUGCODE(this->validate();)
355 * Increases the verb count by numVbs and point count by the required amount.
356 * The new points are uninitialized. All the new verbs are set to the specified
357 * verb. If 'verb' is kConic_Verb, 'weights' will return a pointer to the
358 * uninitialized conic weights.
360 SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
363 * Increases the verb count 1, records the new verb, and creates room for the requisite number
364 * of additional points. A pointer to the first point is returned. Any new points are
367 SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
370 * Ensures that the free space available in the path ref is >= size. The verb and point counts
373 void makeSpace(size_t size) {
374 SkDEBUGCODE(this->validate();)
375 ptrdiff_t growSize = size - fFreeSpace;
379 size_t oldSize = this->currSize();
380 // round to next multiple of 8 bytes
381 growSize = (growSize + 7) & ~static_cast<size_t>(7);
382 // we always at least double the allocation
383 if (static_cast<size_t>(growSize) < oldSize) {
386 if (growSize < kMinSize) {
389 size_t newSize = oldSize + growSize;
390 // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
392 fPoints = reinterpret_cast<SkPoint*>(sk_realloc_throw(fPoints, newSize));
393 size_t oldVerbSize = fVerbCnt * sizeof(uint8_t);
394 void* newVerbsDst = reinterpret_cast<void*>(
395 reinterpret_cast<intptr_t>(fPoints) + newSize - oldVerbSize);
396 void* oldVerbsSrc = reinterpret_cast<void*>(
397 reinterpret_cast<intptr_t>(fPoints) + oldSize - oldVerbSize);
398 memmove(newVerbsDst, oldVerbsSrc, oldVerbSize);
399 fVerbs = reinterpret_cast<uint8_t*>(reinterpret_cast<intptr_t>(fPoints) + newSize);
400 fFreeSpace += growSize;
401 SkDEBUGCODE(this->validate();)
405 * Private, non-const-ptr version of the public function verbsMemBegin().
407 uint8_t* verbsMemWritable() {
408 SkDEBUGCODE(this->validate();)
409 return fVerbs - fVerbCnt;
413 * Gets the total amount of space allocated for verbs, points, and reserve.
415 size_t currSize() const {
416 return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
419 SkDEBUGCODE(void validate() const;)
422 * Called the first time someone calls CreateEmpty to actually create the singleton.
424 static SkPathRef* CreateEmptyImpl();
426 void setIsOval(bool isOval) { fIsOval = isOval; }
428 SkPoint* getPoints() {
429 SkDEBUGCODE(this->validate();)
438 mutable SkTRacyReffable<SkRect> fBounds;
439 mutable SkTRacy<uint8_t> fBoundsIsDirty;
440 mutable SkTRacy<SkBool8> fIsFinite; // only meaningful if bounds are valid
443 uint8_t fSegmentMask;
445 SkPoint* fPoints; // points to begining of the allocation
446 uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
449 size_t fFreeSpace; // redundant but saves computation
450 SkTDArray<SkScalar> fConicWeights;
453 kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
455 mutable uint32_t fGenerationID;
456 SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
458 friend class PathRefTest_Private;
459 typedef SkRefCnt INHERITED;