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 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
84 void setVersion(int version) {
85 SkASSERT(0 == fVersion || version == fVersion);
90 kCrossProcess_Flag = 1 << 0,
91 kScalarIsFloat_Flag = 1 << 1,
92 kPtrIs64Bit_Flag = 1 << 2,
93 kValidation_Flag = 1 << 3,
96 void setFlags(uint32_t flags) { fFlags = flags; }
97 uint32_t getFlags() const { return fFlags; }
99 bool isCrossProcess() const {
100 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
102 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
103 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
104 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
106 SkReader32* getReader32() { return &fReader; }
108 size_t size() { return fReader.size(); }
109 size_t offset() { return fReader.offset(); }
110 bool eof() { return fReader.eof(); }
111 virtual const void* skip(size_t size) { return fReader.skip(size); }
114 virtual bool readBool();
115 virtual SkColor readColor();
116 virtual int32_t readInt();
117 virtual SkScalar readScalar();
118 virtual uint32_t readUInt();
119 virtual int32_t read32();
122 virtual uint8_t peekByte();
124 // strings -- the caller is responsible for freeing the string contents
125 virtual void readString(SkString* string);
127 // common data structures
128 virtual void readColor4f(SkColor4f* color);
129 virtual void readPoint(SkPoint* point);
130 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
131 virtual void readMatrix(SkMatrix* matrix);
132 virtual void readIRect(SkIRect* rect);
133 virtual void readRect(SkRect* rect);
134 virtual void readRRect(SkRRect* rrect);
135 virtual void readRegion(SkRegion* region);
137 virtual void readPath(SkPath* path);
138 virtual void readPaint(SkPaint* paint) { paint->unflatten(*this); }
140 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
141 template <typename T> sk_sp<T> readFlattenable() {
142 return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
144 sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
145 sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
146 sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
147 sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
148 sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
149 sk_sp<SkRasterizer> readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
150 sk_sp<SkShader> readShader() { return this->readFlattenable<SkShader>(); }
151 sk_sp<SkXfermode> readXfermode() { return this->readFlattenable<SkXfermode>(); }
153 // binary data and arrays
154 virtual bool readByteArray(void* value, size_t size);
155 virtual bool readColorArray(SkColor* colors, size_t size);
156 virtual bool readColor4fArray(SkColor4f* colors, size_t size);
157 virtual bool readIntArray(int32_t* values, size_t size);
158 virtual bool readPointArray(SkPoint* points, size_t size);
159 virtual bool readScalarArray(SkScalar* values, size_t size);
161 sk_sp<SkData> readByteArrayAsData() {
162 size_t len = this->getArrayCount();
163 if (!this->validateAvailable(len)) {
164 return SkData::MakeEmpty();
166 void* buffer = sk_malloc_throw(len);
167 this->readByteArray(buffer, len);
168 return SkData::MakeFromMalloc(buffer, len);
171 // helpers to get info about arrays and binary data
172 virtual uint32_t getArrayCount();
174 sk_sp<SkImage> readBitmapAsImage();
175 sk_sp<SkImage> readImage();
176 virtual sk_sp<SkTypeface> readTypeface();
178 void setTypefaceArray(SkTypeface* array[], int count) {
184 * Call this with a pre-loaded array of Factories, in the same order as
185 * were created/written by the writer. SkPicture uses this.
187 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
188 fFactoryArray = array;
189 fFactoryCount = count;
193 * For an input flattenable (specified by name), set a custom factory proc
194 * to use when unflattening. Will make a copy of |name|.
196 * If the global registry already has a default factory for the flattenable,
197 * this will override that factory. If a custom factory has already been
198 * set for the flattenable, this will override that factory.
200 * Custom factories can be removed by calling setCustomFactory("...", nullptr).
202 void setCustomFactory(const SkString& name, SkFlattenable::Factory factory) {
203 fCustomFactory.set(name, factory);
206 // If nullptr is passed, then the default deserializer will be used
207 // which calls SkImage::MakeFromEncoded()
208 void setImageDeserializer(SkImageDeserializer* factory);
210 // Default impelementations don't check anything.
211 virtual bool validate(bool isValid) { return isValid; }
212 virtual bool isValid() const { return true; }
213 virtual bool validateAvailable(size_t size) { return true; }
214 bool validateIndex(int index, int count) {
215 return this->validate(index >= 0 && index < count);
218 SkInflator* getInflator() const { return fInflator; }
219 void setInflator(SkInflator* inf) { fInflator = inf; }
221 // sk_sp<SkImage> inflateImage();
225 * Allows subclass to check if we are using factories for expansion
228 int factoryCount() { return fFactoryCount; }
231 * Checks if a custom factory has been set for a given flattenable.
232 * Returns the custom factory if it exists, or nullptr otherwise.
234 SkFlattenable::Factory getCustomFactory(const SkString& name) {
235 SkFlattenable::Factory* factoryPtr = fCustomFactory.find(name);
236 return factoryPtr ? *factoryPtr : nullptr;
241 // Only used if we do not have an fFactoryArray.
242 SkTHashMap<uint32_t, SkString> fFlattenableDict;
245 bool readArray(void* value, size_t size, size_t elementSize);
252 SkTypeface** fTFArray;
255 SkFlattenable::Factory* fFactoryArray;
258 // Only used if we do not have an fFactoryArray.
259 SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory;
261 // We do not own this ptr, we just use it (guaranteed to never be null)
262 SkImageDeserializer* fImageDeserializer;
264 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
265 // Debugging counter to keep track of how many bitmaps we
267 int fDecodedBitmapIndex;
268 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
270 SkInflator* fInflator = nullptr;
273 #endif // SkReadBuffer_DEFINED