blob: 31c8acd428920aa9bfad735753993659ea2e78ee [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"
12#include "src/codec/SkBmpCodec.h"
13#include "src/codec/SkCodecPriv.h"
14#include "src/codec/SkFrameHolder.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040015#ifdef SK_HAS_HEIF_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/codec/SkHeifCodec.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040017#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/codec/SkIcoCodec.h"
19#include "src/codec/SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070020#ifdef SK_HAS_PNG_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/codec/SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080022#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkStream.h"
24#include "src/codec/SkRawCodec.h"
25#include "src/codec/SkWbmpCodec.h"
26#include "src/codec/SkWebpCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040027#ifdef SK_HAS_WUFFS_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/codec/SkWuffsCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040029#else
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/codec/SkGifCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040031#endif
scroggof24f2242015-03-03 08:59:20 -080032
msarett74114382015-03-16 11:55:18 -070033struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080034 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040035 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070036};
37
Mike Klein159a9592019-04-26 15:47:56 +000038static std::vector<DecoderProc>* decoders() {
39 static auto* decoders = new std::vector<DecoderProc> {
40 #ifdef SK_HAS_JPEG_LIBRARY
41 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
42 #endif
43 #ifdef SK_HAS_WEBP_LIBRARY
44 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
45 #endif
46 #ifdef SK_HAS_WUFFS_LIBRARY
47 { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
48 #else
49 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
50 #endif
51 #ifdef SK_HAS_PNG_LIBRARY
52 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
53 #endif
54 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
55 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
56 #ifdef SK_HAS_HEIF_LIBRARY
57 { SkHeifCodec::IsHeif, SkHeifCodec::MakeFromStream },
58 #endif
59 };
60 return decoders;
61}
62
63void SkCodec::Register(
64 bool (*peek)(const void*, size_t),
65 std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)) {
66 decoders()->push_back(DecoderProc{peek, make});
67}
68
msarett74114382015-03-16 11:55:18 -070069
Mike Reedede7bac2017-07-23 15:30:02 -040070std::unique_ptr<SkCodec> SkCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
71 Result* outResult, SkPngChunkReader* chunkReader) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040072 Result resultStorage;
73 if (!outResult) {
74 outResult = &resultStorage;
75 }
76
scroggof24f2242015-03-03 08:59:20 -080077 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040078 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070079 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080080 }
scroggo0a7e69c2015-04-03 07:22:22 -070081
Leon Scroggins III04be2b52017-08-17 15:13:20 -040082 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080083
84 char buffer[bytesToRead];
85 size_t bytesRead = stream->peek(buffer, bytesToRead);
86
87 // It is also possible to have a complete image less than bytesToRead bytes
88 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
89 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
90 // than bytesToRead, so pass that directly to the decoder.
91 // It also is possible the stream uses too small a buffer for peeking, but
92 // we trust the caller to use a large enough buffer.
93
94 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080095 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
96 // printf could be useful to notice failures.
97 // SkCodecPrintf("Encoded image data failed to peek!\n");
98
scroggodb30be22015-12-08 18:54:13 -080099 // It is possible the stream does not support peeking, but does support
100 // rewinding.
101 // Attempt to read() and pass the actual amount read to the decoder.
102 bytesRead = stream->read(buffer, bytesToRead);
103 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800104 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400105 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -0800106 return nullptr;
107 }
108 }
109
scroggocf98fa92015-11-23 08:14:40 -0800110 // PNG is special, since we want to be able to supply an SkPngChunkReader.
111 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -0700112#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -0800113 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400114 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -0800115 } else
116#endif
117 {
Mike Klein159a9592019-04-26 15:47:56 +0000118 for (DecoderProc proc : *decoders()) {
scroggodb30be22015-12-08 18:54:13 -0800119 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400120 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800121 }
msarett74114382015-03-16 11:55:18 -0700122 }
yujieqin916de9f2016-01-25 08:26:16 -0800123
124#ifdef SK_CODEC_DECODES_RAW
125 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400126 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800127#endif
scroggof24f2242015-03-03 08:59:20 -0800128 }
msarett8c8f22a2015-04-01 06:58:48 -0700129
Leon Scroggins III588fb042017-07-14 16:32:31 -0400130 if (bytesRead < bytesToRead) {
131 *outResult = kIncompleteInput;
132 } else {
133 *outResult = kUnimplemented;
134 }
135
msarettf44631b2016-01-13 10:54:20 -0800136 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800137}
138
Mike Reedede7bac2017-07-23 15:30:02 -0400139std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800140 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700141 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800142 }
Mike Reed847068c2017-07-26 11:35:53 -0400143 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800144}
145
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400146SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
147 SkEncodedOrigin origin)
148 : fEncodedInfo(std::move(info))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400149 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400150 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700151 , fNeedsRewind(false)
152 , fOrigin(origin)
153 , fDstInfo()
154 , fOptions()
155 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700156 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700157{}
158
scroggo9b2cdbf42015-07-10 12:07:02 -0700159SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700160
Leon Scroggins III712476e2018-10-03 15:47:00 -0400161bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400162 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
163 return false;
164 }
165
166 switch (dst.colorType()) {
167 case kRGBA_8888_SkColorType:
168 case kBGRA_8888_SkColorType:
169 return true;
170 case kRGBA_F16_SkColorType:
Mike Kleince4cf722018-05-10 11:29:15 -0400171 return dst.colorSpace();
Leon Scroggins III07418182017-08-15 12:24:02 -0400172 case kRGB_565_SkColorType:
173 return srcIsOpaque;
174 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400175 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500176 case kAlpha_8_SkColorType:
177 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400178 // all of those other conversions yet.
179 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400180 default:
181 return false;
182 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400183}
184
scroggob427db12015-08-12 07:24:13 -0700185bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800186 // Store the value of fNeedsRewind so we can update it. Next read will
187 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700188 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800189 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700190 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700191 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700192 }
scroggob427db12015-08-12 07:24:13 -0700193
scroggo46c57472015-09-30 08:57:13 -0700194 // startScanlineDecode will need to be called before decoding scanlines.
195 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700196 // startIncrementalDecode will need to be called before incrementalDecode.
197 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700198
scroggo19b91532016-10-24 09:03:26 -0700199 // Some codecs do not have a stream. They may hold onto their own data or another codec.
200 // They must handle rewinding themselves.
201 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700202 return false;
203 }
204
205 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800206}
scroggo05245902015-03-25 11:11:52 -0700207
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400208static SkIRect frame_rect_on_screen(SkIRect frameRect,
209 const SkIRect& screenRect) {
210 if (!frameRect.intersect(screenRect)) {
211 return SkIRect::MakeEmpty();
212 }
213
214 return frameRect;
215}
216
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400217bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
218 SkISize srcDimensions, SkIRect prevRect) {
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400219 prevRect = frame_rect_on_screen(prevRect, SkIRect::MakeSize(srcDimensions));
220 if (prevRect.isEmpty()) {
221 return true;
222 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400223 const auto dimensions = dstInfo.dimensions();
224 if (dimensions != srcDimensions) {
225 SkRect src = SkRect::Make(srcDimensions);
226 SkRect dst = SkRect::Make(dimensions);
227 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
228 SkRect asRect = SkRect::Make(prevRect);
229 if (!map.mapRect(&asRect)) {
230 return false;
231 }
232 asRect.roundIn(&prevRect);
233 if (prevRect.isEmpty()) {
234 // Down-scaling shrank the empty portion to nothing,
235 // so nothing to zero.
236 return true;
237 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400238 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400239
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400240 const SkImageInfo info = dstInfo.makeWH(prevRect.width(), prevRect.height());
Mike Reed7fcfb622018-02-09 13:26:46 -0500241 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400242 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
243 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400244 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400245 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400246}
247
248SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
249 const Options& options) {
250 const int index = options.fFrameIndex;
251 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400252 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
253 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400254 }
255
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400256 if (index < 0) {
257 return kInvalidParameters;
258 }
259
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500260 if (options.fSubset) {
261 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400262 // a kRestoreBGColor frame.
263 return kInvalidParameters;
264 }
265
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400266 if (index >= this->onGetFrameCount()) {
267 return kIncompleteInput;
268 }
269
270 const auto* frameHolder = this->getFrameHolder();
271 SkASSERT(frameHolder);
272
273 const auto* frame = frameHolder->getFrame(index);
274 SkASSERT(frame);
275
276 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000277 if (requiredFrame != kNoFrame) {
278 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400279 // Check for a valid frame as a starting point. Alternatively, we could
280 // treat an invalid frame as not providing one, but rejecting it will
281 // make it easier to catch the mistake.
282 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400283 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400284 }
285 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
286 switch (prevFrame->getDisposalMethod()) {
287 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
288 return kInvalidParameters;
289 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
290 // If a frame after the required frame is provided, there is no
291 // need to clear, since it must be covered by the desired frame.
292 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500293 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400294 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400295 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500296 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400297 }
298 break;
299 default:
300 break;
301 }
302 } else {
303 Options prevFrameOptions(options);
304 prevFrameOptions.fFrameIndex = requiredFrame;
305 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
306 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
307 if (result != kSuccess) {
308 return result;
309 }
310 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
311 const auto disposalMethod = prevFrame->getDisposalMethod();
312 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400313 auto prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400314 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400315 return kInternalError;
316 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400317 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400318 }
319 }
320
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400321 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
322 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400323}
scroggo8e6c7ad2016-09-16 08:20:38 -0700324
Brian Osman84f8a612018-09-28 10:35:22 -0400325SkCodec::Result SkCodec::getPixels(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000326 const Options* options) {
Brian Osman84f8a612018-09-28 10:35:22 -0400327 SkImageInfo info = dstInfo;
328 if (!info.colorSpace()) {
329 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
330 }
331
scroggoeb602a52015-07-09 08:16:03 -0700332 if (kUnknown_SkColorType == info.colorType()) {
333 return kInvalidConversion;
334 }
halcanary96fcdcc2015-08-27 07:41:13 -0700335 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700336 return kInvalidParameters;
337 }
338 if (rowBytes < info.minRowBytes()) {
339 return kInvalidParameters;
340 }
341
scroggo3a7701c2015-09-30 09:15:14 -0700342 if (!this->rewindIfNeeded()) {
343 return kCouldNotRewind;
344 }
345
scroggoeb602a52015-07-09 08:16:03 -0700346 // Default options.
347 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700348 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700349 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400350 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400351 if (options->fSubset) {
352 SkIRect subset(*options->fSubset);
353 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
354 // FIXME: How to differentiate between not supporting subset at all
355 // and not supporting this particular subset?
356 return kUnimplemented;
357 }
scroggoe7fc14b2015-10-02 13:14:46 -0700358 }
scroggoeb602a52015-07-09 08:16:03 -0700359 }
scroggoe7fc14b2015-10-02 13:14:46 -0700360
Leon Scroggins III07418182017-08-15 12:24:02 -0400361 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
362 *options);
363 if (frameIndexResult != kSuccess) {
364 return frameIndexResult;
365 }
366
scroggoe7fc14b2015-10-02 13:14:46 -0700367 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
368 // because it supports arbitrary scaling/subset combinations.
369 if (!this->dimensionsSupported(info.dimensions())) {
370 return kInvalidScale;
371 }
372
scroggo8e6c7ad2016-09-16 08:20:38 -0700373 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500374 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700375
msarette6dd0042015-10-09 11:07:34 -0700376 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
377 // successfully.
378 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000379 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700380
381 // A return value of kIncompleteInput indicates a truncated image stream.
382 // In this case, we will fill any uninitialized memory with a default value.
383 // Some subclasses will take care of filling any uninitialized memory on
384 // their own. They indicate that all of the memory has been filled by
385 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400386 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500387 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
388 // there is a subset. In that case, it will use the width of the subset. From here, the
389 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
390 // differenty from the other codecs, and it needs to use the width specified by the info.
391 // Set the subset to null so SkWebpCodec uses the correct width.
392 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700393 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
394 rowsDecoded);
395 }
396
scroggoeb602a52015-07-09 08:16:03 -0700397 return result;
398}
399
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400400SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& dstInfo, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000401 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700402 fStartedIncrementalDecode = false;
403
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400404 SkImageInfo info = dstInfo;
405 if (!info.colorSpace()) {
406 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
407 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700408 if (kUnknown_SkColorType == info.colorType()) {
409 return kInvalidConversion;
410 }
411 if (nullptr == pixels) {
412 return kInvalidParameters;
413 }
414
scroggo8e6c7ad2016-09-16 08:20:38 -0700415 // FIXME: If the rows come after the rows of a previous incremental decode,
416 // we might be able to skip the rewind, but only the implementation knows
417 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
418 // a bottom-up BMP could skip rewinding if the new rows are above the old
419 // rows.)
420 if (!this->rewindIfNeeded()) {
421 return kCouldNotRewind;
422 }
423
424 // Set options.
425 Options optsStorage;
426 if (nullptr == options) {
427 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400428 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400429 if (options->fSubset) {
430 SkIRect size = SkIRect::MakeSize(info.dimensions());
431 if (!size.contains(*options->fSubset)) {
432 return kInvalidParameters;
433 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700434
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400435 const int top = options->fSubset->top();
436 const int bottom = options->fSubset->bottom();
437 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
438 return kInvalidParameters;
439 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700440 }
441 }
442
Leon Scroggins III07418182017-08-15 12:24:02 -0400443 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
444 *options);
445 if (frameIndexResult != kSuccess) {
446 return frameIndexResult;
447 }
448
scroggo8e6c7ad2016-09-16 08:20:38 -0700449 if (!this->dimensionsSupported(info.dimensions())) {
450 return kInvalidScale;
451 }
452
453 fDstInfo = info;
454 fOptions = *options;
455
Leon Scroggins571b30f2017-07-11 17:35:31 +0000456 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700457 if (kSuccess == result) {
458 fStartedIncrementalDecode = true;
459 } else if (kUnimplemented == result) {
460 // FIXME: This is temporarily necessary, until we transition SkCodec
461 // implementations from scanline decoding to incremental decoding.
462 // SkAndroidCodec will first attempt to use incremental decoding, but
463 // will fall back to scanline decoding if incremental returns
464 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
465 // (after potentially rewinding), but we do not want the next call to
466 // startScanlineDecode() to do a rewind.
467 fNeedsRewind = false;
468 }
469 return result;
470}
471
472
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400473SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000474 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700475 // Reset fCurrScanline in case of failure.
476 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700477
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400478 SkImageInfo info = dstInfo;
479 if (!info.colorSpace()) {
480 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
481 }
482
scroggo3a7701c2015-09-30 09:15:14 -0700483 if (!this->rewindIfNeeded()) {
484 return kCouldNotRewind;
485 }
486
scroggo46c57472015-09-30 08:57:13 -0700487 // Set options.
488 Options optsStorage;
489 if (nullptr == options) {
490 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700491 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700492 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700493 if (!size.contains(*options->fSubset)) {
494 return kInvalidInput;
495 }
496
497 // We only support subsetting in the x-dimension for scanline decoder.
498 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700499 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700500 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700501 }
502 }
503
Leon Scroggins III07418182017-08-15 12:24:02 -0400504 // Scanline decoding only supports decoding the first frame.
505 if (options->fFrameIndex != 0) {
506 return kUnimplemented;
507 }
508
509 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
510 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
511 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
512 if (frameIndexResult != kSuccess) {
513 return frameIndexResult;
514 }
515
scroggoe7fc14b2015-10-02 13:14:46 -0700516 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700517 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700518 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700519 }
520
Leon Scroggins571b30f2017-07-11 17:35:31 +0000521 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700522 if (result != SkCodec::kSuccess) {
523 return result;
524 }
525
526 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700527 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700528 fOptions = *options;
529 return kSuccess;
530}
531
msarette6dd0042015-10-09 11:07:34 -0700532int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700533 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700534 return 0;
scroggo46c57472015-09-30 08:57:13 -0700535 }
536
537 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700538 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700539 return 0;
scroggo46c57472015-09-30 08:57:13 -0700540 }
541
msarette6dd0042015-10-09 11:07:34 -0700542 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
543 if (linesDecoded < countLines) {
544 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
545 countLines, linesDecoded);
546 }
547 fCurrScanline += countLines;
548 return linesDecoded;
549}
550
551bool SkCodec::skipScanlines(int countLines) {
552 if (fCurrScanline < 0) {
553 return false;
554 }
555
556 SkASSERT(!fDstInfo.isEmpty());
557 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
558 // Arguably, we could just skip the scanlines which are remaining,
559 // and return true. We choose to return false so the client
560 // can catch their bug.
561 return false;
562 }
563
564 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700565 fCurrScanline += countLines;
566 return result;
567}
568
msarette6dd0042015-10-09 11:07:34 -0700569int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400570 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700571 return this->onOutputScanline(inputScanline);
572}
scroggo46c57472015-09-30 08:57:13 -0700573
msarette6dd0042015-10-09 11:07:34 -0700574int SkCodec::onOutputScanline(int inputScanline) const {
575 switch (this->getScanlineOrder()) {
576 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700577 return inputScanline;
578 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400579 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700580 default:
581 // This case indicates an interlaced gif and is implemented by SkGifCodec.
582 SkASSERT(false);
583 return 0;
scroggo46c57472015-09-30 08:57:13 -0700584 }
msarette6dd0042015-10-09 11:07:34 -0700585}
scroggo46c57472015-09-30 08:57:13 -0700586
msarette6dd0042015-10-09 11:07:34 -0700587void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
588 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400589 if (kYes_ZeroInitialized == zeroInit) {
590 return;
591 }
msarette6dd0042015-10-09 11:07:34 -0700592
msarette6dd0042015-10-09 11:07:34 -0700593 const int linesRemaining = linesRequested - linesDecoded;
594 SkSampler* sampler = this->getSampler(false);
595
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400596 const int fillWidth = sampler ? sampler->fillWidth() :
597 fOptions.fSubset ? fOptions.fSubset->width() :
598 info.width() ;
599 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
600 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
601 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
602 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700603}
Matt Sarett313c4632016-10-20 12:35:23 -0400604
Leon Scroggins III179559f2018-12-10 12:30:12 -0500605bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
606 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400607 SkASSERT(outFormat);
608
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400609 switch (colorType) {
610 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400611 *outFormat = skcms_PixelFormat_RGBA_8888;
612 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400613 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400614 *outFormat = skcms_PixelFormat_BGRA_8888;
615 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400616 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400617 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400618#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400619 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400620#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400621 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400622#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400623 break;
624 }
625 *outFormat = skcms_PixelFormat_BGR_565;
626 break;
627 case kRGBA_F16_SkColorType:
628 *outFormat = skcms_PixelFormat_RGBA_hhhh;
629 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400630 case kGray_8_SkColorType:
631 *outFormat = skcms_PixelFormat_G_8;
632 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400633 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000634 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000635 }
Matt Sarett313c4632016-10-20 12:35:23 -0400636 return true;
637}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400638
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400639bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
640 bool srcIsOpaque) {
641 fXformTime = kNo_XformTime;
642 bool needsColorXform = false;
643 if (this->usesColorXform() && dstInfo.colorSpace()) {
644 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400645 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400646 needsColorXform = true;
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400647 } else {
648 const auto* srcProfile = fEncodedInfo.profile();
649 if (!srcProfile) {
650 srcProfile = skcms_sRGB_profile();
651 }
652 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
653 needsColorXform = true;
654 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400655 }
656 }
657
Leon Scroggins III712476e2018-10-03 15:47:00 -0400658 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400659 return false;
660 }
661
662 if (needsColorXform) {
663 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
664 || kRGBA_F16_SkColorType == dstInfo.colorType()
665 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500666 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
667 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400668 return false;
669 }
670 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
671 && dstInfo.alphaType() == kPremul_SkAlphaType) {
672 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
673 } else {
674 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
675 }
676 }
677 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400678}
679
680void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400681 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400682 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400683 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
684 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
685 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400686}
687
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400688std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400689 const int frameCount = this->getFrameCount();
690 SkASSERT(frameCount >= 0);
691 if (frameCount <= 0) {
692 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400693 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400694
695 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
696 // Not animated.
697 return std::vector<FrameInfo>{};
698 }
699
700 std::vector<FrameInfo> result(frameCount);
701 for (int i = 0; i < frameCount; ++i) {
702 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
703 }
704 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400705}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500706
707const char* SkCodec::ResultToString(Result result) {
708 switch (result) {
709 case kSuccess:
710 return "success";
711 case kIncompleteInput:
712 return "incomplete input";
713 case kErrorInInput:
714 return "error in input";
715 case kInvalidConversion:
716 return "invalid conversion";
717 case kInvalidScale:
718 return "invalid scale";
719 case kInvalidParameters:
720 return "invalid parameters";
721 case kInvalidInput:
722 return "invalid input";
723 case kCouldNotRewind:
724 return "could not rewind";
725 case kInternalError:
726 return "internal error";
727 case kUnimplemented:
728 return "unimplemented";
729 default:
730 SkASSERT(false);
731 return "bogus result value";
732 }
733}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000734
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000735static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000736 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000737}
738
739static bool restore_bg(const SkFrame& frame) {
740 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
741}
742
Nigel Taob8ec7f12019-03-26 10:44:58 +1100743// As its name suggests, this method computes a frame's alpha (e.g. completely
744// opaque, unpremul, binary) and its required frame (a preceding frame that
745// this frame depends on, to draw the complete image at this frame's point in
746// the animation stream), and calls this frame's setter methods with that
747// computed information.
748//
749// A required frame of kNoFrame means that this frame is independent: drawing
750// the complete image at this frame's point in the animation stream does not
751// require first preparing the pixel buffer based on another frame. Instead,
752// drawing can start from an uninitialized pixel buffer.
753//
754// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
755// implementation, for independent frames, first party Skia code (in src/codec)
756// will typically fill the buffer with a uniform background color (e.g.
757// transparent black) before calling into third party codec-specific code (e.g.
758// libjpeg or libpng). Pixels outside of the frame's rect will remain this
759// background color after drawing this frame. For incomplete decodes, pixels
760// inside that rect may be (at least temporarily) set to that background color.
761// In an incremental decode, later passes may then overwrite that background
762// color.
763//
764// Determining kNoFrame or otherwise involves testing a number of conditions
765// sequentially. The first satisfied condition results in setting the required
766// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
767// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
768// labels also map to comments in the function body.
769//
770// - IND1: this frame is the first frame.
771// - IND2: this frame fills out the whole image, and it is completely opaque
772// or it overwrites (not blends with) the previous frame.
773// - IND3: all preceding frames' disposals are kRestorePrevious.
774// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
775// whole image or it is itself otherwise independent.
776// - DEP5: this frame reports alpha (it is not completely opaque) and it
777// blends with (not overwrites) the previous frame.
778// - IND6: this frame's rect covers the rects of all preceding frames back to
779// and including the most recent independent frame before this frame.
780// - DEP7: unconditional.
781//
782// The "prevFrame" variable initially points to the previous frame (also known
783// as the prior frame), but that variable may iterate further backwards over
784// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000785void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
786 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
787 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
788 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
789
790 const int i = frame->frameId();
791 if (0 == i) {
792 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100793 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000794 return;
795 }
796
797
798 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
799 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
800 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100801 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000802 return;
803 }
804
805 const SkFrame* prevFrame = this->getFrame(i-1);
806 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
807 const int prevId = prevFrame->frameId();
808 if (0 == prevId) {
809 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100810 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000811 return;
812 }
813
814 prevFrame = this->getFrame(prevId - 1);
815 }
816
817 const bool clearPrevFrame = restore_bg(*prevFrame);
818 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
819
820 if (clearPrevFrame) {
821 if (prevFrameRect == screenRect || independent(*prevFrame)) {
822 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100823 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000824 return;
825 }
826 }
827
828 if (reportsAlpha && blendWithPrevFrame) {
829 // Note: We could be more aggressive here. If prevFrame clears
830 // to background color and covers its required frame (and that
831 // frame is independent), prevFrame could be marked independent.
832 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100833 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000834 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
835 return;
836 }
837
838 while (frameRect.contains(prevFrameRect)) {
839 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000840 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100841 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000842 frame->setHasAlpha(true);
843 return;
844 }
845
846 prevFrame = this->getFrame(prevRequiredFrame);
847 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
848 }
849
Nigel Taob8ec7f12019-03-26 10:44:58 +1100850 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000851 if (restore_bg(*prevFrame)) {
852 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000853 return;
854 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000855 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000856 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
857}
858