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