xref: /aosp_15_r20/external/skia/src/core/SkReadBuffer.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/private/base/SkAlign.h"
25 #include "include/private/base/SkAssert.h"
26 #include "src/core/SkBlenderBase.h"
27 #include "src/core/SkImageFilter_Base.h"
28 #include "src/core/SkMaskFilterBase.h"
29 #include "src/core/SkPaintPriv.h"
30 #include "src/core/SkPicturePriv.h"
31 #include "src/core/SkSamplingPriv.h"
32 #include "src/core/SkTHash.h"
33 #include "src/effects/colorfilters/SkColorFilterBase.h"
34 #include "src/shaders/SkShaderBase.h"
35 
36 #include <cstddef>
37 #include <cstdint>
38 
39 class SkBlender;
40 class SkData;
41 class SkImage;
42 class SkM44;
43 class SkMaskFilter;
44 class SkMatrix;
45 class SkPath;
46 class SkRRect;
47 class SkRegion;
48 class SkString;
49 class SkTypeface;
50 struct SkPoint3;
51 
52 class SkReadBuffer {
53 public:
54     SkReadBuffer() = default;
SkReadBuffer(const void * data,size_t size)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      */
isVersionLT(SkPicturePriv::Version targetVersion)64     bool isVersionLT(SkPicturePriv::Version targetVersion) const {
65         SkASSERT(targetVersion > 0);
66         return fVersion > 0 && fVersion < targetVersion;
67     }
68 
getVersion()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. */
setVersion(int version)72     void setVersion(int version) {
73         SkASSERT(0 == fVersion || version == fVersion);
74         fVersion = version;
75     }
76 
size()77     size_t size() const { return fStop - fBase; }
offset()78     size_t offset() const { return fCurr - fBase; }
eof()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
available()82     size_t available() const { return fStop - fCurr; }
83 
skipT()84     template <typename T> const T* skipT() {
85         return static_cast<const T*>(this->skip(sizeof(T)));
86     }
skipT(size_t count)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();
93     SkColor readColor();
94     int32_t readInt();
95     SkScalar readScalar();
96     uint32_t readUInt();
97     int32_t read32();
98 
read32LE(T max)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
113     void readColor4f(SkColor4f* color);
114     void readPoint(SkPoint* point);
readPoint()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);
121     SkRect readRect();
122     void readRRect(SkRRect* rrect);
123     void readRegion(SkRegion* region);
124 
125     void readPath(SkPath* path);
126 
readPaint()127     SkPaint readPaint() {
128         return SkPaintPriv::Unflatten(*this);
129     }
130 
131     SkFlattenable* readRawFlattenable();
132     SkFlattenable* readFlattenable(SkFlattenable::Type);
readFlattenable()133     template <typename T> sk_sp<T> readFlattenable() {
134         return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
135     }
readColorFilter()136     sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilterBase>(); }
readImageFilter()137     sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter_Base>(); }
readBlender()138     sk_sp<SkBlender> readBlender() { return this->readFlattenable<SkBlenderBase>(); }
readMaskFilter()139     sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilterBase>(); }
readPathEffect()140     sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
readShader()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 
156     sk_sp<SkData> readByteArrayAsData();
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.
164     sk_sp<SkImage> readImage();
165     sk_sp<SkTypeface> readTypeface();
166 
setTypefaceArray(sk_sp<SkTypeface> array[],int count)167     void setTypefaceArray(sk_sp<SkTypeface> array[], int count) {
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      */
setFactoryPlayback(SkFlattenable::Factory array[],int count)176     void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
177         fFactoryArray = array;
178         fFactoryCount = count;
179     }
180 
181     void setDeserialProcs(const SkDeserialProcs& procs);
getDeserialProcs()182     const SkDeserialProcs& getDeserialProcs() const { return fProcs; }
183 
allowSkSL()184     bool allowSkSL() const { return fAllowSkSL; }
setAllowSkSL(bool allow)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      */
validate(bool isValid)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>
validateCanReadN(size_t n)204     bool validateCanReadN(size_t n) {
205         return this->validate(n <= (this->available() / sizeof(T)));
206     }
207 
isValid()208     bool isValid() const { return !fError; }
validateIndex(int index,int count)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 
checkRange(T min,T max)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 
224     SkLegacyFQ checkFilterQuality();
225 
226     SkSamplingOptions readSampling();
227 
228 private:
229     const char* readString(size_t* length);
230 
231     void setInvalid();
232     bool readArray(void* value, size_t size, size_t elementSize);
isAvailable(size_t size)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.
241     skia_private::THashMap<uint32_t, SkFlattenable::Factory> fFlattenableDict;
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 
IsPtrAlign4(const void * ptr)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
262