Chromium Code Reviews| Index: src/images/SkDecodingImageGenerator.cpp |
| diff --git a/src/images/SkDecodingImageGenerator.cpp b/src/images/SkDecodingImageGenerator.cpp |
| index 05651c78c29e2f67af1b568846f2bfd24fb9727e..a19031b4bd634ce5177e9915ee85f20c94e26d2f 100644 |
| --- a/src/images/SkDecodingImageGenerator.cpp |
| +++ b/src/images/SkDecodingImageGenerator.cpp |
| @@ -9,39 +9,168 @@ |
| #include "SkData.h" |
| #include "SkDiscardablePixelRef.h" |
| #include "SkImageDecoder.h" |
| +#include "SkImagePriv.h" |
| +#include "SkStream.h" |
| + |
| + |
| +namespace { |
| +/** |
| + * Special allocator used by getPixels(). Uses preallocated memory |
| + * provided. |
| + */ |
| +class TargetAllocator : public SkBitmap::Allocator { |
| +public: |
| + TargetAllocator(void* target, size_t rowBytes, const SkImageInfo& info) |
| + : fTarget(target) |
| + , fRowBytes(rowBytes) |
| + , fInfo(info) { } |
| + |
| + virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) SK_OVERRIDE { |
| + if ((SkImageInfoToBitmapConfig(fInfo) != bm->config()) |
| + || (bm->width() != fInfo.fWidth) |
| + || (bm->height() != fInfo.fHeight)) { |
| + return false; |
| + } |
| + bm->setConfig(bm->config(), bm->width(), bm->height(), |
| + fRowBytes, bm->alphaType()); |
| + bm->setPixels(fTarget, ct); |
| + return true; |
| + } |
| + |
| +private: |
| + void* fTarget; |
| + size_t fRowBytes; |
| + SkImageInfo fInfo; |
| + typedef SkBitmap::Allocator INHERITED; |
| +}; |
| +} // namespace |
| +//////////////////////////////////////////////////////////////////////////////// |
| SkDecodingImageGenerator::SkDecodingImageGenerator(SkData* data) |
| - : fData(data) { |
| + : fData(data) |
| + , fHasInfo(false) |
| + , fDoCopyTo(false) { |
| SkASSERT(fData != NULL); |
| + fStream = SkNEW_ARGS(SkMemoryStream, (fData)); |
| + SkASSERT(fStream != NULL); |
| fData->ref(); |
| } |
| +SkDecodingImageGenerator::SkDecodingImageGenerator(SkStreamRewindable* stream) |
| + : fData(NULL) |
| + , fStream(stream) |
| + , fHasInfo(false) |
| + , fDoCopyTo(false) { |
| + SkASSERT(fStream != NULL); |
| + fHasInfo = false; |
| +} |
| + |
| SkDecodingImageGenerator::~SkDecodingImageGenerator() { |
| - fData->unref(); |
| + SkSafeUnref(fData); |
| + fStream->unref(); |
| } |
| SkData* SkDecodingImageGenerator::refEncodedData() { |
| // This functionality is used in `gm --serialize` |
| - fData->ref(); |
| - return fData; |
| + if (fData != NULL) { |
| + return SkSafeRef(fData); |
| + } |
| + fData = fStream->getData(); |
| + if (fData != NULL) { |
| + return SkSafeRef(fData); |
| + } |
| + return SkData::NewWithCopyFromStream(fStream); |
| } |
| bool SkDecodingImageGenerator::getInfo(SkImageInfo* info) { |
| - SkASSERT(info != NULL); |
| - return SkImageDecoder::DecodeMemoryToTarget(fData->data(), |
| - fData->size(), |
| - info, NULL); |
| + // info can be NULL. If so, will update fInfo, fDoCopyTo, and fHasInfo. |
| + if (fHasInfo) { |
| + if (info != NULL) { |
| + *info = fInfo; |
| + } |
| + return true; |
| + } |
| + SkAssertResult(fStream->rewind()); |
| + SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| + if (NULL == decoder.get()) { |
| + return false; |
| + } |
| + SkBitmap bitmap; |
| + if (!decoder->decode(fStream, &bitmap, |
| + SkImageDecoder::kDecodeBounds_Mode)) { |
| + return false; |
| + } |
| + if (bitmap.config() == SkBitmap::kNo_Config) { |
| + return false; |
| + } |
| + if (!SkBitmapToImageInfo(bitmap, &fInfo)) { |
|
scroggo
2013/12/05 21:07:18
Once https://codereview.chromium.org/68973005/ is
hal.canary
2013/12/05 22:29:29
That makes sense.
|
| + // We can't use bitmap.config() as is. |
| + // Must be kARGB_4444_Config. |
| + if (!bitmap.canCopyTo(SkBitmap::kARGB_8888_Config)) { |
| + // kARGB_4444_Config can copy to kARGB_8888. |
| + SkDEBUGFAIL("!bitmap->canCopyTo(SkBitmap::kARGB_8888_Config)"); |
| + return false; |
| + } |
| + fDoCopyTo = true; |
| + fInfo.fWidth = bitmap.width(); |
| + fInfo.fHeight = bitmap.height(); |
| + fInfo.fColorType = kPMColor_SkColorType; |
| + fInfo.fAlphaType = bitmap.alphaType(); |
| + } |
| + if (info != NULL) { |
| + *info = fInfo; |
| + } |
| + fHasInfo = true; |
| + return true; |
| } |
| bool SkDecodingImageGenerator::getPixels(const SkImageInfo& info, |
| void* pixels, |
| size_t rowBytes) { |
| - SkASSERT(pixels != NULL); |
| - SkImageDecoder::Target target = {pixels, rowBytes}; |
| - SkImageInfo tmpInfo = info; |
| - return SkImageDecoder::DecodeMemoryToTarget(fData->data(), |
| - fData->size(), |
| - &tmpInfo, &target); |
| + if (NULL == pixels) { |
| + return false; |
| + } |
| + if (!this->getInfo(NULL)) { |
| + return false; |
| + } |
| + SkAssertResult(fStream->rewind()); |
| + SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); |
| + if (NULL == decoder.get()) { |
| + return false; |
| + } |
| + if (fInfo != info) { |
| + // if (SkImageInfoToBitmapConfig(info) == SkBitmap::kNo_Config) { |
| + // return false; |
| + // } |
| + // The caller has specified a different info. for now, this |
| + // is an error. In the future, we will check to see if we can |
| + // convert. |
| + return false; |
| + } |
| + int bpp = SkBitmap::ComputeBytesPerPixel(SkImageInfoToBitmapConfig(info)); |
| + if (static_cast<size_t>(bpp * info.fWidth) > rowBytes) { |
| + return false; |
| + } |
| + SkBitmap bitmap; |
| + if (!bitmap.setConfig(info, rowBytes)) { |
| + return false; |
| + } |
| + |
| + TargetAllocator allocator(pixels, rowBytes, info); |
| + if (!fDoCopyTo) { |
| + decoder->setAllocator(&allocator); |
| + } |
| + bool success = decoder->decode(fStream, &bitmap, |
| + SkImageDecoder::kDecodePixels_Mode); |
| + decoder->setAllocator(NULL); |
| + if (!success) { |
| + return false; |
| + } |
| + if (fDoCopyTo) { |
| + SkBitmap bm8888; |
| + bitmap.copyTo(&bm8888, SkBitmap::kARGB_8888_Config, &allocator); |
| + } |
| + return true; |
| } |
| bool SkDecodingImageGenerator::Install(SkData* data, SkBitmap* dst, |
| SkDiscardableMemory::Factory* factory) { |
| @@ -50,3 +179,13 @@ bool SkDecodingImageGenerator::Install(SkData* data, SkBitmap* dst, |
| SkImageGenerator* gen(SkNEW_ARGS(SkDecodingImageGenerator, (data))); |
| return SkDiscardablePixelRef::Install(gen, dst, factory); |
| } |
| + |
| +bool SkDecodingImageGenerator::Install(SkStreamRewindable* stream, |
| + SkBitmap* dst, |
| + SkDiscardableMemory::Factory* factory) { |
| + SkASSERT(stream != NULL); |
| + SkASSERT(dst != NULL); |
| + SkImageGenerator* gen(SkNEW_ARGS(SkDecodingImageGenerator, (stream))); |
| + return SkDiscardablePixelRef::Install(gen, dst, factory); |
| +} |
| + |