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