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 | #ifndef SkSharingProc_DEFINED |
| 9 | #define SkSharingProc_DEFINED |
| 10 | |
| 11 | #include <unordered_map> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "include/core/SkData.h" |
| 15 | #include "include/core/SkImage.h" |
| 16 | #include "include/core/SkSerialProcs.h" |
Leon Scroggins III | d84b2d5 | 2023-08-25 14:37:23 -0400 | [diff] [blame] | 17 | #include "src/core/SkTHash.h" |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 18 | |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 19 | /** |
| 20 | * This serial proc serializes each image it encounters only once, using their uniqueId as the |
| 21 | * property for sameness. |
| 22 | * |
| 23 | * It's most basic usage involves setting your imageProc to SkSharingSerialContext::serializeImage |
| 24 | * and creating an SkSharingSerialContext in an appropriate scope to outlive all the images that |
| 25 | * will be encountered before serialization. |
| 26 | * |
| 27 | * Optionally, collectNonTextureImagesFromPicture can be called with an SkSharingContext and an |
| 28 | * SkPicture that may reference not-yet-released texture backed images. It will make non-texture |
| 29 | * copies if necessary and store them in the context. If present, they will be used in the |
| 30 | * final serialization. |
| 31 | * |
| 32 | * This is intended to be used on Android with MultiPictureDocument's onEndPage parameter, in a |
| 33 | * lambda that captures the context, because MPD cannot make assumptions about the type of proc it |
| 34 | * receives and clients (Chrome) build MPD without this source file. |
| 35 | */ |
| 36 | |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 37 | struct SkSharingSerialContext { |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 38 | // --- Data and and function for optional texture collection pass --- // |
| 39 | |
| 40 | // A map from uniqueID of images referenced by commands to non-texture images |
| 41 | // collected at the end of each frame. |
Leon Scroggins III | d84b2d5 | 2023-08-25 14:37:23 -0400 | [diff] [blame] | 42 | skia_private::THashMap<uint32_t, sk_sp<SkImage>> fNonTexMap; |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 43 | |
| 44 | // Collects any non-texture images referenced by the picture and stores non-texture copies |
| 45 | // in the fNonTexMap of the provided SkSharingContext |
| 46 | static void collectNonTextureImagesFromPicture( |
| 47 | const SkPicture* pic, SkSharingSerialContext* sharingCtx); |
| 48 | |
| 49 | |
| 50 | // --- Data and serialization function for regular use --- // |
| 51 | |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 52 | // A map from the ids from SkImage::uniqueID() to ids used within the file |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 53 | // The keys are ids of original images, not of non-texture copies |
Leon Scroggins III | d84b2d5 | 2023-08-25 14:37:23 -0400 | [diff] [blame] | 54 | skia_private::THashMap<uint32_t, int> fImageMap; |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 55 | |
| 56 | // A serial proc that shares images between subpictures |
| 57 | // To use this, create an instance of SkSerialProcs and populate it this way. |
| 58 | // The client must retain ownership of the context. |
| 59 | // auto ctx = std::make_unique<SkSharingSerialContext>() |
| 60 | // SkSerialProcs procs; |
| 61 | // procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 62 | // procs.fImageCtx = ctx.get(); |
| 63 | static sk_sp<SkData> serializeImage(SkImage* img, void* ctx); |
| 64 | }; |
| 65 | |
| 66 | struct SkSharingDeserialContext { |
| 67 | // a list of unique images in the order they were encountered in the file |
| 68 | // Subsequent occurrences of an image refer to it by it's index in this list. |
| 69 | std::vector<sk_sp<SkImage>> fImages; |
| 70 | |
| 71 | // A deserial proc that can interpret id's in place of images as references to previous images. |
| 72 | // Can also deserialize a SKP where all images are inlined (it's backwards compatible) |
| 73 | static sk_sp<SkImage> deserializeImage(const void* data, size_t length, void* ctx); |
| 74 | }; |
| 75 | |
| 76 | #endif |