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/codec/SkPngDecoder.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkPicture.h"
15 #include "include/core/SkSerialProcs.h"
16 #include "include/core/SkStream.h"
17 #include "include/encode/SkPngEncoder.h"
18
19 namespace {
collectNonTextureImagesProc(SkImage * img,void * ctx)20 sk_sp<SkData> collectNonTextureImagesProc(SkImage* img, void* ctx) {
21 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
22 uint32_t originalId = img->uniqueID();
23 sk_sp<SkImage>* imageInMap = context->fNonTexMap.find(originalId);
24 if (!imageInMap) {
25 context->fNonTexMap[originalId] = img->makeNonTextureImage();
26 }
27
28 // This function implements a proc that is more generally for serialization, but
29 // we really only want to build our map. The output of this function is ignored.
30 return SkData::MakeEmpty();
31 }
32 }
33
collectNonTextureImagesFromPicture(const SkPicture * pic,SkSharingSerialContext * sharingCtx)34 void SkSharingSerialContext::collectNonTextureImagesFromPicture(
35 const SkPicture* pic, SkSharingSerialContext* sharingCtx) {
36 SkSerialProcs tempProc;
37 tempProc.fImageCtx = sharingCtx;
38 tempProc.fImageProc = collectNonTextureImagesProc;
39 SkNullWStream ns;
40 pic->serialize(&ns, &tempProc);
41 }
42
serializeImage(SkImage * img,void * ctx)43 sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
44 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
45 uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
46 // find out if we have already serialized this, and if so, what its in-file id is.
47 int* fid = context->fImageMap.find(id);
48 if (!fid) {
49 // When not present, add its id to the map and return its usual serialized form.
50 context->fImageMap[id] = context->fImageMap.count(); // Next in-file id
51 // encode the image or it's non-texture replacement if one was collected
52 sk_sp<SkImage>* replacementImage = context->fNonTexMap.find(id);
53 if (replacementImage) {
54 img = replacementImage->get();
55 }
56 return SkPngEncoder::Encode(nullptr, img, {});
57 }
58 // if present, return only the in-file id we registered the first time we serialized it.
59 return SkData::MakeWithCopy(fid, sizeof(*fid));
60 }
61
deserializeImage(const void * data,size_t length,void * ctx)62 sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
63 const void* data, size_t length, void* ctx) {
64 if (!data || !length || !ctx) {
65 SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n",
66 data, length, ctx);
67 // Return something so the rest of the debugger can proceed.
68 SkBitmap bm;
69 bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
70 return bm.asImage();
71 }
72 SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
73 uint32_t fid;
74 // If the data is an image fid, look up an already deserialized image from our map
75 if (length == sizeof(fid)) {
76 memcpy(&fid, data, sizeof(fid));
77 if (fid >= context->fImages.size()) {
78 SkDebugf("Cannot deserialize using id, We do not have the data for image %u.\n", fid);
79 return nullptr;
80 }
81 return context->fImages[fid];
82 }
83 // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
84 // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
85 sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
86 auto codec = SkPngDecoder::Decode(std::move(dataView), nullptr);
87 if (!codec) {
88 SkDebugf("Cannot deserialize image - might not be a PNG.\n");
89 return nullptr;
90 }
91 SkImageInfo info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
92 auto [image, result] = codec->getImage(info);
93 if (result != SkCodec::Result::kSuccess) {
94 SkDebugf("Error decoding image %d.\n", result);
95 // Might have partially decoded.
96 }
97 context->fImages.push_back(image);
98 return image;
99 }
100