6b0d332e0f4316d2495051dad1d3be692ae29d72
[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         kXfermodeToBlendMode_Version       = 50,
73     };
74
75     /**
76      *  Returns true IFF the version is older than the specified version.
77      */
78     bool isVersionLT(Version targetVersion) const {
79         SkASSERT(targetVersion > 0);
80         return fVersion > 0 && fVersion < targetVersion;
81     }
82
83     uint32_t getVersion() const { return fVersion; }
84
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);
88         fVersion = version;
89     }
90
91     enum Flags {
92         kCrossProcess_Flag  = 1 << 0,
93         kScalarIsFloat_Flag = 1 << 1,
94         kPtrIs64Bit_Flag    = 1 << 2,
95         kValidation_Flag    = 1 << 3,
96     };
97
98     void setFlags(uint32_t flags) { fFlags = flags; }
99     uint32_t getFlags() const { return fFlags; }
100
101     bool isCrossProcess() const {
102         return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
103     }
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); }
107
108     SkReader32* getReader32() { return &fReader; }
109
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); }
114
115     // primitives
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();
122
123     // peek
124     virtual uint8_t peekByte();
125
126     // strings -- the caller is responsible for freeing the string contents
127     virtual void readString(SkString* string);
128
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);
138
139     virtual void readPath(SkPath* path);
140     virtual void readPaint(SkPaint* paint) { paint->unflatten(*this); }
141
142     virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
143     template <typename T> sk_sp<T> readFlattenable() {
144         return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
145     }
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>(); }
154
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);
162
163     sk_sp<SkData> readByteArrayAsData() {
164         size_t len = this->getArrayCount();
165         if (!this->validateAvailable(len)) {
166             return SkData::MakeEmpty();
167         }
168         void* buffer = sk_malloc_throw(len);
169         this->readByteArray(buffer, len);
170         return SkData::MakeFromMalloc(buffer, len);
171     }
172
173     // helpers to get info about arrays and binary data
174     virtual uint32_t getArrayCount();
175
176     sk_sp<SkImage> readBitmapAsImage();
177     sk_sp<SkImage> readImage();
178     virtual sk_sp<SkTypeface> readTypeface();
179
180     void setTypefaceArray(SkTypeface* array[], int count) {
181         fTFArray = array;
182         fTFCount = count;
183     }
184
185     /**
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.
188      */
189     void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
190         fFactoryArray = array;
191         fFactoryCount = count;
192     }
193
194     /**
195      *  For an input flattenable (specified by name), set a custom factory proc
196      *  to use when unflattening.  Will make a copy of |name|.
197      *
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.
201      *
202      *  Custom factories can be removed by calling setCustomFactory("...", nullptr).
203      */
204     void setCustomFactory(const SkString& name, SkFlattenable::Factory factory) {
205         fCustomFactory.set(name, factory);
206     }
207
208     // If nullptr is passed, then the default deserializer will be used
209     // which calls SkImage::MakeFromEncoded()
210     void setImageDeserializer(SkImageDeserializer* factory);
211
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);
218     }
219
220     SkInflator* getInflator() const { return fInflator; }
221     void setInflator(SkInflator* inf) { fInflator = inf; }
222
223 //    sk_sp<SkImage> inflateImage();
224     
225 protected:
226     /**
227      *  Allows subclass to check if we are using factories for expansion
228      *  of flattenables.
229      */
230     int factoryCount() { return fFactoryCount; }
231
232     /**
233      *  Checks if a custom factory has been set for a given flattenable.
234      *  Returns the custom factory if it exists, or nullptr otherwise.
235      */
236     SkFlattenable::Factory getCustomFactory(const SkString& name) {
237         SkFlattenable::Factory* factoryPtr = fCustomFactory.find(name);
238         return factoryPtr ? *factoryPtr : nullptr;
239     }
240
241     SkReader32 fReader;
242
243     // Only used if we do not have an fFactoryArray.
244     SkTHashMap<uint32_t, SkString> fFlattenableDict;
245
246 private:
247     bool readArray(void* value, size_t size, size_t elementSize);
248
249     uint32_t fFlags;
250     int fVersion;
251
252     void* fMemoryPtr;
253
254     SkTypeface** fTFArray;
255     int        fTFCount;
256
257     SkFlattenable::Factory* fFactoryArray;
258     int                     fFactoryCount;
259
260     // Only used if we do not have an fFactoryArray.
261     SkTHashMap<SkString, SkFlattenable::Factory> fCustomFactory;
262
263     // We do not own this ptr, we just use it (guaranteed to never be null)
264     SkImageDeserializer* fImageDeserializer;
265
266 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
267     // Debugging counter to keep track of how many bitmaps we
268     // have decoded.
269     int fDecodedBitmapIndex;
270 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
271
272     SkInflator* fInflator = nullptr;
273 };
274
275 #endif // SkReadBuffer_DEFINED