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 "BitmapImage.h"
29 
30 #include "FloatRect.h"
31 #include "ImageObserver.h"
32 #include "IntRect.h"
33 #include "MIMETypeRegistry.h"
34 #include "PlatformString.h"
35 #include "Timer.h"
36 #include <wtf/CurrentTime.h>
37 #include <wtf/Vector.h>
38 
39 namespace WebCore {
40 
frameBytes(const IntSize & frameSize)41 static int frameBytes(const IntSize& frameSize)
42 {
43     return frameSize.width() * frameSize.height() * 4;
44 }
45 
BitmapImage(ImageObserver * observer)46 BitmapImage::BitmapImage(ImageObserver* observer)
47     : Image(observer)
48     , m_currentFrame(0)
49     , m_frames(0)
50     , m_frameTimer(0)
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)
59     , m_haveSize(false)
60     , m_sizeAvailable(false)
61     , m_hasUniformFrameSize(true)
62     , m_decodedSize(0)
63     , m_decodedPropertiesSize(0)
64     , m_haveFrameCount(false)
65     , m_frameCount(0)
66 {
67     initPlatformData();
68 }
69 
~BitmapImage()70 BitmapImage::~BitmapImage()
71 {
72     invalidatePlatformData();
73     stopAnimation();
74 }
75 
destroyDecodedData(bool destroyAll)76 void BitmapImage::destroyDecodedData(bool destroyAll)
77 {
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
83         // the metadata.
84         if (m_frames[i].clear(false))
85           ++framesCleared;
86     }
87 
88     destroyMetadataAndNotify(framesCleared);
89 
90     m_source.clear(destroyAll, clearBeforeFrame, data(), m_allDataReceived);
91     return;
92 }
93 
destroyDecodedDataIfNecessary(bool destroyAll)94 void BitmapImage::destroyDecodedDataIfNecessary(bool destroyAll)
95 {
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);
101 }
102 
destroyMetadataAndNotify(int framesCleared)103 void BitmapImage::destroyMetadataAndNotify(int framesCleared)
104 {
105     m_isSolidColor = false;
106     m_checkedForSolidColor = false;
107     invalidatePlatformData();
108 
109     int deltaBytes = framesCleared * -frameBytes(m_size);
110     m_decodedSize += deltaBytes;
111     if (framesCleared > 0) {
112         deltaBytes -= m_decodedPropertiesSize;
113         m_decodedPropertiesSize = 0;
114     }
115     if (deltaBytes && imageObserver())
116         imageObserver()->decodedSizeChanged(this, deltaBytes);
117 }
118 
cacheFrame(size_t index)119 void BitmapImage::cacheFrame(size_t index)
120 {
121     size_t numFrames = frameCount();
122     ASSERT(m_decodedSize == 0 || numFrames > 1);
123 
124     if (m_frames.size() < numFrames)
125         m_frames.grow(numFrames);
126 
127     m_frames[index].m_frame = m_source.createFrameAtIndex(index);
128     if (numFrames == 1 && m_frames[index].m_frame)
129         checkForSolidColor();
130 
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);
136 
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;
147         if (imageObserver())
148             imageObserver()->decodedSizeChanged(this, deltaBytes);
149     }
150 }
151 
didDecodeProperties() const152 void BitmapImage::didDecodeProperties() const
153 {
154     if (m_decodedSize)
155         return;
156     size_t updatedSize = m_source.bytesDecodedToDetermineProperties();
157     if (m_decodedPropertiesSize == updatedSize)
158         return;
159     int deltaBytes = updatedSize - m_decodedPropertiesSize;
160 #ifndef NDEBUG
161     bool overflow = updatedSize > m_decodedPropertiesSize && deltaBytes < 0;
162     bool underflow = updatedSize < m_decodedPropertiesSize && deltaBytes > 0;
163     ASSERT(!overflow && !underflow);
164 #endif
165     m_decodedPropertiesSize = updatedSize;
166     if (imageObserver())
167         imageObserver()->decodedSizeChanged(this, deltaBytes);
168 }
169 
size() const170 IntSize BitmapImage::size() const
171 {
172     if (m_sizeAvailable && !m_haveSize) {
173         m_size = m_source.size();
174         m_haveSize = true;
175         didDecodeProperties();
176     }
177     return m_size;
178 }
179 
currentFrameSize() const180 IntSize BitmapImage::currentFrameSize() const
181 {
182     if (!m_currentFrame || m_hasUniformFrameSize)
183         return size();
184     IntSize frameSize = m_source.frameSizeAtIndex(m_currentFrame);
185     didDecodeProperties();
186     return frameSize;
187 }
188 
getHotSpot(IntPoint & hotSpot) const189 bool BitmapImage::getHotSpot(IntPoint& hotSpot) const
190 {
191     bool result = m_source.getHotSpot(hotSpot);
192     didDecodeProperties();
193     return result;
194 }
195 
dataChanged(bool allDataReceived)196 bool BitmapImage::dataChanged(bool allDataReceived)
197 {
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);
201 
202     // Feed all the data we've seen so far to the image decoder.
203     m_allDataReceived = allDataReceived;
204     m_source.setData(data(), allDataReceived);
205 
206     // Clear the frame count.
207     m_haveFrameCount = false;
208 
209     m_hasUniformFrameSize = true;
210 
211     // Image properties will not be available until the first frame of the file
212     // reaches kCGImageStatusIncomplete.
213     return isSizeAvailable();
214 }
215 
filenameExtension() const216 String BitmapImage::filenameExtension() const
217 {
218     return m_source.filenameExtension();
219 }
220 
frameCount()221 size_t BitmapImage::frameCount()
222 {
223     if (!m_haveFrameCount) {
224         m_haveFrameCount = true;
225         m_frameCount = m_source.frameCount();
226         didDecodeProperties();
227     }
228     return m_frameCount;
229 }
230 
isSizeAvailable()231 bool BitmapImage::isSizeAvailable()
232 {
233     if (m_sizeAvailable)
234         return true;
235 
236     m_sizeAvailable = m_source.isSizeAvailable();
237     didDecodeProperties();
238 
239     return m_sizeAvailable;
240 }
241 
frameAtIndex(size_t index)242 NativeImagePtr BitmapImage::frameAtIndex(size_t index)
243 {
244     if (index >= frameCount())
245         return 0;
246 
247     if (index >= m_frames.size() || !m_frames[index].m_frame)
248         cacheFrame(index);
249 
250     return m_frames[index].m_frame;
251 }
252 
frameIsCompleteAtIndex(size_t index)253 bool BitmapImage::frameIsCompleteAtIndex(size_t index)
254 {
255     if (index >= frameCount())
256         return true;
257 
258     if (index >= m_frames.size() || !m_frames[index].m_haveMetadata)
259         cacheFrame(index);
260 
261     return m_frames[index].m_isComplete;
262 }
263 
frameDurationAtIndex(size_t index)264 float BitmapImage::frameDurationAtIndex(size_t index)
265 {
266     if (index >= frameCount())
267         return 0;
268 
269     if (index >= m_frames.size() || !m_frames[index].m_haveMetadata)
270         cacheFrame(index);
271 
272     return m_frames[index].m_duration;
273 }
274 
frameHasAlphaAtIndex(size_t index)275 bool BitmapImage::frameHasAlphaAtIndex(size_t index)
276 {
277     if (index >= frameCount())
278         return true;
279 
280     if (index >= m_frames.size() || !m_frames[index].m_haveMetadata)
281         cacheFrame(index);
282 
283     return m_frames[index].m_hasAlpha;
284 }
285 
repetitionCount(bool imageKnownToBeComplete)286 int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
287 {
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;
296     }
297     return m_repetitionCount;
298 }
299 
shouldAnimate()300 bool BitmapImage::shouldAnimate()
301 {
302     return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
303 }
304 
startAnimation(bool catchUpIfNecessary)305 void BitmapImage::startAnimation(bool catchUpIfNecessary)
306 {
307     if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
308         return;
309 
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;
314 
315     // Don't advance the animation to an incomplete frame.
316     size_t nextFrame = (m_currentFrame + 1) % frameCount();
317     if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
318         return;
319 
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
323     // wait on it.
324     if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
325         return;
326 
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;
332 
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;
339 
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;
352 
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.));
357     } else {
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)
366                 break;
367 
368             // Yes; skip over it without notifying our observers.
369             if (!internalAdvanceAnimation(true))
370                 return;
371             m_desiredFrameStartTime = frameAfterNextStartTime;
372             nextFrame = frameAfterNext;
373         }
374 
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
378         // suggest.
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);
397         }
398     }
399 }
400 
stopAnimation()401 void BitmapImage::stopAnimation()
402 {
403     // This timer is used to animate all occurrences of this image.  Don't invalidate
404     // the timer unless all renderers have stopped drawing.
405     delete m_frameTimer;
406     m_frameTimer = 0;
407 }
408 
resetAnimation()409 void BitmapImage::resetAnimation()
410 {
411     stopAnimation();
412     m_currentFrame = 0;
413     m_repetitionsComplete = 0;
414     m_desiredFrameStartTime = 0;
415     m_animationFinished = false;
416 
417     // For extremely large animations, when the animation is reset, we just throw everything away.
418     destroyDecodedDataIfNecessary(true);
419 }
420 
advanceAnimation(Timer<BitmapImage> *)421 void BitmapImage::advanceAnimation(Timer<BitmapImage>*)
422 {
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.
427 }
428 
internalAdvanceAnimation(bool skippingFrames)429 bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
430 {
431     // Stop the animation.
432     stopAnimation();
433 
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))
437         return false;
438 
439     ++m_currentFrame;
440     bool advancedAnimation = true;
441     bool destroyAll = false;
442     if (m_currentFrame >= frameCount()) {
443         ++m_repetitionsComplete;
444 
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;
453             --m_currentFrame;
454             advancedAnimation = false;
455         } else {
456             m_currentFrame = 0;
457             destroyAll = true;
458         }
459     }
460     destroyDecodedDataIfNecessary(destroyAll);
461 
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;
467 }
468 
469 }
470