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 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkErrorInternals.h" | 10 #include "SkErrorInternals.h" |
11 #include "SkOrderedReadBuffer.h" | 11 #include "SkReadBuffer.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
14 | 14 |
15 SkOrderedReadBuffer::SkOrderedReadBuffer() : INHERITED() { | 15 static uint32_t default_flags() { |
| 16 uint32_t flags = 0; |
| 17 #ifdef SK_SCALAR_IS_FLOAT |
| 18 flags |= SkReadBuffer::kScalarIsFloat_Flag; |
| 19 #endif |
| 20 if (8 == sizeof(void*)) { |
| 21 flags |= SkReadBuffer::kPtrIs64Bit_Flag; |
| 22 } |
| 23 return flags; |
| 24 } |
| 25 |
| 26 SkReadBuffer::SkReadBuffer() { |
| 27 fFlags = default_flags(); |
16 fMemoryPtr = NULL; | 28 fMemoryPtr = NULL; |
17 | 29 |
18 fBitmapStorage = NULL; | 30 fBitmapStorage = NULL; |
19 fTFArray = NULL; | 31 fTFArray = NULL; |
20 fTFCount = 0; | 32 fTFCount = 0; |
21 | 33 |
22 fFactoryTDArray = NULL; | 34 fFactoryTDArray = NULL; |
23 fFactoryArray = NULL; | 35 fFactoryArray = NULL; |
24 fFactoryCount = 0; | 36 fFactoryCount = 0; |
25 fBitmapDecoder = NULL; | 37 fBitmapDecoder = NULL; |
26 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 38 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
27 fDecodedBitmapIndex = -1; | 39 fDecodedBitmapIndex = -1; |
28 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 40 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
29 } | 41 } |
30 | 42 |
31 SkOrderedReadBuffer::SkOrderedReadBuffer(const void* data, size_t size) : INHERI
TED() { | 43 SkReadBuffer::SkReadBuffer(const void* data, size_t size) { |
| 44 fFlags = default_flags(); |
32 fReader.setMemory(data, size); | 45 fReader.setMemory(data, size); |
33 fMemoryPtr = NULL; | 46 fMemoryPtr = NULL; |
34 | 47 |
35 fBitmapStorage = NULL; | 48 fBitmapStorage = NULL; |
36 fTFArray = NULL; | 49 fTFArray = NULL; |
37 fTFCount = 0; | 50 fTFCount = 0; |
38 | 51 |
39 fFactoryTDArray = NULL; | 52 fFactoryTDArray = NULL; |
40 fFactoryArray = NULL; | 53 fFactoryArray = NULL; |
41 fFactoryCount = 0; | 54 fFactoryCount = 0; |
42 fBitmapDecoder = NULL; | 55 fBitmapDecoder = NULL; |
43 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 56 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
44 fDecodedBitmapIndex = -1; | 57 fDecodedBitmapIndex = -1; |
45 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 58 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
46 } | 59 } |
47 | 60 |
48 SkOrderedReadBuffer::SkOrderedReadBuffer(SkStream* stream) { | 61 SkReadBuffer::SkReadBuffer(SkStream* stream) { |
| 62 fFlags = default_flags(); |
49 const size_t length = stream->getLength(); | 63 const size_t length = stream->getLength(); |
50 fMemoryPtr = sk_malloc_throw(length); | 64 fMemoryPtr = sk_malloc_throw(length); |
51 stream->read(fMemoryPtr, length); | 65 stream->read(fMemoryPtr, length); |
52 fReader.setMemory(fMemoryPtr, length); | 66 fReader.setMemory(fMemoryPtr, length); |
53 | 67 |
54 fBitmapStorage = NULL; | 68 fBitmapStorage = NULL; |
55 fTFArray = NULL; | 69 fTFArray = NULL; |
56 fTFCount = 0; | 70 fTFCount = 0; |
57 | 71 |
58 fFactoryTDArray = NULL; | 72 fFactoryTDArray = NULL; |
59 fFactoryArray = NULL; | 73 fFactoryArray = NULL; |
60 fFactoryCount = 0; | 74 fFactoryCount = 0; |
61 fBitmapDecoder = NULL; | 75 fBitmapDecoder = NULL; |
62 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 76 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
63 fDecodedBitmapIndex = -1; | 77 fDecodedBitmapIndex = -1; |
64 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 78 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
65 } | 79 } |
66 | 80 |
67 SkOrderedReadBuffer::~SkOrderedReadBuffer() { | 81 SkReadBuffer::~SkReadBuffer() { |
68 sk_free(fMemoryPtr); | 82 sk_free(fMemoryPtr); |
69 SkSafeUnref(fBitmapStorage); | 83 SkSafeUnref(fBitmapStorage); |
70 } | 84 } |
71 | 85 |
72 bool SkOrderedReadBuffer::readBool() { | 86 bool SkReadBuffer::readBool() { |
73 return fReader.readBool(); | 87 return fReader.readBool(); |
74 } | 88 } |
75 | 89 |
76 SkColor SkOrderedReadBuffer::readColor() { | 90 SkColor SkReadBuffer::readColor() { |
77 return fReader.readInt(); | 91 return fReader.readInt(); |
78 } | 92 } |
79 | 93 |
80 SkFixed SkOrderedReadBuffer::readFixed() { | 94 SkFixed SkReadBuffer::readFixed() { |
81 return fReader.readS32(); | 95 return fReader.readS32(); |
82 } | 96 } |
83 | 97 |
84 int32_t SkOrderedReadBuffer::readInt() { | 98 int32_t SkReadBuffer::readInt() { |
85 return fReader.readInt(); | 99 return fReader.readInt(); |
86 } | 100 } |
87 | 101 |
88 SkScalar SkOrderedReadBuffer::readScalar() { | 102 SkScalar SkReadBuffer::readScalar() { |
89 return fReader.readScalar(); | 103 return fReader.readScalar(); |
90 } | 104 } |
91 | 105 |
92 uint32_t SkOrderedReadBuffer::readUInt() { | 106 uint32_t SkReadBuffer::readUInt() { |
93 return fReader.readU32(); | 107 return fReader.readU32(); |
94 } | 108 } |
95 | 109 |
96 int32_t SkOrderedReadBuffer::read32() { | 110 int32_t SkReadBuffer::read32() { |
97 return fReader.readInt(); | 111 return fReader.readInt(); |
98 } | 112 } |
99 | 113 |
100 void SkOrderedReadBuffer::readString(SkString* string) { | 114 void SkReadBuffer::readString(SkString* string) { |
101 size_t len; | 115 size_t len; |
102 const char* strContents = fReader.readString(&len); | 116 const char* strContents = fReader.readString(&len); |
103 string->set(strContents, len); | 117 string->set(strContents, len); |
104 } | 118 } |
105 | 119 |
106 void* SkOrderedReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncodi
ng encoding) { | 120 void* SkReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding enco
ding) { |
107 SkDEBUGCODE(int32_t encodingType = ) fReader.readInt(); | 121 SkDEBUGCODE(int32_t encodingType = ) fReader.readInt(); |
108 SkASSERT(encodingType == encoding); | 122 SkASSERT(encodingType == encoding); |
109 *length = fReader.readInt(); | 123 *length = fReader.readInt(); |
110 void* data = sk_malloc_throw(*length); | 124 void* data = sk_malloc_throw(*length); |
111 memcpy(data, fReader.skip(SkAlign4(*length)), *length); | 125 memcpy(data, fReader.skip(SkAlign4(*length)), *length); |
112 return data; | 126 return data; |
113 } | 127 } |
114 | 128 |
115 void SkOrderedReadBuffer::readPoint(SkPoint* point) { | 129 void SkReadBuffer::readPoint(SkPoint* point) { |
116 point->fX = fReader.readScalar(); | 130 point->fX = fReader.readScalar(); |
117 point->fY = fReader.readScalar(); | 131 point->fY = fReader.readScalar(); |
118 } | 132 } |
119 | 133 |
120 void SkOrderedReadBuffer::readMatrix(SkMatrix* matrix) { | 134 void SkReadBuffer::readMatrix(SkMatrix* matrix) { |
121 fReader.readMatrix(matrix); | 135 fReader.readMatrix(matrix); |
122 } | 136 } |
123 | 137 |
124 void SkOrderedReadBuffer::readIRect(SkIRect* rect) { | 138 void SkReadBuffer::readIRect(SkIRect* rect) { |
125 memcpy(rect, fReader.skip(sizeof(SkIRect)), sizeof(SkIRect)); | 139 memcpy(rect, fReader.skip(sizeof(SkIRect)), sizeof(SkIRect)); |
126 } | 140 } |
127 | 141 |
128 void SkOrderedReadBuffer::readRect(SkRect* rect) { | 142 void SkReadBuffer::readRect(SkRect* rect) { |
129 memcpy(rect, fReader.skip(sizeof(SkRect)), sizeof(SkRect)); | 143 memcpy(rect, fReader.skip(sizeof(SkRect)), sizeof(SkRect)); |
130 } | 144 } |
131 | 145 |
132 void SkOrderedReadBuffer::readRegion(SkRegion* region) { | 146 void SkReadBuffer::readRegion(SkRegion* region) { |
133 fReader.readRegion(region); | 147 fReader.readRegion(region); |
134 } | 148 } |
135 | 149 |
136 void SkOrderedReadBuffer::readPath(SkPath* path) { | 150 void SkReadBuffer::readPath(SkPath* path) { |
137 fReader.readPath(path); | 151 fReader.readPath(path); |
138 } | 152 } |
139 | 153 |
140 bool SkOrderedReadBuffer::readArray(void* value, size_t size, size_t elementSize
) { | 154 bool SkReadBuffer::readArray(void* value, size_t size, size_t elementSize) { |
141 const size_t count = this->getArrayCount(); | 155 const size_t count = this->getArrayCount(); |
142 if (count == size) { | 156 if (count == size) { |
143 (void)fReader.skip(sizeof(uint32_t)); // Skip array count | 157 (void)fReader.skip(sizeof(uint32_t)); // Skip array count |
144 const size_t byteLength = count * elementSize; | 158 const size_t byteLength = count * elementSize; |
145 memcpy(value, fReader.skip(SkAlign4(byteLength)), byteLength); | 159 memcpy(value, fReader.skip(SkAlign4(byteLength)), byteLength); |
146 return true; | 160 return true; |
147 } | 161 } |
148 SkASSERT(false); | 162 SkASSERT(false); |
149 fReader.skip(fReader.available()); | 163 fReader.skip(fReader.available()); |
150 return false; | 164 return false; |
151 } | 165 } |
152 | 166 |
153 bool SkOrderedReadBuffer::readByteArray(void* value, size_t size) { | 167 bool SkReadBuffer::readByteArray(void* value, size_t size) { |
154 return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned c
har)); | 168 return readArray(static_cast<unsigned char*>(value), size, sizeof(unsigned c
har)); |
155 } | 169 } |
156 | 170 |
157 bool SkOrderedReadBuffer::readColorArray(SkColor* colors, size_t size) { | 171 bool SkReadBuffer::readColorArray(SkColor* colors, size_t size) { |
158 return readArray(colors, size, sizeof(SkColor)); | 172 return readArray(colors, size, sizeof(SkColor)); |
159 } | 173 } |
160 | 174 |
161 bool SkOrderedReadBuffer::readIntArray(int32_t* values, size_t size) { | 175 bool SkReadBuffer::readIntArray(int32_t* values, size_t size) { |
162 return readArray(values, size, sizeof(int32_t)); | 176 return readArray(values, size, sizeof(int32_t)); |
163 } | 177 } |
164 | 178 |
165 bool SkOrderedReadBuffer::readPointArray(SkPoint* points, size_t size) { | 179 bool SkReadBuffer::readPointArray(SkPoint* points, size_t size) { |
166 return readArray(points, size, sizeof(SkPoint)); | 180 return readArray(points, size, sizeof(SkPoint)); |
167 } | 181 } |
168 | 182 |
169 bool SkOrderedReadBuffer::readScalarArray(SkScalar* values, size_t size) { | 183 bool SkReadBuffer::readScalarArray(SkScalar* values, size_t size) { |
170 return readArray(values, size, sizeof(SkScalar)); | 184 return readArray(values, size, sizeof(SkScalar)); |
171 } | 185 } |
172 | 186 |
173 uint32_t SkOrderedReadBuffer::getArrayCount() { | 187 uint32_t SkReadBuffer::getArrayCount() { |
174 return *(uint32_t*)fReader.peek(); | 188 return *(uint32_t*)fReader.peek(); |
175 } | 189 } |
176 | 190 |
177 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { | 191 void SkReadBuffer::readBitmap(SkBitmap* bitmap) { |
178 const int width = this->readInt(); | 192 const int width = this->readInt(); |
179 const int height = this->readInt(); | 193 const int height = this->readInt(); |
180 // The writer stored a boolean value to determine whether an SkBitmapHeap wa
s used during | 194 // The writer stored a boolean value to determine whether an SkBitmapHeap wa
s used during |
181 // writing. | 195 // writing. |
182 if (this->readBool()) { | 196 if (this->readBool()) { |
183 // An SkBitmapHeap was used for writing. Read the index from the stream
and find the | 197 // An SkBitmapHeap was used for writing. Read the index from the stream
and find the |
184 // corresponding SkBitmap in fBitmapStorage. | 198 // corresponding SkBitmap in fBitmapStorage. |
185 const uint32_t index = fReader.readU32(); | 199 const uint32_t index = fReader.readU32(); |
186 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer::wr
iteBitmap) | 200 fReader.readU32(); // bitmap generation ID (see SkWriteBuffer::writeBitm
ap) |
187 if (fBitmapStorage) { | 201 if (fBitmapStorage) { |
188 *bitmap = *fBitmapStorage->getBitmap(index); | 202 *bitmap = *fBitmapStorage->getBitmap(index); |
189 fBitmapStorage->releaseRef(index); | 203 fBitmapStorage->releaseRef(index); |
190 return; | 204 return; |
191 } else { | 205 } else { |
192 // The bitmap was stored in a heap, but there is no way to access it
. Set an error and | 206 // The bitmap was stored in a heap, but there is no way to access it
. Set an error and |
193 // fall through to use a place holder bitmap. | 207 // fall through to use a place holder bitmap. |
194 SkErrorInternals::SetError(kParseError_SkError, "SkOrderedWriteBuffe
r::writeBitmap " | 208 SkErrorInternals::SetError(kParseError_SkError, "SkWriteBuffer::writ
eBitmap " |
195 "stored the SkBitmap in an SkBitmapHeap,
but " | 209 "stored the SkBitmap in an SkBitmapHeap,
but " |
196 "SkOrderedReadBuffer has no SkBitmapHeapR
eader to " | 210 "SkReadBuffer has no SkBitmapHeapReader t
o " |
197 "retrieve the SkBitmap."); | 211 "retrieve the SkBitmap."); |
198 } | 212 } |
199 } else { | 213 } else { |
200 // The writer stored false, meaning the SkBitmap was not stored in an Sk
BitmapHeap. | 214 // The writer stored false, meaning the SkBitmap was not stored in an Sk
BitmapHeap. |
201 const size_t length = this->readUInt(); | 215 const size_t length = this->readUInt(); |
202 if (length > 0) { | 216 if (length > 0) { |
203 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 217 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
204 fDecodedBitmapIndex++; | 218 fDecodedBitmapIndex++; |
205 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 219 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
206 // A non-zero size means the SkBitmap was encoded. Read the data and
pixel | 220 // A non-zero size means the SkBitmap was encoded. Read the data and
pixel |
207 // offset. | 221 // offset. |
208 const void* data = this->skip(length); | 222 const void* data = this->skip(length); |
209 const int32_t xOffset = fReader.readS32(); | 223 const int32_t xOffset = fReader.readS32(); |
210 const int32_t yOffset = fReader.readS32(); | 224 const int32_t yOffset = fReader.readS32(); |
211 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap))
{ | 225 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap))
{ |
212 if (bitmap->width() == width && bitmap->height() == height) { | 226 if (bitmap->width() == width && bitmap->height() == height) { |
213 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 227 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
214 if (0 != xOffset || 0 != yOffset) { | 228 if (0 != xOffset || 0 != yOffset) { |
215 SkDebugf("SkOrderedReadBuffer::readBitmap: heights match
," | 229 SkDebugf("SkReadBuffer::readBitmap: heights match," |
216 " but offset is not zero. \nInfo about the bitm
ap:" | 230 " but offset is not zero. \nInfo about the bitm
ap:" |
217 "\n\tIndex: %d\n\tDimensions: [%d %d]\n\tEncode
d" | 231 "\n\tIndex: %d\n\tDimensions: [%d %d]\n\tEncode
d" |
218 " data size: %d\n\tOffset: (%d, %d)\n", | 232 " data size: %d\n\tOffset: (%d, %d)\n", |
219 fDecodedBitmapIndex, width, height, length, xOf
fset, | 233 fDecodedBitmapIndex, width, height, length, xOf
fset, |
220 yOffset); | 234 yOffset); |
221 } | 235 } |
222 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 236 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
223 // If the width and height match, there should be no offset. | 237 // If the width and height match, there should be no offset. |
224 SkASSERT(0 == xOffset && 0 == yOffset); | 238 SkASSERT(0 == xOffset && 0 == yOffset); |
225 return; | 239 return; |
(...skipping 23 matching lines...) Expand all Loading... |
249 // A size of zero means the SkBitmap was simply flattened. | 263 // A size of zero means the SkBitmap was simply flattened. |
250 bitmap->unflatten(*this); | 264 bitmap->unflatten(*this); |
251 return; | 265 return; |
252 } | 266 } |
253 } | 267 } |
254 // Could not read the SkBitmap. Use a placeholder bitmap. | 268 // Could not read the SkBitmap. Use a placeholder bitmap. |
255 bitmap->allocPixels(SkImageInfo::MakeN32Premul(width, height)); | 269 bitmap->allocPixels(SkImageInfo::MakeN32Premul(width, height)); |
256 bitmap->eraseColor(SK_ColorRED); | 270 bitmap->eraseColor(SK_ColorRED); |
257 } | 271 } |
258 | 272 |
259 SkTypeface* SkOrderedReadBuffer::readTypeface() { | 273 SkTypeface* SkReadBuffer::readTypeface() { |
260 | 274 |
261 uint32_t index = fReader.readU32(); | 275 uint32_t index = fReader.readU32(); |
262 if (0 == index || index > (unsigned)fTFCount) { | 276 if (0 == index || index > (unsigned)fTFCount) { |
263 if (index) { | 277 if (index) { |
264 SkDebugf("====== typeface index %d\n", index); | 278 SkDebugf("====== typeface index %d\n", index); |
265 } | 279 } |
266 return NULL; | 280 return NULL; |
267 } else { | 281 } else { |
268 SkASSERT(fTFArray); | 282 SkASSERT(fTFArray); |
269 return fTFArray[index - 1]; | 283 return fTFArray[index - 1]; |
270 } | 284 } |
271 } | 285 } |
272 | 286 |
273 SkFlattenable* SkOrderedReadBuffer::readFlattenable(SkFlattenable::Type ft) { | 287 SkFlattenable* SkReadBuffer::readFlattenable(SkFlattenable::Type ft) { |
274 // | 288 // |
275 // TODO: confirm that ft matches the factory we decide to use | 289 // TODO: confirm that ft matches the factory we decide to use |
276 // | 290 // |
277 | 291 |
278 SkFlattenable::Factory factory = NULL; | 292 SkFlattenable::Factory factory = NULL; |
279 | 293 |
280 if (fFactoryCount > 0) { | 294 if (fFactoryCount > 0) { |
281 int32_t index = fReader.readU32(); | 295 int32_t index = fReader.readU32(); |
282 if (0 == index) { | 296 if (0 == index) { |
283 return NULL; // writer failed to give us the flattenable | 297 return NULL; // writer failed to give us the flattenable |
(...skipping 27 matching lines...) Expand all Loading... |
311 if (sizeRecorded != sizeRead) { | 325 if (sizeRecorded != sizeRead) { |
312 // we could try to fix up the offset... | 326 // we could try to fix up the offset... |
313 sk_throw(); | 327 sk_throw(); |
314 } | 328 } |
315 } else { | 329 } else { |
316 // we must skip the remaining data | 330 // we must skip the remaining data |
317 fReader.skip(sizeRecorded); | 331 fReader.skip(sizeRecorded); |
318 } | 332 } |
319 return obj; | 333 return obj; |
320 } | 334 } |
OLD | NEW |