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