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 // As a template argument, this must have external linkage.
33 SkPathRef* sk_create_empty_pathref() {
34 SkPathRef* empty = SkNEW(SkPathRef);
35 empty->computeBounds(); // Avoids races later to be the first to do this.
39 SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, sk_create_empty_pathref);
41 SkPathRef* SkPathRef::CreateEmpty() {
42 return SkRef(empty.get());
45 void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
47 const SkMatrix& matrix) {
48 SkDEBUGCODE(src.validate();)
49 if (matrix.isIdentity()) {
52 dst->reset(const_cast<SkPathRef*>(&src));
53 SkDEBUGCODE((*dst)->validate();)
58 if (!(*dst)->unique()) {
59 dst->reset(SkNEW(SkPathRef));
63 (*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;
68 SkASSERT((*dst)->countPoints() == src.countPoints());
69 SkASSERT((*dst)->countVerbs() == src.countVerbs());
70 SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count());
72 // Need to check this here in case (&src == dst)
73 bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
75 matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
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
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.
88 (*dst)->fBoundsIsDirty = false;
90 matrix.mapRect((*dst)->fBounds.get(), src.fBounds);
91 if (!((*dst)->fIsFinite = (*dst)->fBounds->isFinite())) {
92 (*dst)->fBounds->setEmpty();
95 (*dst)->fIsFinite = false;
96 (*dst)->fBounds->setEmpty();
99 (*dst)->fBoundsIsDirty = true;
102 (*dst)->fSegmentMask = src.fSegmentMask;
104 // It's an oval only if it stays a rect.
105 (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect();
107 SkDEBUGCODE((*dst)->validate();)
110 SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) {
111 SkPathRef* ref = SkNEW(SkPathRef);
116 if (!buffer->readS32(&packed)) {
121 ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
122 segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
123 isOval = (packed >> kIsOval_SerializationShift) & 1;
125 int32_t verbCount, pointCount, conicCount;
126 if (!buffer->readU32(&(ref->fGenerationID)) ||
127 !buffer->readS32(&verbCount) ||
128 !buffer->readS32(&pointCount) ||
129 !buffer->readS32(&conicCount)) {
134 ref->resetToSize(verbCount, pointCount, conicCount);
135 SkASSERT(verbCount == ref->countVerbs());
136 SkASSERT(pointCount == ref->countPoints());
137 SkASSERT(conicCount == ref->fConicWeights.count());
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))) {
146 ref->fBoundsIsDirty = false;
148 // resetToSize clears fSegmentMask and fIsOval
149 ref->fSegmentMask = segmentMask;
150 ref->fIsOval = isOval;
154 void SkPathRef::Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
155 if ((*pathRef)->unique()) {
156 SkDEBUGCODE((*pathRef)->validate();)
157 (*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();
163 (*pathRef)->fSegmentMask = 0;
164 (*pathRef)->fIsOval = false;
165 SkDEBUGCODE((*pathRef)->validate();)
167 int oldVCnt = (*pathRef)->countVerbs();
168 int oldPCnt = (*pathRef)->countPoints();
169 pathRef->reset(SkNEW(SkPathRef));
170 (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt);
174 bool SkPathRef::operator== (const SkPathRef& ref) const {
175 SkDEBUGCODE(this->validate();)
176 SkDEBUGCODE(ref.validate();)
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) {
185 bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID;
191 if (fPointCnt != ref.fPointCnt ||
192 fVerbCnt != ref.fVerbCnt) {
193 SkASSERT(!genIDMatch);
196 if (0 != memcmp(this->verbsMemBegin(),
198 ref.fVerbCnt * sizeof(uint8_t))) {
199 SkASSERT(!genIDMatch);
202 if (0 != memcmp(this->points(),
204 ref.fPointCnt * sizeof(SkPoint))) {
205 SkASSERT(!genIDMatch);
208 if (fConicWeights != ref.fConicWeights) {
209 SkASSERT(!genIDMatch);
212 // We've done the work to determine that these are equal. If either has a zero genID, copy
213 // the other's. If both are 0 then genID() will compute the next ID.
214 if (0 == fGenerationID) {
215 fGenerationID = ref.genID();
216 } else if (0 == ref.fGenerationID) {
217 ref.fGenerationID = this->genID();
222 void SkPathRef::writeToBuffer(SkWBuffer* buffer) const {
223 SkDEBUGCODE(this->validate();)
224 SkDEBUGCODE(size_t beforePos = buffer->pos();)
226 // Call getBounds() to ensure (as a side-effect) that fBounds
227 // and fIsFinite are computed.
228 const SkRect& bounds = this->getBounds();
230 int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
231 ((fIsOval & 1) << kIsOval_SerializationShift) |
232 (fSegmentMask << kSegmentMask_SerializationShift);
233 buffer->write32(packed);
235 // TODO: write gen ID here. Problem: We don't know if we're cross process or not from
236 // SkWBuffer. Until this is fixed we write 0.
238 buffer->write32(fVerbCnt);
239 buffer->write32(fPointCnt);
240 buffer->write32(fConicWeights.count());
241 buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
242 buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
243 buffer->write(fConicWeights.begin(), fConicWeights.bytes());
244 buffer->write(&bounds, sizeof(bounds));
246 SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
249 uint32_t SkPathRef::writeSize() const {
250 return uint32_t(5 * sizeof(uint32_t) +
251 fVerbCnt * sizeof(uint8_t) +
252 fPointCnt * sizeof(SkPoint) +
253 fConicWeights.bytes() +
257 void SkPathRef::copy(const SkPathRef& ref,
258 int additionalReserveVerbs,
259 int additionalReservePoints) {
260 SkDEBUGCODE(this->validate();)
261 this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(),
262 additionalReserveVerbs, additionalReservePoints);
263 memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t));
264 memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint));
265 fConicWeights = ref.fConicWeights;
266 // We could call genID() here to force a real ID (instead of 0). However, if we're making
267 // a copy then presumably we intend to make a modification immediately afterwards.
268 fGenerationID = ref.fGenerationID;
269 fBoundsIsDirty = ref.fBoundsIsDirty;
270 if (!fBoundsIsDirty) {
271 fBounds = ref.fBounds;
272 fIsFinite = ref.fIsFinite;
274 fSegmentMask = ref.fSegmentMask;
275 fIsOval = ref.fIsOval;
276 SkDEBUGCODE(this->validate();)
279 SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb,
281 SkScalar** weights) {
282 // This value is just made-up for now. When count is 4, calling memset was much
283 // slower than just writing the loop. This seems odd, and hopefully in the
284 // future this will appear to have been a fluke...
285 static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16;
287 SkDEBUGCODE(this->validate();)
289 bool dirtyAfterEdit = true;
291 case SkPath::kMove_Verb:
293 dirtyAfterEdit = false;
295 case SkPath::kLine_Verb:
296 fSegmentMask |= SkPath::kLine_SegmentMask;
299 case SkPath::kQuad_Verb:
300 fSegmentMask |= SkPath::kQuad_SegmentMask;
303 case SkPath::kConic_Verb:
304 fSegmentMask |= SkPath::kConic_SegmentMask;
307 case SkPath::kCubic_Verb:
308 fSegmentMask |= SkPath::kCubic_SegmentMask;
311 case SkPath::kClose_Verb:
312 SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb");
314 dirtyAfterEdit = false;
316 case SkPath::kDone_Verb:
317 SkDEBUGFAIL("growForRepeatedVerb called for kDone");
320 SkDEBUGFAIL("default should not be reached");
322 dirtyAfterEdit = false;
325 size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint);
326 this->makeSpace(space);
328 SkPoint* ret = fPoints + fPointCnt;
329 uint8_t* vb = fVerbs - fVerbCnt;
331 // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to
332 // be 0, the compiler will remove the test/branch entirely.
333 if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) {
334 memset(vb - numVbs, verb, numVbs);
336 for (int i = 0; i < numVbs; ++i) {
344 fBoundsIsDirty = true; // this also invalidates fIsFinite
345 if (dirtyAfterEdit) {
349 if (SkPath::kConic_Verb == verb) {
351 *weights = fConicWeights.append(numVbs);
354 SkDEBUGCODE(this->validate();)
358 SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) {
359 SkDEBUGCODE(this->validate();)
361 bool dirtyAfterEdit = true;
363 case SkPath::kMove_Verb:
365 dirtyAfterEdit = false;
367 case SkPath::kLine_Verb:
368 fSegmentMask |= SkPath::kLine_SegmentMask;
371 case SkPath::kQuad_Verb:
372 fSegmentMask |= SkPath::kQuad_SegmentMask;
375 case SkPath::kConic_Verb:
376 fSegmentMask |= SkPath::kConic_SegmentMask;
379 case SkPath::kCubic_Verb:
380 fSegmentMask |= SkPath::kCubic_SegmentMask;
383 case SkPath::kClose_Verb:
385 dirtyAfterEdit = false;
387 case SkPath::kDone_Verb:
388 SkDEBUGFAIL("growForVerb called for kDone");
391 SkDEBUGFAIL("default is not reached");
392 dirtyAfterEdit = false;
395 size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint);
396 this->makeSpace(space);
397 this->fVerbs[~fVerbCnt] = verb;
398 SkPoint* ret = fPoints + fPointCnt;
402 fBoundsIsDirty = true; // this also invalidates fIsFinite
403 if (dirtyAfterEdit) {
407 if (SkPath::kConic_Verb == verb) {
408 *fConicWeights.append() = weight;
411 SkDEBUGCODE(this->validate();)
415 uint32_t SkPathRef::genID() const {
416 SkASSERT(!fEditorsAttached);
417 static const uint32_t kMask = (static_cast<int64_t>(1) << SkPath::kPathRefGenIDBitCnt) - 1;
418 if (!fGenerationID) {
419 if (0 == fPointCnt && 0 == fVerbCnt) {
420 fGenerationID = kEmptyGenID;
422 static int32_t gPathRefGenerationID;
423 // do a loop in case our global wraps around, as we never want to return a 0 or the
426 fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask;
427 } while (fGenerationID <= kEmptyGenID);
430 return fGenerationID;
434 void SkPathRef::validate() const {
435 this->INHERITED::validate();
436 SkASSERT(static_cast<ptrdiff_t>(fFreeSpace) >= 0);
437 SkASSERT(reinterpret_cast<intptr_t>(fVerbs) - reinterpret_cast<intptr_t>(fPoints) >= 0);
438 SkASSERT((NULL == fPoints) == (NULL == fVerbs));
439 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
440 SkASSERT(!(NULL == fPoints && 0 != fFreeSpace));
441 SkASSERT(!(NULL == fPoints && fPointCnt));
442 SkASSERT(!(NULL == fVerbs && fVerbCnt));
443 SkASSERT(this->currSize() ==
444 fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
446 if (!fBoundsIsDirty && !fBounds->isEmpty()) {
447 bool isFinite = true;
448 for (int i = 0; i < fPointCnt; ++i) {
449 SkASSERT(!fPoints[i].isFinite() || (
450 fBounds->fLeft - fPoints[i].fX < SK_ScalarNearlyZero &&
451 fPoints[i].fX - fBounds->fRight < SK_ScalarNearlyZero &&
452 fBounds->fTop - fPoints[i].fY < SK_ScalarNearlyZero &&
453 fPoints[i].fY - fBounds->fBottom < SK_ScalarNearlyZero));
454 if (!fPoints[i].isFinite()) {
458 SkASSERT(SkToBool(fIsFinite) == isFinite);
463 for (int i = 0; i < fVerbCnt; ++i) {
464 switch (fVerbs[~i]) {
465 case SkPath::kMove_Verb:
467 case SkPath::kLine_Verb:
468 mask |= SkPath::kLine_SegmentMask;
470 case SkPath::kQuad_Verb:
471 mask |= SkPath::kQuad_SegmentMask;
473 case SkPath::kConic_Verb:
474 mask |= SkPath::kConic_SegmentMask;
476 case SkPath::kCubic_Verb:
477 mask |= SkPath::kCubic_SegmentMask;
479 case SkPath::kClose_Verb:
481 case SkPath::kDone_Verb:
482 SkDEBUGFAIL("Done verb shouldn't be recorded.");
485 SkDEBUGFAIL("Unknown Verb");
489 SkASSERT(mask == fSegmentMask);
490 #endif // SK_DEBUG_PATH