Bug 1610307 [wpt PR 21266] - Update wpt metadata, a=testonly
[gecko.git] / image / FrameAnimator.cpp
blob6a06aef201b8ae7c50fe4baf9a4d226d44427baa
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/. */
6 #include "FrameAnimator.h"
8 #include <utility>
10 #include "LookupResult.h"
11 #include "RasterImage.h"
12 #include "imgIContainer.h"
13 #include "mozilla/CheckedInt.h"
14 #include "mozilla/StaticPrefs_image.h"
16 namespace mozilla {
18 using namespace gfx;
20 namespace image {
22 ///////////////////////////////////////////////////////////////////////////////
23 // AnimationState implementation.
24 ///////////////////////////////////////////////////////////////////////////////
26 const gfx::IntRect AnimationState::UpdateState(
27 bool aAnimationFinished, RasterImage* aImage, const gfx::IntSize& aSize,
28 bool aAllowInvalidation /* = true */) {
29 LookupResult result = SurfaceCache::Lookup(
30 ImageKey(aImage),
31 RasterSurfaceKey(aSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
32 /* aMarkUsed = */ false);
34 return UpdateStateInternal(result, aAnimationFinished, aSize,
35 aAllowInvalidation);
38 const gfx::IntRect AnimationState::UpdateStateInternal(
39 LookupResult& aResult, bool aAnimationFinished, 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;
56 // If mHasBeenDecoded is true then we know the true total frame count and
57 // we can use it to determine if we have all the frames now so we know if
58 // we are currently fully decoded.
59 // If mHasBeenDecoded is false then we'll get another UpdateState call
60 // when the decode finishes.
61 if (mHasBeenDecoded) {
62 Maybe<uint32_t> frameCount = FrameCount();
63 MOZ_ASSERT(frameCount.isSome());
64 mIsCurrentlyDecoded = aResult.Surface().IsFullyDecoded();
68 gfx::IntRect ret;
70 if (aAllowInvalidation) {
71 // Update the value of mCompositedFrameInvalid.
72 if (mIsCurrentlyDecoded || aAnimationFinished) {
73 // Animated images that have finished their animation (ie because it is a
74 // finite length animation) don't have RequestRefresh called on them, and
75 // so mCompositedFrameInvalid would never get cleared. We clear it here
76 // (and also in RasterImage::Decode when we create a decoder for an image
77 // that has finished animated so it can display sooner than waiting until
78 // the decode completes). We also do it if we are fully decoded. This is
79 // safe to do for images that aren't finished animating because before we
80 // paint the refresh driver will call into us to advance to the correct
81 // frame, and that will succeed because we have all the frames.
82 if (mCompositedFrameInvalid) {
83 // Invalidate if we are marking the composited frame valid.
84 ret.SizeTo(aSize);
86 mCompositedFrameInvalid = false;
87 } else if (aResult.Type() == MatchType::NOT_FOUND ||
88 aResult.Type() == MatchType::PENDING) {
89 if (mHasRequestedDecode) {
90 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
91 mCompositedFrameInvalid = true;
94 // Otherwise don't change the value of mCompositedFrameInvalid, it will be
95 // updated by RequestRefresh.
98 return ret;
101 void AnimationState::NotifyDecodeComplete() { mHasBeenDecoded = true; }
103 void AnimationState::ResetAnimation() { mCurrentAnimationFrameIndex = 0; }
105 void AnimationState::SetAnimationMode(uint16_t aAnimationMode) {
106 mAnimationMode = aAnimationMode;
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;
117 MOZ_ASSERT(!mHasBeenDecoded, "Adding new frames after decoding is finished?");
118 MOZ_ASSERT(aFrameCount <= mFrameCount + 1, "Skipped a frame?");
120 mFrameCount = aFrameCount;
123 Maybe<uint32_t> AnimationState::FrameCount() const {
124 return mHasBeenDecoded ? Some(mFrameCount) : Nothing();
127 void AnimationState::SetFirstFrameRefreshArea(const IntRect& aRefreshArea) {
128 mFirstFrameRefreshArea = aRefreshArea;
131 void AnimationState::InitAnimationFrameTimeIfNecessary() {
132 if (mCurrentAnimationFrameTime.IsNull()) {
133 mCurrentAnimationFrameTime = TimeStamp::Now();
137 void AnimationState::SetAnimationFrameTime(const TimeStamp& aTime) {
138 mCurrentAnimationFrameTime = aTime;
141 bool AnimationState::MaybeAdvanceAnimationFrameTime(const TimeStamp& aTime) {
142 if (!StaticPrefs::image_animated_resume_from_last_displayed() ||
143 mCurrentAnimationFrameTime >= aTime) {
144 return false;
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;
154 uint32_t AnimationState::GetCurrentAnimationFrameIndex() const {
155 return mCurrentAnimationFrameIndex;
158 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();
164 MOZ_ASSERT(mHasBeenDecoded,
165 "We know the loop length but decoding isn't done?");
167 // If we're not looping, a single loop time has no meaning.
168 if (mAnimationMode != imgIContainer::kNormalAnimMode) {
169 return FrameTimeout::Forever();
172 return *mLoopLength;
175 ///////////////////////////////////////////////////////////////////////////////
176 // FrameAnimator implementation.
177 ///////////////////////////////////////////////////////////////////////////////
179 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);
191 TimeDuration durationOfTimeout =
192 TimeDuration::FromMilliseconds(double(aCurrentTimeout.AsMilliseconds()));
193 return aState.mCurrentAnimationFrameTime + durationOfTimeout;
196 RefreshResult FrameAnimator::AdvanceFrame(AnimationState& aState,
197 DrawableSurface& aFrames,
198 RefPtr<imgFrame>& aCurrentFrame,
199 TimeStamp aTime) {
200 AUTO_PROFILER_LABEL("FrameAnimator::AdvanceFrame", GRAPHICS);
202 RefreshResult ret;
204 // Determine what the next frame is, taking into account looping.
205 uint32_t currentFrameIndex = aState.mCurrentAnimationFrameIndex;
206 uint32_t nextFrameIndex = currentFrameIndex + 1;
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();
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;
223 nextFrameIndex = 0;
225 if (aState.mLoopRemainingCount > 0) {
226 aState.mLoopRemainingCount--;
229 // If we're done, exit early.
230 if (ret.mAnimationFinished) {
231 return ret;
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;
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);
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;
273 if (nextFrame->GetTimeout() == FrameTimeout::Forever()) {
274 ret.mAnimationFinished = true;
277 if (nextFrameIndex == 0) {
278 ret.mDirtyRect = aState.FirstFrameRefreshArea();
279 } else {
280 ret.mDirtyRect = nextFrame->GetDirtyRect();
283 aState.mCurrentAnimationFrameTime =
284 GetCurrentImgFrameEndTime(aState, aCurrentFrame->GetTimeout());
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();
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());
309 aState.mCurrentAnimationFrameTime +=
310 TimeDuration::FromMilliseconds(loops * loopTime.AsMilliseconds());
312 if (aState.mLoopRemainingCount >= 0) {
313 MOZ_ASSERT(loops <= CheckedUint64(aState.mLoopRemainingCount).value());
314 aState.mLoopRemainingCount -= CheckedInt32(loops).value();
319 // Set currentAnimationFrameIndex at the last possible moment
320 aState.mCurrentAnimationFrameIndex = nextFrameIndex;
321 aState.mCompositedFrameRequested = false;
322 aCurrentFrame = std::move(nextFrame);
323 aFrames.Advance(nextFrameIndex);
325 // If we're here, we successfully advanced the frame.
326 ret.mFrameAdvanced = true;
328 return ret;
331 void FrameAnimator::ResetAnimation(AnimationState& aState) {
332 aState.ResetAnimation();
334 // Our surface provider is synchronized to our state, so we need to reset its
335 // state as well, if we still have one.
336 LookupResult result = SurfaceCache::Lookup(
337 ImageKey(mImage),
338 RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
339 /* aMarkUsed = */ false);
340 if (!result) {
341 return;
344 result.Surface().Reset();
347 RefreshResult FrameAnimator::RequestRefresh(AnimationState& aState,
348 const TimeStamp& aTime,
349 bool aAnimationFinished) {
350 // By default, an empty RefreshResult.
351 RefreshResult ret;
353 if (aState.IsDiscarded()) {
354 aState.MaybeAdvanceAnimationFrameTime(aTime);
355 return ret;
358 // Get the animation frames once now, and pass them down to callees because
359 // the surface could be discarded at anytime on a different thread. This is
360 // must easier to reason about then trying to write code that is safe to
361 // having the surface disappear at anytime.
362 LookupResult result = SurfaceCache::Lookup(
363 ImageKey(mImage),
364 RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
365 /* aMarkUsed = */ true);
367 ret.mDirtyRect =
368 aState.UpdateStateInternal(result, aAnimationFinished, mSize);
369 if (aState.IsDiscarded() || !result) {
370 aState.MaybeAdvanceAnimationFrameTime(aTime);
371 if (!ret.mDirtyRect.IsEmpty()) {
372 ret.mFrameAdvanced = true;
374 return ret;
377 RefPtr<imgFrame> currentFrame =
378 result.Surface().GetFrame(aState.mCurrentAnimationFrameIndex);
380 // only advance the frame if the current time is greater than or
381 // equal to the current frame's end time.
382 if (!currentFrame) {
383 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
384 MOZ_ASSERT(aState.GetHasRequestedDecode() &&
385 !aState.GetIsCurrentlyDecoded());
386 MOZ_ASSERT(aState.mCompositedFrameInvalid);
387 // Nothing we can do but wait for our previous current frame to be decoded
388 // again so we can determine what to do next.
389 aState.MaybeAdvanceAnimationFrameTime(aTime);
390 return ret;
393 TimeStamp currentFrameEndTime =
394 GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
396 // If nothing has accessed the composited frame since the last time we
397 // advanced, then there is no point in continuing to advance the animation.
398 // This has the effect of freezing the animation while not in view.
399 if (!aState.mCompositedFrameRequested &&
400 aState.MaybeAdvanceAnimationFrameTime(aTime)) {
401 return ret;
404 while (currentFrameEndTime <= aTime) {
405 TimeStamp oldFrameEndTime = currentFrameEndTime;
407 RefreshResult frameRes =
408 AdvanceFrame(aState, result.Surface(), currentFrame, aTime);
410 // Accumulate our result for returning to callers.
411 ret.Accumulate(frameRes);
413 // currentFrame was updated by AdvanceFrame so it is still current.
414 currentFrameEndTime =
415 GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
417 // If we didn't advance a frame, and our frame end time didn't change,
418 // then we need to break out of this loop & wait for the frame(s)
419 // to finish downloading.
420 if (!frameRes.mFrameAdvanced && currentFrameEndTime == oldFrameEndTime) {
421 break;
425 // We should only mark the composited frame as valid and reset the dirty rect
426 // if we advanced (meaning the next frame was actually produced somehow), the
427 // composited frame was previously invalid (so we may need to repaint
428 // everything) and either the frame index is valid (to know we were doing
429 // blending on the main thread, instead of on the decoder threads in advance),
430 // or the current frame is a full frame (blends off the main thread).
432 // If for some reason we forget to reset aState.mCompositedFrameInvalid, then
433 // GetCompositedFrame will fail, even if we have all the data available for
434 // display.
435 if (currentFrameEndTime > aTime && aState.mCompositedFrameInvalid) {
436 aState.mCompositedFrameInvalid = false;
437 ret.mDirtyRect = IntRect(IntPoint(0, 0), mSize);
440 MOZ_ASSERT(!aState.mIsCurrentlyDecoded || !aState.mCompositedFrameInvalid);
442 return ret;
445 LookupResult FrameAnimator::GetCompositedFrame(AnimationState& aState,
446 bool aMarkUsed) {
447 aState.mCompositedFrameRequested = true;
449 LookupResult result = SurfaceCache::Lookup(
450 ImageKey(mImage),
451 RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
452 aMarkUsed);
454 if (aState.mCompositedFrameInvalid) {
455 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
456 MOZ_ASSERT(aState.GetHasRequestedDecode());
457 MOZ_ASSERT(!aState.GetIsCurrentlyDecoded());
458 if (result.Type() == MatchType::NOT_FOUND) {
459 return result;
461 return LookupResult(MatchType::PENDING);
464 // Otherwise return the raw frame. DoBlend is required to ensure that we only
465 // hit this case if the frame is not paletted and doesn't require compositing.
466 if (!result) {
467 return result;
470 // Seek to the appropriate frame. If seeking fails, it means that we couldn't
471 // get the frame we're looking for; treat this as if the lookup failed.
472 if (NS_FAILED(result.Surface().Seek(aState.mCurrentAnimationFrameIndex))) {
473 if (result.Type() == MatchType::NOT_FOUND) {
474 return result;
476 return LookupResult(MatchType::PENDING);
479 return result;
482 } // namespace image
483 } // namespace mozilla