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