Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2006 Samuel Weinig (sam.weinig (at) gmail.com)
      3  * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "core/platform/graphics/BitmapImage.h"
     29 
     30 #include "core/platform/Timer.h"
     31 #include "core/platform/graphics/FloatRect.h"
     32 #include "core/platform/graphics/GraphicsContextStateSaver.h"
     33 #include "core/platform/graphics/ImageObserver.h"
     34 #include "core/platform/graphics/skia/NativeImageSkia.h"
     35 #include "core/platform/graphics/skia/SkiaUtils.h"
     36 #include "wtf/CurrentTime.h"
     37 #include "wtf/PassRefPtr.h"
     38 #include "wtf/Vector.h"
     39 #include "wtf/text/WTFString.h"
     40 
     41 namespace WebCore {
     42 
     43 BitmapImage::BitmapImage(ImageObserver* observer)
     44     : Image(observer)
     45     , m_currentFrame(0)
     46     , m_frames(0)
     47     , m_frameTimer(0)
     48     , m_repetitionCount(cAnimationNone)
     49     , m_repetitionCountStatus(Unknown)
     50     , m_repetitionsComplete(0)
     51     , m_desiredFrameStartTime(0)
     52     , m_decodedSize(0)
     53     , m_decodedPropertiesSize(0)
     54     , m_frameCount(0)
     55     , m_isSolidColor(false)
     56     , m_checkedForSolidColor(false)
     57     , m_animationFinished(false)
     58     , m_allDataReceived(false)
     59     , m_haveSize(false)
     60     , m_sizeAvailable(false)
     61     , m_hasUniformFrameSize(true)
     62     , m_haveFrameCount(false)
     63 {
     64 }
     65 
     66 BitmapImage::BitmapImage(PassRefPtr<NativeImageSkia> nativeImage, ImageObserver* observer)
     67     : Image(observer)
     68     , m_size(nativeImage->bitmap().width(), nativeImage->bitmap().height())
     69     , m_currentFrame(0)
     70     , m_frames(0)
     71     , m_frameTimer(0)
     72     , m_repetitionCount(cAnimationNone)
     73     , m_repetitionCountStatus(Unknown)
     74     , m_repetitionsComplete(0)
     75     , m_decodedSize(nativeImage->decodedSize())
     76     , m_decodedPropertiesSize(0)
     77     , m_frameCount(1)
     78     , m_isSolidColor(false)
     79     , m_checkedForSolidColor(false)
     80     , m_animationFinished(true)
     81     , m_allDataReceived(true)
     82     , m_haveSize(true)
     83     , m_sizeAvailable(true)
     84     , m_haveFrameCount(true)
     85 {
     86     // Since we don't have a decoder, we can't figure out the image orientation.
     87     // Set m_sizeRespectingOrientation to be the same as m_size so it's not 0x0.
     88     m_sizeRespectingOrientation = m_size;
     89 
     90     m_frames.grow(1);
     91     m_frames[0].m_hasAlpha = !nativeImage->bitmap().isOpaque();
     92     m_frames[0].m_frame = nativeImage;
     93     m_frames[0].m_haveMetadata = true;
     94 
     95     checkForSolidColor();
     96 }
     97 
     98 BitmapImage::~BitmapImage()
     99 {
    100     stopAnimation();
    101 }
    102 
    103 bool BitmapImage::isBitmapImage() const
    104 {
    105     return true;
    106 }
    107 
    108 bool BitmapImage::hasSingleSecurityOrigin() const
    109 {
    110     return true;
    111 }
    112 
    113 
    114 void BitmapImage::destroyDecodedData(bool destroyAll)
    115 {
    116     for (size_t i = 0; i < m_frames.size(); ++i) {
    117         // The underlying frame isn't actually changing (we're just trying to
    118         // save the memory for the framebuffer data), so we don't need to clear
    119         // the metadata.
    120         m_frames[i].clear(false);
    121     }
    122 
    123     destroyMetadataAndNotify(m_source.clearCacheExceptFrame(destroyAll ? notFound : m_currentFrame));
    124 }
    125 
    126 void BitmapImage::destroyDecodedDataIfNecessary()
    127 {
    128     // Animated images >5MB are considered large enough that we'll only hang on
    129     // to one frame at a time.
    130     static const size_t cLargeAnimationCutoff = 5242880;
    131     size_t allFrameBytes = 0;
    132     for (size_t i = 0; i < m_frames.size(); ++i)
    133         allFrameBytes += m_frames[i].m_frameBytes;
    134 
    135     if (allFrameBytes > cLargeAnimationCutoff)
    136         destroyDecodedData(false);
    137 }
    138 
    139 void BitmapImage::destroyMetadataAndNotify(size_t frameBytesCleared)
    140 {
    141     m_isSolidColor = false;
    142     m_checkedForSolidColor = false;
    143 
    144     ASSERT(m_decodedSize >= frameBytesCleared);
    145     m_decodedSize -= frameBytesCleared;
    146     if (frameBytesCleared > 0) {
    147         frameBytesCleared += m_decodedPropertiesSize;
    148         m_decodedPropertiesSize = 0;
    149     }
    150     if (frameBytesCleared && imageObserver())
    151         imageObserver()->decodedSizeChanged(this, -safeCast<int>(frameBytesCleared));
    152 }
    153 
    154 void BitmapImage::cacheFrame(size_t index)
    155 {
    156     size_t numFrames = frameCount();
    157     if (m_frames.size() < numFrames)
    158         m_frames.grow(numFrames);
    159 
    160     m_frames[index].m_frame = m_source.createFrameAtIndex(index);
    161     if (numFrames == 1 && m_frames[index].m_frame)
    162         checkForSolidColor();
    163 
    164     m_frames[index].m_orientation = m_source.orientationAtIndex(index);
    165     m_frames[index].m_haveMetadata = true;
    166     m_frames[index].m_isComplete = m_source.frameIsCompleteAtIndex(index);
    167     if (repetitionCount(false) != cAnimationNone)
    168         m_frames[index].m_duration = m_source.frameDurationAtIndex(index);
    169     m_frames[index].m_hasAlpha = m_source.frameHasAlphaAtIndex(index);
    170     m_frames[index].m_frameBytes = m_source.frameBytesAtIndex(index);
    171 
    172     const IntSize frameSize(index ? m_source.frameSizeAtIndex(index) : m_size);
    173     if (frameSize != m_size)
    174         m_hasUniformFrameSize = false;
    175     if (m_frames[index].m_frame) {
    176         int deltaBytes = safeCast<int>(m_frames[index].m_frameBytes);
    177         m_decodedSize += deltaBytes;
    178         // The fully-decoded frame will subsume the partially decoded data used
    179         // to determine image properties.
    180         deltaBytes -= m_decodedPropertiesSize;
    181         m_decodedPropertiesSize = 0;
    182         if (imageObserver())
    183             imageObserver()->decodedSizeChanged(this, deltaBytes);
    184     }
    185 }
    186 
    187 void BitmapImage::didDecodeProperties() const
    188 {
    189     if (m_decodedSize)
    190         return;
    191     size_t updatedSize = m_source.bytesDecodedToDetermineProperties();
    192     if (m_decodedPropertiesSize == updatedSize)
    193         return;
    194     int deltaBytes = updatedSize - m_decodedPropertiesSize;
    195 #if !ASSERT_DISABLED
    196     bool overflow = updatedSize > m_decodedPropertiesSize && deltaBytes < 0;
    197     bool underflow = updatedSize < m_decodedPropertiesSize && deltaBytes > 0;
    198     ASSERT(!overflow && !underflow);
    199 #endif
    200     m_decodedPropertiesSize = updatedSize;
    201     if (imageObserver())
    202         imageObserver()->decodedSizeChanged(this, deltaBytes);
    203 }
    204 
    205 void BitmapImage::updateSize() const
    206 {
    207     if (!m_sizeAvailable || m_haveSize)
    208         return;
    209 
    210     m_size = m_source.size();
    211     m_sizeRespectingOrientation = m_source.size(RespectImageOrientation);
    212     m_haveSize = true;
    213     didDecodeProperties();
    214 }
    215 
    216 IntSize BitmapImage::size() const
    217 {
    218     updateSize();
    219     return m_size;
    220 }
    221 
    222 IntSize BitmapImage::sizeRespectingOrientation() const
    223 {
    224     updateSize();
    225     return m_sizeRespectingOrientation;
    226 }
    227 
    228 IntSize BitmapImage::currentFrameSize() const
    229 {
    230     if (!m_currentFrame || m_hasUniformFrameSize)
    231         return size();
    232     IntSize frameSize = m_source.frameSizeAtIndex(m_currentFrame);
    233     didDecodeProperties();
    234     return frameSize;
    235 }
    236 
    237 bool BitmapImage::getHotSpot(IntPoint& hotSpot) const
    238 {
    239     bool result = m_source.getHotSpot(hotSpot);
    240     didDecodeProperties();
    241     return result;
    242 }
    243 
    244 bool BitmapImage::dataChanged(bool allDataReceived)
    245 {
    246     // Clear all partially-decoded frames. For most image formats, there is only
    247     // one frame, but at least GIF and ICO can have more. With GIFs, the frames
    248     // come in order and we ask to decode them in order, waiting to request a
    249     // subsequent frame until the prior one is complete. Given that we clear
    250     // incomplete frames here, this means there is at most one incomplete frame
    251     // (even if we use destroyDecodedData() -- since it doesn't reset the
    252     // metadata), and it is after all the complete frames.
    253     //
    254     // With ICOs, on the other hand, we may ask for arbitrary frames at
    255     // different times (e.g. because we're displaying a higher-resolution image
    256     // in the content area and using a lower-resolution one for the favicon),
    257     // and the frames aren't even guaranteed to appear in the file in the same
    258     // order as in the directory, so an arbitrary number of the frames might be
    259     // incomplete (if we ask for frames for which we've not yet reached the
    260     // start of the frame data), and any or none of them might be the particular
    261     // frame affected by appending new data here. Thus we have to clear all the
    262     // incomplete frames to be safe.
    263     unsigned frameBytesCleared = 0;
    264     for (size_t i = 0; i < m_frames.size(); ++i) {
    265         // NOTE: Don't call frameIsCompleteAtIndex() here, that will try to
    266         // decode any uncached (i.e. never-decoded or
    267         // cleared-on-a-previous-pass) frames!
    268         unsigned frameBytes = m_frames[i].m_frameBytes;
    269         if (m_frames[i].m_haveMetadata && !m_frames[i].m_isComplete)
    270             frameBytesCleared += (m_frames[i].clear(true) ? frameBytes : 0);
    271     }
    272     destroyMetadataAndNotify(frameBytesCleared);
    273 
    274     // Feed all the data we've seen so far to the image decoder.
    275     m_allDataReceived = allDataReceived;
    276     m_source.setData(data(), allDataReceived);
    277 
    278     m_haveFrameCount = false;
    279     m_hasUniformFrameSize = true;
    280     return isSizeAvailable();
    281 }
    282 
    283 String BitmapImage::filenameExtension() const
    284 {
    285     return m_source.filenameExtension();
    286 }
    287 
    288 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, BlendMode blendMode)
    289 {
    290     draw(ctxt, dstRect, srcRect, compositeOp, blendMode, DoNotRespectImageOrientation);
    291 }
    292 
    293 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, BlendMode blendMode, RespectImageOrientationEnum shouldRespectImageOrientation)
    294 {
    295     // Spin the animation to the correct frame before we try to draw it, so we
    296     // don't draw an old frame and then immediately need to draw a newer one,
    297     // causing flicker and wasting CPU.
    298     startAnimation();
    299 
    300     RefPtr<NativeImageSkia> bm = nativeImageForCurrentFrame();
    301     if (!bm)
    302         return; // It's too early and we don't have an image yet.
    303 
    304     FloatRect normDstRect = adjustForNegativeSize(dstRect);
    305     FloatRect normSrcRect = adjustForNegativeSize(srcRect);
    306     normSrcRect.intersect(FloatRect(0, 0, bm->bitmap().width(), bm->bitmap().height()));
    307 
    308     if (normSrcRect.isEmpty() || normDstRect.isEmpty())
    309         return; // Nothing to draw.
    310 
    311     ImageOrientation orientation = DefaultImageOrientation;
    312     if (shouldRespectImageOrientation == RespectImageOrientation)
    313         orientation = frameOrientationAtIndex(m_currentFrame);
    314 
    315     GraphicsContextStateSaver saveContext(*ctxt, false);
    316     if (orientation != DefaultImageOrientation) {
    317         saveContext.save();
    318 
    319         // ImageOrientation expects the origin to be at (0, 0)
    320         ctxt->translate(normDstRect.x(), normDstRect.y());
    321         normDstRect.setLocation(FloatPoint());
    322 
    323         ctxt->concatCTM(orientation.transformFromDefault(normDstRect.size()));
    324 
    325         if (orientation.usesWidthAsHeight()) {
    326             // The destination rect will have it's width and height already reversed for the orientation of
    327             // the image, as it was needed for page layout, so we need to reverse it back here.
    328             normDstRect = FloatRect(normDstRect.x(), normDstRect.y(), normDstRect.height(), normDstRect.width());
    329         }
    330     }
    331 
    332     bm->draw(ctxt, normSrcRect, normDstRect, WebCoreCompositeToSkiaComposite(compositeOp, blendMode));
    333 
    334     if (ImageObserver* observer = imageObserver())
    335         observer->didDraw(this);
    336 }
    337 
    338 size_t BitmapImage::frameCount()
    339 {
    340     if (!m_haveFrameCount) {
    341         m_frameCount = m_source.frameCount();
    342         // If decoder is not initialized yet, m_source.frameCount() returns 0.
    343         if (m_frameCount) {
    344             didDecodeProperties();
    345             m_haveFrameCount = true;
    346         }
    347     }
    348     return m_frameCount;
    349 }
    350 
    351 bool BitmapImage::isSizeAvailable()
    352 {
    353     if (m_sizeAvailable)
    354         return true;
    355 
    356     m_sizeAvailable = m_source.isSizeAvailable();
    357     didDecodeProperties();
    358 
    359     return m_sizeAvailable;
    360 }
    361 
    362 bool BitmapImage::ensureFrameIsCached(size_t index)
    363 {
    364     if (index >= frameCount())
    365         return false;
    366 
    367     if (index >= m_frames.size() || !m_frames[index].m_frame)
    368         cacheFrame(index);
    369     return true;
    370 }
    371 
    372 PassRefPtr<NativeImageSkia> BitmapImage::frameAtIndex(size_t index)
    373 {
    374     if (!ensureFrameIsCached(index))
    375         return 0;
    376     return m_frames[index].m_frame;
    377 }
    378 
    379 bool BitmapImage::frameIsCompleteAtIndex(size_t index)
    380 {
    381     if (index < m_frames.size() && m_frames[index].m_haveMetadata && m_frames[index].m_isComplete)
    382         return true;
    383     return m_source.frameIsCompleteAtIndex(index);
    384 }
    385 
    386 float BitmapImage::frameDurationAtIndex(size_t index)
    387 {
    388     if (index < m_frames.size() && m_frames[index].m_haveMetadata)
    389         return m_frames[index].m_duration;
    390     return m_source.frameDurationAtIndex(index);
    391 }
    392 
    393 PassRefPtr<NativeImageSkia> BitmapImage::nativeImageForCurrentFrame()
    394 {
    395     return frameAtIndex(currentFrame());
    396 }
    397 
    398 bool BitmapImage::frameHasAlphaAtIndex(size_t index)
    399 {
    400     if (m_frames.size() <= index)
    401         return true;
    402 
    403     if (m_frames[index].m_haveMetadata)
    404         return m_frames[index].m_hasAlpha;
    405 
    406     return m_source.frameHasAlphaAtIndex(index);
    407 }
    408 
    409 bool BitmapImage::currentFrameKnownToBeOpaque()
    410 {
    411     return !frameHasAlphaAtIndex(currentFrame());
    412 }
    413 
    414 ImageOrientation BitmapImage::currentFrameOrientation()
    415 {
    416     return frameOrientationAtIndex(currentFrame());
    417 }
    418 
    419 ImageOrientation BitmapImage::frameOrientationAtIndex(size_t index)
    420 {
    421     if (m_frames.size() <= index)
    422         return DefaultImageOrientation;
    423 
    424     if (m_frames[index].m_haveMetadata)
    425         return m_frames[index].m_orientation;
    426 
    427     return m_source.orientationAtIndex(index);
    428 }
    429 
    430 #if !ASSERT_DISABLED
    431 bool BitmapImage::notSolidColor()
    432 {
    433     return size().width() != 1 || size().height() != 1 || frameCount() > 1;
    434 }
    435 #endif
    436 
    437 
    438 
    439 int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
    440 {
    441     if ((m_repetitionCountStatus == Unknown) || ((m_repetitionCountStatus == Uncertain) && imageKnownToBeComplete)) {
    442         // Snag the repetition count.  If |imageKnownToBeComplete| is false, the
    443         // repetition count may not be accurate yet for GIFs; in this case the
    444         // decoder will default to cAnimationLoopOnce, and we'll try and read
    445         // the count again once the whole image is decoded.
    446         m_repetitionCount = m_source.repetitionCount();
    447         didDecodeProperties();
    448         m_repetitionCountStatus = (imageKnownToBeComplete || m_repetitionCount == cAnimationNone) ? Certain : Uncertain;
    449     }
    450     return m_repetitionCount;
    451 }
    452 
    453 bool BitmapImage::shouldAnimate()
    454 {
    455     return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
    456 }
    457 
    458 void BitmapImage::startAnimation(bool catchUpIfNecessary)
    459 {
    460     if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
    461         return;
    462 
    463     // If we aren't already animating, set now as the animation start time.
    464     const double time = monotonicallyIncreasingTime();
    465     if (!m_desiredFrameStartTime)
    466         m_desiredFrameStartTime = time;
    467 
    468     // Don't advance the animation to an incomplete frame.
    469     size_t nextFrame = (m_currentFrame + 1) % frameCount();
    470     if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
    471         return;
    472 
    473     // Don't advance past the last frame if we haven't decoded the whole image
    474     // yet and our repetition count is potentially unset.  The repetition count
    475     // in a GIF can potentially come after all the rest of the image data, so
    476     // wait on it.
    477     if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
    478         return;
    479 
    480     // Determine time for next frame to start.  By ignoring paint and timer lag
    481     // in this calculation, we make the animation appear to run at its desired
    482     // rate regardless of how fast it's being repainted.
    483     const double currentDuration = frameDurationAtIndex(m_currentFrame);
    484     m_desiredFrameStartTime += currentDuration;
    485 
    486     // When an animated image is more than five minutes out of date, the
    487     // user probably doesn't care about resyncing and we could burn a lot of
    488     // time looping through frames below.  Just reset the timings.
    489     const double cAnimationResyncCutoff = 5 * 60;
    490     if ((time - m_desiredFrameStartTime) > cAnimationResyncCutoff)
    491         m_desiredFrameStartTime = time + currentDuration;
    492 
    493     // The image may load more slowly than it's supposed to animate, so that by
    494     // the time we reach the end of the first repetition, we're well behind.
    495     // Clamp the desired frame start time in this case, so that we don't skip
    496     // frames (or whole iterations) trying to "catch up".  This is a tradeoff:
    497     // It guarantees users see the whole animation the second time through and
    498     // don't miss any repetitions, and is closer to what other browsers do; on
    499     // the other hand, it makes animations "less accurate" for pages that try to
    500     // sync an image and some other resource (e.g. audio), especially if users
    501     // switch tabs (and thus stop drawing the animation, which will pause it)
    502     // during that initial loop, then switch back later.
    503     if (nextFrame == 0 && m_repetitionsComplete == 0 && m_desiredFrameStartTime < time)
    504         m_desiredFrameStartTime = time;
    505 
    506     if (!catchUpIfNecessary || time < m_desiredFrameStartTime) {
    507         // Haven't yet reached time for next frame to start; delay until then.
    508         m_frameTimer = new Timer<BitmapImage>(this, &BitmapImage::advanceAnimation);
    509         m_frameTimer->startOneShot(std::max(m_desiredFrameStartTime - time, 0.));
    510     } else {
    511         // We've already reached or passed the time for the next frame to start.
    512         // See if we've also passed the time for frames after that to start, in
    513         // case we need to skip some frames entirely.  Remember not to advance
    514         // to an incomplete frame.
    515         for (size_t frameAfterNext = (nextFrame + 1) % frameCount(); frameIsCompleteAtIndex(frameAfterNext); frameAfterNext = (nextFrame + 1) % frameCount()) {
    516             // Should we skip the next frame?
    517             double frameAfterNextStartTime = m_desiredFrameStartTime + frameDurationAtIndex(nextFrame);
    518             if (time < frameAfterNextStartTime)
    519                 break;
    520 
    521             // Yes; skip over it without notifying our observers.
    522             if (!internalAdvanceAnimation(true))
    523                 return;
    524             m_desiredFrameStartTime = frameAfterNextStartTime;
    525             nextFrame = frameAfterNext;
    526         }
    527 
    528         // Draw the next frame immediately.  Note that m_desiredFrameStartTime
    529         // may be in the past, meaning the next time through this function we'll
    530         // kick off the next advancement sooner than this frame's duration would
    531         // suggest.
    532         if (internalAdvanceAnimation(false)) {
    533             // The image region has been marked dirty, but once we return to our
    534             // caller, draw() will clear it, and nothing will cause the
    535             // animation to advance again.  We need to start the timer for the
    536             // next frame running, or the animation can hang.  (Compare this
    537             // with when advanceAnimation() is called, and the region is dirtied
    538             // while draw() is not in the callstack, meaning draw() gets called
    539             // to update the region and thus startAnimation() is reached again.)
    540             // NOTE: For large images with slow or heavily-loaded systems,
    541             // throwing away data as we go (see destroyDecodedData()) means we
    542             // can spend so much time re-decoding data above that by the time we
    543             // reach here we're behind again.  If we let startAnimation() run
    544             // the catch-up code again, we can get long delays without painting
    545             // as we race the timer, or even infinite recursion.  In this
    546             // situation the best we can do is to simply change frames as fast
    547             // as possible, so force startAnimation() to set a zero-delay timer
    548             // and bail out if we're not caught up.
    549             startAnimation(false);
    550         }
    551     }
    552 }
    553 
    554 void BitmapImage::stopAnimation()
    555 {
    556     // This timer is used to animate all occurrences of this image.  Don't invalidate
    557     // the timer unless all renderers have stopped drawing.
    558     delete m_frameTimer;
    559     m_frameTimer = 0;
    560 }
    561 
    562 void BitmapImage::resetAnimation()
    563 {
    564     stopAnimation();
    565     m_currentFrame = 0;
    566     m_repetitionsComplete = 0;
    567     m_desiredFrameStartTime = 0;
    568     m_animationFinished = false;
    569 
    570     // For extremely large animations, when the animation is reset, we just throw everything away.
    571     destroyDecodedDataIfNecessary();
    572 }
    573 
    574 unsigned BitmapImage::decodedSize() const
    575 {
    576     return m_decodedSize;
    577 }
    578 
    579 
    580 
    581 void BitmapImage::advanceAnimation(Timer<BitmapImage>*)
    582 {
    583     internalAdvanceAnimation(false);
    584     // At this point the image region has been marked dirty, and if it's
    585     // onscreen, we'll soon make a call to draw(), which will call
    586     // startAnimation() again to keep the animation moving.
    587 }
    588 
    589 bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
    590 {
    591     // Stop the animation.
    592     stopAnimation();
    593 
    594     // See if anyone is still paying attention to this animation.  If not, we don't
    595     // advance and will remain suspended at the current frame until the animation is resumed.
    596     if (!skippingFrames && imageObserver()->shouldPauseAnimation(this))
    597         return false;
    598 
    599     ++m_currentFrame;
    600     bool advancedAnimation = true;
    601     if (m_currentFrame >= frameCount()) {
    602         ++m_repetitionsComplete;
    603 
    604         // Get the repetition count again.  If we weren't able to get a
    605         // repetition count before, we should have decoded the whole image by
    606         // now, so it should now be available.
    607         // Note that we don't need to special-case cAnimationLoopOnce here
    608         // because it is 0 (see comments on its declaration in ImageSource.h).
    609         if (repetitionCount(true) != cAnimationLoopInfinite && m_repetitionsComplete > m_repetitionCount) {
    610             m_animationFinished = true;
    611             m_desiredFrameStartTime = 0;
    612             --m_currentFrame;
    613             advancedAnimation = false;
    614         } else
    615             m_currentFrame = 0;
    616     }
    617     destroyDecodedDataIfNecessary();
    618 
    619     // We need to draw this frame if we advanced to it while not skipping, or if
    620     // while trying to skip frames we hit the last frame and thus had to stop.
    621     if (skippingFrames != advancedAnimation)
    622         imageObserver()->animationAdvanced(this);
    623     return advancedAnimation;
    624 }
    625 
    626 void BitmapImage::checkForSolidColor()
    627 {
    628     m_isSolidColor = false;
    629     m_checkedForSolidColor = true;
    630 
    631     if (frameCount() > 1)
    632         return;
    633 
    634     RefPtr<NativeImageSkia> frame = frameAtIndex(0);
    635 
    636     if (frame && size().width() == 1 && size().height() == 1) {
    637         SkAutoLockPixels lock(frame->bitmap());
    638         if (!frame->bitmap().getPixels())
    639             return;
    640 
    641         m_isSolidColor = true;
    642         m_solidColor = Color(frame->bitmap().getColor(0, 0));
    643     }
    644 }
    645 
    646 bool BitmapImage::mayFillWithSolidColor()
    647 {
    648     if (!m_checkedForSolidColor && frameCount() > 0) {
    649         checkForSolidColor();
    650         ASSERT(m_checkedForSolidColor);
    651     }
    652     return m_isSolidColor && !m_currentFrame;
    653 }
    654 
    655 Color BitmapImage::solidColor() const
    656 {
    657     return m_solidColor;
    658 }
    659 
    660 }
    661