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