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