1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "FrameAnimator.h"
7 
8 #include <utility>
9 
10 #include "LookupResult.h"
11 #include "RasterImage.h"
12 #include "imgIContainer.h"
13 #include "mozilla/CheckedInt.h"
14 #include "mozilla/ProfilerLabels.h"
15 #include "mozilla/StaticPrefs_image.h"
16 
17 namespace mozilla {
18 
19 using namespace gfx;
20 
21 namespace image {
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 // AnimationState implementation.
25 ///////////////////////////////////////////////////////////////////////////////
26 
UpdateState(RasterImage * aImage,const gfx::IntSize & aSize,bool aAllowInvalidation)27 const gfx::IntRect AnimationState::UpdateState(
28     RasterImage* aImage, const gfx::IntSize& aSize,
29     bool aAllowInvalidation /* = true */) {
30   LookupResult result = SurfaceCache::Lookup(
31       ImageKey(aImage),
32       RasterSurfaceKey(aSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
33       /* aMarkUsed = */ false);
34 
35   return UpdateStateInternal(result, aSize, aAllowInvalidation);
36 }
37 
UpdateStateInternal(LookupResult & aResult,const gfx::IntSize & aSize,bool aAllowInvalidation)38 const gfx::IntRect AnimationState::UpdateStateInternal(
39     LookupResult& aResult, const gfx::IntSize& aSize,
40     bool aAllowInvalidation /* = true */) {
41   // Update mDiscarded and mIsCurrentlyDecoded.
42   if (aResult.Type() == MatchType::NOT_FOUND) {
43     // no frames, we've either been discarded, or never been decoded before.
44     mDiscarded = mHasBeenDecoded;
45     mIsCurrentlyDecoded = false;
46   } else if (aResult.Type() == MatchType::PENDING) {
47     // no frames yet, but a decoder is or will be working on it.
48     mDiscarded = false;
49     mIsCurrentlyDecoded = false;
50     mHasRequestedDecode = true;
51   } else {
52     MOZ_ASSERT(aResult.Type() == MatchType::EXACT);
53     mDiscarded = false;
54     mHasRequestedDecode = true;
55 
56     // If we can seek to the current animation frame we consider it decoded.
57     // Animated images are never fully decoded unless very short.
58     // Note that we use GetFrame instead of Seek here. The difference is that
59     // Seek eventually calls AnimationFrameBuffer::Get with aForDisplay == true,
60     // whereas GetFrame calls AnimationFrameBuffer::Get with aForDisplay ==
61     // false. The aForDisplay can change whether those functions succeed or not
62     // (only for the first frame). Since this is not for display we want to pass
63     // aForDisplay == false, but also for consistency with
64     // RequestRefresh/AdvanceFrame, because we want our state to be in sync with
65     // those functions. The user of Seek (GetCompositedFrame) doesn't need to be
66     // in sync with our state.
67     RefPtr<imgFrame> currentFrame =
68         bool(aResult.Surface())
69             ? aResult.Surface().GetFrame(mCurrentAnimationFrameIndex)
70             : nullptr;
71     mIsCurrentlyDecoded = !!currentFrame;
72   }
73 
74   gfx::IntRect ret;
75 
76   if (aAllowInvalidation) {
77     // Update the value of mCompositedFrameInvalid.
78     if (mIsCurrentlyDecoded) {
79       // It is safe to clear mCompositedFrameInvalid safe to do for images that
80       // are fully decoded but aren't finished animating because before we paint
81       // the refresh driver will call into us to advance to the correct frame,
82       // and that will succeed because we have all the frames.
83       if (mCompositedFrameInvalid) {
84         // Invalidate if we are marking the composited frame valid.
85         ret.SizeTo(aSize);
86       }
87       mCompositedFrameInvalid = false;
88     } else {
89       if (mHasRequestedDecode) {
90         MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
91         mCompositedFrameInvalid = true;
92       }
93     }
94     // Otherwise don't change the value of mCompositedFrameInvalid, it will be
95     // updated by RequestRefresh.
96   }
97 
98   return ret;
99 }
100 
NotifyDecodeComplete()101 void AnimationState::NotifyDecodeComplete() { mHasBeenDecoded = true; }
102 
ResetAnimation()103 void AnimationState::ResetAnimation() { mCurrentAnimationFrameIndex = 0; }
104 
SetAnimationMode(uint16_t aAnimationMode)105 void AnimationState::SetAnimationMode(uint16_t aAnimationMode) {
106   mAnimationMode = aAnimationMode;
107 }
108 
UpdateKnownFrameCount(uint32_t aFrameCount)109 void AnimationState::UpdateKnownFrameCount(uint32_t aFrameCount) {
110   if (aFrameCount <= mFrameCount) {
111     // Nothing to do. Since we can redecode animated images, we may see the same
112     // sequence of updates replayed again, so seeing a smaller frame count than
113     // what we already know about doesn't indicate an error.
114     return;
115   }
116 
117   MOZ_ASSERT(!mHasBeenDecoded, "Adding new frames after decoding is finished?");
118   MOZ_ASSERT(aFrameCount <= mFrameCount + 1, "Skipped a frame?");
119 
120   mFrameCount = aFrameCount;
121 }
122 
FrameCount() const123 Maybe<uint32_t> AnimationState::FrameCount() const {
124   return mHasBeenDecoded ? Some(mFrameCount) : Nothing();
125 }
126 
SetFirstFrameRefreshArea(const IntRect & aRefreshArea)127 void AnimationState::SetFirstFrameRefreshArea(const IntRect& aRefreshArea) {
128   mFirstFrameRefreshArea = aRefreshArea;
129 }
130 
InitAnimationFrameTimeIfNecessary()131 void AnimationState::InitAnimationFrameTimeIfNecessary() {
132   if (mCurrentAnimationFrameTime.IsNull()) {
133     mCurrentAnimationFrameTime = TimeStamp::Now();
134   }
135 }
136 
SetAnimationFrameTime(const TimeStamp & aTime)137 void AnimationState::SetAnimationFrameTime(const TimeStamp& aTime) {
138   mCurrentAnimationFrameTime = aTime;
139 }
140 
MaybeAdvanceAnimationFrameTime(const TimeStamp & aTime)141 bool AnimationState::MaybeAdvanceAnimationFrameTime(const TimeStamp& aTime) {
142   if (!StaticPrefs::image_animated_resume_from_last_displayed() ||
143       mCurrentAnimationFrameTime >= aTime) {
144     return false;
145   }
146 
147   // We are configured to stop an animation when it is out of view, and restart
148   // it from the same point when it comes back into view. The same applies if it
149   // was discarded while out of view.
150   mCurrentAnimationFrameTime = aTime;
151   return true;
152 }
153 
GetCurrentAnimationFrameIndex() const154 uint32_t AnimationState::GetCurrentAnimationFrameIndex() const {
155   return mCurrentAnimationFrameIndex;
156 }
157 
LoopLength() const158 FrameTimeout AnimationState::LoopLength() const {
159   // If we don't know the loop length yet, we have to treat it as infinite.
160   if (!mLoopLength) {
161     return FrameTimeout::Forever();
162   }
163 
164   MOZ_ASSERT(mHasBeenDecoded,
165              "We know the loop length but decoding isn't done?");
166 
167   // If we're not looping, a single loop time has no meaning.
168   if (mAnimationMode != imgIContainer::kNormalAnimMode) {
169     return FrameTimeout::Forever();
170   }
171 
172   return *mLoopLength;
173 }
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 // FrameAnimator implementation.
177 ///////////////////////////////////////////////////////////////////////////////
178 
GetCurrentImgFrameEndTime(AnimationState & aState,FrameTimeout aCurrentTimeout) const179 TimeStamp FrameAnimator::GetCurrentImgFrameEndTime(
180     AnimationState& aState, FrameTimeout aCurrentTimeout) const {
181   if (aCurrentTimeout == FrameTimeout::Forever()) {
182     // We need to return a sentinel value in this case, because our logic
183     // doesn't work correctly if we have an infinitely long timeout. We use one
184     // year in the future as the sentinel because it works with the loop in
185     // RequestRefresh() below.
186     // XXX(seth): It'd be preferable to make our logic work correctly with
187     // infinitely long timeouts.
188     return TimeStamp::NowLoRes() + TimeDuration::FromMilliseconds(31536000.0);
189   }
190 
191   TimeDuration durationOfTimeout =
192       TimeDuration::FromMilliseconds(double(aCurrentTimeout.AsMilliseconds()));
193   return aState.mCurrentAnimationFrameTime + durationOfTimeout;
194 }
195 
AdvanceFrame(AnimationState & aState,DrawableSurface & aFrames,RefPtr<imgFrame> & aCurrentFrame,TimeStamp aTime)196 RefreshResult FrameAnimator::AdvanceFrame(AnimationState& aState,
197                                           DrawableSurface& aFrames,
198                                           RefPtr<imgFrame>& aCurrentFrame,
199                                           TimeStamp aTime) {
200   AUTO_PROFILER_LABEL("FrameAnimator::AdvanceFrame", GRAPHICS);
201 
202   RefreshResult ret;
203 
204   // Determine what the next frame is, taking into account looping.
205   uint32_t currentFrameIndex = aState.mCurrentAnimationFrameIndex;
206   uint32_t nextFrameIndex = currentFrameIndex + 1;
207 
208   // Check if we're at the end of the loop. (FrameCount() returns Nothing() if
209   // we don't know the total count yet.)
210   if (aState.FrameCount() == Some(nextFrameIndex)) {
211     // If we are not looping forever, initialize the loop counter
212     if (aState.mLoopRemainingCount < 0 && aState.LoopCount() >= 0) {
213       aState.mLoopRemainingCount = aState.LoopCount();
214     }
215 
216     // If animation mode is "loop once", or we're at end of loop counter,
217     // it's time to stop animating.
218     if (aState.mAnimationMode == imgIContainer::kLoopOnceAnimMode ||
219         aState.mLoopRemainingCount == 0) {
220       ret.mAnimationFinished = true;
221     }
222 
223     nextFrameIndex = 0;
224 
225     if (aState.mLoopRemainingCount > 0) {
226       aState.mLoopRemainingCount--;
227     }
228 
229     // If we're done, exit early.
230     if (ret.mAnimationFinished) {
231       return ret;
232     }
233   }
234 
235   if (nextFrameIndex >= aState.KnownFrameCount()) {
236     // We've already advanced to the last decoded frame, nothing more we can do.
237     // We're blocked by network/decoding from displaying the animation at the
238     // rate specified, so that means the frame we are displaying (the latest
239     // available) is the frame we want to be displaying at this time. So we
240     // update the current animation time. If we didn't update the current
241     // animation time then it could lag behind, which would indicate that we are
242     // behind in the animation and should try to catch up. When we are done
243     // decoding (and thus can loop around back to the start of the animation) we
244     // would then jump to a random point in the animation to try to catch up.
245     // But we were never behind in the animation.
246     aState.mCurrentAnimationFrameTime = aTime;
247     return ret;
248   }
249 
250   // There can be frames in the surface cache with index >= KnownFrameCount()
251   // which GetRawFrame() can access because an async decoder has decoded them,
252   // but which AnimationState doesn't know about yet because we haven't received
253   // the appropriate notification on the main thread. Make sure we stay in sync
254   // with AnimationState.
255   MOZ_ASSERT(nextFrameIndex < aState.KnownFrameCount());
256   RefPtr<imgFrame> nextFrame = aFrames.GetFrame(nextFrameIndex);
257 
258   // We should always check to see if we have the next frame even if we have
259   // previously finished decoding. If we needed to redecode (e.g. due to a draw
260   // failure) we would have discarded all the old frames and may not yet have
261   // the new ones. DrawableSurface::RawAccessRef promises to only return
262   // finished frames.
263   if (!nextFrame) {
264     // Uh oh, the frame we want to show is currently being decoded (partial).
265     // Similar to the above case, we could be blocked by network or decoding,
266     // and so we should advance our current time rather than risk jumping
267     // through the animation. We will wait until the next refresh driver tick
268     // and try again.
269     aState.mCurrentAnimationFrameTime = aTime;
270     return ret;
271   }
272 
273   if (nextFrame->GetTimeout() == FrameTimeout::Forever()) {
274     ret.mAnimationFinished = true;
275   }
276 
277   if (nextFrameIndex == 0) {
278     ret.mDirtyRect = aState.FirstFrameRefreshArea();
279   } else {
280     ret.mDirtyRect = nextFrame->GetDirtyRect();
281   }
282 
283   aState.mCurrentAnimationFrameTime =
284       GetCurrentImgFrameEndTime(aState, aCurrentFrame->GetTimeout());
285 
286   // If we can get closer to the current time by a multiple of the image's loop
287   // time, we should. We can only do this if we're done decoding; otherwise, we
288   // don't know the full loop length, and LoopLength() will have to return
289   // FrameTimeout::Forever(). We also skip this for images with a finite loop
290   // count if we have initialized mLoopRemainingCount (it only gets initialized
291   // after one full loop).
292   FrameTimeout loopTime = aState.LoopLength();
293   if (loopTime != FrameTimeout::Forever() &&
294       (aState.LoopCount() < 0 || aState.mLoopRemainingCount >= 0)) {
295     TimeDuration delay = aTime - aState.mCurrentAnimationFrameTime;
296     if (delay.ToMilliseconds() > loopTime.AsMilliseconds()) {
297       // Explicitly use integer division to get the floor of the number of
298       // loops.
299       uint64_t loops = static_cast<uint64_t>(delay.ToMilliseconds()) /
300                        loopTime.AsMilliseconds();
301 
302       // If we have a finite loop count limit the number of loops we advance.
303       if (aState.mLoopRemainingCount >= 0) {
304         MOZ_ASSERT(aState.LoopCount() >= 0);
305         loops =
306             std::min(loops, CheckedUint64(aState.mLoopRemainingCount).value());
307       }
308 
309       aState.mCurrentAnimationFrameTime +=
310           TimeDuration::FromMilliseconds(loops * loopTime.AsMilliseconds());
311 
312       if (aState.mLoopRemainingCount >= 0) {
313         MOZ_ASSERT(loops <= CheckedUint64(aState.mLoopRemainingCount).value());
314         aState.mLoopRemainingCount -= CheckedInt32(loops).value();
315       }
316     }
317   }
318 
319   // Set currentAnimationFrameIndex at the last possible moment
320   aState.mCurrentAnimationFrameIndex = nextFrameIndex;
321   aCurrentFrame = std::move(nextFrame);
322   aFrames.Advance(nextFrameIndex);
323 
324   // If we're here, we successfully advanced the frame.
325   ret.mFrameAdvanced = true;
326 
327   return ret;
328 }
329 
ResetAnimation(AnimationState & aState)330 void FrameAnimator::ResetAnimation(AnimationState& aState) {
331   aState.ResetAnimation();
332 
333   // Our surface provider is synchronized to our state, so we need to reset its
334   // state as well, if we still have one.
335   LookupResult result = SurfaceCache::Lookup(
336       ImageKey(mImage),
337       RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
338       /* aMarkUsed = */ false);
339   if (!result) {
340     return;
341   }
342 
343   result.Surface().Reset();
344 
345   // Calling Reset on the surface of the animation can cause discarding surface
346   // providers to throw out all their frames so refresh our state.
347   aState.UpdateStateInternal(result, mSize);
348 }
349 
RequestRefresh(AnimationState & aState,const TimeStamp & aTime)350 RefreshResult FrameAnimator::RequestRefresh(AnimationState& aState,
351                                             const TimeStamp& aTime) {
352   // By default, an empty RefreshResult.
353   RefreshResult ret;
354 
355   if (aState.IsDiscarded()) {
356     aState.MaybeAdvanceAnimationFrameTime(aTime);
357     return ret;
358   }
359 
360   // Get the animation frames once now, and pass them down to callees because
361   // the surface could be discarded at anytime on a different thread. This is
362   // must easier to reason about then trying to write code that is safe to
363   // having the surface disappear at anytime.
364   LookupResult result = SurfaceCache::Lookup(
365       ImageKey(mImage),
366       RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
367       /* aMarkUsed = */ true);
368 
369   ret.mDirtyRect = aState.UpdateStateInternal(result, mSize);
370   if (aState.IsDiscarded() || !result) {
371     aState.MaybeAdvanceAnimationFrameTime(aTime);
372     return ret;
373   }
374 
375   RefPtr<imgFrame> currentFrame =
376       result.Surface().GetFrame(aState.mCurrentAnimationFrameIndex);
377 
378   // only advance the frame if the current time is greater than or
379   // equal to the current frame's end time.
380   if (!currentFrame) {
381     MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
382     MOZ_ASSERT(aState.GetHasRequestedDecode() &&
383                !aState.GetIsCurrentlyDecoded());
384     MOZ_ASSERT(aState.mCompositedFrameInvalid);
385     // Nothing we can do but wait for our previous current frame to be decoded
386     // again so we can determine what to do next.
387     aState.MaybeAdvanceAnimationFrameTime(aTime);
388     return ret;
389   }
390 
391   TimeStamp currentFrameEndTime =
392       GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
393 
394   // If nothing has accessed the composited frame since the last time we
395   // advanced, then there is no point in continuing to advance the animation.
396   // This has the effect of freezing the animation while not in view.
397   if (!result.Surface().MayAdvance() &&
398       aState.MaybeAdvanceAnimationFrameTime(aTime)) {
399     return ret;
400   }
401 
402   while (currentFrameEndTime <= aTime) {
403     TimeStamp oldFrameEndTime = currentFrameEndTime;
404 
405     RefreshResult frameRes =
406         AdvanceFrame(aState, result.Surface(), currentFrame, aTime);
407 
408     // Accumulate our result for returning to callers.
409     ret.Accumulate(frameRes);
410 
411     // currentFrame was updated by AdvanceFrame so it is still current.
412     currentFrameEndTime =
413         GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
414 
415     // If we didn't advance a frame, and our frame end time didn't change,
416     // then we need to break out of this loop & wait for the frame(s)
417     // to finish downloading.
418     if (!frameRes.mFrameAdvanced && currentFrameEndTime == oldFrameEndTime) {
419       break;
420     }
421   }
422 
423   // We should only mark the composited frame as valid and reset the dirty rect
424   // if we advanced (meaning the next frame was actually produced somehow), the
425   // composited frame was previously invalid (so we may need to repaint
426   // everything) and either the frame index is valid (to know we were doing
427   // blending on the main thread, instead of on the decoder threads in advance),
428   // or the current frame is a full frame (blends off the main thread).
429   //
430   // If for some reason we forget to reset aState.mCompositedFrameInvalid, then
431   // GetCompositedFrame will fail, even if we have all the data available for
432   // display.
433   if (currentFrameEndTime > aTime && aState.mCompositedFrameInvalid) {
434     aState.mCompositedFrameInvalid = false;
435     ret.mDirtyRect = IntRect(IntPoint(0, 0), mSize);
436   }
437 
438   MOZ_ASSERT(!aState.mIsCurrentlyDecoded || !aState.mCompositedFrameInvalid);
439 
440   return ret;
441 }
442 
GetCompositedFrame(AnimationState & aState,bool aMarkUsed)443 LookupResult FrameAnimator::GetCompositedFrame(AnimationState& aState,
444                                                bool aMarkUsed) {
445   LookupResult result = SurfaceCache::Lookup(
446       ImageKey(mImage),
447       RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
448       aMarkUsed);
449 
450   if (result) {
451     // If we are getting the frame directly (e.g. through tests or canvas), we
452     // need to ensure the animation is marked to allow advancing to the next
453     // frame.
454     result.Surface().MarkMayAdvance();
455   }
456 
457   if (aState.mCompositedFrameInvalid) {
458     MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
459     MOZ_ASSERT(aState.GetHasRequestedDecode());
460     MOZ_ASSERT(!aState.GetIsCurrentlyDecoded());
461 
462     if (result.Type() == MatchType::EXACT) {
463       // If our composited frame is marked as invalid but our frames are in the
464       // surface cache we might just have not updated our internal state yet.
465       // This can happen if the image is not in a document so that
466       // RequestRefresh is not getting called to advance the frame.
467       // RequestRefresh would result in our composited frame getting marked as
468       // valid either at the end of RequestRefresh when we are able to advance
469       // to the current time or if advancing frames eventually causes us to
470       // decode all of the frames of the image resulting in DecodeComplete
471       // getting called which calls UpdateState. The reason we care about this
472       // is that img.decode promises won't resolve until GetCompositedFrame
473       // returns a frame.
474       OrientedIntRect rect = OrientedIntRect::FromUnknownRect(
475           aState.UpdateStateInternal(result, mSize));
476 
477       if (!rect.IsEmpty()) {
478         nsCOMPtr<nsIEventTarget> eventTarget = do_GetMainThread();
479         RefPtr<RasterImage> image = mImage;
480         nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
481             "FrameAnimator::GetCompositedFrame",
482             [=]() -> void { image->NotifyProgress(NoProgress, rect); });
483         eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
484       }
485     }
486 
487     // If it's still invalid we have to return.
488     if (aState.mCompositedFrameInvalid) {
489       if (result.Type() == MatchType::NOT_FOUND) {
490         return result;
491       }
492       return LookupResult(MatchType::PENDING);
493     }
494   }
495 
496   // Otherwise return the raw frame. DoBlend is required to ensure that we only
497   // hit this case if the frame is not paletted and doesn't require compositing.
498   if (!result) {
499     return result;
500   }
501 
502   // Seek to the appropriate frame. If seeking fails, it means that we couldn't
503   // get the frame we're looking for; treat this as if the lookup failed.
504   if (NS_FAILED(result.Surface().Seek(aState.mCurrentAnimationFrameIndex))) {
505     if (result.Type() == MatchType::NOT_FOUND) {
506       return result;
507     }
508     return LookupResult(MatchType::PENDING);
509   }
510 
511   return result;
512 }
513 
514 }  // namespace image
515 }  // namespace mozilla
516