Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 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 | |
Nathaniel Nifong | 82e0d52 | 2020-12-11 10:38:35 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkSerialProcs.h" |
| 14 | |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 15 | namespace { |
| 16 | sk_sp<SkData> collectNonTextureImagesProc(SkImage* img, void* ctx) { |
| 17 | SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx); |
| 18 | uint32_t originalId = img->uniqueID(); |
| 19 | auto it = context->fNonTexMap.find(originalId); |
| 20 | if (it == context->fNonTexMap.end()) { |
| 21 | context->fNonTexMap[originalId] = img->makeNonTextureImage(); |
| 22 | } |
| 23 | return SkData::MakeEmpty(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void SkSharingSerialContext::collectNonTextureImagesFromPicture( |
| 28 | const SkPicture* pic, SkSharingSerialContext* sharingCtx) { |
| 29 | SkSerialProcs tempProc; |
| 30 | tempProc.fImageCtx = sharingCtx; |
| 31 | tempProc.fImageProc = collectNonTextureImagesProc; |
| 32 | auto ns = SkNullWStream(); |
| 33 | pic->serialize(&ns, &tempProc); |
| 34 | } |
| 35 | |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 36 | sk_sp<SkData> SkSharingSerialContext::serializeImage(SkImage* img, void* ctx) { |
| 37 | SkSharingSerialContext* context = reinterpret_cast<SkSharingSerialContext*>(ctx); |
| 38 | uint32_t id = img->uniqueID(); // get this process's id for the image. these are not hashes. |
| 39 | // find out if we have already serialized this, and if so, what its in-file id is. |
| 40 | auto iter = context->fImageMap.find(id); |
| 41 | if (iter == context->fImageMap.end()) { |
| 42 | // When not present, add its id to the map and return its usual serialized form. |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 43 | context->fImageMap[id] = context->fImageMap.size(); // Next in-file id |
| 44 | // encode the image or it's non-texture replacement if one was collected |
| 45 | auto iter2 = context->fNonTexMap.find(id); |
| 46 | if (iter2 != context->fNonTexMap.end()) { |
| 47 | img = iter2->second.get(); |
| 48 | } |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 49 | return img->encodeToData(); |
| 50 | } |
| 51 | uint32_t fid = context->fImageMap[id]; |
| 52 | // if present, return only the in-file id we registered the first time we serialized it. |
| 53 | return SkData::MakeWithCopy(&fid, sizeof(fid)); |
| 54 | } |
| 55 | |
| 56 | sk_sp<SkImage> SkSharingDeserialContext::deserializeImage( |
| 57 | const void* data, size_t length, void* ctx) { |
Nathaniel Nifong | 82e0d52 | 2020-12-11 10:38:35 -0500 | [diff] [blame] | 58 | if (!data || !length || !ctx) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 59 | SkDebugf("SkSharingDeserialContext::deserializeImage arguments invalid %p %zu %p.\n", |
Nathaniel Nifong | 82e0d52 | 2020-12-11 10:38:35 -0500 | [diff] [blame] | 60 | data, length, ctx); |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 61 | // Return something so the rest of the debugger can proceed. |
Nathaniel Nifong | 82e0d52 | 2020-12-11 10:38:35 -0500 | [diff] [blame] | 62 | SkBitmap bm; |
| 63 | bm.allocPixels(SkImageInfo::MakeN32Premul(1, 1)); |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 64 | return bm.asImage(); |
Nathaniel Nifong | 82e0d52 | 2020-12-11 10:38:35 -0500 | [diff] [blame] | 65 | } |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 66 | SkSharingDeserialContext* context = reinterpret_cast<SkSharingDeserialContext*>(ctx); |
| 67 | uint32_t fid; |
| 68 | // If the data is an image fid, look up an already deserialized image from our map |
| 69 | if (length == sizeof(fid)) { |
| 70 | memcpy(&fid, data, sizeof(fid)); |
| 71 | if (fid >= context->fImages.size()) { |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 72 | SkDebugf("Cannot deserialize using id, We do not have the data for image %d.\n", fid); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 73 | return nullptr; |
| 74 | } |
| 75 | return context->fImages[fid]; |
| 76 | } |
| 77 | // Otherwise, the data is an image, deserialise it, store it in our map at its fid. |
| 78 | // TODO(nifong): make DeserialProcs accept sk_sp<SkData> so we don't have to copy this. |
| 79 | sk_sp<SkData> dataView = SkData::MakeWithCopy(data, length); |
| 80 | const sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(dataView)); |
| 81 | context->fImages.push_back(image); |
| 82 | return image; |
| 83 | } |