• 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 "include/private/SkPathRef.h"
9 
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRRect.h"
13 #include "include/private/base/SkOnce.h"
14 #include "src/base/SkVx.h"
15 
16 #include <cstring>
17 
18 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
19     static constexpr int kPathRefGenIDBitCnt = 30; // leave room for the fill type (skbug.com/1762)
20 #else
21     static constexpr int kPathRefGenIDBitCnt = 32;
22 #endif
23 
24 //////////////////////////////////////////////////////////////////////////////
Editor(sk_sp<SkPathRef> * pathRef,int incReserveVerbs,int incReservePoints)25 SkPathRef::Editor::Editor(sk_sp<SkPathRef>* pathRef,
26                           int incReserveVerbs,
27                           int incReservePoints)
28 {
29     SkASSERT(incReserveVerbs >= 0);
30     SkASSERT(incReservePoints >= 0);
31 
32     if ((*pathRef)->unique()) {
33         (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
34     } else {
35         SkPathRef* copy;
36         // No need to copy if the existing ref is the empty ref (because it doesn't contain
37         // anything).
38         if (!(*pathRef)->isInitialEmptyPathRef()) {
39             copy = new SkPathRef;
40             copy->copy(**pathRef, incReserveVerbs, incReservePoints);
41         } else {
42             // Size previously empty paths to exactly fit the supplied hints. The assumpion is
43             // the caller knows the exact size they want (as happens in chrome when deserializing
44             // paths).
45             copy = new SkPathRef(incReserveVerbs, incReservePoints);
46         }
47         pathRef->reset(copy);
48     }
49     fPathRef = pathRef->get();
50     fPathRef->callGenIDChangeListeners();
51     fPathRef->fGenerationID = 0;
52     fPathRef->fBoundsIsDirty = true;
53     SkDEBUGCODE(fPathRef->fEditorsAttached++;)
54 }
55 
56 //////////////////////////////////////////////////////////////////////////////
57 
approximateBytesUsed() const58 size_t SkPathRef::approximateBytesUsed() const {
59     return sizeof(SkPathRef)
60          + fPoints      .capacity() * sizeof(fPoints      [0])
61          + fVerbs       .capacity() * sizeof(fVerbs       [0])
62          + fConicWeights.capacity() * sizeof(fConicWeights[0]);
63 }
64 
~SkPathRef()65 SkPathRef::~SkPathRef() {
66     // Deliberately don't validate() this path ref, otherwise there's no way
67     // to read one that's not valid and then free its memory without asserting.
68     SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;)
69     SkDEBUGCODE(fEditorsAttached.store(0x7777777);)
70 }
71 
72 static SkPathRef* gEmpty = nullptr;
73 
CreateEmpty()74 SkPathRef* SkPathRef::CreateEmpty() {
75     static SkOnce once;
76     once([]{
77         gEmpty = new SkPathRef;
78         gEmpty->computeBounds();   // Avoids races later to be the first to do this.
79     });
80     return SkRef(gEmpty);
81 }
82 
transform_dir_and_start(const SkMatrix & matrix,bool isRRect,bool * isCCW,unsigned * start)83 static void transform_dir_and_start(const SkMatrix& matrix, bool isRRect, bool* isCCW,
84                                     unsigned* start) {
85     int inStart = *start;
86     int rm = 0;
87     if (isRRect) {
88         // Degenerate rrect indices to oval indices and remember the remainder.
89         // Ovals have one index per side whereas rrects have two.
90         rm = inStart & 0b1;
91         inStart /= 2;
92     }
93     // Is the antidiagonal non-zero (otherwise the diagonal is zero)
94     int antiDiag;
95     // Is the non-zero value in the top row (either kMScaleX or kMSkewX) negative
96     int topNeg;
97     // Are the two non-zero diagonal or antidiagonal values the same sign.
98     int sameSign;
99     if (matrix.get(SkMatrix::kMScaleX) != 0) {
100         antiDiag = 0b00;
101         if (matrix.get(SkMatrix::kMScaleX) > 0) {
102             topNeg = 0b00;
103             sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b01 : 0b00;
104         } else {
105             topNeg = 0b10;
106             sameSign = matrix.get(SkMatrix::kMScaleY) > 0 ? 0b00 : 0b01;
107         }
108     } else {
109         antiDiag = 0b01;
110         if (matrix.get(SkMatrix::kMSkewX) > 0) {
111             topNeg = 0b00;
112             sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b01 : 0b00;
113         } else {
114             topNeg = 0b10;
115             sameSign = matrix.get(SkMatrix::kMSkewY) > 0 ? 0b00 : 0b01;
116         }
117     }
118     if (sameSign != antiDiag) {
119         // This is a rotation (and maybe scale). The direction is unchanged.
120         // Trust me on the start computation (or draw yourself some pictures)
121         *start = (inStart + 4 - (topNeg | antiDiag)) % 4;
122         SkASSERT(*start < 4);
123         if (isRRect) {
124             *start = 2 * *start + rm;
125         }
126     } else {
127         // This is a mirror (and maybe scale). The direction is reversed.
128         *isCCW = !*isCCW;
129         // Trust me on the start computation (or draw yourself some pictures)
130         *start = (6 + (topNeg | antiDiag) - inStart) % 4;
131         SkASSERT(*start < 4);
132         if (isRRect) {
133             *start = 2 * *start + (rm ? 0 : 1);
134         }
135     }
136 }
137 
CreateTransformedCopy(sk_sp<SkPathRef> * dst,const SkPathRef & src,const SkMatrix & matrix)138 void SkPathRef::CreateTransformedCopy(sk_sp<SkPathRef>* dst,
139                                       const SkPathRef& src,
140                                       const SkMatrix& matrix) {
141     SkDEBUGCODE(src.validate();)
142     if (matrix.isIdentity()) {
143         if (dst->get() != &src) {
144             src.ref();
145             dst->reset(const_cast<SkPathRef*>(&src));
146             SkDEBUGCODE((*dst)->validate();)
147         }
148         return;
149     }
150 
151     sk_sp<const SkPathRef> srcKeepAlive;
152     if (!(*dst)->unique()) {
153         // If dst and src are the same then we are about to drop our only ref on the common path
154         // ref. Some other thread may have owned src when we checked unique() above but it may not
155         // continue to do so. Add another ref so we continue to be an owner until we're done.
156         if (dst->get() == &src) {
157             srcKeepAlive.reset(SkRef(&src));
158         }
159         dst->reset(new SkPathRef);
160     }
161 
162     if (dst->get() != &src) {
163         (*dst)->fVerbs = src.fVerbs;
164         (*dst)->fConicWeights = src.fConicWeights;
165         (*dst)->callGenIDChangeListeners();
166         (*dst)->fGenerationID = 0;  // mark as dirty
167         // don't copy, just allocate the points
168         (*dst)->fPoints.resize(src.fPoints.size());
169     }
170     matrix.mapPoints((*dst)->fPoints.begin(), src.fPoints.begin(), src.fPoints.size());
171 
172     // Need to check this here in case (&src == dst)
173     bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
174 
175     /*
176      *  Here we optimize the bounds computation, by noting if the bounds are
177      *  already known, and if so, we just transform those as well and mark
178      *  them as "known", rather than force the transformed path to have to
179      *  recompute them.
180      *
181      *  Special gotchas if the path is effectively empty (<= 1 point) or
182      *  if it is non-finite. In those cases bounds need to stay empty,
183      *  regardless of the matrix.
184      */
185     if (canXformBounds) {
186         (*dst)->fBoundsIsDirty = false;
187         if (src.fIsFinite) {
188             matrix.mapRect(&(*dst)->fBounds, src.fBounds);
189             if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
190                 (*dst)->fBounds.setEmpty();
191             }
192         } else {
193             (*dst)->fIsFinite = false;
194             (*dst)->fBounds.setEmpty();
195         }
196     } else {
197         (*dst)->fBoundsIsDirty = true;
198     }
199 
200     (*dst)->fSegmentMask = src.fSegmentMask;
201 
202     // It's an oval only if it stays a rect.
203     bool rectStaysRect = matrix.rectStaysRect();
204     (*dst)->fIsOval = src.fIsOval && rectStaysRect;
205     (*dst)->fIsRRect = src.fIsRRect && rectStaysRect;
206     if ((*dst)->fIsOval || (*dst)->fIsRRect) {
207         unsigned start = src.fRRectOrOvalStartIdx;
208         bool isCCW = SkToBool(src.fRRectOrOvalIsCCW);
209         transform_dir_and_start(matrix, (*dst)->fIsRRect, &isCCW, &start);
210         (*dst)->fRRectOrOvalIsCCW = isCCW;
211         (*dst)->fRRectOrOvalStartIdx = start;
212     }
213 
214     if (dst->get() == &src) {
215         (*dst)->callGenIDChangeListeners();
216         (*dst)->fGenerationID = 0;
217     }
218 
219     SkDEBUGCODE((*dst)->validate();)
220 }
221 
Rewind(sk_sp<SkPathRef> * pathRef)222 void SkPathRef::Rewind(sk_sp<SkPathRef>* pathRef) {
223     if ((*pathRef)->unique()) {
224         SkDEBUGCODE((*pathRef)->validate();)
225         (*pathRef)->callGenIDChangeListeners();
226         (*pathRef)->fBoundsIsDirty = true;  // this also invalidates fIsFinite
227         (*pathRef)->fGenerationID = 0;
228         (*pathRef)->fPoints.clear();
229         (*pathRef)->fVerbs.clear();
230         (*pathRef)->fConicWeights.clear();
231         (*pathRef)->fSegmentMask = 0;
232         (*pathRef)->fIsOval = false;
233         (*pathRef)->fIsRRect = false;
234         SkDEBUGCODE((*pathRef)->validate();)
235     } else {
236         int oldVCnt = (*pathRef)->countVerbs();
237         int oldPCnt = (*pathRef)->countPoints();
238         pathRef->reset(new SkPathRef);
239         (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
240     }
241 }
242 
operator ==(const SkPathRef & ref) const243 bool SkPathRef::operator== (const SkPathRef& ref) const {
244     SkDEBUGCODE(this->validate();)
245     SkDEBUGCODE(ref.validate();)
246 
247     // We explicitly check fSegmentMask as a quick-reject. We could skip it,
248     // since it is only a cache of info in the fVerbs, but its a fast way to
249     // notice a difference
250     if (fSegmentMask != ref.fSegmentMask) {
251         return false;
252     }
253 
254     bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
255 #ifdef SK_RELEASE
256     if (genIDMatch) {
257         return true;
258     }
259 #endif
260     if (fPoints != ref.fPoints || fConicWeights != ref.fConicWeights || fVerbs != ref.fVerbs) {
261         SkASSERT(!genIDMatch);
262         return false;
263     }
264     if (ref.fVerbs.empty()) {
265         SkASSERT(ref.fPoints.empty());
266     }
267     return true;
268 }
269 
copy(const SkPathRef & ref,int additionalReserveVerbs,int additionalReservePoints)270 void SkPathRef::copy(const SkPathRef& ref,
271                      int additionalReserveVerbs,
272                      int additionalReservePoints) {
273     SkDEBUGCODE(this->validate();)
274     this->resetToSize(ref.fVerbs.size(), ref.fPoints.size(), ref.fConicWeights.size(),
275                       additionalReserveVerbs, additionalReservePoints);
276     fVerbs = ref.fVerbs;
277     fPoints = ref.fPoints;
278     fConicWeights = ref.fConicWeights;
279     fBoundsIsDirty = ref.fBoundsIsDirty;
280     if (!fBoundsIsDirty) {
281         fBounds = ref.fBounds;
282         fIsFinite = ref.fIsFinite;
283     }
284     fSegmentMask = ref.fSegmentMask;
285     fIsOval = ref.fIsOval;
286     fIsRRect = ref.fIsRRect;
287     fRRectOrOvalIsCCW = ref.fRRectOrOvalIsCCW;
288     fRRectOrOvalStartIdx = ref.fRRectOrOvalStartIdx;
289     SkDEBUGCODE(this->validate();)
290 }
291 
interpolate(const SkPathRef & ending,SkScalar weight,SkPathRef * out) const292 void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const {
293     const SkScalar* inValues = &ending.getPoints()->fX;
294     SkScalar* outValues = &out->getWritablePoints()->fX;
295     int count = out->countPoints() * 2;
296     for (int index = 0; index < count; ++index) {
297         outValues[index] = outValues[index] * weight + inValues[index] * (1 - weight);
298     }
299     out->fBoundsIsDirty = true;
300     out->fIsOval = false;
301     out->fIsRRect = false;
302 }
303 
growForVerbsInPath(const SkPathRef & path)304 std::tuple<SkPoint*, SkScalar*> SkPathRef::growForVerbsInPath(const SkPathRef& path) {
305     SkDEBUGCODE(this->validate();)
306 
307     fSegmentMask |= path.fSegmentMask;
308     fBoundsIsDirty = true;  // this also invalidates fIsFinite
309     fIsOval = false;
310     fIsRRect = false;
311 
312     if (int numVerbs = path.countVerbs()) {
313         memcpy(fVerbs.push_back_n(numVerbs), path.fVerbs.begin(), numVerbs * sizeof(fVerbs[0]));
314     }
315 
316     SkPoint* pts = nullptr;
317     if (int numPts = path.countPoints()) {
318         pts = fPoints.push_back_n(numPts);
319     }
320 
321     SkScalar* weights = nullptr;
322     if (int numConics = path.countWeights()) {
323         weights = fConicWeights.push_back_n(numConics);
324     }
325 
326     SkDEBUGCODE(this->validate();)
327     return {pts, weights};
328 }
329 
growForRepeatedVerb(int verb,int numVbs,SkScalar ** weights)330 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
331                                         int numVbs,
332                                         SkScalar** weights) {
333     SkDEBUGCODE(this->validate();)
334     int pCnt;
335     switch (verb) {
336         case SkPath::kMove_Verb:
337             pCnt = numVbs;
338             break;
339         case SkPath::kLine_Verb:
340             fSegmentMask |= SkPath::kLine_SegmentMask;
341             pCnt = numVbs;
342             break;
343         case SkPath::kQuad_Verb:
344             fSegmentMask |= SkPath::kQuad_SegmentMask;
345             pCnt = 2 * numVbs;
346             break;
347         case SkPath::kConic_Verb:
348             fSegmentMask |= SkPath::kConic_SegmentMask;
349             pCnt = 2 * numVbs;
350             break;
351         case SkPath::kCubic_Verb:
352             fSegmentMask |= SkPath::kCubic_SegmentMask;
353             pCnt = 3 * numVbs;
354             break;
355         case SkPath::kClose_Verb:
356             SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
357             pCnt = 0;
358             break;
359         case SkPath::kDone_Verb:
360             SkDEBUGFAIL("growForRepeatedVerb called for kDone");
361             pCnt = 0;
362             break;
363         default:
364             SkDEBUGFAIL("default should not be reached");
365             pCnt = 0;
366             break;
367     }
368 
369     fBoundsIsDirty = true;  // this also invalidates fIsFinite
370     fIsOval = false;
371     fIsRRect = false;
372 
373     memset(fVerbs.push_back_n(numVbs), verb, numVbs);
374     if (SkPath::kConic_Verb == verb) {
375         SkASSERT(weights);
376         *weights = fConicWeights.push_back_n(numVbs);
377     }
378     SkPoint* pts = fPoints.push_back_n(pCnt);
379 
380     SkDEBUGCODE(this->validate();)
381     return pts;
382 }
383 
growForVerb(int verb,SkScalar weight)384 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
385     SkDEBUGCODE(this->validate();)
386     int pCnt;
387     unsigned mask = 0;
388     switch (verb) {
389         case SkPath::kMove_Verb:
390             pCnt = 1;
391             break;
392         case SkPath::kLine_Verb:
393             mask = SkPath::kLine_SegmentMask;
394             pCnt = 1;
395             break;
396         case SkPath::kQuad_Verb:
397             mask = SkPath::kQuad_SegmentMask;
398             pCnt = 2;
399             break;
400         case SkPath::kConic_Verb:
401             mask = SkPath::kConic_SegmentMask;
402             pCnt = 2;
403             break;
404         case SkPath::kCubic_Verb:
405             mask = SkPath::kCubic_SegmentMask;
406             pCnt = 3;
407             break;
408         case SkPath::kClose_Verb:
409             pCnt = 0;
410             break;
411         case SkPath::kDone_Verb:
412             SkDEBUGFAIL("growForVerb called for kDone");
413             pCnt = 0;
414             break;
415         default:
416             SkDEBUGFAIL("default is not reached");
417             pCnt = 0;
418             break;
419     }
420 
421     fSegmentMask |= mask;
422     fBoundsIsDirty = true;  // this also invalidates fIsFinite
423     fIsOval = false;
424     fIsRRect = false;
425 
426     fVerbs.push_back(verb);
427     if (SkPath::kConic_Verb == verb) {
428         fConicWeights.push_back(weight);
429     }
430     SkPoint* pts = fPoints.push_back_n(pCnt);
431 
432     SkDEBUGCODE(this->validate();)
433     return pts;
434 }
435 
genID(uint8_t fillType) const436 uint32_t SkPathRef::genID(uint8_t fillType) const {
437     SkASSERT(fEditorsAttached.load() == 0);
438     static const uint32_t kMask = (static_cast<int64_t>(1) << kPathRefGenIDBitCnt) - 1;
439 
440     if (fGenerationID == 0) {
441         if (fPoints.empty() && fVerbs.empty()) {
442             fGenerationID = kEmptyGenID;
443         } else {
444             static std::atomic<uint32_t> nextID{kEmptyGenID + 1};
445             do {
446                 fGenerationID = nextID.fetch_add(1, std::memory_order_relaxed) & kMask;
447             } while (fGenerationID == 0 || fGenerationID == kEmptyGenID);
448         }
449     }
450     #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
451         SkASSERT((unsigned)fillType < (1 << (32 - kPathRefGenIDBitCnt)));
452         fGenerationID |= static_cast<uint32_t>(fillType) << kPathRefGenIDBitCnt;
453     #endif
454     return fGenerationID;
455 }
456 
addGenIDChangeListener(sk_sp<SkIDChangeListener> listener)457 void SkPathRef::addGenIDChangeListener(sk_sp<SkIDChangeListener> listener) {
458     if (this == gEmpty) {
459         return;
460     }
461     fGenIDChangeListeners.add(std::move(listener));
462 }
463 
genIDChangeListenerCount()464 int SkPathRef::genIDChangeListenerCount() { return fGenIDChangeListeners.count(); }
465 
466 // we need to be called *before* the genID gets changed or zerod
callGenIDChangeListeners()467 void SkPathRef::callGenIDChangeListeners() {
468     fGenIDChangeListeners.changed();
469 }
470 
getRRect() const471 SkRRect SkPathRef::getRRect() const {
472     const SkRect& bounds = this->getBounds();
473     SkVector radii[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
474     Iter iter(*this);
475     SkPoint pts[4];
476     uint8_t verb = iter.next(pts);
477     SkASSERT(SkPath::kMove_Verb == verb);
478     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
479         if (SkPath::kConic_Verb == verb) {
480             SkVector v1_0 = pts[1] - pts[0];
481             SkVector v2_1 = pts[2] - pts[1];
482             SkVector dxdy;
483             if (v1_0.fX) {
484                 SkASSERT(!v2_1.fX && !v1_0.fY);
485                 dxdy.set(SkScalarAbs(v1_0.fX), SkScalarAbs(v2_1.fY));
486             } else if (!v1_0.fY) {
487                 SkASSERT(!v2_1.fX || !v2_1.fY);
488                 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v2_1.fY));
489             } else {
490                 SkASSERT(!v2_1.fY);
491                 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v1_0.fY));
492             }
493             SkRRect::Corner corner =
494                     pts[1].fX == bounds.fLeft ?
495                         pts[1].fY == bounds.fTop ?
496                             SkRRect::kUpperLeft_Corner : SkRRect::kLowerLeft_Corner :
497                     pts[1].fY == bounds.fTop ?
498                             SkRRect::kUpperRight_Corner : SkRRect::kLowerRight_Corner;
499             SkASSERT(!radii[corner].fX && !radii[corner].fY);
500             radii[corner] = dxdy;
501         } else {
502             SkASSERT((verb == SkPath::kLine_Verb
503                     && (!(pts[1].fX - pts[0].fX) || !(pts[1].fY - pts[0].fY)))
504                     || verb == SkPath::kClose_Verb);
505         }
506     }
507     SkRRect rrect;
508     rrect.setRectRadii(bounds, radii);
509     return rrect;
510 }
511 
isRRect(SkRRect * rrect,bool * isCCW,unsigned * start) const512 bool SkPathRef::isRRect(SkRRect* rrect, bool* isCCW, unsigned* start) const {
513         if (fIsRRect) {
514             if (rrect) {
515                 *rrect = this->getRRect();
516             }
517             if (isCCW) {
518                 *isCCW = SkToBool(fRRectOrOvalIsCCW);
519             }
520             if (start) {
521                 *start = fRRectOrOvalStartIdx;
522             }
523         }
524         return SkToBool(fIsRRect);
525     }
526 
527 ///////////////////////////////////////////////////////////////////////////////
528 
Iter()529 SkPathRef::Iter::Iter() {
530 #ifdef SK_DEBUG
531     fPts = nullptr;
532     fConicWeights = nullptr;
533 #endif
534     // need to init enough to make next() harmlessly return kDone_Verb
535     fVerbs = nullptr;
536     fVerbStop = nullptr;
537 }
538 
Iter(const SkPathRef & path)539 SkPathRef::Iter::Iter(const SkPathRef& path) {
540     this->setPathRef(path);
541 }
542 
setPathRef(const SkPathRef & path)543 void SkPathRef::Iter::setPathRef(const SkPathRef& path) {
544     fPts = path.points();
545     fVerbs = path.verbsBegin();
546     fVerbStop = path.verbsEnd();
547     fConicWeights = path.conicWeights();
548     if (fConicWeights) {
549         fConicWeights -= 1;  // begin one behind
550     }
551 
552     // Don't allow iteration through non-finite points.
553     if (!path.isFinite()) {
554         fVerbStop = fVerbs;
555     }
556 }
557 
next(SkPoint pts[4])558 uint8_t SkPathRef::Iter::next(SkPoint pts[4]) {
559     SkASSERT(pts);
560 
561     SkDEBUGCODE(unsigned peekResult = this->peek();)
562 
563     if (fVerbs == fVerbStop) {
564         SkASSERT(peekResult == SkPath::kDone_Verb);
565         return (uint8_t) SkPath::kDone_Verb;
566     }
567 
568     // fVerbs points one beyond next verb so decrement first.
569     unsigned verb = *fVerbs++;
570     const SkPoint* srcPts = fPts;
571 
572     switch (verb) {
573         case SkPath::kMove_Verb:
574             pts[0] = srcPts[0];
575             srcPts += 1;
576             break;
577         case SkPath::kLine_Verb:
578             pts[0] = srcPts[-1];
579             pts[1] = srcPts[0];
580             srcPts += 1;
581             break;
582         case SkPath::kConic_Verb:
583             fConicWeights += 1;
584             [[fallthrough]];
585         case SkPath::kQuad_Verb:
586             pts[0] = srcPts[-1];
587             pts[1] = srcPts[0];
588             pts[2] = srcPts[1];
589             srcPts += 2;
590             break;
591         case SkPath::kCubic_Verb:
592             pts[0] = srcPts[-1];
593             pts[1] = srcPts[0];
594             pts[2] = srcPts[1];
595             pts[3] = srcPts[2];
596             srcPts += 3;
597             break;
598         case SkPath::kClose_Verb:
599             break;
600         case SkPath::kDone_Verb:
601             SkASSERT(fVerbs == fVerbStop);
602             break;
603     }
604     fPts = srcPts;
605     SkASSERT(peekResult == verb);
606     return (uint8_t) verb;
607 }
608 
peek() const609 uint8_t SkPathRef::Iter::peek() const {
610     return fVerbs < fVerbStop ? *fVerbs : (uint8_t) SkPath::kDone_Verb;
611 }
612 
613 
isValid() const614 bool SkPathRef::isValid() const {
615     if (fIsOval || fIsRRect) {
616         // Currently we don't allow both of these to be set, even though ovals are ro
617         if (fIsOval == fIsRRect) {
618             return false;
619         }
620         if (fIsOval) {
621             if (fRRectOrOvalStartIdx >= 4) {
622                 return false;
623             }
624         } else {
625             if (fRRectOrOvalStartIdx >= 8) {
626                 return false;
627             }
628         }
629     }
630 
631     if (!fBoundsIsDirty && !fBounds.isEmpty()) {
632         bool isFinite = true;
633         auto leftTop = skvx::float2(fBounds.fLeft, fBounds.fTop);
634         auto rightBot = skvx::float2(fBounds.fRight, fBounds.fBottom);
635         for (int i = 0; i < fPoints.size(); ++i) {
636             auto point = skvx::float2(fPoints[i].fX, fPoints[i].fY);
637 #ifdef SK_DEBUG
638             if (fPoints[i].isFinite() && (any(point < leftTop)|| any(point > rightBot))) {
639                 SkDebugf("bad SkPathRef bounds: %g %g %g %g\n",
640                          fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
641                 for (int j = 0; j < fPoints.size(); ++j) {
642                     if (i == j) {
643                         SkDebugf("*** bounds do not contain: ");
644                     }
645                     SkDebugf("%g %g\n", fPoints[j].fX, fPoints[j].fY);
646                 }
647                 return false;
648             }
649 #endif
650 
651             if (fPoints[i].isFinite() && any(point < leftTop) && !any(point > rightBot))
652                 return false;
653             if (!fPoints[i].isFinite()) {
654                 isFinite = false;
655             }
656         }
657         if (SkToBool(fIsFinite) != isFinite) {
658             return false;
659         }
660     }
661     return true;
662 }
663 
reset()664 void SkPathRef::reset() {
665     commonReset();
666     fPoints.clear();
667     fVerbs.clear();
668     fConicWeights.clear();
669     SkDEBUGCODE(validate();)
670 }
671 
dataMatchesVerbs() const672 bool SkPathRef::dataMatchesVerbs() const {
673     const auto info = sk_path_analyze_verbs(fVerbs.begin(), fVerbs.size());
674     return info.valid                          &&
675            info.segmentMask == fSegmentMask    &&
676            info.points      == fPoints.size()  &&
677            info.weights     == fConicWeights.size();
678 }
679