Revert "replace SkXfermode obj with SkBlendMode enum in paints"
[platform/upstream/libSkiaSharp.git] / src / core / SkReadBuffer.h
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 "SkColorFilter.h"
12 #include "SkData.h"
13 #include "SkDrawLooper.h"
14 #include "SkImageFilter.h"
15 #include "SkMaskFilter.h"
16 #include "SkPath.h"
17 #include "SkPathEffect.h"
18 #include "SkPicture.h"
19 #include "SkRasterizer.h"
20 #include "SkReadBuffer.h"
21 #include "SkReader32.h"
22 #include "SkRefCnt.h"
23 #include "SkShader.h"
24 #include "SkTHash.h"
25 #include "SkWriteBuffer.h"
26 #include "SkXfermode.h"
27
28 class SkBitmap;
29 class SkImage;
30 class SkInflator;
31
32 #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
33     #define DEBUG_NON_DETERMINISTIC_ASSERT
34 #endif
35
36 class SkReadBuffer {
37 public:
38     SkReadBuffer();
39     SkReadBuffer(const void* data, size_t size);
40     SkReadBuffer(SkStream* stream);
41     virtual ~SkReadBuffer();
42
43     virtual SkReadBuffer* clone(const void* data, size_t size) const {
44         return new SkReadBuffer(data, size);
45     }
46
47     enum Version {
48         /*
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,
59         */
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     };
73
74     /**
75      *  Returns true IFF the version is older than the specified version.
76      */
77     bool isVersionLT(Version targetVersion) const {
78         SkASSERT(targetVersion > 0);
79         return fVersion > 0 && fVersion < targetVersion;
80     }
81
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);
85         fVersion = version;
86     }
87
88     enum Flags {
89         kCrossProcess_Flag  = 1 << 0,
90         kScalarIsFloat_Flag = 1 << 1,
91         kPtrIs64Bit_Flag    = 1 << 2,
92         kValidation_Flag    = 1 << 3,
93     };
94
95     void setFlags(uint32_t flags) { fFlags = flags; }
96     uint32_t getFlags() const { return fFlags; }
97
98     bool isCrossProcess() const {
99         return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
100     }
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); }
104
105     SkReader32* getReader32() { return &fReader; }
106
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); }
111
112     // primitives
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();
119
120     // peek
121     virtual uint8_t peekByte();
122
123     // strings -- the caller is responsible for freeing the string contents
124     virtual void readString(SkString* string);
125
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);
135
136     virtual void readPath(SkPath* path);
137     virtual void readPaint(SkPaint* paint) { paint->unflatten(*this); }
138
139     virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
140     template <typename T> sk_sp<T> readFlattenable() {
141         return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
142     }
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>(); }
151
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);
159
160     sk_sp<SkData> readByteArrayAsData() {
161         size_t len = this->getArrayCount();
162         if (!this->validateAvailable(len)) {
163             return SkData::MakeEmpty();
164         }
165         void* buffer = sk_malloc_throw(len);
166         this->readByteArray(buffer, len);
167         return SkData::MakeFromMalloc(buffer, len);
168     }
169
170     // helpers to get info about arrays and binary data
171     virtual uint32_t getArrayCount();
172
173     sk_sp<SkImage> readBitmapAsImage();
174     sk_sp<SkImage> readImage();
175     virtual sk_sp<SkTypeface> readTypeface();
176
177     void setTypefaceArray(SkTypeface* array[], int count) {
178         fTFArray = array;
179         fTFCount = count;
180     }
181
182     /**
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.
185      */
186     void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
187         fFactoryArray = array;
188         fFactoryCount = count;
189     }
190
191     /**
192      *  For an input flattenable (specified by name), set a custom factory proc
193      *  to use when unflattening.  Will make a copy of |name|.
194      *
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.
198      *
199      *  Custom factories can be removed by calling setCustomFactory("...", nullptr).
200      */
201     void setCustomFactory(const SkString& name, SkFlattenable::Factory factory) {
202         fCustomFactory.set(name, factory);
203     }
204
205     // If nullptr is passed, then the default deserializer will be used
206     // which calls SkImage::MakeFromEncoded()
207     void setImageDeserializer(SkImageDeserializer* factory);
208
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);
215     }
216
217     SkInflator* getInflator() const { return fInflator; }
218     void setInflator(SkInflator* inf) { fInflator = inf; }
219
220 //    sk_sp<SkImage> inflateImage();
221     
222 protected:
223     /**
224      *  Allows subclass to check if we are using factories for expansion
225      *  of flattenables.
226      */
227     int factoryCount() { return fFactoryCount; }
228
229     /**
230      *  Checks if a custom factory has been set for a given flattenable.
231      *  Returns the custom factory if it exists, or nullptr otherwise.
232      */
233     SkFlattenable::Factory getCustomFactory(const SkString& name) {
234         SkFlattenable::Factory* factoryPtr = fCustomFactory.find(name);
235         return factoryPtr ? *factoryPtr : nullptr;
236     }
237
238     SkReader32 fReader;
239
240     // Only used if we do not have an fFactoryArray.
241     SkTHashMap<uint32_t, SkString> fFlattenableDict;
242
243 private:
244     bool readArray(void* value, size_t size, size_t elementSize);
245
246     uint32_t fFlags;
247     int fVersion;
248
249     void* fMemoryPtr;
250
251     SkTypeface** fTFArray;
252     int        fTFCount;
253
254     SkFlattenable::Factory* fFactoryArray;
255     int                     fFactoryCount;
256
257     // Only used if we do not have an fFactoryArray.
258     SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory;
259
260     // We do not own this ptr, we just use it (guaranteed to never be null)
261     SkImageDeserializer* fImageDeserializer;
262
263 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
264     // Debugging counter to keep track of how many bitmaps we
265     // have decoded.
266     int fDecodedBitmapIndex;
267 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
268
269     SkInflator* fInflator = nullptr;
270 };
271
272 #endif // SkReadBuffer_DEFINED