| 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 |
#include "src/core/SkTHash.h" |
| 18 |
|
| 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 |
|
| 37 |
struct SkSharingSerialContext { |
| 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. |
| 42 |
skia_private::THashMap<uint32_t, sk_sp<SkImage>> fNonTexMap; |
| 43 |
GrDirectContext* fDirectContext; |
| 44 |
|
| 45 |
// Collects any non-texture images referenced by the picture and stores non-texture copies |
| 46 |
// in the fNonTexMap of the provided SkSharingContext |
| 47 |
static void collectNonTextureImagesFromPicture( |
| 48 |
const SkPicture* pic, SkSharingSerialContext* sharingCtx); |
| 49 |
|
| 50 |
void setDirectContext(GrDirectContext* ctx); |
| 51 |
|
| 52 |
// --- Data and serialization function for regular use --- // |
| 53 |
|
| 54 |
// A map from the ids from SkImage::uniqueID() to ids used within the file |
| 55 |
// The keys are ids of original images, not of non-texture copies |
| 56 |
skia_private::THashMap<uint32_t, int> fImageMap; |
| 57 |
|
| 58 |
// A serial proc that shares images between subpictures |
| 59 |
// To use this, create an instance of SkSerialProcs and populate it this way. |
| 60 |
// The client must retain ownership of the context. |
| 61 |
// auto ctx = std::make_unique<SkSharingSerialContext>() |
| 62 |
// SkSerialProcs procs; |
| 63 |
// procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 64 |
// procs.fImageCtx = ctx.get(); |
| 65 |
static SkSerialReturnType serializeImage(SkImage* img, void* ctx); |
| 66 |
}; |
| 67 |
|
| 68 |
struct SkSharingDeserialContext { |
| 69 |
// a list of unique images in the order they were encountered in the file |
| 70 |
// Subsequent occurrences of an image refer to it by it's index in this list. |
| 71 |
std::vector<sk_sp<SkImage>> fImages; |
| 72 |
|
| 73 |
// A deserial proc that can interpret id's in place of images as references to previous images. |
| 74 |
// Can also deserialize a SKP where all images are inlined (it's backwards compatible) |
| 75 |
static sk_sp<SkImage> deserializeImage(const void* data, size_t length, void* ctx); |
| 76 |
}; |
| 77 |
|
| 78 |
#endif |