blob: 9c57c6ce9b9400bb339b4ff07a632fa1f645560d [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
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 Salomon9241a6d2019-10-03 13:26:54 -040035 decodeInfo = decodeInfo.makeDimensions(dims);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050036 }
37
38 auto image = sk_sp<SkAnimatedImage>(new SkAnimatedImage(std::move(codec), scaledSize,
39 decodeInfo, cropRect, std::move(postProcess)));
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -040040 if (!image->fDisplayFrame.fBitmap.getPixels()) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -050041 // tryAllocPixels failed.
42 return nullptr;
43 }
44
45 return image;
46}
47
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050048sk_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 III4aafb3a2018-05-23 16:15:09 -040059 if (!image->fDisplayFrame.fBitmap.getPixels()) {
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050060 // tryAllocPixels failed.
61 return nullptr;
62 }
63
Leon Scroggins7d1153f2020-11-06 17:05:36 -050064 SkASSERT(image->simple());
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050065 return image;
66}
67
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050068SkAnimatedImage::SkAnimatedImage(std::unique_ptr<SkAndroidCodec> codec, SkISize scaledSize,
69 SkImageInfo decodeInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050070 : fCodec(std::move(codec))
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050071 , fDecodeInfo(decodeInfo)
72 , fCropRect(cropRect)
73 , fPostProcess(std::move(postProcess))
Leon Scroggins III4c119452018-01-20 10:33:24 -050074 , fFrameCount(fCodec->codec()->getFrameCount())
Leon Scroggins III7a10b332018-01-12 11:24:30 -050075 , fFinished(false)
Leon Scroggins III4c119452018-01-20 10:33:24 -050076 , fRepetitionCount(fCodec->codec()->getRepetitionCount())
77 , fRepetitionsCompleted(0)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050078{
Leon Scroggins7d1153f2020-11-06 17:05:36 -050079 // 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 III4aafb3a2018-05-23 16:15:09 -040099 if (!fDecodingFrame.fBitmap.tryAllocPixels(fDecodeInfo)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500100 return;
101 }
102
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500103 if (scaledSize != fDecodeInfo.dimensions()) {
104 float scaleX = (float) scaledSize.width() / fDecodeInfo.width();
105 float scaleY = (float) scaledSize.height() / fDecodeInfo.height();
Mike Reed1f607332020-05-21 12:11:27 -0400106 fMatrix.preConcat(SkMatrix::Scale(scaleX, scaleY));
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500107 }
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500108 fMatrix.postConcat(SkMatrix::Translate(-fCropRect.fLeft, -fCropRect.fTop));
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500109 this->decodeNextFrame();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500110}
111
112SkAnimatedImage::~SkAnimatedImage() { }
113
114SkRect SkAnimatedImage::onGetBounds() {
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500115 return SkRect::MakeIWH(fCropRect.width(), fCropRect.height());
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500116}
117
118SkAnimatedImage::Frame::Frame()
Nigel Tao66bc5242018-08-22 10:56:03 +1000119 : fIndex(SkCodec::kNoFrame)
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500120{}
121
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400122bool 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 Wagnerf08d1d02018-06-18 15:11:00 -0400139 using std::swap;
140 swap(tmp, fBitmap);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400141 return true;
142 }
143 }
144
145 return fBitmap.tryAllocPixels(info);
146}
147
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500148bool SkAnimatedImage::Frame::copyTo(Frame* dst) const {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400149 if (!dst->init(fBitmap.info(), OnInit::kNoRestore)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500150 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 III7a10b332018-01-12 11:24:30 -0500159void SkAnimatedImage::reset() {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500160 fFinished = false;
161 fRepetitionsCompleted = 0;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400162 if (fDisplayFrame.fIndex != 0) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000163 fDisplayFrame.fIndex = SkCodec::kNoFrame;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400164 this->decodeNextFrame();
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500165 }
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500166}
167
168static bool is_restore_previous(SkCodecAnimation::DisposalMethod dispose) {
169 return SkCodecAnimation::DisposalMethod::kRestorePrevious == dispose;
170}
171
Leon Scroggins III4c119452018-01-20 10:33:24 -0500172int 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
190double SkAnimatedImage::finish() {
191 fFinished = true;
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500192 fCurrentFrameDuration = kFinished;
193 return kFinished;
Leon Scroggins III4c119452018-01-20 10:33:24 -0500194}
195
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500196int SkAnimatedImage::decodeNextFrame() {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500197 if (fFinished) {
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500198 return kFinished;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500199 }
200
Leon Scroggins III4c119452018-01-20 10:33:24 -0500201 bool animationEnded = false;
Leon Scroggins III0e68f442019-08-28 11:41:02 -0400202 const int frameToDecode = this->computeNextFrame(fDisplayFrame.fIndex, &animationEnded);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500203
204 SkCodec::FrameInfo frameInfo;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500205 if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500206 if (!frameInfo.fFullyReceived) {
207 SkCodecPrintf("Frame %i not fully received\n", frameToDecode);
Leon Scroggins III4c119452018-01-20 10:33:24 -0500208 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500209 }
210
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500211 fCurrentFrameDuration = frameInfo.fDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500212 } else {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500213 animationEnded = true;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500214 if (0 == frameToDecode) {
215 // Static image. This is okay.
Nigel Tao66bc5242018-08-22 10:56:03 +1000216 frameInfo.fRequiredFrame = SkCodec::kNoFrame;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500217 frameInfo.fAlphaType = fCodec->getInfo().alphaType();
Kevin Lubick289d36f2018-02-13 10:25:00 -0500218 frameInfo.fDisposalMethod = SkCodecAnimation::DisposalMethod::kKeep;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500219 // These fields won't be read.
220 frameInfo.fDuration = INT_MAX;
221 frameInfo.fFullyReceived = true;
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500222 fCurrentFrameDuration = kFinished;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500223 } else {
224 SkCodecPrintf("Error getting frameInfo for frame %i\n",
225 frameToDecode);
Leon Scroggins III4c119452018-01-20 10:33:24 -0500226 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500227 }
228 }
229
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400230 if (frameToDecode == fDisplayFrame.fIndex) {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500231 if (animationEnded) {
232 return this->finish();
233 }
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500234 return fCurrentFrameDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500235 }
236
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400237 for (Frame* frame : { &fRestoreFrame, &fDecodingFrame }) {
238 if (frameToDecode == frame->fIndex) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400239 using std::swap;
240 swap(fDisplayFrame, *frame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400241 if (animationEnded) {
242 return this->finish();
243 }
244 return fCurrentFrameDuration;
Leon Scroggins III4c119452018-01-20 10:33:24 -0500245 }
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500246 }
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 Tao66bc5242018-08-22 10:56:03 +1000256 if (frameInfo.fRequiredFrame == SkCodec::kNoFrame) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500257 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 Tao66bc5242018-08-22 10:56:03 +1000261 if (fDecodingFrame.fIndex != SkCodec::kNoFrame &&
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400262 !is_restore_previous(fDecodingFrame.fDisposalMethod)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400263 using std::swap;
264 swap(fDecodingFrame, fRestoreFrame);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500265 }
266 }
267 } else {
268 auto validPriorFrame = [&frameInfo, &frameToDecode](const Frame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000269 if (SkCodec::kNoFrame == frame.fIndex ||
270 is_restore_previous(frame.fDisposalMethod)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500271 return false;
272 }
273
274 return frame.fIndex >= frameInfo.fRequiredFrame && frame.fIndex < frameToDecode;
275 };
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400276 if (validPriorFrame(fDecodingFrame)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500277 if (is_restore_previous(frameInfo.fDisposalMethod)) {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400278 // fDecodingFrame is a good frame to use for this one, but we
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500279 // don't want to overwrite it.
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400280 fDecodingFrame.copyTo(&fRestoreFrame);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500281 }
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400282 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 III7a10b332018-01-12 11:24:30 -0500289 } else if (validPriorFrame(fRestoreFrame)) {
290 if (!is_restore_previous(frameInfo.fDisposalMethod)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400291 using std::swap;
292 swap(fDecodingFrame, fRestoreFrame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400293 } else if (!fRestoreFrame.copyTo(&fDecodingFrame)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500294 SkCodecPrintf("Failed to restore frame\n");
Leon Scroggins III4c119452018-01-20 10:33:24 -0500295 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500296 }
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400297 options.fPriorFrame = fDecodingFrame.fIndex;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500298 }
299 }
300
301 auto alphaType = kOpaque_SkAlphaType == frameInfo.fAlphaType ?
302 kOpaque_SkAlphaType : kPremul_SkAlphaType;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400303 auto info = fDecodeInfo.makeAlphaType(alphaType);
304 SkBitmap* dst = &fDecodingFrame.fBitmap;
305 if (!fDecodingFrame.init(info, Frame::OnInit::kRestoreIfNecessary)) {
306 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500307 }
308
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500309 auto result = fCodec->codec()->getPixels(dst->info(), dst->getPixels(), dst->rowBytes(),
310 &options);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500311 if (result != SkCodec::kSuccess) {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500312 SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, fFrameCount);
313 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500314 }
315
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400316 fDecodingFrame.fIndex = frameToDecode;
317 fDecodingFrame.fDisposalMethod = frameInfo.fDisposalMethod;
318
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400319 using std::swap;
320 swap(fDecodingFrame, fDisplayFrame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400321 fDisplayFrame.fBitmap.notifyPixelsChanged();
Leon Scroggins III4c119452018-01-20 10:33:24 -0500322
323 if (animationEnded) {
324 return this->finish();
Leon Scroggins III0e68f442019-08-28 11:41:02 -0400325 } 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 III4c119452018-01-20 10:33:24 -0500336 }
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500337 return fCurrentFrameDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500338}
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500339
340void SkAnimatedImage::onDraw(SkCanvas* canvas) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500341 auto image = this->getCurrentFrameSimple();
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400342
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500343 if (this->simple()) {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400344 canvas->drawImage(image, 0, 0);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500345 return;
346 }
347
348 SkRect bounds = this->getBounds();
349 if (fPostProcess) {
350 canvas->saveLayer(&bounds, nullptr);
351 }
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500352 canvas->clipRect(bounds);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500353 {
Ben Wagner5d1adbf2018-05-28 13:35:39 -0400354 SkAutoCanvasRestore acr(canvas, fPostProcess != nullptr);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500355 canvas->concat(fMatrix);
356 SkPaint paint;
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500357 paint.setFilterQuality(kLow_SkFilterQuality);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400358 canvas->drawImage(image, 0, 0, &paint);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500359 }
360 if (fPostProcess) {
361 canvas->drawPicture(fPostProcess);
362 canvas->restore();
363 }
364}
Leon Scroggins III4c119452018-01-20 10:33:24 -0500365
366void SkAnimatedImage::setRepetitionCount(int newCount) {
367 fRepetitionCount = newCount;
368}
Kevin Lubickf5bc8fb2020-01-08 13:29:31 -0500369
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500370sk_sp<SkImage> SkAnimatedImage::getCurrentFrameSimple() {
Kevin Lubickf5bc8fb2020-01-08 13:29:31 -0500371 // 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 Scroggins7d1153f2020-11-06 17:05:36 -0500377
378sk_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}