return data instead of bool

Bug: skia:
Change-Id: Id7c3044a2fce087703544c624c75a7756b9c62b4
Reviewed-on: https://skia-review.googlesource.com/82602
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/include/core/SkSerialProcs.h b/include/core/SkSerialProcs.h
index d55d1a8..e1f4c61 100644
--- a/include/core/SkSerialProcs.h
+++ b/include/core/SkSerialProcs.h
@@ -13,16 +13,14 @@
 #include "SkTypeface.h"
 
 /**
- *  A serial-proc is asked to serialize the specified object (e.g. picture or image), by writing
- *  its serialized form into the specified stream. If the proc does this, it returns true.
- *
- *  If the proc chooses to have Skia perform its default action, it ignores the stream parameter
- *  and just returns false.
+ *  A serial-proc is asked to serialize the specified object (e.g. picture or image).
+ *  If a data object is returned, it will be used (even if it is zero-length).
+ *  If null is returned, then Skia will take its default action.
  */
 
-typedef bool (*SkSerialPictureProc)(SkPicture*, SkWStream*, void* ctx);
-typedef bool (*SkSerialImageProc)(SkImage*, SkWStream*, void* ctx);
-typedef bool (*SkSerialTypefaceProc)(SkTypeface*, SkWStream*, void* ctx);
+typedef sk_sp<SkData> (*SkSerialPictureProc)(SkPicture*, void* ctx);
+typedef sk_sp<SkData> (*SkSerialImageProc)(SkImage*, void* ctx);
+typedef sk_sp<SkData> (*SkSerialTypefaceProc)(SkTypeface*, void* ctx);
 
 /**
  *  A deserial-proc is given the serialized form previously returned by the corresponding
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp
index e87439a..5442738 100644
--- a/src/core/SkPicture.cpp
+++ b/src/core/SkPicture.cpp
@@ -273,14 +273,9 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool PixelSerializer_SkSerialImageProc(SkImage* img, SkWStream* stream, void* ctx) {
+sk_sp<SkData> PixelSerializer_SkSerialImageProc(SkImage* img, void* ctx) {
     SkASSERT(ctx);
-    sk_sp<SkData> enc = img->encodeToData(static_cast<SkPixelSerializer*>(ctx));
-    if (enc) {
-        stream->write(enc->data(), enc->size());
-        return true;
-    }
-    return false;
+    return img->encodeToData(static_cast<SkPixelSerializer*>(ctx));
 }
 
 sk_sp<SkImage> ImageDeserializer_SkDeserialImageProc(const void* data, size_t length, void* ctx) {
diff --git a/src/core/SkPictureCommon.h b/src/core/SkPictureCommon.h
index 74d693f..c282f01 100644
--- a/src/core/SkPictureCommon.h
+++ b/src/core/SkPictureCommon.h
@@ -142,7 +142,7 @@
     int fNumSlowPathsAndDashEffects;
 };
 
-bool PixelSerializer_SkSerialImageProc(SkImage*, SkWStream*, void* sk_pixelserializer);
+sk_sp<SkData> PixelSerializer_SkSerialImageProc(SkImage*, void* pixelserializer);
 sk_sp<SkImage> ImageDeserializer_SkDeserialImageProc(const void*, size_t, void* imagedeserializer);
 
 #endif  // SkPictureCommon_DEFINED
diff --git a/src/core/SkWriteBuffer.cpp b/src/core/SkWriteBuffer.cpp
index 49cccd6..bd1d64b 100644
--- a/src/core/SkWriteBuffer.cpp
+++ b/src/core/SkWriteBuffer.cpp
@@ -140,17 +140,18 @@
     this->writeInt(image->height());
 
     auto write_data = [this](sk_sp<SkData> data, int sign) {
-        if (data) {
-            size_t size = data->size();
-            if (size && sk_64_isS32(size)) {
-                this->write32(SkToS32(size) * sign);
-                this->writePad32(data->data(), size);    // does nothing if size == 0
-                this->write32(0);   // origin-x
-                this->write32(0);   // origin-y
-                return;
-            }
+        size_t size = data ? data->size() : 0;
+        if (!sk_64_isS32(size)) {
+            size = 0;   // too big to store
         }
-        this->write32(0);   // no data or size too big
+        if (size) {
+            this->write32(SkToS32(size) * sign);
+            this->writePad32(data->data(), size);    // does nothing if size == 0
+            this->write32(0);   // origin-x
+            this->write32(0);   // origin-y
+        } else {
+            this->write32(0);   // signal no image
+        }
     };
 
     /*
@@ -159,14 +160,19 @@
      *  <0 : negative (int32_t) of a custom encoded blob using SerialProcs
      *  >0 : standard encoded blob size (use MakeFromEncoded)
      */
+    sk_sp<SkData> data;
+    int sign = 1;   // +1 signals standard encoder
     if (fProcs.fImageProc) {
-        SkDynamicMemoryWStream stream;
-        if (fProcs.fImageProc(const_cast<SkImage*>(image), &stream, fProcs.fImageCtx)) {
-            write_data(stream.detachAsData(), -1);  // -1 signals custom encoder
-            return;
-        }
+        data = fProcs.fImageProc(const_cast<SkImage*>(image), fProcs.fImageCtx);
+        sign = -1;  // +1 signals custom encoder
     }
-    write_data(image->encodeToData(), 1);   // +1 signals standard encoder
+    // We check data, since a custom proc can return nullptr, in which case we behave as if
+    // there was no custom proc.
+    if (!data) {
+        data = image->encodeToData();
+        sign = 1;
+    }
+    write_data(std::move(data), sign);
 }
 
 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) {
diff --git a/tests/SerialProcsTest.cpp b/tests/SerialProcsTest.cpp
index 7368a20..6361309 100644
--- a/tests/SerialProcsTest.cpp
+++ b/tests/SerialProcsTest.cpp
@@ -31,31 +31,21 @@
     const char magic_str[] = "magic signature";
 
     const SkSerialImageProc sprocs[] = {
-        [](SkImage* img, SkWStream* stream, void* ctx) {
-            return false;
-        },
-        [](SkImage* img, SkWStream* stream, void* ctx) {
-            auto d = img->encodeToData();
-            stream->write(d->data(), d->size());
-            return true;
-        },
-        [](SkImage* img, SkWStream* stream, void* ctx) {
-            State* state = (State*)ctx;
-            stream->write(state->fStr, strlen(state->fStr));
-            return true;
-        },
+        [](SkImage* img, void* ctx) -> sk_sp<SkData> { return nullptr; },
+        [](SkImage* img, void* ctx) { return img->encodeToData(); },
+        [](SkImage* img, void* ctx) { return SkData::MakeWithCString(((State*)ctx)->fStr); },
     };
     const SkDeserialImageProc dprocs[] = {
         [](const void* data, size_t length, void*) -> sk_sp<SkImage> {
             SK_ABORT("should not get called");
             return nullptr;
         },
-        [](const void* data, size_t length, void*) -> sk_sp<SkImage> {
+        [](const void* data, size_t length, void*) {
             return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length));
         },
         [](const void* data, size_t length, void* ctx) -> sk_sp<SkImage> {
             State* state = (State*)ctx;
-            if (length != strlen(state->fStr) || memcmp(data, state->fStr, length)) {
+            if (length != strlen(state->fStr)+1 || memcmp(data, state->fStr, length)) {
                 return nullptr;
             }
             return sk_ref_sp(state->fImg);