Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 8 | #include "SkAndroidCodec.h" |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 9 | #include "SkAnimatedImage.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkCodec.h" |
| 12 | #include "SkCodecPriv.h" |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 13 | #include "SkPicture.h" |
| 14 | #include "SkPictureRecorder.h" |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 15 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 16 | sk_sp<SkAnimatedImage> SkAnimatedImage::Make(std::unique_ptr<SkAndroidCodec> codec, |
| 17 | SkISize scaledSize, SkIRect cropRect, sk_sp<SkPicture> postProcess) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 18 | if (!codec) { |
| 19 | return nullptr; |
| 20 | } |
| 21 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 22 | SkISize decodeSize = scaledSize; |
| 23 | auto decodeInfo = codec->getInfo(); |
| 24 | if (codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP |
| 25 | && scaledSize.width() < decodeInfo.width() |
| 26 | && scaledSize.height() < decodeInfo.height()) { |
| 27 | // libwebp can decode to arbitrary smaller sizes. |
| 28 | decodeInfo = decodeInfo.makeWH(decodeSize.width(), decodeSize.height()); |
| 29 | } |
| 30 | |
| 31 | auto image = sk_sp<SkAnimatedImage>(new SkAnimatedImage(std::move(codec), scaledSize, |
| 32 | decodeInfo, cropRect, std::move(postProcess))); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 33 | if (!image->fActiveFrame.fBitmap.getPixels()) { |
| 34 | // tryAllocPixels failed. |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | return image; |
| 39 | } |
| 40 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 41 | sk_sp<SkAnimatedImage> SkAnimatedImage::Make(std::unique_ptr<SkAndroidCodec> codec) { |
| 42 | if (!codec) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
| 46 | const auto decodeInfo = codec->getInfo(); |
| 47 | const auto scaledSize = decodeInfo.dimensions(); |
| 48 | const auto cropRect = SkIRect::MakeSize(scaledSize); |
| 49 | auto image = sk_sp<SkAnimatedImage>(new SkAnimatedImage(std::move(codec), scaledSize, |
| 50 | decodeInfo, cropRect, nullptr)); |
| 51 | |
| 52 | if (!image->fActiveFrame.fBitmap.getPixels()) { |
| 53 | // tryAllocPixels failed. |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | SkASSERT(image->fSimple); |
| 58 | return image; |
| 59 | } |
| 60 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 61 | SkAnimatedImage::SkAnimatedImage(std::unique_ptr<SkAndroidCodec> codec, SkISize scaledSize, |
| 62 | SkImageInfo decodeInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 63 | : fCodec(std::move(codec)) |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 64 | , fScaledSize(scaledSize) |
| 65 | , fDecodeInfo(decodeInfo) |
| 66 | , fCropRect(cropRect) |
| 67 | , fPostProcess(std::move(postProcess)) |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 68 | , fFrameCount(fCodec->codec()->getFrameCount()) |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 69 | , fSimple(fScaledSize == fDecodeInfo.dimensions() && !fPostProcess |
| 70 | && fCropRect == fDecodeInfo.bounds()) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 71 | , fFinished(false) |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 72 | , fRepetitionCount(fCodec->codec()->getRepetitionCount()) |
| 73 | , fRepetitionsCompleted(0) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 74 | { |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 75 | if (!fActiveFrame.fBitmap.tryAllocPixels(fDecodeInfo)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 79 | if (!fSimple) { |
| 80 | fMatrix = SkMatrix::MakeTrans(-fCropRect.fLeft, -fCropRect.fTop); |
| 81 | float scaleX = (float) fScaledSize.width() / fDecodeInfo.width(); |
| 82 | float scaleY = (float) fScaledSize.height() / fDecodeInfo.height(); |
| 83 | fMatrix.preConcat(SkMatrix::MakeScale(scaleX, scaleY)); |
| 84 | } |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 85 | this->decodeNextFrame(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | SkAnimatedImage::~SkAnimatedImage() { } |
| 89 | |
| 90 | SkRect SkAnimatedImage::onGetBounds() { |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 91 | return SkRect::MakeIWH(fCropRect.width(), fCropRect.height()); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SkAnimatedImage::Frame::Frame() |
| 95 | : fIndex(SkCodec::kNone) |
| 96 | {} |
| 97 | |
| 98 | bool SkAnimatedImage::Frame::copyTo(Frame* dst) const { |
| 99 | if (dst->fBitmap.getPixels()) { |
| 100 | dst->fBitmap.setAlphaType(fBitmap.alphaType()); |
| 101 | } else if (!dst->fBitmap.tryAllocPixels(fBitmap.info())) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | memcpy(dst->fBitmap.getPixels(), fBitmap.getPixels(), fBitmap.computeByteSize()); |
| 106 | dst->fIndex = fIndex; |
| 107 | dst->fDisposalMethod = fDisposalMethod; |
| 108 | return true; |
| 109 | } |
| 110 | |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 111 | void SkAnimatedImage::reset() { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 112 | fFinished = false; |
| 113 | fRepetitionsCompleted = 0; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 114 | if (fActiveFrame.fIndex == 0) { |
| 115 | // Already showing the first frame. |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (fRestoreFrame.fIndex == 0) { |
| 120 | SkTSwap(fActiveFrame, fRestoreFrame); |
| 121 | // Now we're showing the first frame. |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | fActiveFrame.fIndex = SkCodec::kNone; |
| 126 | this->decodeNextFrame(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static bool is_restore_previous(SkCodecAnimation::DisposalMethod dispose) { |
| 130 | return SkCodecAnimation::DisposalMethod::kRestorePrevious == dispose; |
| 131 | } |
| 132 | |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 133 | int SkAnimatedImage::computeNextFrame(int current, bool* animationEnded) { |
| 134 | SkASSERT(animationEnded != nullptr); |
| 135 | *animationEnded = false; |
| 136 | |
| 137 | const int frameToDecode = current + 1; |
| 138 | if (frameToDecode == fFrameCount - 1) { |
| 139 | // Final frame. Check to determine whether to stop. |
| 140 | fRepetitionsCompleted++; |
| 141 | if (fRepetitionCount != SkCodec::kRepetitionCountInfinite |
| 142 | && fRepetitionsCompleted > fRepetitionCount) { |
| 143 | *animationEnded = true; |
| 144 | } |
| 145 | } else if (frameToDecode == fFrameCount) { |
| 146 | return 0; |
| 147 | } |
| 148 | return frameToDecode; |
| 149 | } |
| 150 | |
| 151 | double SkAnimatedImage::finish() { |
| 152 | fFinished = true; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 153 | fCurrentFrameDuration = kFinished; |
| 154 | return kFinished; |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 157 | int SkAnimatedImage::decodeNextFrame() { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 158 | if (fFinished) { |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 159 | return kFinished; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 162 | bool animationEnded = false; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 163 | int frameToDecode = this->computeNextFrame(fActiveFrame.fIndex, &animationEnded); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 164 | |
| 165 | SkCodec::FrameInfo frameInfo; |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 166 | if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 167 | if (!frameInfo.fFullyReceived) { |
| 168 | SkCodecPrintf("Frame %i not fully received\n", frameToDecode); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 169 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 172 | fCurrentFrameDuration = frameInfo.fDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 173 | } else { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 174 | animationEnded = true; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 175 | if (0 == frameToDecode) { |
| 176 | // Static image. This is okay. |
| 177 | frameInfo.fRequiredFrame = SkCodec::kNone; |
| 178 | frameInfo.fAlphaType = fCodec->getInfo().alphaType(); |
Kevin Lubick | 289d36f | 2018-02-13 10:25:00 -0500 | [diff] [blame] | 179 | frameInfo.fDisposalMethod = SkCodecAnimation::DisposalMethod::kKeep; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 180 | // These fields won't be read. |
| 181 | frameInfo.fDuration = INT_MAX; |
| 182 | frameInfo.fFullyReceived = true; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 183 | fCurrentFrameDuration = kFinished; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 184 | } else { |
| 185 | SkCodecPrintf("Error getting frameInfo for frame %i\n", |
| 186 | frameToDecode); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 187 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | if (frameToDecode == fActiveFrame.fIndex) { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 192 | if (animationEnded) { |
| 193 | return this->finish(); |
| 194 | } |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 195 | return fCurrentFrameDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (frameToDecode == fRestoreFrame.fIndex) { |
| 199 | SkTSwap(fActiveFrame, fRestoreFrame); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 200 | if (animationEnded) { |
| 201 | return this->finish(); |
| 202 | } |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 203 | return fCurrentFrameDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // The following code makes an effort to avoid overwriting a frame that will |
| 207 | // be used again. If frame |i| is_restore_previous, frame |i+1| will not |
| 208 | // depend on frame |i|, so do not overwrite frame |i-1|, which may be needed |
| 209 | // for frame |i+1|. |
| 210 | // We could be even smarter about which frames to save by looking at the |
| 211 | // entire dependency chain. |
| 212 | SkCodec::Options options; |
| 213 | options.fFrameIndex = frameToDecode; |
| 214 | if (frameInfo.fRequiredFrame == SkCodec::kNone) { |
| 215 | if (is_restore_previous(frameInfo.fDisposalMethod)) { |
| 216 | // frameToDecode will be discarded immediately after drawing, so |
| 217 | // do not overwrite a frame which could possibly be used in the |
| 218 | // future. |
| 219 | if (fActiveFrame.fIndex != SkCodec::kNone && |
| 220 | !is_restore_previous(fActiveFrame.fDisposalMethod)) { |
| 221 | SkTSwap(fActiveFrame, fRestoreFrame); |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | auto validPriorFrame = [&frameInfo, &frameToDecode](const Frame& frame) { |
| 226 | if (SkCodec::kNone == frame.fIndex || is_restore_previous(frame.fDisposalMethod)) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return frame.fIndex >= frameInfo.fRequiredFrame && frame.fIndex < frameToDecode; |
| 231 | }; |
| 232 | if (validPriorFrame(fActiveFrame)) { |
| 233 | if (is_restore_previous(frameInfo.fDisposalMethod)) { |
| 234 | // fActiveFrame is a good frame to use for this one, but we |
| 235 | // don't want to overwrite it. |
| 236 | fActiveFrame.copyTo(&fRestoreFrame); |
| 237 | } |
| 238 | options.fPriorFrame = fActiveFrame.fIndex; |
| 239 | } else if (validPriorFrame(fRestoreFrame)) { |
| 240 | if (!is_restore_previous(frameInfo.fDisposalMethod)) { |
| 241 | SkTSwap(fActiveFrame, fRestoreFrame); |
| 242 | } else if (!fRestoreFrame.copyTo(&fActiveFrame)) { |
| 243 | SkCodecPrintf("Failed to restore frame\n"); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 244 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 245 | } |
| 246 | options.fPriorFrame = fActiveFrame.fIndex; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | auto alphaType = kOpaque_SkAlphaType == frameInfo.fAlphaType ? |
| 251 | kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 252 | SkBitmap* dst = &fActiveFrame.fBitmap; |
| 253 | if (dst->getPixels()) { |
| 254 | SkAssertResult(dst->setAlphaType(alphaType)); |
| 255 | } else { |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 256 | auto info = fDecodeInfo.makeAlphaType(alphaType); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 257 | if (!dst->tryAllocPixels(info)) { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 258 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 262 | auto result = fCodec->codec()->getPixels(dst->info(), dst->getPixels(), dst->rowBytes(), |
| 263 | &options); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 264 | if (result != SkCodec::kSuccess) { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 265 | SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, fFrameCount); |
| 266 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | fActiveFrame.fIndex = frameToDecode; |
| 270 | fActiveFrame.fDisposalMethod = frameInfo.fDisposalMethod; |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 271 | |
| 272 | if (animationEnded) { |
| 273 | return this->finish(); |
| 274 | } |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 275 | return fCurrentFrameDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 276 | } |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 277 | |
| 278 | void SkAnimatedImage::onDraw(SkCanvas* canvas) { |
| 279 | if (fSimple) { |
| 280 | canvas->drawBitmap(fActiveFrame.fBitmap, 0, 0); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | SkRect bounds = this->getBounds(); |
| 285 | if (fPostProcess) { |
| 286 | canvas->saveLayer(&bounds, nullptr); |
| 287 | } |
| 288 | { |
| 289 | SkAutoCanvasRestore acr(canvas, fPostProcess); |
| 290 | canvas->concat(fMatrix); |
| 291 | SkPaint paint; |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 292 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 293 | canvas->drawBitmap(fActiveFrame.fBitmap, 0, 0, &paint); |
| 294 | } |
| 295 | if (fPostProcess) { |
| 296 | canvas->drawPicture(fPostProcess); |
| 297 | canvas->restore(); |
| 298 | } |
| 299 | } |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 300 | |
| 301 | void SkAnimatedImage::setRepetitionCount(int newCount) { |
| 302 | fRepetitionCount = newCount; |
| 303 | } |