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 "SkOrderedReadBuffer.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
14 | 14 |
15 SkOrderedReadBuffer::SkOrderedReadBuffer() : INHERITED() { | 15 SkOrderedReadBuffer::SkOrderedReadBuffer() : INHERITED() { |
16 fMemoryPtr = NULL; | 16 fMemoryPtr = NULL; |
17 | 17 |
18 fBitmapStorage = NULL; | 18 fBitmapStorage = NULL; |
19 fTFArray = NULL; | 19 fTFArray = NULL; |
20 fTFCount = 0; | 20 fTFCount = 0; |
21 | 21 |
22 fFactoryTDArray = NULL; | 22 fFactoryTDArray = NULL; |
23 fFactoryArray = NULL; | 23 fFactoryArray = NULL; |
24 fFactoryCount = 0; | 24 fFactoryCount = 0; |
25 fBitmapDecoder = NULL; | 25 fBitmapDecoder = NULL; |
26 fPictureVersion = SkPicture::PICTURE_VERSION; | |
27 } | 26 } |
28 | 27 |
29 SkOrderedReadBuffer::SkOrderedReadBuffer(const void* data, size_t size) : INHERI
TED() { | 28 SkOrderedReadBuffer::SkOrderedReadBuffer(const void* data, size_t size) : INHERI
TED() { |
30 fReader.setMemory(data, size); | 29 fReader.setMemory(data, size); |
31 fMemoryPtr = NULL; | 30 fMemoryPtr = NULL; |
32 | 31 |
33 fBitmapStorage = NULL; | 32 fBitmapStorage = NULL; |
34 fTFArray = NULL; | 33 fTFArray = NULL; |
35 fTFCount = 0; | 34 fTFCount = 0; |
36 | 35 |
37 fFactoryTDArray = NULL; | 36 fFactoryTDArray = NULL; |
38 fFactoryArray = NULL; | 37 fFactoryArray = NULL; |
39 fFactoryCount = 0; | 38 fFactoryCount = 0; |
40 fBitmapDecoder = NULL; | 39 fBitmapDecoder = NULL; |
41 fPictureVersion = SkPicture::PICTURE_VERSION; | |
42 } | 40 } |
43 | 41 |
44 SkOrderedReadBuffer::SkOrderedReadBuffer(SkStream* stream) { | 42 SkOrderedReadBuffer::SkOrderedReadBuffer(SkStream* stream) { |
45 const size_t length = stream->getLength(); | 43 const size_t length = stream->getLength(); |
46 fMemoryPtr = sk_malloc_throw(length); | 44 fMemoryPtr = sk_malloc_throw(length); |
47 stream->read(fMemoryPtr, length); | 45 stream->read(fMemoryPtr, length); |
48 fReader.setMemory(fMemoryPtr, length); | 46 fReader.setMemory(fMemoryPtr, length); |
49 | 47 |
50 fBitmapStorage = NULL; | 48 fBitmapStorage = NULL; |
51 fTFArray = NULL; | 49 fTFArray = NULL; |
52 fTFCount = 0; | 50 fTFCount = 0; |
53 | 51 |
54 fFactoryTDArray = NULL; | 52 fFactoryTDArray = NULL; |
55 fFactoryArray = NULL; | 53 fFactoryArray = NULL; |
56 fFactoryCount = 0; | 54 fFactoryCount = 0; |
57 fBitmapDecoder = NULL; | 55 fBitmapDecoder = NULL; |
58 fPictureVersion = SkPicture::PICTURE_VERSION; | |
59 } | 56 } |
60 | 57 |
61 SkOrderedReadBuffer::~SkOrderedReadBuffer() { | 58 SkOrderedReadBuffer::~SkOrderedReadBuffer() { |
62 sk_free(fMemoryPtr); | 59 sk_free(fMemoryPtr); |
63 SkSafeUnref(fBitmapStorage); | 60 SkSafeUnref(fBitmapStorage); |
64 } | 61 } |
65 | 62 |
66 bool SkOrderedReadBuffer::readBool() { | 63 bool SkOrderedReadBuffer::readBool() { |
67 return fReader.readBool(); | 64 return fReader.readBool(); |
68 } | 65 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 const uint32_t count = fReader.readU32(); | 161 const uint32_t count = fReader.readU32(); |
165 const uint32_t byteLength = count * sizeof(SkScalar); | 162 const uint32_t byteLength = count * sizeof(SkScalar); |
166 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); | 163 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); |
167 return count; | 164 return count; |
168 } | 165 } |
169 | 166 |
170 uint32_t SkOrderedReadBuffer::getArrayCount() { | 167 uint32_t SkOrderedReadBuffer::getArrayCount() { |
171 return *(uint32_t*)fReader.peek(); | 168 return *(uint32_t*)fReader.peek(); |
172 } | 169 } |
173 | 170 |
174 void SkOrderedReadBuffer::setPictureVersion(uint32_t version) { | |
175 SkASSERT(version <= SkPicture::PICTURE_VERSION); | |
176 fPictureVersion = version; | |
177 } | |
178 | |
179 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { | 171 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { |
180 if (10 == fPictureVersion) { | 172 const int width = this->readInt(); |
181 // Old code to read a bitmap in PICTURE_VERSION 10 | 173 const int height = this->readInt(); |
| 174 // The writer stored a boolean value to determine whether an SkBitmapHeap wa
s used during |
| 175 // writing. |
| 176 if (this->readBool()) { |
| 177 // An SkBitmapHeap was used for writing. Read the index from the stream
and find the |
| 178 // corresponding SkBitmap in fBitmapStorage. |
| 179 const uint32_t index = fReader.readU32(); |
| 180 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer::wr
iteBitmap) |
| 181 if (fBitmapStorage) { |
| 182 *bitmap = *fBitmapStorage->getBitmap(index); |
| 183 fBitmapStorage->releaseRef(index); |
| 184 return; |
| 185 } else { |
| 186 // The bitmap was stored in a heap, but there is no way to access it
. Set an error and |
| 187 // fall through to use a place holder bitmap. |
| 188 SkErrorInternals::SetError(kParseError_SkError, "SkOrderedWriteBuffe
r::writeBitmap " |
| 189 "stored the SkBitmap in an SkBitmapHeap,
but " |
| 190 "SkOrderedReadBuffer has no SkBitmapHeapR
eader to " |
| 191 "retrieve the SkBitmap."); |
| 192 } |
| 193 } else { |
| 194 // The writer stored false, meaning the SkBitmap was not stored in an Sk
BitmapHeap. |
182 const size_t length = this->readUInt(); | 195 const size_t length = this->readUInt(); |
183 if (length > 0) { | 196 if (length > 0) { |
184 // Bitmap was encoded. | 197 // A non-zero size means the SkBitmap was encoded. |
185 const void* data = this->skip(length); | 198 const void* data = this->skip(length); |
186 const int width = this->readInt(); | |
187 const int height = this->readInt(); | |
188 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap))
{ | 199 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap))
{ |
189 SkASSERT(bitmap->width() == width && bitmap->height() == height)
; | 200 SkASSERT(bitmap->width() == width && bitmap->height() == height)
; |
190 } else { | |
191 // This bitmap was encoded when written, but we are unable to de
code, possibly due to | |
192 // not having a decoder. Use a placeholder bitmap. | |
193 SkErrorInternals::SetError(kParseError_SkError, | |
194 "Could not decode bitmap. Resulting b
itmap will be red."); | |
195 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
196 bitmap->allocPixels(); | |
197 bitmap->eraseColor(SK_ColorRED); | |
198 } | |
199 } else { | |
200 if (fBitmapStorage) { | |
201 const uint32_t index = fReader.readU32(); | |
202 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBu
ffer::writeBitmap) | |
203 *bitmap = *fBitmapStorage->getBitmap(index); | |
204 fBitmapStorage->releaseRef(index); | |
205 } else { | |
206 bitmap->unflatten(*this); | |
207 } | |
208 } | |
209 } else { | |
210 const int width = this->readInt(); | |
211 const int height = this->readInt(); | |
212 // The writer stored a boolean value to determine whether an SkBitmapHea
p was used during | |
213 // writing. | |
214 if (this->readBool()) { | |
215 // An SkBitmapHeap was used for writing. Read the index from the str
eam and find the | |
216 // corresponding SkBitmap in fBitmapStorage. | |
217 const uint32_t index = fReader.readU32(); | |
218 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer
::writeBitmap) | |
219 if (fBitmapStorage) { | |
220 *bitmap = *fBitmapStorage->getBitmap(index); | |
221 fBitmapStorage->releaseRef(index); | |
222 return; | |
223 } else { | |
224 // The bitmap was stored in a heap, but there is no way to acces
s it. Set an error and | |
225 // fall through to use a place holder bitmap. | |
226 SkErrorInternals::SetError(kParseError_SkError, "SkOrderedWriteB
uffer::writeBitmap " | |
227 "stored the SkBitmap in an SkBitmapHe
ap, but " | |
228 "SkOrderedReadBuffer has no SkBitmapH
eapReader to " | |
229 "retrieve the SkBitmap."); | |
230 } | |
231 } else { | |
232 // The writer stored false, meaning the SkBitmap was not stored in a
n SkBitmapHeap. | |
233 const size_t length = this->readUInt(); | |
234 if (length > 0) { | |
235 // A non-zero size means the SkBitmap was encoded. | |
236 const void* data = this->skip(length); | |
237 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitma
p)) { | |
238 SkASSERT(bitmap->width() == width && bitmap->height() == hei
ght); | |
239 return; | |
240 } | |
241 // This bitmap was encoded when written, but we are unable to de
code, possibly due to | |
242 // not having a decoder. | |
243 SkErrorInternals::SetError(kParseError_SkError, | |
244 "Could not decode bitmap. Resulting b
itmap will be red."); | |
245 } else { | |
246 // A size of zero means the SkBitmap was simply flattened. | |
247 bitmap->unflatten(*this); | |
248 return; | 201 return; |
249 } | 202 } |
| 203 // This bitmap was encoded when written, but we are unable to decode
, possibly due to |
| 204 // not having a decoder. |
| 205 SkErrorInternals::SetError(kParseError_SkError, |
| 206 "Could not decode bitmap. Resulting bitma
p will be red."); |
| 207 } else { |
| 208 // A size of zero means the SkBitmap was simply flattened. |
| 209 bitmap->unflatten(*this); |
| 210 return; |
250 } | 211 } |
251 // Could not read the SkBitmap. Use a placeholder bitmap. | |
252 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
253 bitmap->allocPixels(); | |
254 bitmap->eraseColor(SK_ColorRED); | |
255 } | 212 } |
| 213 // Could not read the SkBitmap. Use a placeholder bitmap. |
| 214 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 215 bitmap->allocPixels(); |
| 216 bitmap->eraseColor(SK_ColorRED); |
256 } | 217 } |
257 | 218 |
258 SkTypeface* SkOrderedReadBuffer::readTypeface() { | 219 SkTypeface* SkOrderedReadBuffer::readTypeface() { |
259 | 220 |
260 uint32_t index = fReader.readU32(); | 221 uint32_t index = fReader.readU32(); |
261 if (0 == index || index > (unsigned)fTFCount) { | 222 if (0 == index || index > (unsigned)fTFCount) { |
262 if (index) { | 223 if (index) { |
263 SkDebugf("====== typeface index %d\n", index); | 224 SkDebugf("====== typeface index %d\n", index); |
264 } | 225 } |
265 return NULL; | 226 return NULL; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 if (sizeRecorded != sizeRead) { | 267 if (sizeRecorded != sizeRead) { |
307 // we could try to fix up the offset... | 268 // we could try to fix up the offset... |
308 sk_throw(); | 269 sk_throw(); |
309 } | 270 } |
310 } else { | 271 } else { |
311 // we must skip the remaining data | 272 // we must skip the remaining data |
312 fReader.skip(sizeRecorded); | 273 fReader.skip(sizeRecorded); |
313 } | 274 } |
314 return obj; | 275 return obj; |
315 } | 276 } |
OLD | NEW |