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