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