convert pipe over to serial procs

Bug: skia:
Change-Id: I2c0482dd3ee1d806ff1054c486f73c705bcf2d72
Reviewed-on: https://skia-review.googlesource.com/85042
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
diff --git a/src/pipe/SkPipeCanvas.cpp b/src/pipe/SkPipeCanvas.cpp
index d53e40d..ee1e771 100644
--- a/src/pipe/SkPipeCanvas.cpp
+++ b/src/pipe/SkPipeCanvas.cpp
@@ -13,7 +13,6 @@
 #include "SkPathEffect.h"
 #include "SkPipeCanvas.h"
 #include "SkPipeFormat.h"
-#include "SkPixelSerializer.h"
 #include "SkRSXform.h"
 #include "SkRasterizer.h"
 #include "SkShader.h"
@@ -804,37 +803,13 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-class A8Serializer : public SkPixelSerializer {
-protected:
-    bool onUseEncodedData(const void* data, size_t len) {
-        return true;
-    }
-
-    SkData* onEncode(const SkPixmap& pmap) {
-        if (kAlpha_8_SkColorType == pmap.colorType()) {
-            SkDynamicMemoryWStream stream;
-            stream.write("skiaimgf", 8);
-            stream.write32(pmap.width());
-            stream.write32(pmap.height());
-            stream.write16(pmap.colorType());
-            stream.write16(pmap.alphaType());
-            stream.write32(0);  // no colorspace for now
-            for (int y = 0; y < pmap.height(); ++y) {
-                stream.write(pmap.addr8(0, y), pmap.width());
-            }
-            return stream.detachAsData().release();
+static sk_sp<SkData> encode(SkImage* img, SkSerialImageProc proc, void* ctx) {
+    if (proc) {
+        if (auto data = proc(img, ctx)) {
+            return data;
         }
-        return nullptr;
     }
-};
-
-static sk_sp<SkData> default_image_serializer(SkImage* image) {
-    A8Serializer serial;
-    sk_sp<SkData> data = image->encodeToData(&serial);
-    if (!data) {
-        data = image->encodeToData();
-    }
-    return data;
+    return img->encodeToData();
 }
 
 static bool show_deduper_traffic = false;
@@ -849,8 +824,7 @@
         return index;
     }
 
-    sk_sp<SkData> data = fIMSerializer ? fIMSerializer->serialize(image)
-                                       : default_image_serializer(image);
+    sk_sp<SkData> data = encode(image, fProcs.fImageProc, fProcs.fImageCtx);
     if (data) {
         index = fImages.add(image->uniqueID());
         SkASSERT(index > 0);
@@ -900,7 +874,13 @@
     return index;
 }
 
-static sk_sp<SkData> encode(SkTypeface* tf) {
+static sk_sp<SkData> encode(const SkSerialProcs& procs, SkTypeface* tf) {
+    if (procs.fTypefaceProc) {
+        auto data = procs.fTypefaceProc(tf, procs.fTypefaceCtx);
+        if (data) {
+            return data;
+        }
+    }
     SkDynamicMemoryWStream stream;
     tf->serialize(&stream);
     return sk_sp<SkData>(stream.detachAsData());
@@ -920,7 +900,7 @@
         return index;
     }
 
-    sk_sp<SkData> data = fTFSerializer ? fTFSerializer->serialize(typeface) : encode(typeface);
+    sk_sp<SkData> data = encode(fProcs, typeface);
     if (data) {
         index = fTypefaces.add(typeface->uniqueID());
         SkASSERT(index > 0);
@@ -987,14 +967,6 @@
     }
 }
 
-void SkPipeSerializer::setTypefaceSerializer(SkTypefaceSerializer* tfs) {
-    fImpl->fDeduper.setTypefaceSerializer(tfs);
-}
-
-void SkPipeSerializer::setImageSerializer(SkImageSerializer* ims) {
-    fImpl->fDeduper.setImageSerializer(ims);
-}
-
 void SkPipeSerializer::resetCache() {
     fImpl->fDeduper.resetCaches();
 }
diff --git a/src/pipe/SkPipeCanvas.h b/src/pipe/SkPipeCanvas.h
index dec346f..f48974a 100644
--- a/src/pipe/SkPipeCanvas.h
+++ b/src/pipe/SkPipeCanvas.h
@@ -62,8 +62,7 @@
 
     void setCanvas(SkPipeCanvas* canvas) { fPipeCanvas = canvas; }
     void setStream(SkWStream* stream) { fStream = stream; }
-    void setTypefaceSerializer(SkTypefaceSerializer* tfs) { fTFSerializer = tfs; }
-    void setImageSerializer(SkImageSerializer* ims) { fIMSerializer = ims; }
+    void setSerialProcs(const SkSerialProcs& procs) { fProcs = procs; }
 
     // returns 0 if not found
     int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); }
@@ -77,9 +76,7 @@
 private:
     SkPipeCanvas*           fPipeCanvas = nullptr;
     SkWStream*              fStream = nullptr;
-
-    SkTypefaceSerializer*   fTFSerializer = nullptr;
-    SkImageSerializer*      fIMSerializer = nullptr;
+    SkSerialProcs           fProcs;
 
     // All our keys (at the moment) are 32bit uniqueIDs
     SkTIndexSet<uint32_t>   fImages;
diff --git a/src/pipe/SkPipeReader.cpp b/src/pipe/SkPipeReader.cpp
index 3afb22e..8ce99ef 100644
--- a/src/pipe/SkPipeReader.cpp
+++ b/src/pipe/SkPipeReader.cpp
@@ -7,7 +7,6 @@
 
 #include "SkCanvas.h"
 #include "SkDeduper.h"
-#include "SkImageDeserializer.h"
 #include "SkPicture.h"
 #include "SkPictureRecorder.h"
 #include "SkPipe.h"
@@ -29,13 +28,12 @@
 public:
     SkPipeInflator(SkRefSet<SkImage>* images, SkRefSet<SkPicture>* pictures,
                    SkRefSet<SkTypeface>* typefaces, SkTDArray<SkFlattenable::Factory>* factories,
-                   SkTypefaceDeserializer* tfd, SkImageDeserializer* imd)
+                   const SkDeserialProcs& procs)
         : fImages(images)
         , fPictures(pictures)
         , fTypefaces(typefaces)
         , fFactories(factories)
-        , fTFDeserializer(tfd)
-        , fIMDeserializer(imd)
+        , fProcs(procs)
     {}
 
     SkImage* getImage(int index) override {
@@ -76,12 +74,8 @@
         return false;
     }
 
-    void setTypefaceDeserializer(SkTypefaceDeserializer* tfd) {
-        fTFDeserializer = tfd;
-    }
-
-    void setImageDeserializer(SkImageDeserializer* imd) {
-        fIMDeserializer = imd;
+    void setDeserialProcs(const SkDeserialProcs& procs) {
+        fProcs = procs;
     }
 
     sk_sp<SkTypeface> makeTypeface(const void* data, size_t size);
@@ -92,9 +86,7 @@
     SkRefSet<SkPicture>*                fPictures;
     SkRefSet<SkTypeface>*               fTypefaces;
     SkTDArray<SkFlattenable::Factory>*  fFactories;
-
-    SkTypefaceDeserializer*             fTFDeserializer;
-    SkImageDeserializer*                fIMDeserializer;
+    SkDeserialProcs                     fProcs;
 };
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -632,44 +624,11 @@
         }
 #endif
 
-static sk_sp<SkImage> make_from_skiaimageformat(const void* encoded, size_t encodedSize) {
-    if (encodedSize < 24) {
-        return nullptr;
-    }
-
-    SkMemoryStream stream(encoded, encodedSize);
-    char signature[8];
-    stream.read(signature, 8);
-    if (memcmp(signature, "skiaimgf", 8)) {
-        return nullptr;
-    }
-
-    int width = stream.readU32();
-    int height = stream.readU32();
-    SkColorType ct = (SkColorType)stream.readU16();
-    SkAlphaType at = (SkAlphaType)stream.readU16();
-    SkASSERT(kAlpha_8_SkColorType == ct);
-
-    SkDEBUGCODE(size_t colorSpaceSize =) stream.readU32();
-    SkASSERT(0 == colorSpaceSize);
-
-    SkImageInfo info = SkImageInfo::Make(width, height, ct, at);
-    size_t size = width * height;
-    sk_sp<SkData> pixels = SkData::MakeUninitialized(size);
-    stream.read(pixels->writable_data(), size);
-    SkASSERT(encodedSize == SkAlign4(stream.getPosition()));
-    return SkImage::MakeRasterData(info, pixels, width);
-}
-
 sk_sp<SkImage> SkPipeInflator::makeImage(const sk_sp<SkData>& data) {
-    if (fIMDeserializer) {
-        return fIMDeserializer->makeFromData(data.get(), nullptr);
+    if (fProcs.fImageProc) {
+        return fProcs.fImageProc(data->data(), data->size(), fProcs.fImageCtx);
     }
-    sk_sp<SkImage> image = make_from_skiaimageformat(data->data(), data->size());
-    if (!image) {
-        image = SkImage::MakeFromEncoded(data);
-    }
-    return image;
+    return SkImage::MakeFromEncoded(data);
 }
 
 
@@ -694,8 +653,8 @@
 }
 
 sk_sp<SkTypeface> SkPipeInflator::makeTypeface(const void* data, size_t size) {
-    if (fTFDeserializer) {
-        return fTFDeserializer->deserialize(data, size);
+    if (fProcs.fTypefaceProc) {
+        return fProcs.fTypefaceProc(data, size, fProcs.fTypefaceCtx);
     }
     SkMemoryStream stream(data, size, false);
     return SkTypeface::MakeDeserialize(&stream);
@@ -819,20 +778,14 @@
     SkRefSet<SkPicture>                 fPictures;
     SkRefSet<SkTypeface>                fTypefaces;
     SkTDArray<SkFlattenable::Factory>   fFactories;
-
-    SkTypefaceDeserializer*             fTFDeserializer = nullptr;
-    SkImageDeserializer*                fIMDeserializer = nullptr;
+    SkDeserialProcs                     fProcs;
 };
 
 SkPipeDeserializer::SkPipeDeserializer() : fImpl(new Impl) {}
 SkPipeDeserializer::~SkPipeDeserializer() {}
 
-void SkPipeDeserializer::setTypefaceDeserializer(SkTypefaceDeserializer* tfd) {
-    fImpl->fTFDeserializer = tfd;
-}
-
-void SkPipeDeserializer::setImageDeserializer(SkImageDeserializer* imd) {
-    fImpl->fIMDeserializer = imd;
+void SkPipeDeserializer::setDeserialProcs(const SkDeserialProcs& procs) {
+    fImpl->fProcs = procs;
 }
 
 sk_sp<SkImage> SkPipeDeserializer::readImage(const void* data, size_t size) {
@@ -848,7 +801,7 @@
     if (SkPipeVerb::kDefineImage == unpack_verb(packedVerb)) {
         SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
                                 &fImpl->fTypefaces, &fImpl->fFactories,
-                                fImpl->fTFDeserializer, fImpl->fIMDeserializer);
+                                fImpl->fProcs);
         SkPipeReader reader(this, ptr, size);
         reader.setInflator(&inflator);
         defineImage_handler(reader, packedVerb, nullptr);
@@ -878,7 +831,7 @@
     if (SkPipeVerb::kDefinePicture == unpack_verb(packedVerb)) {
         SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
                                 &fImpl->fTypefaces, &fImpl->fFactories,
-                                fImpl->fTFDeserializer, fImpl->fIMDeserializer);
+                                fImpl->fProcs);
         SkPipeReader reader(this, ptr, size);
         reader.setInflator(&inflator);
         definePicture_handler(reader, packedVerb, nullptr);
@@ -947,7 +900,7 @@
 bool SkPipeDeserializer::playback(const void* data, size_t size, SkCanvas* canvas) {
     SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
                             &fImpl->fTypefaces, &fImpl->fFactories,
-                            fImpl->fTFDeserializer, fImpl->fIMDeserializer);
+                            fImpl->fProcs);
     SkPipeReader reader(this, data, size);
     reader.setInflator(&inflator);
     return do_playback(reader, canvas);