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