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/geometry/FloatRect.h"
32 #include "platform/graphics/GraphicsContextStateSaver.h"
33 #include "platform/graphics/ImageObserver.h"
34 #include "platform/graphics/skia/NativeImageSkia.h"
35 #include "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"
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("webkit", "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, blink::WebBlendMode blendMode)
263 draw(ctxt, dstRect, srcRect, compositeOp, blendMode, DoNotRespectImageOrientation);
266 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::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, WebCoreCompositeToSkiaComposite(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 bool BitmapImage::frameHasAlphaAtIndex(size_t index)
371 if (m_frames.size() <= index)
374 if (m_frames[index].m_haveMetadata)
375 return m_frames[index].m_hasAlpha;
377 return m_source.frameHasAlphaAtIndex(index);
380 bool BitmapImage::currentFrameKnownToBeOpaque()
382 return !frameHasAlphaAtIndex(currentFrame());
385 ImageOrientation BitmapImage::currentFrameOrientation()
387 return frameOrientationAtIndex(currentFrame());
390 ImageOrientation BitmapImage::frameOrientationAtIndex(size_t index)
392 if (m_frames.size() <= index)
393 return DefaultImageOrientation;
395 if (m_frames[index].m_haveMetadata)
396 return m_frames[index].m_orientation;
398 return m_source.orientationAtIndex(index);
402 bool BitmapImage::notSolidColor()
404 return size().width() != 1 || size().height() != 1 || frameCount() > 1;
410 int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
412 if ((m_repetitionCountStatus == Unknown) || ((m_repetitionCountStatus == Uncertain) && imageKnownToBeComplete)) {
413 // Snag the repetition count. If |imageKnownToBeComplete| is false, the
414 // repetition count may not be accurate yet for GIFs; in this case the
415 // decoder will default to cAnimationLoopOnce, and we'll try and read
416 // the count again once the whole image is decoded.
417 m_repetitionCount = m_source.repetitionCount();
418 m_repetitionCountStatus = (imageKnownToBeComplete || m_repetitionCount == cAnimationNone) ? Certain : Uncertain;
420 return m_repetitionCount;
423 bool BitmapImage::shouldAnimate()
425 return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
428 void BitmapImage::startAnimation(CatchUpAnimation catchUpIfNecessary)
430 if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
433 // If we aren't already animating, set now as the animation start time.
434 const double time = monotonicallyIncreasingTime();
435 if (!m_desiredFrameStartTime)
436 m_desiredFrameStartTime = time;
438 // Don't advance the animation to an incomplete frame.
439 size_t nextFrame = (m_currentFrame + 1) % frameCount();
440 if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
443 // Don't advance past the last frame if we haven't decoded the whole image
444 // yet and our repetition count is potentially unset. The repetition count
445 // in a GIF can potentially come after all the rest of the image data, so
447 if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
450 // Determine time for next frame to start. By ignoring paint and timer lag
451 // in this calculation, we make the animation appear to run at its desired
452 // rate regardless of how fast it's being repainted.
453 const double currentDuration = frameDurationAtIndex(m_currentFrame);
454 m_desiredFrameStartTime += currentDuration;
456 // When an animated image is more than five minutes out of date, the
457 // user probably doesn't care about resyncing and we could burn a lot of
458 // time looping through frames below. Just reset the timings.
459 const double cAnimationResyncCutoff = 5 * 60;
460 if ((time - m_desiredFrameStartTime) > cAnimationResyncCutoff)
461 m_desiredFrameStartTime = time + currentDuration;
463 // The image may load more slowly than it's supposed to animate, so that by
464 // the time we reach the end of the first repetition, we're well behind.
465 // Clamp the desired frame start time in this case, so that we don't skip
466 // frames (or whole iterations) trying to "catch up". This is a tradeoff:
467 // It guarantees users see the whole animation the second time through and
468 // don't miss any repetitions, and is closer to what other browsers do; on
469 // the other hand, it makes animations "less accurate" for pages that try to
470 // sync an image and some other resource (e.g. audio), especially if users
471 // switch tabs (and thus stop drawing the animation, which will pause it)
472 // during that initial loop, then switch back later.
473 if (nextFrame == 0 && m_repetitionsComplete == 0 && m_desiredFrameStartTime < time)
474 m_desiredFrameStartTime = time;
476 if (catchUpIfNecessary == DoNotCatchUp || time < m_desiredFrameStartTime) {
477 // Haven't yet reached time for next frame to start; delay until then.
478 m_frameTimer = new Timer<BitmapImage>(this, &BitmapImage::advanceAnimation);
479 m_frameTimer->startOneShot(std::max(m_desiredFrameStartTime - time, 0.), FROM_HERE);
481 // We've already reached or passed the time for the next frame to start.
482 // See if we've also passed the time for frames after that to start, in
483 // case we need to skip some frames entirely. Remember not to advance
484 // to an incomplete frame.
485 for (size_t frameAfterNext = (nextFrame + 1) % frameCount(); frameIsCompleteAtIndex(frameAfterNext); frameAfterNext = (nextFrame + 1) % frameCount()) {
486 // Should we skip the next frame?
487 double frameAfterNextStartTime = m_desiredFrameStartTime + frameDurationAtIndex(nextFrame);
488 if (time < frameAfterNextStartTime)
491 // Yes; skip over it without notifying our observers.
492 if (!internalAdvanceAnimation(true))
494 m_desiredFrameStartTime = frameAfterNextStartTime;
495 nextFrame = frameAfterNext;
498 // Draw the next frame immediately. Note that m_desiredFrameStartTime
499 // may be in the past, meaning the next time through this function we'll
500 // kick off the next advancement sooner than this frame's duration would
502 if (internalAdvanceAnimation(false)) {
503 // The image region has been marked dirty, but once we return to our
504 // caller, draw() will clear it, and nothing will cause the
505 // animation to advance again. We need to start the timer for the
506 // next frame running, or the animation can hang. (Compare this
507 // with when advanceAnimation() is called, and the region is dirtied
508 // while draw() is not in the callstack, meaning draw() gets called
509 // to update the region and thus startAnimation() is reached again.)
510 // NOTE: For large images with slow or heavily-loaded systems,
511 // throwing away data as we go (see destroyDecodedData()) means we
512 // can spend so much time re-decoding data above that by the time we
513 // reach here we're behind again. If we let startAnimation() run
514 // the catch-up code again, we can get long delays without painting
515 // as we race the timer, or even infinite recursion. In this
516 // situation the best we can do is to simply change frames as fast
517 // as possible, so force startAnimation() to set a zero-delay timer
518 // and bail out if we're not caught up.
519 startAnimation(DoNotCatchUp);
524 void BitmapImage::stopAnimation()
526 // This timer is used to animate all occurrences of this image. Don't invalidate
527 // the timer unless all renderers have stopped drawing.
532 void BitmapImage::resetAnimation()
536 m_repetitionsComplete = 0;
537 m_desiredFrameStartTime = 0;
538 m_animationFinished = false;
540 // For extremely large animations, when the animation is reset, we just throw everything away.
541 destroyDecodedDataIfNecessary();
544 bool BitmapImage::maybeAnimated()
546 if (m_animationFinished)
548 if (frameCount() > 1)
550 return m_source.repetitionCount() != cAnimationNone;
553 void BitmapImage::advanceAnimation(Timer<BitmapImage>*)
555 internalAdvanceAnimation(false);
556 // At this point the image region has been marked dirty, and if it's
557 // onscreen, we'll soon make a call to draw(), which will call
558 // startAnimation() again to keep the animation moving.
561 bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
563 // Stop the animation.
566 // See if anyone is still paying attention to this animation. If not, we don't
567 // advance and will remain suspended at the current frame until the animation is resumed.
568 if (!skippingFrames && imageObserver()->shouldPauseAnimation(this))
572 bool advancedAnimation = true;
573 if (m_currentFrame >= frameCount()) {
574 ++m_repetitionsComplete;
576 // Get the repetition count again. If we weren't able to get a
577 // repetition count before, we should have decoded the whole image by
578 // now, so it should now be available.
579 // Note that we don't need to special-case cAnimationLoopOnce here
580 // because it is 0 (see comments on its declaration in ImageSource.h).
581 if (repetitionCount(true) != cAnimationLoopInfinite && m_repetitionsComplete > m_repetitionCount) {
582 m_animationFinished = true;
583 m_desiredFrameStartTime = 0;
585 advancedAnimation = false;
589 destroyDecodedDataIfNecessary();
591 // We need to draw this frame if we advanced to it while not skipping, or if
592 // while trying to skip frames we hit the last frame and thus had to stop.
593 if (skippingFrames != advancedAnimation)
594 imageObserver()->animationAdvanced(this);
595 return advancedAnimation;
598 void BitmapImage::checkForSolidColor()
600 m_isSolidColor = false;
601 m_checkedForSolidColor = true;
603 if (frameCount() > 1)
606 RefPtr<NativeImageSkia> frame = frameAtIndex(0);
608 if (frame && size().width() == 1 && size().height() == 1) {
609 SkAutoLockPixels lock(frame->bitmap());
610 if (!frame->bitmap().getPixels())
613 m_isSolidColor = true;
614 m_solidColor = Color(frame->bitmap().getColor(0, 0));
618 bool BitmapImage::mayFillWithSolidColor()
620 if (!m_checkedForSolidColor && frameCount() > 0) {
621 checkForSolidColor();
622 ASSERT(m_checkedForSolidColor);
624 return m_isSolidColor && !m_currentFrame;
627 Color BitmapImage::solidColor() const