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