Reland "Deserialize MultiPictureDocument based SKP files (with image sharing proc) in wasm debugger."
This is a reland of 7635013ad16a60cfa5ff93af28c5fab8927c92ce
Original change's description:
> Deserialize MultiPictureDocument based SKP files (with image sharing proc) in wasm debugger.
>
> Change-Id: I73affae3cd05a2aa6ac1c75c8e049d352bbf3a85
> Bug: 9176
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/217135
> Commit-Queue: Nathaniel Nifong <nifong@google.com>
> Reviewed-by: Derek Sollenberger <djsollen@google.com>
> Reviewed-by: Kevin Lubick <kjlubick@google.com>
Bug: 9176
Change-Id: Ifef1ff45ac0013ba3015f88c7ecd75527b28b604
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/222505
Commit-Queue: Nathaniel Nifong <nifong@google.com>
Reviewed-by: Derek Sollenberger <djsollen@google.com>
diff --git a/tools/SkSharingProc.cpp b/tools/SkSharingProc.cpp
new file mode 100644
index 0000000..803f766
--- /dev/null
+++ b/tools/SkSharingProc.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "tools/SkSharingProc.h"
+
+#include "include/core/SkData.h"
+#include "include/core/SkImage.h"
+#include "include/core/SkSerialProcs.h"
+
+sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
+ SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
+ uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
+ // find out if we have already serialized this, and if so, what its in-file id is.
+ auto iter = context->fImageMap.find(id);
+ if (iter == context->fImageMap.end()) {
+ // When not present, add its id to the map and return its usual serialized form.
+ context->fImageMap[id] = context->fImageMap.size();
+ return img->encodeToData();
+ }
+ uint32_t fid = context->fImageMap[id];
+ // if present, return only the in-file id we registered the first time we serialized it.
+ return SkData::MakeWithCopy(&fid, sizeof(fid));
+}
+
+sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
+ const void* data, size_t length, void* ctx) {
+ SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
+ uint32_t fid;
+ // If the data is an image fid, look up an already deserialized image from our map
+ if (length == sizeof(fid)) {
+ memcpy(&fid, data, sizeof(fid));
+ if (fid >= context->fImages.size()) {
+ SkDebugf("We do not have the data for image %d.\n", fid);
+ return nullptr;
+ }
+ return context->fImages[fid];
+ }
+ // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
+ // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
+ sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
+ const sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(dataView));
+ context->fImages.push_back(image);
+ return image;
+}
diff --git a/tools/SkSharingProc.h b/tools/SkSharingProc.h
new file mode 100644
index 0000000..12a7b80
--- /dev/null
+++ b/tools/SkSharingProc.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkSharingProc_DEFINED
+#define SkSharingProc_DEFINED
+
+#include <unordered_map>
+#include <vector>
+
+#include "include/core/SkData.h"
+#include "include/core/SkImage.h"
+#include "include/core/SkSerialProcs.h"
+
+struct SkSharingSerialContext {
+ // A map from the ids from SkImage::uniqueID() to ids used within the file
+ std::unordered_map<uint32_t, uint32_t> fImageMap;
+
+ // A serial proc that shares images between subpictures
+ // To use this, create an instance of SkSerialProcs and populate it this way.
+ // The client must retain ownership of the context.
+ // auto ctx = std::make_unique<SkSharingSerialContext>()
+ // SkSerialProcs procs;
+ // procs.fImageProc = SkSharingSerialContext::serializeImage;
+ // procs.fImageCtx = ctx.get();
+ static sk_sp<SkData> serializeImage(SkImage* img, void* ctx);
+};
+
+struct SkSharingDeserialContext {
+ // a list of unique images in the order they were encountered in the file
+ // Subsequent occurrences of an image refer to it by it's index in this list.
+ std::vector<sk_sp<SkImage>> fImages;
+
+ // A deserial proc that can interpret id's in place of images as references to previous images.
+ // Can also deserialize a SKP where all images are inlined (it's backwards compatible)
+ static sk_sp<SkImage> deserializeImage(const void* data, size_t length, void* ctx);
+};
+
+#endif