blob: 4c87596b7efd349df34395f6888f03b6dab6104e [file] [log] [blame]
Leon Scroggins III7a10b332018-01-12 11:24:30 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Scroggins7d1153f2020-11-06 17:05:36 -050017#include "src/core/SkPixmapPriv.h"
Leon Scroggins III7a10b332018-01-12 11:24:30 -050018
Ben Wagner0b9b1f12019-04-04 18:00:05 -040019#include <limits.h>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040020#include <utility>
21
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050022sk_sp<SkAnimatedImage> SkAnimatedImage::Make(std::unique_ptr<SkAndroidCodec> codec,
Leon Scroggins III85463352019-02-04 15:22:10 -050023 const SkImageInfo& requestedInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess) {
24 if (!codec) {
25 return nullptr;
26 }
27
Leon Scrogginsafdbd9d2020-11-06 11:07:37 -050028 if (!requestedInfo.bounds().contains(cropRect)) {
29 return nullptr;
30 }
31
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050032 auto image = sk_sp<SkAnimatedImage>(new SkAnimatedImage(std::move(codec), requestedInfo,
33 cropRect, std::move(postProcess)));
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -040034 if (!image->fDisplayFrame.fBitmap.getPixels()) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -050035 // tryAllocPixels failed.
36 return nullptr;
37 }
38
39 return image;
40}
41
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050042sk_sp<SkAnimatedImage> SkAnimatedImage::Make(std::unique_ptr<SkAndroidCodec> codec) {
43 if (!codec) {
44 return nullptr;
45 }
46
Leon Scroggins III7c40b672021-05-10 16:27:02 -040047 auto decodeInfo = codec->getInfo();
48 const auto origin = codec->codec()->getOrigin();
49 if (SkEncodedOriginSwapsWidthHeight(origin)) {
50 decodeInfo = decodeInfo.makeWH(decodeInfo.height(), decodeInfo.width());
51 }
52 const auto cropRect = SkIRect::MakeSize(decodeInfo.dimensions());
Leon Scroggins IIIe4271602020-12-15 15:52:39 -050053 return Make(std::move(codec), decodeInfo, cropRect, nullptr);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050054}
55
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050056SkAnimatedImage::SkAnimatedImage(std::unique_ptr<SkAndroidCodec> codec,
57 const SkImageInfo& requestedInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050058 : fCodec(std::move(codec))
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050059 , fDecodeInfo(requestedInfo)
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050060 , fCropRect(cropRect)
61 , fPostProcess(std::move(postProcess))
Leon Scroggins III4c119452018-01-20 10:33:24 -050062 , fFrameCount(fCodec->codec()->getFrameCount())
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050063 , fSampleSize(1)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050064 , fFinished(false)
Leon Scroggins III4c119452018-01-20 10:33:24 -050065 , fRepetitionCount(fCodec->codec()->getRepetitionCount())
66 , fRepetitionsCompleted(0)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050067{
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050068 auto scaledSize = requestedInfo.dimensions();
69
Leon Scroggins7d1153f2020-11-06 17:05:36 -050070 // For simplicity in decoding and compositing frames, decode directly to a size and
71 // orientation that fCodec can do directly, and then use fMatrix to handle crop (along with a
72 // clip), orientation, and scaling outside of fCodec. The matrices are computed individually
73 // and applied in the following order:
74 // [crop] X [origin] X [scale]
75 const auto origin = fCodec->codec()->getOrigin();
Leon Scroggins IIIe4271602020-12-15 15:52:39 -050076 if (origin != SkEncodedOrigin::kDefault_SkEncodedOrigin) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -050077 // The origin is applied after scaling, so use scaledSize, which is the final scaled size.
78 fMatrix = SkEncodedOriginToMatrix(origin, scaledSize.width(), scaledSize.height());
79
Leon Scroggins IIIe4271602020-12-15 15:52:39 -050080 if (SkEncodedOriginSwapsWidthHeight(origin)) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -050081 // The client asked for sizes post-rotation. Swap back to the pre-rotation sizes to pass
82 // to fCodec and for the scale matrix computation.
83 fDecodeInfo = SkPixmapPriv::SwapWidthHeight(fDecodeInfo);
84 scaledSize = { scaledSize.height(), scaledSize.width() };
85 }
86 }
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050087
88 auto decodeSize = scaledSize;
89 fSampleSize = fCodec->computeSampleSize(&decodeSize);
90 fDecodeInfo = fDecodeInfo.makeDimensions(decodeSize);
91
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -040092 if (!fDecodingFrame.fBitmap.tryAllocPixels(fDecodeInfo)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -050093 return;
94 }
95
Leon Scroggins7d1153f2020-11-06 17:05:36 -050096 if (scaledSize != fDecodeInfo.dimensions()) {
97 float scaleX = (float) scaledSize.width() / fDecodeInfo.width();
98 float scaleY = (float) scaledSize.height() / fDecodeInfo.height();
Mike Reed1f607332020-05-21 12:11:27 -040099 fMatrix.preConcat(SkMatrix::Scale(scaleX, scaleY));
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500100 }
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500101 fMatrix.postConcat(SkMatrix::Translate(-fCropRect.fLeft, -fCropRect.fTop));
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500102 this->decodeNextFrame();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500103}
104
105SkAnimatedImage::~SkAnimatedImage() { }
106
107SkRect SkAnimatedImage::onGetBounds() {
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500108 return SkRect::MakeIWH(fCropRect.width(), fCropRect.height());
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500109}
110
111SkAnimatedImage::Frame::Frame()
Nigel Tao66bc5242018-08-22 10:56:03 +1000112 : fIndex(SkCodec::kNoFrame)
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500113{}
114
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400115bool SkAnimatedImage::Frame::init(const SkImageInfo& info, OnInit onInit) {
116 if (fBitmap.getPixels()) {
117 if (fBitmap.pixelRef()->unique()) {
118 SkAssertResult(fBitmap.setAlphaType(info.alphaType()));
119 return true;
120 }
121
122 // An SkCanvas provided to onDraw is still holding a reference.
123 // Copy before we decode to ensure that we don't overwrite the
124 // expected contents of the image.
125 if (OnInit::kRestoreIfNecessary == onInit) {
126 SkBitmap tmp;
127 if (!tmp.tryAllocPixels(info)) {
128 return false;
129 }
130
131 memcpy(tmp.getPixels(), fBitmap.getPixels(), fBitmap.computeByteSize());
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400132 using std::swap;
133 swap(tmp, fBitmap);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400134 return true;
135 }
136 }
137
138 return fBitmap.tryAllocPixels(info);
139}
140
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500141bool SkAnimatedImage::Frame::copyTo(Frame* dst) const {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400142 if (!dst->init(fBitmap.info(), OnInit::kNoRestore)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500143 return false;
144 }
145
146 memcpy(dst->fBitmap.getPixels(), fBitmap.getPixels(), fBitmap.computeByteSize());
147 dst->fIndex = fIndex;
148 dst->fDisposalMethod = fDisposalMethod;
149 return true;
150}
151
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500152void SkAnimatedImage::reset() {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500153 fFinished = false;
154 fRepetitionsCompleted = 0;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400155 if (fDisplayFrame.fIndex != 0) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000156 fDisplayFrame.fIndex = SkCodec::kNoFrame;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400157 this->decodeNextFrame();
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500158 }
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500159}
160
161static bool is_restore_previous(SkCodecAnimation::DisposalMethod dispose) {
162 return SkCodecAnimation::DisposalMethod::kRestorePrevious == dispose;
163}
164
Leon Scroggins III4c119452018-01-20 10:33:24 -0500165int SkAnimatedImage::computeNextFrame(int current, bool* animationEnded) {
166 SkASSERT(animationEnded != nullptr);
167 *animationEnded = false;
168
169 const int frameToDecode = current + 1;
170 if (frameToDecode == fFrameCount - 1) {
171 // Final frame. Check to determine whether to stop.
172 fRepetitionsCompleted++;
173 if (fRepetitionCount != SkCodec::kRepetitionCountInfinite
174 && fRepetitionsCompleted > fRepetitionCount) {
175 *animationEnded = true;
176 }
177 } else if (frameToDecode == fFrameCount) {
178 return 0;
179 }
180 return frameToDecode;
181}
182
183double SkAnimatedImage::finish() {
184 fFinished = true;
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500185 fCurrentFrameDuration = kFinished;
186 return kFinished;
Leon Scroggins III4c119452018-01-20 10:33:24 -0500187}
188
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500189int SkAnimatedImage::decodeNextFrame() {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500190 if (fFinished) {
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500191 return kFinished;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500192 }
193
Leon Scroggins III4c119452018-01-20 10:33:24 -0500194 bool animationEnded = false;
Leon Scroggins III0e68f442019-08-28 11:41:02 -0400195 const int frameToDecode = this->computeNextFrame(fDisplayFrame.fIndex, &animationEnded);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500196
197 SkCodec::FrameInfo frameInfo;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500198 if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500199 if (!frameInfo.fFullyReceived) {
200 SkCodecPrintf("Frame %i not fully received\n", frameToDecode);
Leon Scroggins III4c119452018-01-20 10:33:24 -0500201 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500202 }
203
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500204 fCurrentFrameDuration = frameInfo.fDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500205 } else {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500206 animationEnded = true;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500207 if (0 == frameToDecode) {
208 // Static image. This is okay.
Nigel Tao66bc5242018-08-22 10:56:03 +1000209 frameInfo.fRequiredFrame = SkCodec::kNoFrame;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500210 frameInfo.fAlphaType = fCodec->getInfo().alphaType();
Kevin Lubick289d36f2018-02-13 10:25:00 -0500211 frameInfo.fDisposalMethod = SkCodecAnimation::DisposalMethod::kKeep;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500212 // These fields won't be read.
213 frameInfo.fDuration = INT_MAX;
214 frameInfo.fFullyReceived = true;
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500215 fCurrentFrameDuration = kFinished;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500216 } else {
217 SkCodecPrintf("Error getting frameInfo for frame %i\n",
218 frameToDecode);
Leon Scroggins III4c119452018-01-20 10:33:24 -0500219 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500220 }
221 }
222
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400223 if (frameToDecode == fDisplayFrame.fIndex) {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500224 if (animationEnded) {
225 return this->finish();
226 }
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500227 return fCurrentFrameDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500228 }
229
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400230 for (Frame* frame : { &fRestoreFrame, &fDecodingFrame }) {
231 if (frameToDecode == frame->fIndex) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400232 using std::swap;
233 swap(fDisplayFrame, *frame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400234 if (animationEnded) {
235 return this->finish();
236 }
237 return fCurrentFrameDuration;
Leon Scroggins III4c119452018-01-20 10:33:24 -0500238 }
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500239 }
240
241 // The following code makes an effort to avoid overwriting a frame that will
242 // be used again. If frame |i| is_restore_previous, frame |i+1| will not
243 // depend on frame |i|, so do not overwrite frame |i-1|, which may be needed
244 // for frame |i+1|.
245 // We could be even smarter about which frames to save by looking at the
246 // entire dependency chain.
Leon Scrogginsda3d8c22020-11-09 16:00:53 -0500247 SkAndroidCodec::AndroidOptions options;
248 options.fSampleSize = fSampleSize;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500249 options.fFrameIndex = frameToDecode;
Nigel Tao66bc5242018-08-22 10:56:03 +1000250 if (frameInfo.fRequiredFrame == SkCodec::kNoFrame) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500251 if (is_restore_previous(frameInfo.fDisposalMethod)) {
252 // frameToDecode will be discarded immediately after drawing, so
253 // do not overwrite a frame which could possibly be used in the
254 // future.
Nigel Tao66bc5242018-08-22 10:56:03 +1000255 if (fDecodingFrame.fIndex != SkCodec::kNoFrame &&
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400256 !is_restore_previous(fDecodingFrame.fDisposalMethod)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400257 using std::swap;
258 swap(fDecodingFrame, fRestoreFrame);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500259 }
260 }
261 } else {
262 auto validPriorFrame = [&frameInfo, &frameToDecode](const Frame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000263 if (SkCodec::kNoFrame == frame.fIndex ||
264 is_restore_previous(frame.fDisposalMethod)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500265 return false;
266 }
267
268 return frame.fIndex >= frameInfo.fRequiredFrame && frame.fIndex < frameToDecode;
269 };
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400270 if (validPriorFrame(fDecodingFrame)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500271 if (is_restore_previous(frameInfo.fDisposalMethod)) {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400272 // fDecodingFrame is a good frame to use for this one, but we
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500273 // don't want to overwrite it.
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400274 fDecodingFrame.copyTo(&fRestoreFrame);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500275 }
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400276 options.fPriorFrame = fDecodingFrame.fIndex;
277 } else if (validPriorFrame(fDisplayFrame)) {
278 if (!fDisplayFrame.copyTo(&fDecodingFrame)) {
279 SkCodecPrintf("Failed to allocate pixels for frame\n");
280 return this->finish();
281 }
282 options.fPriorFrame = fDecodingFrame.fIndex;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500283 } else if (validPriorFrame(fRestoreFrame)) {
284 if (!is_restore_previous(frameInfo.fDisposalMethod)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400285 using std::swap;
286 swap(fDecodingFrame, fRestoreFrame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400287 } else if (!fRestoreFrame.copyTo(&fDecodingFrame)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500288 SkCodecPrintf("Failed to restore frame\n");
Leon Scroggins III4c119452018-01-20 10:33:24 -0500289 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500290 }
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400291 options.fPriorFrame = fDecodingFrame.fIndex;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500292 }
293 }
294
295 auto alphaType = kOpaque_SkAlphaType == frameInfo.fAlphaType ?
296 kOpaque_SkAlphaType : kPremul_SkAlphaType;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400297 auto info = fDecodeInfo.makeAlphaType(alphaType);
298 SkBitmap* dst = &fDecodingFrame.fBitmap;
299 if (!fDecodingFrame.init(info, Frame::OnInit::kRestoreIfNecessary)) {
300 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500301 }
302
Leon Scrogginsda3d8c22020-11-09 16:00:53 -0500303 auto result = fCodec->getAndroidPixels(dst->info(), dst->getPixels(), dst->rowBytes(),
304 &options);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500305 if (result != SkCodec::kSuccess) {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500306 SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, fFrameCount);
307 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500308 }
309
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400310 fDecodingFrame.fIndex = frameToDecode;
311 fDecodingFrame.fDisposalMethod = frameInfo.fDisposalMethod;
312
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400313 using std::swap;
314 swap(fDecodingFrame, fDisplayFrame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400315 fDisplayFrame.fBitmap.notifyPixelsChanged();
Leon Scroggins III4c119452018-01-20 10:33:24 -0500316
317 if (animationEnded) {
318 return this->finish();
Leon Scroggins III0e68f442019-08-28 11:41:02 -0400319 } else if (fCodec->getEncodedFormat() == SkEncodedImageFormat::kHEIF) {
320 // HEIF doesn't know the frame duration until after decoding. Update to
321 // the correct value. Note that earlier returns in this method either
322 // return kFinished, or fCurrentFrameDuration. If they return the
323 // latter, it is a frame that was previously decoded, so it has the
324 // updated value.
325 if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) {
326 fCurrentFrameDuration = frameInfo.fDuration;
327 } else {
328 SkCodecPrintf("Failed to getFrameInfo on second attempt (HEIF)");
329 }
Leon Scroggins III4c119452018-01-20 10:33:24 -0500330 }
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500331 return fCurrentFrameDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500332}
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500333
334void SkAnimatedImage::onDraw(SkCanvas* canvas) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500335 auto image = this->getCurrentFrameSimple();
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400336
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500337 if (this->simple()) {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400338 canvas->drawImage(image, 0, 0);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500339 return;
340 }
341
342 SkRect bounds = this->getBounds();
343 if (fPostProcess) {
344 canvas->saveLayer(&bounds, nullptr);
345 }
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500346 canvas->clipRect(bounds);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500347 {
Ben Wagner5d1adbf2018-05-28 13:35:39 -0400348 SkAutoCanvasRestore acr(canvas, fPostProcess != nullptr);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500349 canvas->concat(fMatrix);
Mike Reede02d7f82021-01-21 22:25:21 -0500350 canvas->drawImage(image, 0, 0, SkSamplingOptions(SkFilterMode::kLinear), nullptr);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500351 }
352 if (fPostProcess) {
353 canvas->drawPicture(fPostProcess);
354 canvas->restore();
355 }
356}
Leon Scroggins III4c119452018-01-20 10:33:24 -0500357
358void SkAnimatedImage::setRepetitionCount(int newCount) {
359 fRepetitionCount = newCount;
360}
Kevin Lubickf5bc8fb2020-01-08 13:29:31 -0500361
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500362sk_sp<SkImage> SkAnimatedImage::getCurrentFrameSimple() {
Kevin Lubickf5bc8fb2020-01-08 13:29:31 -0500363 // This SkBitmap may be reused later to decode the following frame. But Frame::init
364 // lazily copies the pixel ref if it has any other references. So it is safe to not
365 // do a deep copy here.
366 return SkMakeImageFromRasterBitmap(fDisplayFrame.fBitmap,
367 kNever_SkCopyPixelsMode);
368}
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500369
370sk_sp<SkImage> SkAnimatedImage::getCurrentFrame() {
371 if (this->simple()) return this->getCurrentFrameSimple();
372
373 auto imageInfo = fDisplayFrame.fBitmap.info().makeDimensions(fCropRect.size());
374 if (fPostProcess) {
375 // Defensively use premul in case the post process adds alpha.
376 imageInfo = imageInfo.makeAlphaType(kPremul_SkAlphaType);
377 }
378
379 SkBitmap dst;
380 if (!dst.tryAllocPixels(imageInfo)) {
381 return nullptr;
382 }
383
384 SkCanvas canvas(dst);
385 this->draw(&canvas);
386 return SkMakeImageFromRasterBitmap(dst, kNever_SkCopyPixelsMode);
387}