image/src/FrameAnimator.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "FrameAnimator.h"
michael@0 7 #include "FrameBlender.h"
michael@0 8
michael@0 9 #include "imgIContainer.h"
michael@0 10
michael@0 11 using namespace mozilla::image;
michael@0 12 using namespace mozilla;
michael@0 13
michael@0 14 FrameAnimator::FrameAnimator(FrameBlender& aFrameBlender,
michael@0 15 uint16_t aAnimationMode)
michael@0 16 : mCurrentAnimationFrameIndex(0)
michael@0 17 , mLoopCounter(-1)
michael@0 18 , mFrameBlender(aFrameBlender)
michael@0 19 , mAnimationMode(aAnimationMode)
michael@0 20 , mDoneDecoding(false)
michael@0 21 {
michael@0 22 }
michael@0 23
michael@0 24 int32_t
michael@0 25 FrameAnimator::GetSingleLoopTime() const
michael@0 26 {
michael@0 27 // If we aren't done decoding, we don't know the image's full play time.
michael@0 28 if (!mDoneDecoding) {
michael@0 29 return -1;
michael@0 30 }
michael@0 31
michael@0 32 // If we're not looping, a single loop time has no meaning
michael@0 33 if (mAnimationMode != imgIContainer::kNormalAnimMode) {
michael@0 34 return -1;
michael@0 35 }
michael@0 36
michael@0 37 uint32_t looptime = 0;
michael@0 38 for (uint32_t i = 0; i < mFrameBlender.GetNumFrames(); ++i) {
michael@0 39 int32_t timeout = mFrameBlender.GetTimeoutForFrame(i);
michael@0 40 if (timeout >= 0) {
michael@0 41 looptime += static_cast<uint32_t>(timeout);
michael@0 42 } else {
michael@0 43 // If we have a frame that never times out, we're probably in an error
michael@0 44 // case, but let's handle it more gracefully.
michael@0 45 NS_WARNING("Negative frame timeout - how did this happen?");
michael@0 46 return -1;
michael@0 47 }
michael@0 48 }
michael@0 49
michael@0 50 return looptime;
michael@0 51 }
michael@0 52
michael@0 53 TimeStamp
michael@0 54 FrameAnimator::GetCurrentImgFrameEndTime() const
michael@0 55 {
michael@0 56 TimeStamp currentFrameTime = mCurrentAnimationFrameTime;
michael@0 57 int32_t timeout = mFrameBlender.GetTimeoutForFrame(mCurrentAnimationFrameIndex);
michael@0 58
michael@0 59 if (timeout < 0) {
michael@0 60 // We need to return a sentinel value in this case, because our logic
michael@0 61 // doesn't work correctly if we have a negative timeout value. We use
michael@0 62 // one year in the future as the sentinel because it works with the loop
michael@0 63 // in RequestRefresh() below.
michael@0 64 // XXX(seth): It'd be preferable to make our logic work correctly with
michael@0 65 // negative timeouts.
michael@0 66 return TimeStamp::NowLoRes() +
michael@0 67 TimeDuration::FromMilliseconds(31536000.0);
michael@0 68 }
michael@0 69
michael@0 70 TimeDuration durationOfTimeout =
michael@0 71 TimeDuration::FromMilliseconds(static_cast<double>(timeout));
michael@0 72 TimeStamp currentFrameEndTime = currentFrameTime + durationOfTimeout;
michael@0 73
michael@0 74 return currentFrameEndTime;
michael@0 75 }
michael@0 76
michael@0 77 FrameAnimator::RefreshResult
michael@0 78 FrameAnimator::AdvanceFrame(TimeStamp aTime)
michael@0 79 {
michael@0 80 NS_ASSERTION(aTime <= TimeStamp::Now(),
michael@0 81 "Given time appears to be in the future");
michael@0 82
michael@0 83 uint32_t currentFrameIndex = mCurrentAnimationFrameIndex;
michael@0 84 uint32_t nextFrameIndex = currentFrameIndex + 1;
michael@0 85 int32_t timeout = 0;
michael@0 86
michael@0 87 RefreshResult ret;
michael@0 88
michael@0 89 // If we're done decoding, we know we've got everything we're going to get.
michael@0 90 // If we aren't, we only display fully-downloaded frames; everything else
michael@0 91 // gets delayed.
michael@0 92 bool canDisplay = mDoneDecoding ||
michael@0 93 (mFrameBlender.RawGetFrame(nextFrameIndex) &&
michael@0 94 mFrameBlender.RawGetFrame(nextFrameIndex)->ImageComplete());
michael@0 95
michael@0 96 if (!canDisplay) {
michael@0 97 // Uh oh, the frame we want to show is currently being decoded (partial)
michael@0 98 // Wait until the next refresh driver tick and try again
michael@0 99 return ret;
michael@0 100 }
michael@0 101
michael@0 102 // If we're done decoding the next frame, go ahead and display it now and
michael@0 103 // reinit with the next frame's delay time.
michael@0 104 if (mFrameBlender.GetNumFrames() == nextFrameIndex) {
michael@0 105 // End of an animation loop...
michael@0 106
michael@0 107 // If we are not looping forever, initialize the loop counter
michael@0 108 if (mLoopCounter < 0 && mFrameBlender.GetLoopCount() >= 0) {
michael@0 109 mLoopCounter = mFrameBlender.GetLoopCount();
michael@0 110 }
michael@0 111
michael@0 112 // If animation mode is "loop once", or we're at end of loop counter, it's time to stop animating
michael@0 113 if (mAnimationMode == imgIContainer::kLoopOnceAnimMode || mLoopCounter == 0) {
michael@0 114 ret.animationFinished = true;
michael@0 115 }
michael@0 116
michael@0 117 nextFrameIndex = 0;
michael@0 118
michael@0 119 if (mLoopCounter > 0) {
michael@0 120 mLoopCounter--;
michael@0 121 }
michael@0 122
michael@0 123 // If we're done, exit early.
michael@0 124 if (ret.animationFinished) {
michael@0 125 return ret;
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 timeout = mFrameBlender.GetTimeoutForFrame(nextFrameIndex);
michael@0 130
michael@0 131 // Bad data
michael@0 132 if (timeout < 0) {
michael@0 133 ret.animationFinished = true;
michael@0 134 ret.error = true;
michael@0 135 }
michael@0 136
michael@0 137 if (nextFrameIndex == 0) {
michael@0 138 ret.dirtyRect = mFirstFrameRefreshArea;
michael@0 139 } else {
michael@0 140 // Change frame
michael@0 141 if (!mFrameBlender.DoBlend(&ret.dirtyRect, currentFrameIndex, nextFrameIndex)) {
michael@0 142 // something went wrong, move on to next
michael@0 143 NS_WARNING("FrameAnimator::AdvanceFrame(): Compositing of frame failed");
michael@0 144 mFrameBlender.RawGetFrame(nextFrameIndex)->SetCompositingFailed(true);
michael@0 145 mCurrentAnimationFrameTime = GetCurrentImgFrameEndTime();
michael@0 146 mCurrentAnimationFrameIndex = nextFrameIndex;
michael@0 147
michael@0 148 ret.error = true;
michael@0 149 return ret;
michael@0 150 }
michael@0 151
michael@0 152 mFrameBlender.RawGetFrame(nextFrameIndex)->SetCompositingFailed(false);
michael@0 153 }
michael@0 154
michael@0 155 mCurrentAnimationFrameTime = GetCurrentImgFrameEndTime();
michael@0 156
michael@0 157 // If we can get closer to the current time by a multiple of the image's loop
michael@0 158 // time, we should.
michael@0 159 uint32_t loopTime = GetSingleLoopTime();
michael@0 160 if (loopTime > 0) {
michael@0 161 TimeDuration delay = aTime - mCurrentAnimationFrameTime;
michael@0 162 if (delay.ToMilliseconds() > loopTime) {
michael@0 163 // Explicitly use integer division to get the floor of the number of
michael@0 164 // loops.
michael@0 165 uint32_t loops = static_cast<uint32_t>(delay.ToMilliseconds()) / loopTime;
michael@0 166 mCurrentAnimationFrameTime += TimeDuration::FromMilliseconds(loops * loopTime);
michael@0 167 }
michael@0 168 }
michael@0 169
michael@0 170 // Set currentAnimationFrameIndex at the last possible moment
michael@0 171 mCurrentAnimationFrameIndex = nextFrameIndex;
michael@0 172
michael@0 173 // If we're here, we successfully advanced the frame.
michael@0 174 ret.frameAdvanced = true;
michael@0 175
michael@0 176 return ret;
michael@0 177 }
michael@0 178
michael@0 179 FrameAnimator::RefreshResult
michael@0 180 FrameAnimator::RequestRefresh(const mozilla::TimeStamp& aTime)
michael@0 181 {
michael@0 182 // only advance the frame if the current time is greater than or
michael@0 183 // equal to the current frame's end time.
michael@0 184 TimeStamp currentFrameEndTime = GetCurrentImgFrameEndTime();
michael@0 185
michael@0 186 // By default, an empty RefreshResult.
michael@0 187 RefreshResult ret;
michael@0 188
michael@0 189 while (currentFrameEndTime <= aTime) {
michael@0 190 TimeStamp oldFrameEndTime = currentFrameEndTime;
michael@0 191
michael@0 192 RefreshResult frameRes = AdvanceFrame(aTime);
michael@0 193
michael@0 194 // Accumulate our result for returning to callers.
michael@0 195 ret.Accumulate(frameRes);
michael@0 196
michael@0 197 currentFrameEndTime = GetCurrentImgFrameEndTime();
michael@0 198
michael@0 199 // if we didn't advance a frame, and our frame end time didn't change,
michael@0 200 // then we need to break out of this loop & wait for the frame(s)
michael@0 201 // to finish downloading
michael@0 202 if (!frameRes.frameAdvanced && (currentFrameEndTime == oldFrameEndTime)) {
michael@0 203 break;
michael@0 204 }
michael@0 205 }
michael@0 206
michael@0 207 return ret;
michael@0 208 }
michael@0 209
michael@0 210 void
michael@0 211 FrameAnimator::ResetAnimation()
michael@0 212 {
michael@0 213 mCurrentAnimationFrameIndex = 0;
michael@0 214 }
michael@0 215
michael@0 216 void
michael@0 217 FrameAnimator::SetDoneDecoding(bool aDone)
michael@0 218 {
michael@0 219 mDoneDecoding = aDone;
michael@0 220 }
michael@0 221
michael@0 222 void
michael@0 223 FrameAnimator::SetAnimationMode(uint16_t aAnimationMode)
michael@0 224 {
michael@0 225 mAnimationMode = aAnimationMode;
michael@0 226 }
michael@0 227
michael@0 228 void
michael@0 229 FrameAnimator::InitAnimationFrameTimeIfNecessary()
michael@0 230 {
michael@0 231 if (mCurrentAnimationFrameTime.IsNull()) {
michael@0 232 mCurrentAnimationFrameTime = TimeStamp::Now();
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 void
michael@0 237 FrameAnimator::SetAnimationFrameTime(const TimeStamp& aTime)
michael@0 238 {
michael@0 239 mCurrentAnimationFrameTime = aTime;
michael@0 240 }
michael@0 241
michael@0 242 void
michael@0 243 FrameAnimator::SetFirstFrameRefreshArea(const nsIntRect& aRect)
michael@0 244 {
michael@0 245 mFirstFrameRefreshArea = aRect;
michael@0 246 }
michael@0 247
michael@0 248 void
michael@0 249 FrameAnimator::UnionFirstFrameRefreshArea(const nsIntRect& aRect)
michael@0 250 {
michael@0 251 mFirstFrameRefreshArea.UnionRect(mFirstFrameRefreshArea, aRect);
michael@0 252 }
michael@0 253
michael@0 254 uint32_t
michael@0 255 FrameAnimator::GetCurrentAnimationFrameIndex() const
michael@0 256 {
michael@0 257 return mCurrentAnimationFrameIndex;
michael@0 258 }
michael@0 259
michael@0 260 nsIntRect
michael@0 261 FrameAnimator::GetFirstFrameRefreshArea() const
michael@0 262 {
michael@0 263 return mFirstFrameRefreshArea;
michael@0 264 }
michael@0 265
michael@0 266

mercurial