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