blob: ee9b73facb563bed60c120e4415e80b81928c2f9 [file] [log] [blame]
Nathaniel Nifong0426c382019-06-21 11:09:19 -04001/*
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
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050010#include "include/core/SkBitmap.h"
Nathaniel Nifong0426c382019-06-21 11:09:19 -040011#include "include/core/SkData.h"
12#include "include/core/SkImage.h"
13#include "include/core/SkSerialProcs.h"
14
Nathaniel Nifong4a565682021-01-05 10:34:48 -050015namespace {
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
27void 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
Nathaniel Nifong0426c382019-06-21 11:09:19 -040036sk_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.
Nathaniel Nifong4a565682021-01-05 10:34:48 -050043 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 }
Nathaniel Nifong0426c382019-06-21 11:09:19 -040049 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
56sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
57 const void* data, size_t length, void* ctx) {
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050058 if (!data || !length || !ctx) {
John Stiles7bf79992021-06-25 11:05:20 -040059 SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n",
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050060 data, length, ctx);
Nathaniel Nifong4a565682021-01-05 10:34:48 -050061 // Return something so the rest of the debugger can proceed.
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050062 SkBitmap bm;
63 bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
Mike Reeddc607e32020-12-23 11:50:36 -050064 return bm.asImage();
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050065 }
Nathaniel Nifong0426c382019-06-21 11:09:19 -040066 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()) {
Nathaniel Nifong4a565682021-01-05 10:34:48 -050072 SkDebugf("Cannot deserialize using id, We do not have the data for image %d.\n", fid);
Nathaniel Nifong0426c382019-06-21 11:09:19 -040073 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}