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 "BitmapImage.h"
30 #include "FloatRect.h"
31 #include "ImageObserver.h"
33 #include "MIMETypeRegistry.h"
34 #include "PlatformString.h"
36 #include <wtf/CurrentTime.h>
37 #include <wtf/Vector.h>
41 static int frameBytes(const IntSize& frameSize)
43 return frameSize.width() * frameSize.height() * 4;
46 BitmapImage::BitmapImage(ImageObserver* observer)
51 , m_repetitionCount(cAnimationNone)
52 , m_repetitionCountStatus(Unknown)
53 , m_repetitionsComplete(0)
54 , m_desiredFrameStartTime(0)
55 , m_isSolidColor(false)
56 , m_checkedForSolidColor(false)
57 , m_animationFinished(false)
58 , m_allDataReceived(false)
60 , m_sizeAvailable(false)
61 , m_hasUniformFrameSize(true)
63 , m_decodedPropertiesSize(0)
64 , m_haveFrameCount(false)
70 BitmapImage::~BitmapImage()
72 invalidatePlatformData();
76 void BitmapImage::destroyDecodedData(bool destroyAll)
78 int framesCleared = 0;
79 const size_t clearBeforeFrame = destroyAll ? m_frames.size() : m_currentFrame;
80 for (size_t i = 0; i < clearBeforeFrame; ++i) {
81 // The underlying frame isn't actually changing (we're just trying to
82 // save the memory for the framebuffer data), so we don't need to clear
84 if (m_frames[i].clear(false))
88 destroyMetadataAndNotify(framesCleared);
90 m_source.clear(destroyAll, clearBeforeFrame, data(), m_allDataReceived);
94 void BitmapImage::destroyDecodedDataIfNecessary(bool destroyAll)
96 // Animated images >5MB are considered large enough that we'll only hang on
97 // to one frame at a time.
98 static const unsigned cLargeAnimationCutoff = 5242880;
99 if (m_frames.size() * frameBytes(m_size) > cLargeAnimationCutoff)
100 destroyDecodedData(destroyAll);
103 void BitmapImage::destroyMetadataAndNotify(int framesCleared)
105 m_isSolidColor = false;
106 m_checkedForSolidColor = false;
107 invalidatePlatformData();
109 int deltaBytes = framesCleared * -frameBytes(m_size);
110 m_decodedSize += deltaBytes;
111 if (framesCleared > 0) {
112 deltaBytes -= m_decodedPropertiesSize;
113 m_decodedPropertiesSize = 0;
115 if (deltaBytes && imageObserver())
116 imageObserver()->decodedSizeChanged(this, deltaBytes);
119 void BitmapImage::cacheFrame(size_t index)
121 size_t numFrames = frameCount();
122 ASSERT(m_decodedSize == 0 || numFrames > 1);
124 if (m_frames.size() < numFrames)
125 m_frames.grow(numFrames);
127 m_frames[index].m_frame = m_source.createFrameAtIndex(index);
128 if (numFrames == 1 && m_frames[index].m_frame)
129 checkForSolidColor();
131 m_frames[index].m_haveMetadata = true;
132 m_frames[index].m_isComplete = m_source.frameIsCompleteAtIndex(index);
133 if (repetitionCount(false) != cAnimationNone)
134 m_frames[index].m_duration = m_source.frameDurationAtIndex(index);
135 m_frames[index].m_hasAlpha = m_source.frameHasAlphaAtIndex(index);
137 const IntSize frameSize(index ? m_source.frameSizeAtIndex(index) : m_size);
138 if (frameSize != m_size)
139 m_hasUniformFrameSize = false;
140 if (m_frames[index].m_frame) {
141 int deltaBytes = frameBytes(frameSize);
142 m_decodedSize += deltaBytes;
143 // The fully-decoded frame will subsume the partially decoded data used
144 // to determine image properties.
145 deltaBytes -= m_decodedPropertiesSize;
146 m_decodedPropertiesSize = 0;
148 imageObserver()->decodedSizeChanged(this, deltaBytes);
152 void BitmapImage::didDecodeProperties() const
156 size_t updatedSize = m_source.bytesDecodedToDetermineProperties();
157 if (m_decodedPropertiesSize == updatedSize)
159 int deltaBytes = updatedSize - m_decodedPropertiesSize;
161 bool overflow = updatedSize > m_decodedPropertiesSize && deltaBytes < 0;
162 bool underflow = updatedSize < m_decodedPropertiesSize && deltaBytes > 0;
163 ASSERT(!overflow && !underflow);
165 m_decodedPropertiesSize = updatedSize;
167 imageObserver()->decodedSizeChanged(this, deltaBytes);
170 IntSize BitmapImage::size() const
172 if (m_sizeAvailable && !m_haveSize) {
173 m_size = m_source.size();
175 didDecodeProperties();
180 IntSize BitmapImage::currentFrameSize() const
182 if (!m_currentFrame || m_hasUniformFrameSize)
184 IntSize frameSize = m_source.frameSizeAtIndex(m_currentFrame);
185 didDecodeProperties();
189 bool BitmapImage::getHotSpot(IntPoint& hotSpot) const
191 bool result = m_source.getHotSpot(hotSpot);
192 didDecodeProperties();
196 bool BitmapImage::dataChanged(bool allDataReceived)
198 // Because we're modifying the current frame, clear its (now possibly
199 // inaccurate) metadata as well.
200 destroyMetadataAndNotify((!m_frames.isEmpty() && m_frames[m_frames.size() - 1].clear(true)) ? 1 : 0);
202 // Feed all the data we've seen so far to the image decoder.
203 m_allDataReceived = allDataReceived;
204 m_source.setData(data(), allDataReceived);
206 // Clear the frame count.
207 m_haveFrameCount = false;
209 m_hasUniformFrameSize = true;
211 // Image properties will not be available until the first frame of the file
212 // reaches kCGImageStatusIncomplete.
213 return isSizeAvailable();
216 String BitmapImage::filenameExtension() const
218 return m_source.filenameExtension();
221 size_t BitmapImage::frameCount()
223 if (!m_haveFrameCount) {
224 m_haveFrameCount = true;
225 m_frameCount = m_source.frameCount();
226 didDecodeProperties();
231 bool BitmapImage::isSizeAvailable()
236 m_sizeAvailable = m_source.isSizeAvailable();
237 didDecodeProperties();
239 return m_sizeAvailable;
242 NativeImagePtr BitmapImage::frameAtIndex(size_t index)
244 if (index >= frameCount())
247 if (index >= m_frames.size() || !m_frames[index].m_frame)
250 return m_frames[index].m_frame;
253 bool BitmapImage::frameIsCompleteAtIndex(size_t index)
255 if (index >= frameCount())
258 if (index >= m_frames.size() || !m_frames[index].m_haveMetadata)
261 return m_frames[index].m_isComplete;
264 float BitmapImage::frameDurationAtIndex(size_t index)
266 if (index >= frameCount())
269 if (index >= m_frames.size() || !m_frames[index].m_haveMetadata)
272 return m_frames[index].m_duration;
275 bool BitmapImage::frameHasAlphaAtIndex(size_t index)
277 if (index >= frameCount())
280 if (index >= m_frames.size() || !m_frames[index].m_haveMetadata)
283 return m_frames[index].m_hasAlpha;
286 int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
288 if ((m_repetitionCountStatus == Unknown) || ((m_repetitionCountStatus == Uncertain) && imageKnownToBeComplete)) {
289 // Snag the repetition count. If |imageKnownToBeComplete| is false, the
290 // repetition count may not be accurate yet for GIFs; in this case the
291 // decoder will default to cAnimationLoopOnce, and we'll try and read
292 // the count again once the whole image is decoded.
293 m_repetitionCount = m_source.repetitionCount();
294 didDecodeProperties();
295 m_repetitionCountStatus = (imageKnownToBeComplete || m_repetitionCount == cAnimationNone) ? Certain : Uncertain;
297 return m_repetitionCount;
300 bool BitmapImage::shouldAnimate()
302 return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
305 void BitmapImage::startAnimation(bool catchUpIfNecessary)
307 if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
310 // If we aren't already animating, set now as the animation start time.
311 const double time = currentTime();
312 if (!m_desiredFrameStartTime)
313 m_desiredFrameStartTime = time;
315 // Don't advance the animation to an incomplete frame.
316 size_t nextFrame = (m_currentFrame + 1) % frameCount();
317 if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
320 // Don't advance past the last frame if we haven't decoded the whole image
321 // yet and our repetition count is potentially unset. The repetition count
322 // in a GIF can potentially come after all the rest of the image data, so
324 if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
327 // Determine time for next frame to start. By ignoring paint and timer lag
328 // in this calculation, we make the animation appear to run at its desired
329 // rate regardless of how fast it's being repainted.
330 const double currentDuration = frameDurationAtIndex(m_currentFrame);
331 m_desiredFrameStartTime += currentDuration;
333 // When an animated image is more than five minutes out of date, the
334 // user probably doesn't care about resyncing and we could burn a lot of
335 // time looping through frames below. Just reset the timings.
336 const double cAnimationResyncCutoff = 5 * 60;
337 if ((time - m_desiredFrameStartTime) > cAnimationResyncCutoff)
338 m_desiredFrameStartTime = time + currentDuration;
340 // The image may load more slowly than it's supposed to animate, so that by
341 // the time we reach the end of the first repetition, we're well behind.
342 // Clamp the desired frame start time in this case, so that we don't skip
343 // frames (or whole iterations) trying to "catch up". This is a tradeoff:
344 // It guarantees users see the whole animation the second time through and
345 // don't miss any repetitions, and is closer to what other browsers do; on
346 // the other hand, it makes animations "less accurate" for pages that try to
347 // sync an image and some other resource (e.g. audio), especially if users
348 // switch tabs (and thus stop drawing the animation, which will pause it)
349 // during that initial loop, then switch back later.
350 if (nextFrame == 0 && m_repetitionsComplete == 0 && m_desiredFrameStartTime < time)
351 m_desiredFrameStartTime = time;
353 if (!catchUpIfNecessary || time < m_desiredFrameStartTime) {
354 // Haven't yet reached time for next frame to start; delay until then.
355 m_frameTimer = new Timer<BitmapImage>(this, &BitmapImage::advanceAnimation);
356 m_frameTimer->startOneShot(std::max(m_desiredFrameStartTime - time, 0.));
358 // We've already reached or passed the time for the next frame to start.
359 // See if we've also passed the time for frames after that to start, in
360 // case we need to skip some frames entirely. Remember not to advance
361 // to an incomplete frame.
362 for (size_t frameAfterNext = (nextFrame + 1) % frameCount(); frameIsCompleteAtIndex(frameAfterNext); frameAfterNext = (nextFrame + 1) % frameCount()) {
363 // Should we skip the next frame?
364 double frameAfterNextStartTime = m_desiredFrameStartTime + frameDurationAtIndex(nextFrame);
365 if (time < frameAfterNextStartTime)
368 // Yes; skip over it without notifying our observers.
369 if (!internalAdvanceAnimation(true))
371 m_desiredFrameStartTime = frameAfterNextStartTime;
372 nextFrame = frameAfterNext;
375 // Draw the next frame immediately. Note that m_desiredFrameStartTime
376 // may be in the past, meaning the next time through this function we'll
377 // kick off the next advancement sooner than this frame's duration would
379 if (internalAdvanceAnimation(false)) {
380 // The image region has been marked dirty, but once we return to our
381 // caller, draw() will clear it, and nothing will cause the
382 // animation to advance again. We need to start the timer for the
383 // next frame running, or the animation can hang. (Compare this
384 // with when advanceAnimation() is called, and the region is dirtied
385 // while draw() is not in the callstack, meaning draw() gets called
386 // to update the region and thus startAnimation() is reached again.)
387 // NOTE: For large images with slow or heavily-loaded systems,
388 // throwing away data as we go (see destroyDecodedData()) means we
389 // can spend so much time re-decoding data above that by the time we
390 // reach here we're behind again. If we let startAnimation() run
391 // the catch-up code again, we can get long delays without painting
392 // as we race the timer, or even infinite recursion. In this
393 // situation the best we can do is to simply change frames as fast
394 // as possible, so force startAnimation() to set a zero-delay timer
395 // and bail out if we're not caught up.
396 startAnimation(false);
401 void BitmapImage::stopAnimation()
403 // This timer is used to animate all occurrences of this image. Don't invalidate
404 // the timer unless all renderers have stopped drawing.
409 void BitmapImage::resetAnimation()
413 m_repetitionsComplete = 0;
414 m_desiredFrameStartTime = 0;
415 m_animationFinished = false;
417 // For extremely large animations, when the animation is reset, we just throw everything away.
418 destroyDecodedDataIfNecessary(true);
421 void BitmapImage::advanceAnimation(Timer<BitmapImage>*)
423 internalAdvanceAnimation(false);
424 // At this point the image region has been marked dirty, and if it's
425 // onscreen, we'll soon make a call to draw(), which will call
426 // startAnimation() again to keep the animation moving.
429 bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
431 // Stop the animation.
434 // See if anyone is still paying attention to this animation. If not, we don't
435 // advance and will remain suspended at the current frame until the animation is resumed.
436 if (!skippingFrames && imageObserver()->shouldPauseAnimation(this))
440 bool advancedAnimation = true;
441 bool destroyAll = false;
442 if (m_currentFrame >= frameCount()) {
443 ++m_repetitionsComplete;
445 // Get the repetition count again. If we weren't able to get a
446 // repetition count before, we should have decoded the whole image by
447 // now, so it should now be available.
448 // Note that we don't need to special-case cAnimationLoopOnce here
449 // because it is 0 (see comments on its declaration in ImageSource.h).
450 if (repetitionCount(true) != cAnimationLoopInfinite && m_repetitionsComplete > m_repetitionCount) {
451 m_animationFinished = true;
452 m_desiredFrameStartTime = 0;
454 advancedAnimation = false;
460 destroyDecodedDataIfNecessary(destroyAll);
462 // We need to draw this frame if we advanced to it while not skipping, or if
463 // while trying to skip frames we hit the last frame and thus had to stop.
464 if (skippingFrames != advancedAnimation)
465 imageObserver()->animationAdvanced(this);
466 return advancedAnimation;