#include "config.h"
#include "BitmapImage.h"
#include "FloatRect.h"
#include "ImageObserver.h"
#include "IntRect.h"
#include "PlatformString.h"
#include "Timer.h"
#include <wtf/Vector.h>
#include "MIMETypeRegistry.h"
#include "SystemTime.h"
namespace WebCore {
const unsigned cLargeAnimationCutoff = 5242880;
BitmapImage::BitmapImage(ImageObserver* observer)
: Image(observer)
, m_imageAnimationDisabled(false)
, m_progressiveLoadChunkTime(0)
, m_progressiveLoadChunkCount(0)
, m_currentFrame(0)
, m_frames(0)
, m_frameTimer(0)
, m_repetitionCount(0)
, m_repetitionsComplete(0)
, m_isSolidColor(false)
, m_animatingImageType(true)
, m_animationFinished(false)
, m_allDataReceived(false)
, m_haveSize(false)
, m_sizeAvailable(false)
, m_decodedSize(0)
{
initPlatformData();
}
BitmapImage::~BitmapImage()
{
invalidatePlatformData();
stopAnimation();
}
void BitmapImage::destroyDecodedData(bool incremental)
{
if (m_frames.size()) {
int sizeChange = 0;
for (unsigned i = incremental ? m_frames.size() - 1 : 0; i < m_frames.size(); i++) {
if (m_frames[i].m_frame) {
sizeChange -= m_frames[i].m_bytes;
m_frames[i].clear();
}
}
m_isSolidColor = false;
invalidatePlatformData();
if (sizeChange) {
m_decodedSize += sizeChange;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, sizeChange);
}
if (!incremental) {
m_source.clear();
m_source.setData(m_data.get(), m_allDataReceived);
}
}
}
void BitmapImage::cacheFrame(size_t index, float scaleHint)
{
size_t numFrames = frameCount();
ASSERT(m_decodedSize == 0 || numFrames > 1);
if (!m_frames.size() && shouldAnimate()) {
m_repetitionCount = m_source.repetitionCount();
if (m_repetitionCount == cAnimationNone)
m_animatingImageType = false;
}
if (m_frames.size() < numFrames)
m_frames.grow(numFrames);
if (m_sizeAvailable && imageObserver() && !imageObserver()->shouldDecodeFrame(this, m_size))
return;
m_frames[index].m_frame = m_source.createFrameAtIndex(index, scaleHint, &m_frames[index].m_scale, &m_frames[index].m_bytes);
if (numFrames == 1 && m_frames[index].m_frame)
checkForSolidColor();
if (!m_frames[index].m_haveInfo)
cacheFrameInfo(index);
int sizeChange = m_frames[index].m_bytes;
if (sizeChange) {
m_decodedSize += sizeChange;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, sizeChange);
}
}
void BitmapImage::cacheFrameInfo(size_t index)
{
size_t numFrames = frameCount();
ASSERT(!m_frames[index].m_haveInfo);
if (m_frames.size() < numFrames)
m_frames.resize(numFrames);
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;
}
IntSize BitmapImage::size() const
{
if (m_sizeAvailable && !m_haveSize) {
m_size = m_source.size();
m_haveSize = true;
}
return m_size;
}
bool BitmapImage::dataChanged(bool allDataReceived)
{
destroyDecodedData(true);
m_allDataReceived = allDataReceived;
static const double chunkLoadIntervals[] = {0.0, 1.0, 3.0, 6.0, 15.0};
double interval = chunkLoadIntervals[std::min(m_progressiveLoadChunkCount, 4u)];
bool needsUpdate = false;
if (currentTime() - m_progressiveLoadChunkTime > interval) { needsUpdate = true;
m_progressiveLoadChunkTime = currentTime();
m_progressiveLoadChunkCount++;
}
if (needsUpdate || allDataReceived)
m_source.setData(m_data.get(), allDataReceived);
return isSizeAvailable();
}
size_t BitmapImage::frameCount()
{
return m_source.frameCount();
}
bool BitmapImage::isSizeAvailable()
{
if (m_sizeAvailable)
return true;
m_sizeAvailable = m_source.isSizeAvailable();
return m_sizeAvailable;
}
NativeImagePtr BitmapImage::frameAtIndex(size_t index)
{
return frameAtIndex(index, 1.0f);
}
NativeImagePtr BitmapImage::frameAtIndex(size_t index, float scaleHint)
{
if (index >= frameCount())
return 0;
if (index >= m_frames.size() || !m_frames[index].m_frame)
cacheFrame(index, scaleHint);
else if (std::min(1.0f, scaleHint) > m_frames[index].m_scale) {
int sizeChange = -m_frames[index].m_bytes;
m_frames[index].clear();
invalidatePlatformData();
m_decodedSize += sizeChange;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, sizeChange);
cacheFrame(index, scaleHint);
}
return m_frames[index].m_frame;
}
float BitmapImage::frameDurationAtIndex(size_t index)
{
if (index >= frameCount())
return 0;
if (index >= m_frames.size() || !m_frames[index].m_haveInfo)
cacheFrameInfo(index);
return m_frames[index].m_duration;
}
bool BitmapImage::frameHasAlphaAtIndex(size_t index)
{
if (index >= frameCount())
return 0;
if (index >= m_frames.size() || !m_frames[index].m_haveInfo)
cacheFrameInfo(index);
return m_frames[index].m_hasAlpha;
}
bool BitmapImage::shouldAnimate()
{
return (m_animatingImageType && !m_animationFinished && imageObserver() && (width() * height() * 4 * frameCount() < 2097152) && !m_imageAnimationDisabled);
}
void BitmapImage::startAnimation()
{
if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
return;
if (!m_source.frameIsCompleteAtIndex(m_currentFrame))
return;
m_frameTimer = new Timer<BitmapImage>(this, &BitmapImage::advanceAnimation);
m_frameTimer->startOneShot(frameDurationAtIndex(m_currentFrame));
}
void BitmapImage::stopAnimation()
{
delete m_frameTimer;
m_frameTimer = 0;
}
void BitmapImage::resetAnimation()
{
stopAnimation();
m_currentFrame = 0;
m_repetitionsComplete = 0;
m_animationFinished = false;
int frameSize = m_size.width() * m_size.height() * 4;
if (frameCount() * frameSize > cLargeAnimationCutoff)
destroyDecodedData();
}
void BitmapImage::advanceAnimation(Timer<BitmapImage>* timer)
{
stopAnimation();
if (imageObserver()->shouldPauseAnimation(this))
return;
m_currentFrame++;
if (m_currentFrame >= frameCount()) {
m_repetitionsComplete += 1;
if (m_repetitionCount && m_repetitionsComplete >= m_repetitionCount) {
m_animationFinished = true;
m_currentFrame--;
return;
}
m_currentFrame = 0;
}
imageObserver()->animationAdvanced(this);
int frameSize = m_size.width() * m_size.height() * 4;
if (frameCount() * frameSize > cLargeAnimationCutoff) {
destroyDecodedData();
frameAtIndex(m_currentFrame);
}
}
unsigned BitmapImage::animatedImageSize()
{
if (frameCount() <= 1)
return 0;
return (width() * height() * 4 * frameCount());
}
void BitmapImage::disableImageAnimation()
{
m_imageAnimationDisabled = true;
}
}