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,
75 * Returns true IFF the version is older than the specified version.
77 bool isVersionLT(Version targetVersion) const {
78 SkASSERT(targetVersion > 0);
79 return fVersion > 0 && fVersion < targetVersion;
82 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
83 void setVersion(int version) {
84 SkASSERT(0 == fVersion || version == fVersion);
89 kCrossProcess_Flag = 1 << 0,
90 kScalarIsFloat_Flag = 1 << 1,
91 kPtrIs64Bit_Flag = 1 << 2,
92 kValidation_Flag = 1 << 3,
95 void setFlags(uint32_t flags) { fFlags = flags; }
96 uint32_t getFlags() const { return fFlags; }
98 bool isCrossProcess() const {
99 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
101 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
102 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
103 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
105 SkReader32* getReader32() { return &fReader; }
107 size_t size() { return fReader.size(); }
108 size_t offset() { return fReader.offset(); }
109 bool eof() { return fReader.eof(); }
110 virtual const void* skip(size_t size) { return fReader.skip(size); }
113 virtual bool readBool();
114 virtual SkColor readColor();
115 virtual int32_t readInt();
116 virtual SkScalar readScalar();
117 virtual uint32_t readUInt();
118 virtual int32_t read32();
121 virtual uint8_t peekByte();
123 // strings -- the caller is responsible for freeing the string contents
124 virtual void readString(SkString* string);
126 // common data structures
127 virtual void readColor4f(SkColor4f* color);
128 virtual void readPoint(SkPoint* point);
129 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
130 virtual void readMatrix(SkMatrix* matrix);
131 virtual void readIRect(SkIRect* rect);
132 virtual void readRect(SkRect* rect);
133 virtual void readRRect(SkRRect* rrect);
134 virtual void readRegion(SkRegion* region);
136 virtual void readPath(SkPath* path);
137 virtual void readPaint(SkPaint* paint) { paint->unflatten(*this); }
139 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
140 template <typename T> sk_sp<T> readFlattenable() {
141 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
143 sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
144 sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
145 sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
146 sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
147 sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
148 sk_sp<SkRasterizer> readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
149 sk_sp<SkShader> readShader() { return this->readFlattenable<SkShader>(); }
150 sk_sp<SkXfermode> readXfermode() { return this->readFlattenable<SkXfermode>(); }
152 // binary data and arrays
153 virtual bool readByteArray(void* value, size_t size);
154 virtual bool readColorArray(SkColor* colors, size_t size);
155 virtual bool readColor4fArray(SkColor4f* colors, size_t size);
156 virtual bool readIntArray(int32_t* values, size_t size);
157 virtual bool readPointArray(SkPoint* points, size_t size);
158 virtual bool readScalarArray(SkScalar* values, size_t size);
160 sk_sp<SkData> readByteArrayAsData() {
161 size_t len = this->getArrayCount();
162 if (!this->validateAvailable(len)) {
163 return SkData::MakeEmpty();
165 void* buffer = sk_malloc_throw(len);
166 this->readByteArray(buffer, len);
167 return SkData::MakeFromMalloc(buffer, len);
170 // helpers to get info about arrays and binary data
171 virtual uint32_t getArrayCount();
173 sk_sp<SkImage> readBitmapAsImage();
174 sk_sp<SkImage> readImage();
175 virtual sk_sp<SkTypeface> readTypeface();
177 void setTypefaceArray(SkTypeface* array[], int count) {
183 * Call this with a pre-loaded array of Factories, in the same order as
184 * were created/written by the writer. SkPicture uses this.
186 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
187 fFactoryArray = array;
188 fFactoryCount = count;
192 * For an input flattenable (specified by name), set a custom factory proc
193 * to use when unflattening. Will make a copy of |name|.
195 * If the global registry already has a default factory for the flattenable,
196 * this will override that factory. If a custom factory has already been
197 * set for the flattenable, this will override that factory.
199 * Custom factories can be removed by calling setCustomFactory("...", nullptr).
201 void setCustomFactory(const SkString& name, SkFlattenable::Factory factory) {
202 fCustomFactory.set(name, factory);
205 // If nullptr is passed, then the default deserializer will be used
206 // which calls SkImage::MakeFromEncoded()
207 void setImageDeserializer(SkImageDeserializer* factory);
209 // Default impelementations don't check anything.
210 virtual bool validate(bool isValid) { return isValid; }
211 virtual bool isValid() const { return true; }
212 virtual bool validateAvailable(size_t size) { return true; }
213 bool validateIndex(int index, int count) {
214 return this->validate(index >= 0 && index < count);
217 SkInflator* getInflator() const { return fInflator; }
218 void setInflator(SkInflator* inf) { fInflator = inf; }
220 // sk_sp<SkImage> inflateImage();
224 * Allows subclass to check if we are using factories for expansion
227 int factoryCount() { return fFactoryCount; }
230 * Checks if a custom factory has been set for a given flattenable.
231 * Returns the custom factory if it exists, or nullptr otherwise.
233 SkFlattenable::Factory getCustomFactory(const SkString& name) {
234 SkFlattenable::Factory* factoryPtr = fCustomFactory.find(name);
235 return factoryPtr ? *factoryPtr : nullptr;
240 // Only used if we do not have an fFactoryArray.
241 SkTHashMap<uint32_t, SkString> fFlattenableDict;
244 bool readArray(void* value, size_t size, size_t elementSize);
251 SkTypeface** fTFArray;
254 SkFlattenable::Factory* fFactoryArray;
257 // Only used if we do not have an fFactoryArray.
258 SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory;
260 // We do not own this ptr, we just use it (guaranteed to never be null)
261 SkImageDeserializer* fImageDeserializer;
263 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
264 // Debugging counter to keep track of how many bitmaps we
266 int fDecodedBitmapIndex;
267 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
269 SkInflator* fInflator = nullptr;
272 #endif // SkReadBuffer_DEFINED