Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Unified Diff: src/core/SkPathRef.h

Issue 24350006: Move bound and isFinite into pathref (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: yet more cleanup Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkPathRef.h
===================================================================
--- src/core/SkPathRef.h (revision 11438)
+++ src/core/SkPathRef.h (working copy)
@@ -129,6 +129,40 @@
}
/**
+ * Returns true if all of the points in this path are finite, meaning there
+ * are no infinities and no NaNs.
+ */
+ bool isFinite() const {
+ if (fBoundsIsDirty) {
+ this->computeBounds();
+ }
+ return SkToBool(fIsFinite);
+ }
+
+ bool hasComputedBounds() const {
+ return !fBoundsIsDirty;
+ }
+
+ /** Returns the bounds of the path's points. If the path contains 0 or 1
+ points, the bounds is set to (0,0,0,0), and isEmpty() will return true.
+ Note: this bounds may be larger than the actual shape, since curves
+ do not extend as far as their control points.
+ */
+ const SkRect& getBounds() const {
+ if (fBoundsIsDirty) {
+ this->computeBounds();
+ }
+ return fBounds;
+ }
+
+ void setBounds(const SkRect& rect) {
+ SkASSERT(rect.fLeft <= rect.fRight && rect.fTop <= rect.fBottom);
+ fBounds = rect;
+ fBoundsIsDirty = false;
+ fIsFinite = fBounds.isFinite();
+ }
+
+ /**
* Transforms a path ref by a matrix, allocating a new one only if necessary.
*/
static void CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
@@ -143,22 +177,66 @@
}
return;
}
+
bool dstUnique = (*dst)->unique();
- if (&src == *dst && dstUnique) {
- matrix.mapPoints((*dst)->fPoints, (*dst)->fPointCnt);
- return;
- } else if (!dstUnique) {
+ if (!dstUnique) {
dst->reset(SkNEW(SkPathRef));
+ (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
+ memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
+ (*dst)->fConicWeights = src.fConicWeights;
}
- (*dst)->resetToSize(src.fVerbCnt, src.fPointCnt, src.fConicWeights.count());
- memcpy((*dst)->verbsMemWritable(), src.verbsMemBegin(), src.fVerbCnt * sizeof(uint8_t));
+
+ // Need to check this here in case (&src == dst)
+ bool canXformBounds = !src.fBoundsIsDirty && matrix.rectStaysRect() && src.countPoints() > 1;
+
matrix.mapPoints((*dst)->fPoints, src.points(), src.fPointCnt);
- (*dst)->fConicWeights = src.fConicWeights;
+
+ /*
+ * Here we optimize the bounds computation, by noting if the bounds are
+ * already known, and if so, we just transform those as well and mark
+ * them as "known", rather than force the transformed path to have to
+ * recompute them.
+ *
+ * Special gotchas if the path is effectively empty (<= 1 point) or
+ * if it is non-finite. In those cases bounds need to stay empty,
+ * regardless of the matrix.
+ */
+ if (canXformBounds) {
+ (*dst)->fBoundsIsDirty = false;
+ if (src.fIsFinite) {
+ matrix.mapRect(&(*dst)->fBounds, src.fBounds);
+ if (!((*dst)->fIsFinite = (*dst)->fBounds.isFinite())) {
+ (*dst)->fBounds.setEmpty();
+ }
+ } else {
+ (*dst)->fIsFinite = false;
+ (*dst)->fBounds.setEmpty();
+ }
+ } else {
+ (*dst)->fBoundsIsDirty = true;
+ }
+
(*dst)->validate();
}
- static SkPathRef* CreateFromBuffer(SkRBuffer* buffer) {
+ static SkPathRef* CreateFromBuffer(SkRBuffer* buffer
+#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO
+ , bool newFormat, int32_t oldPacked
+#endif
+ ) {
SkPathRef* ref = SkNEW(SkPathRef);
+#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO
+ if (newFormat) {
+#endif
+ int32_t packed = buffer->readU32();
+
+ ref->fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
+#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO
+ } else {
+ ref->fIsFinite = (oldPacked >> SkPath::kOldIsFinite_SerializationShift) & 1;
+ }
+#endif
+
ref->fGenerationID = buffer->readU32();
int32_t verbCount = buffer->readS32();
int32_t pointCount = buffer->readS32();
@@ -171,6 +249,8 @@
buffer->read(ref->verbsMemWritable(), verbCount * sizeof(uint8_t));
buffer->read(ref->fPoints, pointCount * sizeof(SkPoint));
buffer->read(ref->fConicWeights.begin(), conicCount * sizeof(SkScalar));
+ buffer->read(&ref->fBounds, sizeof(SkRect));
+ ref->fBoundsIsDirty = false;
return ref;
}
@@ -182,6 +262,7 @@
static void Rewind(SkAutoTUnref<SkPathRef>* pathRef) {
if ((*pathRef)->unique()) {
(*pathRef)->validate();
+ (*pathRef)->fBoundsIsDirty = true; // this also invalidates fIsFinite
(*pathRef)->fVerbCnt = 0;
(*pathRef)->fPointCnt = 0;
(*pathRef)->fFreeSpace = (*pathRef)->currSize();
@@ -294,6 +375,13 @@
this->validate();
SkDEBUGCODE(size_t beforePos = buffer->pos();)
+ // Call getBounds() to ensure (as a side-effect) that fBounds
+ // and fIsFinite are computed.
+ const SkRect& bounds = this->getBounds();
+
+ int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift);
+ buffer->write32(packed);
+
// TODO: write gen ID here. Problem: We don't know if we're cross process or not from
// SkWBuffer. Until this is fixed we write 0.
buffer->write32(0);
@@ -303,6 +391,7 @@
buffer->write(verbsMemBegin(), fVerbCnt * sizeof(uint8_t));
buffer->write(fPoints, fPointCnt * sizeof(SkPoint));
buffer->write(fConicWeights.begin(), fConicWeights.bytes());
+ buffer->write(&bounds, sizeof(bounds));
SkASSERT(buffer->pos() - beforePos == (size_t) this->writeSize());
}
@@ -311,14 +400,20 @@
* Gets the number of bytes that would be written in writeBuffer()
*/
uint32_t writeSize() {
- return 4 * sizeof(uint32_t) +
+ return 5 * sizeof(uint32_t) +
fVerbCnt * sizeof(uint8_t) +
fPointCnt * sizeof(SkPoint) +
- fConicWeights.bytes();
+ fConicWeights.bytes() +
+ sizeof(SkRect);
}
private:
+ enum SerializationOffsets {
+ kIsFinite_SerializationShift = 25, // requires 1 bit
+ };
+
SkPathRef() {
+ fBoundsIsDirty = true; // this also invalidates fIsFinite
fPointCnt = 0;
fVerbCnt = 0;
fVerbs = NULL;
@@ -339,9 +434,34 @@
// We could call genID() here to force a real ID (instead of 0). However, if we're making
// a copy then presumably we intend to make a modification immediately afterwards.
fGenerationID = ref.fGenerationID;
+ fBoundsIsDirty = ref.fBoundsIsDirty;
+ if (!fBoundsIsDirty) {
+ fBounds = ref.fBounds;
+ fIsFinite = ref.fIsFinite;
+ }
this->validate();
}
+ // Return true if the computed bounds are finite.
+ static bool ComputePtBounds(SkRect* bounds, const SkPathRef& ref) {
+ int count = ref.countPoints();
+ if (count <= 1) { // we ignore just 1 point (moveto)
+ bounds->setEmpty();
+ return count ? ref.points()->isFinite() : true;
+ } else {
+ return bounds->setBoundsCheck(ref.points(), count);
+ }
+ }
+
+ // called, if dirty, by getBounds()
+ void computeBounds() const {
+ SkDEBUGCODE(this->validate();)
+ SkASSERT(fBoundsIsDirty);
+
+ fIsFinite = ComputePtBounds(&fBounds, *this);
+ fBoundsIsDirty = false;
+ }
+
/** Makes additional room but does not change the counts or change the genID */
void incReserve(int additionalVerbs, int additionalPoints) {
this->validate();
@@ -350,11 +470,12 @@
this->validate();
}
- /** Resets the path ref with verbCount verbs and pointCount points, all unitialized. Also
+ /** Resets the path ref with verbCount verbs and pointCount points, all uninitialized. Also
* allocates space for reserveVerb additional verbs and reservePoints additional points.*/
void resetToSize(int verbCount, int pointCount, int conicCount,
int reserveVerbs = 0, int reservePoints = 0) {
this->validate();
+ fBoundsIsDirty = true; // this also invalidates fIsFinite
fGenerationID = 0;
size_t newSize = sizeof(uint8_t) * verbCount + sizeof(SkPoint) * pointCount;
@@ -394,6 +515,7 @@
fVerbCnt += newVerbs;
fPointCnt += newPoints;
fFreeSpace -= space;
+ fBoundsIsDirty = true; // this also invalidates fIsFinite
this->validate();
}
@@ -437,6 +559,7 @@
fVerbCnt += 1;
fPointCnt += pCnt;
fFreeSpace -= space;
+ fBoundsIsDirty = true; // this also invalidates fIsFinite
this->validate();
return ret;
}
@@ -524,12 +647,30 @@
SkASSERT(!(NULL == fVerbs && fVerbCnt));
SkASSERT(this->currSize() ==
fFreeSpace + sizeof(SkPoint) * fPointCnt + sizeof(uint8_t) * fVerbCnt);
+
+#ifdef SK_DEBUG
+ if (!fBoundsIsDirty && !fBounds.isEmpty()) {
+ bool isFinite = true;
+ for (int i = 0; i < fPointCnt; ++i) {
+ SkASSERT(fPoints[i].fX >= fBounds.fLeft && fPoints[i].fX <= fBounds.fRight &&
+ fPoints[i].fY >= fBounds.fTop && fPoints[i].fY <= fBounds.fBottom);
+ if (!fPoints[i].isFinite()) {
+ isFinite = false;
+ }
+ }
+ SkASSERT(SkToBool(fIsFinite) == isFinite);
+ }
+#endif
}
enum {
kMinSize = 256,
};
+ mutable SkRect fBounds;
+ mutable uint8_t fBoundsIsDirty;
+ mutable SkBool8 fIsFinite; // only meaningful if bounds are valid
+
SkPoint* fPoints; // points to begining of the allocation
uint8_t* fVerbs; // points just past the end of the allocation (verbs grow backwards)
int fVerbCnt;
« include/core/SkPath.h ('K') | « src/core/SkPath.cpp ('k') | src/core/SkPicture.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698