blob: c37b34e8ac0137a7c5eb12f5146db784a89d97c6 [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 III7916c0e2018-05-24 13:04:06 -0400208bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
209 SkISize srcDimensions, SkIRect prevRect) {
210 const auto dimensions = dstInfo.dimensions();
211 if (dimensions != srcDimensions) {
212 SkRect src = SkRect::Make(srcDimensions);
213 SkRect dst = SkRect::Make(dimensions);
214 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
215 SkRect asRect = SkRect::Make(prevRect);
216 if (!map.mapRect(&asRect)) {
217 return false;
218 }
219 asRect.roundIn(&prevRect);
220 if (prevRect.isEmpty()) {
221 // Down-scaling shrank the empty portion to nothing,
222 // so nothing to zero.
223 return true;
224 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400225 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400226
227 if (!prevRect.intersect(dstInfo.bounds())) {
228 SkCodecPrintf("rectangles do not intersect!");
229 SkASSERT(false);
230 return true;
231 }
232
233 const SkImageInfo info = dstInfo.makeWH(prevRect.width(), prevRect.height());
Mike Reed7fcfb622018-02-09 13:26:46 -0500234 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400235 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
236 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400237 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400238 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400239}
240
241SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
242 const Options& options) {
243 const int index = options.fFrameIndex;
244 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400245 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
246 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400247 }
248
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400249 if (index < 0) {
250 return kInvalidParameters;
251 }
252
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500253 if (options.fSubset) {
254 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400255 // a kRestoreBGColor frame.
256 return kInvalidParameters;
257 }
258
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400259 if (index >= this->onGetFrameCount()) {
260 return kIncompleteInput;
261 }
262
263 const auto* frameHolder = this->getFrameHolder();
264 SkASSERT(frameHolder);
265
266 const auto* frame = frameHolder->getFrame(index);
267 SkASSERT(frame);
268
269 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000270 if (requiredFrame != kNoFrame) {
271 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400272 // Check for a valid frame as a starting point. Alternatively, we could
273 // treat an invalid frame as not providing one, but rejecting it will
274 // make it easier to catch the mistake.
275 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400276 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400277 }
278 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
279 switch (prevFrame->getDisposalMethod()) {
280 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
281 return kInvalidParameters;
282 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
283 // If a frame after the required frame is provided, there is no
284 // need to clear, since it must be covered by the desired frame.
285 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500286 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400287 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400288 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500289 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400290 }
291 break;
292 default:
293 break;
294 }
295 } else {
296 Options prevFrameOptions(options);
297 prevFrameOptions.fFrameIndex = requiredFrame;
298 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
299 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
300 if (result != kSuccess) {
301 return result;
302 }
303 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
304 const auto disposalMethod = prevFrame->getDisposalMethod();
305 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400306 auto prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400307 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400308 return kInternalError;
309 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400310 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400311 }
312 }
313
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400314 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
315 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400316}
scroggo8e6c7ad2016-09-16 08:20:38 -0700317
Brian Osman84f8a612018-09-28 10:35:22 -0400318SkCodec::Result SkCodec::getPixels(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000319 const Options* options) {
Brian Osman84f8a612018-09-28 10:35:22 -0400320 SkImageInfo info = dstInfo;
321 if (!info.colorSpace()) {
322 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
323 }
324
scroggoeb602a52015-07-09 08:16:03 -0700325 if (kUnknown_SkColorType == info.colorType()) {
326 return kInvalidConversion;
327 }
halcanary96fcdcc2015-08-27 07:41:13 -0700328 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700329 return kInvalidParameters;
330 }
331 if (rowBytes < info.minRowBytes()) {
332 return kInvalidParameters;
333 }
334
scroggo3a7701c2015-09-30 09:15:14 -0700335 if (!this->rewindIfNeeded()) {
336 return kCouldNotRewind;
337 }
338
scroggoeb602a52015-07-09 08:16:03 -0700339 // Default options.
340 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700341 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700342 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400343 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400344 if (options->fSubset) {
345 SkIRect subset(*options->fSubset);
346 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
347 // FIXME: How to differentiate between not supporting subset at all
348 // and not supporting this particular subset?
349 return kUnimplemented;
350 }
scroggoe7fc14b2015-10-02 13:14:46 -0700351 }
scroggoeb602a52015-07-09 08:16:03 -0700352 }
scroggoe7fc14b2015-10-02 13:14:46 -0700353
Leon Scroggins III07418182017-08-15 12:24:02 -0400354 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
355 *options);
356 if (frameIndexResult != kSuccess) {
357 return frameIndexResult;
358 }
359
scroggoe7fc14b2015-10-02 13:14:46 -0700360 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
361 // because it supports arbitrary scaling/subset combinations.
362 if (!this->dimensionsSupported(info.dimensions())) {
363 return kInvalidScale;
364 }
365
scroggo8e6c7ad2016-09-16 08:20:38 -0700366 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500367 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700368
msarette6dd0042015-10-09 11:07:34 -0700369 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
370 // successfully.
371 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000372 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700373
374 // A return value of kIncompleteInput indicates a truncated image stream.
375 // In this case, we will fill any uninitialized memory with a default value.
376 // Some subclasses will take care of filling any uninitialized memory on
377 // their own. They indicate that all of the memory has been filled by
378 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400379 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500380 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
381 // there is a subset. In that case, it will use the width of the subset. From here, the
382 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
383 // differenty from the other codecs, and it needs to use the width specified by the info.
384 // Set the subset to null so SkWebpCodec uses the correct width.
385 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700386 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
387 rowsDecoded);
388 }
389
scroggoeb602a52015-07-09 08:16:03 -0700390 return result;
391}
392
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400393SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& dstInfo, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000394 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700395 fStartedIncrementalDecode = false;
396
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400397 SkImageInfo info = dstInfo;
398 if (!info.colorSpace()) {
399 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
400 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700401 if (kUnknown_SkColorType == info.colorType()) {
402 return kInvalidConversion;
403 }
404 if (nullptr == pixels) {
405 return kInvalidParameters;
406 }
407
scroggo8e6c7ad2016-09-16 08:20:38 -0700408 // FIXME: If the rows come after the rows of a previous incremental decode,
409 // we might be able to skip the rewind, but only the implementation knows
410 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
411 // a bottom-up BMP could skip rewinding if the new rows are above the old
412 // rows.)
413 if (!this->rewindIfNeeded()) {
414 return kCouldNotRewind;
415 }
416
417 // Set options.
418 Options optsStorage;
419 if (nullptr == options) {
420 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400421 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400422 if (options->fSubset) {
423 SkIRect size = SkIRect::MakeSize(info.dimensions());
424 if (!size.contains(*options->fSubset)) {
425 return kInvalidParameters;
426 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700427
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400428 const int top = options->fSubset->top();
429 const int bottom = options->fSubset->bottom();
430 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
431 return kInvalidParameters;
432 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700433 }
434 }
435
Leon Scroggins III07418182017-08-15 12:24:02 -0400436 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
437 *options);
438 if (frameIndexResult != kSuccess) {
439 return frameIndexResult;
440 }
441
scroggo8e6c7ad2016-09-16 08:20:38 -0700442 if (!this->dimensionsSupported(info.dimensions())) {
443 return kInvalidScale;
444 }
445
446 fDstInfo = info;
447 fOptions = *options;
448
Leon Scroggins571b30f2017-07-11 17:35:31 +0000449 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700450 if (kSuccess == result) {
451 fStartedIncrementalDecode = true;
452 } else if (kUnimplemented == result) {
453 // FIXME: This is temporarily necessary, until we transition SkCodec
454 // implementations from scanline decoding to incremental decoding.
455 // SkAndroidCodec will first attempt to use incremental decoding, but
456 // will fall back to scanline decoding if incremental returns
457 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
458 // (after potentially rewinding), but we do not want the next call to
459 // startScanlineDecode() to do a rewind.
460 fNeedsRewind = false;
461 }
462 return result;
463}
464
465
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400466SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000467 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700468 // Reset fCurrScanline in case of failure.
469 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700470
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400471 SkImageInfo info = dstInfo;
472 if (!info.colorSpace()) {
473 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
474 }
475
scroggo3a7701c2015-09-30 09:15:14 -0700476 if (!this->rewindIfNeeded()) {
477 return kCouldNotRewind;
478 }
479
scroggo46c57472015-09-30 08:57:13 -0700480 // Set options.
481 Options optsStorage;
482 if (nullptr == options) {
483 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700484 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700485 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700486 if (!size.contains(*options->fSubset)) {
487 return kInvalidInput;
488 }
489
490 // We only support subsetting in the x-dimension for scanline decoder.
491 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700492 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700493 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700494 }
495 }
496
Leon Scroggins III07418182017-08-15 12:24:02 -0400497 // Scanline decoding only supports decoding the first frame.
498 if (options->fFrameIndex != 0) {
499 return kUnimplemented;
500 }
501
502 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
503 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
504 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
505 if (frameIndexResult != kSuccess) {
506 return frameIndexResult;
507 }
508
scroggoe7fc14b2015-10-02 13:14:46 -0700509 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700510 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700511 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700512 }
513
Leon Scroggins571b30f2017-07-11 17:35:31 +0000514 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700515 if (result != SkCodec::kSuccess) {
516 return result;
517 }
518
519 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700520 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700521 fOptions = *options;
522 return kSuccess;
523}
524
msarette6dd0042015-10-09 11:07:34 -0700525int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700526 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700527 return 0;
scroggo46c57472015-09-30 08:57:13 -0700528 }
529
530 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700531 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700532 return 0;
scroggo46c57472015-09-30 08:57:13 -0700533 }
534
msarette6dd0042015-10-09 11:07:34 -0700535 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
536 if (linesDecoded < countLines) {
537 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
538 countLines, linesDecoded);
539 }
540 fCurrScanline += countLines;
541 return linesDecoded;
542}
543
544bool SkCodec::skipScanlines(int countLines) {
545 if (fCurrScanline < 0) {
546 return false;
547 }
548
549 SkASSERT(!fDstInfo.isEmpty());
550 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
551 // Arguably, we could just skip the scanlines which are remaining,
552 // and return true. We choose to return false so the client
553 // can catch their bug.
554 return false;
555 }
556
557 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700558 fCurrScanline += countLines;
559 return result;
560}
561
msarette6dd0042015-10-09 11:07:34 -0700562int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400563 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700564 return this->onOutputScanline(inputScanline);
565}
scroggo46c57472015-09-30 08:57:13 -0700566
msarette6dd0042015-10-09 11:07:34 -0700567int SkCodec::onOutputScanline(int inputScanline) const {
568 switch (this->getScanlineOrder()) {
569 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700570 return inputScanline;
571 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400572 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700573 default:
574 // This case indicates an interlaced gif and is implemented by SkGifCodec.
575 SkASSERT(false);
576 return 0;
scroggo46c57472015-09-30 08:57:13 -0700577 }
msarette6dd0042015-10-09 11:07:34 -0700578}
scroggo46c57472015-09-30 08:57:13 -0700579
msarette6dd0042015-10-09 11:07:34 -0700580void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
581 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400582 if (kYes_ZeroInitialized == zeroInit) {
583 return;
584 }
msarette6dd0042015-10-09 11:07:34 -0700585
msarette6dd0042015-10-09 11:07:34 -0700586 const int linesRemaining = linesRequested - linesDecoded;
587 SkSampler* sampler = this->getSampler(false);
588
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400589 const int fillWidth = sampler ? sampler->fillWidth() :
590 fOptions.fSubset ? fOptions.fSubset->width() :
591 info.width() ;
592 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
593 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
594 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
595 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700596}
Matt Sarett313c4632016-10-20 12:35:23 -0400597
Leon Scroggins III179559f2018-12-10 12:30:12 -0500598bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
599 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400600 SkASSERT(outFormat);
601
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400602 switch (colorType) {
603 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400604 *outFormat = skcms_PixelFormat_RGBA_8888;
605 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400606 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400607 *outFormat = skcms_PixelFormat_BGRA_8888;
608 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400609 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400610 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400611#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400612 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400613#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400614 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400615#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400616 break;
617 }
618 *outFormat = skcms_PixelFormat_BGR_565;
619 break;
620 case kRGBA_F16_SkColorType:
621 *outFormat = skcms_PixelFormat_RGBA_hhhh;
622 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400623 case kGray_8_SkColorType:
624 *outFormat = skcms_PixelFormat_G_8;
625 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400626 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000627 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000628 }
Matt Sarett313c4632016-10-20 12:35:23 -0400629 return true;
630}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400631
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400632bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
633 bool srcIsOpaque) {
634 fXformTime = kNo_XformTime;
635 bool needsColorXform = false;
636 if (this->usesColorXform() && dstInfo.colorSpace()) {
637 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400638 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400639 needsColorXform = true;
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400640 } else {
641 const auto* srcProfile = fEncodedInfo.profile();
642 if (!srcProfile) {
643 srcProfile = skcms_sRGB_profile();
644 }
645 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
646 needsColorXform = true;
647 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400648 }
649 }
650
Leon Scroggins III712476e2018-10-03 15:47:00 -0400651 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400652 return false;
653 }
654
655 if (needsColorXform) {
656 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
657 || kRGBA_F16_SkColorType == dstInfo.colorType()
658 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500659 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
660 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400661 return false;
662 }
663 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
664 && dstInfo.alphaType() == kPremul_SkAlphaType) {
665 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
666 } else {
667 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
668 }
669 }
670 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400671}
672
673void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400674 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400675 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400676 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
677 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
678 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400679}
680
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400681std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400682 const int frameCount = this->getFrameCount();
683 SkASSERT(frameCount >= 0);
684 if (frameCount <= 0) {
685 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400686 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400687
688 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
689 // Not animated.
690 return std::vector<FrameInfo>{};
691 }
692
693 std::vector<FrameInfo> result(frameCount);
694 for (int i = 0; i < frameCount; ++i) {
695 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
696 }
697 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400698}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500699
700const char* SkCodec::ResultToString(Result result) {
701 switch (result) {
702 case kSuccess:
703 return "success";
704 case kIncompleteInput:
705 return "incomplete input";
706 case kErrorInInput:
707 return "error in input";
708 case kInvalidConversion:
709 return "invalid conversion";
710 case kInvalidScale:
711 return "invalid scale";
712 case kInvalidParameters:
713 return "invalid parameters";
714 case kInvalidInput:
715 return "invalid input";
716 case kCouldNotRewind:
717 return "could not rewind";
718 case kInternalError:
719 return "internal error";
720 case kUnimplemented:
721 return "unimplemented";
722 default:
723 SkASSERT(false);
724 return "bogus result value";
725 }
726}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000727
728static SkIRect frame_rect_on_screen(SkIRect frameRect,
729 const SkIRect& screenRect) {
730 if (!frameRect.intersect(screenRect)) {
731 return SkIRect::MakeEmpty();
732 }
733
734 return frameRect;
735}
736
737static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000738 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000739}
740
741static bool restore_bg(const SkFrame& frame) {
742 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
743}
744
Nigel Taob8ec7f12019-03-26 10:44:58 +1100745// As its name suggests, this method computes a frame's alpha (e.g. completely
746// opaque, unpremul, binary) and its required frame (a preceding frame that
747// this frame depends on, to draw the complete image at this frame's point in
748// the animation stream), and calls this frame's setter methods with that
749// computed information.
750//
751// A required frame of kNoFrame means that this frame is independent: drawing
752// the complete image at this frame's point in the animation stream does not
753// require first preparing the pixel buffer based on another frame. Instead,
754// drawing can start from an uninitialized pixel buffer.
755//
756// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
757// implementation, for independent frames, first party Skia code (in src/codec)
758// will typically fill the buffer with a uniform background color (e.g.
759// transparent black) before calling into third party codec-specific code (e.g.
760// libjpeg or libpng). Pixels outside of the frame's rect will remain this
761// background color after drawing this frame. For incomplete decodes, pixels
762// inside that rect may be (at least temporarily) set to that background color.
763// In an incremental decode, later passes may then overwrite that background
764// color.
765//
766// Determining kNoFrame or otherwise involves testing a number of conditions
767// sequentially. The first satisfied condition results in setting the required
768// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
769// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
770// labels also map to comments in the function body.
771//
772// - IND1: this frame is the first frame.
773// - IND2: this frame fills out the whole image, and it is completely opaque
774// or it overwrites (not blends with) the previous frame.
775// - IND3: all preceding frames' disposals are kRestorePrevious.
776// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
777// whole image or it is itself otherwise independent.
778// - DEP5: this frame reports alpha (it is not completely opaque) and it
779// blends with (not overwrites) the previous frame.
780// - IND6: this frame's rect covers the rects of all preceding frames back to
781// and including the most recent independent frame before this frame.
782// - DEP7: unconditional.
783//
784// The "prevFrame" variable initially points to the previous frame (also known
785// as the prior frame), but that variable may iterate further backwards over
786// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000787void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
788 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
789 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
790 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
791
792 const int i = frame->frameId();
793 if (0 == i) {
794 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100795 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000796 return;
797 }
798
799
800 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
801 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
802 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100803 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000804 return;
805 }
806
807 const SkFrame* prevFrame = this->getFrame(i-1);
808 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
809 const int prevId = prevFrame->frameId();
810 if (0 == prevId) {
811 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100812 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000813 return;
814 }
815
816 prevFrame = this->getFrame(prevId - 1);
817 }
818
819 const bool clearPrevFrame = restore_bg(*prevFrame);
820 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
821
822 if (clearPrevFrame) {
823 if (prevFrameRect == screenRect || independent(*prevFrame)) {
824 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100825 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000826 return;
827 }
828 }
829
830 if (reportsAlpha && blendWithPrevFrame) {
831 // Note: We could be more aggressive here. If prevFrame clears
832 // to background color and covers its required frame (and that
833 // frame is independent), prevFrame could be marked independent.
834 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100835 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000836 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
837 return;
838 }
839
840 while (frameRect.contains(prevFrameRect)) {
841 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000842 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100843 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000844 frame->setHasAlpha(true);
845 return;
846 }
847
848 prevFrame = this->getFrame(prevRequiredFrame);
849 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
850 }
851
Nigel Taob8ec7f12019-03-26 10:44:58 +1100852 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000853 if (restore_bg(*prevFrame)) {
854 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000855 return;
856 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000857 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000858 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
859}
860