• 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/SkData.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkSerialProcs.h"
13 
serializeImage(SkImage * img,void * ctx)14 sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
15     SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
16     uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
17     // find out if we have already serialized this, and if so, what its in-file id is.
18     auto iter = context->fImageMap.find(id);
19     if (iter == context->fImageMap.end()) {
20         // When not present, add its id to the map and return its usual serialized form.
21         context->fImageMap[id] = context->fImageMap.size();
22         return img->encodeToData();
23     }
24     uint32_t fid = context->fImageMap[id];
25     // if present, return only the in-file id we registered the first time we serialized it.
26     return SkData::MakeWithCopy(&fid, sizeof(fid));
27 }
28 
deserializeImage(const void * data,size_t length,void * ctx)29 sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
30   const void* data, size_t length, void* ctx) {
31     SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
32     uint32_t fid;
33     // If the data is an image fid, look up an already deserialized image from our map
34     if (length == sizeof(fid)) {
35         memcpy(&fid, data, sizeof(fid));
36         if (fid >= context->fImages.size()) {
37             SkDebugf("We do not have the data for image %d.\n", fid);
38             return nullptr;
39         }
40         return context->fImages[fid];
41     }
42     // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
43     // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
44     sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
45     const sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(dataView));
46     context->fImages.push_back(image);
47     return image;
48 }
49