#ifndef BitmapImage_h
#define BitmapImage_h
#include "Image.h"
#include "Color.h"
#include "ImageOrientation.h"
#include "ImageSource.h"
#include "IntSize.h"
#if USE(CG) || USE(APPKIT)
#include <wtf/RetainPtr.h>
#endif
#if USE(APPKIT)
OBJC_CLASS NSImage;
#endif
#if PLATFORM(WIN)
typedef struct HBITMAP__ *HBITMAP;
#endif
namespace WebCore {
struct FrameData;
}
namespace WTF {
template<> struct VectorTraits<WebCore::FrameData> : public SimpleClassVectorTraits {
static const bool canInitializeWithMemset = false; };
}
namespace WebCore {
class Timer;
struct FrameData {
WTF_MAKE_NONCOPYABLE(FrameData);
public:
FrameData()
: m_frame(0)
, m_orientation(DefaultImageOrientation)
, m_subsamplingLevel(0)
, m_duration(0)
, m_haveMetadata(false)
, m_isComplete(false)
, m_hasAlpha(true)
, m_frameBytes(0)
{
}
~FrameData()
{
clear(true);
}
bool clear(bool clearMetadata);
NativeImagePtr m_frame;
ImageOrientation m_orientation;
SubsamplingLevel m_subsamplingLevel;
float m_duration;
bool m_haveMetadata : 1;
bool m_isComplete : 1;
bool m_hasAlpha : 1;
unsigned m_frameBytes;
};
class BitmapImage final : public Image {
friend class GeneratedImage;
friend class CrossfadeGeneratedImage;
friend class GradientImage;
friend class GraphicsContext;
public:
static Ref<BitmapImage> create(PassNativeImagePtr nativeImage, ImageObserver* observer = 0)
{
return adoptRef(*new BitmapImage(nativeImage, observer));
}
static Ref<BitmapImage> create(ImageObserver* observer = 0)
{
return adoptRef(*new BitmapImage(observer));
}
#if PLATFORM(WIN)
WEBCORE_EXPORT static PassRefPtr<BitmapImage> create(HBITMAP);
#endif
virtual ~BitmapImage();
virtual bool isBitmapImage() const override { return true; }
virtual bool hasSingleSecurityOrigin() const override;
virtual FloatSize size() const override;
IntSize sizeRespectingOrientation(ImageOrientationDescription = ImageOrientationDescription()) const;
virtual bool getHotSpot(IntPoint&) const override;
unsigned decodedSize() const { return m_decodedSize; }
virtual bool dataChanged(bool allDataReceived) override;
virtual String filenameExtension() const override;
virtual void stopAnimation() override;
virtual void resetAnimation() override;
virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
const FloatPoint& phase, const FloatSize& spacing, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal) override;
#if USE(APPKIT)
virtual NSImage* getNSImage() override;
#endif
#if PLATFORM(COCOA)
virtual CFDataRef getTIFFRepresentation() override;
#endif
#if USE(CG)
WEBCORE_EXPORT virtual CGImageRef getCGImageRef() override;
virtual CGImageRef getFirstCGImageRefOfSize(const IntSize&) override;
virtual RetainPtr<CFArrayRef> getCGImageArray() override;
#endif
#if PLATFORM(WIN)
virtual bool getHBITMAP(HBITMAP) override;
virtual bool getHBITMAPOfSize(HBITMAP, const IntSize*) override;
#endif
#if PLATFORM(GTK)
virtual GdkPixbuf* getGdkPixbuf() override;
#endif
#if PLATFORM(EFL)
virtual Evas_Object* getEvasObject(Evas*) override;
#endif
virtual PassNativeImagePtr nativeImageForCurrentFrame() override;
virtual ImageOrientation orientationForCurrentFrame() override { return frameOrientationAtIndex(currentFrame()); }
virtual bool currentFrameKnownToBeOpaque() override;
virtual bool isAnimated() override { return m_frameCount > 1; }
bool canAnimate();
bool allowSubsampling() const { return m_allowSubsampling; }
void setAllowSubsampling(bool allowSubsampling) { m_allowSubsampling = allowSubsampling; }
private:
void updateSize(ImageOrientationDescription = ImageOrientationDescription()) const;
void determineMinimumSubsamplingLevel() const;
protected:
enum RepetitionCountStatus {
Unknown, Uncertain, Certain };
WEBCORE_EXPORT BitmapImage(PassNativeImagePtr, ImageObserver* = 0);
WEBCORE_EXPORT BitmapImage(ImageObserver* = 0);
#if PLATFORM(WIN)
virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator) override;
#endif
virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator, BlendMode, ImageOrientationDescription) override;
#if USE(WINGDI)
virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
const FloatPoint& phase, const FloatSize& spacing, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect);
#endif
size_t currentFrame() const { return m_currentFrame; }
size_t frameCount();
PassNativeImagePtr frameAtIndex(size_t, float presentationScaleHint = 1);
PassNativeImagePtr copyUnscaledFrameAtIndex(size_t);
bool haveFrameAtIndex(size_t);
bool frameIsCompleteAtIndex(size_t);
float frameDurationAtIndex(size_t);
bool frameHasAlphaAtIndex(size_t);
ImageOrientation frameOrientationAtIndex(size_t);
enum ImageFrameCaching { CacheMetadataOnly, CacheMetadataAndFrame };
void cacheFrame(size_t index, SubsamplingLevel, ImageFrameCaching = CacheMetadataAndFrame);
bool ensureFrameIsCached(size_t index, ImageFrameCaching = CacheMetadataAndFrame);
virtual void destroyDecodedData(bool destroyAll = true) override;
void destroyDecodedDataIfNecessary(bool destroyAll);
enum class ClearedSource { No, Yes };
void destroyMetadataAndNotify(unsigned frameBytesCleared, ClearedSource);
bool isSizeAvailable();
void didDecodeProperties() const;
int repetitionCount(bool imageKnownToBeComplete); bool shouldAnimate();
virtual void startAnimation(CatchUpAnimation = CatchUp) override;
void advanceAnimation();
enum AnimationAdvancement { Normal, SkippingFramesToCatchUp };
bool internalAdvanceAnimation(AnimationAdvancement = Normal);
void invalidatePlatformData();
void checkForSolidColor();
virtual bool mayFillWithSolidColor() override;
virtual Color solidColor() const override;
#if !ASSERT_DISABLED
virtual bool notSolidColor() override;
#endif
private:
virtual bool decodedDataIsPurgeable() const override;
void clearTimer();
void startTimer(double delay);
ImageSource m_source;
mutable IntSize m_size; mutable IntSize m_sizeRespectingOrientation;
mutable SubsamplingLevel m_minimumSubsamplingLevel;
mutable unsigned m_imageOrientation : 4; mutable unsigned m_shouldRespectImageOrientation : 1;
size_t m_currentFrame; Vector<FrameData, 1> m_frames;
std::unique_ptr<Timer> m_frameTimer;
int m_repetitionCount; RepetitionCountStatus m_repetitionCountStatus;
int m_repetitionsComplete; double m_desiredFrameStartTime;
#if USE(APPKIT)
mutable RetainPtr<NSImage> m_nsImage; #endif
#if USE(CG)
mutable RetainPtr<CFDataRef> m_tiffRep; #endif
Color m_solidColor;
unsigned m_decodedSize; mutable unsigned m_decodedPropertiesSize; size_t m_frameCount;
#if PLATFORM(IOS)
double m_progressiveLoadChunkTime;
uint16_t m_progressiveLoadChunkCount;
#endif
bool m_allowSubsampling : 1; bool m_isSolidColor : 1; bool m_checkedForSolidColor : 1;
bool m_animationFinished : 1;
bool m_allDataReceived : 1; mutable bool m_haveSize : 1; bool m_sizeAvailable : 1; mutable bool m_hasUniformFrameSize : 1;
mutable bool m_haveFrameCount : 1;
bool m_animationFinishedWhenCatchingUp : 1;
RefPtr<Image> m_cachedImage;
};
}
SPECIALIZE_TYPE_TRAITS_IMAGE(BitmapImage)
#endif // BitmapImage_h