blob: bb86afdd6e62b471613563066ee930ef40149795 [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 Scrogginsda3d8c22020-11-09 16:00:53 -050047 const auto& decodeInfo = codec->getInfo();
48 const auto cropRect = SkIRect::MakeSize(decodeInfo.dimensions());
Leon Scroggins IIIe4271602020-12-15 15:52:39 -050049 return Make(std::move(codec), decodeInfo, cropRect, nullptr);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050050}
51
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050052SkAnimatedImage::SkAnimatedImage(std::unique_ptr<SkAndroidCodec> codec,
53 const SkImageInfo& requestedInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050054 : fCodec(std::move(codec))
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050055 , fDecodeInfo(requestedInfo)
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050056 , fCropRect(cropRect)
57 , fPostProcess(std::move(postProcess))
Leon Scroggins III4c119452018-01-20 10:33:24 -050058 , fFrameCount(fCodec->codec()->getFrameCount())
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050059 , fSampleSize(1)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050060 , fFinished(false)
Leon Scroggins III4c119452018-01-20 10:33:24 -050061 , fRepetitionCount(fCodec->codec()->getRepetitionCount())
62 , fRepetitionsCompleted(0)
Leon Scroggins III7a10b332018-01-12 11:24:30 -050063{
Leon Scrogginsda3d8c22020-11-09 16:00:53 -050064 auto scaledSize = requestedInfo.dimensions();
65
Leon Scroggins7d1153f2020-11-06 17:05:36 -050066 // 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 IIIe4271602020-12-15 15:52:39 -050072 if (origin != SkEncodedOrigin::kDefault_SkEncodedOrigin) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -050073 // 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 IIIe4271602020-12-15 15:52:39 -050076 if (SkEncodedOriginSwapsWidthHeight(origin)) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -050077 // 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 Scrogginsda3d8c22020-11-09 16:00:53 -050083
84 auto decodeSize = scaledSize;
85 fSampleSize = fCodec->computeSampleSize(&decodeSize);
86 fDecodeInfo = fDecodeInfo.makeDimensions(decodeSize);
87
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -040088 if (!fDecodingFrame.fBitmap.tryAllocPixels(fDecodeInfo)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -050089 return;
90 }
91
Leon Scroggins7d1153f2020-11-06 17:05:36 -050092 if (scaledSize != fDecodeInfo.dimensions()) {
93 float scaleX = (float) scaledSize.width() / fDecodeInfo.width();
94 float scaleY = (float) scaledSize.height() / fDecodeInfo.height();
Mike Reed1f607332020-05-21 12:11:27 -040095 fMatrix.preConcat(SkMatrix::Scale(scaleX, scaleY));
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -050096 }
Leon Scroggins7d1153f2020-11-06 17:05:36 -050097 fMatrix.postConcat(SkMatrix::Translate(-fCropRect.fLeft, -fCropRect.fTop));
Leon Scroggins III495e0f02018-01-29 19:35:55 -050098 this->decodeNextFrame();
Leon Scroggins III7a10b332018-01-12 11:24:30 -050099}
100
101SkAnimatedImage::~SkAnimatedImage() { }
102
103SkRect SkAnimatedImage::onGetBounds() {
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500104 return SkRect::MakeIWH(fCropRect.width(), fCropRect.height());
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500105}
106
107SkAnimatedImage::Frame::Frame()
Nigel Tao66bc5242018-08-22 10:56:03 +1000108 : fIndex(SkCodec::kNoFrame)
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500109{}
110
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400111bool 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 Wagnerf08d1d02018-06-18 15:11:00 -0400128 using std::swap;
129 swap(tmp, fBitmap);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400130 return true;
131 }
132 }
133
134 return fBitmap.tryAllocPixels(info);
135}
136
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500137bool SkAnimatedImage::Frame::copyTo(Frame* dst) const {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400138 if (!dst->init(fBitmap.info(), OnInit::kNoRestore)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500139 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 III7a10b332018-01-12 11:24:30 -0500148void SkAnimatedImage::reset() {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500149 fFinished = false;
150 fRepetitionsCompleted = 0;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400151 if (fDisplayFrame.fIndex != 0) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000152 fDisplayFrame.fIndex = SkCodec::kNoFrame;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400153 this->decodeNextFrame();
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500154 }
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500155}
156
157static bool is_restore_previous(SkCodecAnimation::DisposalMethod dispose) {
158 return SkCodecAnimation::DisposalMethod::kRestorePrevious == dispose;
159}
160
Leon Scroggins III4c119452018-01-20 10:33:24 -0500161int 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
179double SkAnimatedImage::finish() {
180 fFinished = true;
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500181 fCurrentFrameDuration = kFinished;
182 return kFinished;
Leon Scroggins III4c119452018-01-20 10:33:24 -0500183}
184
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500185int SkAnimatedImage::decodeNextFrame() {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500186 if (fFinished) {
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500187 return kFinished;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500188 }
189
Leon Scroggins III4c119452018-01-20 10:33:24 -0500190 bool animationEnded = false;
Leon Scroggins III0e68f442019-08-28 11:41:02 -0400191 const int frameToDecode = this->computeNextFrame(fDisplayFrame.fIndex, &animationEnded);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500192
193 SkCodec::FrameInfo frameInfo;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500194 if (fCodec->codec()->getFrameInfo(frameToDecode, &frameInfo)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500195 if (!frameInfo.fFullyReceived) {
196 SkCodecPrintf("Frame %i not fully received\n", frameToDecode);
Leon Scroggins III4c119452018-01-20 10:33:24 -0500197 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500198 }
199
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500200 fCurrentFrameDuration = frameInfo.fDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500201 } else {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500202 animationEnded = true;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500203 if (0 == frameToDecode) {
204 // Static image. This is okay.
Nigel Tao66bc5242018-08-22 10:56:03 +1000205 frameInfo.fRequiredFrame = SkCodec::kNoFrame;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500206 frameInfo.fAlphaType = fCodec->getInfo().alphaType();
Kevin Lubick289d36f2018-02-13 10:25:00 -0500207 frameInfo.fDisposalMethod = SkCodecAnimation::DisposalMethod::kKeep;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500208 // These fields won't be read.
209 frameInfo.fDuration = INT_MAX;
210 frameInfo.fFullyReceived = true;
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500211 fCurrentFrameDuration = kFinished;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500212 } else {
213 SkCodecPrintf("Error getting frameInfo for frame %i\n",
214 frameToDecode);
Leon Scroggins III4c119452018-01-20 10:33:24 -0500215 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500216 }
217 }
218
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400219 if (frameToDecode == fDisplayFrame.fIndex) {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500220 if (animationEnded) {
221 return this->finish();
222 }
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500223 return fCurrentFrameDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500224 }
225
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400226 for (Frame* frame : { &fRestoreFrame, &fDecodingFrame }) {
227 if (frameToDecode == frame->fIndex) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400228 using std::swap;
229 swap(fDisplayFrame, *frame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400230 if (animationEnded) {
231 return this->finish();
232 }
233 return fCurrentFrameDuration;
Leon Scroggins III4c119452018-01-20 10:33:24 -0500234 }
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500235 }
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 Scrogginsda3d8c22020-11-09 16:00:53 -0500243 SkAndroidCodec::AndroidOptions options;
244 options.fSampleSize = fSampleSize;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500245 options.fFrameIndex = frameToDecode;
Nigel Tao66bc5242018-08-22 10:56:03 +1000246 if (frameInfo.fRequiredFrame == SkCodec::kNoFrame) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500247 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 Tao66bc5242018-08-22 10:56:03 +1000251 if (fDecodingFrame.fIndex != SkCodec::kNoFrame &&
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400252 !is_restore_previous(fDecodingFrame.fDisposalMethod)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400253 using std::swap;
254 swap(fDecodingFrame, fRestoreFrame);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500255 }
256 }
257 } else {
258 auto validPriorFrame = [&frameInfo, &frameToDecode](const Frame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000259 if (SkCodec::kNoFrame == frame.fIndex ||
260 is_restore_previous(frame.fDisposalMethod)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500261 return false;
262 }
263
264 return frame.fIndex >= frameInfo.fRequiredFrame && frame.fIndex < frameToDecode;
265 };
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400266 if (validPriorFrame(fDecodingFrame)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500267 if (is_restore_previous(frameInfo.fDisposalMethod)) {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400268 // fDecodingFrame is a good frame to use for this one, but we
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500269 // don't want to overwrite it.
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400270 fDecodingFrame.copyTo(&fRestoreFrame);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500271 }
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400272 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 III7a10b332018-01-12 11:24:30 -0500279 } else if (validPriorFrame(fRestoreFrame)) {
280 if (!is_restore_previous(frameInfo.fDisposalMethod)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400281 using std::swap;
282 swap(fDecodingFrame, fRestoreFrame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400283 } else if (!fRestoreFrame.copyTo(&fDecodingFrame)) {
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500284 SkCodecPrintf("Failed to restore frame\n");
Leon Scroggins III4c119452018-01-20 10:33:24 -0500285 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500286 }
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400287 options.fPriorFrame = fDecodingFrame.fIndex;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500288 }
289 }
290
291 auto alphaType = kOpaque_SkAlphaType == frameInfo.fAlphaType ?
292 kOpaque_SkAlphaType : kPremul_SkAlphaType;
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400293 auto info = fDecodeInfo.makeAlphaType(alphaType);
294 SkBitmap* dst = &fDecodingFrame.fBitmap;
295 if (!fDecodingFrame.init(info, Frame::OnInit::kRestoreIfNecessary)) {
296 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500297 }
298
Leon Scrogginsda3d8c22020-11-09 16:00:53 -0500299 auto result = fCodec->getAndroidPixels(dst->info(), dst->getPixels(), dst->rowBytes(),
300 &options);
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500301 if (result != SkCodec::kSuccess) {
Leon Scroggins III4c119452018-01-20 10:33:24 -0500302 SkCodecPrintf("error %i, frame %i of %i\n", result, frameToDecode, fFrameCount);
303 return this->finish();
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500304 }
305
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400306 fDecodingFrame.fIndex = frameToDecode;
307 fDecodingFrame.fDisposalMethod = frameInfo.fDisposalMethod;
308
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400309 using std::swap;
310 swap(fDecodingFrame, fDisplayFrame);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400311 fDisplayFrame.fBitmap.notifyPixelsChanged();
Leon Scroggins III4c119452018-01-20 10:33:24 -0500312
313 if (animationEnded) {
314 return this->finish();
Leon Scroggins III0e68f442019-08-28 11:41:02 -0400315 } 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 III4c119452018-01-20 10:33:24 -0500326 }
Leon Scroggins III495e0f02018-01-29 19:35:55 -0500327 return fCurrentFrameDuration;
Leon Scroggins III7a10b332018-01-12 11:24:30 -0500328}
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500329
330void SkAnimatedImage::onDraw(SkCanvas* canvas) {
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500331 auto image = this->getCurrentFrameSimple();
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400332
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500333 if (this->simple()) {
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400334 canvas->drawImage(image, 0, 0);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500335 return;
336 }
337
338 SkRect bounds = this->getBounds();
339 if (fPostProcess) {
340 canvas->saveLayer(&bounds, nullptr);
341 }
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500342 canvas->clipRect(bounds);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500343 {
Ben Wagner5d1adbf2018-05-28 13:35:39 -0400344 SkAutoCanvasRestore acr(canvas, fPostProcess != nullptr);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500345 canvas->concat(fMatrix);
346 SkPaint paint;
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500347 paint.setFilterQuality(kLow_SkFilterQuality);
Leon Scroggins III4aafb3a2018-05-23 16:15:09 -0400348 canvas->drawImage(image, 0, 0, &paint);
Leon Scroggins IIIb1b7f7012018-01-16 15:26:35 -0500349 }
350 if (fPostProcess) {
351 canvas->drawPicture(fPostProcess);
352 canvas->restore();
353 }
354}
Leon Scroggins III4c119452018-01-20 10:33:24 -0500355
356void SkAnimatedImage::setRepetitionCount(int newCount) {
357 fRepetitionCount = newCount;
358}
Kevin Lubickf5bc8fb2020-01-08 13:29:31 -0500359
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500360sk_sp<SkImage> SkAnimatedImage::getCurrentFrameSimple() {
Kevin Lubickf5bc8fb2020-01-08 13:29:31 -0500361 // 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 Scroggins7d1153f2020-11-06 17:05:36 -0500367
368sk_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}