OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #ifndef SkFlattenableBuffers_DEFINED | 9 #ifndef SkFlattenableBuffers_DEFINED |
10 #define SkFlattenableBuffers_DEFINED | 10 #define SkFlattenableBuffers_DEFINED |
11 | 11 |
12 #include "SkColor.h" | 12 #include "SkColor.h" |
13 #include "SkData.h" | 13 #include "SkData.h" |
14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
15 #include "SkPoint.h" | 15 #include "SkPoint.h" |
16 | 16 |
17 class SkBitmap; | 17 class SkBitmap; |
18 class SkDrawLooper; | 18 class SkDrawLooper; |
19 class SkFlattenable; | 19 class SkFlattenable; |
20 struct SkIRect; | 20 struct SkIRect; |
21 class SkMatrix; | 21 class SkMatrix; |
22 class SkOrderedReadBuffer; | 22 class SkOrderedReadBuffer; |
23 class SkOrderedWriteBuffer; | 23 class SkOrderedWriteBuffer; |
24 class SkPath; | 24 class SkPath; |
25 class SkPixelRef; | 25 class SkPixelRef; |
26 struct SkRect; | 26 struct SkRect; |
27 class SkRefCnt; | |
28 class SkRegion; | 27 class SkRegion; |
29 class SkStream; | 28 class SkStream; |
30 class SkString; | 29 class SkString; |
31 class SkTypeface; | 30 class SkTypeface; |
32 class SkUnitMapper; | 31 class SkUnitMapper; |
33 class SkWStream; | 32 class SkWStream; |
34 | 33 |
35 enum SkEffectType { | |
36 kColorFilter_SkEffectType, | |
37 kDrawLooper_SkEffectType, | |
38 kImageFilter_SkEffectType, | |
39 kMaskFilter_SkEffectType, | |
40 kPathEffect_SkEffectType, | |
41 kPixelRef_SkEffectType, | |
42 kRasterizer_SkEffectType, | |
43 kShader_SkEffectType, | |
44 kUnitMapper_SkEffectType, | |
45 kXfermode_SkEffectType, | |
46 }; | |
47 | |
48 class SkFlattenableReadBuffer { | 34 class SkFlattenableReadBuffer { |
49 public: | 35 public: |
50 SkFlattenableReadBuffer(); | 36 SkFlattenableReadBuffer(); |
51 virtual ~SkFlattenableReadBuffer(); | 37 virtual ~SkFlattenableReadBuffer(); |
52 | 38 |
53 bool isOrderedBinaryBuffer() { return NULL != getOrderedBinaryBuffer(); } | 39 bool isOrderedBinaryBuffer() { return NULL != getOrderedBinaryBuffer(); } |
54 virtual SkOrderedReadBuffer* getOrderedBinaryBuffer() { return NULL; } | 40 virtual SkOrderedReadBuffer* getOrderedBinaryBuffer() { return NULL; } |
55 | 41 |
56 enum Flags { | 42 enum Flags { |
57 kCrossProcess_Flag = 1 << 0, | 43 kCrossProcess_Flag = 1 << 0, |
58 kScalarIsFloat_Flag = 1 << 1, | 44 kScalarIsFloat_Flag = 1 << 1, |
59 kPtrIs64Bit_Flag = 1 << 2, | 45 kPtrIs64Bit_Flag = 1 << 2, |
| 46 /** The kValidation_Flag is used to force stream validations (by making |
| 47 * sure that no operation reads past the end of the stream, for example) |
| 48 * and error handling if any reading operation yields an invalid value. |
| 49 */ |
| 50 kValidation_Flag = 1 << 3, |
60 }; | 51 }; |
61 | 52 |
62 void setFlags(uint32_t flags) { fFlags = flags; } | 53 void setFlags(uint32_t flags) { fFlags = flags; } |
63 uint32_t getFlags() const { return fFlags; } | 54 uint32_t getFlags() const { return fFlags; } |
64 | 55 |
65 bool isCrossProcess() const { return SkToBool(fFlags & kCrossProcess_Flag);
} | 56 bool isCrossProcess() const { return SkToBool(fFlags & (kCrossProcess_Flag |
kValidation_Flag)); } |
66 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag);
} | 57 bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag);
} |
67 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } | 58 bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); } |
| 59 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } |
68 | 60 |
69 // primitives | 61 // primitives |
70 virtual bool readBool() = 0; | 62 virtual bool readBool() = 0; |
71 virtual SkColor readColor() = 0; | 63 virtual SkColor readColor() = 0; |
72 virtual SkFixed readFixed() = 0; | 64 virtual SkFixed readFixed() = 0; |
73 virtual int32_t readInt() = 0; | 65 virtual int32_t readInt() = 0; |
74 virtual SkScalar readScalar() = 0; | 66 virtual SkScalar readScalar() = 0; |
75 virtual uint32_t readUInt() = 0; | 67 virtual uint32_t readUInt() = 0; |
76 virtual int32_t read32() = 0; | 68 virtual int32_t read32() = 0; |
77 | 69 |
78 // strings -- the caller is responsible for freeing the string contents | 70 // strings -- the caller is responsible for freeing the string contents |
79 virtual void readString(SkString* string) = 0; | 71 virtual void readString(SkString* string) = 0; |
80 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encodi
ng) = 0; | 72 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encodi
ng) = 0; |
81 | 73 |
82 virtual SkFlattenable* readFlattenable(SkEffectType) = 0; | 74 SkColorFilter* readColorFilter(); |
83 | 75 SkDrawLooper* readDrawLooper(); |
84 SkColorFilter* readColorFilter() { | 76 SkImageFilter* readImageFilter(); |
85 return (SkColorFilter*)this->readFlattenable(kColorFilter_SkEffectType); | 77 SkMaskFilter* readMaskFilter(); |
86 } | 78 SkPathEffect* readPathEffect(); |
87 SkDrawLooper* readDrawLooper() { | 79 SkPixelRef* readPixelRef(); |
88 return (SkDrawLooper*)this->readFlattenable(kDrawLooper_SkEffectType); | 80 SkRasterizer* readRasterizer(); |
89 } | 81 SkShader* readShader(); |
90 SkImageFilter* readImageFilter() { | 82 SkUnitMapper* readUnitMapper(); |
91 return (SkImageFilter*)this->readFlattenable(kImageFilter_SkEffectType); | 83 SkXfermode* readXfermode(); |
92 } | |
93 SkMaskFilter* readMaskFilter() { | |
94 return (SkMaskFilter*)this->readFlattenable(kMaskFilter_SkEffectType); | |
95 } | |
96 SkPathEffect* readPathEffect() { | |
97 return (SkPathEffect*)this->readFlattenable(kPathEffect_SkEffectType); | |
98 } | |
99 SkPixelRef* readPixelRef() { | |
100 return (SkPixelRef*)this->readFlattenable(kPixelRef_SkEffectType); | |
101 } | |
102 SkRasterizer* readRasterizer() { | |
103 return (SkRasterizer*)this->readFlattenable(kRasterizer_SkEffectType); | |
104 } | |
105 SkShader* readShader() { | |
106 return (SkShader*)this->readFlattenable(kShader_SkEffectType); | |
107 } | |
108 SkUnitMapper* readUnitMapper() { | |
109 return (SkUnitMapper*)this->readFlattenable(kUnitMapper_SkEffectType); | |
110 } | |
111 SkXfermode* readXfermode() { | |
112 return (SkXfermode*)this->readFlattenable(kXfermode_SkEffectType); | |
113 } | |
114 | 84 |
115 // common data structures | 85 // common data structures |
116 virtual void readPoint(SkPoint* point) = 0; | 86 virtual void readPoint(SkPoint* point) = 0; |
117 virtual void readMatrix(SkMatrix* matrix) = 0; | 87 virtual void readMatrix(SkMatrix* matrix) = 0; |
118 virtual void readIRect(SkIRect* rect) = 0; | 88 virtual void readIRect(SkIRect* rect) = 0; |
119 virtual void readRect(SkRect* rect) = 0; | 89 virtual void readRect(SkRect* rect) = 0; |
120 virtual void readRegion(SkRegion* region) = 0; | 90 virtual void readRegion(SkRegion* region) = 0; |
121 virtual void readPath(SkPath* path) = 0; | 91 virtual void readPath(SkPath* path) = 0; |
122 | 92 |
123 // binary data and arrays | 93 // binary data and arrays |
(...skipping 22 matching lines...) Expand all Loading... |
146 return point; | 116 return point; |
147 } | 117 } |
148 | 118 |
149 SkData* readByteArrayAsData() { | 119 SkData* readByteArrayAsData() { |
150 size_t len = this->getArrayCount(); | 120 size_t len = this->getArrayCount(); |
151 void* buffer = sk_malloc_throw(len); | 121 void* buffer = sk_malloc_throw(len); |
152 (void)this->readByteArray(buffer); | 122 (void)this->readByteArray(buffer); |
153 return SkData::NewFromMalloc(buffer, len); | 123 return SkData::NewFromMalloc(buffer, len); |
154 } | 124 } |
155 | 125 |
| 126 virtual void validate(bool isValid) SK_OVERRIDE {} |
| 127 |
| 128 protected: |
| 129 /** |
| 130 @param type This parameter is only used when using SkValidatingReadBuffe
r. It will verify |
| 131 that the object about to be deserialized is of the given typ
e or early return |
| 132 NULL otherwise. The type provided here is the type of the ba
se class of the |
| 133 object to deserialize. |
| 134 */ |
| 135 virtual SkFlattenable* readFlattenable(SkEffectType type) = 0; |
| 136 |
156 private: | 137 private: |
| 138 template <typename T> T* readFlattenableT(); |
157 uint32_t fFlags; | 139 uint32_t fFlags; |
158 }; | 140 }; |
159 | 141 |
160 /////////////////////////////////////////////////////////////////////////////// | 142 /////////////////////////////////////////////////////////////////////////////// |
161 | 143 |
162 class SkFlattenableWriteBuffer { | 144 class SkFlattenableWriteBuffer { |
163 public: | 145 public: |
164 SkFlattenableWriteBuffer(); | 146 SkFlattenableWriteBuffer(); |
165 virtual ~SkFlattenableWriteBuffer(); | 147 virtual ~SkFlattenableWriteBuffer(); |
166 | 148 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 virtual void writeFunctionPtr(void* ptr); | 180 virtual void writeFunctionPtr(void* ptr); |
199 virtual void writePaint(const SkPaint& paint); | 181 virtual void writePaint(const SkPaint& paint); |
200 | 182 |
201 virtual void writeBitmap(const SkBitmap& bitmap) = 0; | 183 virtual void writeBitmap(const SkBitmap& bitmap) = 0; |
202 virtual void writeTypeface(SkTypeface* typeface) = 0; | 184 virtual void writeTypeface(SkTypeface* typeface) = 0; |
203 | 185 |
204 virtual bool writeToStream(SkWStream*) = 0; | 186 virtual bool writeToStream(SkWStream*) = 0; |
205 | 187 |
206 enum Flags { | 188 enum Flags { |
207 kCrossProcess_Flag = 0x01, | 189 kCrossProcess_Flag = 0x01, |
| 190 /** The kValidation_Flag is used here to make sure the write operation |
| 191 * is symmetric with the read operation using the equivalent flag |
| 192 * SkFlattenableReadBuffer::kValidation_Flag. |
| 193 */ |
| 194 kValidation_Flag = 0x02, |
208 }; | 195 }; |
209 | 196 |
210 uint32_t getFlags() const { return fFlags; } | 197 uint32_t getFlags() const { return fFlags; } |
211 void setFlags(uint32_t flags) { fFlags = flags; } | 198 void setFlags(uint32_t flags) { fFlags = flags; } |
212 | 199 |
213 bool isCrossProcess() const { | 200 bool isCrossProcess() const { |
214 return SkToBool(fFlags & kCrossProcess_Flag); | 201 return SkToBool(fFlags & (kCrossProcess_Flag | kValidation_Flag)); |
| 202 } |
| 203 |
| 204 bool isValidating() const { |
| 205 return SkToBool(fFlags & kValidation_Flag); |
215 } | 206 } |
216 | 207 |
217 bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; } | 208 bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; } |
218 | 209 |
219 void writeDataAsByteArray(SkData* data) { | 210 void writeDataAsByteArray(SkData* data) { |
220 this->writeByteArray(data->data(), data->size()); | 211 this->writeByteArray(data->data(), data->size()); |
221 } | 212 } |
222 | 213 |
223 protected: | 214 protected: |
224 // A helper function so that each subclass does not have to be a friend of S
kFlattenable | 215 // A helper function so that each subclass does not have to be a friend of S
kFlattenable |
225 void flattenObject(SkFlattenable* obj, SkFlattenableWriteBuffer& buffer); | 216 void flattenObject(SkFlattenable* obj, SkFlattenableWriteBuffer& buffer); |
226 | 217 |
227 uint32_t fFlags; | 218 uint32_t fFlags; |
228 }; | 219 }; |
229 | 220 |
230 #endif | 221 #endif |
OLD | NEW |