#include "config.h"
#include "BitmapImage.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "ImageObserver.h"
#include "IntRect.h"
#include "MIMETypeRegistry.h"
#include "Timer.h"
#include <wtf/CurrentTime.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
#if PLATFORM(IOS)
#include <limits>
#endif
namespace WebCore {
BitmapImage::BitmapImage(ImageObserver* observer)
: Image(observer)
, m_currentFrame(0)
, m_frames(0)
, m_repetitionCount(cAnimationNone)
, m_repetitionCountStatus(Unknown)
, m_repetitionsComplete(0)
, m_desiredFrameStartTime(0)
, m_decodedSize(0)
, m_decodedPropertiesSize(0)
, m_frameCount(0)
#if PLATFORM(IOS)
, m_progressiveLoadChunkTime(0)
, m_progressiveLoadChunkCount(0)
#endif
, m_isSolidColor(false)
, m_checkedForSolidColor(false)
, m_animationFinished(false)
, m_allDataReceived(false)
, m_haveSize(false)
, m_sizeAvailable(false)
, m_hasUniformFrameSize(true)
, m_haveFrameCount(false)
, m_cachedImage(0)
{
}
BitmapImage::~BitmapImage()
{
invalidatePlatformData();
stopAnimation();
}
bool BitmapImage::hasSingleSecurityOrigin() const
{
return true;
}
void BitmapImage::destroyDecodedData(bool destroyAll)
{
unsigned frameBytesCleared = 0;
const size_t clearBeforeFrame = destroyAll ? m_frames.size() : m_currentFrame;
for (size_t i = 0; i < std::min(clearBeforeFrame, m_frames.size()); ++i) {
unsigned frameBytes = m_frames[i].m_frameBytes;
if (m_frames[i].clear(false))
frameBytesCleared += frameBytes;
}
destroyMetadataAndNotify(frameBytesCleared);
m_source.clear(destroyAll, clearBeforeFrame, data(), m_allDataReceived);
return;
}
void BitmapImage::destroyDecodedDataIfNecessary(bool destroyAll)
{
#if PLATFORM(IOS)
static const unsigned cLargeAnimationCutoff = 2097152;
if (!data() && m_frames.size())
return;
#else
static const unsigned cLargeAnimationCutoff = 5242880;
#endif
if (!data() && m_frames.size())
return;
unsigned allFrameBytes = 0;
for (size_t i = 0; i < m_frames.size(); ++i)
allFrameBytes += m_frames[i].m_frameBytes;
if (allFrameBytes > cLargeAnimationCutoff)
destroyDecodedData(destroyAll);
}
void BitmapImage::destroyMetadataAndNotify(unsigned frameBytesCleared)
{
m_isSolidColor = false;
m_checkedForSolidColor = false;
invalidatePlatformData();
ASSERT(m_decodedSize >= frameBytesCleared);
m_decodedSize -= frameBytesCleared;
if (frameBytesCleared > 0) {
frameBytesCleared += m_decodedPropertiesSize;
m_decodedPropertiesSize = 0;
}
if (frameBytesCleared && imageObserver())
imageObserver()->decodedSizeChanged(this, -safeCast<int>(frameBytesCleared));
}
#if PLATFORM(IOS)
void BitmapImage::cacheFrame(size_t index, float scaleHint)
#else
void BitmapImage::cacheFrame(size_t index)
#endif
{
size_t numFrames = frameCount();
ASSERT(m_decodedSize == 0 || numFrames > 1);
if (m_frames.size() < numFrames)
m_frames.grow(numFrames);
#if PLATFORM(IOS)
m_frames[index].m_frame = m_source.createFrameAtIndex(index, &scaleHint);
m_frames[index].m_subsamplingScale = scaleHint;
#else
m_frames[index].m_frame = m_source.createFrameAtIndex(index);
#endif
if (numFrames == 1 && m_frames[index].m_frame)
checkForSolidColor();
m_frames[index].m_orientation = m_source.orientationAtIndex(index);
m_frames[index].m_haveMetadata = true;
m_frames[index].m_isComplete = m_source.frameIsCompleteAtIndex(index);
if (repetitionCount(false) != cAnimationNone)
m_frames[index].m_duration = m_source.frameDurationAtIndex(index);
m_frames[index].m_hasAlpha = m_source.frameHasAlphaAtIndex(index);
m_frames[index].m_frameBytes = m_source.frameBytesAtIndex(index);
const IntSize frameSize(index ? m_source.frameSizeAtIndex(index) : m_size);
if (frameSize != m_size)
m_hasUniformFrameSize = false;
if (m_frames[index].m_frame) {
int deltaBytes = safeCast<int>(m_frames[index].m_frameBytes);
m_decodedSize += deltaBytes;
deltaBytes -= m_decodedPropertiesSize;
m_decodedPropertiesSize = 0;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, deltaBytes);
}
}
#if PLATFORM(IOS)
void BitmapImage::cacheFrameInfo(size_t index)
{
size_t numFrames = frameCount();
if (m_frames.size() < numFrames)
m_frames.resize(numFrames);
ASSERT(!m_frames[index].m_haveInfo);
if (shouldAnimate())
m_frames[index].m_duration = m_source.frameDurationAtIndex(index);
m_frames[index].m_hasAlpha = m_source.frameHasAlphaAtIndex(index);
m_frames[index].m_haveInfo = true;
}
#endif
void BitmapImage::didDecodeProperties() const
{
if (m_decodedSize)
return;
size_t updatedSize = m_source.bytesDecodedToDetermineProperties();
if (m_decodedPropertiesSize == updatedSize)
return;
int deltaBytes = updatedSize - m_decodedPropertiesSize;
#if !ASSERT_DISABLED
bool overflow = updatedSize > m_decodedPropertiesSize && deltaBytes < 0;
bool underflow = updatedSize < m_decodedPropertiesSize && deltaBytes > 0;
ASSERT(!overflow && !underflow);
#endif
m_decodedPropertiesSize = updatedSize;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, deltaBytes);
}
void BitmapImage::updateSize(ImageOrientationDescription description) const
{
if (!m_sizeAvailable || m_haveSize)
return;
m_size = m_source.size(description);
m_sizeRespectingOrientation = m_source.size(ImageOrientationDescription(RespectImageOrientation, description.imageOrientation()));
m_imageOrientation = static_cast<unsigned>(description.imageOrientation());
m_shouldRespectImageOrientation = static_cast<unsigned>(description.respectImageOrientation());
#if PLATFORM(IOS)
m_originalSize = m_source.originalSize();
m_originalSizeRespectingOrientation = m_source.originalSize(RespectImageOrientation);
#endif
m_haveSize = true;
didDecodeProperties();
}
FloatSize BitmapImage::size() const
{
updateSize();
return m_size;
}
IntSize BitmapImage::sizeRespectingOrientation(ImageOrientationDescription description) const
{
updateSize(description);
return m_sizeRespectingOrientation;
}
#if PLATFORM(IOS)
FloatSize BitmapImage::originalSize() const
{
updateSize();
return m_originalSize;
}
IntSize BitmapImage::originalSizeRespectingOrientation() const
{
updateSize();
return m_originalSizeRespectingOrientation;
}
#endif
IntSize BitmapImage::currentFrameSize() const
{
if (!m_currentFrame || m_hasUniformFrameSize)
return IntSize(size());
IntSize frameSize = m_source.frameSizeAtIndex(m_currentFrame);
didDecodeProperties();
return frameSize;
}
bool BitmapImage::getHotSpot(IntPoint& hotSpot) const
{
bool result = m_source.getHotSpot(hotSpot);
didDecodeProperties();
return result;
}
bool BitmapImage::dataChanged(bool allDataReceived)
{
#if !PLATFORM(IOS)
unsigned frameBytesCleared = 0;
for (size_t i = 0; i < m_frames.size(); ++i) {
unsigned frameBytes = m_frames[i].m_frameBytes;
if (m_frames[i].m_haveMetadata && !m_frames[i].m_isComplete)
frameBytesCleared += (m_frames[i].clear(true) ? frameBytes : 0);
}
destroyMetadataAndNotify(frameBytesCleared);
#else
int deltaBytes = 0;
if (!m_frames.isEmpty()) {
int bytes = m_frames[m_frames.size() - 1].m_frameBytes;
if (m_frames[m_frames.size() - 1].clear(true)) {
deltaBytes += bytes;
deltaBytes += m_decodedPropertiesSize;
m_decodedPropertiesSize = 0;
}
}
destroyMetadataAndNotify(deltaBytes);
#endif
m_allDataReceived = allDataReceived;
#if PLATFORM(IOS)
static const double chunkLoadIntervals[] = {0, 1, 3, 6, 15};
double interval = chunkLoadIntervals[std::min(m_progressiveLoadChunkCount, static_cast<uint16_t>(4))];
bool needsUpdate = false;
if (currentTime() - m_progressiveLoadChunkTime > interval) { needsUpdate = true;
m_progressiveLoadChunkTime = currentTime();
ASSERT(m_progressiveLoadChunkCount <= std::numeric_limits<uint16_t>::max());
++m_progressiveLoadChunkCount;
}
if (needsUpdate || allDataReceived)
m_source.setData(data(), allDataReceived);
#else
m_source.setData(data(), allDataReceived);
#endif
m_haveFrameCount = false;
m_hasUniformFrameSize = true;
return isSizeAvailable();
}
String BitmapImage::filenameExtension() const
{
return m_source.filenameExtension();
}
size_t BitmapImage::frameCount()
{
if (!m_haveFrameCount) {
m_frameCount = m_source.frameCount();
if (m_frameCount) {
didDecodeProperties();
m_haveFrameCount = true;
}
}
return m_frameCount;
}
bool BitmapImage::isSizeAvailable()
{
if (m_sizeAvailable)
return true;
m_sizeAvailable = m_source.isSizeAvailable();
didDecodeProperties();
return m_sizeAvailable;
}
#if !PLATFORM(IOS)
bool BitmapImage::ensureFrameIsCached(size_t index)
{
if (index >= frameCount())
return false;
if (index >= m_frames.size() || !m_frames[index].m_frame)
cacheFrame(index);
return true;
}
#else
bool BitmapImage::ensureFrameInfoIsCached(size_t index)
{
if (index >= frameCount())
return false;
if (index >= m_frames.size() || !m_frames[index].m_haveInfo)
cacheFrameInfo(index);
return true;
}
#endif
PassNativeImagePtr BitmapImage::frameAtIndex(size_t index)
{
#if PLATFORM(IOS)
return frameAtIndex(index, 1.0f);
#else
if (!ensureFrameIsCached(index))
return nullptr;
return m_frames[index].m_frame;
#endif
}
#if PLATFORM(IOS)
PassNativeImagePtr BitmapImage::frameAtIndex(size_t index, float scaleHint)
{
if (index >= frameCount())
return nullptr;
if (index >= m_frames.size() || !m_frames[index].m_frame)
cacheFrame(index, scaleHint);
else if (std::min(1.0f, scaleHint) > m_frames[index].m_subsamplingScale) {
int sizeChange = -m_frames[index].m_frameBytes;
ASSERT(static_cast<int>(m_decodedSize) + sizeChange >= 0);
m_frames[index].clear(true);
invalidatePlatformData();
m_decodedSize += sizeChange;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, sizeChange);
cacheFrame(index, scaleHint);
}
return m_frames[index].m_frame;
}
#endif
bool BitmapImage::frameIsCompleteAtIndex(size_t index)
{
#if PLATFORM(IOS)
if (!ensureFrameInfoIsCached(index))
return false;
#else
if (!ensureFrameIsCached(index))
return false;
#endif
return m_frames[index].m_isComplete;
}
float BitmapImage::frameDurationAtIndex(size_t index)
{
#if PLATFORM(IOS)
if (!ensureFrameInfoIsCached(index))
return 0;
#else
if (!ensureFrameIsCached(index))
return 0;
#endif
return m_frames[index].m_duration;
}
PassNativeImagePtr BitmapImage::nativeImageForCurrentFrame()
{
return frameAtIndex(currentFrame());
}
bool BitmapImage::frameHasAlphaAtIndex(size_t index)
{
#if PLATFORM(IOS)
if (!ensureFrameInfoIsCached(index))
return true; #else
if (m_frames.size() <= index)
return true;
#endif
if (m_frames[index].m_haveMetadata)
return m_frames[index].m_hasAlpha;
return m_source.frameHasAlphaAtIndex(index);
}
bool BitmapImage::currentFrameKnownToBeOpaque()
{
return !frameHasAlphaAtIndex(currentFrame());
}
ImageOrientation BitmapImage::frameOrientationAtIndex(size_t index)
{
#if PLATFORM(IOS)
if (!ensureFrameInfoIsCached(index))
return DefaultImageOrientation;
#else
if (!ensureFrameIsCached(index))
return DefaultImageOrientation;
#endif
if (m_frames[index].m_haveMetadata)
return m_frames[index].m_orientation;
return m_source.orientationAtIndex(index);
}
#if !ASSERT_DISABLED
bool BitmapImage::notSolidColor()
{
return size().width() != 1 || size().height() != 1 || frameCount() > 1;
}
#endif
int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
{
if ((m_repetitionCountStatus == Unknown) || ((m_repetitionCountStatus == Uncertain) && imageKnownToBeComplete)) {
m_repetitionCount = m_source.repetitionCount();
didDecodeProperties();
m_repetitionCountStatus = (imageKnownToBeComplete || m_repetitionCount == cAnimationNone) ? Certain : Uncertain;
}
return m_repetitionCount;
}
bool BitmapImage::shouldAnimate()
{
return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
}
void BitmapImage::startAnimation(CatchUpAnimation catchUpIfNecessary)
{
if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
return;
const double time = monotonicallyIncreasingTime();
if (!m_desiredFrameStartTime)
m_desiredFrameStartTime = time;
size_t nextFrame = (m_currentFrame + 1) % frameCount();
if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
return;
if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
return;
const double currentDuration = frameDurationAtIndex(m_currentFrame);
m_desiredFrameStartTime += currentDuration;
#if !PLATFORM(IOS)
const double cAnimationResyncCutoff = 5 * 60;
if ((time - m_desiredFrameStartTime) > cAnimationResyncCutoff)
m_desiredFrameStartTime = time + currentDuration;
#else
m_desiredFrameStartTime = time + currentDuration;
#endif
if (nextFrame == 0 && m_repetitionsComplete == 0 && m_desiredFrameStartTime < time)
m_desiredFrameStartTime = time;
if (catchUpIfNecessary == DoNotCatchUp || time < m_desiredFrameStartTime) {
m_frameTimer = std::make_unique<Timer>(this, &BitmapImage::advanceAnimation);
m_frameTimer->startOneShot(std::max(m_desiredFrameStartTime - time, 0.));
} else {
for (size_t frameAfterNext = (nextFrame + 1) % frameCount(); frameIsCompleteAtIndex(frameAfterNext); frameAfterNext = (nextFrame + 1) % frameCount()) {
double frameAfterNextStartTime = m_desiredFrameStartTime + frameDurationAtIndex(nextFrame);
if (time < frameAfterNextStartTime)
break;
if (!internalAdvanceAnimation(true))
return;
m_desiredFrameStartTime = frameAfterNextStartTime;
nextFrame = frameAfterNext;
}
if (internalAdvanceAnimation(false)) {
startAnimation(DoNotCatchUp);
}
}
}
void BitmapImage::stopAnimation()
{
m_frameTimer = nullptr;
}
void BitmapImage::resetAnimation()
{
stopAnimation();
m_currentFrame = 0;
m_repetitionsComplete = 0;
m_desiredFrameStartTime = 0;
m_animationFinished = false;
destroyDecodedDataIfNecessary(true);
}
void BitmapImage::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& transform,
const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, BlendMode blendMode)
{
if (tileRect.isEmpty())
return;
if (!ctxt->drawLuminanceMask()) {
Image::drawPattern(ctxt, tileRect, transform, phase, styleColorSpace, op, destRect, blendMode);
return;
}
if (!m_cachedImage) {
std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(expandedIntSize(tileRect.size()));
ASSERT(buffer.get());
ImageObserver* observer = imageObserver();
ASSERT(observer);
setImageObserver(0);
draw(buffer->context(), tileRect, tileRect, styleColorSpace, op, blendMode, ImageOrientationDescription());
setImageObserver(observer);
buffer->convertToLuminanceMask();
m_cachedImage = buffer->copyImage(DontCopyBackingStore, Unscaled);
m_cachedImage->setSpaceSize(spaceSize());
setImageObserver(observer);
}
ctxt->setDrawLuminanceMask(false);
m_cachedImage->drawPattern(ctxt, tileRect, transform, phase, styleColorSpace, op, destRect, blendMode);
}
void BitmapImage::advanceAnimation(Timer&)
{
internalAdvanceAnimation(false);
}
bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
{
stopAnimation();
++m_currentFrame;
bool advancedAnimation = true;
bool destroyAll = false;
if (m_currentFrame >= frameCount()) {
++m_repetitionsComplete;
if (repetitionCount(true) != cAnimationLoopInfinite && m_repetitionsComplete > m_repetitionCount) {
m_animationFinished = true;
m_desiredFrameStartTime = 0;
--m_currentFrame;
advancedAnimation = false;
} else {
m_currentFrame = 0;
destroyAll = true;
}
}
destroyDecodedDataIfNecessary(destroyAll);
if (skippingFrames != advancedAnimation)
imageObserver()->animationAdvanced(this);
return advancedAnimation;
}
bool BitmapImage::mayFillWithSolidColor()
{
if (!m_checkedForSolidColor && frameCount() > 0) {
checkForSolidColor();
#if !OS(WINCE)
ASSERT(m_checkedForSolidColor);
#endif
}
return m_isSolidColor && !m_currentFrame;
}
Color BitmapImage::solidColor() const
{
return m_solidColor;
}
bool BitmapImage::canAnimate()
{
return shouldAnimate() && frameCount() > 1;
}
}