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