/*
 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#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 "MemoryCache.h"
#include "PlatformString.h"
#include "Timer.h"
#include <wtf/CurrentTime.h>
#include <wtf/Vector.h>

namespace WebCore {

static HashSet<BitmapImage*> gBitmapImages;

void BitmapImage::destroyAcceleratedSurfaces()
{
	HashSet<RefPtr<BitmapImage>> bitmapImages;

	for (auto* image : gBitmapImages)
		bitmapImages.add(image);

	for(auto& image : bitmapImages)
		image->destroyAllAcceleratedSurfaces();
}

BitmapImage::BitmapImage(ImageObserver* observer, const KURL& url)
	: Image(observer, url)
	, m_source(this)
	, m_currentFrame(0)
	, m_frames(0)
	, m_desiredFrameStartTime(0)
	, m_frameCount(0)
	, 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_animationFinishedWhenCatchingUp(false)
{
}

BitmapImage::~BitmapImage()
{
    invalidatePlatformData();
    stopAnimation();

	gBitmapImages.remove(this);
}

void BitmapImage::clearTimer()
{
    m_frameTimer = nullptr;
}

void BitmapImage::startTimer(double delay)
{
    ASSERT(!m_frameTimer);
    m_frameTimer = adoptPtr(new Timer<BitmapImage>(this, &BitmapImage::advanceAnimation));
    m_frameTimer->startOneShot(delay);
}

bool BitmapImage::isBitmapImage() const
{
    return true;
}

bool BitmapImage::hasSingleSecurityOrigin() const
{
    return true;
}

void BitmapImage::destroyAllAcceleratedSurfaces()
{
	if(!data())
		return;

	if(m_lockCount)
		return;

#if USE(CAIRO)
    for (auto& frame : m_frames)
        frame.clear(false);
#endif
}

void BitmapImage::destroyDecodedData(bool destroyAll)
{
    unsigned frameBytesCleared = 0;
    const size_t clearBeforeFrame = destroyAll ? m_frames.size() : m_currentFrame;

    for (size_t i = 0; i < clearBeforeFrame; ++i) {
    	if(i >= m_frames.size()) // frame is not cached yet.
    		break;

        // The underlying frame isn't actually changing (we're just trying to
        // save the memory for the framebuffer data), so we don't need to clear
        // the metadata.
    	auto& frame = m_frames[i];

    	if (frame.m_didNotifyBytes) {
            frameBytesCleared += frame.m_frameBytes;
            frame.m_didNotifyBytes = false;
    	}

        frame.clear(false);
    }

    destroyMetadataAndNotify(frameBytesCleared);

    m_source.clear(destroyAll, clearBeforeFrame, data(), m_allDataReceived, m_mimeType);
}

void BitmapImage::destroyDecodedDataIfNecessary(bool destroyAll)
{
    // Animated images > HD frame are considered large enough that we'll only hang on
    // to one frame at a time.
    //static const unsigned cLargeAnimationCutoff = 1920 * 1080 * 4;
	unsigned cLargeAnimationCutoff = MemoryCache::singleton().largeAnimationCutoff();

	if(cLargeAnimationCutoff == 0)
		return;

    // If we have decoded frames but there is no encoded data, we shouldn't destroy
    // the decoded image since we won't be able to reconstruct it later.
    if (!data() && m_frames.size())
        return;

    unsigned allFrameBytes = 0;
    for (auto& frame : m_frames)
		allFrameBytes += frame.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 USE(CG)
    if (frameBytesCleared > 0) {
        frameBytesCleared += m_decodedPropertiesSize;
        m_decodedPropertiesSize = 0;
    }
#endif
    if (frameBytesCleared && imageObserver())
        imageObserver()->decodedSizeChanged(this, -safeCast<int>(frameBytesCleared));
}

ImageFrame* BitmapImage::frameBufferAtIndex(size_t index)
{
    if (!ensureFrameIsCached(index, false))
        return 0;
	return m_source.frameBufferAtIndex(index);
}

ImageFrame* BitmapImage::frameBufferForCurrentFrame()
{
	return frameBufferAtIndex(currentFrame());
}

void BitmapImage::cacheFrame(size_t index, bool withNativeSurface)
{
	Locker locker(this);
	
    size_t numFrames = frameCount();
    ASSERT(m_decodedSize == 0 || numFrames > 1);
    
    if (m_frames.size() < numFrames)
        m_frames.grow(numFrames);

    auto& frame = m_frames[index];

    frame.m_frame = m_source.createFrameAtIndex(index, withNativeSurface);
    if (withNativeSurface && numFrames == 1 && frame.m_frame)
        checkForSolidColor();

    frame.m_orientation = m_source.orientationAtIndex(index);
    frame.m_haveMetadata = true;
    frame.m_isComplete = m_source.frameIsCompleteAtIndex(index);
    if (repetitionCount(false) != RepetitionCountNone)
        frame.m_duration = m_source.frameDurationAtIndex(index);
    frame.m_hasAlpha = m_source.frameHasAlphaAtIndex(index);
    frame.m_frameBytes = m_source.frameBytesAtIndex(index);

    const IntSize frameSize(index ? m_source.frameSizeAtIndex(index) : m_size);
    if (frameSize != m_size)
        m_hasUniformFrameSize = false;
    if (frame.m_frame && !frame.m_didNotifyBytes) {
        int deltaBytes = safeCast<int>(frame.m_frameBytes);
        m_decodedSize += deltaBytes;
#if USE(CG)
        // The fully-decoded frame will subsume the partially decoded data used
        // to determine image properties.
        deltaBytes -= m_decodedPropertiesSize;
        m_decodedPropertiesSize = 0;
#endif
        if (auto observer = imageObserver())
            observer->decodedSizeChanged(this, deltaBytes);
        frame.m_didNotifyBytes = true;
    }

#if USE(CAIRO)
    if (frame.m_frame) {
    	if(auto* surface = frame.m_frame.get()){
    		if(cairo_surface_get_type(surface) != CAIRO_SURFACE_TYPE_IMAGE)
        		gBitmapImages.add(this);
    	}
    }
#endif
}

bool BitmapImage::isAccelerated()
{
	bool accelerated = false;
	Locker locker(this);

	if(m_frames.size() > 0)
		accelerated = m_frames[0].m_frame.get();

	return accelerated;
}

void BitmapImage::didDecodeProperties() const
{
    if (m_decodedSize)
        return;

#if USE(CG)
	Locker locker(this);
    
    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 (auto observer = imageObserver())
        observer->decodedSizeChanged(this, deltaBytes);
#endif
}

void BitmapImage::updateSize() const
{
    if (!m_sizeAvailable || m_haveSize)
        return;

	Locker locker(this);
	
    m_size = m_source.size();
    m_sizeRespectingOrientation = m_source.size(RespectImageOrientation);
    m_haveSize = true;    
    didDecodeProperties();
}

DLL_PUBLIC FloatSize BitmapImage::size() const
{
    updateSize();
    return m_size;
}

IntSize BitmapImage::sizeRespectingOrientation() const
{
    updateSize();
    return m_sizeRespectingOrientation;
}

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)
{
    // Clear all partially-decoded frames. For most image formats, there is only
    // one frame, but at least GIF and ICO can have more. With GIFs, the frames
    // come in order and we ask to decode them in order, waiting to request a
    // subsequent frame until the prior one is complete. Given that we clear
    // incomplete frames here, this means there is at most one incomplete frame
    // (even if we use destroyDecodedData() -- since it doesn't reset the
    // metadata), and it is after all the complete frames.
    //
    // With ICOs, on the other hand, we may ask for arbitrary frames at
    // different times (e.g. because we're displaying a higher-resolution image
    // in the content area and using a lower-resolution one for the favicon),
    // and the frames aren't even guaranteed to appear in the file in the same
    // order as in the directory, so an arbitrary number of the frames might be
    // incomplete (if we ask for frames for which we've not yet reached the
    // start of the frame data), and any or none of them might be the particular
    // frame affected by appending new data here. Thus we have to clear all the
    // incomplete frames to be safe.

	Locker locker(this);

    unsigned frameBytesCleared = 0;
    for (auto& frame : m_frames) {
        // NOTE: Don't call frameIsCompleteAtIndex() here, that will try to
        // decode any uncached (i.e. never-decoded or
        // cleared-on-a-previous-pass) frames!
        if (frame.m_haveMetadata && !frame.m_isComplete) {
        	if (frame.m_didNotifyBytes) {
            	frameBytesCleared += frame.m_frameBytes;
        		frame.m_didNotifyBytes = false;
        	}

        	frame.clear(true);
        }
    }
    destroyMetadataAndNotify(frameBytesCleared);
    
    // Feed all the data we've seen so far to the image decoder.
    m_allDataReceived = allDataReceived;
    m_source.setData(data(), allDataReceived, m_mimeType);
    
    m_haveFrameCount = false;
    m_hasUniformFrameSize = true;
    
    return isSizeAvailable();
}

String BitmapImage::filenameExtension() const
{
    return m_source.filenameExtension();
}

size_t BitmapImage::frameCount()
{
	Locker locker(this);
	
    if (!m_haveFrameCount) {
        m_frameCount = m_source.frameCount();
        // If decoder is not initialized yet, m_source.frameCount() returns 0.
        if (m_frameCount) {
            didDecodeProperties();
            m_haveFrameCount = true;
        }
    }
    
    return m_frameCount;
}

bool BitmapImage::isSizeAvailable()
{
    if (m_sizeAvailable)
        return true;

	Locker locker(this);
	
    m_sizeAvailable = m_source.isSizeAvailable();
    didDecodeProperties();
	
    return m_sizeAvailable;
}

bool BitmapImage::ensureFrameIsCached(size_t index, bool withNativeSurface)
{
	Locker locker(this);

    if (index >= frameCount())
        return false;

    if (index >= m_frames.size() || !m_frames[index].m_frame)
        cacheFrame(index, withNativeSurface);

    return true;
}

FrameData* BitmapImage::frameAtIndex(size_t index)
{
    if (!ensureFrameIsCached(index))
        return 0;
    return &m_frames[index];
}

bool BitmapImage::frameIsCompleteAtIndex(size_t index)
{
    if (!ensureFrameIsCached(index))
        return false;
    return m_frames[index].m_isComplete;
}

float BitmapImage::frameDurationAtIndex(size_t index)
{
    if (!ensureFrameIsCached(index))
        return 0;
    return m_frames[index].m_duration;
}

DLL_PUBLIC NativeImagePtr BitmapImage::nativeImageForCurrentFrame()
{
    if(FrameData* frameData = frameAtIndex(currentFrame()))
    	return frameData->m_frame;
    return nullptr;
}

bool BitmapImage::frameHasAlphaAtIndex(size_t index)
{
    if (m_frames.size() <= index)
        return true;

    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 (!ensureFrameIsCached(index))
        return ImageOrientation::OriginTopLeft;

    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


RepetitionCount BitmapImage::repetitionCount(bool imageKnownToBeComplete)
{
    if ((m_repetitionCountStatus == Unknown) || ((m_repetitionCountStatus == Uncertain) && imageKnownToBeComplete)) {
        // Snag the repetition count.  If |imageKnownToBeComplete| is false, the
        // repetition count may not be accurate yet for GIFs; in this case the
        // decoder will default to RepetitionCountOnce, and we'll try and read
        // the count again once the whole image is decoded.
        m_repetitionCount = m_source.repetitionCount();
        didDecodeProperties();
        m_repetitionCountStatus = (imageKnownToBeComplete || m_repetitionCount == RepetitionCountNone) ? Certain : Uncertain;
    }
    return m_repetitionCount;
}

bool BitmapImage::shouldAnimate()
{
    return repetitionCount(false) && !m_animationFinished && imageObserver();
}

void BitmapImage::startAnimation(bool catchUpIfNecessary)
{
    if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
        return;

    // If we aren't already animating, set now as the animation start time.
    const double time = monotonicallyIncreasingTime();
    if (!m_desiredFrameStartTime)
        m_desiredFrameStartTime = time;

    // Don't advance the animation to an incomplete frame.
    size_t nextFrame = (m_currentFrame + 1) % frameCount();
    if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
        return;

    // Don't advance past the last frame if we haven't decoded the whole image
    // yet and our repetition count is potentially unset.  The repetition count
    // in a GIF can potentially come after all the rest of the image data, so
    // wait on it.
    if (!m_allDataReceived && repetitionCount(false) == RepetitionCountOnce && m_currentFrame >= (frameCount() - 1))
        return;

    // Determine time for next frame to start.  By ignoring paint and timer lag
    // in this calculation, we make the animation appear to run at its desired
    // rate regardless of how fast it's being repainted.
    const double currentDuration = frameDurationAtIndex(m_currentFrame);
    m_desiredFrameStartTime += currentDuration;

    // When an animated image is more than five minutes out of date, the
    // user probably doesn't care about resyncing and we could burn a lot of
    // time looping through frames below.  Just reset the timings.
    const double cAnimationResyncCutoff = 5 * 60;
    if ((time - m_desiredFrameStartTime) > cAnimationResyncCutoff)
        m_desiredFrameStartTime = time + currentDuration;

    // The image may load more slowly than it's supposed to animate, so that by
    // the time we reach the end of the first repetition, we're well behind.
    // Clamp the desired frame start time in this case, so that we don't skip
    // frames (or whole iterations) trying to "catch up".  This is a tradeoff:
    // It guarantees users see the whole animation the second time through and
    // don't miss any repetitions, and is closer to what other browsers do; on
    // the other hand, it makes animations "less accurate" for pages that try to
    // sync an image and some other resource (e.g. audio), especially if users
    // switch tabs (and thus stop drawing the animation, which will pause it)
    // during that initial loop, then switch back later.
    if (nextFrame == 0 && m_repetitionsComplete == 0 && m_desiredFrameStartTime < time)
        m_desiredFrameStartTime = time;

    if (!catchUpIfNecessary || time < m_desiredFrameStartTime) {
        // Haven't yet reached time for next frame to start; delay until then.
    	startTimer(std::max<double>(m_desiredFrameStartTime - time, 0));
		return;
	}

	ASSERT(!m_frameTimer);

	// We've already reached or passed the time for the next frame to start.
	// See if we've also passed the time for frames after that to start, in
	// case we need to skip some frames entirely.  Remember not to advance
	// to an incomplete frame.
	for (size_t frameAfterNext = (nextFrame + 1) % frameCount(); frameIsCompleteAtIndex(frameAfterNext); frameAfterNext = (nextFrame + 1) % frameCount()) {
		// Should we skip the next frame?
		double frameAfterNextStartTime = m_desiredFrameStartTime + frameDurationAtIndex(nextFrame);
		if (time < frameAfterNextStartTime)
			break;

        // Yes; skip over it without notifying our observers. If we hit the end while catching up,
        // tell the observer asynchronously.
        if (!internalAdvanceAnimation(SkippingFramesToCatchUp)) {
            m_animationFinishedWhenCatchingUp = true;
            startTimer(0);
            return;
        }
		m_desiredFrameStartTime = frameAfterNextStartTime;
		nextFrame = frameAfterNext;
	}

    // Draw the next frame as soon as possible. Note that m_desiredFrameStartTime
    // may be in the past, meaning the next time through this function we'll
    // kick off the next advancement sooner than this frame's duration would suggest.
    startTimer(0);
}

bool BitmapImage::isAnimating() const
{
    return !!m_frameTimer;
}

void BitmapImage::stopAnimation()
{
    // This timer is used to animate all occurrences of this image.  Don't invalidate
    // the timer unless all renderers have stopped drawing.
    clearTimer();
}

void BitmapImage::resetAnimation()
{
    stopAnimation();
    m_currentFrame = 0;
    m_repetitionsComplete = 0;
    m_desiredFrameStartTime = 0;
    m_animationFinished = false;
    
    // For extremely large animations, when the animation is reset, we just throw everything away.
    destroyDecodedDataIfNecessary(true);
}

unsigned BitmapImage::decodedSize() const
{
    return m_decodedSize;
}

void BitmapImage::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const AffineTransform& transform,
    const FloatPoint& phase, const FloatSize& spacing, CompositeOperator op, const FloatRect& destRect, BlendMode blendMode)
{
	Locker locker(this);

    if (tileRect.isEmpty())
        return;

    if (!ctxt->drawLuminanceMask()) {
        Image::drawPattern(ctxt, tileRect, transform, phase, spacing, op, destRect, blendMode);
        return;
    }
    if (!m_cachedImage) {
        OwnPtr<ImageBuffer> buffer = ImageBuffer::create(expandedIntSize(tileRect.size()), 1, ColorSpaceSRGB, RenderingMode::Unaccelerated, LogGLMemoryError::No);
        if (!buffer)
        	return;

        auto observer = imageObserver();

        // Temporarily reset image observer, we don't want to receive any changeInRect() calls due to this relayout.
        setImageObserver(nullptr);

        draw(buffer->context(), tileRect, tileRect, op, blendMode);

        setImageObserver(observer.release());
        buffer->convertToLuminanceMask();

        m_cachedImage = buffer->copyImage(DontCopyBackingStore/*, Unscaled*/);
        if (!m_cachedImage)
			return;
    }

    ctxt->setDrawLuminanceMask(false);
    m_cachedImage->drawPattern(ctxt, tileRect, transform, phase, spacing, op, destRect, blendMode);
}

void BitmapImage::advanceAnimation()
{
    internalAdvanceAnimation();
    // At this point the image region has been marked dirty, and if it's
    // onscreen, we'll soon make a call to draw(), which will call
    // startAnimation() again to keep the animation moving.
}

bool BitmapImage::internalAdvanceAnimation(AnimationAdvancement advancement)
{
    clearTimer();

    if (m_animationFinishedWhenCatchingUp) {
    	if (auto observer = imageObserver())
			observer->animationAdvanced(this);
        m_animationFinishedWhenCatchingUp = false;
        return false;
    }
    
    ++m_currentFrame;
    bool advancedAnimation = true;
    bool destroyAll = false;
    if (m_currentFrame >= frameCount()) {
        ++m_repetitionsComplete;

        // Get the repetition count again.  If we weren't able to get a
        // repetition count before, we should have decoded the whole image by
        // now, so it should now be available.
        if (repetitionCount(true) != RepetitionCountInfinite && m_repetitionsComplete > m_repetitionCount) {
            m_animationFinished = true;
            m_desiredFrameStartTime = 0;
            --m_currentFrame;
            advancedAnimation = false;
        } else {
            m_currentFrame = 0;
            destroyAll = true;
        }
    }
    destroyDecodedDataIfNecessary(destroyAll);

    // We need to draw this frame if we advanced to it while not skipping, or if
    // while trying to skip frames we hit the last frame and thus had to stop.
    if (advancement == Normal && advancedAnimation) {
    	if (auto observer = imageObserver())
			observer->animationAdvanced(this);
    }

    return advancedAnimation;
}

bool BitmapImage::mayFillWithSolidColor()
{
    if (!m_checkedForSolidColor && frameCount() > 0) {
        checkForSolidColor();
        // WINCE PORT: checkForSolidColor() doesn't set m_checkedForSolidColor until
        // it gets enough information to make final decision.
#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;
}

void BitmapImage::handleOutOfMemory()
{
	if(m_lockCount)
		return;

	destroyDecodedData(true);
}

/*
DestinationColorSpace BitmapImage::colorSpace()
{
    if (auto nativeImage = this->nativeImage())
        return nativeImage->colorSpace();
    return DestinationColorSpace::SRGB();
}
*/

}
