aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/android
diff options
context:
space:
mode:
authorGravatar Leon Scroggins III <scroggo@google.com>2018-01-20 10:33:24 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-01-22 18:53:47 +0000
commit4c11945a971f18177aa494b773b24d88a942bba6 (patch)
treed413112fe36318bc3dd201fb298413bbf7c5cb27 /src/android
parent19dd8fbabfbf38644c47af4b0feac2392e9049fc (diff)
Respect repetition count in SkAnimatedImage
Bug: b/63908092 By default use the repetition count stored in the encoded data (if any). Allow setting the repetition count manually, so that the animation will stop after n+1 total cycles (unless -1 is used for infinite). If the animation is complete, make start reset it. When the animation is not running, make update return max double (i.e. no need to update any time soon). Fix a bug where the first call to update returned -1. Share write_bm with CodecAnimTest, for debugging. Update Sample to check isRunning rather than keeping its own record of whether the animation is running. Change-Id: I883e4d7325f7a7b23a422fa9d756f9ea3018f0f8 Reviewed-on: https://skia-review.googlesource.com/97082 Reviewed-by: Derek Sollenberger <djsollen@google.com> Commit-Queue: Leon Scroggins <scroggo@google.com>
Diffstat (limited to 'src/android')
-rw-r--r--src/android/SkAnimatedImage.cpp85
1 files changed, 66 insertions, 19 deletions
diff --git a/src/android/SkAnimatedImage.cpp b/src/android/SkAnimatedImage.cpp
index 4c87083957..577aa725d2 100644
--- a/src/android/SkAnimatedImage.cpp
+++ b/src/android/SkAnimatedImage.cpp
@@ -68,12 +68,15 @@ SkAnimatedImage::SkAnimatedImage(std::unique_ptr<SkAndroidCodec> codec, SkISize
, fDecodeInfo(decodeInfo)
, fCropRect(cropRect)
, fPostProcess(std::move(postProcess))
+ , fFrameCount(fCodec->codec()->getFrameCount())
, fSimple(fScaledSize == fDecodeInfo.dimensions() && !fPostProcess
&& fCropRect == fDecodeInfo.bounds())
, fFinished(false)
, fRunning(false)
, fNowMS(kInit)
, fRemainingMS(kInit)
+ , fRepetitionCount(fCodec->codec()->getRepetitionCount())
+ , fRepetitionsCompleted(0)
{
if (!fActiveFrame.fBitmap.tryAllocPixels(fDecodeInfo)) {
return;
@@ -113,6 +116,9 @@ bool SkAnimatedImage::Frame::copyTo(Frame* dst) const {
void SkAnimatedImage::start() {
fRunning = true;
+ if (fFinished) {
+ this->reset();
+ }
}
void SkAnimatedImage::stop() {
@@ -120,6 +126,8 @@ void SkAnimatedImage::stop() {
}
void SkAnimatedImage::reset() {
+ fFinished = false;
+ fRepetitionsCompleted = 0;
this->update(kInit);
}
@@ -127,6 +135,30 @@ static bool is_restore_previous(SkCodecAnimation::DisposalMethod dispose) {
return SkCodecAnimation::DisposalMethod::kRestorePrevious == dispose;
}
+int SkAnimatedImage::computeNextFrame(int current, bool* animationEnded) {
+ SkASSERT(animationEnded != nullptr);
+ *animationEnded = false;
+
+ const int frameToDecode = current + 1;
+ if (frameToDecode == fFrameCount - 1) {
+ // Final frame. Check to determine whether to stop.
+ fRepetitionsCompleted++;
+ if (fRepetitionCount != SkCodec::kRepetitionCountInfinite
+ && fRepetitionsCompleted > fRepetitionCount) {
+ *animationEnded = true;
+ }
+ } else if (frameToDecode == fFrameCount) {
+ return 0;
+ }
+ return frameToDecode;
+}
+
+double SkAnimatedImage::finish() {
+ fFinished = true;
+ fRunning = false;
+ return std::numeric_limits<double>::max();
+}
+
double SkAnimatedImage::update(double msecs) {
if (fFinished) {
return std::numeric_limits<double>::max();
@@ -136,19 +168,25 @@ double SkAnimatedImage::update(double msecs) {
fNowMS = msecs;
const double msSinceLastUpdate = fNowMS - lastUpdateMS;
- const int frameCount = fCodec->codec()->getFrameCount();
+ bool animationEnded = false;
int frameToDecode = SkCodec::kNone;
if (kInit == msecs) {
frameToDecode = 0;
+ fNowMS = lastUpdateMS;
} else {
- if (!fRunning || lastUpdateMS == kInit) {
- return kInit;
+ if (!fRunning) {
+ return std::numeric_limits<double>::max();
}
+
+ if (lastUpdateMS == kInit) {
+ return fRemainingMS + fNowMS;
+ }
+
if (msSinceLastUpdate < fRemainingMS) {
fRemainingMS -= msSinceLastUpdate;
return fRemainingMS + fNowMS;
} else {
- frameToDecode = (fActiveFrame.fIndex + 1) % frameCount;
+ frameToDecode = this->computeNextFrame(fActiveFrame.fIndex, &animationEnded);
}
}
@@ -156,8 +194,7 @@ double SkAnimatedImage::update(double msecs) {
if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) {
if (!frameInfo.fFullyReceived) {
SkCodecPrintf("Frame %i not fully received\n", frameToDecode);
- fFinished = true;
- return std::numeric_limits<double>::max();
+ return this->finish();
}
if (kInit == msecs) {
@@ -168,22 +205,22 @@ double SkAnimatedImage::update(double msecs) {
while (pastUpdate >= frameInfo.fDuration) {
SkCodecPrintf("Skipping frame %i\n", frameToDecode);
pastUpdate -= frameInfo.fDuration;
- frameToDecode = (frameToDecode + 1) % frameCount;
+ frameToDecode = computeNextFrame(frameToDecode, &animationEnded);
if (!fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) {
SkCodecPrintf("Could not getFrameInfo for frame %i",
frameToDecode);
// Prior call to getFrameInfo succeeded, so use that one.
frameToDecode--;
- fFinished = true;
+ animationEnded = true;
if (frameToDecode < 0) {
- return std::numeric_limits<double>::max();
+ return this->finish();
}
}
}
fRemainingMS = frameInfo.fDuration - pastUpdate;
}
} else {
- fFinished = true;
+ animationEnded = true;
if (0 == frameToDecode) {
// Static image. This is okay.
frameInfo.fRequiredFrame = SkCodec::kNone;
@@ -194,16 +231,22 @@ double SkAnimatedImage::update(double msecs) {
} else {
SkCodecPrintf("Error getting frameInfo for frame %i\n",
frameToDecode);
- return std::numeric_limits<double>::max();
+ return this->finish();
}
}
if (frameToDecode == fActiveFrame.fIndex) {
+ if (animationEnded) {
+ return this->finish();
+ }
return fRemainingMS + fNowMS;
}
if (frameToDecode == fRestoreFrame.fIndex) {
SkTSwap(fActiveFrame, fRestoreFrame);
+ if (animationEnded) {
+ return this->finish();
+ }
return fRemainingMS + fNowMS;
}
@@ -245,8 +288,7 @@ double SkAnimatedImage::update(double msecs) {
SkTSwap(fActiveFrame, fRestoreFrame);
} else if (!fRestoreFrame.copyTo(&fActiveFrame)) {
SkCodecPrintf("Failed to restore frame\n");
- fFinished = true;
- return std::numeric_limits<double>::max();
+ return this->finish();
}
options.fPriorFrame = fActiveFrame.fIndex;
}
@@ -260,22 +302,23 @@ double SkAnimatedImage::update(double msecs) {
} else {
auto info = fDecodeInfo.makeAlphaType(alphaType);
if (!dst->tryAllocPixels(info)) {
- fFinished = true;
- return std::numeric_limits<double>::max();
+ return this->finish();
}
}
auto result = fCodec->codec()->getPixels(dst->info(), dst->getPixels(), dst->rowBytes(),
&options);
if (result != SkCodec::kSuccess) {
- SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, frameCount);
- // Reset to the beginning.
- fActiveFrame.fIndex = SkCodec::kNone;
- return 0.0;
+ SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, fFrameCount);
+ return this->finish();
}
fActiveFrame.fIndex = frameToDecode;
fActiveFrame.fDisposalMethod = frameInfo.fDisposalMethod;
+
+ if (animationEnded) {
+ return this->finish();
+ }
return fRemainingMS + fNowMS;
}
@@ -302,3 +345,7 @@ void SkAnimatedImage::onDraw(SkCanvas* canvas) {
canvas->restore();
}
}
+
+void SkAnimatedImage::setRepetitionCount(int newCount) {
+ fRepetitionCount = newCount;
+}