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