blob: f51efe46b63a5f7fb5b0b6a6a405a19924f37c1d [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"
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040020#ifdef SK_CODEC_DECODES_PNG
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"
Hal Canary2dad9902019-11-20 16:01:31 -050029#elif defined(SK_USE_LIBGIFCODEC)
Hal Canary1ec9a282019-11-21 10:15:50 -050030#include "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> {
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040040 #ifdef SK_CODEC_DECODES_JPEG
Mike Klein159a9592019-04-26 15:47:56 +000041 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
42 #endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040043 #ifdef SK_CODEC_DECODES_WEBP
Mike Klein159a9592019-04-26 15:47:56 +000044 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
45 #endif
46 #ifdef SK_HAS_WUFFS_LIBRARY
47 { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
Hal Canary2dad9902019-11-20 16:01:31 -050048 #elif defined(SK_USE_LIBGIFCODEC)
Mike Klein159a9592019-04-26 15:47:56 +000049 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
50 #endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040051 #ifdef SK_CODEC_DECODES_PNG
Mike Klein159a9592019-04-26 15:47:56 +000052 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
53 #endif
54 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
55 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
Mike Klein159a9592019-04-26 15:47:56 +000056 };
57 return decoders;
58}
59
60void SkCodec::Register(
61 bool (*peek)(const void*, size_t),
62 std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)) {
63 decoders()->push_back(DecoderProc{peek, make});
64}
65
Leon Scroggins III6154ac42019-08-14 11:29:29 -040066std::unique_ptr<SkCodec> SkCodec::MakeFromStream(
67 std::unique_ptr<SkStream> stream, Result* outResult,
68 SkPngChunkReader* chunkReader, SelectionPolicy selectionPolicy) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040069 Result resultStorage;
70 if (!outResult) {
71 outResult = &resultStorage;
72 }
73
scroggof24f2242015-03-03 08:59:20 -080074 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040075 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070076 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080077 }
scroggo0a7e69c2015-04-03 07:22:22 -070078
Leon Scroggins III6154ac42019-08-14 11:29:29 -040079 if (selectionPolicy != SelectionPolicy::kPreferStillImage
80 && selectionPolicy != SelectionPolicy::kPreferAnimation) {
81 *outResult = kInvalidParameters;
82 return nullptr;
83 }
84
Leon Scroggins III04be2b52017-08-17 15:13:20 -040085 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080086
87 char buffer[bytesToRead];
88 size_t bytesRead = stream->peek(buffer, bytesToRead);
89
90 // It is also possible to have a complete image less than bytesToRead bytes
91 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
92 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
93 // than bytesToRead, so pass that directly to the decoder.
94 // It also is possible the stream uses too small a buffer for peeking, but
95 // we trust the caller to use a large enough buffer.
96
97 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080098 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
99 // printf could be useful to notice failures.
100 // SkCodecPrintf("Encoded image data failed to peek!\n");
101
scroggodb30be22015-12-08 18:54:13 -0800102 // It is possible the stream does not support peeking, but does support
103 // rewinding.
104 // Attempt to read() and pass the actual amount read to the decoder.
105 bytesRead = stream->read(buffer, bytesToRead);
106 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800107 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400108 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -0800109 return nullptr;
110 }
111 }
112
scroggocf98fa92015-11-23 08:14:40 -0800113 // PNG is special, since we want to be able to supply an SkPngChunkReader.
114 // But this code follows the same pattern as the loop.
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -0400115#ifdef SK_CODEC_DECODES_PNG
scroggodb30be22015-12-08 18:54:13 -0800116 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400117 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -0800118 } else
119#endif
120 {
Mike Klein159a9592019-04-26 15:47:56 +0000121 for (DecoderProc proc : *decoders()) {
scroggodb30be22015-12-08 18:54:13 -0800122 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400123 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800124 }
msarett74114382015-03-16 11:55:18 -0700125 }
yujieqin916de9f2016-01-25 08:26:16 -0800126
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400127#ifdef SK_HAS_HEIF_LIBRARY
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -0800128 SkEncodedImageFormat format;
129 if (SkHeifCodec::IsSupported(buffer, bytesRead, &format)) {
130 return SkHeifCodec::MakeFromStream(std::move(stream), selectionPolicy,
131 format, outResult);
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400132 }
133#endif
134
yujieqin916de9f2016-01-25 08:26:16 -0800135#ifdef SK_CODEC_DECODES_RAW
136 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400137 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800138#endif
scroggof24f2242015-03-03 08:59:20 -0800139 }
msarett8c8f22a2015-04-01 06:58:48 -0700140
Leon Scroggins III588fb042017-07-14 16:32:31 -0400141 if (bytesRead < bytesToRead) {
142 *outResult = kIncompleteInput;
143 } else {
144 *outResult = kUnimplemented;
145 }
146
msarettf44631b2016-01-13 10:54:20 -0800147 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800148}
149
Mike Reedede7bac2017-07-23 15:30:02 -0400150std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800151 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700152 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800153 }
Mike Reed847068c2017-07-26 11:35:53 -0400154 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800155}
156
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400157SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
158 SkEncodedOrigin origin)
159 : fEncodedInfo(std::move(info))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400160 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400161 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700162 , fNeedsRewind(false)
163 , fOrigin(origin)
164 , fDstInfo()
165 , fOptions()
166 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700167 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700168{}
169
scroggo9b2cdbf42015-07-10 12:07:02 -0700170SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700171
Brian Salomon59c60b02020-09-01 15:01:15 -0400172bool SkCodec::queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
173 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400174 if (!yuvaPixmapInfo) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400175 return false;
176 }
Brian Salomon59c60b02020-09-01 15:01:15 -0400177 return this->onQueryYUVAInfo(supportedDataTypes, yuvaPixmapInfo) &&
178 yuvaPixmapInfo->isSupported(supportedDataTypes);
Brian Salomon87d42e52020-08-24 09:18:16 -0400179}
180
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400181SkCodec::Result SkCodec::getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
182 if (!yuvaPixmaps.isValid()) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400183 return kInvalidInput;
184 }
185 if (!this->rewindIfNeeded()) {
186 return kCouldNotRewind;
187 }
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400188 return this->onGetYUVAPlanes(yuvaPixmaps);
Brian Salomon87d42e52020-08-24 09:18:16 -0400189}
190
Leon Scroggins III712476e2018-10-03 15:47:00 -0400191bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400192 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
193 return false;
194 }
195
196 switch (dst.colorType()) {
197 case kRGBA_8888_SkColorType:
198 case kBGRA_8888_SkColorType:
Leon Scroggins III07418182017-08-15 12:24:02 -0400199 case kRGBA_F16_SkColorType:
Leon Scroggins III196f3192020-02-03 12:39:54 -0500200 return true;
Leon Scroggins III07418182017-08-15 12:24:02 -0400201 case kRGB_565_SkColorType:
202 return srcIsOpaque;
203 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400204 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500205 case kAlpha_8_SkColorType:
206 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400207 // all of those other conversions yet.
208 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400209 default:
210 return false;
211 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400212}
213
scroggob427db12015-08-12 07:24:13 -0700214bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800215 // Store the value of fNeedsRewind so we can update it. Next read will
216 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700217 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800218 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700219 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700220 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700221 }
scroggob427db12015-08-12 07:24:13 -0700222
scroggo46c57472015-09-30 08:57:13 -0700223 // startScanlineDecode will need to be called before decoding scanlines.
224 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700225 // startIncrementalDecode will need to be called before incrementalDecode.
226 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700227
scroggo19b91532016-10-24 09:03:26 -0700228 // Some codecs do not have a stream. They may hold onto their own data or another codec.
229 // They must handle rewinding themselves.
230 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700231 return false;
232 }
233
234 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800235}
scroggo05245902015-03-25 11:11:52 -0700236
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400237static SkIRect frame_rect_on_screen(SkIRect frameRect,
238 const SkIRect& screenRect) {
239 if (!frameRect.intersect(screenRect)) {
240 return SkIRect::MakeEmpty();
241 }
242
243 return frameRect;
244}
245
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400246bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
247 SkISize srcDimensions, SkIRect prevRect) {
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400248 prevRect = frame_rect_on_screen(prevRect, SkIRect::MakeSize(srcDimensions));
249 if (prevRect.isEmpty()) {
250 return true;
251 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400252 const auto dimensions = dstInfo.dimensions();
253 if (dimensions != srcDimensions) {
254 SkRect src = SkRect::Make(srcDimensions);
255 SkRect dst = SkRect::Make(dimensions);
256 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
257 SkRect asRect = SkRect::Make(prevRect);
258 if (!map.mapRect(&asRect)) {
259 return false;
260 }
261 asRect.roundIn(&prevRect);
262 if (prevRect.isEmpty()) {
263 // Down-scaling shrank the empty portion to nothing,
264 // so nothing to zero.
265 return true;
266 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400267 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400268
Brian Salomon9241a6d2019-10-03 13:26:54 -0400269 const SkImageInfo info = dstInfo.makeDimensions(prevRect.size());
Mike Reed7fcfb622018-02-09 13:26:46 -0500270 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400271 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
272 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400273 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400274 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400275}
276
277SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
278 const Options& options) {
279 const int index = options.fFrameIndex;
280 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400281 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
282 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400283 }
284
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400285 if (index < 0) {
286 return kInvalidParameters;
287 }
288
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500289 if (options.fSubset) {
290 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400291 // a kRestoreBGColor frame.
292 return kInvalidParameters;
293 }
294
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400295 if (index >= this->onGetFrameCount()) {
296 return kIncompleteInput;
297 }
298
299 const auto* frameHolder = this->getFrameHolder();
300 SkASSERT(frameHolder);
301
302 const auto* frame = frameHolder->getFrame(index);
303 SkASSERT(frame);
304
305 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000306 if (requiredFrame != kNoFrame) {
307 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400308 // Check for a valid frame as a starting point. Alternatively, we could
309 // treat an invalid frame as not providing one, but rejecting it will
310 // make it easier to catch the mistake.
311 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400312 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400313 }
314 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
315 switch (prevFrame->getDisposalMethod()) {
316 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
317 return kInvalidParameters;
318 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
319 // If a frame after the required frame is provided, there is no
320 // need to clear, since it must be covered by the desired frame.
321 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500322 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400323 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400324 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500325 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400326 }
327 break;
328 default:
329 break;
330 }
331 } else {
332 Options prevFrameOptions(options);
333 prevFrameOptions.fFrameIndex = requiredFrame;
334 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
335 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
336 if (result != kSuccess) {
337 return result;
338 }
339 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
340 const auto disposalMethod = prevFrame->getDisposalMethod();
341 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400342 auto prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400343 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400344 return kInternalError;
345 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400346 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400347 }
348 }
349
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400350 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
351 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400352}
scroggo8e6c7ad2016-09-16 08:20:38 -0700353
Leon Scroggins III196f3192020-02-03 12:39:54 -0500354SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000355 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700356 if (kUnknown_SkColorType == info.colorType()) {
357 return kInvalidConversion;
358 }
halcanary96fcdcc2015-08-27 07:41:13 -0700359 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700360 return kInvalidParameters;
361 }
362 if (rowBytes < info.minRowBytes()) {
363 return kInvalidParameters;
364 }
365
scroggo3a7701c2015-09-30 09:15:14 -0700366 if (!this->rewindIfNeeded()) {
367 return kCouldNotRewind;
368 }
369
scroggoeb602a52015-07-09 08:16:03 -0700370 // Default options.
371 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700372 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700373 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400374 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400375 if (options->fSubset) {
376 SkIRect subset(*options->fSubset);
377 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
378 // FIXME: How to differentiate between not supporting subset at all
379 // and not supporting this particular subset?
380 return kUnimplemented;
381 }
scroggoe7fc14b2015-10-02 13:14:46 -0700382 }
scroggoeb602a52015-07-09 08:16:03 -0700383 }
scroggoe7fc14b2015-10-02 13:14:46 -0700384
Leon Scroggins III07418182017-08-15 12:24:02 -0400385 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
386 *options);
387 if (frameIndexResult != kSuccess) {
388 return frameIndexResult;
389 }
390
scroggoe7fc14b2015-10-02 13:14:46 -0700391 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
392 // because it supports arbitrary scaling/subset combinations.
393 if (!this->dimensionsSupported(info.dimensions())) {
394 return kInvalidScale;
395 }
396
scroggo8e6c7ad2016-09-16 08:20:38 -0700397 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500398 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700399
msarette6dd0042015-10-09 11:07:34 -0700400 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
401 // successfully.
402 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000403 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700404
405 // A return value of kIncompleteInput indicates a truncated image stream.
406 // In this case, we will fill any uninitialized memory with a default value.
407 // Some subclasses will take care of filling any uninitialized memory on
408 // their own. They indicate that all of the memory has been filled by
409 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400410 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500411 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
412 // there is a subset. In that case, it will use the width of the subset. From here, the
413 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
414 // differenty from the other codecs, and it needs to use the width specified by the info.
415 // Set the subset to null so SkWebpCodec uses the correct width.
416 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700417 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
418 rowsDecoded);
419 }
420
scroggoeb602a52015-07-09 08:16:03 -0700421 return result;
422}
423
Leon Scroggins III196f3192020-02-03 12:39:54 -0500424SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000425 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700426 fStartedIncrementalDecode = false;
427
428 if (kUnknown_SkColorType == info.colorType()) {
429 return kInvalidConversion;
430 }
431 if (nullptr == pixels) {
432 return kInvalidParameters;
433 }
434
scroggo8e6c7ad2016-09-16 08:20:38 -0700435 // FIXME: If the rows come after the rows of a previous incremental decode,
436 // we might be able to skip the rewind, but only the implementation knows
437 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
438 // a bottom-up BMP could skip rewinding if the new rows are above the old
439 // rows.)
440 if (!this->rewindIfNeeded()) {
441 return kCouldNotRewind;
442 }
443
444 // Set options.
445 Options optsStorage;
446 if (nullptr == options) {
447 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400448 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400449 if (options->fSubset) {
450 SkIRect size = SkIRect::MakeSize(info.dimensions());
451 if (!size.contains(*options->fSubset)) {
452 return kInvalidParameters;
453 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700454
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400455 const int top = options->fSubset->top();
456 const int bottom = options->fSubset->bottom();
457 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
458 return kInvalidParameters;
459 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700460 }
461 }
462
Leon Scroggins III07418182017-08-15 12:24:02 -0400463 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
464 *options);
465 if (frameIndexResult != kSuccess) {
466 return frameIndexResult;
467 }
468
scroggo8e6c7ad2016-09-16 08:20:38 -0700469 if (!this->dimensionsSupported(info.dimensions())) {
470 return kInvalidScale;
471 }
472
473 fDstInfo = info;
474 fOptions = *options;
475
Leon Scroggins571b30f2017-07-11 17:35:31 +0000476 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700477 if (kSuccess == result) {
478 fStartedIncrementalDecode = true;
479 } else if (kUnimplemented == result) {
480 // FIXME: This is temporarily necessary, until we transition SkCodec
481 // implementations from scanline decoding to incremental decoding.
482 // SkAndroidCodec will first attempt to use incremental decoding, but
483 // will fall back to scanline decoding if incremental returns
484 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
485 // (after potentially rewinding), but we do not want the next call to
486 // startScanlineDecode() to do a rewind.
487 fNeedsRewind = false;
488 }
489 return result;
490}
491
492
Leon Scroggins III196f3192020-02-03 12:39:54 -0500493SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000494 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700495 // Reset fCurrScanline in case of failure.
496 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700497
scroggo3a7701c2015-09-30 09:15:14 -0700498 if (!this->rewindIfNeeded()) {
499 return kCouldNotRewind;
500 }
501
scroggo46c57472015-09-30 08:57:13 -0700502 // Set options.
503 Options optsStorage;
504 if (nullptr == options) {
505 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700506 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700507 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700508 if (!size.contains(*options->fSubset)) {
509 return kInvalidInput;
510 }
511
512 // We only support subsetting in the x-dimension for scanline decoder.
513 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700514 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700515 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700516 }
517 }
518
Leon Scroggins III07418182017-08-15 12:24:02 -0400519 // Scanline decoding only supports decoding the first frame.
520 if (options->fFrameIndex != 0) {
521 return kUnimplemented;
522 }
523
524 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
525 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
526 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
527 if (frameIndexResult != kSuccess) {
528 return frameIndexResult;
529 }
530
scroggoe7fc14b2015-10-02 13:14:46 -0700531 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700532 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700533 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700534 }
535
Leon Scroggins571b30f2017-07-11 17:35:31 +0000536 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700537 if (result != SkCodec::kSuccess) {
538 return result;
539 }
540
541 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700542 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700543 fOptions = *options;
544 return kSuccess;
545}
546
msarette6dd0042015-10-09 11:07:34 -0700547int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700548 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700549 return 0;
scroggo46c57472015-09-30 08:57:13 -0700550 }
551
552 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700553 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700554 return 0;
scroggo46c57472015-09-30 08:57:13 -0700555 }
556
msarette6dd0042015-10-09 11:07:34 -0700557 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
558 if (linesDecoded < countLines) {
559 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
560 countLines, linesDecoded);
561 }
562 fCurrScanline += countLines;
563 return linesDecoded;
564}
565
566bool SkCodec::skipScanlines(int countLines) {
567 if (fCurrScanline < 0) {
568 return false;
569 }
570
571 SkASSERT(!fDstInfo.isEmpty());
572 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
573 // Arguably, we could just skip the scanlines which are remaining,
574 // and return true. We choose to return false so the client
575 // can catch their bug.
576 return false;
577 }
578
579 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700580 fCurrScanline += countLines;
581 return result;
582}
583
msarette6dd0042015-10-09 11:07:34 -0700584int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400585 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700586 return this->onOutputScanline(inputScanline);
587}
scroggo46c57472015-09-30 08:57:13 -0700588
msarette6dd0042015-10-09 11:07:34 -0700589int SkCodec::onOutputScanline(int inputScanline) const {
590 switch (this->getScanlineOrder()) {
591 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700592 return inputScanline;
593 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400594 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700595 default:
596 // This case indicates an interlaced gif and is implemented by SkGifCodec.
597 SkASSERT(false);
598 return 0;
scroggo46c57472015-09-30 08:57:13 -0700599 }
msarette6dd0042015-10-09 11:07:34 -0700600}
scroggo46c57472015-09-30 08:57:13 -0700601
msarette6dd0042015-10-09 11:07:34 -0700602void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
603 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400604 if (kYes_ZeroInitialized == zeroInit) {
605 return;
606 }
msarette6dd0042015-10-09 11:07:34 -0700607
msarette6dd0042015-10-09 11:07:34 -0700608 const int linesRemaining = linesRequested - linesDecoded;
609 SkSampler* sampler = this->getSampler(false);
610
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400611 const int fillWidth = sampler ? sampler->fillWidth() :
612 fOptions.fSubset ? fOptions.fSubset->width() :
613 info.width() ;
614 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
615 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
616 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
617 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700618}
Matt Sarett313c4632016-10-20 12:35:23 -0400619
Leon Scroggins III179559f2018-12-10 12:30:12 -0500620bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
621 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400622 SkASSERT(outFormat);
623
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400624 switch (colorType) {
625 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400626 *outFormat = skcms_PixelFormat_RGBA_8888;
627 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400628 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400629 *outFormat = skcms_PixelFormat_BGRA_8888;
630 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400631 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400632 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400633#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400634 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400635#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400636 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400637#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400638 break;
639 }
640 *outFormat = skcms_PixelFormat_BGR_565;
641 break;
642 case kRGBA_F16_SkColorType:
643 *outFormat = skcms_PixelFormat_RGBA_hhhh;
644 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400645 case kGray_8_SkColorType:
646 *outFormat = skcms_PixelFormat_G_8;
647 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400648 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000649 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000650 }
Matt Sarett313c4632016-10-20 12:35:23 -0400651 return true;
652}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400653
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400654bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
655 bool srcIsOpaque) {
656 fXformTime = kNo_XformTime;
657 bool needsColorXform = false;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500658 if (this->usesColorXform()) {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400659 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400660 needsColorXform = true;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500661 if (dstInfo.colorSpace()) {
662 dstInfo.colorSpace()->toProfile(&fDstProfile);
663 } else {
664 // Use the srcProfile to avoid conversion.
665 const auto* srcProfile = fEncodedInfo.profile();
666 fDstProfile = srcProfile ? *srcProfile : *skcms_sRGB_profile();
667 }
668 } else if (dstInfo.colorSpace()) {
669 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400670 const auto* srcProfile = fEncodedInfo.profile();
671 if (!srcProfile) {
672 srcProfile = skcms_sRGB_profile();
673 }
674 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
675 needsColorXform = true;
676 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400677 }
678 }
679
Leon Scroggins III712476e2018-10-03 15:47:00 -0400680 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400681 return false;
682 }
683
684 if (needsColorXform) {
685 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
686 || kRGBA_F16_SkColorType == dstInfo.colorType()
687 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500688 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
689 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400690 return false;
691 }
692 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
693 && dstInfo.alphaType() == kPremul_SkAlphaType) {
694 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
695 } else {
696 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
697 }
698 }
699 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400700}
701
702void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400703 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400704 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400705 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
706 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
707 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400708}
709
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400710std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400711 const int frameCount = this->getFrameCount();
712 SkASSERT(frameCount >= 0);
713 if (frameCount <= 0) {
714 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400715 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400716
717 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
718 // Not animated.
719 return std::vector<FrameInfo>{};
720 }
721
722 std::vector<FrameInfo> result(frameCount);
723 for (int i = 0; i < frameCount; ++i) {
724 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
725 }
726 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400727}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500728
729const char* SkCodec::ResultToString(Result result) {
730 switch (result) {
731 case kSuccess:
732 return "success";
733 case kIncompleteInput:
734 return "incomplete input";
735 case kErrorInInput:
736 return "error in input";
737 case kInvalidConversion:
738 return "invalid conversion";
739 case kInvalidScale:
740 return "invalid scale";
741 case kInvalidParameters:
742 return "invalid parameters";
743 case kInvalidInput:
744 return "invalid input";
745 case kCouldNotRewind:
746 return "could not rewind";
747 case kInternalError:
748 return "internal error";
749 case kUnimplemented:
750 return "unimplemented";
751 default:
752 SkASSERT(false);
753 return "bogus result value";
754 }
755}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000756
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000757static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000758 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000759}
760
761static bool restore_bg(const SkFrame& frame) {
762 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
763}
764
Nigel Taob8ec7f12019-03-26 10:44:58 +1100765// As its name suggests, this method computes a frame's alpha (e.g. completely
766// opaque, unpremul, binary) and its required frame (a preceding frame that
767// this frame depends on, to draw the complete image at this frame's point in
768// the animation stream), and calls this frame's setter methods with that
769// computed information.
770//
771// A required frame of kNoFrame means that this frame is independent: drawing
772// the complete image at this frame's point in the animation stream does not
773// require first preparing the pixel buffer based on another frame. Instead,
774// drawing can start from an uninitialized pixel buffer.
775//
776// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
777// implementation, for independent frames, first party Skia code (in src/codec)
778// will typically fill the buffer with a uniform background color (e.g.
779// transparent black) before calling into third party codec-specific code (e.g.
780// libjpeg or libpng). Pixels outside of the frame's rect will remain this
781// background color after drawing this frame. For incomplete decodes, pixels
782// inside that rect may be (at least temporarily) set to that background color.
783// In an incremental decode, later passes may then overwrite that background
784// color.
785//
786// Determining kNoFrame or otherwise involves testing a number of conditions
787// sequentially. The first satisfied condition results in setting the required
788// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
789// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
790// labels also map to comments in the function body.
791//
792// - IND1: this frame is the first frame.
793// - IND2: this frame fills out the whole image, and it is completely opaque
794// or it overwrites (not blends with) the previous frame.
795// - IND3: all preceding frames' disposals are kRestorePrevious.
796// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
797// whole image or it is itself otherwise independent.
798// - DEP5: this frame reports alpha (it is not completely opaque) and it
799// blends with (not overwrites) the previous frame.
800// - IND6: this frame's rect covers the rects of all preceding frames back to
801// and including the most recent independent frame before this frame.
802// - DEP7: unconditional.
803//
804// The "prevFrame" variable initially points to the previous frame (also known
805// as the prior frame), but that variable may iterate further backwards over
806// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000807void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
808 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
809 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
810 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
811
812 const int i = frame->frameId();
813 if (0 == i) {
814 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100815 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000816 return;
817 }
818
819
820 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
821 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
822 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100823 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000824 return;
825 }
826
827 const SkFrame* prevFrame = this->getFrame(i-1);
828 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
829 const int prevId = prevFrame->frameId();
830 if (0 == prevId) {
831 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100832 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000833 return;
834 }
835
836 prevFrame = this->getFrame(prevId - 1);
837 }
838
839 const bool clearPrevFrame = restore_bg(*prevFrame);
840 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
841
842 if (clearPrevFrame) {
843 if (prevFrameRect == screenRect || independent(*prevFrame)) {
844 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100845 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000846 return;
847 }
848 }
849
850 if (reportsAlpha && blendWithPrevFrame) {
851 // Note: We could be more aggressive here. If prevFrame clears
852 // to background color and covers its required frame (and that
853 // frame is independent), prevFrame could be marked independent.
854 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100855 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000856 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
857 return;
858 }
859
860 while (frameRect.contains(prevFrameRect)) {
861 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000862 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100863 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000864 frame->setHasAlpha(true);
865 return;
866 }
867
868 prevFrame = this->getFrame(prevRequiredFrame);
869 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
870 }
871
Nigel Taob8ec7f12019-03-26 10:44:58 +1100872 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000873 if (restore_bg(*prevFrame)) {
874 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000875 return;
876 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000877 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000878 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
879}
880