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