ScalableImageDecoder.h [plain text]
#pragma once
#include "ImageDecoder.h"
#include "IntRect.h"
#include "ScalableImageDecoderFrame.h"
#include "SharedBuffer.h"
#include <wtf/Assertions.h>
#include <wtf/Lock.h>
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class ScalableImageDecoder : public ImageDecoder {
WTF_MAKE_NONCOPYABLE(ScalableImageDecoder); WTF_MAKE_FAST_ALLOCATED;
public:
ScalableImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption)
: m_premultiplyAlpha(alphaOption == AlphaOption::Premultiplied)
, m_ignoreGammaAndColorProfile(gammaAndColorProfileOption == GammaAndColorProfileOption::Ignored)
{
}
virtual ~ScalableImageDecoder()
{
}
static bool supportsMediaType(MediaType type) { return type == MediaType::Image; }
static RefPtr<ScalableImageDecoder> create(SharedBuffer& data, AlphaOption, GammaAndColorProfileOption);
bool premultiplyAlpha() const { return m_premultiplyAlpha; }
bool isAllDataReceived() const override
{
ASSERT(!m_decodingSizeFromSetData);
return m_encodedDataStatus == EncodedDataStatus::Complete;
}
void setData(SharedBuffer& data, bool allDataReceived) override
{
LockHolder lockHolder(m_mutex);
if (m_encodedDataStatus == EncodedDataStatus::Error)
return;
if (data.data()) {
m_data = data.begin()->segment.copyRef();
}
if (m_encodedDataStatus == EncodedDataStatus::TypeAvailable) {
m_decodingSizeFromSetData = true;
tryDecodeSize(allDataReceived);
m_decodingSizeFromSetData = false;
}
if (m_encodedDataStatus == EncodedDataStatus::Error)
return;
if (allDataReceived) {
ASSERT(m_encodedDataStatus == EncodedDataStatus::SizeAvailable);
m_encodedDataStatus = EncodedDataStatus::Complete;
}
}
EncodedDataStatus encodedDataStatus() const override { return m_encodedDataStatus; }
bool isSizeAvailable() const override { return m_encodedDataStatus >= EncodedDataStatus::SizeAvailable; }
IntSize size() const override { return isSizeAvailable() ? m_size : IntSize(); }
IntSize frameSizeAtIndex(size_t, SubsamplingLevel) const override
{
return size();
}
virtual bool setSize(const IntSize& size)
{
if (ImageBackingStore::isOverSize(size))
return setFailed();
m_size = size;
m_encodedDataStatus = EncodedDataStatus::SizeAvailable;
return true;
}
size_t frameCount() const override { return 1; }
RepetitionCount repetitionCount() const override { return RepetitionCountNone; }
virtual ScalableImageDecoderFrame* frameBufferAtIndex(size_t) = 0;
bool frameIsCompleteAtIndex(size_t) const override;
bool frameHasAlphaAtIndex(size_t) const override;
unsigned frameBytesAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default) const override;
Seconds frameDurationAtIndex(size_t) const final;
NativeImagePtr createFrameImageAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default, const DecodingOptions& = DecodingOptions(DecodingMode::Synchronous)) override;
void setIgnoreGammaAndColorProfile(bool flag) { m_ignoreGammaAndColorProfile = flag; }
bool ignoresGammaAndColorProfile() const { return m_ignoreGammaAndColorProfile; }
ImageOrientation frameOrientationAtIndex(size_t) const override { return m_orientation; }
bool frameAllowSubsamplingAtIndex(size_t) const override { return false; }
enum { ICCColorProfileHeaderLength = 128 };
static bool rgbColorProfile(const char* profileData, unsigned profileLength)
{
ASSERT_UNUSED(profileLength, profileLength >= ICCColorProfileHeaderLength);
return !memcmp(&profileData[16], "RGB ", 4);
}
size_t bytesDecodedToDetermineProperties() const final { return 0; }
static SubsamplingLevel subsamplingLevelForScale(float, SubsamplingLevel) { return SubsamplingLevel::Default; }
static bool inputDeviceColorProfile(const char* profileData, unsigned profileLength)
{
ASSERT_UNUSED(profileLength, profileLength >= ICCColorProfileHeaderLength);
return !memcmp(&profileData[12], "mntr", 4) || !memcmp(&profileData[12], "scnr", 4);
}
virtual bool setFailed()
{
m_encodedDataStatus = EncodedDataStatus::Error;
return false;
}
bool failed() const { return m_encodedDataStatus == EncodedDataStatus::Error; }
void clearFrameBufferCache(size_t) override { }
Optional<IntPoint> hotSpot() const override { return WTF::nullopt; }
protected:
RefPtr<SharedBuffer::DataSegment> m_data;
Vector<ScalableImageDecoderFrame, 1> m_frameBufferCache;
mutable Lock m_mutex;
bool m_premultiplyAlpha;
bool m_ignoreGammaAndColorProfile;
ImageOrientation m_orientation;
private:
virtual void tryDecodeSize(bool) = 0;
#if USE(DIRECT2D)
void setTargetContext(ID2D1RenderTarget*) override;
#endif
IntSize m_size;
EncodedDataStatus m_encodedDataStatus { EncodedDataStatus::TypeAvailable };
bool m_decodingSizeFromSetData { false };
};
}