Flutter Engine
The Flutter Engine
SkSharingProc.cpp
Go to the documentation of this file.
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
9
12#include "include/core/SkData.h"
18
19namespace {
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
35 const SkPicture* pic, SkSharingSerialContext* sharingCtx) {
36 SkSerialProcs tempProc;
37 tempProc.fImageCtx = sharingCtx;
38 tempProc.fImageProc = collectNonTextureImagesProc;
40 pic->serialize(&ns, &tempProc);
41}
42
44 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
45 uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
46 // find out if we have already serialized this, and if so, what its in-file id is.
47 int* fid = context->fImageMap.find(id);
48 if (!fid) {
49 // When not present, add its id to the map and return its usual serialized form.
50 context->fImageMap[id] = context->fImageMap.count(); // Next in-file id
51 // encode the image or it's non-texture replacement if one was collected
52 sk_sp<SkImage>* replacementImage = context->fNonTexMap.find(id);
53 if (replacementImage) {
54 img = replacementImage->get();
55 }
56 return SkPngEncoder::Encode(nullptr, img, {});
57 }
58 // if present, return only the in-file id we registered the first time we serialized it.
59 return SkData::MakeWithCopy(fid, sizeof(*fid));
60}
61
63 const void* data, size_t length, void* ctx) {
64 if (!data || !length || !ctx) {
65 SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n",
66 data, length, ctx);
67 // Return something so the rest of the debugger can proceed.
68 SkBitmap bm;
70 return bm.asImage();
71 }
72 SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
73 uint32_t fid;
74 // If the data is an image fid, look up an already deserialized image from our map
75 if (length == sizeof(fid)) {
76 memcpy(&fid, data, sizeof(fid));
77 if (fid >= context->fImages.size()) {
78 SkDebugf("Cannot deserialize using id, We do not have the data for image %u.\n", fid);
79 return nullptr;
80 }
81 return context->fImages[fid];
82 }
83 // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
84 // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
86 auto codec = SkPngDecoder::Decode(std::move(dataView), nullptr);
87 if (!codec) {
88 SkDebugf("Cannot deserialize image - might not be a PNG.\n");
89 return nullptr;
90 }
91 SkImageInfo info = codec->getInfo().makeAlphaType(kPremul_SkAlphaType);
92 auto [image, result] = codec->getImage(info);
94 SkDebugf("Error decoding image %d.\n", result);
95 // Might have partially decoded.
96 }
97 context->fImages.push_back(image);
98 return image;
99}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94
uint32_t uniqueID() const
Definition: SkImage.h:311
sk_sp< SkImage > makeNonTextureImage(GrDirectContext *=nullptr) const
Definition: SkImage.cpp:260
sk_sp< SkData > serialize(const SkSerialProcs *procs=nullptr) const
Definition: SkPicture.cpp:249
int count() const
Definition: SkTHash.h:471
V * find(const K &key) const
Definition: SkTHash.h:494
@ kSuccess
Definition: embedder.h:73
GAsyncResult * result
size_t length
SK_API std::unique_ptr< SkCodec > Decode(std::unique_ptr< SkStream >, SkCodec::Result *, SkCodecs::DecodeContext=nullptr)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
static SkImageInfo MakeN32Premul(int width, int height)
void * fImageCtx
Definition: SkSerialProcs.h:91
SkSerialImageProc fImageProc
Definition: SkSerialProcs.h:90
std::vector< sk_sp< SkImage > > fImages
Definition: SkSharingProc.h:69
static sk_sp< SkImage > deserializeImage(const void *data, size_t length, void *ctx)
static sk_sp< SkData > serializeImage(SkImage *img, void *ctx)
static void collectNonTextureImagesFromPicture(const SkPicture *pic, SkSharingSerialContext *sharingCtx)
skia_private::THashMap< uint32_t, sk_sp< SkImage > > fNonTexMap
Definition: SkSharingProc.h:42
skia_private::THashMap< uint32_t, int > fImageMap
Definition: SkSharingProc.h:54
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
const uintptr_t id