Added Serialization of SkPath's bound
authorrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 17 Aug 2012 10:58:49 +0000 (10:58 +0000)
committerrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 17 Aug 2012 10:58:49 +0000 (10:58 +0000)
http://codereview.appspot.com/6458143/

git-svn-id: http://skia.googlecode.com/svn/trunk@5143 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkPath.h
src/core/SkPath.cpp
src/core/SkPicture.cpp

index ae8ecb7..fe98336 100644 (file)
@@ -824,6 +824,14 @@ public:
     SkDEBUGCODE(void validate() const;)
 
 private:
+    enum SerializationOffsets {
+        kIsFinite_SerializationShift = 25,
+        kIsOval_SerializationShift = 24,
+        kConvexity_SerializationShift = 16,\r
+        kFillType_SerializationShift = 8,\r
+        kSegmentMask_SerializationShift = 0
+    };
+
     SkTDArray<SkPoint>  fPts;
     SkTDArray<uint8_t>  fVerbs;
     mutable SkRect      fBounds;
index 4be1c98..674ba98 100644 (file)
@@ -1721,18 +1721,33 @@ uint32_t SkPath::writeToMemory(void* storage) const {
     if (NULL == storage) {
         const int byteCount = 3 * sizeof(int32_t)
                       + sizeof(SkPoint) * fPts.count()
-                      + sizeof(uint8_t) * fVerbs.count();
+                      + sizeof(uint8_t) * fVerbs.count()
+                      + sizeof(SkRect);
         return SkAlign4(byteCount);
     }
 
     SkWBuffer   buffer(storage);
     buffer.write32(fPts.count());
     buffer.write32(fVerbs.count());
-    int32_t packed = (fIsOval << 24) | (fConvexity << 16) | 
-                     (fFillType << 8) | fSegmentMask;
+
+    // Call getBounds() to ensure (as a side-effect) that fBounds
+    // and fIsFinite are computed.
+    const SkRect& bounds = this->getBounds();
+    SkASSERT(!fBoundsIsDirty);
+
+    int32_t packed = ((fIsFinite & 1) << kIsFinite_SerializationShift) |
+                     ((fIsOval & 1) << kIsOval_SerializationShift) |
+                     (fConvexity << kConvexity_SerializationShift) |
+                     (fFillType << kFillType_SerializationShift) |
+                     (fSegmentMask << kSegmentMask_SerializationShift);
+
     buffer.write32(packed);
+
     buffer.write(fPts.begin(), sizeof(SkPoint) * fPts.count());
     buffer.write(fVerbs.begin(), fVerbs.count());
+
+    buffer.write(&bounds, sizeof(bounds));
+
     buffer.padToAlign4();
     return buffer.pos();
 }
@@ -1741,18 +1756,23 @@ uint32_t SkPath::readFromMemory(const void* storage) {
     SkRBuffer   buffer(storage);
     fPts.setCount(buffer.readS32());
     fVerbs.setCount(buffer.readS32());
+
     uint32_t packed = buffer.readS32();
-    fFillType = (packed >> 8) & 0xFF;
-    fSegmentMask = packed & 0xFF;
+    fIsFinite = (packed >> kIsFinite_SerializationShift) & 1;
+    fIsOval = (packed >> kIsOval_SerializationShift) & 1;
+    fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF;
+    fFillType = (packed >> kFillType_SerializationShift) & 0xFF;
+    fSegmentMask = (packed >> kSegmentMask_SerializationShift) & 0xFF;
+
     buffer.read(fPts.begin(), sizeof(SkPoint) * fPts.count());
     buffer.read(fVerbs.begin(), fVerbs.count());
+
+    buffer.read(&fBounds, sizeof(fBounds));
+    fBoundsIsDirty = false;
+
     buffer.skipToAlign4();
 
     GEN_ID_INC;
-    DIRTY_AFTER_EDIT;
-    // DIRTY_AFTER_EDIT resets fIsOval and fConvexity
-    fIsOval =    packed >> 24;
-    fConvexity = (packed >> 16) & 0xFF;
 
     SkDEBUGCODE(this->validate();)
     return buffer.pos();
index 9f606f7..70e329e 100644 (file)
@@ -200,7 +200,8 @@ void SkPicture::draw(SkCanvas* surface) {
 // V3 : PictInfo tag at beginning, and EOF tag at the end
 // V4 : move SkPictInfo to be the header
 // V5 : don't read/write FunctionPtr on cross-process (we can detect that)
-#define PICTURE_VERSION     5
+// V6 : added serialization of SkPath's bounds (and packed its flags tighter)
+#define PICTURE_VERSION     6
 
 SkPicture::SkPicture(SkStream* stream) : SkRefCnt() {
     fRecord = NULL;