Debian Codegraph @

src/core/SkReadBuffer.h

raw ◀ ▶
1 Debian patch touch this file
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 SkReadBuffer_DEFINED
9 #define SkReadBuffer_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkFlattenable.h"
•14 #include "include/core/SkFourByteTag.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPathEffect.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkSamplingOptions.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkSerialProcs.h"
24 #include "include/core/SkShader.h"
25 #include "include/core/SkSpan.h"
26 #include "include/private/base/SkAlign.h"
27 #include "include/private/base/SkAssert.h"
28 #include "src/core/SkBlenderBase.h"
29 #include "src/core/SkImageFilter_Base.h"
30 #include "src/core/SkMaskFilterBase.h"
31 #include "src/core/SkPaintPriv.h"
32 #include "src/core/SkPicturePriv.h"
33 #include "src/core/SkSamplingPriv.h"
34 #include "src/core/SkTHash.h"
35 #include "src/effects/colorfilters/SkColorFilterBase.h"
36 #include "src/shaders/SkShaderBase.h"
37
38 #include <cstddef>
39 #include <cstdint>
40 #include <optional>
41
42 class SkBlender;
43 class SkData;
44 class SkImage;
45 class SkM44;
46 class SkMaskFilter;
47 class SkMatrix;
48 class SkPath;
49 class SkRRect;
50 class SkRegion;
51 class SkString;
52 class SkTypeface;
53 struct SkPoint3;
54
55 class SkReadBuffer {
56 public:
57 SkReadBuffer() = default;
58 SkReadBuffer(const void* data, size_t size) {
59 this->setMemory(data, size);
60 }
61
62 void setMemory(const void*, size_t);
63
64 /**
65 * Returns true IFF the version is older than the specified version.
66 */
67 bool isVersionLT(SkPicturePriv::Version targetVersion) const {
68 SkASSERT(targetVersion > 0);
69 return fVersion > 0 && fVersion < targetVersion;
70 }
71
72 uint32_t getVersion() const { return fVersion; }
73
74 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
75 void setVersion(int version) {
76 SkASSERT(0 == fVersion || version == fVersion);
77 fVersion = version;
78 }
79
80 size_t size() const { return fStop - fBase; }
81 size_t offset() const { return fCurr - fBase; }
82 bool eof() { return fCurr >= fStop; }
83 const void* skip(size_t size);
84 const void* skip(size_t count, size_t size); // does safe multiply
85 size_t available() const { return fStop - fCurr; }
86
87 template <typename T> const T* skipT() {
88 return static_cast<const T*>(this->skip(sizeof(T)));
89 }
90 template <typename T> const T* skipT(size_t count) {
91 return static_cast<const T*>(this->skip(count, sizeof(T)));
92 }
93
94 // primitives
95 bool readBool();
96 SkColor readColor();
97 int32_t readInt();
98 SkScalar readScalar();
99 uint32_t readUInt();
100 int32_t read32();
101
102 template <typename T> T read32LE(T max) {
103 uint32_t value = this->readUInt();
104 if (!this->validate(value <= static_cast<uint32_t>(max))) {
105 value = 0;
106 }
107 return static_cast<T>(value);
108 }
109
110 // peek
111 uint8_t peekByte();
112
113 void readString(SkString* string);
114
115 // common data structures
116 void readColor4f(SkColor4f* color);
117 void readPoint(SkPoint* point);
118 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
119 void readPoint3(SkPoint3* point);
120 void read(SkM44*);
121 void readMatrix(SkMatrix* matrix);
122 void readIRect(SkIRect* rect);
123 void readRect(SkRect* rect);
124 SkRect readRect();
125 void readRRect(SkRRect* rrect);
126 void readRegion(SkRegion* region);
127
128 std::optional<SkPath> readPath();
129
130 SkPaint readPaint() {
131 return SkPaintPriv::Unflatten(*this);
132 }
133
134 SkFlattenable* readRawFlattenable();
135 SkFlattenable* readFlattenable(SkFlattenable::Type);
136 template <typename T> sk_sp<T> readFlattenable() {
137 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
138 }
139 sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilterBase>(); }
140 sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter_Base>(); }
141 sk_sp<SkBlender> readBlender() { return this->readFlattenable<SkBlenderBase>(); }
142 sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilterBase>(); }
143 sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
144 sk_sp<SkShader> readShader() { return this->readFlattenable<SkShaderBase>(); }
145
146 // Reads SkAlign4(bytes), but will only copy bytes into the buffer.
147 bool readPad32(void* buffer, size_t bytes);
148
149 // binary data and arrays
150 bool readByteArray(void* value, size_t size);
151 bool readColorArray(SkSpan<SkColor>);
152 bool readColor4fArray(SkSpan<SkColor4f>);
153 bool readIntArray(SkSpan<int32_t>);
154 bool readPointArray(SkSpan<SkPoint>);
155 bool readScalarArray(SkSpan<SkScalar>);
156
157 const void* skipByteArray(size_t* size);
158
159 sk_sp<SkData> readByteArrayAsData();
160
161 // helpers to get info about arrays and binary data
162 uint32_t getArrayCount();
163
164 // If there is a real error (e.g. data is corrupted) this returns null. If the image cannot
165 // be created (e.g. it was not originally encoded) then this returns an image that doesn't
166 // draw.
167 sk_sp<SkImage> readImage();
168 sk_sp<SkTypeface> readTypeface();
169
170 void setTypefaceArray(sk_sp<SkTypeface> array[], int count) {
171 fTFArray = array;
172 fTFCount = count;
173 }
174
175 /**
176 * Call this with a pre-loaded array of Factories, in the same order as
177 * were created/written by the writer. SkPicture uses this.
178 */
179 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
180 fFactoryArray = array;
181 fFactoryCount = count;
182 }
183
184 void setDeserialProcs(const SkDeserialProcs& procs);
185 const SkDeserialProcs& getDeserialProcs() const { return fProcs; }
186
187 bool allowSkSL() const { return fAllowSkSL; }
188 void setAllowSkSL(bool allow) { fAllowSkSL = allow; }
189
•190 bool allowTags(SkFourByteTag tag) const {
191 if (!fProcs.fAllowTagsProc) {
192 return true;
193 }
194 return fProcs.fAllowTagsProc(tag, fProcs.fAllowTagsCtx);
195 }
196
197 /**
198 * If isValid is false, sets the buffer to be "invalid". Returns true if the buffer
199 * is still valid.
200 */
201 bool validate(bool isValid) {
202 if (!isValid) {
203 this->setInvalid();
204 }
205 return !fError;
206 }
207
208 /**
209 * Helper function to do a preflight check before a large allocation or read.
210 * Returns true if there is enough bytes in the buffer to read n elements of T.
211 * If not, the buffer will be "invalid" and false will be returned.
212 */
213 template <typename T>
214 bool validateCanReadN(size_t n) {
215 return this->validate(n <= (this->available() / sizeof(T)));
216 }
217
218 bool isValid() const { return !fError; }
219 bool validateIndex(int index, int count) {
220 return this->validate(index >= 0 && index < count);
221 }
222
223 // Utilities that mark the buffer invalid if the requested value is out-of-range
224
225 // If the read value is outside of the range, validate(false) is called, and min
226 // is returned, else the value is returned.
227 int32_t checkInt(int min, int max);
228
229 template <typename T> T checkRange(T min, T max) {
230 return static_cast<T>(this->checkInt(static_cast<int32_t>(min),
231 static_cast<int32_t>(max)));
232 }
233
234 SkLegacyFQ checkFilterQuality();
235
236 SkSamplingOptions readSampling();
237
238 private:
239 const char* readString(size_t* length);
240
241 void setInvalid();
242 bool readArray(void* value, size_t size, size_t elementSize);
243 bool isAvailable(size_t size) const { return size <= this->available(); }
244
245 // These are always 4-byte aligned
246 const char* fCurr = nullptr; // current position within buffer
247 const char* fStop = nullptr; // end of buffer
248 const char* fBase = nullptr; // beginning of buffer
249
250 // Only used if we do not have an fFactoryArray.
251 skia_private::THashMap<uint32_t, SkFlattenable::Factory> fFlattenableDict;
252
253 int fVersion = 0;
254
255 sk_sp<SkTypeface>* fTFArray = nullptr;
256 int fTFCount = 0;
257
258 SkFlattenable::Factory* fFactoryArray = nullptr;
259 int fFactoryCount = 0;
260
261 SkDeserialProcs fProcs;
262
263 static bool IsPtrAlign4(const void* ptr) {
264 return SkIsAlign4((uintptr_t)ptr);
265 }
266
267 bool fAllowSkSL = true;
268 bool fError = false;
269 };
270
271 #endif // SkReadBuffer_DEFINED