Bug 1568860 - Part 2: Make getAllFonts fission compatible. r=ochameau
[gecko.git] / image / FrameAnimator.cpp
blob8dc34f647281c8c5d7b18eba9c914a31c77e3fd1
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 "mozilla/Move.h"
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/StaticPrefs_image.h"
11 #include "imgIContainer.h"
12 #include "LookupResult.h"
13 #include "RasterImage.h"
15 namespace mozilla {
17 using namespace gfx;
19 namespace image {
21 ///////////////////////////////////////////////////////////////////////////////
22 // AnimationState implementation.
23 ///////////////////////////////////////////////////////////////////////////////
25 const gfx::IntRect AnimationState::UpdateState(
26 bool aAnimationFinished, RasterImage* aImage, const gfx::IntSize& aSize,
27 bool aAllowInvalidation /* = true */) {
28 LookupResult result = SurfaceCache::Lookup(
29 ImageKey(aImage),
30 RasterSurfaceKey(aSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
31 /* aMarkUsed = */ false);
33 return UpdateStateInternal(result, aAnimationFinished, aSize,
34 aAllowInvalidation);
37 const gfx::IntRect AnimationState::UpdateStateInternal(
38 LookupResult& aResult, bool aAnimationFinished, const gfx::IntSize& aSize,
39 bool aAllowInvalidation /* = true */) {
40 // Update mDiscarded and mIsCurrentlyDecoded.
41 if (aResult.Type() == MatchType::NOT_FOUND) {
42 // no frames, we've either been discarded, or never been decoded before.
43 mDiscarded = mHasBeenDecoded;
44 mIsCurrentlyDecoded = false;
45 } else if (aResult.Type() == MatchType::PENDING) {
46 // no frames yet, but a decoder is or will be working on it.
47 mDiscarded = false;
48 mIsCurrentlyDecoded = false;
49 mHasRequestedDecode = true;
50 } else {
51 MOZ_ASSERT(aResult.Type() == MatchType::EXACT);
52 mDiscarded = false;
53 mHasRequestedDecode = true;
55 // If mHasBeenDecoded is true then we know the true total frame count and
56 // we can use it to determine if we have all the frames now so we know if
57 // we are currently fully decoded.
58 // If mHasBeenDecoded is false then we'll get another UpdateState call
59 // when the decode finishes.
60 if (mHasBeenDecoded) {
61 Maybe<uint32_t> frameCount = FrameCount();
62 MOZ_ASSERT(frameCount.isSome());
63 mIsCurrentlyDecoded = aResult.Surface().IsFullyDecoded();
67 gfx::IntRect ret;
69 if (aAllowInvalidation) {
70 // Update the value of mCompositedFrameInvalid.
71 if (mIsCurrentlyDecoded || aAnimationFinished) {
72 // Animated images that have finished their animation (ie because it is a
73 // finite length animation) don't have RequestRefresh called on them, and
74 // so mCompositedFrameInvalid would never get cleared. We clear it here
75 // (and also in RasterImage::Decode when we create a decoder for an image
76 // that has finished animated so it can display sooner than waiting until
77 // the decode completes). We also do it if we are fully decoded. This is
78 // safe to do for images that aren't finished animating because before we
79 // paint the refresh driver will call into us to advance to the correct
80 // frame, and that will succeed because we have all the frames.
81 if (mCompositedFrameInvalid) {
82 // Invalidate if we are marking the composited frame valid.
83 ret.SizeTo(aSize);
85 mCompositedFrameInvalid = false;
86 } else if (aResult.Type() == MatchType::NOT_FOUND ||
87 aResult.Type() == MatchType::PENDING) {
88 if (mHasRequestedDecode) {
89 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
90 mCompositedFrameInvalid = true;
93 // Otherwise don't change the value of mCompositedFrameInvalid, it will be
94 // updated by RequestRefresh.
97 return ret;
100 void AnimationState::NotifyDecodeComplete() { mHasBeenDecoded = true; }
102 void AnimationState::ResetAnimation() { mCurrentAnimationFrameIndex = 0; }
104 void AnimationState::SetAnimationMode(uint16_t aAnimationMode) {
105 mAnimationMode = aAnimationMode;
108 void AnimationState::UpdateKnownFrameCount(uint32_t aFrameCount) {
109 if (aFrameCount <= mFrameCount) {
110 // Nothing to do. Since we can redecode animated images, we may see the same
111 // sequence of updates replayed again, so seeing a smaller frame count than
112 // what we already know about doesn't indicate an error.
113 return;
116 MOZ_ASSERT(!mHasBeenDecoded, "Adding new frames after decoding is finished?");
117 MOZ_ASSERT(aFrameCount <= mFrameCount + 1, "Skipped a frame?");
119 mFrameCount = aFrameCount;
122 Maybe<uint32_t> AnimationState::FrameCount() const {
123 return mHasBeenDecoded ? Some(mFrameCount) : Nothing();
126 void AnimationState::SetFirstFrameRefreshArea(const IntRect& aRefreshArea) {
127 mFirstFrameRefreshArea = aRefreshArea;
130 void AnimationState::InitAnimationFrameTimeIfNecessary() {
131 if (mCurrentAnimationFrameTime.IsNull()) {
132 mCurrentAnimationFrameTime = TimeStamp::Now();
136 void AnimationState::SetAnimationFrameTime(const TimeStamp& aTime) {
137 mCurrentAnimationFrameTime = aTime;
140 bool AnimationState::MaybeAdvanceAnimationFrameTime(const TimeStamp& aTime) {
141 if (!StaticPrefs::image_animated_resume_from_last_displayed() ||
142 mCurrentAnimationFrameTime >= aTime) {
143 return false;
146 // We are configured to stop an animation when it is out of view, and restart
147 // it from the same point when it comes back into view. The same applies if it
148 // was discarded while out of view.
149 mCurrentAnimationFrameTime = aTime;
150 return true;
153 uint32_t AnimationState::GetCurrentAnimationFrameIndex() const {
154 return mCurrentAnimationFrameIndex;
157 FrameTimeout AnimationState::LoopLength() const {
158 // If we don't know the loop length yet, we have to treat it as infinite.
159 if (!mLoopLength) {
160 return FrameTimeout::Forever();
163 MOZ_ASSERT(mHasBeenDecoded,
164 "We know the loop length but decoding isn't done?");
166 // If we're not looping, a single loop time has no meaning.
167 if (mAnimationMode != imgIContainer::kNormalAnimMode) {
168 return FrameTimeout::Forever();
171 return *mLoopLength;
174 ///////////////////////////////////////////////////////////////////////////////
175 // FrameAnimator implementation.
176 ///////////////////////////////////////////////////////////////////////////////
178 TimeStamp FrameAnimator::GetCurrentImgFrameEndTime(
179 AnimationState& aState, FrameTimeout aCurrentTimeout) const {
180 if (aCurrentTimeout == FrameTimeout::Forever()) {
181 // We need to return a sentinel value in this case, because our logic
182 // doesn't work correctly if we have an infinitely long timeout. We use one
183 // year in the future as the sentinel because it works with the loop in
184 // RequestRefresh() below.
185 // XXX(seth): It'd be preferable to make our logic work correctly with
186 // infinitely long timeouts.
187 return TimeStamp::NowLoRes() + TimeDuration::FromMilliseconds(31536000.0);
190 TimeDuration durationOfTimeout =
191 TimeDuration::FromMilliseconds(double(aCurrentTimeout.AsMilliseconds()));
192 return aState.mCurrentAnimationFrameTime + durationOfTimeout;
195 RefreshResult FrameAnimator::AdvanceFrame(AnimationState& aState,
196 DrawableSurface& aFrames,
197 RefPtr<imgFrame>& aCurrentFrame,
198 TimeStamp aTime) {
199 AUTO_PROFILER_LABEL("FrameAnimator::AdvanceFrame", GRAPHICS);
201 RefreshResult ret;
203 // Determine what the next frame is, taking into account looping.
204 uint32_t currentFrameIndex = aState.mCurrentAnimationFrameIndex;
205 uint32_t nextFrameIndex = currentFrameIndex + 1;
207 // Check if we're at the end of the loop. (FrameCount() returns Nothing() if
208 // we don't know the total count yet.)
209 if (aState.FrameCount() == Some(nextFrameIndex)) {
210 // If we are not looping forever, initialize the loop counter
211 if (aState.mLoopRemainingCount < 0 && aState.LoopCount() >= 0) {
212 aState.mLoopRemainingCount = aState.LoopCount();
215 // If animation mode is "loop once", or we're at end of loop counter,
216 // it's time to stop animating.
217 if (aState.mAnimationMode == imgIContainer::kLoopOnceAnimMode ||
218 aState.mLoopRemainingCount == 0) {
219 ret.mAnimationFinished = true;
222 nextFrameIndex = 0;
224 if (aState.mLoopRemainingCount > 0) {
225 aState.mLoopRemainingCount--;
228 // If we're done, exit early.
229 if (ret.mAnimationFinished) {
230 return ret;
234 if (nextFrameIndex >= aState.KnownFrameCount()) {
235 // We've already advanced to the last decoded frame, nothing more we can do.
236 // We're blocked by network/decoding from displaying the animation at the
237 // rate specified, so that means the frame we are displaying (the latest
238 // available) is the frame we want to be displaying at this time. So we
239 // update the current animation time. If we didn't update the current
240 // animation time then it could lag behind, which would indicate that we are
241 // behind in the animation and should try to catch up. When we are done
242 // decoding (and thus can loop around back to the start of the animation) we
243 // would then jump to a random point in the animation to try to catch up.
244 // But we were never behind in the animation.
245 aState.mCurrentAnimationFrameTime = aTime;
246 return ret;
249 // There can be frames in the surface cache with index >= KnownFrameCount()
250 // which GetRawFrame() can access because an async decoder has decoded them,
251 // but which AnimationState doesn't know about yet because we haven't received
252 // the appropriate notification on the main thread. Make sure we stay in sync
253 // with AnimationState.
254 MOZ_ASSERT(nextFrameIndex < aState.KnownFrameCount());
255 RefPtr<imgFrame> nextFrame = aFrames.GetFrame(nextFrameIndex);
257 // We should always check to see if we have the next frame even if we have
258 // previously finished decoding. If we needed to redecode (e.g. due to a draw
259 // failure) we would have discarded all the old frames and may not yet have
260 // the new ones. DrawableSurface::RawAccessRef promises to only return
261 // finished frames.
262 if (!nextFrame) {
263 // Uh oh, the frame we want to show is currently being decoded (partial).
264 // Similar to the above case, we could be blocked by network or decoding,
265 // and so we should advance our current time rather than risk jumping
266 // through the animation. We will wait until the next refresh driver tick
267 // and try again.
268 aState.mCurrentAnimationFrameTime = aTime;
269 return ret;
272 if (nextFrame->GetTimeout() == FrameTimeout::Forever()) {
273 ret.mAnimationFinished = true;
276 if (nextFrameIndex == 0) {
277 ret.mDirtyRect = aState.FirstFrameRefreshArea();
278 } else {
279 ret.mDirtyRect = nextFrame->GetDirtyRect();
282 aState.mCurrentAnimationFrameTime =
283 GetCurrentImgFrameEndTime(aState, aCurrentFrame->GetTimeout());
285 // If we can get closer to the current time by a multiple of the image's loop
286 // time, we should. We can only do this if we're done decoding; otherwise, we
287 // don't know the full loop length, and LoopLength() will have to return
288 // FrameTimeout::Forever(). We also skip this for images with a finite loop
289 // count if we have initialized mLoopRemainingCount (it only gets initialized
290 // after one full loop).
291 FrameTimeout loopTime = aState.LoopLength();
292 if (loopTime != FrameTimeout::Forever() &&
293 (aState.LoopCount() < 0 || aState.mLoopRemainingCount >= 0)) {
294 TimeDuration delay = aTime - aState.mCurrentAnimationFrameTime;
295 if (delay.ToMilliseconds() > loopTime.AsMilliseconds()) {
296 // Explicitly use integer division to get the floor of the number of
297 // loops.
298 uint64_t loops = static_cast<uint64_t>(delay.ToMilliseconds()) /
299 loopTime.AsMilliseconds();
301 // If we have a finite loop count limit the number of loops we advance.
302 if (aState.mLoopRemainingCount >= 0) {
303 MOZ_ASSERT(aState.LoopCount() >= 0);
304 loops =
305 std::min(loops, CheckedUint64(aState.mLoopRemainingCount).value());
308 aState.mCurrentAnimationFrameTime +=
309 TimeDuration::FromMilliseconds(loops * loopTime.AsMilliseconds());
311 if (aState.mLoopRemainingCount >= 0) {
312 MOZ_ASSERT(loops <= CheckedUint64(aState.mLoopRemainingCount).value());
313 aState.mLoopRemainingCount -= CheckedInt32(loops).value();
318 // Set currentAnimationFrameIndex at the last possible moment
319 aState.mCurrentAnimationFrameIndex = nextFrameIndex;
320 aState.mCompositedFrameRequested = false;
321 aCurrentFrame = std::move(nextFrame);
322 aFrames.Advance(nextFrameIndex);
324 // If we're here, we successfully advanced the frame.
325 ret.mFrameAdvanced = true;
327 return ret;
330 void FrameAnimator::ResetAnimation(AnimationState& aState) {
331 aState.ResetAnimation();
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;
343 result.Surface().Reset();
346 RefreshResult FrameAnimator::RequestRefresh(AnimationState& aState,
347 const TimeStamp& aTime,
348 bool aAnimationFinished) {
349 // By default, an empty RefreshResult.
350 RefreshResult ret;
352 if (aState.IsDiscarded()) {
353 aState.MaybeAdvanceAnimationFrameTime(aTime);
354 return ret;
357 // Get the animation frames once now, and pass them down to callees because
358 // the surface could be discarded at anytime on a different thread. This is
359 // must easier to reason about then trying to write code that is safe to
360 // having the surface disappear at anytime.
361 LookupResult result = SurfaceCache::Lookup(
362 ImageKey(mImage),
363 RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
364 /* aMarkUsed = */ true);
366 ret.mDirtyRect =
367 aState.UpdateStateInternal(result, aAnimationFinished, mSize);
368 if (aState.IsDiscarded() || !result) {
369 aState.MaybeAdvanceAnimationFrameTime(aTime);
370 if (!ret.mDirtyRect.IsEmpty()) {
371 ret.mFrameAdvanced = true;
373 return ret;
376 RefPtr<imgFrame> currentFrame =
377 result.Surface().GetFrame(aState.mCurrentAnimationFrameIndex);
379 // only advance the frame if the current time is greater than or
380 // equal to the current frame's end time.
381 if (!currentFrame) {
382 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
383 MOZ_ASSERT(aState.GetHasRequestedDecode() &&
384 !aState.GetIsCurrentlyDecoded());
385 MOZ_ASSERT(aState.mCompositedFrameInvalid);
386 // Nothing we can do but wait for our previous current frame to be decoded
387 // again so we can determine what to do next.
388 aState.MaybeAdvanceAnimationFrameTime(aTime);
389 return ret;
392 TimeStamp currentFrameEndTime =
393 GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
395 // If nothing has accessed the composited frame since the last time we
396 // advanced, then there is no point in continuing to advance the animation.
397 // This has the effect of freezing the animation while not in view.
398 if (!aState.mCompositedFrameRequested &&
399 aState.MaybeAdvanceAnimationFrameTime(aTime)) {
400 return ret;
403 while (currentFrameEndTime <= aTime) {
404 TimeStamp oldFrameEndTime = currentFrameEndTime;
406 RefreshResult frameRes =
407 AdvanceFrame(aState, result.Surface(), currentFrame, aTime);
409 // Accumulate our result for returning to callers.
410 ret.Accumulate(frameRes);
412 // currentFrame was updated by AdvanceFrame so it is still current.
413 currentFrameEndTime =
414 GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
416 // If we didn't advance a frame, and our frame end time didn't change,
417 // then we need to break out of this loop & wait for the frame(s)
418 // to finish downloading.
419 if (!frameRes.mFrameAdvanced && currentFrameEndTime == oldFrameEndTime) {
420 break;
424 // We should only mark the composited frame as valid and reset the dirty rect
425 // if we advanced (meaning the next frame was actually produced somehow), the
426 // composited frame was previously invalid (so we may need to repaint
427 // everything) and either the frame index is valid (to know we were doing
428 // blending on the main thread, instead of on the decoder threads in advance),
429 // or the current frame is a full frame (blends off the main thread).
431 // If for some reason we forget to reset aState.mCompositedFrameInvalid, then
432 // GetCompositedFrame will fail, even if we have all the data available for
433 // display.
434 if (currentFrameEndTime > aTime && aState.mCompositedFrameInvalid) {
435 aState.mCompositedFrameInvalid = false;
436 ret.mDirtyRect = IntRect(IntPoint(0, 0), mSize);
439 MOZ_ASSERT(!aState.mIsCurrentlyDecoded || !aState.mCompositedFrameInvalid);
441 return ret;
444 LookupResult FrameAnimator::GetCompositedFrame(AnimationState& aState,
445 bool aMarkUsed) {
446 aState.mCompositedFrameRequested = true;
448 LookupResult result = SurfaceCache::Lookup(
449 ImageKey(mImage),
450 RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
451 aMarkUsed);
453 if (aState.mCompositedFrameInvalid) {
454 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
455 MOZ_ASSERT(aState.GetHasRequestedDecode());
456 MOZ_ASSERT(!aState.GetIsCurrentlyDecoded());
457 if (result.Type() == MatchType::NOT_FOUND) {
458 return result;
460 return LookupResult(MatchType::PENDING);
463 // Otherwise return the raw frame. DoBlend is required to ensure that we only
464 // hit this case if the frame is not paletted and doesn't require compositing.
465 if (!result) {
466 return result;
469 // Seek to the appropriate frame. If seeking fails, it means that we couldn't
470 // get the frame we're looking for; treat this as if the lookup failed.
471 if (NS_FAILED(result.Surface().Seek(aState.mCurrentAnimationFrameIndex))) {
472 if (result.Type() == MatchType::NOT_FOUND) {
473 return result;
475 return LookupResult(MatchType::PENDING);
478 return result;
481 } // namespace image
482 } // namespace mozilla