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