1 | /* |
2 | * Copyright 2011 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 SkPictureData_DEFINED |
9 | #define SkPictureData_DEFINED |
10 | |
11 | #include "include/core/SkBitmap.h" |
12 | #include "include/core/SkDrawable.h" |
13 | #include "include/core/SkPicture.h" |
14 | #include "include/private/SkTArray.h" |
15 | #include "src/core/SkPictureFlat.h" |
16 | |
17 | #include <memory> |
18 | |
19 | class SkData; |
20 | class SkPictureRecord; |
21 | struct SkSerialProcs; |
22 | class SkStream; |
23 | class SkWStream; |
24 | class SkBBoxHierarchy; |
25 | class SkMatrix; |
26 | class SkPaint; |
27 | class SkPath; |
28 | class SkReadBuffer; |
29 | class SkTextBlob; |
30 | |
31 | struct SkPictInfo { |
32 | SkPictInfo() : fVersion(~0U) {} |
33 | |
34 | uint32_t getVersion() const { |
35 | SkASSERT(fVersion != ~0U); |
36 | return fVersion; |
37 | } |
38 | |
39 | void setVersion(uint32_t version) { |
40 | SkASSERT(version != ~0U); |
41 | fVersion = version; |
42 | } |
43 | |
44 | public: |
45 | char fMagic[8]; |
46 | private: |
47 | uint32_t fVersion; |
48 | public: |
49 | SkRect fCullRect; |
50 | }; |
51 | |
52 | #define SK_PICT_READER_TAG SkSetFourByteTag('r', 'e', 'a', 'd') |
53 | #define SK_PICT_FACTORY_TAG SkSetFourByteTag('f', 'a', 'c', 't') |
54 | #define SK_PICT_TYPEFACE_TAG SkSetFourByteTag('t', 'p', 'f', 'c') |
55 | #define SK_PICT_PICTURE_TAG SkSetFourByteTag('p', 'c', 't', 'r') |
56 | #define SK_PICT_DRAWABLE_TAG SkSetFourByteTag('d', 'r', 'a', 'w') |
57 | |
58 | // This tag specifies the size of the ReadBuffer, needed for the following tags |
59 | #define SK_PICT_BUFFER_SIZE_TAG SkSetFourByteTag('a', 'r', 'a', 'y') |
60 | // these are all inside the ARRAYS tag |
61 | #define SK_PICT_PAINT_BUFFER_TAG SkSetFourByteTag('p', 'n', 't', ' ') |
62 | #define SK_PICT_PATH_BUFFER_TAG SkSetFourByteTag('p', 't', 'h', ' ') |
63 | #define SK_PICT_TEXTBLOB_BUFFER_TAG SkSetFourByteTag('b', 'l', 'o', 'b') |
64 | #define SK_PICT_VERTICES_BUFFER_TAG SkSetFourByteTag('v', 'e', 'r', 't') |
65 | #define SK_PICT_IMAGE_BUFFER_TAG SkSetFourByteTag('i', 'm', 'a', 'g') |
66 | |
67 | // Always write this last (with no length field afterwards) |
68 | #define SK_PICT_EOF_TAG SkSetFourByteTag('e', 'o', 'f', ' ') |
69 | |
70 | template <typename T> |
71 | T* read_index_base_1_or_null(SkReadBuffer* reader, const SkTArray<sk_sp<T>>& array) { |
72 | int index = reader->readInt(); |
73 | return reader->validate(index > 0 && index <= array.count()) ? array[index - 1].get() : nullptr; |
74 | } |
75 | |
76 | class SkPictureData { |
77 | public: |
78 | SkPictureData(const SkPictureRecord& record, const SkPictInfo&); |
79 | // Does not affect ownership of SkStream. |
80 | static SkPictureData* CreateFromStream(SkStream*, |
81 | const SkPictInfo&, |
82 | const SkDeserialProcs&, |
83 | SkTypefacePlayback*); |
84 | static SkPictureData* CreateFromBuffer(SkReadBuffer&, const SkPictInfo&); |
85 | |
86 | void serialize(SkWStream*, const SkSerialProcs&, SkRefCntSet*, bool textBlobsOnly=false) const; |
87 | void flatten(SkWriteBuffer&) const; |
88 | |
89 | const sk_sp<SkData>& opData() const { return fOpData; } |
90 | |
91 | protected: |
92 | explicit SkPictureData(const SkPictInfo& info); |
93 | |
94 | // Does not affect ownership of SkStream. |
95 | bool parseStream(SkStream*, const SkDeserialProcs&, SkTypefacePlayback*); |
96 | bool parseBuffer(SkReadBuffer& buffer); |
97 | |
98 | public: |
99 | const SkImage* getImage(SkReadBuffer* reader) const { |
100 | // images are written base-0, unlike paths, pictures, drawables, etc. |
101 | const int index = reader->readInt(); |
102 | return reader->validateIndex(index, fImages.count()) ? fImages[index].get() : nullptr; |
103 | } |
104 | |
105 | const SkPath& getPath(SkReadBuffer* reader) const { |
106 | int index = reader->readInt(); |
107 | return reader->validate(index > 0 && index <= fPaths.count()) ? |
108 | fPaths[index - 1] : fEmptyPath; |
109 | } |
110 | |
111 | const SkPicture* getPicture(SkReadBuffer* reader) const { |
112 | return read_index_base_1_or_null(reader, fPictures); |
113 | } |
114 | |
115 | SkDrawable* getDrawable(SkReadBuffer* reader) const { |
116 | return read_index_base_1_or_null(reader, fDrawables); |
117 | } |
118 | |
119 | // Return a paint if one was used for this op, or nullptr if none was used. |
120 | const SkPaint* optionalPaint(SkReadBuffer* reader) const; |
121 | |
122 | // Return the paint used for this op, invalidating the SkReadBuffer if there appears to be none. |
123 | // The returned paint is always safe to use. |
124 | const SkPaint& requiredPaint(SkReadBuffer* reader) const; |
125 | |
126 | const SkTextBlob* getTextBlob(SkReadBuffer* reader) const { |
127 | return read_index_base_1_or_null(reader, fTextBlobs); |
128 | } |
129 | |
130 | const SkVertices* getVertices(SkReadBuffer* reader) const { |
131 | return read_index_base_1_or_null(reader, fVertices); |
132 | } |
133 | |
134 | private: |
135 | // these help us with reading/writing |
136 | // Does not affect ownership of SkStream. |
137 | bool parseStreamTag(SkStream*, uint32_t tag, uint32_t size, |
138 | const SkDeserialProcs&, SkTypefacePlayback*); |
139 | void parseBufferTag(SkReadBuffer&, uint32_t tag, uint32_t size); |
140 | void flattenToBuffer(SkWriteBuffer&, bool textBlobsOnly) const; |
141 | |
142 | SkTArray<SkPaint> fPaints; |
143 | SkTArray<SkPath> fPaths; |
144 | |
145 | sk_sp<SkData> fOpData; // opcodes and parameters |
146 | |
147 | const SkPath fEmptyPath; |
148 | const SkBitmap fEmptyBitmap; |
149 | |
150 | SkTArray<sk_sp<const SkPicture>> fPictures; |
151 | SkTArray<sk_sp<SkDrawable>> fDrawables; |
152 | SkTArray<sk_sp<const SkTextBlob>> fTextBlobs; |
153 | SkTArray<sk_sp<const SkVertices>> fVertices; |
154 | SkTArray<sk_sp<const SkImage>> fImages; |
155 | |
156 | SkTypefacePlayback fTFPlayback; |
157 | std::unique_ptr<SkFactoryPlayback> fFactoryPlayback; |
158 | |
159 | const SkPictInfo fInfo; |
160 | |
161 | static void WriteFactories(SkWStream* stream, const SkFactorySet& rec); |
162 | static void WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec, const SkSerialProcs&); |
163 | |
164 | void initForPlayback() const; |
165 | }; |
166 | |
167 | #endif |
168 | |