Index: third_party/WebKit/Source/platform/graphics/ImageFrameGenerator.cpp |
diff --git a/third_party/WebKit/Source/platform/graphics/ImageFrameGenerator.cpp b/third_party/WebKit/Source/platform/graphics/ImageFrameGenerator.cpp |
index 0d29bce0f18e6463f7fcbfd94cabe702c6455231..eb6cbe36474627402d7b26f68f3e1ddcbd94963e 100644 |
--- a/third_party/WebKit/Source/platform/graphics/ImageFrameGenerator.cpp |
+++ b/third_party/WebKit/Source/platform/graphics/ImageFrameGenerator.cpp |
@@ -105,6 +105,7 @@ ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<Sha |
, m_decodeFailedAndEmpty(false) |
, m_decodeCount(0) |
, m_frameCount(0) |
+ , m_decoderCanDecodeToIndex8(false) |
{ |
setData(data.get(), allDataReceived); |
} |
@@ -127,7 +128,7 @@ void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataRece |
*data = buffer->copy(); |
} |
-bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes) |
+bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes, SkPMColor ctable[], int* ctableCount) |
{ |
// This method is called to populate a discardable memory owned by Skia. |
@@ -145,7 +146,7 @@ bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, |
m_externalAllocator = adoptPtr(new ExternalMemoryAllocator(info, pixels, rowBytes)); |
- SkBitmap bitmap = tryToResumeDecode(scaledSize, index); |
+ SkBitmap bitmap = tryToResumeDecode(scaledSize, index, info.colorType()); |
if (bitmap.isNull()) |
return false; |
@@ -156,12 +157,25 @@ bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, |
ASSERT(bitmap.width() == scaledSize.width()); |
ASSERT(bitmap.height() == scaledSize.height()); |
+ // If decoder cannot produce requested color type, return false. |
+ if (bitmap.colorType() != info.colorType()) |
+ return false; |
+ |
bool result = true; |
SkAutoLockPixels bitmapLock(bitmap); |
// Check to see if decoder has written directly to the memory provided |
// by Skia. If not make a copy. |
if (bitmap.getPixels() != pixels) |
result = bitmap.copyPixelsTo(pixels, rowBytes * info.height(), rowBytes); |
+ if (bitmap.colorType() == kIndex_8_SkColorType && ctable && bitmap.getColorTable()) { |
+ memcpy(ctable, bitmap.getColorTable()->readColors(), bitmap.getColorTable()->count() * sizeof(SkPMColor)); |
+ } |
+ if (ctableCount) { |
+ // Based on SkImageGenerator API spec, ctableCount needs to be set ot 0 for other types. |
+ *ctableCount = (bitmap.colorType() == kIndex_8_SkColorType && bitmap.getColorTable()) |
+ ? bitmap.getColorTable()->count() |
+ : 0; |
+ } |
return result; |
} |
@@ -207,7 +221,7 @@ bool ImageFrameGenerator::decodeToYUV(SkISize componentSizes[3], void* planes[3] |
return yuvDecoded; |
} |
-SkBitmap ImageFrameGenerator::tryToResumeDecode(const SkISize& scaledSize, size_t index) |
+SkBitmap ImageFrameGenerator::tryToResumeDecode(const SkISize& scaledSize, size_t index, SkColorType outputColor) |
{ |
TRACE_EVENT1("blink", "ImageFrameGenerator::tryToResumeDecodeAndScale", "index", static_cast<int>(index)); |
@@ -216,7 +230,7 @@ SkBitmap ImageFrameGenerator::tryToResumeDecode(const SkISize& scaledSize, size_ |
ASSERT(!resumeDecoding || decoder); |
SkBitmap fullSizeImage; |
- bool complete = decode(index, &decoder, &fullSizeImage); |
+ bool complete = decode(index, &decoder, &fullSizeImage, outputColor); |
if (!decoder) |
return SkBitmap(); |
@@ -282,7 +296,7 @@ void ImageFrameGenerator::setHasAlpha(size_t index, bool hasAlpha) |
m_hasAlpha[index] = hasAlpha; |
} |
-bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap) |
+bool ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder, SkBitmap* bitmap, SkColorType outputColor) |
{ |
TRACE_EVENT2("blink", "ImageFrameGenerator::decode", "width", m_fullSize.width(), "height", m_fullSize.height()); |
@@ -381,4 +395,37 @@ bool ImageFrameGenerator::getYUVComponentSizes(SkISize componentSizes[3]) |
return updateYUVComponentSizes(decoder.get(), componentSizes, ImageDecoder::SizeForMemoryAllocation); |
} |
+bool ImageFrameGenerator::canDecodeTo(size_t index, SkColorType outputType) |
+{ |
+ if (m_decoderCanDecodeToIndex8) { |
+ // Prevents concurrent decode or scale operations on the same image data. |
+ MutexLocker lock(m_decodeMutex); |
+ ImageDecoder* decoder = 0; |
+ const bool decoderExist = ImageDecodingStore::instance().lockDecoder(this, m_fullSize, &decoder); |
+ ASSERT(!decoderExist || decoder); |
+ SharedBuffer* data = 0; |
+ bool allDataReceived = false; |
+ m_data.data(&data, &allDataReceived); |
+ if (!decoderExist) { |
+ if (m_imageDecoderFactory) |
+ decoder = m_imageDecoderFactory->create().leakPtr(); |
+ if (!decoder) |
+ decoder = ImageDecoder::create(*data, ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileApplied).leakPtr(); |
+ if (!decoder) |
+ return (outputType == kIndex_8_SkColorType); |
+ } |
+ decoder->setData(data, allDataReceived); |
+ bool canDecodeToIndex8 = decoder->canDecodeTo(index, static_cast<ImageFrame::ColorType>(outputType)); |
+ decoder->setData(0, false); // Unref SharedBuffer from ImageDecoder. |
+ if (decoderExist) { |
+ ImageDecodingStore::instance().unlockDecoder(this, decoder); |
+ } else { |
+ ImageDecodingStore::instance().insertDecoder(this, PassOwnPtr<blink::ImageDecoder>(adoptPtr(decoder))); |
+ } |
+ return (canDecodeToIndex8 && (outputType == kIndex_8_SkColorType)); |
+ } |
+ return (outputType == kN32_SkColorType); |
+} |
+ |
+ |
} // namespace blink |