2019-06-21 15:09:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tools/SkSharingProc.h"
|
|
|
|
|
2020-12-11 15:38:35 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-06-21 15:09:19 +00:00
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkSerialProcs.h"
|
|
|
|
|
2021-01-05 15:34:48 +00:00
|
|
|
namespace {
|
|
|
|
sk_sp<SkData> collectNonTextureImagesProc(SkImage* img, void* ctx) {
|
|
|
|
SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
|
|
|
|
uint32_t originalId = img->uniqueID();
|
|
|
|
auto it = context->fNonTexMap.find(originalId);
|
|
|
|
if (it == context->fNonTexMap.end()) {
|
|
|
|
context->fNonTexMap[originalId] = img->makeNonTextureImage();
|
|
|
|
}
|
|
|
|
return SkData::MakeEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkSharingSerialContext::collectNonTextureImagesFromPicture(
|
|
|
|
const SkPicture* pic, SkSharingSerialContext* sharingCtx) {
|
|
|
|
SkSerialProcs tempProc;
|
|
|
|
tempProc.fImageCtx = sharingCtx;
|
|
|
|
tempProc.fImageProc = collectNonTextureImagesProc;
|
|
|
|
auto ns = SkNullWStream();
|
|
|
|
pic->serialize(&ns, &tempProc);
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:09:19 +00:00
|
|
|
sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
|
|
|
|
SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
|
|
|
|
uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
|
|
|
|
// find out if we have already serialized this, and if so, what its in-file id is.
|
|
|
|
auto iter = context->fImageMap.find(id);
|
|
|
|
if (iter == context->fImageMap.end()) {
|
|
|
|
// When not present, add its id to the map and return its usual serialized form.
|
2021-01-05 15:34:48 +00:00
|
|
|
context->fImageMap[id] = context->fImageMap.size(); // Next in-file id
|
|
|
|
// encode the image or it's non-texture replacement if one was collected
|
|
|
|
auto iter2 = context->fNonTexMap.find(id);
|
|
|
|
if (iter2 != context->fNonTexMap.end()) {
|
|
|
|
img = iter2->second.get();
|
|
|
|
}
|
2019-06-21 15:09:19 +00:00
|
|
|
return img->encodeToData();
|
|
|
|
}
|
|
|
|
uint32_t fid = context->fImageMap[id];
|
|
|
|
// if present, return only the in-file id we registered the first time we serialized it.
|
|
|
|
return SkData::MakeWithCopy(&fid, sizeof(fid));
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
|
|
|
|
const void* data, size_t length, void* ctx) {
|
2020-12-11 15:38:35 +00:00
|
|
|
if (!data || !length || !ctx) {
|
2021-06-25 13:32:45 +00:00
|
|
|
SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %d %p.\n",
|
2020-12-11 15:38:35 +00:00
|
|
|
data, length, ctx);
|
2021-01-05 15:34:48 +00:00
|
|
|
// Return something so the rest of the debugger can proceed.
|
2020-12-11 15:38:35 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
|
2020-12-23 16:50:36 +00:00
|
|
|
return bm.asImage();
|
2020-12-11 15:38:35 +00:00
|
|
|
}
|
2019-06-21 15:09:19 +00:00
|
|
|
SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
|
|
|
|
uint32_t fid;
|
|
|
|
// If the data is an image fid, look up an already deserialized image from our map
|
|
|
|
if (length == sizeof(fid)) {
|
|
|
|
memcpy(&fid, data, sizeof(fid));
|
|
|
|
if (fid >= context->fImages.size()) {
|
2021-01-05 15:34:48 +00:00
|
|
|
SkDebugf("Cannot deserialize using id, We do not have the data for image %d.\n", fid);
|
2019-06-21 15:09:19 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return context->fImages[fid];
|
|
|
|
}
|
|
|
|
// Otherwise, the data is an image, deserialise it, store it in our map at its fid.
|
|
|
|
// TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
|
|
|
|
sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
|
|
|
|
const sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(dataView));
|
|
|
|
context->fImages.push_back(image);
|
|
|
|
return image;
|
|
|
|
}
|