Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkPathRef.h
1
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
12 #include "SkDynamicAnnotations.h"
13 #include "SkMatrix.h"
14 #include "SkPoint.h"
15 #include "SkRect.h"
16 #include "SkRefCnt.h"
17 #include "SkTDArray.h"
18 #include <stddef.h> // ptrdiff_t
19
20 class SkRBuffer;
21 class SkWBuffer;
22
23 /**
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.
30  *
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).
36  */
37
38 class SK_API SkPathRef : public ::SkRefCnt {
39 public:
40     SK_DECLARE_INST_COUNT(SkPathRef);
41
42     class Editor {
43     public:
44         Editor(SkAutoTUnref<SkPathRef>* pathRef,
45                int incReserveVerbs = 0,
46                int incReservePoints = 0);
47
48         ~Editor() { SkDEBUGCODE(sk_atomic_dec(&fPathRef->fEditorsAttached);) }
49
50         /**
51          * Returns the array of points.
52          */
53         SkPoint* points() { return fPathRef->getPoints(); }
54         const SkPoint* points() const { return fPathRef->points(); }
55
56         /**
57          * Gets the ith point. Shortcut for this->points() + i
58          */
59         SkPoint* atPoint(int i) {
60             SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
61             return this->points() + i;
62         };
63         const SkPoint* atPoint(int i) const {
64             SkASSERT((unsigned) i < (unsigned) fPathRef->fPointCnt);
65             return this->points() + i;
66         };
67
68         /**
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
72          */
73         SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight = 0) {
74             SkDEBUGCODE(fPathRef->validate();)
75             return fPathRef->growForVerb(verb, weight);
76         }
77
78         /**
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).
84          */
85         SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb,
86                                      int numVbs,
87                                      SkScalar** weights = NULL) {
88             return fPathRef->growForRepeatedVerb(verb, numVbs, weights);
89         }
90
91         /**
92          * Resets the path ref to a new verb and point count. The new verbs and points are
93          * uninitialized.
94          */
95         void resetToSize(int newVerbCnt, int newPointCnt, int newConicCount) {
96             fPathRef->resetToSize(newVerbCnt, newPointCnt, newConicCount);
97         }
98
99         /**
100          * Gets the path ref that is wrapped in the Editor.
101          */
102         SkPathRef* pathRef() { return fPathRef; }
103
104         void setIsOval(bool isOval) { fPathRef->setIsOval(isOval); }
105
106         void setBounds(const SkRect& rect) { fPathRef->setBounds(rect); }
107
108     private:
109         SkPathRef* fPathRef;
110     };
111
112 public:
113     /**
114      * Gets a path ref with no verbs or points.
115      */
116     static SkPathRef* CreateEmpty();
117
118     /**
119      *  Returns true if all of the points in this path are finite, meaning there
120      *  are no infinities and no NaNs.
121      */
122     bool isFinite() const {
123         if (fBoundsIsDirty) {
124             this->computeBounds();
125         }
126         return SkToBool(fIsFinite);
127     }
128
129     /**
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).
133      */
134     uint32_t getSegmentMasks() const { return fSegmentMask; }
135
136     /** Returns true if the path is an oval.
137      *
138      * @param rect      returns the bounding rect of this oval. It's a circle
139      *                  if the height and width are the same.
140      *
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.
145      */
146     bool isOval(SkRect* rect) const {
147         if (fIsOval && rect) {
148             *rect = getBounds();
149         }
150
151         return SkToBool(fIsOval);
152     }
153
154     bool hasComputedBounds() const {
155         return !fBoundsIsDirty;
156     }
157
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.
162     */
163     const SkRect& getBounds() const {
164         if (fBoundsIsDirty) {
165             this->computeBounds();
166         }
167         return fBounds;
168     }
169
170     /**
171      * Transforms a path ref by a matrix, allocating a new one only if necessary.
172      */
173     static void CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
174                                       const SkPathRef& src,
175                                       const SkMatrix& matrix);
176
177     static SkPathRef* CreateFromBuffer(SkRBuffer* buffer);
178
179     /**
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
182      * only if necessary.
183      */
184     static void Rewind(SkAutoTUnref<SkPathRef>* pathRef);
185
186     virtual ~SkPathRef() {
187         SkDEBUGCODE(this->validate();)
188         sk_free(fPoints);
189
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;)
197     }
198
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(); }
202
203     /**
204      * Returns a pointer one beyond the first logical verb (last verb in memory order).
205      */
206     const uint8_t* verbs() const { SkDEBUGCODE(this->validate();) return fVerbs; }
207
208     /**
209      * Returns a const pointer to the first verb in memory (which is the last logical verb).
210      */
211     const uint8_t* verbsMemBegin() const { return this->verbs() - fVerbCnt; }
212
213     /**
214      * Returns a const pointer to the first point.
215      */
216     const SkPoint* points() const { SkDEBUGCODE(this->validate();) return fPoints; }
217
218     /**
219      * Shortcut for this->points() + this->countPoints()
220      */
221     const SkPoint* pointsEnd() const { return this->points() + this->countPoints(); }
222
223     const SkScalar* conicWeights() const { SkDEBUGCODE(this->validate();) return fConicWeights.begin(); }
224     const SkScalar* conicWeightsEnd() const { SkDEBUGCODE(this->validate();) return fConicWeights.end(); }
225
226     /**
227      * Convenience methods for getting to a verb or point by index.
228      */
229     uint8_t atVerb(int index) const {
230         SkASSERT((unsigned) index < (unsigned) fVerbCnt);
231         return this->verbs()[~index];
232     }
233     const SkPoint& atPoint(int index) const {
234         SkASSERT((unsigned) index < (unsigned) fPointCnt);
235         return this->points()[index];
236     }
237
238     bool operator== (const SkPathRef& ref) const;
239
240     /**
241      * Writes the path points and verbs to a buffer.
242      */
243     void writeToBuffer(SkWBuffer* buffer) const;
244
245     /**
246      * Gets the number of bytes that would be written in writeBuffer()
247      */
248     uint32_t writeSize() const;
249
250     /**
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.
254      */
255     uint32_t genID() const;
256
257 private:
258     enum SerializationOffsets {
259         kIsFinite_SerializationShift = 25,  // requires 1 bit
260         kIsOval_SerializationShift = 24,    // requires 1 bit
261         kSegmentMask_SerializationShift = 0 // requires 4 bits
262     };
263
264     SkPathRef() {
265         fBoundsIsDirty = true;    // this also invalidates fIsFinite
266         fPointCnt = 0;
267         fVerbCnt = 0;
268         fVerbs = NULL;
269         fPoints = NULL;
270         fFreeSpace = 0;
271         fGenerationID = kEmptyGenID;
272         fSegmentMask = 0;
273         fIsOval = false;
274         SkDEBUGCODE(fEditorsAttached = 0;)
275         SkDEBUGCODE(this->validate();)
276     }
277
278     void copy(const SkPathRef& ref, int additionalReserveVerbs, int additionalReservePoints);
279
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)
284             bounds->setEmpty();
285             return count ? ref.points()->isFinite() : true;
286         } else {
287             return bounds->setBoundsCheck(ref.points(), count);
288         }
289     }
290
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);
297
298         fIsFinite = ComputePtBounds(fBounds.get(), *this);
299         fBoundsIsDirty = false;
300     }
301
302     void setBounds(const SkRect& rect) {
303         SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
304         fBounds = rect;
305         fBoundsIsDirty = false;
306         fIsFinite = fBounds->isFinite();
307     }
308
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();)
315     }
316
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
323         fGenerationID = 0;
324
325         fSegmentMask = 0;
326         fIsOval = false;
327
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;
331
332         ptrdiff_t sizeDelta = this->currSize() - minSize;
333
334         if (sizeDelta < 0 || static_cast<size_t>(sizeDelta) >= 3 * minSize) {
335             sk_free(fPoints);
336             fPoints = NULL;
337             fVerbs = NULL;
338             fFreeSpace = 0;
339             fVerbCnt = 0;
340             fPointCnt = 0;
341             this->makeSpace(minSize);
342             fVerbCnt = verbCount;
343             fPointCnt = pointCount;
344             fFreeSpace -= newSize;
345         } else {
346             fPointCnt = pointCount;
347             fVerbCnt = verbCount;
348             fFreeSpace = this->currSize() - minSize;
349         }
350         fConicWeights.setCount(conicCount);
351         SkDEBUGCODE(this->validate();)
352     }
353
354     /**
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.
359      */
360     SkPoint* growForRepeatedVerb(int /*SkPath::Verb*/ verb, int numVbs, SkScalar** weights);
361
362     /**
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
365      * uninitialized.
366      */
367     SkPoint* growForVerb(int /*SkPath::Verb*/ verb, SkScalar weight);
368
369     /**
370      * Ensures that the free space available in the path ref is >= size. The verb and point counts
371      * are not changed.
372      */
373     void makeSpace(size_t size) {
374         SkDEBUGCODE(this->validate();)
375         ptrdiff_t growSize = size - fFreeSpace;
376         if (growSize <= 0) {
377             return;
378         }
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) {
384             growSize = oldSize;
385         }
386         if (growSize < kMinSize) {
387             growSize = kMinSize;
388         }
389         size_t newSize = oldSize + growSize;
390         // Note that realloc could memcpy more than we need. It seems to be a win anyway. TODO:
391         // encapsulate this.
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();)
402     }
403
404     /**
405      * Private, non-const-ptr version of the public function verbsMemBegin().
406      */
407     uint8_t* verbsMemWritable() {
408         SkDEBUGCODE(this->validate();)
409         return fVerbs - fVerbCnt;
410     }
411
412     /**
413      * Gets the total amount of space allocated for verbs, points, and reserve.
414      */
415     size_t currSize() const {
416         return reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints);
417     }
418
419     SkDEBUGCODE(void validate() const;)
420
421     /**
422      * Called the first time someone calls CreateEmpty to actually create the singleton.
423      */
424     static SkPathRef* CreateEmptyImpl();
425
426     void setIsOval(bool isOval) { fIsOval = isOval; }
427
428     SkPoint* getPoints() {
429         SkDEBUGCODE(this->validate();)
430         fIsOval = false;
431         return fPoints;
432     }
433
434     enum {
435         kMinSize = 256,
436     };
437
438     mutable SkTRacyReffable<SkRect> fBounds;
439     mutable SkTRacy<uint8_t>        fBoundsIsDirty;
440     mutable SkTRacy<SkBool8>        fIsFinite;    // only meaningful if bounds are valid
441
442     SkBool8  fIsOval;
443     uint8_t  fSegmentMask;
444
445     SkPoint*            fPoints; // points to begining of the allocation
446     uint8_t*            fVerbs; // points just past the end of the allocation (verbs grow backwards)
447     int                 fVerbCnt;
448     int                 fPointCnt;
449     size_t              fFreeSpace; // redundant but saves computation
450     SkTDArray<SkScalar> fConicWeights;
451
452     enum {
453         kEmptyGenID = 1, // GenID reserved for path ref with zero points and zero verbs.
454     };
455     mutable uint32_t    fGenerationID;
456     SkDEBUGCODE(int32_t fEditorsAttached;) // assert that only one editor in use at any time.
457
458     friend class PathRefTest_Private;
459     typedef SkRefCnt INHERITED;
460 };
461
462 #endif