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 "SkBitmapHeap.h"
12 #include "SkColorFilter.h"
14 #include "SkDrawLooper.h"
15 #include "SkImageFilter.h"
16 #include "SkMaskFilter.h"
18 #include "SkPathEffect.h"
19 #include "SkPicture.h"
20 #include "SkRasterizer.h"
21 #include "SkReadBuffer.h"
22 #include "SkReader32.h"
25 #include "SkWriteBuffer.h"
26 #include "SkXfermode.h"
30 #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
31 #define DEBUG_NON_DETERMINISTIC_ASSERT
37 SkReadBuffer(const void* data, size_t size);
38 SkReadBuffer(SkStream* stream);
39 virtual ~SkReadBuffer();
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 kRemoveAndroidPaintOpts_Version = 32,
51 kFlattenCreateProc_Version = 33,
55 * Returns true IFF the version is older than the specified version.
57 bool isVersionLT(Version targetVersion) const {
58 SkASSERT(targetVersion > 0);
59 return fVersion > 0 && fVersion < targetVersion;
62 /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
63 void setVersion(int version) {
64 SkASSERT(0 == fVersion || version == fVersion);
69 kCrossProcess_Flag = 1 << 0,
70 kScalarIsFloat_Flag = 1 << 1,
71 kPtrIs64Bit_Flag = 1 << 2,
72 kValidation_Flag = 1 << 3,
75 void setFlags(uint32_t flags) { fFlags = flags; }
76 uint32_t getFlags() const { return fFlags; }
78 bool isCrossProcess() const {
79 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
81 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
82 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
83 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
85 SkReader32* getReader32() { return &fReader; }
87 size_t size() { return fReader.size(); }
88 size_t offset() { return fReader.offset(); }
89 bool eof() { return fReader.eof(); }
90 virtual const void* skip(size_t size) { return fReader.skip(size); }
91 void* readFunctionPtr() { return fReader.readPtr(); }
94 virtual bool readBool();
95 virtual SkColor readColor();
96 virtual SkFixed readFixed();
97 virtual int32_t readInt();
98 virtual SkScalar readScalar();
99 virtual uint32_t readUInt();
100 virtual int32_t read32();
102 // strings -- the caller is responsible for freeing the string contents
103 virtual void readString(SkString* string);
104 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding);
106 // common data structures
107 virtual void readPoint(SkPoint* point);
108 SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
109 virtual void readMatrix(SkMatrix* matrix);
110 virtual void readIRect(SkIRect* rect);
111 virtual void readRect(SkRect* rect);
112 virtual void readRegion(SkRegion* region);
114 virtual void readPath(SkPath* path);
115 void readPaint(SkPaint* paint) { paint->unflatten(*this); }
117 virtual SkFlattenable* readFlattenable(SkFlattenable::Type);
118 template <typename T> T* readFlattenable() {
119 return (T*) this->readFlattenable(T::GetFlattenableType());
121 SkColorFilter* readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
122 SkDrawLooper* readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
123 SkImageFilter* readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
124 SkMaskFilter* readMaskFilter() { return this->readFlattenable<SkMaskFilter>(); }
125 SkPathEffect* readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
126 SkRasterizer* readRasterizer() { return this->readFlattenable<SkRasterizer>(); }
127 SkShader* readShader() { return this->readFlattenable<SkShader>(); }
128 SkXfermode* readXfermode() { return this->readFlattenable<SkXfermode>(); }
131 * Like readFlattenable() but explicitly just skips the data that was written for the
132 * flattenable (or the sentinel that there wasn't one).
134 virtual void skipFlattenable();
136 // binary data and arrays
137 virtual bool readByteArray(void* value, size_t size);
138 virtual bool readColorArray(SkColor* colors, size_t size);
139 virtual bool readIntArray(int32_t* values, size_t size);
140 virtual bool readPointArray(SkPoint* points, size_t size);
141 virtual bool readScalarArray(SkScalar* values, size_t size);
143 SkData* readByteArrayAsData() {
144 size_t len = this->getArrayCount();
145 if (!this->validateAvailable(len)) {
146 return SkData::NewEmpty();
148 void* buffer = sk_malloc_throw(len);
149 this->readByteArray(buffer, len);
150 return SkData::NewFromMalloc(buffer, len);
153 // helpers to get info about arrays and binary data
154 virtual uint32_t getArrayCount();
157 * Returns false if the bitmap could not be completely read. In that case, it will be set
158 * to have width/height, but no pixels.
160 bool readBitmap(SkBitmap* bitmap);
162 virtual SkTypeface* readTypeface();
164 void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
165 SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
168 void setTypefaceArray(SkTypeface* array[], int count) {
174 * Call this with a pre-loaded array of Factories, in the same order as
175 * were created/written by the writer. SkPicture uses this.
177 void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
178 fFactoryTDArray = NULL;
179 fFactoryArray = array;
180 fFactoryCount = count;
184 * Call this with an initially empty array, so the reader can cache each
185 * factory it sees by name. Used by the pipe code in conjunction with
186 * SkWriteBuffer::setNamedFactoryRecorder.
188 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
189 fFactoryTDArray = array;
190 fFactoryArray = NULL;
195 * Provide a function to decode an SkBitmap from encoded data. Only used if the writer
196 * encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
197 * appropriate size will be used.
199 void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
200 fBitmapDecoder = bitmapDecoder;
203 // Default impelementations don't check anything.
204 virtual bool validate(bool isValid) { return true; }
205 virtual bool isValid() const { return true; }
206 virtual bool validateAvailable(size_t size) { return true; }
212 bool readArray(void* value, size_t size, size_t elementSize);
219 SkBitmapHeapReader* fBitmapStorage;
220 SkTypeface** fTFArray;
223 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
224 SkFlattenable::Factory* fFactoryArray;
227 SkPicture::InstallPixelRefProc fBitmapDecoder;
229 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT
230 // Debugging counter to keep track of how many bitmaps we
232 int fDecodedBitmapIndex;
233 #endif // DEBUG_NON_DETERMINISTIC_ASSERT
236 #endif // SkReadBuffer_DEFINED