Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkPathRef.cpp
1 /*
2  * Copyright 2013 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 #include "SkBuffer.h"
9 #include "SkLazyPtr.h"
10 #include "SkPath.h"
11 #include "SkPathRef.h"
12
13 //////////////////////////////////////////////////////////////////////////////
14 SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
15                           int incReserveVerbs,
16                           int incReservePoints)
17 {
18     if ((*pathRef)->unique()) {
19         (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
20     } else {
21         SkPathRef* copy = SkNEW(SkPathRef);
22         copy->copy(**pathRef, incReserveVerbs, incReservePoints);
23         pathRef->reset(copy);
24     }
25     fPathRef = *pathRef;
26     fPathRef->fGenerationID = 0;
27     SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
28 }
29
30 //////////////////////////////////////////////////////////////////////////////
31
32 SkPathRef* SkPathRef::CreateEmptyImpl() {
33     return SkNEW(SkPathRef);
34 }
35
36 SkPathRef* SkPathRef::CreateEmpty() {
37     SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, CreateEmptyImpl);
38     return SkRef(empty.get());
39 }
40
41 void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
42                                       const SkPathRef& src,
43                                       const SkMatrix& matrix) {
44     SkDEBUGCODE(src.validate();)
45     if (matrix.isIdentity()) {
46         if (*dst != &src) {
47             src.ref();
48             dst->reset(const_cast<SkPathRef*>(&src));
49             SkDEBUGCODE((*dst)->validate();)
50         }
51         return;
52     }
53
54     if (!(*dst)->unique()) {
55         dst->reset(SkNEW(SkPathRef));
56     }
57
58     if (*dst != &src) {
59         (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
60         memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
61         (*dst)->fConicWeights = src.fConicWeights;
62     }
63
64     SkASSERT((*dst)->countPoints() == src.countPoints());
65     SkASSERT((*dst)->countVerbs() == src.countVerbs());
66     SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
67
68     // Need to check this here in case (&src == dst)
69     bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
70
71     matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
72
73     /*
74         *  Here we optimize the bounds computation, by noting if the bounds are
75         *  already known, and if so, we just transform those as well and mark
76         *  them as "known", rather than force the transformed path to have to
77         *  recompute them.
78         *
79         *  Special gotchas if the path is effectively empty (<= 1 point) or
80         *  if it is non-finite. In those cases bounds need to stay empty,
81         *  regardless of the matrix.
82         */
83     if (canXformBounds) {
84         (*dst)->fBoundsIsDirty = false;
85         if (src.fIsFinite) {
86             matrix.mapRect((*dst)->fBounds.get(), src.fBounds);
87             if (!((*dst)->fIsFinite = (*dst)->fBounds->isFinite())) {
88                 (*dst)->fBounds->setEmpty();
89             }
90         } else {
91             (*dst)->fIsFinite = false;
92             (*dst)->fBounds->setEmpty();
93         }
94     } else {
95         (*dst)->fBoundsIsDirty = true;
96     }
97
98     (*dst)->fSegmentMask = src.fSegmentMask;
99
100     // It's an oval only if it stays a rect.
101     (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
102
103     SkDEBUGCODE((*dst)->validate();)
104 }
105
106 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
107     SkPathRef* ref = SkNEW(SkPathRef);
108     bool isOval;
109     uint8_t segmentMask;
110
111     int32_t packed;
112     if (!buffer->readS32(&packed)) {
113         SkDELETE(ref);
114         return NULL;
115     }
116
117     ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
118     segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
119     isOval  = (packed >> kIsOval_SerializationShift) & 1;
120
121     int32_t verbCount, pointCount, conicCount;
122     if (!buffer->readU32(&(ref->fGenerationID)) ||
123         !buffer->readS32(&verbCount) ||
124         !buffer->readS32(&pointCount) ||
125         !buffer->readS32(&conicCount)) {
126         SkDELETE(ref);
127         return NULL;
128     }
129
130     ref->resetToSize(verbCount, pointCount, conicCount);
131     SkASSERT(verbCount == ref->countVerbs());
132     SkASSERT(pointCount == ref->countPoints());
133     SkASSERT(conicCount == ref->fConicWeights.count());
134
135     if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
136         !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
137         !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
138         !buffer->read(&ref->fBounds, sizeof(SkRect))) {
139         SkDELETE(ref);
140         return NULL;
141     }
142     ref->fBoundsIsDirty = false;
143
144     // resetToSize clears fSegmentMask and fIsOval
145     ref->fSegmentMask = segmentMask;
146     ref->fIsOval = isOval;
147     return ref;
148 }
149
150 void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
151     if ((*pathRef)->unique()) {
152         SkDEBUGCODE((*pathRef)->validate();)
153         (*pathRef)->fBoundsIsDirty = true;  // this also invalidates fIsFinite
154         (*pathRef)->fVerbCnt = 0;
155         (*pathRef)->fPointCnt = 0;
156         (*pathRef)->fFreeSpace = (*pathRef)->currSize();
157         (*pathRef)->fGenerationID = 0;
158         (*pathRef)->fConicWeights.rewind();
159         (*pathRef)->fSegmentMask = 0;
160         (*pathRef)->fIsOval = false;
161         SkDEBUGCODE((*pathRef)->validate();)
162     } else {
163         int oldVCnt = (*pathRef)->countVerbs();
164         int oldPCnt = (*pathRef)->countPoints();
165         pathRef->reset(SkNEW(SkPathRef));
166         (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
167     }
168 }
169
170 bool SkPathRef::operator== (const SkPathRef& ref) const {
171     SkDEBUGCODE(this->validate();)
172     SkDEBUGCODE(ref.validate();)
173
174     // We explicitly check fSegmentMask as a quick-reject. We could skip it,
175     // since it is only a cache of info in the fVerbs, but its a fast way to
176     // notice a difference
177     if (fSegmentMask != ref.fSegmentMask) {
178         return false;
179     }
180
181     bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
182 #ifdef SK_RELEASE
183     if (genIDMatch) {
184         return true;
185     }
186 #endif
187     if (fPointCnt != ref.fPointCnt ||
188         fVerbCnt != ref.fVerbCnt) {
189         SkASSERT(!genIDMatch);
190         return false;
191     }
192     if (0 != memcmp(this->verbsMemBegin(),
193                     ref.verbsMemBegin(),
194                     ref.fVerbCnt * sizeof(uint8_t))) {
195         SkASSERT(!genIDMatch);
196         return false;
197     }
198     if (0 != memcmp(this->points(),
199                     ref.points(),
200                     ref.fPointCnt * sizeof(SkPoint))) {
201         SkASSERT(!genIDMatch);
202         return false;
203     }
204     if (fConicWeights != ref.fConicWeights) {
205         SkASSERT(!genIDMatch);
206         return false;
207     }
208     // We've done the work to determine that these are equal. If either has a zero genID, copy
209     // the other's. If both are 0 then genID() will compute the next ID.
210     if (0 == fGenerationID) {
211         fGenerationID = ref.genID();
212     } else if (0 == ref.fGenerationID) {
213         ref.fGenerationID = this->genID();
214     }
215     return true;
216 }
217
218 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
219     SkDEBUGCODE(this->validate();)
220     SkDEBUGCODE(size_t beforePos = buffer->pos();)
221
222     // Call getBounds() to ensure (as a side-effect) that fBounds
223     // and fIsFinite are computed.
224     const SkRect& bounds = this->getBounds();
225
226     int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
227                      ((fIsOval & 1) << kIsOval_SerializationShift) |
228                      (fSegmentMask << kSegmentMask_SerializationShift);
229     buffer->write32(packed);
230
231     // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
232     // SkWBuffer. Until this is fixed we write 0.
233     buffer->write32(0);
234     buffer->write32(fVerbCnt);
235     buffer->write32(fPointCnt);
236     buffer->write32(fConicWeights.count());
237     buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
238     buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
239     buffer->write(fConicWeights.begin(), fConicWeights.bytes());
240     buffer->write(&bounds, sizeof(bounds));
241
242     SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
243 }
244
245 uint32_t SkPathRef::writeSize() const {
246     return uint32_t(5 * sizeof(uint32_t) +
247                     fVerbCnt * sizeof(uint8_t) +
248                     fPointCnt * sizeof(SkPoint) +
249                     fConicWeights.bytes() +
250                     sizeof(SkRect));
251 }
252
253 void SkPathRef::copy(const SkPathRef& ref,
254                      int additionalReserveVerbs,
255                      int additionalReservePoints) {
256     SkDEBUGCODE(this->validate();)
257     this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
258                         additionalReserveVerbs, additionalReservePoints);
259     memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
260     memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
261     fConicWeights = ref.fConicWeights;
262     // We could call genID() here to force a real ID (instead of 0). However, if we're making
263     // a copy then presumably we intend to make a modification immediately afterwards.
264     fGenerationID = ref.fGenerationID;
265     fBoundsIsDirty = ref.fBoundsIsDirty;
266     if (!fBoundsIsDirty) {
267         fBounds = ref.fBounds;
268         fIsFinite = ref.fIsFinite;
269     }
270     fSegmentMask = ref.fSegmentMask;
271     fIsOval = ref.fIsOval;
272     SkDEBUGCODE(this->validate();)
273 }
274
275 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
276                                         int numVbs,
277                                         SkScalar** weights) {
278     // This value is just made-up for now. When count is 4, calling memset was much
279     // slower than just writing the loop. This seems odd, and hopefully in the
280     // future this will appear to have been a fluke...
281     static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
282
283     SkDEBUGCODE(this->validate();)
284     int pCnt;
285     bool dirtyAfterEdit = true;
286     switch (verb) {
287         case SkPath::kMove_Verb:
288             pCnt = numVbs;
289             dirtyAfterEdit = false;
290             break;
291         case SkPath::kLine_Verb:
292             fSegmentMask |= SkPath::kLine_SegmentMask;
293             pCnt = numVbs;
294             break;
295         case SkPath::kQuad_Verb:
296             fSegmentMask |= SkPath::kQuad_SegmentMask;
297             pCnt = 2 * numVbs;
298             break;
299         case SkPath::kConic_Verb:
300             fSegmentMask |= SkPath::kConic_SegmentMask;
301             pCnt = 2 * numVbs;
302             break;
303         case SkPath::kCubic_Verb:
304             fSegmentMask |= SkPath::kCubic_SegmentMask;
305             pCnt = 3 * numVbs;
306             break;
307         case SkPath::kClose_Verb:
308             SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
309             pCnt = 0;
310             dirtyAfterEdit = false;
311             break;
312         case SkPath::kDone_Verb:
313             SkDEBUGFAIL("growForRepeatedVerb called for kDone");
314             // fall through
315         default:
316             SkDEBUGFAIL("default should not be reached");
317             pCnt = 0;
318             dirtyAfterEdit = false;
319     }
320
321     size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
322     this->makeSpace(space);
323
324     SkPoint* ret = fPoints + fPointCnt;
325     uint8_t* vb = fVerbs - fVerbCnt;
326
327     // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
328     // be 0, the compiler will remove the test/branch entirely.
329     if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
330         memset(vb - numVbs, verb, numVbs);
331     } else {
332         for (int i = 0; i < numVbs; ++i) {
333             vb[~i] = verb;
334         }
335     }
336
337     fVerbCnt += numVbs;
338     fPointCnt += pCnt;
339     fFreeSpace -= space;
340     fBoundsIsDirty = true;  // this also invalidates fIsFinite
341     if (dirtyAfterEdit) {
342         fIsOval = false;
343     }
344
345     if (SkPath::kConic_Verb == verb) {
346         SkASSERT(NULL != weights);
347         *weights = fConicWeights.append(numVbs);
348     }
349
350     SkDEBUGCODE(this->validate();)
351     return ret;
352 }
353
354 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
355     SkDEBUGCODE(this->validate();)
356     int pCnt;
357     bool dirtyAfterEdit = true;
358     switch (verb) {
359         case SkPath::kMove_Verb:
360             pCnt = 1;
361             dirtyAfterEdit = false;
362             break;
363         case SkPath::kLine_Verb:
364             fSegmentMask |= SkPath::kLine_SegmentMask;
365             pCnt = 1;
366             break;
367         case SkPath::kQuad_Verb:
368             fSegmentMask |= SkPath::kQuad_SegmentMask;
369             pCnt = 2;
370             break;
371         case SkPath::kConic_Verb:
372             fSegmentMask |= SkPath::kConic_SegmentMask;
373             pCnt = 2;
374             break;
375         case SkPath::kCubic_Verb:
376             fSegmentMask |= SkPath::kCubic_SegmentMask;
377             pCnt = 3;
378             break;
379         case SkPath::kClose_Verb:
380             pCnt = 0;
381             dirtyAfterEdit = false;
382             break;
383         case SkPath::kDone_Verb:
384             SkDEBUGFAIL("growForVerb called for kDone");
385             // fall through
386         default:
387             SkDEBUGFAIL("default is not reached");
388             dirtyAfterEdit = false;
389             pCnt = 0;
390     }
391     size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
392     this->makeSpace(space);
393     this->fVerbs[~fVerbCnt] = verb;
394     SkPoint* ret = fPoints + fPointCnt;
395     fVerbCnt += 1;
396     fPointCnt += pCnt;
397     fFreeSpace -= space;
398     fBoundsIsDirty = true;  // this also invalidates fIsFinite
399     if (dirtyAfterEdit) {
400         fIsOval = false;
401     }
402
403     if (SkPath::kConic_Verb == verb) {
404         *fConicWeights.append() = weight;
405     }
406
407     SkDEBUGCODE(this->validate();)
408     return ret;
409 }
410
411 uint32_t SkPathRef::genID() const {
412     SkASSERT(!fEditorsAttached);
413     static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
414     if (!fGenerationID) {
415         if (0 == fPointCnt && 0 == fVerbCnt) {
416             fGenerationID = kEmptyGenID;
417         } else {
418             static int32_t  gPathRefGenerationID;
419             // do a loop in case our global wraps around, as we never want to return a 0 or the
420             // empty ID
421             do {
422                 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
423             } while (fGenerationID <= kEmptyGenID);
424         }
425     }
426     return fGenerationID;
427 }
428
429 #ifdef SK_DEBUG
430 void SkPathRef::validate() const {
431     this->INHERITED::validate();
432     SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
433     SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
434     SkASSERT((NULL == fPoints) == (NULL == fVerbs));
435     SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
436     SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
437     SkASSERT(!(NULL == fPoints && fPointCnt));
438     SkASSERT(!(NULL == fVerbs && fVerbCnt));
439     SkASSERT(this->currSize() ==
440                 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
441
442     if (!fBoundsIsDirty && !fBounds->isEmpty()) {
443         bool isFinite = true;
444         for (int i = 0; i < fPointCnt; ++i) {
445             SkASSERT(!fPoints[i].isFinite() || (
446                      fBounds->fLeft - fPoints[i].fX   < SK_ScalarNearlyZero &&
447                      fPoints[i].fX - fBounds->fRight  < SK_ScalarNearlyZero &&
448                      fBounds->fTop  - fPoints[i].fY   < SK_ScalarNearlyZero &&
449                      fPoints[i].fY - fBounds->fBottom < SK_ScalarNearlyZero));
450             if (!fPoints[i].isFinite()) {
451                 isFinite = false;
452             }
453         }
454         SkASSERT(SkToBool(fIsFinite) == isFinite);
455     }
456
457 #ifdef SK_DEBUG_PATH
458     uint32_t mask = 0;
459     for (int i = 0; i < fVerbCnt; ++i) {
460         switch (fVerbs[~i]) {
461             case SkPath::kMove_Verb:
462                 break;
463             case SkPath::kLine_Verb:
464                 mask |= SkPath::kLine_SegmentMask;
465                 break;
466             case SkPath::kQuad_Verb:
467                 mask |= SkPath::kQuad_SegmentMask;
468                 break;
469             case SkPath::kConic_Verb:
470                 mask |= SkPath::kConic_SegmentMask;
471                 break;
472             case SkPath::kCubic_Verb:
473                 mask |= SkPath::kCubic_SegmentMask;
474                 break;
475             case SkPath::kClose_Verb:
476                 break;
477             case SkPath::kDone_Verb:
478                 SkDEBUGFAIL("Done verb shouldn't be recorded.");
479                 break;
480             default:
481                 SkDEBUGFAIL("Unknown Verb");
482                 break;
483         }
484     }
485     SkASSERT(mask == fSegmentMask);
486 #endif // SK_DEBUG_PATH
487 }
488 #endif