1 /* 2 * Copyright 2017 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 SkSerialProcs_DEFINED 9 #define SkSerialProcs_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/private/base/SkAPI.h" 13 14 #include <cstddef> 15 #include <optional> 16 17 class SkData; 18 class SkImage; 19 class SkPicture; 20 class SkTypeface; 21 class SkReadBuffer; 22 enum SkAlphaType : int; 23 namespace sktext::gpu { 24 class Slug; 25 } 26 27 /** 28 * A serial-proc is asked to serialize the specified object (e.g. picture or image). 29 * If a data object is returned, it will be used (even if it is zero-length). 30 * If null is returned, then Skia will take its default action. 31 * 32 * The default action for pictures is to use Skia's internal format. 33 * The default action for images is to encode either in its native format or PNG. 34 * The default action for typefaces is to use Skia's internal format. 35 */ 36 37 using SkSerialPictureProc = sk_sp<SkData> (*)(SkPicture*, void* ctx); 38 using SkSerialImageProc = sk_sp<SkData> (*)(SkImage*, void* ctx); 39 using SkSerialTypefaceProc = sk_sp<SkData> (*)(SkTypeface*, void* ctx); 40 41 /** 42 * Called with the encoded form of a picture (previously written with a custom 43 * SkSerialPictureProc proc). Return a picture object, or nullptr indicating failure. 44 */ 45 using SkDeserialPictureProc = sk_sp<SkPicture> (*)(const void* data, size_t length, void* ctx); 46 47 /** 48 * Called with the encoded form of an image. The proc can return an image object, or if it 49 * returns nullptr, then Skia will take its default action to try to create an image from the data. 50 * 51 * This will also be used to decode the internal mipmap layers that are saved on some images. 52 * 53 * An explicit SkAlphaType may have been encoded in the bytestream; if not, then the passed in 54 * optional will be not present. 55 * 56 * Clients should set at least SkDeserialImageProc; SkDeserialImageFromDataProc may be called 57 * if the internal implementation has a SkData copy already. Implementations of SkDeserialImageProc 58 * must make a copy of any data they needed after the proc finishes, since the data will go away 59 * after serialization ends. 60 */ 61 #if !defined(SK_LEGACY_DESERIAL_IMAGE_PROC) 62 using SkDeserialImageProc = sk_sp<SkImage> (*)(const void* data, size_t length, void* ctx); 63 #else 64 using SkDeserialImageProc = sk_sp<SkImage> (*)(const void* data, 65 size_t length, 66 std::optional<SkAlphaType>, 67 void* ctx); 68 #endif 69 using SkDeserialImageFromDataProc = sk_sp<SkImage> (*)(sk_sp<SkData>, 70 std::optional<SkAlphaType>, 71 void* ctx); 72 73 /** 74 * Slugs are currently only deserializable with a GPU backend. Clients will not be able to 75 * provide a custom mechanism here, but can enable Slug deserialization by calling 76 * sktext::gpu::AddDeserialProcs to add Skia's implementation. 77 */ 78 using SkSlugProc = sk_sp<sktext::gpu::Slug> (*)(SkReadBuffer&, void* ctx); 79 80 /** 81 * Called with the encoded form of a typeface (previously written with a custom 82 * SkSerialTypefaceProc proc). Return a typeface object, or nullptr indicating failure. 83 */ 84 using SkDeserialTypefaceProc = sk_sp<SkTypeface> (*)(const void* data, size_t length, void* ctx); 85 86 struct SK_API SkSerialProcs { 87 SkSerialPictureProc fPictureProc = nullptr; 88 void* fPictureCtx = nullptr; 89 90 SkSerialImageProc fImageProc = nullptr; 91 void* fImageCtx = nullptr; 92 93 SkSerialTypefaceProc fTypefaceProc = nullptr; 94 void* fTypefaceCtx = nullptr; 95 }; 96 97 struct SK_API SkDeserialProcs { 98 SkDeserialPictureProc fPictureProc = nullptr; 99 void* fPictureCtx = nullptr; 100 101 SkDeserialImageProc fImageProc = nullptr; 102 SkDeserialImageFromDataProc fImageDataProc = nullptr; 103 void* fImageCtx = nullptr; 104 105 SkSlugProc fSlugProc = nullptr; 106 void* fSlugCtx = nullptr; 107 108 SkDeserialTypefaceProc fTypefaceProc = nullptr; 109 void* fTypefaceCtx = nullptr; 110 111 // This looks like a flag, but it could be considered a proc as well (one that takes no 112 // parameters and returns a bool). Given that there are only two valid implementations of that 113 // proc, we just insert the bool directly. 114 bool fAllowSkSL = true; 115 }; 116 117 #endif 118