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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/android/SkAnimatedImage.h" |
| 9 | #include "include/codec/SkAndroidCodec.h" |
| 10 | #include "include/codec/SkCodec.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkPicture.h" |
| 13 | #include "include/core/SkPictureRecorder.h" |
| 14 | #include "include/core/SkPixelRef.h" |
| 15 | #include "src/codec/SkCodecPriv.h" |
| 16 | #include "src/core/SkImagePriv.h" |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 17 | #include "src/core/SkPixmapPriv.h" |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 18 | |
Ben Wagner | 0b9b1f1 | 2019-04-04 18:00:05 -0400 | [diff] [blame] | 19 | #include <limits.h> |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 22 | sk_sp<SkAnimatedImage> SkAnimatedImage::Make(std::unique_ptr<SkAndroidCodec> codec, |
Leon Scroggins III | 8546335 | 2019-02-04 15:22:10 -0500 | [diff] [blame] | 23 | const SkImageInfo& requestedInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess) { |
| 24 | if (!codec) { |
| 25 | return nullptr; |
| 26 | } |
| 27 | |
Leon Scroggins | afdbd9d | 2020-11-06 11:07:37 -0500 | [diff] [blame^] | 28 | if (!requestedInfo.bounds().contains(cropRect)) { |
| 29 | return nullptr; |
| 30 | } |
| 31 | |
Leon Scroggins III | 8546335 | 2019-02-04 15:22:10 -0500 | [diff] [blame] | 32 | auto scaledSize = requestedInfo.dimensions(); |
| 33 | auto decodeInfo = requestedInfo; |
| 34 | if (codec->getEncodedFormat() != SkEncodedImageFormat::kWEBP |
| 35 | || scaledSize.width() >= decodeInfo.width() |
| 36 | || scaledSize.height() >= decodeInfo.height()) { |
| 37 | // Only libwebp can decode to arbitrary smaller sizes. |
| 38 | auto dims = codec->getInfo().dimensions(); |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 39 | decodeInfo = decodeInfo.makeDimensions(dims); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | auto image = sk_sp<SkAnimatedImage>(new SkAnimatedImage(std::move(codec), scaledSize, |
| 43 | decodeInfo, cropRect, std::move(postProcess))); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 44 | if (!image->fDisplayFrame.fBitmap.getPixels()) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 45 | // tryAllocPixels failed. |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
| 49 | return image; |
| 50 | } |
| 51 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 52 | sk_sp<SkAnimatedImage> SkAnimatedImage::Make(std::unique_ptr<SkAndroidCodec> codec) { |
| 53 | if (!codec) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | const auto decodeInfo = codec->getInfo(); |
| 58 | const auto scaledSize = decodeInfo.dimensions(); |
| 59 | const auto cropRect = SkIRect::MakeSize(scaledSize); |
| 60 | auto image = sk_sp<SkAnimatedImage>(new SkAnimatedImage(std::move(codec), scaledSize, |
| 61 | decodeInfo, cropRect, nullptr)); |
| 62 | |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 63 | if (!image->fDisplayFrame.fBitmap.getPixels()) { |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 64 | // tryAllocPixels failed. |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 68 | SkASSERT(image->simple()); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 69 | return image; |
| 70 | } |
| 71 | |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 72 | SkAnimatedImage::SkAnimatedImage(std::unique_ptr<SkAndroidCodec> codec, SkISize scaledSize, |
| 73 | SkImageInfo decodeInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 74 | : fCodec(std::move(codec)) |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 75 | , fDecodeInfo(decodeInfo) |
| 76 | , fCropRect(cropRect) |
| 77 | , fPostProcess(std::move(postProcess)) |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 78 | , fFrameCount(fCodec->codec()->getFrameCount()) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 79 | , fFinished(false) |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 80 | , fRepetitionCount(fCodec->codec()->getRepetitionCount()) |
| 81 | , fRepetitionsCompleted(0) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 82 | { |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 83 | // For simplicity in decoding and compositing frames, decode directly to a size and |
| 84 | // orientation that fCodec can do directly, and then use fMatrix to handle crop (along with a |
| 85 | // clip), orientation, and scaling outside of fCodec. The matrices are computed individually |
| 86 | // and applied in the following order: |
| 87 | // [crop] X [origin] X [scale] |
| 88 | const auto origin = fCodec->codec()->getOrigin(); |
| 89 | if (fCodec->fOrientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kRespect |
| 90 | && origin != SkEncodedOrigin::kDefault_SkEncodedOrigin) { |
| 91 | // The origin is applied after scaling, so use scaledSize, which is the final scaled size. |
| 92 | fMatrix = SkEncodedOriginToMatrix(origin, scaledSize.width(), scaledSize.height()); |
| 93 | |
| 94 | fCodec = SkAndroidCodec::MakeFromCodec(std::move(fCodec->fCodec), |
| 95 | SkAndroidCodec::ExifOrientationBehavior::kIgnore); |
| 96 | if (SkPixmapPriv::ShouldSwapWidthHeight(origin)) { |
| 97 | // The client asked for sizes post-rotation. Swap back to the pre-rotation sizes to pass |
| 98 | // to fCodec and for the scale matrix computation. |
| 99 | fDecodeInfo = SkPixmapPriv::SwapWidthHeight(fDecodeInfo); |
| 100 | scaledSize = { scaledSize.height(), scaledSize.width() }; |
| 101 | } |
| 102 | } |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 103 | if (!fDecodingFrame.fBitmap.tryAllocPixels(fDecodeInfo)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 107 | if (scaledSize != fDecodeInfo.dimensions()) { |
| 108 | float scaleX = (float) scaledSize.width() / fDecodeInfo.width(); |
| 109 | float scaleY = (float) scaledSize.height() / fDecodeInfo.height(); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 110 | fMatrix.preConcat(SkMatrix::Scale(scaleX, scaleY)); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 111 | } |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 112 | fMatrix.postConcat(SkMatrix::Translate(-fCropRect.fLeft, -fCropRect.fTop)); |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 113 | this->decodeNextFrame(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | SkAnimatedImage::~SkAnimatedImage() { } |
| 117 | |
| 118 | SkRect SkAnimatedImage::onGetBounds() { |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 119 | return SkRect::MakeIWH(fCropRect.width(), fCropRect.height()); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | SkAnimatedImage::Frame::Frame() |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 123 | : fIndex(SkCodec::kNoFrame) |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 124 | {} |
| 125 | |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 126 | bool SkAnimatedImage::Frame::init(const SkImageInfo& info, OnInit onInit) { |
| 127 | if (fBitmap.getPixels()) { |
| 128 | if (fBitmap.pixelRef()->unique()) { |
| 129 | SkAssertResult(fBitmap.setAlphaType(info.alphaType())); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | // An SkCanvas provided to onDraw is still holding a reference. |
| 134 | // Copy before we decode to ensure that we don't overwrite the |
| 135 | // expected contents of the image. |
| 136 | if (OnInit::kRestoreIfNecessary == onInit) { |
| 137 | SkBitmap tmp; |
| 138 | if (!tmp.tryAllocPixels(info)) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | memcpy(tmp.getPixels(), fBitmap.getPixels(), fBitmap.computeByteSize()); |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 143 | using std::swap; |
| 144 | swap(tmp, fBitmap); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return fBitmap.tryAllocPixels(info); |
| 150 | } |
| 151 | |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 152 | bool SkAnimatedImage::Frame::copyTo(Frame* dst) const { |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 153 | if (!dst->init(fBitmap.info(), OnInit::kNoRestore)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | |
| 157 | memcpy(dst->fBitmap.getPixels(), fBitmap.getPixels(), fBitmap.computeByteSize()); |
| 158 | dst->fIndex = fIndex; |
| 159 | dst->fDisposalMethod = fDisposalMethod; |
| 160 | return true; |
| 161 | } |
| 162 | |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 163 | void SkAnimatedImage::reset() { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 164 | fFinished = false; |
| 165 | fRepetitionsCompleted = 0; |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 166 | if (fDisplayFrame.fIndex != 0) { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 167 | fDisplayFrame.fIndex = SkCodec::kNoFrame; |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 168 | this->decodeNextFrame(); |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 169 | } |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static bool is_restore_previous(SkCodecAnimation::DisposalMethod dispose) { |
| 173 | return SkCodecAnimation::DisposalMethod::kRestorePrevious == dispose; |
| 174 | } |
| 175 | |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 176 | int SkAnimatedImage::computeNextFrame(int current, bool* animationEnded) { |
| 177 | SkASSERT(animationEnded != nullptr); |
| 178 | *animationEnded = false; |
| 179 | |
| 180 | const int frameToDecode = current + 1; |
| 181 | if (frameToDecode == fFrameCount - 1) { |
| 182 | // Final frame. Check to determine whether to stop. |
| 183 | fRepetitionsCompleted++; |
| 184 | if (fRepetitionCount != SkCodec::kRepetitionCountInfinite |
| 185 | && fRepetitionsCompleted > fRepetitionCount) { |
| 186 | *animationEnded = true; |
| 187 | } |
| 188 | } else if (frameToDecode == fFrameCount) { |
| 189 | return 0; |
| 190 | } |
| 191 | return frameToDecode; |
| 192 | } |
| 193 | |
| 194 | double SkAnimatedImage::finish() { |
| 195 | fFinished = true; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 196 | fCurrentFrameDuration = kFinished; |
| 197 | return kFinished; |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 200 | int SkAnimatedImage::decodeNextFrame() { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 201 | if (fFinished) { |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 202 | return kFinished; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 205 | bool animationEnded = false; |
Leon Scroggins III | 0e68f44 | 2019-08-28 11:41:02 -0400 | [diff] [blame] | 206 | const int frameToDecode = this->computeNextFrame(fDisplayFrame.fIndex, &animationEnded); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 207 | |
| 208 | SkCodec::FrameInfo frameInfo; |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 209 | if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 210 | if (!frameInfo.fFullyReceived) { |
| 211 | SkCodecPrintf("Frame %i not fully received\n", frameToDecode); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 212 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 215 | fCurrentFrameDuration = frameInfo.fDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 216 | } else { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 217 | animationEnded = true; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 218 | if (0 == frameToDecode) { |
| 219 | // Static image. This is okay. |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 220 | frameInfo.fRequiredFrame = SkCodec::kNoFrame; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 221 | frameInfo.fAlphaType = fCodec->getInfo().alphaType(); |
Kevin Lubick | 289d36f | 2018-02-13 10:25:00 -0500 | [diff] [blame] | 222 | frameInfo.fDisposalMethod = SkCodecAnimation::DisposalMethod::kKeep; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 223 | // These fields won't be read. |
| 224 | frameInfo.fDuration = INT_MAX; |
| 225 | frameInfo.fFullyReceived = true; |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 226 | fCurrentFrameDuration = kFinished; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 227 | } else { |
| 228 | SkCodecPrintf("Error getting frameInfo for frame %i\n", |
| 229 | frameToDecode); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 230 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 234 | if (frameToDecode == fDisplayFrame.fIndex) { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 235 | if (animationEnded) { |
| 236 | return this->finish(); |
| 237 | } |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 238 | return fCurrentFrameDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 241 | for (Frame* frame : { &fRestoreFrame, &fDecodingFrame }) { |
| 242 | if (frameToDecode == frame->fIndex) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 243 | using std::swap; |
| 244 | swap(fDisplayFrame, *frame); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 245 | if (animationEnded) { |
| 246 | return this->finish(); |
| 247 | } |
| 248 | return fCurrentFrameDuration; |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 249 | } |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // The following code makes an effort to avoid overwriting a frame that will |
| 253 | // be used again. If frame |i| is_restore_previous, frame |i+1| will not |
| 254 | // depend on frame |i|, so do not overwrite frame |i-1|, which may be needed |
| 255 | // for frame |i+1|. |
| 256 | // We could be even smarter about which frames to save by looking at the |
| 257 | // entire dependency chain. |
| 258 | SkCodec::Options options; |
| 259 | options.fFrameIndex = frameToDecode; |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 260 | if (frameInfo.fRequiredFrame == SkCodec::kNoFrame) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 261 | if (is_restore_previous(frameInfo.fDisposalMethod)) { |
| 262 | // frameToDecode will be discarded immediately after drawing, so |
| 263 | // do not overwrite a frame which could possibly be used in the |
| 264 | // future. |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 265 | if (fDecodingFrame.fIndex != SkCodec::kNoFrame && |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 266 | !is_restore_previous(fDecodingFrame.fDisposalMethod)) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 267 | using std::swap; |
| 268 | swap(fDecodingFrame, fRestoreFrame); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | auto validPriorFrame = [&frameInfo, &frameToDecode](const Frame& frame) { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 273 | if (SkCodec::kNoFrame == frame.fIndex || |
| 274 | is_restore_previous(frame.fDisposalMethod)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
| 278 | return frame.fIndex >= frameInfo.fRequiredFrame && frame.fIndex < frameToDecode; |
| 279 | }; |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 280 | if (validPriorFrame(fDecodingFrame)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 281 | if (is_restore_previous(frameInfo.fDisposalMethod)) { |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 282 | // fDecodingFrame is a good frame to use for this one, but we |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 283 | // don't want to overwrite it. |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 284 | fDecodingFrame.copyTo(&fRestoreFrame); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 285 | } |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 286 | options.fPriorFrame = fDecodingFrame.fIndex; |
| 287 | } else if (validPriorFrame(fDisplayFrame)) { |
| 288 | if (!fDisplayFrame.copyTo(&fDecodingFrame)) { |
| 289 | SkCodecPrintf("Failed to allocate pixels for frame\n"); |
| 290 | return this->finish(); |
| 291 | } |
| 292 | options.fPriorFrame = fDecodingFrame.fIndex; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 293 | } else if (validPriorFrame(fRestoreFrame)) { |
| 294 | if (!is_restore_previous(frameInfo.fDisposalMethod)) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 295 | using std::swap; |
| 296 | swap(fDecodingFrame, fRestoreFrame); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 297 | } else if (!fRestoreFrame.copyTo(&fDecodingFrame)) { |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 298 | SkCodecPrintf("Failed to restore frame\n"); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 299 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 300 | } |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 301 | options.fPriorFrame = fDecodingFrame.fIndex; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | auto alphaType = kOpaque_SkAlphaType == frameInfo.fAlphaType ? |
| 306 | kOpaque_SkAlphaType : kPremul_SkAlphaType; |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 307 | auto info = fDecodeInfo.makeAlphaType(alphaType); |
| 308 | SkBitmap* dst = &fDecodingFrame.fBitmap; |
| 309 | if (!fDecodingFrame.init(info, Frame::OnInit::kRestoreIfNecessary)) { |
| 310 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 313 | auto result = fCodec->codec()->getPixels(dst->info(), dst->getPixels(), dst->rowBytes(), |
| 314 | &options); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 315 | if (result != SkCodec::kSuccess) { |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 316 | SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, fFrameCount); |
| 317 | return this->finish(); |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 320 | fDecodingFrame.fIndex = frameToDecode; |
| 321 | fDecodingFrame.fDisposalMethod = frameInfo.fDisposalMethod; |
| 322 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 323 | using std::swap; |
| 324 | swap(fDecodingFrame, fDisplayFrame); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 325 | fDisplayFrame.fBitmap.notifyPixelsChanged(); |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 326 | |
| 327 | if (animationEnded) { |
| 328 | return this->finish(); |
Leon Scroggins III | 0e68f44 | 2019-08-28 11:41:02 -0400 | [diff] [blame] | 329 | } else if (fCodec->getEncodedFormat() == SkEncodedImageFormat::kHEIF) { |
| 330 | // HEIF doesn't know the frame duration until after decoding. Update to |
| 331 | // the correct value. Note that earlier returns in this method either |
| 332 | // return kFinished, or fCurrentFrameDuration. If they return the |
| 333 | // latter, it is a frame that was previously decoded, so it has the |
| 334 | // updated value. |
| 335 | if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) { |
| 336 | fCurrentFrameDuration = frameInfo.fDuration; |
| 337 | } else { |
| 338 | SkCodecPrintf("Failed to getFrameInfo on second attempt (HEIF)"); |
| 339 | } |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 340 | } |
Leon Scroggins III | 495e0f0 | 2018-01-29 19:35:55 -0500 | [diff] [blame] | 341 | return fCurrentFrameDuration; |
Leon Scroggins III | 7a10b33 | 2018-01-12 11:24:30 -0500 | [diff] [blame] | 342 | } |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 343 | |
| 344 | void SkAnimatedImage::onDraw(SkCanvas* canvas) { |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 345 | auto image = this->getCurrentFrameSimple(); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 346 | |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 347 | if (this->simple()) { |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 348 | canvas->drawImage(image, 0, 0); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
| 352 | SkRect bounds = this->getBounds(); |
| 353 | if (fPostProcess) { |
| 354 | canvas->saveLayer(&bounds, nullptr); |
| 355 | } |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 356 | canvas->clipRect(bounds); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 357 | { |
Ben Wagner | 5d1adbf | 2018-05-28 13:35:39 -0400 | [diff] [blame] | 358 | SkAutoCanvasRestore acr(canvas, fPostProcess != nullptr); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 359 | canvas->concat(fMatrix); |
| 360 | SkPaint paint; |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 361 | paint.setFilterQuality(kLow_SkFilterQuality); |
Leon Scroggins III | 4aafb3a | 2018-05-23 16:15:09 -0400 | [diff] [blame] | 362 | canvas->drawImage(image, 0, 0, &paint); |
Leon Scroggins III | b1b7f701 | 2018-01-16 15:26:35 -0500 | [diff] [blame] | 363 | } |
| 364 | if (fPostProcess) { |
| 365 | canvas->drawPicture(fPostProcess); |
| 366 | canvas->restore(); |
| 367 | } |
| 368 | } |
Leon Scroggins III | 4c11945 | 2018-01-20 10:33:24 -0500 | [diff] [blame] | 369 | |
| 370 | void SkAnimatedImage::setRepetitionCount(int newCount) { |
| 371 | fRepetitionCount = newCount; |
| 372 | } |
Kevin Lubick | f5bc8fb | 2020-01-08 13:29:31 -0500 | [diff] [blame] | 373 | |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 374 | sk_sp<SkImage> SkAnimatedImage::getCurrentFrameSimple() { |
Kevin Lubick | f5bc8fb | 2020-01-08 13:29:31 -0500 | [diff] [blame] | 375 | // This SkBitmap may be reused later to decode the following frame. But Frame::init |
| 376 | // lazily copies the pixel ref if it has any other references. So it is safe to not |
| 377 | // do a deep copy here. |
| 378 | return SkMakeImageFromRasterBitmap(fDisplayFrame.fBitmap, |
| 379 | kNever_SkCopyPixelsMode); |
| 380 | } |
Leon Scroggins | 7d1153f | 2020-11-06 17:05:36 -0500 | [diff] [blame] | 381 | |
| 382 | sk_sp<SkImage> SkAnimatedImage::getCurrentFrame() { |
| 383 | if (this->simple()) return this->getCurrentFrameSimple(); |
| 384 | |
| 385 | auto imageInfo = fDisplayFrame.fBitmap.info().makeDimensions(fCropRect.size()); |
| 386 | if (fPostProcess) { |
| 387 | // Defensively use premul in case the post process adds alpha. |
| 388 | imageInfo = imageInfo.makeAlphaType(kPremul_SkAlphaType); |
| 389 | } |
| 390 | |
| 391 | SkBitmap dst; |
| 392 | if (!dst.tryAllocPixels(imageInfo)) { |
| 393 | return nullptr; |
| 394 | } |
| 395 | |
| 396 | SkCanvas canvas(dst); |
| 397 | this->draw(&canvas); |
| 398 | return SkMakeImageFromRasterBitmap(dst, kNever_SkCopyPixelsMode); |
| 399 | } |