int32_t FrameAnimator::GetTimeoutForFrame(uint32_t aFrameNum) const { int32_t rawTimeout = 0; RawAccessFrameRef frame = GetRawFrame(aFrameNum); if (frame) { AnimationData data = frame->GetAnimationData(); rawTimeout = data.mRawTimeout; } else if (aFrameNum == 0) { rawTimeout = mFirstFrameTimeout; } else { NS_WARNING("No frame; called GetTimeoutForFrame too early?"); return 100; } // Ensure a minimal time between updates so we don't throttle the UI thread. // consider 0 == unspecified and make it fast but not too fast. Unless we // have a single loop GIF. See bug 890743, bug 125137, bug 139677, and bug // 207059. The behavior of recent IE and Opera versions seems to be: // IE 6/Win: // 10 - 50ms go 100ms // >50ms go correct speed // Opera 7 final/Win: // 10ms goes 100ms // >10ms go correct speed // It seems that there are broken tools out there that set a 0ms or 10ms // timeout when they really want a "default" one. So munge values in that // range. if (rawTimeout >= 0 && rawTimeout <= 10) { return 100; } return rawTimeout; }
FrameTimeout FrameAnimator::GetTimeoutForFrame(uint32_t aFrameNum) const { RawAccessFrameRef frame = GetRawFrame(aFrameNum); if (frame) { AnimationData data = frame->GetAnimationData(); return data.mTimeout; } NS_WARNING("No frame; called GetTimeoutForFrame too early?"); return FrameTimeout::FromRawMilliseconds(100); }
//****************************************************************************** // DoBlend gets called when the timer for animation get fired and we have to // update the composited frame of the animation. bool FrameAnimator::DoBlend(IntRect* aDirtyRect, uint32_t aPrevFrameIndex, uint32_t aNextFrameIndex) { RawAccessFrameRef prevFrame = GetRawFrame(aPrevFrameIndex); RawAccessFrameRef nextFrame = GetRawFrame(aNextFrameIndex); MOZ_ASSERT(prevFrame && nextFrame, "Should have frames here"); AnimationData prevFrameData = prevFrame->GetAnimationData(); if (prevFrameData.mDisposalMethod == DisposalMethod::RESTORE_PREVIOUS && !mCompositingPrevFrame) { prevFrameData.mDisposalMethod = DisposalMethod::CLEAR; } IntRect prevRect = prevFrameData.mBlendRect ? prevFrameData.mRect.Intersect(*prevFrameData.mBlendRect) : prevFrameData.mRect; bool isFullPrevFrame = prevRect.x == 0 && prevRect.y == 0 && prevRect.width == mSize.width && prevRect.height == mSize.height; // Optimization: DisposeClearAll if the previous frame is the same size as // container and it's clearing itself if (isFullPrevFrame && (prevFrameData.mDisposalMethod == DisposalMethod::CLEAR)) { prevFrameData.mDisposalMethod = DisposalMethod::CLEAR_ALL; } AnimationData nextFrameData = nextFrame->GetAnimationData(); IntRect nextRect = nextFrameData.mBlendRect ? nextFrameData.mRect.Intersect(*nextFrameData.mBlendRect) : nextFrameData.mRect; bool isFullNextFrame = nextRect.x == 0 && nextRect.y == 0 && nextRect.width == mSize.width && nextRect.height == mSize.height; if (!nextFrame->GetIsPaletted()) { // Optimization: Skip compositing if the previous frame wants to clear the // whole image if (prevFrameData.mDisposalMethod == DisposalMethod::CLEAR_ALL) { aDirtyRect->SetRect(0, 0, mSize.width, mSize.height); return true; } // Optimization: Skip compositing if this frame is the same size as the // container and it's fully drawing over prev frame (no alpha) if (isFullNextFrame && (nextFrameData.mDisposalMethod != DisposalMethod::RESTORE_PREVIOUS) && !nextFrameData.mHasAlpha) { aDirtyRect->SetRect(0, 0, mSize.width, mSize.height); return true; } } // Calculate area that needs updating switch (prevFrameData.mDisposalMethod) { default: MOZ_FALLTHROUGH_ASSERT("Unexpected DisposalMethod"); case DisposalMethod::NOT_SPECIFIED: case DisposalMethod::KEEP: *aDirtyRect = nextRect; break; case DisposalMethod::CLEAR_ALL: // Whole image container is cleared aDirtyRect->SetRect(0, 0, mSize.width, mSize.height); break; case DisposalMethod::CLEAR: // Calc area that needs to be redrawn (the combination of previous and // this frame) // XXX - This could be done with multiple framechanged calls // Having prevFrame way at the top of the image, and nextFrame // way at the bottom, and both frames being small, we'd be // telling framechanged to refresh the whole image when only two // small areas are needed. aDirtyRect->UnionRect(nextRect, prevRect); break; case DisposalMethod::RESTORE_PREVIOUS: aDirtyRect->SetRect(0, 0, mSize.width, mSize.height); break; } // Optimization: // Skip compositing if the last composited frame is this frame // (Only one composited frame was made for this animation. Example: // Only Frame 3 of a 10 frame image required us to build a composite frame // On the second loop, we do not need to rebuild the frame // since it's still sitting in compositingFrame) if (mLastCompositedFrameIndex == int32_t(aNextFrameIndex)) { return true; } bool needToBlankComposite = false; // Create the Compositing Frame if (!mCompositingFrame) { RefPtr<imgFrame> newFrame = new imgFrame; nsresult rv = newFrame->InitForDecoder(mSize, SurfaceFormat::B8G8R8A8); if (NS_FAILED(rv)) { mCompositingFrame.reset(); return false; } mCompositingFrame = newFrame->RawAccessRef(); needToBlankComposite = true; } else if (int32_t(aNextFrameIndex) != mLastCompositedFrameIndex+1) { // If we are not drawing on top of last composited frame, // then we are building a new composite frame, so let's clear it first. needToBlankComposite = true; } AnimationData compositingFrameData = mCompositingFrame->GetAnimationData(); // More optimizations possible when next frame is not transparent // But if the next frame has DisposalMethod::RESTORE_PREVIOUS, // this "no disposal" optimization is not possible, // because the frame in "after disposal operation" state // needs to be stored in compositingFrame, so it can be // copied into compositingPrevFrame later. bool doDisposal = true; if (!nextFrameData.mHasAlpha && nextFrameData.mDisposalMethod != DisposalMethod::RESTORE_PREVIOUS) { if (isFullNextFrame) { // Optimization: No need to dispose prev.frame when // next frame is full frame and not transparent. doDisposal = false; // No need to blank the composite frame needToBlankComposite = false; } else { if ((prevRect.x >= nextRect.x) && (prevRect.y >= nextRect.y) && (prevRect.x + prevRect.width <= nextRect.x + nextRect.width) && (prevRect.y + prevRect.height <= nextRect.y + nextRect.height)) { // Optimization: No need to dispose prev.frame when // next frame fully overlaps previous frame. doDisposal = false; } } } if (doDisposal) { // Dispose of previous: clear, restore, or keep (copy) switch (prevFrameData.mDisposalMethod) { case DisposalMethod::CLEAR: if (needToBlankComposite) { // If we just created the composite, it could have anything in its // buffer. Clear whole frame ClearFrame(compositingFrameData.mRawData, compositingFrameData.mRect); } else { // Only blank out previous frame area (both color & Mask/Alpha) ClearFrame(compositingFrameData.mRawData, compositingFrameData.mRect, prevRect); } break; case DisposalMethod::CLEAR_ALL: ClearFrame(compositingFrameData.mRawData, compositingFrameData.mRect); break; case DisposalMethod::RESTORE_PREVIOUS: // It would be better to copy only the area changed back to // compositingFrame. if (mCompositingPrevFrame) { AnimationData compositingPrevFrameData = mCompositingPrevFrame->GetAnimationData(); CopyFrameImage(compositingPrevFrameData.mRawData, compositingPrevFrameData.mRect, compositingFrameData.mRawData, compositingFrameData.mRect); // destroy only if we don't need it for this frame's disposal if (nextFrameData.mDisposalMethod != DisposalMethod::RESTORE_PREVIOUS) { mCompositingPrevFrame.reset(); } } else { ClearFrame(compositingFrameData.mRawData, compositingFrameData.mRect); } break; default: MOZ_FALLTHROUGH_ASSERT("Unexpected DisposalMethod"); case DisposalMethod::NOT_SPECIFIED: case DisposalMethod::KEEP: // Copy previous frame into compositingFrame before we put the new // frame on top // Assumes that the previous frame represents a full frame (it could be // smaller in size than the container, as long as the frame before it // erased itself) // Note: Frame 1 never gets into DoBlend(), so (aNextFrameIndex - 1) // will always be a valid frame number. if (mLastCompositedFrameIndex != int32_t(aNextFrameIndex - 1)) { if (isFullPrevFrame && !prevFrame->GetIsPaletted()) { // Just copy the bits CopyFrameImage(prevFrameData.mRawData, prevRect, compositingFrameData.mRawData, compositingFrameData.mRect); } else { if (needToBlankComposite) { // Only blank composite when prev is transparent or not full. if (prevFrameData.mHasAlpha || !isFullPrevFrame) { ClearFrame(compositingFrameData.mRawData, compositingFrameData.mRect); } } DrawFrameTo(prevFrameData.mRawData, prevFrameData.mRect, prevFrameData.mPaletteDataLength, prevFrameData.mHasAlpha, compositingFrameData.mRawData, compositingFrameData.mRect, prevFrameData.mBlendMethod, prevFrameData.mBlendRect); } } } } else if (needToBlankComposite) { // If we just created the composite, it could have anything in its // buffers. Clear them ClearFrame(compositingFrameData.mRawData, compositingFrameData.mRect); } // Check if the frame we are composing wants the previous image restored after // it is done. Don't store it (again) if last frame wanted its image restored // too if ((nextFrameData.mDisposalMethod == DisposalMethod::RESTORE_PREVIOUS) && (prevFrameData.mDisposalMethod != DisposalMethod::RESTORE_PREVIOUS)) { // We are storing the whole image. // It would be better if we just stored the area that nextFrame is going to // overwrite. if (!mCompositingPrevFrame) { RefPtr<imgFrame> newFrame = new imgFrame; nsresult rv = newFrame->InitForDecoder(mSize, SurfaceFormat::B8G8R8A8); if (NS_FAILED(rv)) { mCompositingPrevFrame.reset(); return false; } mCompositingPrevFrame = newFrame->RawAccessRef(); } AnimationData compositingPrevFrameData = mCompositingPrevFrame->GetAnimationData(); CopyFrameImage(compositingFrameData.mRawData, compositingFrameData.mRect, compositingPrevFrameData.mRawData, compositingPrevFrameData.mRect); mCompositingPrevFrame->Finish(); } // blit next frame into it's correct spot DrawFrameTo(nextFrameData.mRawData, nextFrameData.mRect, nextFrameData.mPaletteDataLength, nextFrameData.mHasAlpha, compositingFrameData.mRawData, compositingFrameData.mRect, nextFrameData.mBlendMethod, nextFrameData.mBlendRect); // Tell the image that it is fully 'downloaded'. mCompositingFrame->Finish(); mLastCompositedFrameIndex = int32_t(aNextFrameIndex); return true; }
RefreshResult FrameAnimator::AdvanceFrame(AnimationState& aState, TimeStamp aTime) { NS_ASSERTION(aTime <= TimeStamp::Now(), "Given time appears to be in the future"); PROFILER_LABEL_FUNC(js::ProfileEntry::Category::GRAPHICS); RefreshResult ret; // Determine what the next frame is, taking into account looping. uint32_t currentFrameIndex = aState.mCurrentAnimationFrameIndex; uint32_t nextFrameIndex = currentFrameIndex + 1; // Check if we're at the end of the loop. (FrameCount() returns Nothing() if // we don't know the total count yet.) if (aState.FrameCount() == Some(nextFrameIndex)) { // If we are not looping forever, initialize the loop counter if (aState.mLoopRemainingCount < 0 && aState.LoopCount() >= 0) { aState.mLoopRemainingCount = aState.LoopCount(); } // If animation mode is "loop once", or we're at end of loop counter, // it's time to stop animating. if (aState.mAnimationMode == imgIContainer::kLoopOnceAnimMode || aState.mLoopRemainingCount == 0) { ret.mAnimationFinished = true; } nextFrameIndex = 0; if (aState.mLoopRemainingCount > 0) { aState.mLoopRemainingCount--; } // If we're done, exit early. if (ret.mAnimationFinished) { return ret; } } if (nextFrameIndex >= aState.KnownFrameCount()) { // We've already advanced to the last decoded frame, nothing more we can do. // We're blocked by network/decoding from displaying the animation at the // rate specified, so that means the frame we are displaying (the latest // available) is the frame we want to be displaying at this time. So we // update the current animation time. If we didn't update the current // animation time then it could lag behind, which would indicate that we are // behind in the animation and should try to catch up. When we are done // decoding (and thus can loop around back to the start of the animation) we // would then jump to a random point in the animation to try to catch up. // But we were never behind in the animation. aState.mCurrentAnimationFrameTime = aTime; return ret; } // There can be frames in the surface cache with index >= KnownFrameCount() // which GetRawFrame() can access because an async decoder has decoded them, // but which AnimationState doesn't know about yet because we haven't received // the appropriate notification on the main thread. Make sure we stay in sync // with AnimationState. MOZ_ASSERT(nextFrameIndex < aState.KnownFrameCount()); RawAccessFrameRef nextFrame = GetRawFrame(nextFrameIndex); // We should always check to see if we have the next frame even if we have // previously finished decoding. If we needed to redecode (e.g. due to a draw // failure) we would have discarded all the old frames and may not yet have // the new ones. if (!nextFrame || !nextFrame->IsFinished()) { // Uh oh, the frame we want to show is currently being decoded (partial) // Wait until the next refresh driver tick and try again return ret; } if (GetTimeoutForFrame(nextFrameIndex) == FrameTimeout::Forever()) { ret.mAnimationFinished = true; } if (nextFrameIndex == 0) { ret.mDirtyRect = aState.FirstFrameRefreshArea(); } else { MOZ_ASSERT(nextFrameIndex == currentFrameIndex + 1); // Change frame if (!DoBlend(&ret.mDirtyRect, currentFrameIndex, nextFrameIndex)) { // something went wrong, move on to next NS_WARNING("FrameAnimator::AdvanceFrame(): Compositing of frame failed"); nextFrame->SetCompositingFailed(true); aState.mCurrentAnimationFrameTime = GetCurrentImgFrameEndTime(aState); aState.mCurrentAnimationFrameIndex = nextFrameIndex; return ret; } nextFrame->SetCompositingFailed(false); } aState.mCurrentAnimationFrameTime = GetCurrentImgFrameEndTime(aState); // If we can get closer to the current time by a multiple of the image's loop // time, we should. We can only do this if we're done decoding; otherwise, we // don't know the full loop length, and LoopLength() will have to return // FrameTimeout::Forever(). FrameTimeout loopTime = aState.LoopLength(); if (loopTime != FrameTimeout::Forever()) { TimeDuration delay = aTime - aState.mCurrentAnimationFrameTime; if (delay.ToMilliseconds() > loopTime.AsMilliseconds()) { // Explicitly use integer division to get the floor of the number of // loops. uint64_t loops = static_cast<uint64_t>(delay.ToMilliseconds()) / loopTime.AsMilliseconds(); aState.mCurrentAnimationFrameTime += TimeDuration::FromMilliseconds(loops * loopTime.AsMilliseconds()); } } // Set currentAnimationFrameIndex at the last possible moment aState.mCurrentAnimationFrameIndex = nextFrameIndex; // If we're here, we successfully advanced the frame. ret.mFrameAdvanced = true; return ret; }
FrameAnimator::RefreshResult FrameAnimator::AdvanceFrame(TimeStamp aTime) { NS_ASSERTION(aTime <= TimeStamp::Now(), "Given time appears to be in the future"); PROFILER_LABEL_FUNC(js::ProfileEntry::Category::GRAPHICS); RefreshResult ret; // Determine what the next frame is, taking into account looping. uint32_t currentFrameIndex = mCurrentAnimationFrameIndex; uint32_t nextFrameIndex = currentFrameIndex + 1; if (mImage->GetNumFrames() == nextFrameIndex) { // We can only accurately determine if we are at the end of the loop if we are // done decoding, otherwise we don't know how many frames there will be. if (!mDoneDecoding) { // We've already advanced to the last decoded frame, nothing more we can do. // We're blocked by network/decoding from displaying the animation at the // rate specified, so that means the frame we are displaying (the latest // available) is the frame we want to be displaying at this time. So we // update the current animation time. If we didn't update the current // animation time then it could lag behind, which would indicate that we // are behind in the animation and should try to catch up. When we are // done decoding (and thus can loop around back to the start of the // animation) we would then jump to a random point in the animation to // try to catch up. But we were never behind in the animation. mCurrentAnimationFrameTime = aTime; return ret; } // End of an animation loop... // If we are not looping forever, initialize the loop counter if (mLoopRemainingCount < 0 && LoopCount() >= 0) { mLoopRemainingCount = LoopCount(); } // If animation mode is "loop once", or we're at end of loop counter, // it's time to stop animating. if (mAnimationMode == imgIContainer::kLoopOnceAnimMode || mLoopRemainingCount == 0) { ret.animationFinished = true; } nextFrameIndex = 0; if (mLoopRemainingCount > 0) { mLoopRemainingCount--; } // If we're done, exit early. if (ret.animationFinished) { return ret; } } // There can be frames in the surface cache with index >= mImage->GetNumFrames() // that GetRawFrame can access because the decoding thread has decoded them, but // RasterImage hasn't acknowledged those frames yet. We don't want to go past // what RasterImage knows about so that we stay in sync with RasterImage. The code // above should obey this, the MOZ_ASSERT records this invariant. MOZ_ASSERT(nextFrameIndex < mImage->GetNumFrames()); RawAccessFrameRef nextFrame = GetRawFrame(nextFrameIndex); // If we're done decoding, we know we've got everything we're going to get. // If we aren't, we only display fully-downloaded frames; everything else // gets delayed. bool canDisplay = mDoneDecoding || (nextFrame && nextFrame->IsFinished()); if (!canDisplay) { // Uh oh, the frame we want to show is currently being decoded (partial) // Wait until the next refresh driver tick and try again return ret; } // Bad data if (GetTimeoutForFrame(nextFrameIndex) < 0) { ret.animationFinished = true; ret.error = true; } if (nextFrameIndex == 0) { ret.dirtyRect = mFirstFrameRefreshArea; } else { MOZ_ASSERT(nextFrameIndex == currentFrameIndex + 1); // Change frame if (!DoBlend(&ret.dirtyRect, currentFrameIndex, nextFrameIndex)) { // something went wrong, move on to next NS_WARNING("FrameAnimator::AdvanceFrame(): Compositing of frame failed"); nextFrame->SetCompositingFailed(true); mCurrentAnimationFrameTime = GetCurrentImgFrameEndTime(); mCurrentAnimationFrameIndex = nextFrameIndex; ret.error = true; return ret; } nextFrame->SetCompositingFailed(false); } mCurrentAnimationFrameTime = GetCurrentImgFrameEndTime(); // If we can get closer to the current time by a multiple of the image's loop // time, we should. We need to be done decoding in order to know the full loop // time though! int32_t loopTime = GetSingleLoopTime(); if (loopTime > 0) { // We shouldn't be advancing by a whole loop unless we are decoded and know // what a full loop actually is. GetSingleLoopTime should return -1 so this // never happens. MOZ_ASSERT(mDoneDecoding); TimeDuration delay = aTime - mCurrentAnimationFrameTime; if (delay.ToMilliseconds() > loopTime) { // Explicitly use integer division to get the floor of the number of // loops. uint64_t loops = static_cast<uint64_t>(delay.ToMilliseconds()) / loopTime; mCurrentAnimationFrameTime += TimeDuration::FromMilliseconds(loops * loopTime); } } // Set currentAnimationFrameIndex at the last possible moment mCurrentAnimationFrameIndex = nextFrameIndex; // If we're here, we successfully advanced the frame. ret.frameAdvanced = true; return ret; }