blob: 44838f924637ff144a83aa86ac3aaf90f89d76fa [file] [log] [blame]
robertphillips@google.comca0c8382013-09-26 12:18:23 +00001/*
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"
mtklein78358bf2014-06-02 08:44:27 -07009#include "SkLazyPtr.h"
robertphillips@google.comca0c8382013-09-26 12:18:23 +000010#include "SkPath.h"
11#include "SkPathRef.h"
12
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000013//////////////////////////////////////////////////////////////////////////////
14SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
15 int incReserveVerbs,
16 int incReservePoints)
17{
18 if ((*pathRef)->unique()) {
19 (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
20 } else {
21 SkPathRef* copy = SkNEW(SkPathRef);
22 copy->copy(**pathRef, incReserveVerbs, incReservePoints);
23 pathRef->reset(copy);
24 }
25 fPathRef = *pathRef;
26 fPathRef->fGenerationID = 0;
27 SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000028}
29
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000030//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000031
mtklein148ec592014-10-13 13:17:56 -070032// As a template argument, this must have external linkage.
33SkPathRef* sk_create_empty_pathref() {
mtklein51936a32014-09-29 05:27:59 -070034 SkPathRef* empty = SkNEW(SkPathRef);
35 empty->computeBounds(); // Avoids races later to be the first to do this.
36 return empty;
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000037}
38
mtklein148ec592014-10-13 13:17:56 -070039SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, sk_create_empty_pathref);
40
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000041SkPathRef* SkPathRef::CreateEmpty() {
mtklein78358bf2014-06-02 08:44:27 -070042 return SkRef(empty.get());
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000043}
44
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000045void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
46 const SkPathRef& src,
47 const SkMatrix& matrix) {
robertphillips@google.com03087072013-10-02 16:42:21 +000048 SkDEBUGCODE(src.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000049 if (matrix.isIdentity()) {
50 if (*dst != &src) {
51 src.ref();
52 dst->reset(const_cast<SkPathRef*>(&src));
robertphillips@google.com03087072013-10-02 16:42:21 +000053 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000054 }
55 return;
56 }
57
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000058 if (!(*dst)->unique()) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000059 dst->reset(SkNEW(SkPathRef));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000060 }
61
62 if (*dst != &src) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000063 (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
64 memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
65 (*dst)->fConicWeights = src.fConicWeights;
66 }
67
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000068 SkASSERT((*dst)->countPoints() == src.countPoints());
69 SkASSERT((*dst)->countVerbs() == src.countVerbs());
70 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
71
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000072 // Need to check this here in case (&src == dst)
73 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
74
75 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
76
77 /*
78 * Here we optimize the bounds computation, by noting if the bounds are
79 * already known, and if so, we just transform those as well and mark
80 * them as "known", rather than force the transformed path to have to
81 * recompute them.
82 *
83 * Special gotchas if the path is effectively empty (<= 1 point) or
84 * if it is non-finite. In those cases bounds need to stay empty,
85 * regardless of the matrix.
86 */
87 if (canXformBounds) {
88 (*dst)->fBoundsIsDirty = false;
89 if (src.fIsFinite) {
mtklein5c9c9be2014-12-01 06:59:54 -080090 matrix.mapRect(&(*dst)->fBounds, src.fBounds);
91 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
92 (*dst)->fBounds.setEmpty();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000093 }
94 } else {
95 (*dst)->fIsFinite = false;
mtklein5c9c9be2014-12-01 06:59:54 -080096 (*dst)->fBounds.setEmpty();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000097 }
98 } else {
99 (*dst)->fBoundsIsDirty = true;
100 }
101
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000102 (*dst)->fSegmentMask = src.fSegmentMask;
103
robertphillips@google.com466310d2013-12-03 16:43:54 +0000104 // It's an oval only if it stays a rect.
105 (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
106
robertphillips@google.com03087072013-10-02 16:42:21 +0000107 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000108}
109
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000110SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000111 SkPathRef* ref = SkNEW(SkPathRef);
robertphillips@google.com466310d2013-12-03 16:43:54 +0000112 bool isOval;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000113 uint8_t segmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000114
115 int32_t packed;
116 if (!buffer->readS32(&packed)) {
117 SkDELETE(ref);
118 return NULL;
119 }
120
121 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000122 segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
123 isOval = (packed >> kIsOval_SerializationShift) & 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000124
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000125 int32_t verbCount, pointCount, conicCount;
126 if (!buffer->readU32(&(ref->fGenerationID)) ||
127 !buffer->readS32(&verbCount) ||
128 !buffer->readS32(&pointCount) ||
129 !buffer->readS32(&conicCount)) {
130 SkDELETE(ref);
131 return NULL;
132 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000133
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000134 ref->resetToSize(verbCount, pointCount, conicCount);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000135 SkASSERT(verbCount == ref->countVerbs());
136 SkASSERT(pointCount == ref->countPoints());
137 SkASSERT(conicCount == ref->fConicWeights.count());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000138
139 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
140 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
141 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
142 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
143 SkDELETE(ref);
144 return NULL;
145 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000146 ref->fBoundsIsDirty = false;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000147
148 // resetToSize clears fSegmentMask and fIsOval
149 ref->fSegmentMask = segmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000150 ref->fIsOval = isOval;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000151 return ref;
152}
153
154void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
155 if ((*pathRef)->unique()) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000156 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000157 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
158 (*pathRef)->fVerbCnt = 0;
159 (*pathRef)->fPointCnt = 0;
160 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
161 (*pathRef)->fGenerationID = 0;
162 (*pathRef)->fConicWeights.rewind();
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000163 (*pathRef)->fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000164 (*pathRef)->fIsOval = false;
robertphillips@google.com03087072013-10-02 16:42:21 +0000165 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000166 } else {
167 int oldVCnt = (*pathRef)->countVerbs();
168 int oldPCnt = (*pathRef)->countPoints();
169 pathRef->reset(SkNEW(SkPathRef));
170 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
171 }
172}
173
174bool SkPathRef::operator== (const SkPathRef& ref) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000175 SkDEBUGCODE(this->validate();)
176 SkDEBUGCODE(ref.validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000177
178 // We explicitly check fSegmentMask as a quick-reject. We could skip it,
179 // since it is only a cache of info in the fVerbs, but its a fast way to
180 // notice a difference
181 if (fSegmentMask != ref.fSegmentMask) {
182 return false;
183 }
184
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000185 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
186#ifdef SK_RELEASE
187 if (genIDMatch) {
188 return true;
189 }
190#endif
191 if (fPointCnt != ref.fPointCnt ||
192 fVerbCnt != ref.fVerbCnt) {
193 SkASSERT(!genIDMatch);
194 return false;
195 }
mtkleind4897592014-11-14 09:22:40 -0800196 if (0 == ref.fVerbCnt) {
197 SkASSERT(0 == ref.fPointCnt);
198 return true;
199 }
200 SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin());
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000201 if (0 != memcmp(this->verbsMemBegin(),
202 ref.verbsMemBegin(),
203 ref.fVerbCnt * sizeof(uint8_t))) {
204 SkASSERT(!genIDMatch);
205 return false;
206 }
mtkleind4897592014-11-14 09:22:40 -0800207 SkASSERT(this->points() && ref.points());
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000208 if (0 != memcmp(this->points(),
209 ref.points(),
210 ref.fPointCnt * sizeof(SkPoint))) {
211 SkASSERT(!genIDMatch);
212 return false;
213 }
214 if (fConicWeights != ref.fConicWeights) {
215 SkASSERT(!genIDMatch);
216 return false;
217 }
218 // We've done the work to determine that these are equal. If either has a zero genID, copy
219 // the other's. If both are 0 then genID() will compute the next ID.
220 if (0 == fGenerationID) {
221 fGenerationID = ref.genID();
222 } else if (0 == ref.fGenerationID) {
223 ref.fGenerationID = this->genID();
224 }
225 return true;
226}
227
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000228void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000229 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000230 SkDEBUGCODE(size_t beforePos = buffer->pos();)
231
232 // Call getBounds() to ensure (as a side-effect) that fBounds
233 // and fIsFinite are computed.
234 const SkRect& bounds = this->getBounds();
235
robertphillips@google.com466310d2013-12-03 16:43:54 +0000236 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000237 ((fIsOval & 1) << kIsOval_SerializationShift) |
238 (fSegmentMask << kSegmentMask_SerializationShift);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000239 buffer->write32(packed);
240
241 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
242 // SkWBuffer. Until this is fixed we write 0.
243 buffer->write32(0);
244 buffer->write32(fVerbCnt);
245 buffer->write32(fPointCnt);
246 buffer->write32(fConicWeights.count());
247 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
248 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
249 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
250 buffer->write(&bounds, sizeof(bounds));
251
252 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
253}
254
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000255uint32_t SkPathRef::writeSize() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000256 return uint32_t(5 * sizeof(uint32_t) +
257 fVerbCnt * sizeof(uint8_t) +
258 fPointCnt * sizeof(SkPoint) +
259 fConicWeights.bytes() +
260 sizeof(SkRect));
261}
262
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000263void SkPathRef::copy(const SkPathRef& ref,
264 int additionalReserveVerbs,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000265 int additionalReservePoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000266 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000267 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
268 additionalReserveVerbs, additionalReservePoints);
269 memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
270 memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
271 fConicWeights = ref.fConicWeights;
272 // We could call genID() here to force a real ID (instead of 0). However, if we're making
273 // a copy then presumably we intend to make a modification immediately afterwards.
274 fGenerationID = ref.fGenerationID;
275 fBoundsIsDirty = ref.fBoundsIsDirty;
276 if (!fBoundsIsDirty) {
277 fBounds = ref.fBounds;
278 fIsFinite = ref.fIsFinite;
279 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000280 fSegmentMask = ref.fSegmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000281 fIsOval = ref.fIsOval;
robertphillips@google.com03087072013-10-02 16:42:21 +0000282 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000283}
284
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000285SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
286 int numVbs,
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000287 SkScalar** weights) {
288 // This value is just made-up for now. When count is 4, calling memset was much
289 // slower than just writing the loop. This seems odd, and hopefully in the
290 // future this will appear to have been a fluke...
291 static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
292
293 SkDEBUGCODE(this->validate();)
294 int pCnt;
295 bool dirtyAfterEdit = true;
296 switch (verb) {
297 case SkPath::kMove_Verb:
298 pCnt = numVbs;
299 dirtyAfterEdit = false;
300 break;
301 case SkPath::kLine_Verb:
302 fSegmentMask |= SkPath::kLine_SegmentMask;
303 pCnt = numVbs;
304 break;
305 case SkPath::kQuad_Verb:
306 fSegmentMask |= SkPath::kQuad_SegmentMask;
307 pCnt = 2 * numVbs;
308 break;
309 case SkPath::kConic_Verb:
310 fSegmentMask |= SkPath::kConic_SegmentMask;
311 pCnt = 2 * numVbs;
312 break;
313 case SkPath::kCubic_Verb:
314 fSegmentMask |= SkPath::kCubic_SegmentMask;
315 pCnt = 3 * numVbs;
316 break;
317 case SkPath::kClose_Verb:
318 SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
319 pCnt = 0;
320 dirtyAfterEdit = false;
321 break;
322 case SkPath::kDone_Verb:
323 SkDEBUGFAIL("growForRepeatedVerb called for kDone");
324 // fall through
325 default:
326 SkDEBUGFAIL("default should not be reached");
327 pCnt = 0;
328 dirtyAfterEdit = false;
329 }
330
331 size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
332 this->makeSpace(space);
333
334 SkPoint* ret = fPoints + fPointCnt;
335 uint8_t* vb = fVerbs - fVerbCnt;
336
337 // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
338 // be 0, the compiler will remove the test/branch entirely.
339 if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
340 memset(vb - numVbs, verb, numVbs);
341 } else {
342 for (int i = 0; i < numVbs; ++i) {
343 vb[~i] = verb;
344 }
345 }
346
347 fVerbCnt += numVbs;
348 fPointCnt += pCnt;
349 fFreeSpace -= space;
350 fBoundsIsDirty = true; // this also invalidates fIsFinite
351 if (dirtyAfterEdit) {
352 fIsOval = false;
353 }
354
355 if (SkPath::kConic_Verb == verb) {
bsalomon49f085d2014-09-05 13:34:00 -0700356 SkASSERT(weights);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000357 *weights = fConicWeights.append(numVbs);
358 }
359
360 SkDEBUGCODE(this->validate();)
361 return ret;
362}
363
364SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000365 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000366 int pCnt;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000367 bool dirtyAfterEdit = true;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000368 switch (verb) {
369 case SkPath::kMove_Verb:
370 pCnt = 1;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000371 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000372 break;
373 case SkPath::kLine_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000374 fSegmentMask |= SkPath::kLine_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000375 pCnt = 1;
376 break;
377 case SkPath::kQuad_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000378 fSegmentMask |= SkPath::kQuad_SegmentMask;
379 pCnt = 2;
380 break;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000381 case SkPath::kConic_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000382 fSegmentMask |= SkPath::kConic_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000383 pCnt = 2;
384 break;
385 case SkPath::kCubic_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000386 fSegmentMask |= SkPath::kCubic_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000387 pCnt = 3;
388 break;
389 case SkPath::kClose_Verb:
390 pCnt = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000391 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000392 break;
393 case SkPath::kDone_Verb:
394 SkDEBUGFAIL("growForVerb called for kDone");
395 // fall through
396 default:
397 SkDEBUGFAIL("default is not reached");
robertphillips@google.com466310d2013-12-03 16:43:54 +0000398 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000399 pCnt = 0;
400 }
401 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
402 this->makeSpace(space);
403 this->fVerbs[~fVerbCnt] = verb;
404 SkPoint* ret = fPoints + fPointCnt;
405 fVerbCnt += 1;
406 fPointCnt += pCnt;
407 fFreeSpace -= space;
408 fBoundsIsDirty = true; // this also invalidates fIsFinite
robertphillips@google.com466310d2013-12-03 16:43:54 +0000409 if (dirtyAfterEdit) {
410 fIsOval = false;
411 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000412
413 if (SkPath::kConic_Verb == verb) {
414 *fConicWeights.append() = weight;
415 }
416
robertphillips@google.com03087072013-10-02 16:42:21 +0000417 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000418 return ret;
419}
420
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000421uint32_t SkPathRef::genID() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000422 SkASSERT(!fEditorsAttached);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000423 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000424 if (!fGenerationID) {
425 if (0 == fPointCnt && 0 == fVerbCnt) {
426 fGenerationID = kEmptyGenID;
427 } else {
428 static int32_t gPathRefGenerationID;
429 // do a loop in case our global wraps around, as we never want to return a 0 or the
430 // empty ID
431 do {
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000432 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000433 } while (fGenerationID <= kEmptyGenID);
434 }
435 }
436 return fGenerationID;
437}
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000438
robertphillips@google.com03087072013-10-02 16:42:21 +0000439#ifdef SK_DEBUG
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000440void SkPathRef::validate() const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000441 this->INHERITED::validate();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000442 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
443 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
444 SkASSERT((NULL == fPoints) == (NULL == fVerbs));
445 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
446 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
447 SkASSERT(!(NULL == fPoints && fPointCnt));
448 SkASSERT(!(NULL == fVerbs && fVerbCnt));
449 SkASSERT(this->currSize() ==
450 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000451
mtklein5c9c9be2014-12-01 06:59:54 -0800452 if (!fBoundsIsDirty && !fBounds.isEmpty()) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000453 bool isFinite = true;
454 for (int i = 0; i < fPointCnt; ++i) {
robertphillipsf1cdead2015-01-05 09:20:04 -0800455#ifdef SK_DEBUG
456 if (fPoints[i].isFinite() &&
457 (fPoints[i].fX < fBounds.fLeft || fPoints[i].fX > fBounds.fRight ||
458 fPoints[i].fY < fBounds.fTop || fPoints[i].fY > fBounds.fBottom)) {
459 SkDebugf("bounds: %f %f %f %f\n",
460 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
461 for (int j = 0; j < fPointCnt; ++j) {
462 if (i == j) {
463 SkDebugf("*");
464 }
465 SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY);
466 }
467 }
468#endif
469
robertphillips0e912462014-12-12 12:47:59 -0800470 SkASSERT(!fPoints[i].isFinite() ||
471 (fPoints[i].fX >= fBounds.fLeft && fPoints[i].fX <= fBounds.fRight &&
472 fPoints[i].fY >= fBounds.fTop && fPoints[i].fY <= fBounds.fBottom));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000473 if (!fPoints[i].isFinite()) {
474 isFinite = false;
475 }
476 }
477 SkASSERT(SkToBool(fIsFinite) == isFinite);
478 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000479
480#ifdef SK_DEBUG_PATH
481 uint32_t mask = 0;
482 for (int i = 0; i < fVerbCnt; ++i) {
483 switch (fVerbs[~i]) {
484 case SkPath::kMove_Verb:
485 break;
486 case SkPath::kLine_Verb:
487 mask |= SkPath::kLine_SegmentMask;
488 break;
489 case SkPath::kQuad_Verb:
490 mask |= SkPath::kQuad_SegmentMask;
491 break;
492 case SkPath::kConic_Verb:
493 mask |= SkPath::kConic_SegmentMask;
494 break;
495 case SkPath::kCubic_Verb:
496 mask |= SkPath::kCubic_SegmentMask;
497 break;
498 case SkPath::kClose_Verb:
499 break;
500 case SkPath::kDone_Verb:
501 SkDEBUGFAIL("Done verb shouldn't be recorded.");
502 break;
503 default:
504 SkDEBUGFAIL("Unknown Verb");
505 break;
506 }
507 }
508 SkASSERT(mask == fSegmentMask);
509#endif // SK_DEBUG_PATH
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000510}
robertphillips@google.com03087072013-10-02 16:42:21 +0000511#endif