blob: 533cef53097560a094fedb49426e8d5165ce193f [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
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -05008#include "include/codec/SkAndroidCodec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/codec/SkCodec.h"
10#include "include/core/SkColorSpace.h"
11#include "include/core/SkData.h"
12#include "include/private/SkHalf.h"
13#include "src/codec/SkBmpCodec.h"
14#include "src/codec/SkCodecPriv.h"
15#include "src/codec/SkFrameHolder.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040016#ifdef SK_HAS_HEIF_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/codec/SkHeifCodec.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040018#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/codec/SkIcoCodec.h"
20#include "src/codec/SkJpegCodec.h"
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040021#ifdef SK_CODEC_DECODES_PNG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/codec/SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080023#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkStream.h"
25#include "src/codec/SkRawCodec.h"
26#include "src/codec/SkWbmpCodec.h"
27#include "src/codec/SkWebpCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040028#ifdef SK_HAS_WUFFS_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/codec/SkWuffsCodec.h"
Hal Canary2dad9902019-11-20 16:01:31 -050030#elif defined(SK_USE_LIBGIFCODEC)
Hal Canary1ec9a282019-11-21 10:15:50 -050031#include "SkGifCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040032#endif
scroggof24f2242015-03-03 08:59:20 -080033
msarett74114382015-03-16 11:55:18 -070034struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080035 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040036 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070037};
38
Mike Klein159a9592019-04-26 15:47:56 +000039static std::vector<DecoderProc>* decoders() {
40 static auto* decoders = new std::vector<DecoderProc> {
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040041 #ifdef SK_CODEC_DECODES_JPEG
Mike Klein159a9592019-04-26 15:47:56 +000042 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
43 #endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040044 #ifdef SK_CODEC_DECODES_WEBP
Mike Klein159a9592019-04-26 15:47:56 +000045 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
46 #endif
47 #ifdef SK_HAS_WUFFS_LIBRARY
48 { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
Hal Canary2dad9902019-11-20 16:01:31 -050049 #elif defined(SK_USE_LIBGIFCODEC)
Mike Klein159a9592019-04-26 15:47:56 +000050 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
51 #endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040052 #ifdef SK_CODEC_DECODES_PNG
Mike Klein159a9592019-04-26 15:47:56 +000053 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
54 #endif
55 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
56 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
Mike Klein159a9592019-04-26 15:47:56 +000057 };
58 return decoders;
59}
60
61void SkCodec::Register(
62 bool (*peek)(const void*, size_t),
63 std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)) {
64 decoders()->push_back(DecoderProc{peek, make});
65}
66
Leon Scroggins III6154ac42019-08-14 11:29:29 -040067std::unique_ptr<SkCodec> SkCodec::MakeFromStream(
68 std::unique_ptr<SkStream> stream, Result* outResult,
69 SkPngChunkReader* chunkReader, SelectionPolicy selectionPolicy) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040070 Result resultStorage;
71 if (!outResult) {
72 outResult = &resultStorage;
73 }
74
scroggof24f2242015-03-03 08:59:20 -080075 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040076 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070077 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080078 }
scroggo0a7e69c2015-04-03 07:22:22 -070079
Leon Scroggins III6154ac42019-08-14 11:29:29 -040080 if (selectionPolicy != SelectionPolicy::kPreferStillImage
81 && selectionPolicy != SelectionPolicy::kPreferAnimation) {
82 *outResult = kInvalidParameters;
83 return nullptr;
84 }
85
Leon Scroggins III04be2b52017-08-17 15:13:20 -040086 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080087
88 char buffer[bytesToRead];
89 size_t bytesRead = stream->peek(buffer, bytesToRead);
90
91 // It is also possible to have a complete image less than bytesToRead bytes
92 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
93 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
94 // than bytesToRead, so pass that directly to the decoder.
95 // It also is possible the stream uses too small a buffer for peeking, but
96 // we trust the caller to use a large enough buffer.
97
98 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080099 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
100 // printf could be useful to notice failures.
101 // SkCodecPrintf("Encoded image data failed to peek!\n");
102
scroggodb30be22015-12-08 18:54:13 -0800103 // It is possible the stream does not support peeking, but does support
104 // rewinding.
105 // Attempt to read() and pass the actual amount read to the decoder.
106 bytesRead = stream->read(buffer, bytesToRead);
107 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800108 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400109 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -0800110 return nullptr;
111 }
112 }
113
scroggocf98fa92015-11-23 08:14:40 -0800114 // PNG is special, since we want to be able to supply an SkPngChunkReader.
115 // But this code follows the same pattern as the loop.
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -0400116#ifdef SK_CODEC_DECODES_PNG
scroggodb30be22015-12-08 18:54:13 -0800117 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400118 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -0800119 } else
120#endif
121 {
Mike Klein159a9592019-04-26 15:47:56 +0000122 for (DecoderProc proc : *decoders()) {
scroggodb30be22015-12-08 18:54:13 -0800123 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400124 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800125 }
msarett74114382015-03-16 11:55:18 -0700126 }
yujieqin916de9f2016-01-25 08:26:16 -0800127
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400128#ifdef SK_HAS_HEIF_LIBRARY
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -0800129 SkEncodedImageFormat format;
130 if (SkHeifCodec::IsSupported(buffer, bytesRead, &format)) {
131 return SkHeifCodec::MakeFromStream(std::move(stream), selectionPolicy,
132 format, outResult);
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400133 }
134#endif
135
yujieqin916de9f2016-01-25 08:26:16 -0800136#ifdef SK_CODEC_DECODES_RAW
137 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400138 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800139#endif
scroggof24f2242015-03-03 08:59:20 -0800140 }
msarett8c8f22a2015-04-01 06:58:48 -0700141
Leon Scroggins III588fb042017-07-14 16:32:31 -0400142 if (bytesRead < bytesToRead) {
143 *outResult = kIncompleteInput;
144 } else {
145 *outResult = kUnimplemented;
146 }
147
msarettf44631b2016-01-13 10:54:20 -0800148 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800149}
150
Mike Reedede7bac2017-07-23 15:30:02 -0400151std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800152 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700153 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800154 }
Mike Reed847068c2017-07-26 11:35:53 -0400155 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800156}
157
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400158SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
159 SkEncodedOrigin origin)
160 : fEncodedInfo(std::move(info))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400161 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400162 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700163 , fNeedsRewind(false)
164 , fOrigin(origin)
165 , fDstInfo()
166 , fOptions()
167 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700168 , fStartedIncrementalDecode(false)
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500169 , fAndroidCodecHandlesFrameIndex(false)
msarett549ca322016-08-17 08:54:08 -0700170{}
171
scroggo9b2cdbf42015-07-10 12:07:02 -0700172SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700173
Brian Salomon59c60b02020-09-01 15:01:15 -0400174bool SkCodec::queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
175 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400176 if (!yuvaPixmapInfo) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400177 return false;
178 }
Brian Salomon59c60b02020-09-01 15:01:15 -0400179 return this->onQueryYUVAInfo(supportedDataTypes, yuvaPixmapInfo) &&
180 yuvaPixmapInfo->isSupported(supportedDataTypes);
Brian Salomon87d42e52020-08-24 09:18:16 -0400181}
182
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400183SkCodec::Result SkCodec::getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
184 if (!yuvaPixmaps.isValid()) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400185 return kInvalidInput;
186 }
187 if (!this->rewindIfNeeded()) {
188 return kCouldNotRewind;
189 }
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400190 return this->onGetYUVAPlanes(yuvaPixmaps);
Brian Salomon87d42e52020-08-24 09:18:16 -0400191}
192
Leon Scroggins III712476e2018-10-03 15:47:00 -0400193bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400194 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
195 return false;
196 }
197
198 switch (dst.colorType()) {
199 case kRGBA_8888_SkColorType:
200 case kBGRA_8888_SkColorType:
Leon Scroggins III07418182017-08-15 12:24:02 -0400201 case kRGBA_F16_SkColorType:
Leon Scroggins III196f3192020-02-03 12:39:54 -0500202 return true;
Leon Scroggins III07418182017-08-15 12:24:02 -0400203 case kRGB_565_SkColorType:
204 return srcIsOpaque;
205 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400206 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500207 case kAlpha_8_SkColorType:
208 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400209 // all of those other conversions yet.
210 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400211 default:
212 return false;
213 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400214}
215
scroggob427db12015-08-12 07:24:13 -0700216bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800217 // Store the value of fNeedsRewind so we can update it. Next read will
218 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700219 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800220 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700221 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700222 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700223 }
scroggob427db12015-08-12 07:24:13 -0700224
scroggo46c57472015-09-30 08:57:13 -0700225 // startScanlineDecode will need to be called before decoding scanlines.
226 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700227 // startIncrementalDecode will need to be called before incrementalDecode.
228 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700229
scroggo19b91532016-10-24 09:03:26 -0700230 // Some codecs do not have a stream. They may hold onto their own data or another codec.
231 // They must handle rewinding themselves.
232 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700233 return false;
234 }
235
236 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800237}
scroggo05245902015-03-25 11:11:52 -0700238
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400239static SkIRect frame_rect_on_screen(SkIRect frameRect,
240 const SkIRect& screenRect) {
241 if (!frameRect.intersect(screenRect)) {
242 return SkIRect::MakeEmpty();
243 }
244
245 return frameRect;
246}
247
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400248bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
249 SkISize srcDimensions, SkIRect prevRect) {
250 const auto dimensions = dstInfo.dimensions();
251 if (dimensions != srcDimensions) {
252 SkRect src = SkRect::Make(srcDimensions);
253 SkRect dst = SkRect::Make(dimensions);
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500254 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400255 SkRect asRect = SkRect::Make(prevRect);
256 if (!map.mapRect(&asRect)) {
257 return false;
258 }
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500259 asRect.roundOut(&prevRect);
260 }
261
262 if (!prevRect.intersect(SkIRect::MakeSize(dimensions))) {
263 // Nothing to zero, due to scaling or bad frame rect.
264 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400265 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400266
Brian Salomon9241a6d2019-10-03 13:26:54 -0400267 const SkImageInfo info = dstInfo.makeDimensions(prevRect.size());
Mike Reed7fcfb622018-02-09 13:26:46 -0500268 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400269 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
270 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400271 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400272 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400273}
274
275SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500276 const Options& options, SkAndroidCodec* androidCodec) {
277 if (androidCodec) {
278 // This is never set back to false. If SkAndroidCodec is calling this method, its fCodec
279 // should never call it directly.
280 fAndroidCodecHandlesFrameIndex = true;
281 } else if (fAndroidCodecHandlesFrameIndex) {
282 return kSuccess;
283 }
284
285 if (!this->rewindIfNeeded()) {
286 return kCouldNotRewind;
287 }
288
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400289 const int index = options.fFrameIndex;
290 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400291 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
292 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400293 }
294
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400295 if (index < 0) {
296 return kInvalidParameters;
297 }
298
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500299 if (options.fSubset) {
300 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400301 // a kRestoreBGColor frame.
302 return kInvalidParameters;
303 }
304
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400305 if (index >= this->onGetFrameCount()) {
306 return kIncompleteInput;
307 }
308
309 const auto* frameHolder = this->getFrameHolder();
310 SkASSERT(frameHolder);
311
312 const auto* frame = frameHolder->getFrame(index);
313 SkASSERT(frame);
314
315 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000316 if (requiredFrame != kNoFrame) {
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500317 const SkFrame* preppedFrame = nullptr;
318 if (options.fPriorFrame == kNoFrame) {
319 Result result = kInternalError;
320 if (androidCodec) {
321#ifdef SK_HAS_ANDROID_CODEC
322 SkAndroidCodec::AndroidOptions prevFrameOptions(
323 reinterpret_cast<const SkAndroidCodec::AndroidOptions&>(options));
324 prevFrameOptions.fFrameIndex = requiredFrame;
325 result = androidCodec->getAndroidPixels(info, pixels, rowBytes, &prevFrameOptions);
326#endif
327 } else {
328 Options prevFrameOptions(options);
329 prevFrameOptions.fFrameIndex = requiredFrame;
330 result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
331 }
332 if (result != kSuccess) {
333 return result;
334 }
335 preppedFrame = frameHolder->getFrame(requiredFrame);
336 } else {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400337 // Check for a valid frame as a starting point. Alternatively, we could
338 // treat an invalid frame as not providing one, but rejecting it will
339 // make it easier to catch the mistake.
340 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400341 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400342 }
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500343 preppedFrame = frameHolder->getFrame(options.fPriorFrame);
344 }
345
346 SkASSERT(preppedFrame);
347 switch (preppedFrame->getDisposalMethod()) {
348 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
349 SkASSERT(options.fPriorFrame != kNoFrame);
350 return kInvalidParameters;
351 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
352 // If a frame after the required frame is provided, there is no
353 // need to clear, since it must be covered by the desired frame.
354 // FIXME: If the required frame is kRestoreBGColor, we don't actually need to decode
355 // it, since we'll just clear it to transparent. Instead, we could decode *its*
356 // required frame and then clear.
357 if (preppedFrame->frameId() == requiredFrame) {
358 SkIRect preppedRect = preppedFrame->frameRect();
359 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), preppedRect)) {
360 return kInternalError;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400361 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400362 }
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500363 break;
364 default:
365 break;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400366 }
367 }
368
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400369 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
370 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400371}
scroggo8e6c7ad2016-09-16 08:20:38 -0700372
Leon Scroggins III196f3192020-02-03 12:39:54 -0500373SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000374 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700375 if (kUnknown_SkColorType == info.colorType()) {
376 return kInvalidConversion;
377 }
halcanary96fcdcc2015-08-27 07:41:13 -0700378 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700379 return kInvalidParameters;
380 }
381 if (rowBytes < info.minRowBytes()) {
382 return kInvalidParameters;
383 }
384
scroggoeb602a52015-07-09 08:16:03 -0700385 // Default options.
386 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700387 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700388 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400389 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400390 if (options->fSubset) {
391 SkIRect subset(*options->fSubset);
392 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
393 // FIXME: How to differentiate between not supporting subset at all
394 // and not supporting this particular subset?
395 return kUnimplemented;
396 }
scroggoe7fc14b2015-10-02 13:14:46 -0700397 }
scroggoeb602a52015-07-09 08:16:03 -0700398 }
scroggoe7fc14b2015-10-02 13:14:46 -0700399
Leon Scroggins III07418182017-08-15 12:24:02 -0400400 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
401 *options);
402 if (frameIndexResult != kSuccess) {
403 return frameIndexResult;
404 }
405
scroggoe7fc14b2015-10-02 13:14:46 -0700406 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
407 // because it supports arbitrary scaling/subset combinations.
408 if (!this->dimensionsSupported(info.dimensions())) {
409 return kInvalidScale;
410 }
411
scroggo8e6c7ad2016-09-16 08:20:38 -0700412 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500413 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700414
msarette6dd0042015-10-09 11:07:34 -0700415 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
416 // successfully.
417 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000418 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700419
420 // A return value of kIncompleteInput indicates a truncated image stream.
421 // In this case, we will fill any uninitialized memory with a default value.
422 // Some subclasses will take care of filling any uninitialized memory on
423 // their own. They indicate that all of the memory has been filled by
424 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400425 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500426 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
427 // there is a subset. In that case, it will use the width of the subset. From here, the
428 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
429 // differenty from the other codecs, and it needs to use the width specified by the info.
430 // Set the subset to null so SkWebpCodec uses the correct width.
431 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700432 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
433 rowsDecoded);
434 }
435
scroggoeb602a52015-07-09 08:16:03 -0700436 return result;
437}
438
Leon Scroggins III196f3192020-02-03 12:39:54 -0500439SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000440 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700441 fStartedIncrementalDecode = false;
442
443 if (kUnknown_SkColorType == info.colorType()) {
444 return kInvalidConversion;
445 }
446 if (nullptr == pixels) {
447 return kInvalidParameters;
448 }
449
scroggo8e6c7ad2016-09-16 08:20:38 -0700450 // Set options.
451 Options optsStorage;
452 if (nullptr == options) {
453 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400454 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400455 if (options->fSubset) {
456 SkIRect size = SkIRect::MakeSize(info.dimensions());
457 if (!size.contains(*options->fSubset)) {
458 return kInvalidParameters;
459 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700460
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400461 const int top = options->fSubset->top();
462 const int bottom = options->fSubset->bottom();
463 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
464 return kInvalidParameters;
465 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700466 }
467 }
468
Leon Scroggins III07418182017-08-15 12:24:02 -0400469 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
470 *options);
471 if (frameIndexResult != kSuccess) {
472 return frameIndexResult;
473 }
474
scroggo8e6c7ad2016-09-16 08:20:38 -0700475 if (!this->dimensionsSupported(info.dimensions())) {
476 return kInvalidScale;
477 }
478
479 fDstInfo = info;
480 fOptions = *options;
481
Leon Scroggins571b30f2017-07-11 17:35:31 +0000482 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700483 if (kSuccess == result) {
484 fStartedIncrementalDecode = true;
485 } else if (kUnimplemented == result) {
486 // FIXME: This is temporarily necessary, until we transition SkCodec
487 // implementations from scanline decoding to incremental decoding.
488 // SkAndroidCodec will first attempt to use incremental decoding, but
489 // will fall back to scanline decoding if incremental returns
490 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
491 // (after potentially rewinding), but we do not want the next call to
492 // startScanlineDecode() to do a rewind.
493 fNeedsRewind = false;
494 }
495 return result;
496}
497
498
Leon Scroggins III196f3192020-02-03 12:39:54 -0500499SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000500 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700501 // Reset fCurrScanline in case of failure.
502 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700503
504 // Set options.
505 Options optsStorage;
506 if (nullptr == options) {
507 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700508 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700509 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700510 if (!size.contains(*options->fSubset)) {
511 return kInvalidInput;
512 }
513
514 // We only support subsetting in the x-dimension for scanline decoder.
515 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700516 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700517 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700518 }
519 }
520
Leon Scroggins III07418182017-08-15 12:24:02 -0400521 // Scanline decoding only supports decoding the first frame.
522 if (options->fFrameIndex != 0) {
523 return kUnimplemented;
524 }
525
526 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
527 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
528 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
529 if (frameIndexResult != kSuccess) {
530 return frameIndexResult;
531 }
532
scroggoe7fc14b2015-10-02 13:14:46 -0700533 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700534 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700535 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700536 }
537
Leon Scroggins571b30f2017-07-11 17:35:31 +0000538 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700539 if (result != SkCodec::kSuccess) {
540 return result;
541 }
542
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500543 // FIXME: See startIncrementalDecode. That method set fNeedsRewind to false
544 // so that when onStartScanlineDecode calls rewindIfNeeded it would not
545 // rewind. But it also relies on that call to rewindIfNeeded to set
546 // fNeedsRewind to true for future decodes. When
547 // fAndroidCodecHandlesFrameIndex is true, that call to rewindIfNeeded is
548 // skipped, so this method sets it back to true.
549 SkASSERT(fAndroidCodecHandlesFrameIndex || fNeedsRewind);
550 fNeedsRewind = true;
551
scroggo46c57472015-09-30 08:57:13 -0700552 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700553 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700554 fOptions = *options;
555 return kSuccess;
556}
557
msarette6dd0042015-10-09 11:07:34 -0700558int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700559 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700560 return 0;
scroggo46c57472015-09-30 08:57:13 -0700561 }
562
563 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700564 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700565 return 0;
scroggo46c57472015-09-30 08:57:13 -0700566 }
567
msarette6dd0042015-10-09 11:07:34 -0700568 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
569 if (linesDecoded < countLines) {
570 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
571 countLines, linesDecoded);
572 }
573 fCurrScanline += countLines;
574 return linesDecoded;
575}
576
577bool SkCodec::skipScanlines(int countLines) {
578 if (fCurrScanline < 0) {
579 return false;
580 }
581
582 SkASSERT(!fDstInfo.isEmpty());
583 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
584 // Arguably, we could just skip the scanlines which are remaining,
585 // and return true. We choose to return false so the client
586 // can catch their bug.
587 return false;
588 }
589
590 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700591 fCurrScanline += countLines;
592 return result;
593}
594
msarette6dd0042015-10-09 11:07:34 -0700595int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400596 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700597 return this->onOutputScanline(inputScanline);
598}
scroggo46c57472015-09-30 08:57:13 -0700599
msarette6dd0042015-10-09 11:07:34 -0700600int SkCodec::onOutputScanline(int inputScanline) const {
601 switch (this->getScanlineOrder()) {
602 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700603 return inputScanline;
604 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400605 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700606 default:
607 // This case indicates an interlaced gif and is implemented by SkGifCodec.
608 SkASSERT(false);
609 return 0;
scroggo46c57472015-09-30 08:57:13 -0700610 }
msarette6dd0042015-10-09 11:07:34 -0700611}
scroggo46c57472015-09-30 08:57:13 -0700612
msarette6dd0042015-10-09 11:07:34 -0700613void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
614 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400615 if (kYes_ZeroInitialized == zeroInit) {
616 return;
617 }
msarette6dd0042015-10-09 11:07:34 -0700618
msarette6dd0042015-10-09 11:07:34 -0700619 const int linesRemaining = linesRequested - linesDecoded;
620 SkSampler* sampler = this->getSampler(false);
621
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400622 const int fillWidth = sampler ? sampler->fillWidth() :
623 fOptions.fSubset ? fOptions.fSubset->width() :
624 info.width() ;
625 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
626 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
627 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
628 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700629}
Matt Sarett313c4632016-10-20 12:35:23 -0400630
Leon Scroggins III179559f2018-12-10 12:30:12 -0500631bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
632 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400633 SkASSERT(outFormat);
634
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400635 switch (colorType) {
636 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400637 *outFormat = skcms_PixelFormat_RGBA_8888;
638 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400639 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400640 *outFormat = skcms_PixelFormat_BGRA_8888;
641 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400642 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400643 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400644#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400645 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400646#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400647 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400648#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400649 break;
650 }
651 *outFormat = skcms_PixelFormat_BGR_565;
652 break;
653 case kRGBA_F16_SkColorType:
654 *outFormat = skcms_PixelFormat_RGBA_hhhh;
655 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400656 case kGray_8_SkColorType:
657 *outFormat = skcms_PixelFormat_G_8;
658 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400659 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000660 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000661 }
Matt Sarett313c4632016-10-20 12:35:23 -0400662 return true;
663}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400664
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400665bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
666 bool srcIsOpaque) {
667 fXformTime = kNo_XformTime;
668 bool needsColorXform = false;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500669 if (this->usesColorXform()) {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400670 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400671 needsColorXform = true;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500672 if (dstInfo.colorSpace()) {
673 dstInfo.colorSpace()->toProfile(&fDstProfile);
674 } else {
675 // Use the srcProfile to avoid conversion.
676 const auto* srcProfile = fEncodedInfo.profile();
677 fDstProfile = srcProfile ? *srcProfile : *skcms_sRGB_profile();
678 }
679 } else if (dstInfo.colorSpace()) {
680 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400681 const auto* srcProfile = fEncodedInfo.profile();
682 if (!srcProfile) {
683 srcProfile = skcms_sRGB_profile();
684 }
685 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
686 needsColorXform = true;
687 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400688 }
689 }
690
Leon Scroggins III712476e2018-10-03 15:47:00 -0400691 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400692 return false;
693 }
694
695 if (needsColorXform) {
696 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
697 || kRGBA_F16_SkColorType == dstInfo.colorType()
698 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500699 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
700 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400701 return false;
702 }
703 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
704 && dstInfo.alphaType() == kPremul_SkAlphaType) {
705 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
706 } else {
707 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
708 }
709 }
710 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400711}
712
713void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400714 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400715 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400716 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
717 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
718 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400719}
720
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400721std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400722 const int frameCount = this->getFrameCount();
723 SkASSERT(frameCount >= 0);
724 if (frameCount <= 0) {
725 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400726 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400727
728 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
729 // Not animated.
730 return std::vector<FrameInfo>{};
731 }
732
733 std::vector<FrameInfo> result(frameCount);
734 for (int i = 0; i < frameCount; ++i) {
735 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
736 }
737 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400738}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500739
740const char* SkCodec::ResultToString(Result result) {
741 switch (result) {
742 case kSuccess:
743 return "success";
744 case kIncompleteInput:
745 return "incomplete input";
746 case kErrorInInput:
747 return "error in input";
748 case kInvalidConversion:
749 return "invalid conversion";
750 case kInvalidScale:
751 return "invalid scale";
752 case kInvalidParameters:
753 return "invalid parameters";
754 case kInvalidInput:
755 return "invalid input";
756 case kCouldNotRewind:
757 return "could not rewind";
758 case kInternalError:
759 return "internal error";
760 case kUnimplemented:
761 return "unimplemented";
762 default:
763 SkASSERT(false);
764 return "bogus result value";
765 }
766}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000767
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000768static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000769 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000770}
771
772static bool restore_bg(const SkFrame& frame) {
773 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
774}
775
Nigel Taob8ec7f12019-03-26 10:44:58 +1100776// As its name suggests, this method computes a frame's alpha (e.g. completely
777// opaque, unpremul, binary) and its required frame (a preceding frame that
778// this frame depends on, to draw the complete image at this frame's point in
779// the animation stream), and calls this frame's setter methods with that
780// computed information.
781//
782// A required frame of kNoFrame means that this frame is independent: drawing
783// the complete image at this frame's point in the animation stream does not
784// require first preparing the pixel buffer based on another frame. Instead,
785// drawing can start from an uninitialized pixel buffer.
786//
787// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
788// implementation, for independent frames, first party Skia code (in src/codec)
789// will typically fill the buffer with a uniform background color (e.g.
790// transparent black) before calling into third party codec-specific code (e.g.
791// libjpeg or libpng). Pixels outside of the frame's rect will remain this
792// background color after drawing this frame. For incomplete decodes, pixels
793// inside that rect may be (at least temporarily) set to that background color.
794// In an incremental decode, later passes may then overwrite that background
795// color.
796//
797// Determining kNoFrame or otherwise involves testing a number of conditions
798// sequentially. The first satisfied condition results in setting the required
799// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
800// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
801// labels also map to comments in the function body.
802//
803// - IND1: this frame is the first frame.
804// - IND2: this frame fills out the whole image, and it is completely opaque
805// or it overwrites (not blends with) the previous frame.
806// - IND3: all preceding frames' disposals are kRestorePrevious.
807// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
808// whole image or it is itself otherwise independent.
809// - DEP5: this frame reports alpha (it is not completely opaque) and it
810// blends with (not overwrites) the previous frame.
811// - IND6: this frame's rect covers the rects of all preceding frames back to
812// and including the most recent independent frame before this frame.
813// - DEP7: unconditional.
814//
815// The "prevFrame" variable initially points to the previous frame (also known
816// as the prior frame), but that variable may iterate further backwards over
817// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000818void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
819 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
820 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
821 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
822
823 const int i = frame->frameId();
824 if (0 == i) {
825 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100826 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000827 return;
828 }
829
830
831 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
832 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
833 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100834 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000835 return;
836 }
837
838 const SkFrame* prevFrame = this->getFrame(i-1);
839 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
840 const int prevId = prevFrame->frameId();
841 if (0 == prevId) {
842 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100843 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000844 return;
845 }
846
847 prevFrame = this->getFrame(prevId - 1);
848 }
849
850 const bool clearPrevFrame = restore_bg(*prevFrame);
851 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
852
853 if (clearPrevFrame) {
854 if (prevFrameRect == screenRect || independent(*prevFrame)) {
855 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100856 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000857 return;
858 }
859 }
860
861 if (reportsAlpha && blendWithPrevFrame) {
862 // Note: We could be more aggressive here. If prevFrame clears
863 // to background color and covers its required frame (and that
864 // frame is independent), prevFrame could be marked independent.
865 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100866 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000867 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
868 return;
869 }
870
871 while (frameRect.contains(prevFrameRect)) {
872 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000873 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100874 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000875 frame->setHasAlpha(true);
876 return;
877 }
878
879 prevFrame = this->getFrame(prevRequiredFrame);
880 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
881 }
882
Nigel Taob8ec7f12019-03-26 10:44:58 +1100883 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000884 if (restore_bg(*prevFrame)) {
885 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000886 return;
887 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000888 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000889 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
890}
891