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