• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkOnce.h"
10 #include "SkPath.h"
11 #include "SkPathRef.h"
12 #include <limits>
13 
14 //////////////////////////////////////////////////////////////////////////////
Editor(sk_sp<SkPathRef> * pathRef,int incReserveVerbs,int incReservePoints)15 SkPathRef::Editor::Editor(sk_sp<SkPathRef>* pathRef,
16                           int incReserveVerbs,
17                           int incReservePoints)
18 {
19     if ((*pathRef)->unique()) {
20         (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
21     } else {
22         SkPathRef* copy = new SkPathRef;
23         copy->copy(**pathRef, incReserveVerbs, incReservePoints);
24         pathRef->reset(copy);
25     }
26     fPathRef = pathRef->get();
27     fPathRef->callGenIDChangeListeners();
28     fPathRef->fGenerationID = 0;
29     SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
30 }
31 
32 //////////////////////////////////////////////////////////////////////////////
33 
~SkPathRef()34 SkPathRef::~SkPathRef() {
35     this->callGenIDChangeListeners();
36     SkDEBUGCODE(this->validate();)
37     sk_free(fPoints);
38 
39     SkDEBUGCODE(fPoints = nullptr;)
40     SkDEBUGCODE(fVerbs = nullptr;)
41     SkDEBUGCODE(fVerbCnt = 0x9999999;)
42     SkDEBUGCODE(fPointCnt = 0xAAAAAAA;)
43     SkDEBUGCODE(fPointCnt = 0xBBBBBBB;)
44     SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;)
45     SkDEBUGCODE(fEditorsAttached = 0x7777777;)
46 }
47 
48 static SkPathRef* gEmpty = nullptr;
49 
CreateEmpty()50 SkPathRef* SkPathRef::CreateEmpty() {
51     static SkOnce once;
52     once([]{
53         gEmpty = new SkPathRef;
54         gEmpty->computeBounds();   // Avoids races later to be the first to do this.
55     });
56     return SkRef(gEmpty);
57 }
58 
transform_dir_and_start(const SkMatrix & matrix,bool isRRect,bool * isCCW,unsigned * start)59 static void transform_dir_and_start(const SkMatrix& matrix, bool isRRect, bool* isCCW,
60                                     unsigned* start) {
61     int inStart = *start;
62     int rm = 0;
63     if (isRRect) {
64         // Degenerate rrect indices to oval indices and remember the remainder.
65         // Ovals have one index per side whereas rrects have two.
66         rm = inStart & 0b1;
67         inStart /= 2;
68     }
69     // Is the antidiagonal non-zero (otherwise the diagonal is zero)
70     int antiDiag;
71     // Is the non-zero value in the top row (either kMScaleX or kMSkewX) negative
72     int topNeg;
73     // Are the two non-zero diagonal or antidiagonal values the same sign.
74     int sameSign;
75     if (matrix.get(SkMatrix::kMScaleX) != 0) {
76         antiDiag = 0b00;
77         if (matrix.get(SkMatrix::kMScaleX) > 0) {
78             topNeg = 0b00;
79             sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b01 : 0b00;
80         } else {
81             topNeg = 0b10;
82             sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b00 : 0b01;
83         }
84     } else {
85         antiDiag = 0b01;
86         if (matrix.get(SkMatrix::kMSkewX) > 0) {
87             topNeg = 0b00;
88             sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b01 : 0b00;
89         } else {
90             topNeg = 0b10;
91             sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b00 : 0b01;
92         }
93     }
94     if (sameSign != antiDiag) {
95         // This is a rotation (and maybe scale). The direction is unchanged.
96         // Trust me on the start computation (or draw yourself some pictures)
97         *start = (inStart + 4 - (topNeg | antiDiag)) % 4;
98         SkASSERT(*start < 4);
99         if (isRRect) {
100             *start = 2 * *start + rm;
101         }
102     } else {
103         // This is a mirror (and maybe scale). The direction is reversed.
104         *isCCW = !*isCCW;
105         // Trust me on the start computation (or draw yourself some pictures)
106         *start = (6 + (topNeg | antiDiag) - inStart) % 4;
107         SkASSERT(*start < 4);
108         if (isRRect) {
109             *start = 2 * *start + (rm ? 0 : 1);
110         }
111     }
112 }
113 
CreateTransformedCopy(sk_sp<SkPathRef> * dst,const SkPathRef & src,const SkMatrix & matrix)114 void SkPathRef::CreateTransformedCopy(sk_sp<SkPathRef>* dst,
115                                       const SkPathRef& src,
116                                       const SkMatrix& matrix) {
117     SkDEBUGCODE(src.validate();)
118     if (matrix.isIdentity()) {
119         if (dst->get() != &src) {
120             src.ref();
121             dst->reset(const_cast<SkPathRef*>(&src));
122             SkDEBUGCODE((*dst)->validate();)
123         }
124         return;
125     }
126 
127     if (!(*dst)->unique()) {
128         dst->reset(new SkPathRef);
129     }
130 
131     if (dst->get() != &src) {
132         (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
133         sk_careful_memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(),
134                            src.fVerbCnt * sizeof(uint8_t));
135         (*dst)->fConicWeights = src.fConicWeights;
136     }
137 
138     SkASSERT((*dst)->countPoints() == src.countPoints());
139     SkASSERT((*dst)->countVerbs() == src.countVerbs());
140     SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
141 
142     // Need to check this here in case (&src == dst)
143     bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
144 
145     matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
146 
147     /*
148      *  Here we optimize the bounds computation, by noting if the bounds are
149      *  already known, and if so, we just transform those as well and mark
150      *  them as "known", rather than force the transformed path to have to
151      *  recompute them.
152      *
153      *  Special gotchas if the path is effectively empty (<= 1 point) or
154      *  if it is non-finite. In those cases bounds need to stay empty,
155      *  regardless of the matrix.
156      */
157     if (canXformBounds) {
158         (*dst)->fBoundsIsDirty = false;
159         if (src.fIsFinite) {
160             matrix.mapRect(&(*dst)->fBounds, src.fBounds);
161             if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
162                 (*dst)->fBounds.setEmpty();
163             }
164         } else {
165             (*dst)->fIsFinite = false;
166             (*dst)->fBounds.setEmpty();
167         }
168     } else {
169         (*dst)->fBoundsIsDirty = true;
170     }
171 
172     (*dst)->fSegmentMask = src.fSegmentMask;
173 
174     // It's an oval only if it stays a rect.
175     bool rectStaysRect = matrix.rectStaysRect();
176     (*dst)->fIsOval = src.fIsOval && rectStaysRect;
177     (*dst)->fIsRRect = src.fIsRRect && rectStaysRect;
178     if ((*dst)->fIsOval || (*dst)->fIsRRect) {
179         unsigned start = src.fRRectOrOvalStartIdx;
180         bool isCCW = SkToBool(src.fRRectOrOvalIsCCW);
181         transform_dir_and_start(matrix, (*dst)->fIsRRect, &isCCW, &start);
182         (*dst)->fRRectOrOvalIsCCW = isCCW;
183         (*dst)->fRRectOrOvalStartIdx = start;
184     }
185 
186     SkDEBUGCODE((*dst)->validate();)
187 }
188 
189 // Given the verb array, deduce the required number of pts and conics,
190 // or if an invalid verb is encountered, return false.
deduce_pts_conics(const uint8_t verbs[],int vCount,int * ptCountPtr,int * conicCountPtr)191 static bool deduce_pts_conics(const uint8_t verbs[], int vCount, int* ptCountPtr,
192                               int* conicCountPtr) {
193     // When there is at least one verb, the first is required to be kMove_Verb.
194     if (0 < vCount && verbs[vCount-1] != SkPath::kMove_Verb) {
195         return false;
196     }
197 
198     int ptCount = 0;
199     int conicCount = 0;
200     for (int i = 0; i < vCount; ++i) {
201         switch (verbs[i]) {
202             case SkPath::kMove_Verb:
203             case SkPath::kLine_Verb:
204                 ptCount += 1;
205                 break;
206             case SkPath::kConic_Verb:
207                 conicCount += 1;
208                 // fall-through
209             case SkPath::kQuad_Verb:
210                 ptCount += 2;
211                 break;
212             case SkPath::kCubic_Verb:
213                 ptCount += 3;
214                 break;
215             case SkPath::kClose_Verb:
216                 break;
217             default:
218                 return false;
219         }
220     }
221     *ptCountPtr = ptCount;
222     *conicCountPtr = conicCount;
223     return true;
224 }
225 
CreateFromBuffer(SkRBuffer * buffer)226 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
227     std::unique_ptr<SkPathRef> ref(new SkPathRef);
228 
229     int32_t packed;
230     if (!buffer->readS32(&packed)) {
231         return nullptr;
232     }
233 
234     ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
235     uint8_t segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
236     bool isOval  = (packed >> kIsOval_SerializationShift) & 1;
237     bool isRRect  = (packed >> kIsRRect_SerializationShift) & 1;
238     if (isOval && isRRect) {
239         // Fuzzing generates data with both oval and rrect flags set; abort early in this case/
240         return nullptr;
241     }
242     bool rrectOrOvalIsCCW = (packed >> kRRectOrOvalIsCCW_SerializationShift) & 1;
243     unsigned rrectOrOvalStartIdx = (packed >> kRRectOrOvalStartIdx_SerializationShift) & 0x7;
244 
245     int32_t verbCount, pointCount, conicCount;
246     ptrdiff_t maxPtrDiff = std::numeric_limits<ptrdiff_t>::max();
247     if (!buffer->readU32(&(ref->fGenerationID)) ||
248         !buffer->readS32(&verbCount) ||
249         verbCount < 0 ||
250         static_cast<uint32_t>(verbCount) > maxPtrDiff/sizeof(uint8_t) ||
251         !buffer->readS32(&pointCount) ||
252         pointCount < 0 ||
253         static_cast<uint32_t>(pointCount) > maxPtrDiff/sizeof(SkPoint) ||
254         sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount >
255             static_cast<size_t>(maxPtrDiff) ||
256         !buffer->readS32(&conicCount) ||
257         conicCount < 0) {
258         return nullptr;
259     }
260 
261     ref->resetToSize(verbCount, pointCount, conicCount);
262     SkASSERT(verbCount == ref->countVerbs());
263     SkASSERT(pointCount == ref->countPoints());
264     SkASSERT(conicCount == ref->fConicWeights.count());
265 
266     if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
267         !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
268         !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
269         !buffer->read(&ref->fBounds, sizeof(SkRect))) {
270         return nullptr;
271     }
272 
273     // Check that the verbs are valid, and imply the correct number of pts and conics
274     {
275         int pCount, cCount;
276         if (!deduce_pts_conics(ref->verbsMemBegin(), ref->countVerbs(), &pCount, &cCount) ||
277             pCount != ref->countPoints() || cCount != ref->fConicWeights.count()) {
278             return nullptr;
279         }
280         // Check that the bounds match the serialized bounds.
281         SkRect bounds;
282         if (ComputePtBounds(&bounds, *ref) != SkToBool(ref->fIsFinite) || bounds != ref->fBounds) {
283             return nullptr;
284         }
285     }
286 
287     ref->fBoundsIsDirty = false;
288 
289     // resetToSize clears fSegmentMask and fIsOval
290     ref->fSegmentMask = segmentMask;
291     ref->fIsOval = isOval;
292     ref->fIsRRect = isRRect;
293     ref->fRRectOrOvalIsCCW = rrectOrOvalIsCCW;
294     ref->fRRectOrOvalStartIdx = rrectOrOvalStartIdx;
295     return ref.release();
296 }
297 
Rewind(sk_sp<SkPathRef> * pathRef)298 void SkPathRef::Rewind(sk_sp<SkPathRef>* pathRef) {
299     if ((*pathRef)->unique()) {
300         SkDEBUGCODE((*pathRef)->validate();)
301         (*pathRef)->callGenIDChangeListeners();
302         (*pathRef)->fBoundsIsDirty = true;  // this also invalidates fIsFinite
303         (*pathRef)->fVerbCnt = 0;
304         (*pathRef)->fPointCnt = 0;
305         (*pathRef)->fFreeSpace = (*pathRef)->currSize();
306         (*pathRef)->fGenerationID = 0;
307         (*pathRef)->fConicWeights.rewind();
308         (*pathRef)->fSegmentMask = 0;
309         (*pathRef)->fIsOval = false;
310         (*pathRef)->fIsRRect = false;
311         SkDEBUGCODE((*pathRef)->validate();)
312     } else {
313         int oldVCnt = (*pathRef)->countVerbs();
314         int oldPCnt = (*pathRef)->countPoints();
315         pathRef->reset(new SkPathRef);
316         (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
317     }
318 }
319 
operator ==(const SkPathRef & ref) const320 bool SkPathRef::operator== (const SkPathRef& ref) const {
321     SkDEBUGCODE(this->validate();)
322     SkDEBUGCODE(ref.validate();)
323 
324     // We explicitly check fSegmentMask as a quick-reject. We could skip it,
325     // since it is only a cache of info in the fVerbs, but its a fast way to
326     // notice a difference
327     if (fSegmentMask != ref.fSegmentMask) {
328         return false;
329     }
330 
331     bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
332 #ifdef SK_RELEASE
333     if (genIDMatch) {
334         return true;
335     }
336 #endif
337     if (fPointCnt != ref.fPointCnt ||
338         fVerbCnt != ref.fVerbCnt) {
339         SkASSERT(!genIDMatch);
340         return false;
341     }
342     if (0 == ref.fVerbCnt) {
343         SkASSERT(0 == ref.fPointCnt);
344         return true;
345     }
346     SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin());
347     if (0 != memcmp(this->verbsMemBegin(),
348                     ref.verbsMemBegin(),
349                     ref.fVerbCnt * sizeof(uint8_t))) {
350         SkASSERT(!genIDMatch);
351         return false;
352     }
353     SkASSERT(this->points() && ref.points());
354     if (0 != memcmp(this->points(),
355                     ref.points(),
356                     ref.fPointCnt * sizeof(SkPoint))) {
357         SkASSERT(!genIDMatch);
358         return false;
359     }
360     if (fConicWeights != ref.fConicWeights) {
361         SkASSERT(!genIDMatch);
362         return false;
363     }
364     return true;
365 }
366 
writeToBuffer(SkWBuffer * buffer) const367 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
368     SkDEBUGCODE(this->validate();)
369     SkDEBUGCODE(size_t beforePos = buffer->pos();)
370 
371     // Call getBounds() to ensure (as a side-effect) that fBounds
372     // and fIsFinite are computed.
373     const SkRect& bounds = this->getBounds();
374 
375     int32_t packed = ((fRRectOrOvalStartIdx & 7) << kRRectOrOvalStartIdx_SerializationShift) |
376                      ((fRRectOrOvalIsCCW & 1) << kRRectOrOvalIsCCW_SerializationShift) |
377                      ((fIsFinite & 1) << kIsFinite_SerializationShift) |
378                      ((fIsOval & 1) << kIsOval_SerializationShift) |
379                      ((fIsRRect & 1) << kIsRRect_SerializationShift) |
380                      (fSegmentMask << kSegmentMask_SerializationShift);
381     buffer->write32(packed);
382 
383     // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
384     // SkWBuffer. Until this is fixed we write 0.
385     buffer->write32(0);
386     buffer->write32(fVerbCnt);
387     buffer->write32(fPointCnt);
388     buffer->write32(fConicWeights.count());
389     buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
390     buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
391     buffer->write(fConicWeights.begin(), fConicWeights.bytes());
392     buffer->write(&bounds, sizeof(bounds));
393 
394     SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
395 }
396 
writeSize() const397 uint32_t SkPathRef::writeSize() const {
398     return uint32_t(5 * sizeof(uint32_t) +
399                     fVerbCnt * sizeof(uint8_t) +
400                     fPointCnt * sizeof(SkPoint) +
401                     fConicWeights.bytes() +
402                     sizeof(SkRect));
403 }
404 
copy(const SkPathRef & ref,int additionalReserveVerbs,int additionalReservePoints)405 void SkPathRef::copy(const SkPathRef& ref,
406                      int additionalReserveVerbs,
407                      int additionalReservePoints) {
408     SkDEBUGCODE(this->validate();)
409     this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
410                         additionalReserveVerbs, additionalReservePoints);
411     sk_careful_memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt*sizeof(uint8_t));
412     sk_careful_memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
413     fConicWeights = ref.fConicWeights;
414     fBoundsIsDirty = ref.fBoundsIsDirty;
415     if (!fBoundsIsDirty) {
416         fBounds = ref.fBounds;
417         fIsFinite = ref.fIsFinite;
418     }
419     fSegmentMask = ref.fSegmentMask;
420     fIsOval = ref.fIsOval;
421     fIsRRect = ref.fIsRRect;
422     fRRectOrOvalIsCCW = ref.fRRectOrOvalIsCCW;
423     fRRectOrOvalStartIdx = ref.fRRectOrOvalStartIdx;
424     SkDEBUGCODE(this->validate();)
425 }
426 
427 
interpolate(const SkPathRef & ending,SkScalar weight,SkPathRef * out) const428 void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const {
429     const SkScalar* inValues = &ending.getPoints()->fX;
430     SkScalar* outValues = &out->getPoints()->fX;
431     int count = out->countPoints() * 2;
432     for (int index = 0; index < count; ++index) {
433         outValues[index] = outValues[index] * weight + inValues[index] * (1 - weight);
434     }
435     out->fBoundsIsDirty = true;
436     out->fIsOval = false;
437     out->fIsRRect = false;
438 }
439 
growForRepeatedVerb(int verb,int numVbs,SkScalar ** weights)440 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
441                                         int numVbs,
442                                         SkScalar** weights) {
443     // This value is just made-up for now. When count is 4, calling memset was much
444     // slower than just writing the loop. This seems odd, and hopefully in the
445     // future this will appear to have been a fluke...
446     static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
447 
448     SkDEBUGCODE(this->validate();)
449     int pCnt;
450     bool dirtyAfterEdit = true;
451     switch (verb) {
452         case SkPath::kMove_Verb:
453             pCnt = numVbs;
454             dirtyAfterEdit = false;
455             break;
456         case SkPath::kLine_Verb:
457             fSegmentMask |= SkPath::kLine_SegmentMask;
458             pCnt = numVbs;
459             break;
460         case SkPath::kQuad_Verb:
461             fSegmentMask |= SkPath::kQuad_SegmentMask;
462             pCnt = 2 * numVbs;
463             break;
464         case SkPath::kConic_Verb:
465             fSegmentMask |= SkPath::kConic_SegmentMask;
466             pCnt = 2 * numVbs;
467             break;
468         case SkPath::kCubic_Verb:
469             fSegmentMask |= SkPath::kCubic_SegmentMask;
470             pCnt = 3 * numVbs;
471             break;
472         case SkPath::kClose_Verb:
473             SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
474             pCnt = 0;
475             dirtyAfterEdit = false;
476             break;
477         case SkPath::kDone_Verb:
478             SkDEBUGFAIL("growForRepeatedVerb called for kDone");
479             // fall through
480         default:
481             SkDEBUGFAIL("default should not be reached");
482             pCnt = 0;
483             dirtyAfterEdit = false;
484     }
485 
486     size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
487     this->makeSpace(space);
488 
489     SkPoint* ret = fPoints + fPointCnt;
490     uint8_t* vb = fVerbs - fVerbCnt;
491 
492     // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
493     // be 0, the compiler will remove the test/branch entirely.
494     if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
495         memset(vb - numVbs, verb, numVbs);
496     } else {
497         for (int i = 0; i < numVbs; ++i) {
498             vb[~i] = verb;
499         }
500     }
501 
502     fVerbCnt += numVbs;
503     fPointCnt += pCnt;
504     fFreeSpace -= space;
505     fBoundsIsDirty = true;  // this also invalidates fIsFinite
506     if (dirtyAfterEdit) {
507         fIsOval = false;
508         fIsRRect = false;
509     }
510 
511     if (SkPath::kConic_Verb == verb) {
512         SkASSERT(weights);
513         *weights = fConicWeights.append(numVbs);
514     }
515 
516     SkDEBUGCODE(this->validate();)
517     return ret;
518 }
519 
growForVerb(int verb,SkScalar weight)520 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
521     SkDEBUGCODE(this->validate();)
522     int pCnt;
523     bool dirtyAfterEdit = true;
524     switch (verb) {
525         case SkPath::kMove_Verb:
526             pCnt = 1;
527             dirtyAfterEdit = false;
528             break;
529         case SkPath::kLine_Verb:
530             fSegmentMask |= SkPath::kLine_SegmentMask;
531             pCnt = 1;
532             break;
533         case SkPath::kQuad_Verb:
534             fSegmentMask |= SkPath::kQuad_SegmentMask;
535             pCnt = 2;
536             break;
537         case SkPath::kConic_Verb:
538             fSegmentMask |= SkPath::kConic_SegmentMask;
539             pCnt = 2;
540             break;
541         case SkPath::kCubic_Verb:
542             fSegmentMask |= SkPath::kCubic_SegmentMask;
543             pCnt = 3;
544             break;
545         case SkPath::kClose_Verb:
546             pCnt = 0;
547             dirtyAfterEdit = false;
548             break;
549         case SkPath::kDone_Verb:
550             SkDEBUGFAIL("growForVerb called for kDone");
551             // fall through
552         default:
553             SkDEBUGFAIL("default is not reached");
554             dirtyAfterEdit = false;
555             pCnt = 0;
556     }
557     size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
558     this->makeSpace(space);
559     this->fVerbs[~fVerbCnt] = verb;
560     SkPoint* ret = fPoints + fPointCnt;
561     fVerbCnt += 1;
562     fPointCnt += pCnt;
563     fFreeSpace -= space;
564     fBoundsIsDirty = true;  // this also invalidates fIsFinite
565     if (dirtyAfterEdit) {
566         fIsOval = false;
567         fIsRRect = false;
568     }
569 
570     if (SkPath::kConic_Verb == verb) {
571         *fConicWeights.append() = weight;
572     }
573 
574     SkDEBUGCODE(this->validate();)
575     return ret;
576 }
577 
genID() const578 uint32_t SkPathRef::genID() const {
579     SkASSERT(!fEditorsAttached);
580     static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
581     if (!fGenerationID) {
582         if (0 == fPointCnt && 0 == fVerbCnt) {
583             fGenerationID = kEmptyGenID;
584         } else {
585             static int32_t  gPathRefGenerationID;
586             // do a loop in case our global wraps around, as we never want to return a 0 or the
587             // empty ID
588             do {
589                 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
590             } while (fGenerationID <= kEmptyGenID);
591         }
592     }
593     return fGenerationID;
594 }
595 
addGenIDChangeListener(GenIDChangeListener * listener)596 void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) {
597     if (nullptr == listener || this == gEmpty) {
598         delete listener;
599         return;
600     }
601     *fGenIDChangeListeners.append() = listener;
602 }
603 
604 // we need to be called *before* the genID gets changed or zerod
callGenIDChangeListeners()605 void SkPathRef::callGenIDChangeListeners() {
606     for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
607         fGenIDChangeListeners[i]->onChange();
608     }
609 
610     // Listeners get at most one shot, so whether these triggered or not, blow them away.
611     fGenIDChangeListeners.deleteAll();
612 }
613 
getRRect() const614 SkRRect SkPathRef::getRRect() const {
615     const SkRect& bounds = this->getBounds();
616     SkVector radii[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
617     Iter iter(*this);
618     SkPoint pts[4];
619     uint8_t verb = iter.next(pts);
620     SkASSERT(SkPath::kMove_Verb == verb);
621     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
622         if (SkPath::kConic_Verb == verb) {
623             SkVector v1_0 = pts[1] - pts[0];
624             SkVector v2_1 = pts[2] - pts[1];
625             SkVector dxdy;
626             if (v1_0.fX) {
627                 SkASSERT(!v2_1.fX && !v1_0.fY);
628                 dxdy.set(SkScalarAbs(v1_0.fX), SkScalarAbs(v2_1.fY));
629             } else if (!v1_0.fY) {
630                 SkASSERT(!v2_1.fX || !v2_1.fY);
631                 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v2_1.fY));
632             } else {
633                 SkASSERT(!v2_1.fY);
634                 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v1_0.fY));
635             }
636             SkRRect::Corner corner =
637                     pts[1].fX == bounds.fLeft ?
638                         pts[1].fY == bounds.fTop ?
639                             SkRRect::kUpperLeft_Corner : SkRRect::kLowerLeft_Corner :
640                     pts[1].fY == bounds.fTop ?
641                             SkRRect::kUpperRight_Corner : SkRRect::kLowerRight_Corner;
642             SkASSERT(!radii[corner].fX && !radii[corner].fY);
643             radii[corner] = dxdy;
644         } else {
645             SkASSERT((verb == SkPath::kLine_Verb
646                     && (!(pts[1].fX - pts[0].fX) || !(pts[1].fY - pts[0].fY)))
647                     || verb == SkPath::kClose_Verb);
648         }
649     }
650     SkRRect rrect;
651     rrect.setRectRadii(bounds, radii);
652     return rrect;
653 }
654 
655 ///////////////////////////////////////////////////////////////////////////////
656 
Iter()657 SkPathRef::Iter::Iter() {
658 #ifdef SK_DEBUG
659     fPts = nullptr;
660     fConicWeights = nullptr;
661 #endif
662     // need to init enough to make next() harmlessly return kDone_Verb
663     fVerbs = nullptr;
664     fVerbStop = nullptr;
665 }
666 
Iter(const SkPathRef & path)667 SkPathRef::Iter::Iter(const SkPathRef& path) {
668     this->setPathRef(path);
669 }
670 
setPathRef(const SkPathRef & path)671 void SkPathRef::Iter::setPathRef(const SkPathRef& path) {
672     fPts = path.points();
673     fVerbs = path.verbs();
674     fVerbStop = path.verbsMemBegin();
675     fConicWeights = path.conicWeights();
676     if (fConicWeights) {
677       fConicWeights -= 1;  // begin one behind
678     }
679 }
680 
next(SkPoint pts[4])681 uint8_t SkPathRef::Iter::next(SkPoint pts[4]) {
682     SkASSERT(pts);
683     if (fVerbs == fVerbStop) {
684         return (uint8_t) SkPath::kDone_Verb;
685     }
686 
687     // fVerbs points one beyond next verb so decrement first.
688     unsigned verb = *(--fVerbs);
689     const SkPoint* srcPts = fPts;
690 
691     switch (verb) {
692         case SkPath::kMove_Verb:
693             pts[0] = srcPts[0];
694             srcPts += 1;
695             break;
696         case SkPath::kLine_Verb:
697             pts[0] = srcPts[-1];
698             pts[1] = srcPts[0];
699             srcPts += 1;
700             break;
701         case SkPath::kConic_Verb:
702             fConicWeights += 1;
703             // fall-through
704         case SkPath::kQuad_Verb:
705             pts[0] = srcPts[-1];
706             pts[1] = srcPts[0];
707             pts[2] = srcPts[1];
708             srcPts += 2;
709             break;
710         case SkPath::kCubic_Verb:
711             pts[0] = srcPts[-1];
712             pts[1] = srcPts[0];
713             pts[2] = srcPts[1];
714             pts[3] = srcPts[2];
715             srcPts += 3;
716             break;
717         case SkPath::kClose_Verb:
718             break;
719         case SkPath::kDone_Verb:
720             SkASSERT(fVerbs == fVerbStop);
721             break;
722     }
723     fPts = srcPts;
724     return (uint8_t) verb;
725 }
726 
peek() const727 uint8_t SkPathRef::Iter::peek() const {
728     const uint8_t* next = fVerbs - 1;
729     return next <= fVerbStop ? (uint8_t) SkPath::kDone_Verb : *next;
730 }
731 
732 #ifdef SK_DEBUG
733 
734 #include "SkNx.h"
735 
validate() const736 void SkPathRef::validate() const {
737     SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
738     SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
739     SkASSERT((nullptr == fPoints) == (nullptr == fVerbs));
740     SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace));
741     SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace));
742     SkASSERT(!(nullptr == fPoints && fPointCnt));
743     SkASSERT(!(nullptr == fVerbs && fVerbCnt));
744     SkASSERT(this->currSize() ==
745                 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
746 
747     if (fIsOval || fIsRRect) {
748         // Currently we don't allow both of these to be set, even though ovals are round rects.
749         SkASSERT(fIsOval != fIsRRect);
750         if (fIsOval) {
751             SkASSERT(fRRectOrOvalStartIdx < 4);
752         } else {
753             SkASSERT(fRRectOrOvalStartIdx < 8);
754         }
755     }
756 
757     if (!fBoundsIsDirty && !fBounds.isEmpty()) {
758         bool isFinite = true;
759         Sk2s leftTop = Sk2s(fBounds.fLeft, fBounds.fTop);
760         Sk2s rightBot = Sk2s(fBounds.fRight, fBounds.fBottom);
761         for (int i = 0; i < fPointCnt; ++i) {
762             Sk2s point = Sk2s(fPoints[i].fX, fPoints[i].fY);
763 #ifdef SK_DEBUG
764             if (fPoints[i].isFinite() &&
765                 ((point < leftTop).anyTrue() || (point > rightBot).anyTrue())) {
766                 SkDebugf("bounds: %f %f %f %f\n",
767                          fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
768                 for (int j = 0; j < fPointCnt; ++j) {
769                     if (i == j) {
770                         SkDebugf("*");
771                     }
772                     SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY);
773                 }
774             }
775 #endif
776 
777             SkASSERT(!fPoints[i].isFinite() ||
778                     (!(point < leftTop).anyTrue() && !(point > rightBot).anyTrue()));
779             if (!fPoints[i].isFinite()) {
780                 isFinite = false;
781             }
782         }
783         SkASSERT(SkToBool(fIsFinite) == isFinite);
784     }
785 
786 #ifdef SK_DEBUG_PATH
787     uint32_t mask = 0;
788     for (int i = 0; i < fVerbCnt; ++i) {
789         switch (fVerbs[~i]) {
790             case SkPath::kMove_Verb:
791                 break;
792             case SkPath::kLine_Verb:
793                 mask |= SkPath::kLine_SegmentMask;
794                 break;
795             case SkPath::kQuad_Verb:
796                 mask |= SkPath::kQuad_SegmentMask;
797                 break;
798             case SkPath::kConic_Verb:
799                 mask |= SkPath::kConic_SegmentMask;
800                 break;
801             case SkPath::kCubic_Verb:
802                 mask |= SkPath::kCubic_SegmentMask;
803                 break;
804             case SkPath::kClose_Verb:
805                 break;
806             case SkPath::kDone_Verb:
807                 SkDEBUGFAIL("Done verb shouldn't be recorded.");
808                 break;
809             default:
810                 SkDEBUGFAIL("Unknown Verb");
811                 break;
812         }
813     }
814     SkASSERT(mask == fSegmentMask);
815 #endif // SK_DEBUG_PATH
816 }
817 #endif
818