blob: 49ec86410cb190081a19076c215af23ef337fd3e [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
2 * Copyright 2015 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/codec/SkCodec.h"
9#include "include/core/SkColorSpace.h"
10#include "include/core/SkData.h"
11#include "include/private/SkHalf.h"
Mike Klein05f45b22019-04-25 12:13:03 -050012#include "include/private/SkMutex.h"
13#include "src/core/SkSharedMutex.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/codec/SkBmpCodec.h"
15#include "src/codec/SkCodecPriv.h"
16#include "src/codec/SkFrameHolder.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040017#ifdef SK_HAS_HEIF_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/codec/SkHeifCodec.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040019#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/codec/SkIcoCodec.h"
21#include "src/codec/SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070022#ifdef SK_HAS_PNG_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/codec/SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080024#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkStream.h"
26#include "src/codec/SkRawCodec.h"
27#include "src/codec/SkWbmpCodec.h"
28#include "src/codec/SkWebpCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040029#ifdef SK_HAS_WUFFS_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/codec/SkWuffsCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040031#else
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/codec/SkGifCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040033#endif
scroggof24f2242015-03-03 08:59:20 -080034
msarett74114382015-03-16 11:55:18 -070035struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080036 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040037 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070038};
39
Mike Klein05f45b22019-04-25 12:13:03 -050040// Wish we had SK_DECLARE_STATIC_SHARED_MUTEX.
41static SkSharedMutex* decoders_mutex() {
42 static SkSharedMutex* mutex = new SkSharedMutex;
43 return mutex;
44}
45
46static std::vector<DecoderProc>* decoders() {
47 static auto* decoders = new std::vector<DecoderProc> {
48 #ifdef SK_HAS_JPEG_LIBRARY
49 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
50 #endif
51 #ifdef SK_HAS_WEBP_LIBRARY
52 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
53 #endif
54 #ifdef SK_HAS_WUFFS_LIBRARY
55 { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
56 #else
57 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
58 #endif
59 #ifdef SK_HAS_PNG_LIBRARY
60 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
61 #endif
62 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
63 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
64 #ifdef SK_HAS_HEIF_LIBRARY
65 { SkHeifCodec::IsHeif, SkHeifCodec::MakeFromStream },
66 #endif
67 };
68 return decoders;
69}
70
71void SkCodec::Register(
72 bool (*peek)(const void*, size_t),
73 std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)) {
74 SkAutoExclusive lock{*decoders_mutex()};
75 decoders()->push_back(DecoderProc{peek, make});
76}
77
msarett74114382015-03-16 11:55:18 -070078
Mike Reedede7bac2017-07-23 15:30:02 -040079std::unique_ptr<SkCodec> SkCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
80 Result* outResult, SkPngChunkReader* chunkReader) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040081 Result resultStorage;
82 if (!outResult) {
83 outResult = &resultStorage;
84 }
85
scroggof24f2242015-03-03 08:59:20 -080086 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040087 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070088 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080089 }
scroggo0a7e69c2015-04-03 07:22:22 -070090
Leon Scroggins III04be2b52017-08-17 15:13:20 -040091 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080092
93 char buffer[bytesToRead];
94 size_t bytesRead = stream->peek(buffer, bytesToRead);
95
96 // It is also possible to have a complete image less than bytesToRead bytes
97 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
98 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
99 // than bytesToRead, so pass that directly to the decoder.
100 // It also is possible the stream uses too small a buffer for peeking, but
101 // we trust the caller to use a large enough buffer.
102
103 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800104 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
105 // printf could be useful to notice failures.
106 // SkCodecPrintf("Encoded image data failed to peek!\n");
107
scroggodb30be22015-12-08 18:54:13 -0800108 // It is possible the stream does not support peeking, but does support
109 // rewinding.
110 // Attempt to read() and pass the actual amount read to the decoder.
111 bytesRead = stream->read(buffer, bytesToRead);
112 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800113 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400114 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -0800115 return nullptr;
116 }
117 }
118
scroggocf98fa92015-11-23 08:14:40 -0800119 // PNG is special, since we want to be able to supply an SkPngChunkReader.
120 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -0700121#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -0800122 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400123 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -0800124 } else
125#endif
126 {
Mike Klein05f45b22019-04-25 12:13:03 -0500127 SkAutoSharedMutexShared lock{*decoders_mutex()};
128 for (DecoderProc proc : *decoders()) {
scroggodb30be22015-12-08 18:54:13 -0800129 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400130 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800131 }
msarett74114382015-03-16 11:55:18 -0700132 }
yujieqin916de9f2016-01-25 08:26:16 -0800133
134#ifdef SK_CODEC_DECODES_RAW
135 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400136 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800137#endif
scroggof24f2242015-03-03 08:59:20 -0800138 }
msarett8c8f22a2015-04-01 06:58:48 -0700139
Leon Scroggins III588fb042017-07-14 16:32:31 -0400140 if (bytesRead < bytesToRead) {
141 *outResult = kIncompleteInput;
142 } else {
143 *outResult = kUnimplemented;
144 }
145
msarettf44631b2016-01-13 10:54:20 -0800146 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800147}
148
Mike Reedede7bac2017-07-23 15:30:02 -0400149std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800150 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700151 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800152 }
Mike Reed847068c2017-07-26 11:35:53 -0400153 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800154}
155
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400156SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
157 SkEncodedOrigin origin)
158 : fEncodedInfo(std::move(info))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400159 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400160 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700161 , fNeedsRewind(false)
162 , fOrigin(origin)
163 , fDstInfo()
164 , fOptions()
165 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700166 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700167{}
168
scroggo9b2cdbf42015-07-10 12:07:02 -0700169SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700170
Leon Scroggins III712476e2018-10-03 15:47:00 -0400171bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400172 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
173 return false;
174 }
175
176 switch (dst.colorType()) {
177 case kRGBA_8888_SkColorType:
178 case kBGRA_8888_SkColorType:
179 return true;
180 case kRGBA_F16_SkColorType:
Mike Kleince4cf722018-05-10 11:29:15 -0400181 return dst.colorSpace();
Leon Scroggins III07418182017-08-15 12:24:02 -0400182 case kRGB_565_SkColorType:
183 return srcIsOpaque;
184 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400185 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500186 case kAlpha_8_SkColorType:
187 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400188 // all of those other conversions yet.
189 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400190 default:
191 return false;
192 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400193}
194
scroggob427db12015-08-12 07:24:13 -0700195bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800196 // Store the value of fNeedsRewind so we can update it. Next read will
197 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700198 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800199 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700200 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700201 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700202 }
scroggob427db12015-08-12 07:24:13 -0700203
scroggo46c57472015-09-30 08:57:13 -0700204 // startScanlineDecode will need to be called before decoding scanlines.
205 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700206 // startIncrementalDecode will need to be called before incrementalDecode.
207 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700208
scroggo19b91532016-10-24 09:03:26 -0700209 // Some codecs do not have a stream. They may hold onto their own data or another codec.
210 // They must handle rewinding themselves.
211 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700212 return false;
213 }
214
215 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800216}
scroggo05245902015-03-25 11:11:52 -0700217
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400218bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
219 SkISize srcDimensions, SkIRect prevRect) {
220 const auto dimensions = dstInfo.dimensions();
221 if (dimensions != srcDimensions) {
222 SkRect src = SkRect::Make(srcDimensions);
223 SkRect dst = SkRect::Make(dimensions);
224 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
225 SkRect asRect = SkRect::Make(prevRect);
226 if (!map.mapRect(&asRect)) {
227 return false;
228 }
229 asRect.roundIn(&prevRect);
230 if (prevRect.isEmpty()) {
231 // Down-scaling shrank the empty portion to nothing,
232 // so nothing to zero.
233 return true;
234 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400235 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400236
237 if (!prevRect.intersect(dstInfo.bounds())) {
238 SkCodecPrintf("rectangles do not intersect!");
239 SkASSERT(false);
240 return true;
241 }
242
243 const SkImageInfo info = dstInfo.makeWH(prevRect.width(), prevRect.height());
Mike Reed7fcfb622018-02-09 13:26:46 -0500244 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400245 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
246 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400247 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400248 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400249}
250
251SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
252 const Options& options) {
253 const int index = options.fFrameIndex;
254 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400255 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
256 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400257 }
258
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400259 if (index < 0) {
260 return kInvalidParameters;
261 }
262
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500263 if (options.fSubset) {
264 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400265 // a kRestoreBGColor frame.
266 return kInvalidParameters;
267 }
268
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400269 if (index >= this->onGetFrameCount()) {
270 return kIncompleteInput;
271 }
272
273 const auto* frameHolder = this->getFrameHolder();
274 SkASSERT(frameHolder);
275
276 const auto* frame = frameHolder->getFrame(index);
277 SkASSERT(frame);
278
279 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000280 if (requiredFrame != kNoFrame) {
281 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400282 // Check for a valid frame as a starting point. Alternatively, we could
283 // treat an invalid frame as not providing one, but rejecting it will
284 // make it easier to catch the mistake.
285 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400286 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400287 }
288 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
289 switch (prevFrame->getDisposalMethod()) {
290 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
291 return kInvalidParameters;
292 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
293 // If a frame after the required frame is provided, there is no
294 // need to clear, since it must be covered by the desired frame.
295 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500296 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400297 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400298 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500299 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400300 }
301 break;
302 default:
303 break;
304 }
305 } else {
306 Options prevFrameOptions(options);
307 prevFrameOptions.fFrameIndex = requiredFrame;
308 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
309 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
310 if (result != kSuccess) {
311 return result;
312 }
313 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
314 const auto disposalMethod = prevFrame->getDisposalMethod();
315 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400316 auto prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400317 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400318 return kInternalError;
319 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400320 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400321 }
322 }
323
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400324 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
325 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400326}
scroggo8e6c7ad2016-09-16 08:20:38 -0700327
Brian Osman84f8a612018-09-28 10:35:22 -0400328SkCodec::Result SkCodec::getPixels(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000329 const Options* options) {
Brian Osman84f8a612018-09-28 10:35:22 -0400330 SkImageInfo info = dstInfo;
331 if (!info.colorSpace()) {
332 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
333 }
334
scroggoeb602a52015-07-09 08:16:03 -0700335 if (kUnknown_SkColorType == info.colorType()) {
336 return kInvalidConversion;
337 }
halcanary96fcdcc2015-08-27 07:41:13 -0700338 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700339 return kInvalidParameters;
340 }
341 if (rowBytes < info.minRowBytes()) {
342 return kInvalidParameters;
343 }
344
scroggo3a7701c2015-09-30 09:15:14 -0700345 if (!this->rewindIfNeeded()) {
346 return kCouldNotRewind;
347 }
348
scroggoeb602a52015-07-09 08:16:03 -0700349 // Default options.
350 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700351 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700352 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400353 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400354 if (options->fSubset) {
355 SkIRect subset(*options->fSubset);
356 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
357 // FIXME: How to differentiate between not supporting subset at all
358 // and not supporting this particular subset?
359 return kUnimplemented;
360 }
scroggoe7fc14b2015-10-02 13:14:46 -0700361 }
scroggoeb602a52015-07-09 08:16:03 -0700362 }
scroggoe7fc14b2015-10-02 13:14:46 -0700363
Leon Scroggins III07418182017-08-15 12:24:02 -0400364 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
365 *options);
366 if (frameIndexResult != kSuccess) {
367 return frameIndexResult;
368 }
369
scroggoe7fc14b2015-10-02 13:14:46 -0700370 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
371 // because it supports arbitrary scaling/subset combinations.
372 if (!this->dimensionsSupported(info.dimensions())) {
373 return kInvalidScale;
374 }
375
scroggo8e6c7ad2016-09-16 08:20:38 -0700376 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500377 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700378
msarette6dd0042015-10-09 11:07:34 -0700379 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
380 // successfully.
381 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000382 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700383
384 // A return value of kIncompleteInput indicates a truncated image stream.
385 // In this case, we will fill any uninitialized memory with a default value.
386 // Some subclasses will take care of filling any uninitialized memory on
387 // their own. They indicate that all of the memory has been filled by
388 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400389 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500390 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
391 // there is a subset. In that case, it will use the width of the subset. From here, the
392 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
393 // differenty from the other codecs, and it needs to use the width specified by the info.
394 // Set the subset to null so SkWebpCodec uses the correct width.
395 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700396 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
397 rowsDecoded);
398 }
399
scroggoeb602a52015-07-09 08:16:03 -0700400 return result;
401}
402
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400403SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& dstInfo, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000404 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700405 fStartedIncrementalDecode = false;
406
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400407 SkImageInfo info = dstInfo;
408 if (!info.colorSpace()) {
409 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
410 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700411 if (kUnknown_SkColorType == info.colorType()) {
412 return kInvalidConversion;
413 }
414 if (nullptr == pixels) {
415 return kInvalidParameters;
416 }
417
scroggo8e6c7ad2016-09-16 08:20:38 -0700418 // FIXME: If the rows come after the rows of a previous incremental decode,
419 // we might be able to skip the rewind, but only the implementation knows
420 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
421 // a bottom-up BMP could skip rewinding if the new rows are above the old
422 // rows.)
423 if (!this->rewindIfNeeded()) {
424 return kCouldNotRewind;
425 }
426
427 // Set options.
428 Options optsStorage;
429 if (nullptr == options) {
430 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400431 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400432 if (options->fSubset) {
433 SkIRect size = SkIRect::MakeSize(info.dimensions());
434 if (!size.contains(*options->fSubset)) {
435 return kInvalidParameters;
436 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700437
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400438 const int top = options->fSubset->top();
439 const int bottom = options->fSubset->bottom();
440 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
441 return kInvalidParameters;
442 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700443 }
444 }
445
Leon Scroggins III07418182017-08-15 12:24:02 -0400446 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
447 *options);
448 if (frameIndexResult != kSuccess) {
449 return frameIndexResult;
450 }
451
scroggo8e6c7ad2016-09-16 08:20:38 -0700452 if (!this->dimensionsSupported(info.dimensions())) {
453 return kInvalidScale;
454 }
455
456 fDstInfo = info;
457 fOptions = *options;
458
Leon Scroggins571b30f2017-07-11 17:35:31 +0000459 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700460 if (kSuccess == result) {
461 fStartedIncrementalDecode = true;
462 } else if (kUnimplemented == result) {
463 // FIXME: This is temporarily necessary, until we transition SkCodec
464 // implementations from scanline decoding to incremental decoding.
465 // SkAndroidCodec will first attempt to use incremental decoding, but
466 // will fall back to scanline decoding if incremental returns
467 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
468 // (after potentially rewinding), but we do not want the next call to
469 // startScanlineDecode() to do a rewind.
470 fNeedsRewind = false;
471 }
472 return result;
473}
474
475
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400476SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000477 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700478 // Reset fCurrScanline in case of failure.
479 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700480
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400481 SkImageInfo info = dstInfo;
482 if (!info.colorSpace()) {
483 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
484 }
485
scroggo3a7701c2015-09-30 09:15:14 -0700486 if (!this->rewindIfNeeded()) {
487 return kCouldNotRewind;
488 }
489
scroggo46c57472015-09-30 08:57:13 -0700490 // Set options.
491 Options optsStorage;
492 if (nullptr == options) {
493 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700494 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700495 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700496 if (!size.contains(*options->fSubset)) {
497 return kInvalidInput;
498 }
499
500 // We only support subsetting in the x-dimension for scanline decoder.
501 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700502 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700503 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700504 }
505 }
506
Leon Scroggins III07418182017-08-15 12:24:02 -0400507 // Scanline decoding only supports decoding the first frame.
508 if (options->fFrameIndex != 0) {
509 return kUnimplemented;
510 }
511
512 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
513 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
514 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
515 if (frameIndexResult != kSuccess) {
516 return frameIndexResult;
517 }
518
scroggoe7fc14b2015-10-02 13:14:46 -0700519 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700520 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700521 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700522 }
523
Leon Scroggins571b30f2017-07-11 17:35:31 +0000524 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700525 if (result != SkCodec::kSuccess) {
526 return result;
527 }
528
529 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700530 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700531 fOptions = *options;
532 return kSuccess;
533}
534
msarette6dd0042015-10-09 11:07:34 -0700535int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700536 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700537 return 0;
scroggo46c57472015-09-30 08:57:13 -0700538 }
539
540 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700541 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700542 return 0;
scroggo46c57472015-09-30 08:57:13 -0700543 }
544
msarette6dd0042015-10-09 11:07:34 -0700545 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
546 if (linesDecoded < countLines) {
547 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
548 countLines, linesDecoded);
549 }
550 fCurrScanline += countLines;
551 return linesDecoded;
552}
553
554bool SkCodec::skipScanlines(int countLines) {
555 if (fCurrScanline < 0) {
556 return false;
557 }
558
559 SkASSERT(!fDstInfo.isEmpty());
560 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
561 // Arguably, we could just skip the scanlines which are remaining,
562 // and return true. We choose to return false so the client
563 // can catch their bug.
564 return false;
565 }
566
567 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700568 fCurrScanline += countLines;
569 return result;
570}
571
msarette6dd0042015-10-09 11:07:34 -0700572int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400573 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700574 return this->onOutputScanline(inputScanline);
575}
scroggo46c57472015-09-30 08:57:13 -0700576
msarette6dd0042015-10-09 11:07:34 -0700577int SkCodec::onOutputScanline(int inputScanline) const {
578 switch (this->getScanlineOrder()) {
579 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700580 return inputScanline;
581 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400582 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700583 default:
584 // This case indicates an interlaced gif and is implemented by SkGifCodec.
585 SkASSERT(false);
586 return 0;
scroggo46c57472015-09-30 08:57:13 -0700587 }
msarette6dd0042015-10-09 11:07:34 -0700588}
scroggo46c57472015-09-30 08:57:13 -0700589
msarette6dd0042015-10-09 11:07:34 -0700590void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
591 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400592 if (kYes_ZeroInitialized == zeroInit) {
593 return;
594 }
msarette6dd0042015-10-09 11:07:34 -0700595
msarette6dd0042015-10-09 11:07:34 -0700596 const int linesRemaining = linesRequested - linesDecoded;
597 SkSampler* sampler = this->getSampler(false);
598
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400599 const int fillWidth = sampler ? sampler->fillWidth() :
600 fOptions.fSubset ? fOptions.fSubset->width() :
601 info.width() ;
602 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
603 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
604 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
605 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700606}
Matt Sarett313c4632016-10-20 12:35:23 -0400607
Leon Scroggins III179559f2018-12-10 12:30:12 -0500608bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
609 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400610 SkASSERT(outFormat);
611
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400612 switch (colorType) {
613 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400614 *outFormat = skcms_PixelFormat_RGBA_8888;
615 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400616 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400617 *outFormat = skcms_PixelFormat_BGRA_8888;
618 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400619 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400620 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400621#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400622 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400623#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400624 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400625#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400626 break;
627 }
628 *outFormat = skcms_PixelFormat_BGR_565;
629 break;
630 case kRGBA_F16_SkColorType:
631 *outFormat = skcms_PixelFormat_RGBA_hhhh;
632 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400633 case kGray_8_SkColorType:
634 *outFormat = skcms_PixelFormat_G_8;
635 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400636 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000637 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000638 }
Matt Sarett313c4632016-10-20 12:35:23 -0400639 return true;
640}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400641
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400642bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
643 bool srcIsOpaque) {
644 fXformTime = kNo_XformTime;
645 bool needsColorXform = false;
646 if (this->usesColorXform() && dstInfo.colorSpace()) {
647 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400648 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400649 needsColorXform = true;
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400650 } else {
651 const auto* srcProfile = fEncodedInfo.profile();
652 if (!srcProfile) {
653 srcProfile = skcms_sRGB_profile();
654 }
655 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
656 needsColorXform = true;
657 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400658 }
659 }
660
Leon Scroggins III712476e2018-10-03 15:47:00 -0400661 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400662 return false;
663 }
664
665 if (needsColorXform) {
666 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
667 || kRGBA_F16_SkColorType == dstInfo.colorType()
668 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500669 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
670 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400671 return false;
672 }
673 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
674 && dstInfo.alphaType() == kPremul_SkAlphaType) {
675 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
676 } else {
677 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
678 }
679 }
680 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400681}
682
683void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400684 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400685 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400686 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
687 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
688 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400689}
690
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400691std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400692 const int frameCount = this->getFrameCount();
693 SkASSERT(frameCount >= 0);
694 if (frameCount <= 0) {
695 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400696 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400697
698 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
699 // Not animated.
700 return std::vector<FrameInfo>{};
701 }
702
703 std::vector<FrameInfo> result(frameCount);
704 for (int i = 0; i < frameCount; ++i) {
705 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
706 }
707 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400708}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500709
710const char* SkCodec::ResultToString(Result result) {
711 switch (result) {
712 case kSuccess:
713 return "success";
714 case kIncompleteInput:
715 return "incomplete input";
716 case kErrorInInput:
717 return "error in input";
718 case kInvalidConversion:
719 return "invalid conversion";
720 case kInvalidScale:
721 return "invalid scale";
722 case kInvalidParameters:
723 return "invalid parameters";
724 case kInvalidInput:
725 return "invalid input";
726 case kCouldNotRewind:
727 return "could not rewind";
728 case kInternalError:
729 return "internal error";
730 case kUnimplemented:
731 return "unimplemented";
732 default:
733 SkASSERT(false);
734 return "bogus result value";
735 }
736}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000737
738static SkIRect frame_rect_on_screen(SkIRect frameRect,
739 const SkIRect& screenRect) {
740 if (!frameRect.intersect(screenRect)) {
741 return SkIRect::MakeEmpty();
742 }
743
744 return frameRect;
745}
746
747static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000748 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000749}
750
751static bool restore_bg(const SkFrame& frame) {
752 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
753}
754
Nigel Taob8ec7f12019-03-26 10:44:58 +1100755// As its name suggests, this method computes a frame's alpha (e.g. completely
756// opaque, unpremul, binary) and its required frame (a preceding frame that
757// this frame depends on, to draw the complete image at this frame's point in
758// the animation stream), and calls this frame's setter methods with that
759// computed information.
760//
761// A required frame of kNoFrame means that this frame is independent: drawing
762// the complete image at this frame's point in the animation stream does not
763// require first preparing the pixel buffer based on another frame. Instead,
764// drawing can start from an uninitialized pixel buffer.
765//
766// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
767// implementation, for independent frames, first party Skia code (in src/codec)
768// will typically fill the buffer with a uniform background color (e.g.
769// transparent black) before calling into third party codec-specific code (e.g.
770// libjpeg or libpng). Pixels outside of the frame's rect will remain this
771// background color after drawing this frame. For incomplete decodes, pixels
772// inside that rect may be (at least temporarily) set to that background color.
773// In an incremental decode, later passes may then overwrite that background
774// color.
775//
776// Determining kNoFrame or otherwise involves testing a number of conditions
777// sequentially. The first satisfied condition results in setting the required
778// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
779// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
780// labels also map to comments in the function body.
781//
782// - IND1: this frame is the first frame.
783// - IND2: this frame fills out the whole image, and it is completely opaque
784// or it overwrites (not blends with) the previous frame.
785// - IND3: all preceding frames' disposals are kRestorePrevious.
786// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
787// whole image or it is itself otherwise independent.
788// - DEP5: this frame reports alpha (it is not completely opaque) and it
789// blends with (not overwrites) the previous frame.
790// - IND6: this frame's rect covers the rects of all preceding frames back to
791// and including the most recent independent frame before this frame.
792// - DEP7: unconditional.
793//
794// The "prevFrame" variable initially points to the previous frame (also known
795// as the prior frame), but that variable may iterate further backwards over
796// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000797void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
798 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
799 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
800 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
801
802 const int i = frame->frameId();
803 if (0 == i) {
804 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100805 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000806 return;
807 }
808
809
810 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
811 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
812 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100813 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000814 return;
815 }
816
817 const SkFrame* prevFrame = this->getFrame(i-1);
818 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
819 const int prevId = prevFrame->frameId();
820 if (0 == prevId) {
821 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100822 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000823 return;
824 }
825
826 prevFrame = this->getFrame(prevId - 1);
827 }
828
829 const bool clearPrevFrame = restore_bg(*prevFrame);
830 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
831
832 if (clearPrevFrame) {
833 if (prevFrameRect == screenRect || independent(*prevFrame)) {
834 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100835 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000836 return;
837 }
838 }
839
840 if (reportsAlpha && blendWithPrevFrame) {
841 // Note: We could be more aggressive here. If prevFrame clears
842 // to background color and covers its required frame (and that
843 // frame is independent), prevFrame could be marked independent.
844 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100845 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000846 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
847 return;
848 }
849
850 while (frameRect.contains(prevFrameRect)) {
851 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000852 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100853 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000854 frame->setHasAlpha(true);
855 return;
856 }
857
858 prevFrame = this->getFrame(prevRequiredFrame);
859 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
860 }
861
Nigel Taob8ec7f12019-03-26 10:44:58 +1100862 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000863 if (restore_bg(*prevFrame)) {
864 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000865 return;
866 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000867 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000868 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
869}
870