Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkErrorInternals.h" | 9 #include "SkErrorInternals.h" |
| 10 #include "SkImage.h" | 10 #include "SkImage.h" |
| 11 #include "SkImageDeserializer.h" | |
| 11 #include "SkImageGenerator.h" | 12 #include "SkImageGenerator.h" |
| 12 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
| 13 #include "SkStream.h" | 14 #include "SkStream.h" |
| 14 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
| 15 | 16 |
| 17 namespace { | |
| 18 | |
| 19 // This generator intentionally should always fail on all attempts to get it s pixels, | |
| 20 // simulating a bad or empty codec stream. | |
| 21 class EmptyImageGenerator final : public SkImageGenerator { | |
| 22 public: | |
| 23 EmptyImageGenerator(const SkImageInfo& info) : INHERITED(info) { } | |
| 24 | |
| 25 private: | |
| 26 typedef SkImageGenerator INHERITED; | |
| 27 }; | |
| 28 | |
| 29 static sk_sp<SkImage> MakeEmptyImage(int width, int height) { | |
| 30 return SkImage::MakeFromGenerator( | |
| 31 new EmptyImageGenerator(SkImageInfo::MakeN32Premul(width, height))); | |
| 32 } | |
| 33 | |
| 34 } // anonymous namespace | |
| 35 | |
| 36 | |
| 16 static uint32_t default_flags() { | 37 static uint32_t default_flags() { |
| 17 uint32_t flags = 0; | 38 uint32_t flags = 0; |
| 18 flags |= SkReadBuffer::kScalarIsFloat_Flag; | 39 flags |= SkReadBuffer::kScalarIsFloat_Flag; |
| 19 if (8 == sizeof(void*)) { | 40 if (8 == sizeof(void*)) { |
| 20 flags |= SkReadBuffer::kPtrIs64Bit_Flag; | 41 flags |= SkReadBuffer::kPtrIs64Bit_Flag; |
| 21 } | 42 } |
| 22 return flags; | 43 return flags; |
| 23 } | 44 } |
| 24 | 45 |
| 46 // This has an empty constructor and destructor, and is thread-safe, so we can u se a singleton. | |
| 47 static SkImageDeserializer gDefaultImageDeserializer; | |
| 48 | |
| 25 SkReadBuffer::SkReadBuffer() { | 49 SkReadBuffer::SkReadBuffer() { |
| 26 fFlags = default_flags(); | 50 fFlags = default_flags(); |
| 27 fVersion = 0; | 51 fVersion = 0; |
| 28 fMemoryPtr = nullptr; | 52 fMemoryPtr = nullptr; |
| 29 | 53 |
| 30 fTFArray = nullptr; | 54 fTFArray = nullptr; |
| 31 fTFCount = 0; | 55 fTFCount = 0; |
| 32 | 56 |
| 33 fFactoryArray = nullptr; | 57 fFactoryArray = nullptr; |
| 34 fFactoryCount = 0; | 58 fFactoryCount = 0; |
| 35 fBitmapDecoder = nullptr; | 59 fImageDeserializer = &gDefaultImageDeserializer; |
| 36 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 60 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 37 fDecodedBitmapIndex = -1; | 61 fDecodedBitmapIndex = -1; |
| 38 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 62 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 39 } | 63 } |
| 40 | 64 |
| 41 SkReadBuffer::SkReadBuffer(const void* data, size_t size) { | 65 SkReadBuffer::SkReadBuffer(const void* data, size_t size) { |
| 42 fFlags = default_flags(); | 66 fFlags = default_flags(); |
| 43 fVersion = 0; | 67 fVersion = 0; |
| 44 fReader.setMemory(data, size); | 68 fReader.setMemory(data, size); |
| 45 fMemoryPtr = nullptr; | 69 fMemoryPtr = nullptr; |
| 46 | 70 |
| 47 fTFArray = nullptr; | 71 fTFArray = nullptr; |
| 48 fTFCount = 0; | 72 fTFCount = 0; |
| 49 | 73 |
| 50 fFactoryArray = nullptr; | 74 fFactoryArray = nullptr; |
| 51 fFactoryCount = 0; | 75 fFactoryCount = 0; |
| 52 fBitmapDecoder = nullptr; | 76 fImageDeserializer = &gDefaultImageDeserializer; |
| 53 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 77 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 54 fDecodedBitmapIndex = -1; | 78 fDecodedBitmapIndex = -1; |
| 55 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 79 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 56 } | 80 } |
| 57 | 81 |
| 58 SkReadBuffer::SkReadBuffer(SkStream* stream) { | 82 SkReadBuffer::SkReadBuffer(SkStream* stream) { |
| 59 fFlags = default_flags(); | 83 fFlags = default_flags(); |
| 60 fVersion = 0; | 84 fVersion = 0; |
| 61 const size_t length = stream->getLength(); | 85 const size_t length = stream->getLength(); |
| 62 fMemoryPtr = sk_malloc_throw(length); | 86 fMemoryPtr = sk_malloc_throw(length); |
| 63 stream->read(fMemoryPtr, length); | 87 stream->read(fMemoryPtr, length); |
| 64 fReader.setMemory(fMemoryPtr, length); | 88 fReader.setMemory(fMemoryPtr, length); |
| 65 | 89 |
| 66 fTFArray = nullptr; | 90 fTFArray = nullptr; |
| 67 fTFCount = 0; | 91 fTFCount = 0; |
| 68 | 92 |
| 69 fFactoryArray = nullptr; | 93 fFactoryArray = nullptr; |
| 70 fFactoryCount = 0; | 94 fFactoryCount = 0; |
| 71 fBitmapDecoder = nullptr; | 95 fImageDeserializer = &gDefaultImageDeserializer; |
| 72 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 96 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 73 fDecodedBitmapIndex = -1; | 97 fDecodedBitmapIndex = -1; |
| 74 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 98 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 75 } | 99 } |
| 76 | 100 |
| 77 SkReadBuffer::~SkReadBuffer() { | 101 SkReadBuffer::~SkReadBuffer() { |
| 78 sk_free(fMemoryPtr); | 102 sk_free(fMemoryPtr); |
| 79 } | 103 } |
| 80 | 104 |
| 105 void SkReadBuffer::setImageDeserializer(SkImageDeserializer* deserializer) { | |
| 106 fImageDeserializer = deserializer ? deserializer : &gDefaultImageDeserialize r; | |
|
mtklein
2016/08/11 01:02:55
Don't you just wish || worked for types other than
| |
| 107 } | |
| 108 | |
| 81 bool SkReadBuffer::readBool() { | 109 bool SkReadBuffer::readBool() { |
| 82 return fReader.readBool(); | 110 return fReader.readBool(); |
| 83 } | 111 } |
| 84 | 112 |
| 85 SkColor SkReadBuffer::readColor() { | 113 SkColor SkReadBuffer::readColor() { |
| 86 return fReader.readInt(); | 114 return fReader.readInt(); |
| 87 } | 115 } |
| 88 | 116 |
| 89 int32_t SkReadBuffer::readInt() { | 117 int32_t SkReadBuffer::readInt() { |
| 90 return fReader.readInt(); | 118 return fReader.readInt(); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 } | 200 } |
| 173 | 201 |
| 174 bool SkReadBuffer::readScalarArray(SkScalar* values, size_t size) { | 202 bool SkReadBuffer::readScalarArray(SkScalar* values, size_t size) { |
| 175 return readArray(values, size, sizeof(SkScalar)); | 203 return readArray(values, size, sizeof(SkScalar)); |
| 176 } | 204 } |
| 177 | 205 |
| 178 uint32_t SkReadBuffer::getArrayCount() { | 206 uint32_t SkReadBuffer::getArrayCount() { |
| 179 return *(uint32_t*)fReader.peek(); | 207 return *(uint32_t*)fReader.peek(); |
| 180 } | 208 } |
| 181 | 209 |
| 182 bool SkReadBuffer::readBitmap(SkBitmap* bitmap) { | 210 sk_sp<SkImage> SkReadBuffer::readBitmapAsImage() { |
| 183 const int width = this->readInt(); | 211 const int width = this->readInt(); |
| 184 const int height = this->readInt(); | 212 const int height = this->readInt(); |
| 185 | 213 |
| 186 // The writer stored a boolean value to determine whether an SkBitmapHeap wa s used during | 214 // The writer stored a boolean value to determine whether an SkBitmapHeap wa s used during |
| 187 // writing. That feature is deprecated. | 215 // writing. That feature is deprecated. |
| 188 if (this->readBool()) { | 216 if (this->readBool()) { |
| 189 this->readUInt(); // Bitmap index | 217 this->readUInt(); // Bitmap index |
| 190 this->readUInt(); // Bitmap generation ID | 218 this->readUInt(); // Bitmap generation ID |
| 191 SkErrorInternals::SetError(kParseError_SkError, "SkWriteBuffer::writeBit map " | 219 SkErrorInternals::SetError(kParseError_SkError, "SkWriteBuffer::writeBit map " |
| 192 "stored the SkBitmap in an SkBitmapHeap, but " | 220 "stored the SkBitmap in an SkBitmapHeap, but " |
| 193 "that feature is no longer supported."); | 221 "that feature is no longer supported."); |
| 194 } else { | 222 } else { |
| 195 // The writer stored false, meaning the SkBitmap was not stored in an Sk BitmapHeap. | 223 // The writer stored false, meaning the SkBitmap was not stored in an Sk BitmapHeap. |
| 196 const size_t length = this->readUInt(); | 224 const size_t length = this->readUInt(); |
| 197 if (length > 0) { | 225 if (length > 0) { |
| 198 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 226 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 199 fDecodedBitmapIndex++; | 227 fDecodedBitmapIndex++; |
| 200 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | 228 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 201 // A non-zero size means the SkBitmap was encoded. Read the data and pixel | 229 // A non-zero size means the SkBitmap was encoded. Read the data and pixel |
| 202 // offset. | 230 // offset. |
| 203 const void* data = this->skip(length); | 231 const void* data = this->skip(length); |
| 204 const int32_t xOffset = this->readInt(); | 232 const int32_t xOffset = this->readInt(); |
| 205 const int32_t yOffset = this->readInt(); | 233 const int32_t yOffset = this->readInt(); |
| 206 if (fBitmapDecoder != nullptr && fBitmapDecoder(data, length, bitmap )) { | 234 SkIRect subset = SkIRect::MakeXYWH(xOffset, yOffset, width, height); |
| 207 if (bitmap->width() == width && bitmap->height() == height) { | 235 sk_sp<SkImage> image = fImageDeserializer->makeFromMemory(data, leng th, &subset); |
| 208 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT | 236 if (image) { |
| 209 if (0 != xOffset || 0 != yOffset) { | 237 return image; |
| 210 SkDebugf("SkReadBuffer::readBitmap: heights match," | 238 } |
| 211 " but offset is not zero. \nInfo about the bitm ap:" | |
| 212 "\n\tIndex: %d\n\tDimensions: [%d %d]\n\tEncode d" | |
| 213 " data size: %d\n\tOffset: (%d, %d)\n", | |
| 214 fDecodedBitmapIndex, width, height, length, xOf fset, | |
| 215 yOffset); | |
| 216 } | |
| 217 #endif // DEBUG_NON_DETERMINISTIC_ASSERT | |
| 218 // If the width and height match, there should be no offset. | |
| 219 SkASSERT(0 == xOffset && 0 == yOffset); | |
| 220 return true; | |
| 221 } | |
| 222 | 239 |
| 223 // This case can only be reached if extractSubset was called, so | |
| 224 // the recorded width and height must be smaller than or equal t o | |
| 225 // the encoded width and height. | |
| 226 // FIXME (scroggo): This assert assumes that our decoder and the | |
| 227 // sources encoder agree on the width and height which may not | |
| 228 // always be the case. Removing until it can be investigated | |
| 229 // further. | |
| 230 //SkASSERT(width <= bitmap->width() && height <= bitmap->height( )); | |
| 231 | |
| 232 SkBitmap subsetBm; | |
| 233 SkIRect subset = SkIRect::MakeXYWH(xOffset, yOffset, width, heig ht); | |
| 234 if (bitmap->extractSubset(&subsetBm, subset)) { | |
| 235 bitmap->swap(subsetBm); | |
| 236 return true; | |
| 237 } | |
| 238 } | |
| 239 // This bitmap was encoded when written, but we are unable to decode , possibly due to | 240 // This bitmap was encoded when written, but we are unable to decode , possibly due to |
| 240 // not having a decoder. | 241 // not having a decoder. |
| 241 SkErrorInternals::SetError(kParseError_SkError, | 242 SkErrorInternals::SetError(kParseError_SkError, |
| 242 "Could not decode bitmap. Resulting bitma p will be empty."); | 243 "Could not decode bitmap. Resulting bitma p will be empty."); |
| 243 // Even though we weren't able to decode the pixels, the readbuffer should still be | 244 // Even though we weren't able to decode the pixels, the readbuffer should still be |
| 244 // intact, so we return true with an empty bitmap, so we don't force an abort of the | 245 // intact, so we return true with an empty bitmap, so we don't force an abort of the |
| 245 // larger deserialize. | 246 // larger deserialize. |
| 246 bitmap->setInfo(SkImageInfo::MakeUnknown(width, height)); | 247 return MakeEmptyImage(width, height); |
| 247 return true; | 248 } else { |
| 248 } else if (SkBitmap::ReadRawPixels(this, bitmap)) { | 249 SkBitmap bitmap; |
| 249 return true; | 250 if (SkBitmap::ReadRawPixels(this, &bitmap)) { |
| 251 bitmap.setImmutable(); | |
| 252 return SkImage::MakeFromBitmap(bitmap); | |
| 253 } | |
| 250 } | 254 } |
| 251 } | 255 } |
| 252 // Could not read the SkBitmap. Use a placeholder bitmap. | 256 // Could not read the SkBitmap. Use a placeholder bitmap. |
| 253 bitmap->setInfo(SkImageInfo::MakeUnknown(width, height)); | 257 return nullptr; |
| 254 return false; | |
| 255 } | 258 } |
| 256 | 259 |
| 257 namespace { | 260 sk_sp<SkImage> SkReadBuffer::readImage() { |
| 258 | |
| 259 // This generator intentionally should always fail on all attempts to get its pi xels, | |
| 260 // simulating a bad or empty codec stream. | |
| 261 class EmptyImageGenerator final : public SkImageGenerator { | |
| 262 public: | |
| 263 EmptyImageGenerator(const SkImageInfo& info) : INHERITED(info) { } | |
| 264 | |
| 265 private: | |
| 266 typedef SkImageGenerator INHERITED; | |
| 267 }; | |
| 268 | |
| 269 } // anonymous namespace | |
| 270 | |
| 271 SkImage* SkReadBuffer::readImage() { | |
| 272 int width = this->read32(); | 261 int width = this->read32(); |
| 273 int height = this->read32(); | 262 int height = this->read32(); |
| 274 if (width <= 0 || height <= 0) { // SkImage never has a zero dimension | 263 if (width <= 0 || height <= 0) { // SkImage never has a zero dimension |
| 275 this->validate(false); | 264 this->validate(false); |
| 276 return nullptr; | 265 return nullptr; |
| 277 } | 266 } |
| 278 | 267 |
| 279 auto placeholder = [=] { | |
| 280 return SkImage::MakeFromGenerator( | |
| 281 new EmptyImageGenerator(SkImageInfo::MakeN32Premul(width, height))). release(); | |
| 282 }; | |
| 283 | |
| 284 uint32_t encoded_size = this->getArrayCount(); | 268 uint32_t encoded_size = this->getArrayCount(); |
| 285 if (encoded_size == 0) { | 269 if (encoded_size == 0) { |
| 286 // The image could not be encoded at serialization time - return an empt y placeholder. | 270 // The image could not be encoded at serialization time - return an empt y placeholder. |
| 287 (void)this->readUInt(); // Swallow that encoded_size == 0 sentinel. | 271 (void)this->readUInt(); // Swallow that encoded_size == 0 sentinel. |
| 288 return placeholder(); | 272 return MakeEmptyImage(width, height); |
| 289 } | 273 } |
| 290 if (encoded_size == 1) { | 274 if (encoded_size == 1) { |
| 291 // We had to encode the image as raw pixels via SkBitmap. | 275 // We had to encode the image as raw pixels via SkBitmap. |
| 292 (void)this->readUInt(); // Swallow that encoded_size == 1 sentinel. | 276 (void)this->readUInt(); // Swallow that encoded_size == 1 sentinel. |
| 293 SkBitmap bm; | 277 SkBitmap bm; |
| 294 if (SkBitmap::ReadRawPixels(this, &bm)) { | 278 if (SkBitmap::ReadRawPixels(this, &bm)) { |
| 295 return SkImage::MakeFromBitmap(bm).release(); | 279 return SkImage::MakeFromBitmap(bm); |
| 296 } | 280 } |
| 297 return placeholder(); | 281 return MakeEmptyImage(width, height); |
| 298 } | 282 } |
| 299 | 283 |
| 300 // The SkImage encoded itself. | 284 // The SkImage encoded itself. |
| 301 sk_sp<SkData> encoded(this->readByteArrayAsData()); | 285 sk_sp<SkData> encoded(this->readByteArrayAsData()); |
| 302 | 286 |
| 303 int originX = this->read32(); | 287 int originX = this->read32(); |
| 304 int originY = this->read32(); | 288 int originY = this->read32(); |
| 305 if (originX < 0 || originY < 0) { | 289 if (originX < 0 || originY < 0) { |
| 306 this->validate(false); | 290 this->validate(false); |
| 307 return nullptr; | 291 return nullptr; |
| 308 } | 292 } |
| 309 | 293 |
| 310 const SkIRect subset = SkIRect::MakeXYWH(originX, originY, width, height); | 294 const SkIRect subset = SkIRect::MakeXYWH(originX, originY, width, height); |
| 311 SkImage* image = SkImage::MakeFromEncoded(std::move(encoded), &subset).relea se(); | 295 |
| 312 if (image) { | 296 sk_sp<SkImage> image; |
| 313 return image; | 297 if (fImageDeserializer) { |
|
mtklein
2016/08/11 01:02:55
always true?
reed1
2016/08/11 10:34:25
Done.
| |
| 298 image = fImageDeserializer->makeFromData(encoded.get(), &subset); | |
| 299 } else { | |
| 300 image = SkImage::MakeFromEncoded(std::move(encoded), &subset); | |
| 314 } | 301 } |
| 315 | 302 return image ? image : MakeEmptyImage(width, height); |
| 316 return SkImage::MakeFromGenerator( | |
| 317 new EmptyImageGenerator(SkImageInfo::MakeN32Premul(width, height))). release(); | |
| 318 } | 303 } |
| 319 | 304 |
| 320 SkTypeface* SkReadBuffer::readTypeface() { | 305 SkTypeface* SkReadBuffer::readTypeface() { |
| 321 | 306 |
| 322 uint32_t index = fReader.readU32(); | 307 uint32_t index = fReader.readU32(); |
| 323 if (0 == index || index > (unsigned)fTFCount) { | 308 if (0 == index || index > (unsigned)fTFCount) { |
| 324 return nullptr; | 309 return nullptr; |
| 325 } else { | 310 } else { |
| 326 SkASSERT(fTFArray); | 311 SkASSERT(fTFArray); |
| 327 return fTFArray[index - 1]; | 312 return fTFArray[index - 1]; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 if (sizeRecorded != sizeRead) { | 373 if (sizeRecorded != sizeRead) { |
| 389 this->validate(false); | 374 this->validate(false); |
| 390 return nullptr; | 375 return nullptr; |
| 391 } | 376 } |
| 392 } else { | 377 } else { |
| 393 // we must skip the remaining data | 378 // we must skip the remaining data |
| 394 fReader.skip(sizeRecorded); | 379 fReader.skip(sizeRecorded); |
| 395 } | 380 } |
| 396 return obj.release(); | 381 return obj.release(); |
| 397 } | 382 } |
| OLD | NEW |