1 /*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/SkSharingProc.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkSerialProcs.h"
14 #include "include/core/SkStream.h"
15
16 namespace {
collectNonTextureImagesProc(SkImage * img,void * ctx)17 sk_sp<SkData> collectNonTextureImagesProc(SkImage* img, void* ctx) {
18 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
19 uint32_t originalId = img->uniqueID();
20 auto it = context->fNonTexMap.find(originalId);
21 if (it == context->fNonTexMap.end()) {
22 context->fNonTexMap[originalId] = img->makeNonTextureImage();
23 }
24 return SkData::MakeEmpty();
25 }
26 }
27
collectNonTextureImagesFromPicture(const SkPicture * pic,SkSharingSerialContext * sharingCtx)28 void SkSharingSerialContext::collectNonTextureImagesFromPicture(
29 const SkPicture* pic, SkSharingSerialContext* sharingCtx) {
30 SkSerialProcs tempProc;
31 tempProc.fImageCtx = sharingCtx;
32 tempProc.fImageProc = collectNonTextureImagesProc;
33 SkNullWStream ns;
34 pic->serialize(&ns, &tempProc);
35 }
36
serializeImage(SkImage * img,void * ctx)37 sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
38 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
39 uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
40 // find out if we have already serialized this, and if so, what its in-file id is.
41 auto iter = context->fImageMap.find(id);
42 if (iter == context->fImageMap.end()) {
43 // When not present, add its id to the map and return its usual serialized form.
44 context->fImageMap[id] = context->fImageMap.size(); // Next in-file id
45 // encode the image or it's non-texture replacement if one was collected
46 auto iter2 = context->fNonTexMap.find(id);
47 if (iter2 != context->fNonTexMap.end()) {
48 img = iter2->second.get();
49 }
50 return img->encodeToData();
51 }
52 uint32_t fid = context->fImageMap[id];
53 // if present, return only the in-file id we registered the first time we serialized it.
54 return SkData::MakeWithCopy(&fid, sizeof(fid));
55 }
56
deserializeImage(const void * data,size_t length,void * ctx)57 sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
58 const void* data, size_t length, void* ctx) {
59 if (!data || !length || !ctx) {
60 SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n",
61 data, length, ctx);
62 // Return something so the rest of the debugger can proceed.
63 SkBitmap bm;
64 bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
65 return bm.asImage();
66 }
67 SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
68 uint32_t fid;
69 // If the data is an image fid, look up an already deserialized image from our map
70 if (length == sizeof(fid)) {
71 memcpy(&fid, data, sizeof(fid));
72 if (fid >= context->fImages.size()) {
73 SkDebugf("Cannot deserialize using id, We do not have the data for image %d.\n", fid);
74 return nullptr;
75 }
76 return context->fImages[fid];
77 }
78 // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
79 // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
80 sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
81 const sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(dataView));
82 context->fImages.push_back(image);
83 return image;
84 }
85