This source file includes following definitions.
- m_rowBytes
- allocPixelRef
- m_discardableAllocator
- setData
- copyData
- decodeAndScale
- decodeAndScale
- tryToLockCompleteCache
- tryToResumeDecode
- decode
- hasAlpha
#include "config.h"
#include "platform/graphics/ImageFrameGenerator.h"
#include "platform/SharedBuffer.h"
#include "platform/TraceEvent.h"
#include "platform/graphics/DiscardablePixelRef.h"
#include "platform/graphics/ImageDecodingStore.h"
#include "platform/graphics/ScaledImageFragment.h"
#include "platform/image-decoders/ImageDecoder.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkMallocPixelRef.h"
namespace WebCore {
class ImageFrameGenerator::ExternalMemoryAllocator : public SkBitmap::Allocator {
public:
ExternalMemoryAllocator(const SkImageInfo& info, void* pixels, size_t rowBytes)
: m_info(info)
, m_pixels(pixels)
, m_rowBytes(rowBytes)
{
}
virtual bool allocPixelRef(SkBitmap* dst, SkColorTable* ctable) OVERRIDE
{
SkImageInfo info;
if (!dst->asImageInfo(&info))
return false;
if (info != m_info || m_rowBytes != dst->rowBytes())
return false;
if (!dst->installPixels(m_info, m_pixels, m_rowBytes, 0, 0))
return false;
dst->lockPixels();
return true;
}
private:
SkImageInfo m_info;
void* m_pixels;
size_t m_rowBytes;
};
ImageFrameGenerator::ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<SharedBuffer> data, bool allDataReceived, bool isMultiFrame)
: m_fullSize(fullSize)
, m_isMultiFrame(isMultiFrame)
, m_decodeFailedAndEmpty(false)
, m_decodeCount(ScaledImageFragment::FirstPartialImage)
, m_discardableAllocator(adoptPtr(new DiscardablePixelRefAllocator()))
{
setData(data.get(), allDataReceived);
}
ImageFrameGenerator::~ImageFrameGenerator()
{
if (ImageDecodingStore::instance())
ImageDecodingStore::instance()->removeCacheIndexedByGenerator(this);
}
void ImageFrameGenerator::setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
{
m_data.setData(data.get(), allDataReceived);
}
void ImageFrameGenerator::copyData(RefPtr<SharedBuffer>* data, bool* allDataReceived)
{
SharedBuffer* buffer = 0;
m_data.data(&buffer, allDataReceived);
if (buffer)
*data = buffer->copy();
}
const ScaledImageFragment* ImageFrameGenerator::decodeAndScale(const SkISize& scaledSize, size_t index)
{
MutexLocker lock(m_decodeMutex);
if (m_decodeFailedAndEmpty)
return 0;
const ScaledImageFragment* cachedImage = 0;
cachedImage = tryToLockCompleteCache(scaledSize, index);
if (cachedImage)
return cachedImage;
TRACE_EVENT2("webkit", "ImageFrameGenerator::decodeAndScale", "generator", this, "decodeCount", static_cast<int>(m_decodeCount));
cachedImage = tryToResumeDecode(scaledSize, index);
if (cachedImage)
return cachedImage;
return 0;
}
bool ImageFrameGenerator::decodeAndScale(const SkImageInfo& info, size_t index, void* pixels, size_t rowBytes)
{
MutexLocker lock(m_decodeMutex);
SkISize scaledSize = SkISize::Make(info.fWidth, info.fHeight);
ASSERT(m_fullSize == scaledSize);
if (m_decodeFailedAndEmpty)
return 0;
TRACE_EVENT2("webkit", "ImageFrameGenerator::decodeAndScale", "generator", this, "decodeCount", static_cast<int>(m_decodeCount));
m_discardableAllocator.clear();
m_externalAllocator = adoptPtr(new ExternalMemoryAllocator(info, pixels, rowBytes));
const ScaledImageFragment* cachedImage = tryToResumeDecode(scaledSize, index);
if (!cachedImage)
return false;
m_externalAllocator.clear();
ASSERT(cachedImage->bitmap().width() == scaledSize.width());
ASSERT(cachedImage->bitmap().height() == scaledSize.height());
bool result = true;
if (cachedImage->bitmap().getPixels() != pixels)
result = cachedImage->bitmap().copyPixelsTo(pixels, rowBytes * info.fHeight, rowBytes);
ImageDecodingStore::instance()->unlockCache(this, cachedImage);
return result;
}
const ScaledImageFragment* ImageFrameGenerator::tryToLockCompleteCache(const SkISize& scaledSize, size_t index)
{
const ScaledImageFragment* cachedImage = 0;
if (ImageDecodingStore::instance()->lockCache(this, scaledSize, index, &cachedImage))
return cachedImage;
return 0;
}
const ScaledImageFragment* ImageFrameGenerator::tryToResumeDecode(const SkISize& scaledSize, size_t index)
{
TRACE_EVENT1("webkit", "ImageFrameGenerator::tryToResumeDecodeAndScale", "index", static_cast<int>(index));
ImageDecoder* decoder = 0;
const bool resumeDecoding = ImageDecodingStore::instance()->lockDecoder(this, m_fullSize, &decoder);
ASSERT(!resumeDecoding || decoder);
OwnPtr<ScaledImageFragment> fullSizeImage = decode(index, &decoder);
if (!decoder)
return 0;
OwnPtr<ImageDecoder> decoderContainer;
if (!resumeDecoding)
decoderContainer = adoptPtr(decoder);
if (!fullSizeImage) {
m_decodeFailedAndEmpty = !m_isMultiFrame && decoder->failed();
if (resumeDecoding)
ImageDecodingStore::instance()->unlockDecoder(this, decoder);
return 0;
}
const ScaledImageFragment* cachedImage = ImageDecodingStore::instance()->insertAndLockCache(this, fullSizeImage.release());
const bool removeDecoder = cachedImage->isComplete() && !m_isMultiFrame;
if (resumeDecoding) {
if (removeDecoder)
ImageDecodingStore::instance()->removeDecoder(this, decoder);
else
ImageDecodingStore::instance()->unlockDecoder(this, decoder);
} else if (!removeDecoder) {
ImageDecodingStore::instance()->insertDecoder(this, decoderContainer.release(), DiscardablePixelRef::isDiscardable(cachedImage->bitmap().pixelRef()));
}
return cachedImage;
}
PassOwnPtr<ScaledImageFragment> ImageFrameGenerator::decode(size_t index, ImageDecoder** decoder)
{
TRACE_EVENT2("webkit", "ImageFrameGenerator::decode", "width", m_fullSize.width(), "height", m_fullSize.height());
ASSERT(decoder);
SharedBuffer* data = 0;
bool allDataReceived = false;
bool newDecoder = false;
m_data.data(&data, &allDataReceived);
if (!*decoder) {
newDecoder = true;
if (m_imageDecoderFactory)
*decoder = m_imageDecoderFactory->create().leakPtr();
if (!*decoder)
*decoder = ImageDecoder::create(*data, ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileApplied).leakPtr();
if (!*decoder)
return nullptr;
}
bool canSkipBitmapCopy = false;
if (!m_isMultiFrame && newDecoder && allDataReceived) {
canSkipBitmapCopy = true;
if (m_externalAllocator)
(*decoder)->setMemoryAllocator(m_externalAllocator.get());
else
(*decoder)->setMemoryAllocator(m_discardableAllocator.get());
}
(*decoder)->setData(data, allDataReceived);
ImageFrame* frame = (*decoder)->frameBufferAtIndex(index);
(*decoder)->setData(0, false);
(*decoder)->clearCacheExceptFrame(index);
(*decoder)->setMemoryAllocator(0);
if (!frame || frame->status() == ImageFrame::FrameEmpty)
return nullptr;
const bool isCacheComplete = frame->status() == ImageFrame::FrameComplete || allDataReceived;
SkBitmap fullSizeBitmap = frame->getSkBitmap();
if (fullSizeBitmap.isNull())
return nullptr;
{
MutexLocker lock(m_alphaMutex);
if (index >= m_hasAlpha.size()) {
const size_t oldSize = m_hasAlpha.size();
m_hasAlpha.resize(index + 1);
for (size_t i = oldSize; i < m_hasAlpha.size(); ++i)
m_hasAlpha[i] = true;
}
m_hasAlpha[index] = !fullSizeBitmap.isOpaque();
}
ASSERT(fullSizeBitmap.width() == m_fullSize.width() && fullSizeBitmap.height() == m_fullSize.height());
if (canSkipBitmapCopy && isCacheComplete)
return ScaledImageFragment::createComplete(m_fullSize, index, fullSizeBitmap);
SkBitmap copyBitmap;
if (!fullSizeBitmap.copyTo(©Bitmap, fullSizeBitmap.colorType(), m_discardableAllocator.get()))
return nullptr;
if (isCacheComplete)
return ScaledImageFragment::createComplete(m_fullSize, index, copyBitmap);
return ScaledImageFragment::createPartial(m_fullSize, index, nextGenerationId(), copyBitmap);
}
bool ImageFrameGenerator::hasAlpha(size_t index)
{
MutexLocker lock(m_alphaMutex);
if (index < m_hasAlpha.size())
return m_hasAlpha[index];
return true;
}
}