Flutter Engine
The Flutter Engine
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*);
119 void readIRect(SkIRect* rect);
120 void readRect(SkRect* rect);
122 void readRRect(SkRRect* rrect);
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
Definition: FontMgrTest.cpp:50
static const int points[]
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
Definition: SkData.h:25
sk_sp< SkFlattenable >(* Factory)(SkReadBuffer &)
Definition: SkFlattenable.h:41
Definition: SkM44.h:150
static SkPaint Unflatten(SkReadBuffer &buffer)
Definition: SkPath.h:59
const T * skipT(size_t count)
Definition: SkReadBuffer.h:87
bool readColor4fArray(SkColor4f *colors, size_t size)
SkReadBuffer()=default
uint32_t getVersion() const
Definition: SkReadBuffer.h:69
SkPaint readPaint()
Definition: SkReadBuffer.h:127
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)
Definition: SkReadBuffer.h:191
bool readIntArray(int32_t *values, size_t size)
uint32_t readUInt()
bool isValid() const
Definition: SkReadBuffer.h:208
const void * skip(size_t size)
sk_sp< SkData > readByteArrayAsData()
void readIRect(SkIRect *rect)
void setAllowSkSL(bool allow)
Definition: SkReadBuffer.h:185
bool validateIndex(int index, int count)
Definition: SkReadBuffer.h:209
int32_t read32()
void setDeserialProcs(const SkDeserialProcs &procs)
const SkDeserialProcs & getDeserialProcs() const
Definition: SkReadBuffer.h:182
void setTypefaceArray(sk_sp< SkTypeface > array[], int count)
Definition: SkReadBuffer.h:167
SkSamplingOptions readSampling()
sk_sp< SkPathEffect > readPathEffect()
Definition: SkReadBuffer.h:140
T checkRange(T min, T max)
Definition: SkReadBuffer.h:219
void setFactoryPlayback(SkFlattenable::Factory array[], int count)
Definition: SkReadBuffer.h:176
const T * skipT()
Definition: SkReadBuffer.h:84
size_t size() const
Definition: SkReadBuffer.h:77
void read(SkM44 *)
bool readPointArray(SkPoint *points, size_t size)
size_t available() const
Definition: SkReadBuffer.h:82
sk_sp< SkColorFilter > readColorFilter()
Definition: SkReadBuffer.h:136
SkRect readRect()
sk_sp< SkBlender > readBlender()
Definition: SkReadBuffer.h:138
int32_t readInt()
sk_sp< SkTypeface > readTypeface()
void readString(SkString *string)
void setVersion(int version)
Definition: SkReadBuffer.h:72
SkReadBuffer(const void *data, size_t size)
Definition: SkReadBuffer.h:55
bool readColorArray(SkColor *colors, size_t size)
size_t offset() const
Definition: SkReadBuffer.h:78
bool readPad32(void *buffer, size_t bytes)
sk_sp< SkMaskFilter > readMaskFilter()
Definition: SkReadBuffer.h:139
sk_sp< SkShader > readShader()
Definition: SkReadBuffer.h:141
int32_t checkInt(int min, int max)
sk_sp< T > readFlattenable()
Definition: SkReadBuffer.h:133
bool readScalarArray(SkScalar *values, size_t size)
SkColor readColor()
void readPoint3(SkPoint3 *point)
sk_sp< SkImage > readImage()
bool allowSkSL() const
Definition: SkReadBuffer.h:184
void readRRect(SkRRect *rrect)
bool validateCanReadN(size_t n)
Definition: SkReadBuffer.h:204
uint32_t getArrayCount()
SkScalar readScalar()
void readRegion(SkRegion *region)
void readPath(SkPath *path)
T read32LE(T max)
Definition: SkReadBuffer.h:99
sk_sp< SkImageFilter > readImageFilter()
Definition: SkReadBuffer.h:137
bool isVersionLT(SkPicturePriv::Version targetVersion) const
Definition: SkReadBuffer.h:64
SkPoint readPoint()
Definition: SkReadBuffer.h:115
DlColor color
float SkScalar
Definition: extension.cpp:12
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
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
ClipOpAndAA opAA SkRegion region
Definition: SkRecords.h:238
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
#define T
Definition: precompiler.cc:65
Definition: SkRect.h:32
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63