2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkReadBuffer_DEFINED
9 #define SkReadBuffer_DEFINED
11 #include "SkColorFilter.h"
13 #include "SkDrawLooper.h"
14 #include "SkImageFilter.h"
15 #include "SkMaskFilter.h"
17 #include "SkPathEffect.h"
18 #include "SkPicture.h"
19 #include "SkRasterizer.h"
20 #include "SkReadBuffer.h"
21 #include "SkReader32.h"
25 #include "SkWriteBuffer.h"
26 #include "SkXfermode.h"
32 #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
33 #define DEBUG_NON_DETERMINISTIC_ASSERT
39 SkReadBuffer(const void* data, size_t size);
40 SkReadBuffer(SkStream* stream);
41 virtual ~SkReadBuffer();
43 virtual SkReadBuffer* clone(const void* data, size_t size) const {
44 return new SkReadBuffer(data, size);
49 kFilterLevelIsEnum_Version = 23,
50 kGradientFlippedFlag_Version = 24,
51 kDashWritesPhaseIntervals_Version = 25,
52 kColorShaderNoBool_Version = 26,
53 kNoUnitMappers_Version = 27,
54 kNoMoreBitmapFlatten_Version = 28,
55 kSimplifyLocalMatrix_Version = 30,
56 kImageFilterUniqueID_Version = 31,
57 kRemoveAndroidPaintOpts_Version = 32,
58 kFlattenCreateProc_Version = 33,
60 kRemoveColorTableAlpha_Version = 36,
61 kDropShadowMode_Version = 37,
62 kPictureImageFilterResolution_Version = 38,
63 kPictureImageFilterLevel_Version = 39,
64 kImageFilterNoUniqueID_Version = 40,
65 kBitmapSourceFilterQuality_Version = 41,
66 kPictureShaderHasPictureBool_Version = 42,
67 kHasDrawImageOpCodes_Version = 43,
68 kAnnotationsMovedToCanvas_Version = 44,
69 kLightingShaderWritesInvNormRotation = 45,
70 kBlurMaskFilterWritesOccluder = 47,
71 kGradientShaderFloatColor_Version = 49,
72 kXfermodeToBlendMode_Version = 50,
76 * Returns true IFF the version is older than the specified version.
78 bool isVersionLT(Version targetVersion) const {
79 SkASSERT(targetVersion > 0);
80 return fVersion > 0 && fVersion < targetVersion;
83 uint32_t getVersion() const { return fVersion; }
85 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
86 void setVersion(int version) {
87 SkASSERT(0 == fVersion || version == fVersion);
92 kCrossProcess_Flag = 1 << 0,
93 kScalarIsFloat_Flag = 1 << 1,
94 kPtrIs64Bit_Flag = 1 << 2,
95 kValidation_Flag = 1 << 3,
98 void setFlags(uint32_t flags) { fFlags = flags; }
99 uint32_t getFlags() const { return fFlags; }
101 bool isCrossProcess() const {
102 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
104 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
105 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
106 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
108 SkReader32* getReader32() { return &fReader; }
110 size_t size() { return fReader.size(); }
111 size_t offset() { return fReader.offset(); }
112 bool eof() { return fReader.eof(); }
113 virtual const void* skip(size_t size) { return fReader.skip(size); }
116 virtual bool readBool();
117 virtual SkColor readColor();
118 virtual int32_t readInt();
119 virtual SkScalar readScalar();
120 virtual uint32_t readUInt();
121 virtual int32_t read32();
124 virtual uint8_t peekByte();
126 // strings -- the caller is responsible for freeing the string contents
127 virtual void readString(SkString* string);
129 // common data structures
130 virtual void readColor4f(SkColor4f* color);
131 virtual void readPoint(SkPoint* point);
132 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
133 virtual void readMatrix(SkMatrix* matrix);
134 virtual void readIRect(SkIRect* rect);
135 virtual void readRect(SkRect* rect);
136 virtual void readRRect(SkRRect* rrect);
137 virtual void readRegion(SkRegion* region);
139 virtual void readPath(SkPath* path);
140 virtual void readPaint(SkPaint* paint) { paint->unflatten(*this); }
142 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
143 template <typename T> sk_sp<T> readFlattenable() {
144 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
146 sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
147 sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
148 sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
149 sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
150 sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
151 sk_sp<SkRasterizer> readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
152 sk_sp<SkShader> readShader() { return this->readFlattenable<SkShader>(); }
153 sk_sp<SkXfermode> readXfermode() { return this->readFlattenable<SkXfermode>(); }
155 // binary data and arrays
156 virtual bool readByteArray(void* value, size_t size);
157 virtual bool readColorArray(SkColor* colors, size_t size);
158 virtual bool readColor4fArray(SkColor4f* colors, size_t size);
159 virtual bool readIntArray(int32_t* values, size_t size);
160 virtual bool readPointArray(SkPoint* points, size_t size);
161 virtual bool readScalarArray(SkScalar* values, size_t size);
163 sk_sp<SkData> readByteArrayAsData() {
164 size_t len = this->getArrayCount();
165 if (!this->validateAvailable(len)) {
166 return SkData::MakeEmpty();
168 void* buffer = sk_malloc_throw(len);
169 this->readByteArray(buffer, len);
170 return SkData::MakeFromMalloc(buffer, len);
173 // helpers to get info about arrays and binary data
174 virtual uint32_t getArrayCount();
176 sk_sp<SkImage> readBitmapAsImage();
177 sk_sp<SkImage> readImage();
178 virtual sk_sp<SkTypeface> readTypeface();
180 void setTypefaceArray(SkTypeface* array[], int count) {
186 * Call this with a pre-loaded array of Factories, in the same order as
187 * were created/written by the writer. SkPicture uses this.
189 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
190 fFactoryArray = array;
191 fFactoryCount = count;
195 * For an input flattenable (specified by name), set a custom factory proc
196 * to use when unflattening. Will make a copy of |name|.
198 * If the global registry already has a default factory for the flattenable,
199 * this will override that factory. If a custom factory has already been
200 * set for the flattenable, this will override that factory.
202 * Custom factories can be removed by calling setCustomFactory("...", nullptr).
204 void setCustomFactory(const SkString& name, SkFlattenable::Factory factory) {
205 fCustomFactory.set(name, factory);
208 // If nullptr is passed, then the default deserializer will be used
209 // which calls SkImage::MakeFromEncoded()
210 void setImageDeserializer(SkImageDeserializer* factory);
212 // Default impelementations don't check anything.
213 virtual bool validate(bool isValid) { return isValid; }
214 virtual bool isValid() const { return true; }
215 virtual bool validateAvailable(size_t size) { return true; }
216 bool validateIndex(int index, int count) {
217 return this->validate(index >= 0 && index < count);
220 SkInflator* getInflator() const { return fInflator; }
221 void setInflator(SkInflator* inf) { fInflator = inf; }
223 // sk_sp<SkImage> inflateImage();
227 * Allows subclass to check if we are using factories for expansion
230 int factoryCount() { return fFactoryCount; }
233 * Checks if a custom factory has been set for a given flattenable.
234 * Returns the custom factory if it exists, or nullptr otherwise.
236 SkFlattenable::Factory getCustomFactory(const SkString& name) {
237 SkFlattenable::Factory* factoryPtr = fCustomFactory.find(name);
238 return factoryPtr ? *factoryPtr : nullptr;
243 // Only used if we do not have an fFactoryArray.
244 SkTHashMap<uint32_t, SkString> fFlattenableDict;
247 bool readArray(void* value, size_t size, size_t elementSize);
254 SkTypeface** fTFArray;
257 SkFlattenable::Factory* fFactoryArray;
260 // Only used if we do not have an fFactoryArray.
261 SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory;
263 // We do not own this ptr, we just use it (guaranteed to never be null)
264 SkImageDeserializer* fImageDeserializer;
266 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
267 // Debugging counter to keep track of how many bitmaps we
269 int fDecodedBitmapIndex;
270 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
272 SkInflator* fInflator = nullptr;
275 #endif // SkReadBuffer_DEFINED