#pragma once
#include "Image.h"
#include "Color.h"
#include "ImageObserver.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 {
class Timer;
class BitmapImage final : public Image {
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 { return true; }
bool dataChanged(bool allDataReceived) override;
unsigned decodedSize() const { return m_source.decodedSize(); }
bool isSizeAvailable() const { return m_source.isSizeAvailable(); }
size_t frameCount() const { return m_source.frameCount(); }
RepetitionCount repetitionCount() const { return m_source.repetitionCount(); }
String filenameExtension() const override { return m_source.filenameExtension(); }
std::optional<IntPoint> hotSpot() const override { return m_source.hotSpot(); }
FloatSize size() const override { return m_source.size(); }
IntSize sizeRespectingOrientation() const { return m_source.sizeRespectingOrientation(); }
Color singlePixelSolidColor() const override { return m_source.singlePixelSolidColor(); }
bool frameIsBeingDecodedAtIndex(size_t index) const { return m_source.frameIsBeingDecodedAtIndex(index); }
bool frameIsCompleteAtIndex(size_t index) const { return m_source.frameIsCompleteAtIndex(index); }
bool frameHasAlphaAtIndex(size_t index) const { return m_source.frameHasAlphaAtIndex(index); }
bool frameHasValidNativeImageAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const { return m_source.frameHasValidNativeImageAtIndex(index, subsamplingLevel); }
SubsamplingLevel frameSubsamplingLevelAtIndex(size_t index) const { return m_source.frameSubsamplingLevelAtIndex(index); }
float frameDurationAtIndex(size_t index) const { return m_source.frameDurationAtIndex(index); }
ImageOrientation frameOrientationAtIndex(size_t index) const { return m_source.frameOrientationAtIndex(index); }
size_t currentFrame() const { return m_currentFrame; }
bool currentFrameKnownToBeOpaque() const override { return !frameHasAlphaAtIndex(currentFrame()); }
ImageOrientation orientationForCurrentFrame() const override { return frameOrientationAtIndex(currentFrame()); }
bool isAsyncDecodingForcedForTesting() const { return m_frameDecodingDurationForTesting > 0; }
void setFrameDecodingDurationForTesting(float duration) { m_frameDecodingDurationForTesting = duration; }
#if USE(APPKIT)
NSImage *nsImage() override;
RetainPtr<NSImage> snapshotNSImage() override;
#endif
#if PLATFORM(COCOA)
CFDataRef tiffRepresentation() 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
WEBCORE_EXPORT NativeImagePtr nativeImage(const GraphicsContext* = nullptr) override;
NativeImagePtr nativeImageForCurrentFrame(const GraphicsContext* = nullptr) override;
#if USE(CG)
NativeImagePtr nativeImageOfSize(const IntSize&, const GraphicsContext* = nullptr) override;
Vector<NativeImagePtr> framesNativeImages() override;
#endif
protected:
WEBCORE_EXPORT BitmapImage(NativeImagePtr&&, ImageObserver* = nullptr);
WEBCORE_EXPORT BitmapImage(ImageObserver* = nullptr);
NativeImagePtr frameImageAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default, const GraphicsContext* = nullptr);
bool allowSubsampling() const { return imageObserver() && imageObserver()->allowSubsampling(); }
bool allowLargeImageAsyncDecoding() const { return imageObserver() && imageObserver()->allowLargeImageAsyncDecoding(); }
bool allowAnimatedImageAsyncDecoding() const { return imageObserver() && imageObserver()->allowAnimatedImageAsyncDecoding(); }
bool showDebugBackground() const { return imageObserver() && imageObserver()->showDebugBackground(); }
void destroyDecodedData(bool destroyAll = true) override;
void destroyDecodedDataIfNecessary(bool destroyAll = true);
void draw(GraphicsContext&, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator, BlendMode, ImageOrientationDescription) override;
void drawPattern(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, BlendMode = BlendModeNormal) override;
#if PLATFORM(WIN)
void drawFrameMatchingSourceSize(GraphicsContext&, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator) override;
#endif
enum class StartAnimationResult { CannotStart, IncompleteData, TimerActive, DecodingActive, Started };
bool isAnimated() const override { return m_source.frameCount() > 1; }
bool shouldAnimate();
bool canAnimate();
void startAnimation() override { internalStartAnimation(); }
StartAnimationResult internalStartAnimation();
void advanceAnimation();
void internalAdvanceAnimation();
void stopAnimation() override;
void resetAnimation() override;
void newFrameNativeImageAvailableAtIndex(size_t) override;
void invalidatePlatformData();
#if !ASSERT_DISABLED
bool notSolidColor() override;
#endif
#if PLATFORM(COCOA)
RetainPtr<CFDataRef> tiffRepresentation(const Vector<NativeImagePtr>&);
#endif
private:
void clearTimer();
void startTimer(double delay);
bool isBitmapImage() const override { return true; }
void dump(TextStream&) const override;
#if !PLATFORM(IOS)
static const unsigned LargeAnimationCutoff = 5242880;
#else
static const unsigned LargeAnimationCutoff = 2097152;
#endif
mutable ImageSource m_source;
size_t m_currentFrame { 0 }; SubsamplingLevel m_currentSubsamplingLevel { SubsamplingLevel::Default };
std::unique_ptr<Timer> m_frameTimer;
RepetitionCount m_repetitionsComplete { RepetitionCountNone }; double m_desiredFrameStartTime { 0 }; bool m_animationFinished { false };
float m_frameDecodingDurationForTesting { 0 };
double m_desiredFrameDecodeTimeForTesting { 0 };
#if !LOG_DISABLED
size_t m_lateFrameCount { 0 };
size_t m_earlyFrameCount { 0 };
size_t m_cachedFrameCount { 0 };
#endif
#if USE(APPKIT)
mutable RetainPtr<NSImage> m_nsImage; #endif
#if USE(CG)
mutable RetainPtr<CFDataRef> m_tiffRep; #endif
RefPtr<Image> m_cachedImage;
};
}
SPECIALIZE_TYPE_TRAITS_IMAGE(BitmapImage)