| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include <new> | 7 #include <new> |
| 8 #include "SkImageGenerator.h" | 8 #include "SkImageGenerator.h" |
| 9 #include "SkPictureData.h" | 9 #include "SkPictureData.h" |
| 10 #include "SkPictureRecord.h" | 10 #include "SkPictureRecord.h" |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 } | 427 } |
| 428 if (!buffer.isValid()) { | 428 if (!buffer.isValid()) { |
| 429 return false; | 429 return false; |
| 430 } | 430 } |
| 431 SkDEBUGCODE(haveBuffer = true;) | 431 SkDEBUGCODE(haveBuffer = true;) |
| 432 } break; | 432 } break; |
| 433 } | 433 } |
| 434 return true; // success | 434 return true; // success |
| 435 } | 435 } |
| 436 | 436 |
| 437 namespace { | |
| 438 | |
| 439 // This generator intentionally should always fail on all attempts to get its pi
xels, | |
| 440 // simulating a bad or empty codec stream. | |
| 441 class EmptyImageGenerator final : public SkImageGenerator { | |
| 442 public: | |
| 443 EmptyImageGenerator(const SkImageInfo& info) : INHERITED(info) { } | |
| 444 | |
| 445 private: | |
| 446 typedef SkImageGenerator INHERITED; | |
| 447 }; | |
| 448 | |
| 449 } // anonymous namespace | |
| 450 | |
| 451 static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) { | 437 static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) { |
| 452 int width = buffer.read32(); | 438 return buffer.readImage(); |
| 453 int height = buffer.read32(); | |
| 454 if (width <= 0 || height <= 0) { // SkImage never has a zero dimension | |
| 455 buffer.validate(false); | |
| 456 return nullptr; | |
| 457 } | |
| 458 | |
| 459 SkAutoTUnref<SkData> encoded(buffer.readByteArrayAsData()); | |
| 460 if (encoded->size() == 0) { | |
| 461 // The image could not be encoded at serialization time - return an empt
y placeholder. | |
| 462 return SkImage::NewFromGenerator( | |
| 463 new EmptyImageGenerator(SkImageInfo::MakeN32Premul(width, height))); | |
| 464 } | |
| 465 | |
| 466 int originX = buffer.read32(); | |
| 467 int originY = buffer.read32(); | |
| 468 if (originX < 0 || originY < 0) { | |
| 469 buffer.validate(false); | |
| 470 return nullptr; | |
| 471 } | |
| 472 | |
| 473 const SkIRect subset = SkIRect::MakeXYWH(originX, originY, width, height); | |
| 474 return SkImage::NewFromEncoded(encoded, &subset); | |
| 475 } | 439 } |
| 476 | 440 |
| 477 // Need a shallow wrapper to return const SkPicture* to match the other factorie
s, | 441 // Need a shallow wrapper to return const SkPicture* to match the other factorie
s, |
| 478 // as SkPicture::CreateFromBuffer() returns SkPicture* | 442 // as SkPicture::CreateFromBuffer() returns SkPicture* |
| 479 static const SkPicture* create_picture_from_buffer(SkReadBuffer& buffer) { | 443 static const SkPicture* create_picture_from_buffer(SkReadBuffer& buffer) { |
| 480 return SkPicture::CreateFromBuffer(buffer); | 444 return SkPicture::CreateFromBuffer(buffer); |
| 481 } | 445 } |
| 482 | 446 |
| 483 template <typename T> | 447 template <typename T> |
| 484 bool new_array_from_buffer(SkReadBuffer& buffer, uint32_t inCount, | 448 bool new_array_from_buffer(SkReadBuffer& buffer, uint32_t inCount, |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 } | 619 } |
| 656 } | 620 } |
| 657 | 621 |
| 658 bool SkPictureData::suitableForLayerOptimization() const { | 622 bool SkPictureData::suitableForLayerOptimization() const { |
| 659 return fContentInfo.numLayers() > 0; | 623 return fContentInfo.numLayers() > 0; |
| 660 } | 624 } |
| 661 #endif | 625 #endif |
| 662 /////////////////////////////////////////////////////////////////////////////// | 626 /////////////////////////////////////////////////////////////////////////////// |
| 663 | 627 |
| 664 | 628 |
| OLD | NEW |