#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;
namespace NativeImage {
IntSize size(const NativeImagePtr&);
bool hasAlpha(const NativeImagePtr&);
Color singlePixelSolidColor(const NativeImagePtr&);
}
struct FrameData {
public:
FrameData()
: m_haveMetadata(false)
, m_isComplete(false)
, m_hasAlpha(true)
{
}
~FrameData()
{
clear(true);
}
bool clear(bool clearMetadata);
unsigned usedFrameBytes() const { return m_image ? m_frameBytes : 0; }
NativeImagePtr m_image;
ImageOrientation m_orientation { DefaultImageOrientation };
SubsamplingLevel m_subsamplingLevel { 0 };
float m_duration { 0 };
bool m_haveMetadata : 1;
bool m_isComplete : 1;
bool m_hasAlpha : 1;
unsigned m_frameBytes { 0 };
};
class BitmapImage final : public Image {
friend class GeneratedImage;
friend class CrossfadeGeneratedImage;
friend class GradientImage;
friend class GraphicsContext;
public:
static Ref<BitmapImage> create(NativeImagePtr&& nativeImage, ImageObserver* observer = nullptr)
{
return adoptRef(*new BitmapImage(WTFMove(nativeImage), observer));
}
static Ref<BitmapImage> create(ImageObserver* observer = nullptr)
{
return adoptRef(*new BitmapImage(observer));
}
#if PLATFORM(WIN)
WEBCORE_EXPORT static RefPtr<BitmapImage> create(HBITMAP);
#endif
virtual ~BitmapImage();
bool hasSingleSecurityOrigin() const override;
FloatSize size() const override;
IntSize sizeRespectingOrientation() const;
Optional<IntPoint> hotSpot() const override;
unsigned decodedSize() const { return m_decodedSize; }
bool dataChanged(bool allDataReceived) override;
String filenameExtension() const override;
void stopAnimation() override;
void resetAnimation() override;
void drawPattern(GraphicsContext&, const FloatRect& srcRect, const AffineTransform& patternTransform,
const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal) override;
#if USE(APPKIT)
NSImage *nsImage() override;
RetainPtr<NSImage> snapshotNSImage() override;
#endif
#if PLATFORM(COCOA)
CFDataRef tiffRepresentation() override;
#endif
#if USE(CG)
WEBCORE_EXPORT CGImageRef getCGImageRef() override;
CGImageRef getFirstCGImageRefOfSize(const IntSize&) override;
RetainPtr<CFArrayRef> getCGImageArray() override;
#endif
#if PLATFORM(WIN)
bool getHBITMAP(HBITMAP) override;
bool getHBITMAPOfSize(HBITMAP, const IntSize*) override;
#endif
#if PLATFORM(GTK)
GdkPixbuf* getGdkPixbuf() override;
#endif
#if PLATFORM(EFL)
Evas_Object* getEvasObject(Evas*) override;
#endif
NativeImagePtr nativeImageForCurrentFrame() override;
ImageOrientation orientationForCurrentFrame() override { return frameOrientationAtIndex(currentFrame()); }
#if USE(CG)
Vector<NativeImagePtr> framesNativeImages() override;
#endif
bool currentFrameKnownToBeOpaque() override;
bool isAnimated() const override { return m_frameCount > 1; }
bool canAnimate();
void setAllowSubsampling(bool allowSubsampling) { m_source.setAllowSubsampling(allowSubsampling); }
size_t currentFrame() const { return m_currentFrame; }
private:
bool isBitmapImage() const override { return true; }
void updateSize() const;
protected:
enum RepetitionCountStatus {
Unknown, Uncertain, Certain };
WEBCORE_EXPORT BitmapImage(NativeImagePtr&&, ImageObserver* = nullptr);
WEBCORE_EXPORT BitmapImage(ImageObserver* = nullptr);
#if PLATFORM(WIN)
void drawFrameMatchingSourceSize(GraphicsContext&, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator) override;
#endif
void draw(GraphicsContext&, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, BlendMode, ImageOrientationDescription) override;
#if USE(WINGDI)
virtual void drawPattern(GraphicsContext&, const FloatRect& srcRect, const AffineTransform& patternTransform,
const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, const FloatRect& destRect);
#endif
size_t frameCount();
NativeImagePtr frameImageAtIndex(size_t, float presentationScaleHint = 1);
NativeImagePtr copyUnscaledFrameImageAtIndex(size_t);
bool haveFrameImageAtIndex(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);
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();
void startAnimation(CatchUpAnimation = CatchUp) override;
void advanceAnimation();
enum AnimationAdvancement { Normal, SkippingFramesToCatchUp };
bool internalAdvanceAnimation(AnimationAdvancement = Normal);
void invalidatePlatformData();
Color singlePixelSolidColor() override;
#if !ASSERT_DISABLED
bool notSolidColor() override;
#endif
#if PLATFORM(COCOA)
RetainPtr<CFDataRef> tiffRepresentation(const Vector<NativeImagePtr>&);
#endif
private:
void clearTimer();
void startTimer(double delay);
void dump(TextStream&) const override;
ImageSource m_source;
mutable IntSize m_size; mutable IntSize m_sizeRespectingOrientation;
size_t m_currentFrame { 0 }; Vector<FrameData, 1> m_frames;
std::unique_ptr<Timer> m_frameTimer;
int m_repetitionCount { cAnimationNone }; RepetitionCountStatus m_repetitionCountStatus { Unknown };
int m_repetitionsComplete { 0 }; double m_desiredFrameStartTime { 0 };
#if USE(APPKIT)
mutable RetainPtr<NSImage> m_nsImage; #endif
#if USE(CG)
mutable RetainPtr<CFDataRef> m_tiffRep; #endif
Optional<Color> m_solidColor;
unsigned m_decodedSize { 0 }; mutable unsigned m_decodedPropertiesSize { 0 }; size_t m_frameCount;
#if PLATFORM(IOS)
double m_progressiveLoadChunkTime { 0 };
uint16_t m_progressiveLoadChunkCount { 0 };
#endif
bool m_animationFinished : 1;
bool m_allDataReceived : 1; mutable bool m_haveSize : 1; bool m_sizeAvailable : 1; mutable bool m_haveFrameCount : 1;
bool m_animationFinishedWhenCatchingUp : 1;
RefPtr<Image> m_cachedImage;
};
}
SPECIALIZE_TYPE_TRAITS_IMAGE(BitmapImage)
#endif // BitmapImage_h