michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkBuffer.h" michael@0: #include "SkOnce.h" michael@0: #include "SkPath.h" michael@0: #include "SkPathRef.h" michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: SkPathRef::Editor::Editor(SkAutoTUnref* pathRef, michael@0: int incReserveVerbs, michael@0: int incReservePoints) michael@0: { michael@0: if ((*pathRef)->unique()) { michael@0: (*pathRef)->incReserve(incReserveVerbs, incReservePoints); michael@0: } else { michael@0: SkPathRef* copy = SkNEW(SkPathRef); michael@0: copy->copy(**pathRef, incReserveVerbs, incReservePoints); michael@0: pathRef->reset(copy); michael@0: } michael@0: fPathRef = *pathRef; michael@0: fPathRef->fGenerationID = 0; michael@0: SkDEBUGCODE(sk_atomic_inc(&fPathRef->fEditorsAttached);) michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: static SkPathRef* gEmptyPathRef = NULL; michael@0: static void cleanup_gEmptyPathRef() { gEmptyPathRef->unref(); } michael@0: michael@0: void SkPathRef::CreateEmptyImpl(int) { michael@0: gEmptyPathRef = SkNEW(SkPathRef); michael@0: gEmptyPathRef->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty. michael@0: } michael@0: michael@0: SkPathRef* SkPathRef::CreateEmpty() { michael@0: SK_DECLARE_STATIC_ONCE(once); michael@0: SkOnce(&once, SkPathRef::CreateEmptyImpl, 0, cleanup_gEmptyPathRef); michael@0: return SkRef(gEmptyPathRef); michael@0: } michael@0: michael@0: void SkPathRef::CreateTransformedCopy(SkAutoTUnref* dst, michael@0: const SkPathRef& src, michael@0: const SkMatrix& matrix) { michael@0: SkDEBUGCODE(src.validate();) michael@0: if (matrix.isIdentity()) { michael@0: if (*dst != &src) { michael@0: src.ref(); michael@0: dst->reset(const_cast(&src)); michael@0: SkDEBUGCODE((*dst)->validate();) michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (!(*dst)->unique()) { michael@0: dst->reset(SkNEW(SkPathRef)); michael@0: } michael@0: michael@0: if (*dst != &src) { michael@0: (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count()); michael@0: memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t)); michael@0: (*dst)->fConicWeights = src.fConicWeights; michael@0: } michael@0: michael@0: SkASSERT((*dst)->countPoints() == src.countPoints()); michael@0: SkASSERT((*dst)->countVerbs() == src.countVerbs()); michael@0: SkASSERT((*dst)->fConicWeights.count() == src.fConicWeights.count()); michael@0: michael@0: // Need to check this here in case (&src == dst) michael@0: bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1; michael@0: michael@0: matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt); michael@0: michael@0: /* michael@0: * Here we optimize the bounds computation, by noting if the bounds are michael@0: * already known, and if so, we just transform those as well and mark michael@0: * them as "known", rather than force the transformed path to have to michael@0: * recompute them. michael@0: * michael@0: * Special gotchas if the path is effectively empty (<= 1 point) or michael@0: * if it is non-finite. In those cases bounds need to stay empty, michael@0: * regardless of the matrix. michael@0: */ michael@0: if (canXformBounds) { michael@0: (*dst)->fBoundsIsDirty = false; michael@0: if (src.fIsFinite) { michael@0: matrix.mapRect(&(*dst)->fBounds, src.fBounds); michael@0: if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) { michael@0: (*dst)->fBounds.setEmpty(); michael@0: } michael@0: } else { michael@0: (*dst)->fIsFinite = false; michael@0: (*dst)->fBounds.setEmpty(); michael@0: } michael@0: } else { michael@0: (*dst)->fBoundsIsDirty = true; michael@0: } michael@0: michael@0: (*dst)->fSegmentMask = src.fSegmentMask; michael@0: michael@0: // It's an oval only if it stays a rect. michael@0: (*dst)->fIsOval = src.fIsOval && matrix.rectStaysRect(); michael@0: michael@0: SkDEBUGCODE((*dst)->validate();) michael@0: } michael@0: michael@0: SkPathRef* SkPathRef::CreateFromBuffer(SkRBuffer* buffer) { michael@0: SkPathRef* ref = SkNEW(SkPathRef); michael@0: bool isOval; michael@0: uint8_t segmentMask; michael@0: michael@0: int32_t packed; michael@0: if (!buffer->readS32(&packed)) { michael@0: SkDELETE(ref); michael@0: return NULL; michael@0: } michael@0: michael@0: ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1; michael@0: segmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; michael@0: isOval = (packed >> kIsOval_SerializationShift) & 1; michael@0: michael@0: int32_t verbCount, pointCount, conicCount; michael@0: if (!buffer->readU32(&(ref->fGenerationID)) || michael@0: !buffer->readS32(&verbCount) || michael@0: !buffer->readS32(&pointCount) || michael@0: !buffer->readS32(&conicCount)) { michael@0: SkDELETE(ref); michael@0: return NULL; michael@0: } michael@0: michael@0: ref->resetToSize(verbCount, pointCount, conicCount); michael@0: SkASSERT(verbCount == ref->countVerbs()); michael@0: SkASSERT(pointCount == ref->countPoints()); michael@0: SkASSERT(conicCount == ref->fConicWeights.count()); michael@0: michael@0: if (!buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t)) || michael@0: !buffer->read(ref->fPoints, pointCount * sizeof(SkPoint)) || michael@0: !buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar)) || michael@0: !buffer->read(&ref->fBounds, sizeof(SkRect))) { michael@0: SkDELETE(ref); michael@0: return NULL; michael@0: } michael@0: ref->fBoundsIsDirty = false; michael@0: michael@0: // resetToSize clears fSegmentMask and fIsOval michael@0: ref->fSegmentMask = segmentMask; michael@0: ref->fIsOval = isOval; michael@0: return ref; michael@0: } michael@0: michael@0: void SkPathRef::Rewind(SkAutoTUnref* pathRef) { michael@0: if ((*pathRef)->unique()) { michael@0: SkDEBUGCODE((*pathRef)->validate();) michael@0: (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite michael@0: (*pathRef)->fVerbCnt = 0; michael@0: (*pathRef)->fPointCnt = 0; michael@0: (*pathRef)->fFreeSpace = (*pathRef)->currSize(); michael@0: (*pathRef)->fGenerationID = 0; michael@0: (*pathRef)->fConicWeights.rewind(); michael@0: (*pathRef)->fSegmentMask = 0; michael@0: (*pathRef)->fIsOval = false; michael@0: SkDEBUGCODE((*pathRef)->validate();) michael@0: } else { michael@0: int oldVCnt = (*pathRef)->countVerbs(); michael@0: int oldPCnt = (*pathRef)->countPoints(); michael@0: pathRef->reset(SkNEW(SkPathRef)); michael@0: (*pathRef)->resetToSize(0, 0, 0, oldVCnt, oldPCnt); michael@0: } michael@0: } michael@0: michael@0: bool SkPathRef::operator== (const SkPathRef& ref) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: SkDEBUGCODE(ref.validate();) michael@0: michael@0: // We explicitly check fSegmentMask as a quick-reject. We could skip it, michael@0: // since it is only a cache of info in the fVerbs, but its a fast way to michael@0: // notice a difference michael@0: if (fSegmentMask != ref.fSegmentMask) { michael@0: return false; michael@0: } michael@0: michael@0: bool genIDMatch = fGenerationID && fGenerationID == ref.fGenerationID; michael@0: #ifdef SK_RELEASE michael@0: if (genIDMatch) { michael@0: return true; michael@0: } michael@0: #endif michael@0: if (fPointCnt != ref.fPointCnt || michael@0: fVerbCnt != ref.fVerbCnt) { michael@0: SkASSERT(!genIDMatch); michael@0: return false; michael@0: } michael@0: if (0 != memcmp(this->verbsMemBegin(), michael@0: ref.verbsMemBegin(), michael@0: ref.fVerbCnt * sizeof(uint8_t))) { michael@0: SkASSERT(!genIDMatch); michael@0: return false; michael@0: } michael@0: if (0 != memcmp(this->points(), michael@0: ref.points(), michael@0: ref.fPointCnt * sizeof(SkPoint))) { michael@0: SkASSERT(!genIDMatch); michael@0: return false; michael@0: } michael@0: if (fConicWeights != ref.fConicWeights) { michael@0: SkASSERT(!genIDMatch); michael@0: return false; michael@0: } michael@0: // We've done the work to determine that these are equal. If either has a zero genID, copy michael@0: // the other's. If both are 0 then genID() will compute the next ID. michael@0: if (0 == fGenerationID) { michael@0: fGenerationID = ref.genID(); michael@0: } else if (0 == ref.fGenerationID) { michael@0: ref.fGenerationID = this->genID(); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void SkPathRef::writeToBuffer(SkWBuffer* buffer) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: SkDEBUGCODE(size_t beforePos = buffer->pos();) michael@0: michael@0: // Call getBounds() to ensure (as a side-effect) that fBounds michael@0: // and fIsFinite are computed. michael@0: const SkRect& bounds = this->getBounds(); michael@0: michael@0: int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) | michael@0: ((fIsOval & 1) << kIsOval_SerializationShift) | michael@0: (fSegmentMask << kSegmentMask_SerializationShift); michael@0: buffer->write32(packed); michael@0: michael@0: // TODO: write gen ID here. Problem: We don't know if we're cross process or not from michael@0: // SkWBuffer. Until this is fixed we write 0. michael@0: buffer->write32(0); michael@0: buffer->write32(fVerbCnt); michael@0: buffer->write32(fPointCnt); michael@0: buffer->write32(fConicWeights.count()); michael@0: buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t)); michael@0: buffer->write(fPoints, fPointCnt * sizeof(SkPoint)); michael@0: buffer->write(fConicWeights.begin(), fConicWeights.bytes()); michael@0: buffer->write(&bounds, sizeof(bounds)); michael@0: michael@0: SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize()); michael@0: } michael@0: michael@0: uint32_t SkPathRef::writeSize() const { michael@0: return uint32_t(5 * sizeof(uint32_t) + michael@0: fVerbCnt * sizeof(uint8_t) + michael@0: fPointCnt * sizeof(SkPoint) + michael@0: fConicWeights.bytes() + michael@0: sizeof(SkRect)); michael@0: } michael@0: michael@0: void SkPathRef::copy(const SkPathRef& ref, michael@0: int additionalReserveVerbs, michael@0: int additionalReservePoints) { michael@0: SkDEBUGCODE(this->validate();) michael@0: this->resetToSize(ref.fVerbCnt, ref.fPointCnt, ref.fConicWeights.count(), michael@0: additionalReserveVerbs, additionalReservePoints); michael@0: memcpy(this->verbsMemWritable(), ref.verbsMemBegin(), ref.fVerbCnt * sizeof(uint8_t)); michael@0: memcpy(this->fPoints, ref.fPoints, ref.fPointCnt * sizeof(SkPoint)); michael@0: fConicWeights = ref.fConicWeights; michael@0: // We could call genID() here to force a real ID (instead of 0). However, if we're making michael@0: // a copy then presumably we intend to make a modification immediately afterwards. michael@0: fGenerationID = ref.fGenerationID; michael@0: fBoundsIsDirty = ref.fBoundsIsDirty; michael@0: if (!fBoundsIsDirty) { michael@0: fBounds = ref.fBounds; michael@0: fIsFinite = ref.fIsFinite; michael@0: } michael@0: fSegmentMask = ref.fSegmentMask; michael@0: fIsOval = ref.fIsOval; michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: SkPoint* SkPathRef::growForRepeatedVerb(int /*SkPath::Verb*/ verb, michael@0: int numVbs, michael@0: SkScalar** weights) { michael@0: // This value is just made-up for now. When count is 4, calling memset was much michael@0: // slower than just writing the loop. This seems odd, and hopefully in the michael@0: // future this will appear to have been a fluke... michael@0: static const unsigned int kMIN_COUNT_FOR_MEMSET_TO_BE_FAST = 16; michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: int pCnt; michael@0: bool dirtyAfterEdit = true; michael@0: switch (verb) { michael@0: case SkPath::kMove_Verb: michael@0: pCnt = numVbs; michael@0: dirtyAfterEdit = false; michael@0: break; michael@0: case SkPath::kLine_Verb: michael@0: fSegmentMask |= SkPath::kLine_SegmentMask; michael@0: pCnt = numVbs; michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: fSegmentMask |= SkPath::kQuad_SegmentMask; michael@0: pCnt = 2 * numVbs; michael@0: break; michael@0: case SkPath::kConic_Verb: michael@0: fSegmentMask |= SkPath::kConic_SegmentMask; michael@0: pCnt = 2 * numVbs; michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: fSegmentMask |= SkPath::kCubic_SegmentMask; michael@0: pCnt = 3 * numVbs; michael@0: break; michael@0: case SkPath::kClose_Verb: michael@0: SkDEBUGFAIL("growForRepeatedVerb called for kClose_Verb"); michael@0: pCnt = 0; michael@0: dirtyAfterEdit = false; michael@0: break; michael@0: case SkPath::kDone_Verb: michael@0: SkDEBUGFAIL("growForRepeatedVerb called for kDone"); michael@0: // fall through michael@0: default: michael@0: SkDEBUGFAIL("default should not be reached"); michael@0: pCnt = 0; michael@0: dirtyAfterEdit = false; michael@0: } michael@0: michael@0: size_t space = numVbs * sizeof(uint8_t) + pCnt * sizeof (SkPoint); michael@0: this->makeSpace(space); michael@0: michael@0: SkPoint* ret = fPoints + fPointCnt; michael@0: uint8_t* vb = fVerbs - fVerbCnt; michael@0: michael@0: // cast to unsigned, so if kMIN_COUNT_FOR_MEMSET_TO_BE_FAST is defined to michael@0: // be 0, the compiler will remove the test/branch entirely. michael@0: if ((unsigned)numVbs >= kMIN_COUNT_FOR_MEMSET_TO_BE_FAST) { michael@0: memset(vb - numVbs, verb, numVbs); michael@0: } else { michael@0: for (int i = 0; i < numVbs; ++i) { michael@0: vb[~i] = verb; michael@0: } michael@0: } michael@0: michael@0: fVerbCnt += numVbs; michael@0: fPointCnt += pCnt; michael@0: fFreeSpace -= space; michael@0: fBoundsIsDirty = true; // this also invalidates fIsFinite michael@0: if (dirtyAfterEdit) { michael@0: fIsOval = false; michael@0: } michael@0: michael@0: if (SkPath::kConic_Verb == verb) { michael@0: SkASSERT(NULL != weights); michael@0: *weights = fConicWeights.append(numVbs); michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: return ret; michael@0: } michael@0: michael@0: SkPoint* SkPathRef::growForVerb(int /* SkPath::Verb*/ verb, SkScalar weight) { michael@0: SkDEBUGCODE(this->validate();) michael@0: int pCnt; michael@0: bool dirtyAfterEdit = true; michael@0: switch (verb) { michael@0: case SkPath::kMove_Verb: michael@0: pCnt = 1; michael@0: dirtyAfterEdit = false; michael@0: break; michael@0: case SkPath::kLine_Verb: michael@0: fSegmentMask |= SkPath::kLine_SegmentMask; michael@0: pCnt = 1; michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: fSegmentMask |= SkPath::kQuad_SegmentMask; michael@0: pCnt = 2; michael@0: break; michael@0: case SkPath::kConic_Verb: michael@0: fSegmentMask |= SkPath::kConic_SegmentMask; michael@0: pCnt = 2; michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: fSegmentMask |= SkPath::kCubic_SegmentMask; michael@0: pCnt = 3; michael@0: break; michael@0: case SkPath::kClose_Verb: michael@0: pCnt = 0; michael@0: dirtyAfterEdit = false; michael@0: break; michael@0: case SkPath::kDone_Verb: michael@0: SkDEBUGFAIL("growForVerb called for kDone"); michael@0: // fall through michael@0: default: michael@0: SkDEBUGFAIL("default is not reached"); michael@0: dirtyAfterEdit = false; michael@0: pCnt = 0; michael@0: } michael@0: size_t space = sizeof(uint8_t) + pCnt * sizeof (SkPoint); michael@0: this->makeSpace(space); michael@0: this->fVerbs[~fVerbCnt] = verb; michael@0: SkPoint* ret = fPoints + fPointCnt; michael@0: fVerbCnt += 1; michael@0: fPointCnt += pCnt; michael@0: fFreeSpace -= space; michael@0: fBoundsIsDirty = true; // this also invalidates fIsFinite michael@0: if (dirtyAfterEdit) { michael@0: fIsOval = false; michael@0: } michael@0: michael@0: if (SkPath::kConic_Verb == verb) { michael@0: *fConicWeights.append() = weight; michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: return ret; michael@0: } michael@0: michael@0: uint32_t SkPathRef::genID() const { michael@0: SkASSERT(!fEditorsAttached); michael@0: static const uint32_t kMask = (static_cast(1) << SkPath::kPathRefGenIDBitCnt) - 1; michael@0: if (!fGenerationID) { michael@0: if (0 == fPointCnt && 0 == fVerbCnt) { michael@0: fGenerationID = kEmptyGenID; michael@0: } else { michael@0: static int32_t gPathRefGenerationID; michael@0: // do a loop in case our global wraps around, as we never want to return a 0 or the michael@0: // empty ID michael@0: do { michael@0: fGenerationID = (sk_atomic_inc(&gPathRefGenerationID) + 1) & kMask; michael@0: } while (fGenerationID <= kEmptyGenID); michael@0: } michael@0: } michael@0: return fGenerationID; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkPathRef::validate() const { michael@0: this->INHERITED::validate(); michael@0: SkASSERT(static_cast(fFreeSpace) >= 0); michael@0: SkASSERT(reinterpret_cast(fVerbs) - reinterpret_cast(fPoints) >= 0); michael@0: SkASSERT((NULL == fPoints) == (NULL == fVerbs)); michael@0: SkASSERT(!(NULL == fPoints && 0 != fFreeSpace)); michael@0: SkASSERT(!(NULL == fPoints && 0 != fFreeSpace)); michael@0: SkASSERT(!(NULL == fPoints && fPointCnt)); michael@0: SkASSERT(!(NULL == fVerbs && fVerbCnt)); michael@0: SkASSERT(this->currSize() == michael@0: fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt); michael@0: michael@0: if (!fBoundsIsDirty && !fBounds.isEmpty()) { michael@0: bool isFinite = true; michael@0: for (int i = 0; i < fPointCnt; ++i) { michael@0: SkASSERT(!fPoints[i].isFinite() || ( michael@0: fBounds.fLeft - fPoints[i].fX < SK_ScalarNearlyZero && michael@0: fPoints[i].fX - fBounds.fRight < SK_ScalarNearlyZero && michael@0: fBounds.fTop - fPoints[i].fY < SK_ScalarNearlyZero && michael@0: fPoints[i].fY - fBounds.fBottom < SK_ScalarNearlyZero)); michael@0: if (!fPoints[i].isFinite()) { michael@0: isFinite = false; michael@0: } michael@0: } michael@0: SkASSERT(SkToBool(fIsFinite) == isFinite); michael@0: } michael@0: michael@0: #ifdef SK_DEBUG_PATH michael@0: uint32_t mask = 0; michael@0: for (int i = 0; i < fVerbCnt; ++i) { michael@0: switch (fVerbs[~i]) { michael@0: case SkPath::kMove_Verb: michael@0: break; michael@0: case SkPath::kLine_Verb: michael@0: mask |= SkPath::kLine_SegmentMask; michael@0: break; michael@0: case SkPath::kQuad_Verb: michael@0: mask |= SkPath::kQuad_SegmentMask; michael@0: break; michael@0: case SkPath::kConic_Verb: michael@0: mask |= SkPath::kConic_SegmentMask; michael@0: break; michael@0: case SkPath::kCubic_Verb: michael@0: mask |= SkPath::kCubic_SegmentMask; michael@0: break; michael@0: case SkPath::kClose_Verb: michael@0: break; michael@0: case SkPath::kDone_Verb: michael@0: SkDEBUGFAIL("Done verb shouldn't be recorded."); michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("Unknown Verb"); michael@0: break; michael@0: } michael@0: } michael@0: SkASSERT(mask == fSegmentMask); michael@0: #endif // SK_DEBUG_PATH michael@0: } michael@0: #endif