blob: bdb39c1e14b54bd8fbaa46a7466a9487cbc9ca77 [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"
Kevin Lubick10270ac2023-03-30 14:13:26 -040013#include "include/core/SkPicture.h"
Nathaniel Nifong0426c382019-06-21 11:09:19 -040014#include "include/core/SkSerialProcs.h"
Kevin Lubickefce17d2022-03-09 23:11:31 -050015#include "include/core/SkStream.h"
Kevin Lubickbab392f2023-04-11 13:51:02 -040016#include "include/encode/SkPngEncoder.h"
Nathaniel Nifong0426c382019-06-21 11:09:19 -040017
Nathaniel Nifong4a565682021-01-05 10:34:48 -050018namespace {
19 sk_sp<SkData> collectNonTextureImagesProc(SkImage* img, void* ctx) {
20 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
21 uint32_t originalId = img->uniqueID();
Leon Scroggins IIId84b2d52023-08-25 14:37:23 -040022 sk_sp<SkImage>* imageInMap = context->fNonTexMap.find(originalId);
23 if (!imageInMap) {
Nathaniel Nifong4a565682021-01-05 10:34:48 -050024 context->fNonTexMap[originalId] = img->makeNonTextureImage();
25 }
Leon Scroggins IIId84b2d52023-08-25 14:37:23 -040026
27 // This function implements a proc that is more generally for serialization, but
28 // we really only want to build our map. The output of this function is ignored.
Nathaniel Nifong4a565682021-01-05 10:34:48 -050029 return SkData::MakeEmpty();
30 }
31}
32
33void SkSharingSerialContext::collectNonTextureImagesFromPicture(
34 const SkPicture* pic, SkSharingSerialContext* sharingCtx) {
35 SkSerialProcs tempProc;
36 tempProc.fImageCtx = sharingCtx;
37 tempProc.fImageProc = collectNonTextureImagesProc;
Brian Salomon87a00782021-10-12 09:37:39 -040038 SkNullWStream ns;
Nathaniel Nifong4a565682021-01-05 10:34:48 -050039 pic->serialize(&ns, &tempProc);
40}
41
Nathaniel Nifong0426c382019-06-21 11:09:19 -040042sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) {
43 SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx);
44 uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes.
45 // find out if we have already serialized this, and if so, what its in-file id is.
Leon Scroggins IIId84b2d52023-08-25 14:37:23 -040046 int* fid = context->fImageMap.find(id);
47 if (!fid) {
Nathaniel Nifong0426c382019-06-21 11:09:19 -040048 // When not present, add its id to the map and return its usual serialized form.
Leon Scroggins IIId84b2d52023-08-25 14:37:23 -040049 context->fImageMap[id] = context->fImageMap.count(); // Next in-file id
Nathaniel Nifong4a565682021-01-05 10:34:48 -050050 // encode the image or it's non-texture replacement if one was collected
Leon Scroggins IIId84b2d52023-08-25 14:37:23 -040051 sk_sp<SkImage>* replacementImage = context->fNonTexMap.find(id);
52 if (replacementImage) {
53 img = replacementImage->get();
Nathaniel Nifong4a565682021-01-05 10:34:48 -050054 }
Kevin Lubickbab392f2023-04-11 13:51:02 -040055 return SkPngEncoder::Encode(nullptr, img, {});
Nathaniel Nifong0426c382019-06-21 11:09:19 -040056 }
Nathaniel Nifong0426c382019-06-21 11:09:19 -040057 // if present, return only the in-file id we registered the first time we serialized it.
Leon Scroggins IIId84b2d52023-08-25 14:37:23 -040058 return SkData::MakeWithCopy(fid, sizeof(*fid));
Nathaniel Nifong0426c382019-06-21 11:09:19 -040059}
60
61sk_sp<SkImage> SkSharingDeserialContext::deserializeImage(
62 const void* data, size_t length, void* ctx) {
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050063 if (!data || !length || !ctx) {
John Stiles7bf79992021-06-25 11:05:20 -040064 SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n",
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050065 data, length, ctx);
Nathaniel Nifong4a565682021-01-05 10:34:48 -050066 // Return something so the rest of the debugger can proceed.
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050067 SkBitmap bm;
68 bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
Mike Reeddc607e32020-12-23 11:50:36 -050069 return bm.asImage();
Nathaniel Nifong82e0d522020-12-11 10:38:35 -050070 }
Nathaniel Nifong0426c382019-06-21 11:09:19 -040071 SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx);
72 uint32_t fid;
73 // If the data is an image fid, look up an already deserialized image from our map
74 if (length == sizeof(fid)) {
75 memcpy(&fid, data, sizeof(fid));
76 if (fid >= context->fImages.size()) {
Nathaniel Nifong4a565682021-01-05 10:34:48 -050077 SkDebugf("Cannot deserialize using id, We do not have the data for image %d.\n", fid);
Nathaniel Nifong0426c382019-06-21 11:09:19 -040078 return nullptr;
79 }
80 return context->fImages[fid];
81 }
82 // Otherwise, the data is an image, deserialise it, store it in our map at its fid.
83 // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this.
84 sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length);
Kevin Lubick77472bf2023-03-24 07:11:17 -040085 const sk_sp<SkImage> image = SkImages::DeferredFromEncodedData(std::move(dataView));
Nathaniel Nifong0426c382019-06-21 11:09:19 -040086 context->fImages.push_back(image);
87 return image;
88}