2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkPathRef.h"
13 //////////////////////////////////////////////////////////////////////////////
14 SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
18 if ((*pathRef)->unique()) {
19 (*pathRef)->incReserve(incReserveVerbs, incReservePoints);
21 SkPathRef* copy = SkNEW(SkPathRef);
22 copy->copy(**pathRef, incReserveVerbs, incReservePoints);
26 fPathRef->fGenerationID = 0;
27 SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);)
30 //////////////////////////////////////////////////////////////////////////////
32 SkPathRef* SkPathRef::CreateEmptyImpl() {
33 return SkNEW(SkPathRef);
36 SkPathRef* SkPathRef::CreateEmpty() {
37 SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, CreateEmptyImpl);
38 return SkRef(empty.get());
41 void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
43 const SkMatrix& matrix) {
44 SkDEBUGCODE(src.validate();)
45 if (matrix.isIdentity()) {
48 dst->reset(const_cast<SkPathRef*>(&src));
49 SkDEBUGCODE((*dst)->validate();)
54 if (!(*dst)->unique()) {
55 dst->reset(SkNEW(SkPathRef));
59 (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
60 memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
61 (*dst)->fConicWeights = src.fConicWeights;
64 SkASSERT((*dst)->countPoints() == src.countPoints());
65 SkASSERT((*dst)->countVerbs() == src.countVerbs());
66 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
68 // Need to check this here in case (&src == dst)
69 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
71 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
74 * Here we optimize the bounds computation, by noting if the bounds are
75 * already known, and if so, we just transform those as well and mark
76 * them as "known", rather than force the transformed path to have to
79 * Special gotchas if the path is effectively empty (<= 1 point) or
80 * if it is non-finite. In those cases bounds need to stay empty,
81 * regardless of the matrix.
84 (*dst)->fBoundsIsDirty = false;
86 matrix.mapRect((*dst)->fBounds.get(), src.fBounds);
87 if (!((*dst)->fIsFinite = (*dst)->fBounds->isFinite())) {
88 (*dst)->fBounds->setEmpty();
91 (*dst)->fIsFinite = false;
92 (*dst)->fBounds->setEmpty();
95 (*dst)->fBoundsIsDirty = true;
98 (*dst)->fSegmentMask = src.fSegmentMask;
100 // It's an oval only if it stays a rect.
101 (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
103 SkDEBUGCODE((*dst)->validate();)
106 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
107 SkPathRef* ref = SkNEW(SkPathRef);
112 if (!buffer->readS32(&packed)) {
117 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
118 segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
119 isOval = (packed >> kIsOval_SerializationShift) & 1;
121 int32_t verbCount, pointCount, conicCount;
122 if (!buffer->readU32(&(ref->fGenerationID)) ||
123 !buffer->readS32(&verbCount) ||
124 !buffer->readS32(&pointCount) ||
125 !buffer->readS32(&conicCount)) {
130 ref->resetToSize(verbCount, pointCount, conicCount);
131 SkASSERT(verbCount == ref->countVerbs());
132 SkASSERT(pointCount == ref->countPoints());
133 SkASSERT(conicCount == ref->fConicWeights.count());
135 if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) ||
136 !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) ||
137 !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) ||
138 !buffer->read(&ref->fBounds, sizeof(SkRect))) {
142 ref->fBoundsIsDirty = false;
144 // resetToSize clears fSegmentMask and fIsOval
145 ref->fSegmentMask = segmentMask;
146 ref->fIsOval = isOval;
150 void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
151 if ((*pathRef)->unique()) {
152 SkDEBUGCODE((*pathRef)->validate();)
153 (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
154 (*pathRef)->fVerbCnt = 0;
155 (*pathRef)->fPointCnt = 0;
156 (*pathRef)->fFreeSpace = (*pathRef)->currSize();
157 (*pathRef)->fGenerationID = 0;
158 (*pathRef)->fConicWeights.rewind();
159 (*pathRef)->fSegmentMask = 0;
160 (*pathRef)->fIsOval = false;
161 SkDEBUGCODE((*pathRef)->validate();)
163 int oldVCnt = (*pathRef)->countVerbs();
164 int oldPCnt = (*pathRef)->countPoints();
165 pathRef->reset(SkNEW(SkPathRef));
166 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
170 bool SkPathRef::operator== (const SkPathRef& ref) const {
171 SkDEBUGCODE(this->validate();)
172 SkDEBUGCODE(ref.validate();)
174 // We explicitly check fSegmentMask as a quick-reject. We could skip it,
175 // since it is only a cache of info in the fVerbs, but its a fast way to
176 // notice a difference
177 if (fSegmentMask != ref.fSegmentMask) {
181 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
187 if (fPointCnt != ref.fPointCnt ||
188 fVerbCnt != ref.fVerbCnt) {
189 SkASSERT(!genIDMatch);
192 if (0 != memcmp(this->verbsMemBegin(),
194 ref.fVerbCnt * sizeof(uint8_t))) {
195 SkASSERT(!genIDMatch);
198 if (0 != memcmp(this->points(),
200 ref.fPointCnt * sizeof(SkPoint))) {
201 SkASSERT(!genIDMatch);
204 if (fConicWeights != ref.fConicWeights) {
205 SkASSERT(!genIDMatch);
208 // We've done the work to determine that these are equal. If either has a zero genID, copy
209 // the other's. If both are 0 then genID() will compute the next ID.
210 if (0 == fGenerationID) {
211 fGenerationID = ref.genID();
212 } else if (0 == ref.fGenerationID) {
213 ref.fGenerationID = this->genID();
218 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
219 SkDEBUGCODE(this->validate();)
220 SkDEBUGCODE(size_t beforePos = buffer->pos();)
222 // Call getBounds() to ensure (as a side-effect) that fBounds
223 // and fIsFinite are computed.
224 const SkRect& bounds = this->getBounds();
226 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
227 ((fIsOval & 1) << kIsOval_SerializationShift) |
228 (fSegmentMask << kSegmentMask_SerializationShift);
229 buffer->write32(packed);
231 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
232 // SkWBuffer. Until this is fixed we write 0.
234 buffer->write32(fVerbCnt);
235 buffer->write32(fPointCnt);
236 buffer->write32(fConicWeights.count());
237 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
238 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
239 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
240 buffer->write(&bounds, sizeof(bounds));
242 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
245 uint32_t SkPathRef::writeSize() const {
246 return uint32_t(5 * sizeof(uint32_t) +
247 fVerbCnt * sizeof(uint8_t) +
248 fPointCnt * sizeof(SkPoint) +
249 fConicWeights.bytes() +
253 void SkPathRef::copy(const SkPathRef& ref,
254 int additionalReserveVerbs,
255 int additionalReservePoints) {
256 SkDEBUGCODE(this->validate();)
257 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
258 additionalReserveVerbs, additionalReservePoints);
259 memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
260 memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
261 fConicWeights = ref.fConicWeights;
262 // We could call genID() here to force a real ID (instead of 0). However, if we're making
263 // a copy then presumably we intend to make a modification immediately afterwards.
264 fGenerationID = ref.fGenerationID;
265 fBoundsIsDirty = ref.fBoundsIsDirty;
266 if (!fBoundsIsDirty) {
267 fBounds = ref.fBounds;
268 fIsFinite = ref.fIsFinite;
270 fSegmentMask = ref.fSegmentMask;
271 fIsOval = ref.fIsOval;
272 SkDEBUGCODE(this->validate();)
275 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
277 SkScalar** weights) {
278 // This value is just made-up for now. When count is 4, calling memset was much
279 // slower than just writing the loop. This seems odd, and hopefully in the
280 // future this will appear to have been a fluke...
281 static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
283 SkDEBUGCODE(this->validate();)
285 bool dirtyAfterEdit = true;
287 case SkPath::kMove_Verb:
289 dirtyAfterEdit = false;
291 case SkPath::kLine_Verb:
292 fSegmentMask |= SkPath::kLine_SegmentMask;
295 case SkPath::kQuad_Verb:
296 fSegmentMask |= SkPath::kQuad_SegmentMask;
299 case SkPath::kConic_Verb:
300 fSegmentMask |= SkPath::kConic_SegmentMask;
303 case SkPath::kCubic_Verb:
304 fSegmentMask |= SkPath::kCubic_SegmentMask;
307 case SkPath::kClose_Verb:
308 SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
310 dirtyAfterEdit = false;
312 case SkPath::kDone_Verb:
313 SkDEBUGFAIL("growForRepeatedVerb called for kDone");
316 SkDEBUGFAIL("default should not be reached");
318 dirtyAfterEdit = false;
321 size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
322 this->makeSpace(space);
324 SkPoint* ret = fPoints + fPointCnt;
325 uint8_t* vb = fVerbs - fVerbCnt;
327 // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
328 // be 0, the compiler will remove the test/branch entirely.
329 if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
330 memset(vb - numVbs, verb, numVbs);
332 for (int i = 0; i < numVbs; ++i) {
340 fBoundsIsDirty = true; // this also invalidates fIsFinite
341 if (dirtyAfterEdit) {
345 if (SkPath::kConic_Verb == verb) {
346 SkASSERT(NULL != weights);
347 *weights = fConicWeights.append(numVbs);
350 SkDEBUGCODE(this->validate();)
354 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
355 SkDEBUGCODE(this->validate();)
357 bool dirtyAfterEdit = true;
359 case SkPath::kMove_Verb:
361 dirtyAfterEdit = false;
363 case SkPath::kLine_Verb:
364 fSegmentMask |= SkPath::kLine_SegmentMask;
367 case SkPath::kQuad_Verb:
368 fSegmentMask |= SkPath::kQuad_SegmentMask;
371 case SkPath::kConic_Verb:
372 fSegmentMask |= SkPath::kConic_SegmentMask;
375 case SkPath::kCubic_Verb:
376 fSegmentMask |= SkPath::kCubic_SegmentMask;
379 case SkPath::kClose_Verb:
381 dirtyAfterEdit = false;
383 case SkPath::kDone_Verb:
384 SkDEBUGFAIL("growForVerb called for kDone");
387 SkDEBUGFAIL("default is not reached");
388 dirtyAfterEdit = false;
391 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
392 this->makeSpace(space);
393 this->fVerbs[~fVerbCnt] = verb;
394 SkPoint* ret = fPoints + fPointCnt;
398 fBoundsIsDirty = true; // this also invalidates fIsFinite
399 if (dirtyAfterEdit) {
403 if (SkPath::kConic_Verb == verb) {
404 *fConicWeights.append() = weight;
407 SkDEBUGCODE(this->validate();)
411 uint32_t SkPathRef::genID() const {
412 SkASSERT(!fEditorsAttached);
413 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
414 if (!fGenerationID) {
415 if (0 == fPointCnt && 0 == fVerbCnt) {
416 fGenerationID = kEmptyGenID;
418 static int32_t gPathRefGenerationID;
419 // do a loop in case our global wraps around, as we never want to return a 0 or the
422 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
423 } while (fGenerationID <= kEmptyGenID);
426 return fGenerationID;
430 void SkPathRef::validate() const {
431 this->INHERITED::validate();
432 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
433 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
434 SkASSERT((NULL == fPoints) == (NULL == fVerbs));
435 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
436 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
437 SkASSERT(!(NULL == fPoints && fPointCnt));
438 SkASSERT(!(NULL == fVerbs && fVerbCnt));
439 SkASSERT(this->currSize() ==
440 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
442 if (!fBoundsIsDirty && !fBounds->isEmpty()) {
443 bool isFinite = true;
444 for (int i = 0; i < fPointCnt; ++i) {
445 SkASSERT(!fPoints[i].isFinite() || (
446 fBounds->fLeft - fPoints[i].fX < SK_ScalarNearlyZero &&
447 fPoints[i].fX - fBounds->fRight < SK_ScalarNearlyZero &&
448 fBounds->fTop - fPoints[i].fY < SK_ScalarNearlyZero &&
449 fPoints[i].fY - fBounds->fBottom < SK_ScalarNearlyZero));
450 if (!fPoints[i].isFinite()) {
454 SkASSERT(SkToBool(fIsFinite) == isFinite);
459 for (int i = 0; i < fVerbCnt; ++i) {
460 switch (fVerbs[~i]) {
461 case SkPath::kMove_Verb:
463 case SkPath::kLine_Verb:
464 mask |= SkPath::kLine_SegmentMask;
466 case SkPath::kQuad_Verb:
467 mask |= SkPath::kQuad_SegmentMask;
469 case SkPath::kConic_Verb:
470 mask |= SkPath::kConic_SegmentMask;
472 case SkPath::kCubic_Verb:
473 mask |= SkPath::kCubic_SegmentMask;
475 case SkPath::kClose_Verb:
477 case SkPath::kDone_Verb:
478 SkDEBUGFAIL("Done verb shouldn't be recorded.");
481 SkDEBUGFAIL("Unknown Verb");
485 SkASSERT(mask == fSegmentMask);
486 #endif // SK_DEBUG_PATH