#ifndef ImageFrameGenerator_h
#define ImageFrameGenerator_h
#include "SkBitmap.h"
#include "SkSize.h"
#include "SkTypes.h"
#include "platform/PlatformExport.h"
#include "platform/graphics/ThreadSafeDataTransport.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
#include "wtf/ThreadingPrimitives.h"
#include "wtf/ThreadSafeRefCounted.h"
#include "wtf/Vector.h"
namespace WebCore {
class ImageDecoder;
class ScaledImageFragment;
class SharedBuffer;
class PLATFORM_EXPORT ImageDecoderFactory {
WTF_MAKE_NONCOPYABLE(ImageDecoderFactory);
public:
ImageDecoderFactory() {}
virtual ~ImageDecoderFactory() { }
virtual PassOwnPtr<ImageDecoder> create() = 0;
};
class PLATFORM_EXPORT ImageFrameGenerator : public ThreadSafeRefCounted<ImageFrameGenerator> {
WTF_MAKE_NONCOPYABLE(ImageFrameGenerator);
public:
static PassRefPtr<ImageFrameGenerator> create(const SkISize& fullSize, PassRefPtr<SharedBuffer> data, bool allDataReceived, bool isMultiFrame = false)
{
return adoptRef(new ImageFrameGenerator(fullSize, data, allDataReceived, isMultiFrame));
}
ImageFrameGenerator(const SkISize& fullSize, PassRefPtr<SharedBuffer>, bool allDataReceived, bool isMultiFrame);
~ImageFrameGenerator();
const ScaledImageFragment* decodeAndScale(const SkISize& scaledSize, size_t index = 0);
bool decodeAndScale(const SkImageInfo&, size_t index, void* pixels, size_t rowBytes);
void setData(PassRefPtr<SharedBuffer>, bool allDataReceived);
void copyData(RefPtr<SharedBuffer>*, bool* allDataReceived);
SkISize getFullSize() const { return m_fullSize; }
bool isMultiFrame() const { return m_isMultiFrame; }
bool hasAlpha(size_t);
private:
class ExternalMemoryAllocator;
friend class ImageFrameGeneratorTest;
friend class DeferredImageDecoderTest;
void setImageDecoderFactory(PassOwnPtr<ImageDecoderFactory> factory) { m_imageDecoderFactory = factory; }
SkBitmap::Allocator* allocator() const { return m_discardableAllocator.get(); }
void setAllocator(PassOwnPtr<SkBitmap::Allocator> allocator) { m_discardableAllocator = allocator; }
const ScaledImageFragment* tryToLockCompleteCache(const SkISize& scaledSize, size_t index);
const ScaledImageFragment* tryToResumeDecode(const SkISize& scaledSize, size_t index);
PassOwnPtr<ScaledImageFragment> decode(size_t index, ImageDecoder**);
size_t nextGenerationId() { return m_decodeCount++; }
SkISize m_fullSize;
ThreadSafeDataTransport m_data;
bool m_isMultiFrame;
bool m_decodeFailedAndEmpty;
Vector<bool> m_hasAlpha;
size_t m_decodeCount;
OwnPtr<SkBitmap::Allocator> m_discardableAllocator;
OwnPtr<ExternalMemoryAllocator> m_externalAllocator;
OwnPtr<ImageDecoderFactory> m_imageDecoderFactory;
Mutex m_decodeMutex;
Mutex m_alphaMutex;
};
}
#endif