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