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 int ptCount = 0;
194 int conicCount = 0;
195 for (int i = 0; i < vCount; ++i) {
196 switch (verbs[i]) {
197 case SkPath::kMove_Verb:
198 case SkPath::kLine_Verb:
199 ptCount += 1;
200 break;
201 case SkPath::kConic_Verb:
202 conicCount += 1;
203 // fall-through
204 case SkPath::kQuad_Verb:
205 ptCount += 2;
206 break;
207 case SkPath::kCubic_Verb:
208 ptCount += 3;
209 break;
210 case SkPath::kClose_Verb:
211 break;
212 default:
213 return false;
214 }
215 }
216 *ptCountPtr = ptCount;
217 *conicCountPtr = conicCount;
218 return true;
219 }
220
CreateFromBuffer(SkRBuffer * buffer)221 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
222 std::unique_ptr<SkPathRef> ref(new SkPathRef);
223
224 int32_t packed;
225 if (!buffer->readS32(&packed)) {
226 return nullptr;
227 }
228
229 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
230 uint8_t segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
231 bool isOval = (packed >> kIsOval_SerializationShift) & 1;
232 bool isRRect = (packed >> kIsRRect_SerializationShift) & 1;
233 if (isOval && isRRect) {
234 // Fuzzing generates data with both oval and rrect flags set; abort early in this case/
235 return nullptr;
236 }
237 bool rrectOrOvalIsCCW = (packed >> kRRectOrOvalIsCCW_SerializationShift) & 1;
238 unsigned rrectOrOvalStartIdx = (packed >> kRRectOrOvalStartIdx_SerializationShift) & 0x7;
239
240 int32_t verbCount, pointCount, conicCount;
241 ptrdiff_t maxPtrDiff = std::numeric_limits<ptrdiff_t>::max();
242 if (!buffer->readU32(&(ref->fGenerationID)) ||
243 !buffer->readS32(&verbCount) ||
244 verbCount < 0 ||
245 static_cast<uint32_t>(verbCount) > maxPtrDiff/sizeof(uint8_t) ||
246 !buffer->readS32(&pointCount) ||
247 pointCount < 0 ||
248 static_cast<uint32_t>(pointCount) > maxPtrDiff/sizeof(SkPoint) ||
249 sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount >
250 static_cast<size_t>(maxPtrDiff) ||
251 !buffer->readS32(&conicCount) ||
252 conicCount < 0) {
253 return nullptr;
254 }
255
256 ref->resetToSize(verbCount, pointCount, conicCount);
257 SkASSERT(verbCount == ref->countVerbs());
258 SkASSERT(pointCount == ref->countPoints());
259 SkASSERT(conicCount == ref->fConicWeights.count());
260
261 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
262 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
263 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
264 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
265 return nullptr;
266 }
267
268 // Check that the verbs are valid, and imply the correct number of pts and conics
269 {
270 int pCount, cCount;
271 if (!deduce_pts_conics(ref->verbsMemBegin(), ref->countVerbs(), &pCount, &cCount) ||
272 pCount != ref->countPoints() || cCount != ref->fConicWeights.count()) {
273 return nullptr;
274 }
275 // Check that the bounds match the serialized bounds.
276 SkRect bounds;
277 if (ComputePtBounds(&bounds, *ref) != SkToBool(ref->fIsFinite) || bounds != ref->fBounds) {
278 return nullptr;
279 }
280 }
281
282 ref->fBoundsIsDirty = false;
283
284 // resetToSize clears fSegmentMask and fIsOval
285 ref->fSegmentMask = segmentMask;
286 ref->fIsOval = isOval;
287 ref->fIsRRect = isRRect;
288 ref->fRRectOrOvalIsCCW = rrectOrOvalIsCCW;
289 ref->fRRectOrOvalStartIdx = rrectOrOvalStartIdx;
290 return ref.release();
291 }
292
Rewind(sk_sp<SkPathRef> * pathRef)293 void SkPathRef::Rewind(sk_sp<SkPathRef>* pathRef) {
294 if ((*pathRef)->unique()) {
295 SkDEBUGCODE((*pathRef)->validate();)
296 (*pathRef)->callGenIDChangeListeners();
297 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
298 (*pathRef)->fVerbCnt = 0;
299 (*pathRef)->fPointCnt = 0;
300 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
301 (*pathRef)->fGenerationID = 0;
302 (*pathRef)->fConicWeights.rewind();
303 (*pathRef)->fSegmentMask = 0;
304 (*pathRef)->fIsOval = false;
305 (*pathRef)->fIsRRect = false;
306 SkDEBUGCODE((*pathRef)->validate();)
307 } else {
308 int oldVCnt = (*pathRef)->countVerbs();
309 int oldPCnt = (*pathRef)->countPoints();
310 pathRef->reset(new SkPathRef);
311 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
312 }
313 }
314
operator ==(const SkPathRef & ref) const315 bool SkPathRef::operator== (const SkPathRef& ref) const {
316 SkDEBUGCODE(this->validate();)
317 SkDEBUGCODE(ref.validate();)
318
319 // We explicitly check fSegmentMask as a quick-reject. We could skip it,
320 // since it is only a cache of info in the fVerbs, but its a fast way to
321 // notice a difference
322 if (fSegmentMask != ref.fSegmentMask) {
323 return false;
324 }
325
326 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
327 #ifdef SK_RELEASE
328 if (genIDMatch) {
329 return true;
330 }
331 #endif
332 if (fPointCnt != ref.fPointCnt ||
333 fVerbCnt != ref.fVerbCnt) {
334 SkASSERT(!genIDMatch);
335 return false;
336 }
337 if (0 == ref.fVerbCnt) {
338 SkASSERT(0 == ref.fPointCnt);
339 return true;
340 }
341 SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin());
342 if (0 != memcmp(this->verbsMemBegin(),
343 ref.verbsMemBegin(),
344 ref.fVerbCnt * sizeof(uint8_t))) {
345 SkASSERT(!genIDMatch);
346 return false;
347 }
348 SkASSERT(this->points() && ref.points());
349 if (0 != memcmp(this->points(),
350 ref.points(),
351 ref.fPointCnt * sizeof(SkPoint))) {
352 SkASSERT(!genIDMatch);
353 return false;
354 }
355 if (fConicWeights != ref.fConicWeights) {
356 SkASSERT(!genIDMatch);
357 return false;
358 }
359 return true;
360 }
361
writeToBuffer(SkWBuffer * buffer) const362 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
363 SkDEBUGCODE(this->validate();)
364 SkDEBUGCODE(size_t beforePos = buffer->pos();)
365
366 // Call getBounds() to ensure (as a side-effect) that fBounds
367 // and fIsFinite are computed.
368 const SkRect& bounds = this->getBounds();
369
370 int32_t packed = ((fRRectOrOvalStartIdx & 7) << kRRectOrOvalStartIdx_SerializationShift) |
371 ((fRRectOrOvalIsCCW & 1) << kRRectOrOvalIsCCW_SerializationShift) |
372 ((fIsFinite & 1) << kIsFinite_SerializationShift) |
373 ((fIsOval & 1) << kIsOval_SerializationShift) |
374 ((fIsRRect & 1) << kIsRRect_SerializationShift) |
375 (fSegmentMask << kSegmentMask_SerializationShift);
376 buffer->write32(packed);
377
378 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
379 // SkWBuffer. Until this is fixed we write 0.
380 buffer->write32(0);
381 buffer->write32(fVerbCnt);
382 buffer->write32(fPointCnt);
383 buffer->write32(fConicWeights.count());
384 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
385 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
386 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
387 buffer->write(&bounds, sizeof(bounds));
388
389 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
390 }
391
writeSize() const392 uint32_t SkPathRef::writeSize() const {
393 return uint32_t(5 * sizeof(uint32_t) +
394 fVerbCnt * sizeof(uint8_t) +
395 fPointCnt * sizeof(SkPoint) +
396 fConicWeights.bytes() +
397 sizeof(SkRect));
398 }
399
copy(const SkPathRef & ref,int additionalReserveVerbs,int additionalReservePoints)400 void SkPathRef::copy(const SkPathRef& ref,
401 int additionalReserveVerbs,
402 int additionalReservePoints) {
403 SkDEBUGCODE(this->validate();)
404 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
405 additionalReserveVerbs, additionalReservePoints);
406 sk_careful_memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt*sizeof(uint8_t));
407 sk_careful_memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
408 fConicWeights = ref.fConicWeights;
409 fBoundsIsDirty = ref.fBoundsIsDirty;
410 if (!fBoundsIsDirty) {
411 fBounds = ref.fBounds;
412 fIsFinite = ref.fIsFinite;
413 }
414 fSegmentMask = ref.fSegmentMask;
415 fIsOval = ref.fIsOval;
416 fIsRRect = ref.fIsRRect;
417 fRRectOrOvalIsCCW = ref.fRRectOrOvalIsCCW;
418 fRRectOrOvalStartIdx = ref.fRRectOrOvalStartIdx;
419 SkDEBUGCODE(this->validate();)
420 }
421
422
interpolate(const SkPathRef & ending,SkScalar weight,SkPathRef * out) const423 void SkPathRef::interpolate(const SkPathRef& ending, SkScalar weight, SkPathRef* out) const {
424 const SkScalar* inValues = &ending.getPoints()->fX;
425 SkScalar* outValues = &out->getPoints()->fX;
426 int count = out->countPoints() * 2;
427 for (int index = 0; index < count; ++index) {
428 outValues[index] = outValues[index] * weight + inValues[index] * (1 - weight);
429 }
430 out->fBoundsIsDirty = true;
431 out->fIsOval = false;
432 out->fIsRRect = false;
433 }
434
growForRepeatedVerb(int verb,int numVbs,SkScalar ** weights)435 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
436 int numVbs,
437 SkScalar** weights) {
438 // This value is just made-up for now. When count is 4, calling memset was much
439 // slower than just writing the loop. This seems odd, and hopefully in the
440 // future this will appear to have been a fluke...
441 static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
442
443 SkDEBUGCODE(this->validate();)
444 int pCnt;
445 bool dirtyAfterEdit = true;
446 switch (verb) {
447 case SkPath::kMove_Verb:
448 pCnt = numVbs;
449 dirtyAfterEdit = false;
450 break;
451 case SkPath::kLine_Verb:
452 fSegmentMask |= SkPath::kLine_SegmentMask;
453 pCnt = numVbs;
454 break;
455 case SkPath::kQuad_Verb:
456 fSegmentMask |= SkPath::kQuad_SegmentMask;
457 pCnt = 2 * numVbs;
458 break;
459 case SkPath::kConic_Verb:
460 fSegmentMask |= SkPath::kConic_SegmentMask;
461 pCnt = 2 * numVbs;
462 break;
463 case SkPath::kCubic_Verb:
464 fSegmentMask |= SkPath::kCubic_SegmentMask;
465 pCnt = 3 * numVbs;
466 break;
467 case SkPath::kClose_Verb:
468 SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
469 pCnt = 0;
470 dirtyAfterEdit = false;
471 break;
472 case SkPath::kDone_Verb:
473 SkDEBUGFAIL("growForRepeatedVerb called for kDone");
474 // fall through
475 default:
476 SkDEBUGFAIL("default should not be reached");
477 pCnt = 0;
478 dirtyAfterEdit = false;
479 }
480
481 size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
482 this->makeSpace(space);
483
484 SkPoint* ret = fPoints + fPointCnt;
485 uint8_t* vb = fVerbs - fVerbCnt;
486
487 // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
488 // be 0, the compiler will remove the test/branch entirely.
489 if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
490 memset(vb - numVbs, verb, numVbs);
491 } else {
492 for (int i = 0; i < numVbs; ++i) {
493 vb[~i] = verb;
494 }
495 }
496
497 fVerbCnt += numVbs;
498 fPointCnt += pCnt;
499 fFreeSpace -= space;
500 fBoundsIsDirty = true; // this also invalidates fIsFinite
501 if (dirtyAfterEdit) {
502 fIsOval = false;
503 fIsRRect = false;
504 }
505
506 if (SkPath::kConic_Verb == verb) {
507 SkASSERT(weights);
508 *weights = fConicWeights.append(numVbs);
509 }
510
511 SkDEBUGCODE(this->validate();)
512 return ret;
513 }
514
growForVerb(int verb,SkScalar weight)515 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
516 SkDEBUGCODE(this->validate();)
517 int pCnt;
518 bool dirtyAfterEdit = true;
519 switch (verb) {
520 case SkPath::kMove_Verb:
521 pCnt = 1;
522 dirtyAfterEdit = false;
523 break;
524 case SkPath::kLine_Verb:
525 fSegmentMask |= SkPath::kLine_SegmentMask;
526 pCnt = 1;
527 break;
528 case SkPath::kQuad_Verb:
529 fSegmentMask |= SkPath::kQuad_SegmentMask;
530 pCnt = 2;
531 break;
532 case SkPath::kConic_Verb:
533 fSegmentMask |= SkPath::kConic_SegmentMask;
534 pCnt = 2;
535 break;
536 case SkPath::kCubic_Verb:
537 fSegmentMask |= SkPath::kCubic_SegmentMask;
538 pCnt = 3;
539 break;
540 case SkPath::kClose_Verb:
541 pCnt = 0;
542 dirtyAfterEdit = false;
543 break;
544 case SkPath::kDone_Verb:
545 SkDEBUGFAIL("growForVerb called for kDone");
546 // fall through
547 default:
548 SkDEBUGFAIL("default is not reached");
549 dirtyAfterEdit = false;
550 pCnt = 0;
551 }
552 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
553 this->makeSpace(space);
554 this->fVerbs[~fVerbCnt] = verb;
555 SkPoint* ret = fPoints + fPointCnt;
556 fVerbCnt += 1;
557 fPointCnt += pCnt;
558 fFreeSpace -= space;
559 fBoundsIsDirty = true; // this also invalidates fIsFinite
560 if (dirtyAfterEdit) {
561 fIsOval = false;
562 fIsRRect = false;
563 }
564
565 if (SkPath::kConic_Verb == verb) {
566 *fConicWeights.append() = weight;
567 }
568
569 SkDEBUGCODE(this->validate();)
570 return ret;
571 }
572
genID() const573 uint32_t SkPathRef::genID() const {
574 SkASSERT(!fEditorsAttached);
575 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
576 if (!fGenerationID) {
577 if (0 == fPointCnt && 0 == fVerbCnt) {
578 fGenerationID = kEmptyGenID;
579 } else {
580 static int32_t gPathRefGenerationID;
581 // do a loop in case our global wraps around, as we never want to return a 0 or the
582 // empty ID
583 do {
584 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
585 } while (fGenerationID <= kEmptyGenID);
586 }
587 }
588 return fGenerationID;
589 }
590
addGenIDChangeListener(GenIDChangeListener * listener)591 void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) {
592 if (nullptr == listener || this == gEmpty) {
593 delete listener;
594 return;
595 }
596 *fGenIDChangeListeners.append() = listener;
597 }
598
599 // we need to be called *before* the genID gets changed or zerod
callGenIDChangeListeners()600 void SkPathRef::callGenIDChangeListeners() {
601 for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
602 fGenIDChangeListeners[i]->onChange();
603 }
604
605 // Listeners get at most one shot, so whether these triggered or not, blow them away.
606 fGenIDChangeListeners.deleteAll();
607 }
608
getRRect() const609 SkRRect SkPathRef::getRRect() const {
610 const SkRect& bounds = this->getBounds();
611 SkVector radii[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
612 Iter iter(*this);
613 SkPoint pts[4];
614 uint8_t verb = iter.next(pts);
615 SkASSERT(SkPath::kMove_Verb == verb);
616 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
617 if (SkPath::kConic_Verb == verb) {
618 SkVector v1_0 = pts[1] - pts[0];
619 SkVector v2_1 = pts[2] - pts[1];
620 SkVector dxdy;
621 if (v1_0.fX) {
622 SkASSERT(!v2_1.fX && !v1_0.fY);
623 dxdy.set(SkScalarAbs(v1_0.fX), SkScalarAbs(v2_1.fY));
624 } else if (!v1_0.fY) {
625 SkASSERT(!v2_1.fX || !v2_1.fY);
626 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v2_1.fY));
627 } else {
628 SkASSERT(!v2_1.fY);
629 dxdy.set(SkScalarAbs(v2_1.fX), SkScalarAbs(v1_0.fY));
630 }
631 SkRRect::Corner corner =
632 pts[1].fX == bounds.fLeft ?
633 pts[1].fY == bounds.fTop ?
634 SkRRect::kUpperLeft_Corner : SkRRect::kLowerLeft_Corner :
635 pts[1].fY == bounds.fTop ?
636 SkRRect::kUpperRight_Corner : SkRRect::kLowerRight_Corner;
637 SkASSERT(!radii[corner].fX && !radii[corner].fY);
638 radii[corner] = dxdy;
639 } else {
640 SkASSERT((verb == SkPath::kLine_Verb
641 && (!(pts[1].fX - pts[0].fX) || !(pts[1].fY - pts[0].fY)))
642 || verb == SkPath::kClose_Verb);
643 }
644 }
645 SkRRect rrect;
646 rrect.setRectRadii(bounds, radii);
647 return rrect;
648 }
649
650 ///////////////////////////////////////////////////////////////////////////////
651
Iter()652 SkPathRef::Iter::Iter() {
653 #ifdef SK_DEBUG
654 fPts = nullptr;
655 fConicWeights = nullptr;
656 #endif
657 // need to init enough to make next() harmlessly return kDone_Verb
658 fVerbs = nullptr;
659 fVerbStop = nullptr;
660 }
661
Iter(const SkPathRef & path)662 SkPathRef::Iter::Iter(const SkPathRef& path) {
663 this->setPathRef(path);
664 }
665
setPathRef(const SkPathRef & path)666 void SkPathRef::Iter::setPathRef(const SkPathRef& path) {
667 fPts = path.points();
668 fVerbs = path.verbs();
669 fVerbStop = path.verbsMemBegin();
670 fConicWeights = path.conicWeights();
671 if (fConicWeights) {
672 fConicWeights -= 1; // begin one behind
673 }
674 }
675
next(SkPoint pts[4])676 uint8_t SkPathRef::Iter::next(SkPoint pts[4]) {
677 SkASSERT(pts);
678 if (fVerbs == fVerbStop) {
679 return (uint8_t) SkPath::kDone_Verb;
680 }
681
682 // fVerbs points one beyond next verb so decrement first.
683 unsigned verb = *(--fVerbs);
684 const SkPoint* srcPts = fPts;
685
686 switch (verb) {
687 case SkPath::kMove_Verb:
688 pts[0] = srcPts[0];
689 srcPts += 1;
690 break;
691 case SkPath::kLine_Verb:
692 pts[0] = srcPts[-1];
693 pts[1] = srcPts[0];
694 srcPts += 1;
695 break;
696 case SkPath::kConic_Verb:
697 fConicWeights += 1;
698 // fall-through
699 case SkPath::kQuad_Verb:
700 pts[0] = srcPts[-1];
701 pts[1] = srcPts[0];
702 pts[2] = srcPts[1];
703 srcPts += 2;
704 break;
705 case SkPath::kCubic_Verb:
706 pts[0] = srcPts[-1];
707 pts[1] = srcPts[0];
708 pts[2] = srcPts[1];
709 pts[3] = srcPts[2];
710 srcPts += 3;
711 break;
712 case SkPath::kClose_Verb:
713 break;
714 case SkPath::kDone_Verb:
715 SkASSERT(fVerbs == fVerbStop);
716 break;
717 }
718 fPts = srcPts;
719 return (uint8_t) verb;
720 }
721
peek() const722 uint8_t SkPathRef::Iter::peek() const {
723 const uint8_t* next = fVerbs - 1;
724 return next <= fVerbStop ? (uint8_t) SkPath::kDone_Verb : *next;
725 }
726
727 #ifdef SK_DEBUG
728
729 #include "SkNx.h"
730
validate() const731 void SkPathRef::validate() const {
732 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
733 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
734 SkASSERT((nullptr == fPoints) == (nullptr == fVerbs));
735 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace));
736 SkASSERT(!(nullptr == fPoints && 0 != fFreeSpace));
737 SkASSERT(!(nullptr == fPoints && fPointCnt));
738 SkASSERT(!(nullptr == fVerbs && fVerbCnt));
739 SkASSERT(this->currSize() ==
740 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
741
742 if (fIsOval || fIsRRect) {
743 // Currently we don't allow both of these to be set, even though ovals are round rects.
744 SkASSERT(fIsOval != fIsRRect);
745 if (fIsOval) {
746 SkASSERT(fRRectOrOvalStartIdx < 4);
747 } else {
748 SkASSERT(fRRectOrOvalStartIdx < 8);
749 }
750 }
751
752 if (!fBoundsIsDirty && !fBounds.isEmpty()) {
753 bool isFinite = true;
754 Sk2s leftTop = Sk2s(fBounds.fLeft, fBounds.fTop);
755 Sk2s rightBot = Sk2s(fBounds.fRight, fBounds.fBottom);
756 for (int i = 0; i < fPointCnt; ++i) {
757 Sk2s point = Sk2s(fPoints[i].fX, fPoints[i].fY);
758 #ifdef SK_DEBUG
759 if (fPoints[i].isFinite() &&
760 ((point < leftTop).anyTrue() || (point > rightBot).anyTrue())) {
761 SkDebugf("bounds: %f %f %f %f\n",
762 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
763 for (int j = 0; j < fPointCnt; ++j) {
764 if (i == j) {
765 SkDebugf("*");
766 }
767 SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY);
768 }
769 }
770 #endif
771
772 SkASSERT(!fPoints[i].isFinite() ||
773 (!(point < leftTop).anyTrue() && !(point > rightBot).anyTrue()));
774 if (!fPoints[i].isFinite()) {
775 isFinite = false;
776 }
777 }
778 SkASSERT(SkToBool(fIsFinite) == isFinite);
779 }
780
781 #ifdef SK_DEBUG_PATH
782 uint32_t mask = 0;
783 for (int i = 0; i < fVerbCnt; ++i) {
784 switch (fVerbs[~i]) {
785 case SkPath::kMove_Verb:
786 break;
787 case SkPath::kLine_Verb:
788 mask |= SkPath::kLine_SegmentMask;
789 break;
790 case SkPath::kQuad_Verb:
791 mask |= SkPath::kQuad_SegmentMask;
792 break;
793 case SkPath::kConic_Verb:
794 mask |= SkPath::kConic_SegmentMask;
795 break;
796 case SkPath::kCubic_Verb:
797 mask |= SkPath::kCubic_SegmentMask;
798 break;
799 case SkPath::kClose_Verb:
800 break;
801 case SkPath::kDone_Verb:
802 SkDEBUGFAIL("Done verb shouldn't be recorded.");
803 break;
804 default:
805 SkDEBUGFAIL("Unknown Verb");
806 break;
807 }
808 }
809 SkASSERT(mask == fSegmentMask);
810 #endif // SK_DEBUG_PATH
811 }
812 #endif
813