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; }