Debian Codegraph @

src/core/SkReadBuffer.h

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