blob: e3317a6e7517bfd0ae99824b5f55558f9ad6b7f4 [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 {
halcanary385fe4d2015-08-26 13:07:48 -070021 SkPathRef* copy = new SkPathRef;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000022 copy->copy(**pathRef, incReserveVerbs, incReservePoints);
23 pathRef->reset(copy);
24 }
25 fPathRef = *pathRef;
senorblanco84cd6212015-08-04 10:01:58 -070026 fPathRef->callGenIDChangeListeners();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000027 fPathRef->fGenerationID = 0;
28 SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
robertphillips@google.comca0c8382013-09-26 12:18:23 +000029}
30
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000031//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org709ca752014-01-24 22:38:39 +000032
senorblanco84cd6212015-08-04 10:01:58 -070033SkPathRef::~SkPathRef() {
34 this->callGenIDChangeListeners();
35 SkDEBUGCODE(this->validate();)
36 sk_free(fPoints);
37
38 SkDEBUGCODE(fPoints = NULL;)
39 SkDEBUGCODE(fVerbs = NULL;)
40 SkDEBUGCODE(fVerbCnt = 0x9999999;)
41 SkDEBUGCODE(fPointCnt = 0xAAAAAAA;)
42 SkDEBUGCODE(fPointCnt = 0xBBBBBBB;)
43 SkDEBUGCODE(fGenerationID = 0xEEEEEEEE;)
44 SkDEBUGCODE(fEditorsAttached = 0x7777777;)
45}
46
mtklein148ec592014-10-13 13:17:56 -070047// As a template argument, this must have external linkage.
48SkPathRef* sk_create_empty_pathref() {
halcanary385fe4d2015-08-26 13:07:48 -070049 SkPathRef* empty = new SkPathRef;
mtklein51936a32014-09-29 05:27:59 -070050 empty->computeBounds(); // Avoids races later to be the first to do this.
51 return empty;
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000052}
53
mtklein148ec592014-10-13 13:17:56 -070054SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, sk_create_empty_pathref);
55
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000056SkPathRef* SkPathRef::CreateEmpty() {
mtklein78358bf2014-06-02 08:44:27 -070057 return SkRef(empty.get());
commit-bot@chromium.org1f81fd62013-10-23 14:44:08 +000058}
59
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000060void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
61 const SkPathRef& src,
62 const SkMatrix& matrix) {
robertphillips@google.com03087072013-10-02 16:42:21 +000063 SkDEBUGCODE(src.validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000064 if (matrix.isIdentity()) {
65 if (*dst != &src) {
66 src.ref();
67 dst->reset(const_cast<SkPathRef*>(&src));
robertphillips@google.com03087072013-10-02 16:42:21 +000068 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000069 }
70 return;
71 }
72
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000073 if (!(*dst)->unique()) {
halcanary385fe4d2015-08-26 13:07:48 -070074 dst->reset(new SkPathRef);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000075 }
76
77 if (*dst != &src) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000078 (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
79 memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
80 (*dst)->fConicWeights = src.fConicWeights;
81 }
82
robertphillips@google.comb06e88d2013-12-03 17:15:36 +000083 SkASSERT((*dst)->countPoints() == src.countPoints());
84 SkASSERT((*dst)->countVerbs() == src.countVerbs());
85 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
86
robertphillips@google.com3e292aa2013-09-27 17:48:49 +000087 // Need to check this here in case (&src == dst)
88 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
89
90 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
91
92 /*
93 * Here we optimize the bounds computation, by noting if the bounds are
94 * already known, and if so, we just transform those as well and mark
95 * them as "known", rather than force the transformed path to have to
96 * recompute them.
97 *
98 * Special gotchas if the path is effectively empty (<= 1 point) or
99 * if it is non-finite. In those cases bounds need to stay empty,
100 * regardless of the matrix.
101 */
102 if (canXformBounds) {
103 (*dst)->fBoundsIsDirty = false;
104 if (src.fIsFinite) {
mtklein5c9c9be2014-12-01 06:59:54 -0800105 matrix.mapRect(&(*dst)->fBounds, src.fBounds);
106 if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
107 (*dst)->fBounds.setEmpty();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000108 }
109 } else {
110 (*dst)->fIsFinite = false;
mtklein5c9c9be2014-12-01 06:59:54 -0800111 (*dst)->fBounds.setEmpty();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000112 }
113 } else {
114 (*dst)->fBoundsIsDirty = true;
115 }
116
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000117 (*dst)->fSegmentMask = src.fSegmentMask;
118
robertphillips@google.com466310d2013-12-03 16:43:54 +0000119 // It's an oval only if it stays a rect.
120 (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
121
robertphillips@google.com03087072013-10-02 16:42:21 +0000122 SkDEBUGCODE((*dst)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000123}
124
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000125SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
halcanary385fe4d2015-08-26 13:07:48 -0700126 SkPathRef* ref = new SkPathRef;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000127 bool isOval;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000128 uint8_t segmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000129
130 int32_t packed;
131 if (!buffer->readS32(&packed)) {
halcanary385fe4d2015-08-26 13:07:48 -0700132 delete ref;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000133 return NULL;
134 }
135
136 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000137 segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
138 isOval = (packed >> kIsOval_SerializationShift) & 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000139
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000140 int32_t verbCount, pointCount, conicCount;
141 if (!buffer->readU32(&(ref->fGenerationID)) ||
142 !buffer->readS32(&verbCount) ||
143 !buffer->readS32(&pointCount) ||
144 !buffer->readS32(&conicCount)) {
halcanary385fe4d2015-08-26 13:07:48 -0700145 delete ref;
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000146 return NULL;
147 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000148
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000149 ref->resetToSize(verbCount, pointCount, conicCount);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000150 SkASSERT(verbCount == ref->countVerbs());
151 SkASSERT(pointCount == ref->countPoints());
152 SkASSERT(conicCount == ref->fConicWeights.count());
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000153
154 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
155 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
156 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
157 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
halcanary385fe4d2015-08-26 13:07:48 -0700158 delete ref;
commit-bot@chromium.org8f457e32013-11-08 19:22:57 +0000159 return NULL;
160 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000161 ref->fBoundsIsDirty = false;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000162
163 // resetToSize clears fSegmentMask and fIsOval
164 ref->fSegmentMask = segmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000165 ref->fIsOval = isOval;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000166 return ref;
167}
168
169void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
170 if ((*pathRef)->unique()) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000171 SkDEBUGCODE((*pathRef)->validate();)
senorblanco84cd6212015-08-04 10:01:58 -0700172 (*pathRef)->callGenIDChangeListeners();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000173 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
174 (*pathRef)->fVerbCnt = 0;
175 (*pathRef)->fPointCnt = 0;
176 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
177 (*pathRef)->fGenerationID = 0;
178 (*pathRef)->fConicWeights.rewind();
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000179 (*pathRef)->fSegmentMask = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000180 (*pathRef)->fIsOval = false;
robertphillips@google.com03087072013-10-02 16:42:21 +0000181 SkDEBUGCODE((*pathRef)->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000182 } else {
183 int oldVCnt = (*pathRef)->countVerbs();
184 int oldPCnt = (*pathRef)->countPoints();
halcanary385fe4d2015-08-26 13:07:48 -0700185 pathRef->reset(new SkPathRef);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000186 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
187 }
188}
189
190bool SkPathRef::operator== (const SkPathRef& ref) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000191 SkDEBUGCODE(this->validate();)
192 SkDEBUGCODE(ref.validate();)
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000193
194 // We explicitly check fSegmentMask as a quick-reject. We could skip it,
195 // since it is only a cache of info in the fVerbs, but its a fast way to
196 // notice a difference
197 if (fSegmentMask != ref.fSegmentMask) {
198 return false;
199 }
200
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000201 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
202#ifdef SK_RELEASE
203 if (genIDMatch) {
204 return true;
205 }
206#endif
207 if (fPointCnt != ref.fPointCnt ||
208 fVerbCnt != ref.fVerbCnt) {
209 SkASSERT(!genIDMatch);
210 return false;
211 }
mtkleind4897592014-11-14 09:22:40 -0800212 if (0 == ref.fVerbCnt) {
213 SkASSERT(0 == ref.fPointCnt);
214 return true;
215 }
216 SkASSERT(this->verbsMemBegin() && ref.verbsMemBegin());
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000217 if (0 != memcmp(this->verbsMemBegin(),
218 ref.verbsMemBegin(),
219 ref.fVerbCnt * sizeof(uint8_t))) {
220 SkASSERT(!genIDMatch);
221 return false;
222 }
mtkleind4897592014-11-14 09:22:40 -0800223 SkASSERT(this->points() && ref.points());
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000224 if (0 != memcmp(this->points(),
225 ref.points(),
226 ref.fPointCnt * sizeof(SkPoint))) {
227 SkASSERT(!genIDMatch);
228 return false;
229 }
230 if (fConicWeights != ref.fConicWeights) {
231 SkASSERT(!genIDMatch);
232 return false;
233 }
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000234 return true;
235}
236
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000237void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000238 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000239 SkDEBUGCODE(size_t beforePos = buffer->pos();)
240
241 // Call getBounds() to ensure (as a side-effect) that fBounds
242 // and fIsFinite are computed.
243 const SkRect& bounds = this->getBounds();
244
robertphillips@google.com466310d2013-12-03 16:43:54 +0000245 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000246 ((fIsOval & 1) << kIsOval_SerializationShift) |
247 (fSegmentMask << kSegmentMask_SerializationShift);
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000248 buffer->write32(packed);
249
250 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
251 // SkWBuffer. Until this is fixed we write 0.
252 buffer->write32(0);
253 buffer->write32(fVerbCnt);
254 buffer->write32(fPointCnt);
255 buffer->write32(fConicWeights.count());
256 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
257 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
258 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
259 buffer->write(&bounds, sizeof(bounds));
260
261 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
262}
263
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000264uint32_t SkPathRef::writeSize() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000265 return uint32_t(5 * sizeof(uint32_t) +
266 fVerbCnt * sizeof(uint8_t) +
267 fPointCnt * sizeof(SkPoint) +
268 fConicWeights.bytes() +
269 sizeof(SkRect));
270}
271
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000272void SkPathRef::copy(const SkPathRef& ref,
273 int additionalReserveVerbs,
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000274 int additionalReservePoints) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000275 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000276 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
277 additionalReserveVerbs, additionalReservePoints);
278 memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
279 memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
280 fConicWeights = ref.fConicWeights;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000281 fBoundsIsDirty = ref.fBoundsIsDirty;
282 if (!fBoundsIsDirty) {
283 fBounds = ref.fBounds;
284 fIsFinite = ref.fIsFinite;
285 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000286 fSegmentMask = ref.fSegmentMask;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000287 fIsOval = ref.fIsOval;
robertphillips@google.com03087072013-10-02 16:42:21 +0000288 SkDEBUGCODE(this->validate();)
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000289}
290
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +0000291SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
292 int numVbs,
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000293 SkScalar** weights) {
294 // This value is just made-up for now. When count is 4, calling memset was much
295 // slower than just writing the loop. This seems odd, and hopefully in the
296 // future this will appear to have been a fluke...
297 static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
298
299 SkDEBUGCODE(this->validate();)
300 int pCnt;
301 bool dirtyAfterEdit = true;
302 switch (verb) {
303 case SkPath::kMove_Verb:
304 pCnt = numVbs;
305 dirtyAfterEdit = false;
306 break;
307 case SkPath::kLine_Verb:
308 fSegmentMask |= SkPath::kLine_SegmentMask;
309 pCnt = numVbs;
310 break;
311 case SkPath::kQuad_Verb:
312 fSegmentMask |= SkPath::kQuad_SegmentMask;
313 pCnt = 2 * numVbs;
314 break;
315 case SkPath::kConic_Verb:
316 fSegmentMask |= SkPath::kConic_SegmentMask;
317 pCnt = 2 * numVbs;
318 break;
319 case SkPath::kCubic_Verb:
320 fSegmentMask |= SkPath::kCubic_SegmentMask;
321 pCnt = 3 * numVbs;
322 break;
323 case SkPath::kClose_Verb:
324 SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
325 pCnt = 0;
326 dirtyAfterEdit = false;
327 break;
328 case SkPath::kDone_Verb:
329 SkDEBUGFAIL("growForRepeatedVerb called for kDone");
330 // fall through
331 default:
332 SkDEBUGFAIL("default should not be reached");
333 pCnt = 0;
334 dirtyAfterEdit = false;
335 }
336
337 size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
338 this->makeSpace(space);
339
340 SkPoint* ret = fPoints + fPointCnt;
341 uint8_t* vb = fVerbs - fVerbCnt;
342
343 // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
344 // be 0, the compiler will remove the test/branch entirely.
345 if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
346 memset(vb - numVbs, verb, numVbs);
347 } else {
348 for (int i = 0; i < numVbs; ++i) {
349 vb[~i] = verb;
350 }
351 }
352
353 fVerbCnt += numVbs;
354 fPointCnt += pCnt;
355 fFreeSpace -= space;
356 fBoundsIsDirty = true; // this also invalidates fIsFinite
357 if (dirtyAfterEdit) {
358 fIsOval = false;
359 }
360
361 if (SkPath::kConic_Verb == verb) {
bsalomon49f085d2014-09-05 13:34:00 -0700362 SkASSERT(weights);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000363 *weights = fConicWeights.append(numVbs);
364 }
365
366 SkDEBUGCODE(this->validate();)
367 return ret;
368}
369
370SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
robertphillips@google.com03087072013-10-02 16:42:21 +0000371 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000372 int pCnt;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000373 bool dirtyAfterEdit = true;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000374 switch (verb) {
375 case SkPath::kMove_Verb:
376 pCnt = 1;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000377 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000378 break;
379 case SkPath::kLine_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000380 fSegmentMask |= SkPath::kLine_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000381 pCnt = 1;
382 break;
383 case SkPath::kQuad_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000384 fSegmentMask |= SkPath::kQuad_SegmentMask;
385 pCnt = 2;
386 break;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000387 case SkPath::kConic_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000388 fSegmentMask |= SkPath::kConic_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000389 pCnt = 2;
390 break;
391 case SkPath::kCubic_Verb:
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000392 fSegmentMask |= SkPath::kCubic_SegmentMask;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000393 pCnt = 3;
394 break;
395 case SkPath::kClose_Verb:
396 pCnt = 0;
robertphillips@google.com466310d2013-12-03 16:43:54 +0000397 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000398 break;
399 case SkPath::kDone_Verb:
400 SkDEBUGFAIL("growForVerb called for kDone");
401 // fall through
402 default:
403 SkDEBUGFAIL("default is not reached");
robertphillips@google.com466310d2013-12-03 16:43:54 +0000404 dirtyAfterEdit = false;
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000405 pCnt = 0;
406 }
407 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
408 this->makeSpace(space);
409 this->fVerbs[~fVerbCnt] = verb;
410 SkPoint* ret = fPoints + fPointCnt;
411 fVerbCnt += 1;
412 fPointCnt += pCnt;
413 fFreeSpace -= space;
414 fBoundsIsDirty = true; // this also invalidates fIsFinite
robertphillips@google.com466310d2013-12-03 16:43:54 +0000415 if (dirtyAfterEdit) {
416 fIsOval = false;
417 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000418
419 if (SkPath::kConic_Verb == verb) {
420 *fConicWeights.append() = weight;
421 }
422
robertphillips@google.com03087072013-10-02 16:42:21 +0000423 SkDEBUGCODE(this->validate();)
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000424 return ret;
425}
426
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000427uint32_t SkPathRef::genID() const {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000428 SkASSERT(!fEditorsAttached);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000429 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000430 if (!fGenerationID) {
431 if (0 == fPointCnt && 0 == fVerbCnt) {
432 fGenerationID = kEmptyGenID;
433 } else {
434 static int32_t gPathRefGenerationID;
435 // do a loop in case our global wraps around, as we never want to return a 0 or the
436 // empty ID
437 do {
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000438 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000439 } while (fGenerationID <= kEmptyGenID);
440 }
441 }
442 return fGenerationID;
443}
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000444
senorblanco84cd6212015-08-04 10:01:58 -0700445void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) {
446 if (NULL == listener || this == empty.get()) {
halcanary385fe4d2015-08-26 13:07:48 -0700447 delete listener;
senorblanco84cd6212015-08-04 10:01:58 -0700448 return;
449 }
450 *fGenIDChangeListeners.append() = listener;
451}
452
453// we need to be called *before* the genID gets changed or zerod
454void SkPathRef::callGenIDChangeListeners() {
455 for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
456 fGenIDChangeListeners[i]->onChange();
457 }
458
459 // Listeners get at most one shot, so whether these triggered or not, blow them away.
460 fGenIDChangeListeners.deleteAll();
461}
462
robertphillips@google.com03087072013-10-02 16:42:21 +0000463#ifdef SK_DEBUG
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000464void SkPathRef::validate() const {
robertphillips@google.com03087072013-10-02 16:42:21 +0000465 this->INHERITED::validate();
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000466 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
467 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
468 SkASSERT((NULL == fPoints) == (NULL == fVerbs));
469 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
470 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
471 SkASSERT(!(NULL == fPoints && fPointCnt));
472 SkASSERT(!(NULL == fVerbs && fVerbCnt));
473 SkASSERT(this->currSize() ==
474 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000475
mtklein5c9c9be2014-12-01 06:59:54 -0800476 if (!fBoundsIsDirty && !fBounds.isEmpty()) {
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000477 bool isFinite = true;
478 for (int i = 0; i < fPointCnt; ++i) {
robertphillipsf1cdead2015-01-05 09:20:04 -0800479#ifdef SK_DEBUG
480 if (fPoints[i].isFinite() &&
481 (fPoints[i].fX < fBounds.fLeft || fPoints[i].fX > fBounds.fRight ||
482 fPoints[i].fY < fBounds.fTop || fPoints[i].fY > fBounds.fBottom)) {
483 SkDebugf("bounds: %f %f %f %f\n",
484 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
485 for (int j = 0; j < fPointCnt; ++j) {
486 if (i == j) {
487 SkDebugf("*");
488 }
489 SkDebugf("%f %f\n", fPoints[j].fX, fPoints[j].fY);
490 }
491 }
492#endif
493
robertphillips0e912462014-12-12 12:47:59 -0800494 SkASSERT(!fPoints[i].isFinite() ||
495 (fPoints[i].fX >= fBounds.fLeft && fPoints[i].fX <= fBounds.fRight &&
496 fPoints[i].fY >= fBounds.fTop && fPoints[i].fY <= fBounds.fBottom));
robertphillips@google.com3e292aa2013-09-27 17:48:49 +0000497 if (!fPoints[i].isFinite()) {
498 isFinite = false;
499 }
500 }
501 SkASSERT(SkToBool(fIsFinite) == isFinite);
502 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +0000503
504#ifdef SK_DEBUG_PATH
505 uint32_t mask = 0;
506 for (int i = 0; i < fVerbCnt; ++i) {
507 switch (fVerbs[~i]) {
508 case SkPath::kMove_Verb:
509 break;
510 case SkPath::kLine_Verb:
511 mask |= SkPath::kLine_SegmentMask;
512 break;
513 case SkPath::kQuad_Verb:
514 mask |= SkPath::kQuad_SegmentMask;
515 break;
516 case SkPath::kConic_Verb:
517 mask |= SkPath::kConic_SegmentMask;
518 break;
519 case SkPath::kCubic_Verb:
520 mask |= SkPath::kCubic_SegmentMask;
521 break;
522 case SkPath::kClose_Verb:
523 break;
524 case SkPath::kDone_Verb:
525 SkDEBUGFAIL("Done verb shouldn't be recorded.");
526 break;
527 default:
528 SkDEBUGFAIL("Unknown Verb");
529 break;
530 }
531 }
532 SkASSERT(mask == fSegmentMask);
533#endif // SK_DEBUG_PATH
robertphillips@google.comca0c8382013-09-26 12:18:23 +0000534}
robertphillips@google.com03087072013-10-02 16:42:21 +0000535#endif