simplify SkRRect serialization
authorMike Klein <mtklein@chromium.org>
Fri, 7 Apr 2017 02:34:59 +0000 (22:34 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Fri, 7 Apr 2017 13:36:54 +0000 (13:36 +0000)
I think this happens to fix the particular issue in the attached bug.
memcpy() is kind of the swiss army knife as far as strict aliasing is
concerned... you're always allowed to use it.

The generated code for writeToMemory() is unchanged, and
readFromMemory() gets a bit better looking.

BUG=skia:5105

Change-Id: Ib5bf96600f1138650c004ced2d696e9a4ba83ca7
Reviewed-on: https://skia-review.googlesource.com/11682
Reviewed-by: Cary Clark <caryclark@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>

src/core/SkRRect.cpp

index 2ed8bbf..824ee62 100644 (file)
@@ -456,10 +456,8 @@ void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
 ///////////////////////////////////////////////////////////////////////////////
 
 size_t SkRRect::writeToMemory(void* buffer) const {
-    SkASSERT(kSizeInMemory == sizeof(SkRect) + sizeof(fRadii));
-
-    memcpy(buffer, &fRect, sizeof(SkRect));
-    memcpy((char*)buffer + sizeof(SkRect), fRadii, sizeof(fRadii));
+    // Serialize only the rect and corners, but not the derived type tag.
+    memcpy(buffer, this, kSizeInMemory);
     return kSizeInMemory;
 }
 
@@ -468,14 +466,10 @@ size_t SkRRect::readFromMemory(const void* buffer, size_t length) {
         return 0;
     }
 
-    SkScalar storage[12];
-    SkASSERT(sizeof(storage) == kSizeInMemory);
-
-    // we make a local copy, to ensure alignment before we cast
-    memcpy(storage, buffer, kSizeInMemory);
+    // Deserialize rect and corners, then rederive the type tag.
+    memcpy(this, buffer, kSizeInMemory);
+    this->computeType();
 
-    this->setRectRadii(*(const SkRect*)&storage[0],
-                       (const SkVector*)&storage[4]);
     return kSizeInMemory;
 }