SkReadBuffer.h revision 55b6d8be997a447ef9ce0f029697677a940bfc24
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 "SkBitmapHeap.h"
12#include "SkColorFilter.h"
13#include "SkData.h"
14#include "SkDrawLooper.h"
15#include "SkImageFilter.h"
16#include "SkMaskFilter.h"
17#include "SkPath.h"
18#include "SkPathEffect.h"
19#include "SkPicture.h"
20#include "SkRasterizer.h"
21#include "SkReadBuffer.h"
22#include "SkReader32.h"
23#include "SkRefCnt.h"
24#include "SkShader.h"
25#include "SkWriteBuffer.h"
26#include "SkXfermode.h"
27
28class SkBitmap;
29
30#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
31    #define DEBUG_NON_DETERMINISTIC_ASSERT
32#endif
33
34class SkReadBuffer {
35public:
36    SkReadBuffer();
37    SkReadBuffer(const void* data, size_t size);
38    SkReadBuffer(SkStream* stream);
39    virtual ~SkReadBuffer();
40
41    enum Version {
42        kFilterLevelIsEnum_Version         = 23,
43        kGradientFlippedFlag_Version       = 24,
44        kDashWritesPhaseIntervals_Version  = 25,
45        kColorShaderNoBool_Version         = 26,
46        kNoUnitMappers_Version             = 27,
47        kNoMoreBitmapFlatten_Version       = 28,
48        kSimplifyLocalMatrix_Version       = 30,
49        kImageFilterUniqueID_Version       = 31,
50    };
51
52    /**
53     *  Returns true IFF the version is older than the specified version.
54     */
55    bool isVersionLT(Version targetVersion) const {
56        SkASSERT(targetVersion > 0);
57        return fVersion > 0 && fVersion < targetVersion;
58    }
59
60    /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
61    void setVersion(int version) {
62        SkASSERT(0 == fVersion || version == fVersion);
63        fVersion = version;
64    }
65
66    enum Flags {
67        kCrossProcess_Flag  = 1 << 0,
68        kScalarIsFloat_Flag = 1 << 1,
69        kPtrIs64Bit_Flag    = 1 << 2,
70        kValidation_Flag    = 1 << 3,
71    };
72
73    void setFlags(uint32_t flags) { fFlags = flags; }
74    uint32_t getFlags() const { return fFlags; }
75
76    bool isCrossProcess() const {
77        return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
78    }
79    bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
80    bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
81    bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
82
83    SkReader32* getReader32() { return &fReader; }
84
85    size_t size() { return fReader.size(); }
86    size_t offset() { return fReader.offset(); }
87    bool eof() { return fReader.eof(); }
88    virtual const void* skip(size_t size) { return fReader.skip(size); }
89    void* readFunctionPtr() { return fReader.readPtr(); }
90
91    // primitives
92    virtual bool readBool();
93    virtual SkColor readColor();
94    virtual SkFixed readFixed();
95    virtual int32_t readInt();
96    virtual SkScalar readScalar();
97    virtual uint32_t readUInt();
98    virtual int32_t read32();
99
100    // strings -- the caller is responsible for freeing the string contents
101    virtual void readString(SkString* string);
102    virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding);
103
104    // common data structures
105    virtual void readPoint(SkPoint* point);
106    SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
107    virtual void readMatrix(SkMatrix* matrix);
108    virtual void readIRect(SkIRect* rect);
109    virtual void readRect(SkRect* rect);
110    virtual void readRegion(SkRegion* region);
111    virtual void readPath(SkPath* path);
112    void readPaint(SkPaint* paint) { paint->unflatten(*this); }
113
114    virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
115    template <typename T> T* readFlattenable() {
116        return (T*) this->readFlattenable(T::GetFlattenableType());
117    }
118    SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
119    SkDrawLooper*  readDrawLooper()  { return this->readFlattenable<SkDrawLooper>(); }
120    SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
121    SkMaskFilter*  readMaskFilter()  { return this->readFlattenable<SkMaskFilter>(); }
122    SkPathEffect*  readPathEffect()  { return this->readFlattenable<SkPathEffect>(); }
123    SkRasterizer*  readRasterizer()  { return this->readFlattenable<SkRasterizer>(); }
124    SkShader*      readShader()      { return this->readFlattenable<SkShader>(); }
125    SkXfermode*    readXfermode()    { return this->readFlattenable<SkXfermode>(); }
126
127    /**
128     *  Like readFlattenable() but explicitly just skips the data that was written for the
129     *  flattenable (or the sentinel that there wasn't one).
130     */
131    virtual void skipFlattenable();
132
133    // binary data and arrays
134    virtual bool readByteArray(void* value, size_t size);
135    virtual bool readColorArray(SkColor* colors, size_t size);
136    virtual bool readIntArray(int32_t* values, size_t size);
137    virtual bool readPointArray(SkPoint* points, size_t size);
138    virtual bool readScalarArray(SkScalar* values, size_t size);
139
140    SkData* readByteArrayAsData() {
141        size_t len = this->getArrayCount();
142        if (!this->validateAvailable(len)) {
143            return SkData::NewEmpty();
144        }
145        void* buffer = sk_malloc_throw(len);
146        this->readByteArray(buffer, len);
147        return SkData::NewFromMalloc(buffer, len);
148    }
149
150    // helpers to get info about arrays and binary data
151    virtual uint32_t getArrayCount();
152
153    /**
154     *  Returns false if the bitmap could not be completely read. In that case, it will be set
155     *  to have width/height, but no pixels.
156     */
157    bool readBitmap(SkBitmap* bitmap);
158
159    virtual SkTypeface* readTypeface();
160
161    void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
162        SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
163    }
164
165    void setTypefaceArray(SkTypeface* array[], int count) {
166        fTFArray = array;
167        fTFCount = count;
168    }
169
170    /**
171     *  Call this with a pre-loaded array of Factories, in the same order as
172     *  were created/written by the writer. SkPicture uses this.
173     */
174    void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
175        fFactoryTDArray = NULL;
176        fFactoryArray = array;
177        fFactoryCount = count;
178    }
179
180    /**
181     *  Call this with an initially empty array, so the reader can cache each
182     *  factory it sees by name. Used by the pipe code in conjunction with
183     *  SkWriteBuffer::setNamedFactoryRecorder.
184     */
185    void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
186        fFactoryTDArray = array;
187        fFactoryArray = NULL;
188        fFactoryCount = 0;
189    }
190
191    /**
192     *  Provide a function to decode an SkBitmap from encoded data. Only used if the writer
193     *  encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
194     *  appropriate size will be used.
195     */
196    void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
197        fBitmapDecoder = bitmapDecoder;
198    }
199
200    // Default impelementations don't check anything.
201    virtual bool validate(bool isValid) { return true; }
202    virtual bool isValid() const { return true; }
203    virtual bool validateAvailable(size_t size) { return true; }
204
205protected:
206    SkReader32 fReader;
207
208private:
209    bool readArray(void* value, size_t size, size_t elementSize);
210
211    uint32_t fFlags;
212    int fVersion;
213
214    void* fMemoryPtr;
215
216    SkBitmapHeapReader* fBitmapStorage;
217    SkTypeface** fTFArray;
218    int        fTFCount;
219
220    SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
221    SkFlattenable::Factory* fFactoryArray;
222    int                     fFactoryCount;
223
224    SkPicture::InstallPixelRefProc fBitmapDecoder;
225
226#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
227    // Debugging counter to keep track of how many bitmaps we
228    // have decoded.
229    int fDecodedBitmapIndex;
230#endif // DEBUG_NON_DETERMINISTIC_ASSERT
231};
232
233#endif // SkReadBuffer_DEFINED
234