2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "platform/graphics/BitmapImage.h"
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"
43 BitmapImage::BitmapImage(ImageObserver* observer)
48 , m_repetitionCount(cAnimationNone)
49 , m_repetitionCountStatus(Unknown)
50 , m_repetitionsComplete(0)
51 , m_desiredFrameStartTime(0)
53 , m_isSolidColor(false)
54 , m_checkedForSolidColor(false)
55 , m_animationFinished(false)
56 , m_allDataReceived(false)
58 , m_sizeAvailable(false)
59 , m_hasUniformFrameSize(true)
60 , m_haveFrameCount(false)
64 BitmapImage::BitmapImage(PassRefPtr<NativeImageSkia> nativeImage, ImageObserver* observer)
66 , m_size(nativeImage->bitmap().width(), nativeImage->bitmap().height())
70 , m_repetitionCount(cAnimationNone)
71 , m_repetitionCountStatus(Unknown)
72 , m_repetitionsComplete(0)
74 , m_isSolidColor(false)
75 , m_checkedForSolidColor(false)
76 , m_animationFinished(true)
77 , m_allDataReceived(true)
79 , m_sizeAvailable(true)
80 , m_haveFrameCount(true)
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;
87 m_frames[0].m_hasAlpha = !nativeImage->bitmap().isOpaque();
88 m_frames[0].m_frame = nativeImage;
89 m_frames[0].m_haveMetadata = true;
94 BitmapImage::~BitmapImage()
99 bool BitmapImage::isBitmapImage() const
104 void BitmapImage::destroyDecodedData(bool destroyAll)
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
110 m_frames[i].clear(false);
113 destroyMetadataAndNotify(m_source.clearCacheExceptFrame(destroyAll ? kNotFound : m_currentFrame));
116 void BitmapImage::destroyDecodedDataIfNecessary()
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;
125 if (allFrameBytes > cLargeAnimationCutoff)
126 destroyDecodedData(false);
129 void BitmapImage::destroyMetadataAndNotify(size_t frameBytesCleared)
131 m_isSolidColor = false;
132 m_checkedForSolidColor = false;
134 if (frameBytesCleared && imageObserver())
135 imageObserver()->decodedSizeChanged(this, -safeCast<int>(frameBytesCleared));
138 void BitmapImage::cacheFrame(size_t index)
140 size_t numFrames = frameCount();
141 if (m_frames.size() < numFrames)
142 m_frames.grow(numFrames);
144 m_frames[index].m_frame = m_source.createFrameAtIndex(index);
145 if (numFrames == 1 && m_frames[index].m_frame)
146 checkForSolidColor();
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);
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.
164 imageObserver()->decodedSizeChanged(this, deltaBytes);
168 void BitmapImage::updateSize() const
170 if (!m_sizeAvailable || m_haveSize)
173 m_size = m_source.size();
174 m_sizeRespectingOrientation = m_source.size(RespectImageOrientation);
178 IntSize BitmapImage::size() const
184 IntSize BitmapImage::sizeRespectingOrientation() const
187 return m_sizeRespectingOrientation;
190 IntSize BitmapImage::currentFrameSize() const
192 if (!m_currentFrame || m_hasUniformFrameSize)
194 IntSize frameSize = m_source.frameSizeAtIndex(m_currentFrame);
198 bool BitmapImage::getHotSpot(IntPoint& hotSpot) const
200 bool result = m_source.getHotSpot(hotSpot);
204 bool BitmapImage::dataChanged(bool allDataReceived)
206 TRACE_EVENT0("blink", "BitmapImage::dataChanged");
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.
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);
234 destroyMetadataAndNotify(frameBytesCleared);
236 // Feed all the data we've seen so far to the image decoder.
237 m_allDataReceived = allDataReceived;
239 m_source.setData(*data(), allDataReceived);
241 m_haveFrameCount = false;
242 m_hasUniformFrameSize = true;
243 return isSizeAvailable();
246 bool BitmapImage::isAllDataReceived() const
248 return m_allDataReceived;
251 bool BitmapImage::hasColorProfile() const
253 return m_source.hasColorProfile();
256 String BitmapImage::filenameExtension() const
258 return m_source.filenameExtension();
261 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, WebBlendMode blendMode)
263 draw(ctxt, dstRect, srcRect, compositeOp, blendMode, DoNotRespectImageOrientation);
266 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, WebBlendMode blendMode, RespectImageOrientationEnum shouldRespectImageOrientation)
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.
273 RefPtr<NativeImageSkia> bm = nativeImageForCurrentFrame();
275 return; // It's too early and we don't have an image yet.
277 FloatRect normDstRect = adjustForNegativeSize(dstRect);
278 FloatRect normSrcRect = adjustForNegativeSize(srcRect);
279 normSrcRect.intersect(FloatRect(0, 0, bm->bitmap().width(), bm->bitmap().height()));
281 if (normSrcRect.isEmpty() || normDstRect.isEmpty())
282 return; // Nothing to draw.
284 ImageOrientation orientation = DefaultImageOrientation;
285 if (shouldRespectImageOrientation == RespectImageOrientation)
286 orientation = frameOrientationAtIndex(m_currentFrame);
288 GraphicsContextStateSaver saveContext(*ctxt, false);
289 if (orientation != DefaultImageOrientation) {
292 // ImageOrientation expects the origin to be at (0, 0)
293 ctxt->translate(normDstRect.x(), normDstRect.y());
294 normDstRect.setLocation(FloatPoint());
296 ctxt->concatCTM(orientation.transformFromDefault(normDstRect.size()));
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());
305 bm->draw(ctxt, normSrcRect, normDstRect, compositeOp, blendMode);
307 if (ImageObserver* observer = imageObserver())
308 observer->didDraw(this);
311 size_t BitmapImage::frameCount()
313 if (!m_haveFrameCount) {
314 m_frameCount = m_source.frameCount();
315 // If decoder is not initialized yet, m_source.frameCount() returns 0.
317 m_haveFrameCount = true;
323 bool BitmapImage::isSizeAvailable()
328 m_sizeAvailable = m_source.isSizeAvailable();
330 return m_sizeAvailable;
333 bool BitmapImage::ensureFrameIsCached(size_t index)
335 if (index >= frameCount())
338 if (index >= m_frames.size() || !m_frames[index].m_frame)
343 PassRefPtr<NativeImageSkia> BitmapImage::frameAtIndex(size_t index)
345 if (!ensureFrameIsCached(index))
347 return m_frames[index].m_frame;
350 bool BitmapImage::frameIsCompleteAtIndex(size_t index)
352 if (index < m_frames.size() && m_frames[index].m_haveMetadata && m_frames[index].m_isComplete)
354 return m_source.frameIsCompleteAtIndex(index);
357 float BitmapImage::frameDurationAtIndex(size_t index)
359 if (index < m_frames.size() && m_frames[index].m_haveMetadata)
360 return m_frames[index].m_duration;
361 return m_source.frameDurationAtIndex(index);
364 PassRefPtr<NativeImageSkia> BitmapImage::nativeImageForCurrentFrame()
366 return frameAtIndex(currentFrame());
369 PassRefPtr<Image> BitmapImage::imageForDefaultFrame()
371 if (isBitmapImage() && maybeAnimated())
372 return BitmapImage::create(frameAtIndex(0));
374 return Image::imageForDefaultFrame();
377 bool BitmapImage::frameHasAlphaAtIndex(size_t index)
379 if (m_frames.size() <= index)
382 if (m_frames[index].m_haveMetadata)
383 return m_frames[index].m_hasAlpha;
385 return m_source.frameHasAlphaAtIndex(index);
388 bool BitmapImage::currentFrameKnownToBeOpaque()
390 return !frameHasAlphaAtIndex(currentFrame());
393 ImageOrientation BitmapImage::currentFrameOrientation()
395 return frameOrientationAtIndex(currentFrame());
398 ImageOrientation BitmapImage::frameOrientationAtIndex(size_t index)
400 if (m_frames.size() <= index)
401 return DefaultImageOrientation;
403 if (m_frames[index].m_haveMetadata)
404 return m_frames[index].m_orientation;
406 return m_source.orientationAtIndex(index);
410 bool BitmapImage::notSolidColor()
412 return size().width() != 1 || size().height() != 1 || frameCount() > 1;
418 int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
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;
428 return m_repetitionCount;
431 bool BitmapImage::shouldAnimate()
433 return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
436 void BitmapImage::startAnimation(CatchUpAnimation catchUpIfNecessary)
438 if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
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;
446 // Don't advance the animation to an incomplete frame.
447 size_t nextFrame = (m_currentFrame + 1) % frameCount();
448 if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
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
455 if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
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;
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;
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;
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);
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)
499 // Yes; skip over it without notifying our observers.
500 if (!internalAdvanceAnimation(true))
502 m_desiredFrameStartTime = frameAfterNextStartTime;
503 nextFrame = frameAfterNext;
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
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);
532 void BitmapImage::stopAnimation()
534 // This timer is used to animate all occurrences of this image. Don't invalidate
535 // the timer unless all renderers have stopped drawing.
540 void BitmapImage::resetAnimation()
544 m_repetitionsComplete = 0;
545 m_desiredFrameStartTime = 0;
546 m_animationFinished = false;
548 // For extremely large animations, when the animation is reset, we just throw everything away.
549 destroyDecodedDataIfNecessary();
552 bool BitmapImage::maybeAnimated()
554 if (m_animationFinished)
556 if (frameCount() > 1)
558 return m_source.repetitionCount() != cAnimationNone;
561 void BitmapImage::advanceAnimation(Timer<BitmapImage>*)
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.
569 bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
571 // Stop the animation.
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))
580 bool advancedAnimation = true;
581 if (m_currentFrame >= frameCount()) {
582 ++m_repetitionsComplete;
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;
593 advancedAnimation = false;
597 destroyDecodedDataIfNecessary();
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;
606 void BitmapImage::checkForSolidColor()
608 m_isSolidColor = false;
609 m_checkedForSolidColor = true;
611 if (frameCount() > 1)
614 RefPtr<NativeImageSkia> frame = frameAtIndex(0);
616 if (frame && size().width() == 1 && size().height() == 1) {
617 SkAutoLockPixels lock(frame->bitmap());
618 if (!frame->bitmap().getPixels())
621 m_isSolidColor = true;
622 m_solidColor = Color(frame->bitmap().getColor(0, 0));
626 bool BitmapImage::mayFillWithSolidColor()
628 if (!m_checkedForSolidColor && frameCount() > 0) {
629 checkForSolidColor();
630 ASSERT(m_checkedForSolidColor);
632 return m_isSolidColor && !m_currentFrame;
635 Color BitmapImage::solidColor() const