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