blob: 5383f9e4753f7759be67db23e64b17044795287a [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 Scroggins1340dbd2020-11-09 14:18:12 -05008#include "include/codec/SkAndroidCodec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/codec/SkCodec.h"
Mike Reed844beb52021-01-25 15:36:09 -050010#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkColorSpace.h"
12#include "include/core/SkData.h"
Mike Reed844beb52021-01-25 15:36:09 -050013#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkHalf.h"
15#include "src/codec/SkBmpCodec.h"
16#include "src/codec/SkCodecPriv.h"
17#include "src/codec/SkFrameHolder.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040018#ifdef SK_HAS_HEIF_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/codec/SkHeifCodec.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040020#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/codec/SkIcoCodec.h"
22#include "src/codec/SkJpegCodec.h"
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040023#ifdef SK_CODEC_DECODES_PNG
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/codec/SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080025#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkStream.h"
27#include "src/codec/SkRawCodec.h"
28#include "src/codec/SkWbmpCodec.h"
29#include "src/codec/SkWebpCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040030#ifdef SK_HAS_WUFFS_LIBRARY
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/codec/SkWuffsCodec.h"
Hal Canary2dad9902019-11-20 16:01:31 -050032#elif defined(SK_USE_LIBGIFCODEC)
Hal Canary1ec9a282019-11-21 10:15:50 -050033#include "SkGifCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040034#endif
scroggof24f2242015-03-03 08:59:20 -080035
msarett74114382015-03-16 11:55:18 -070036struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080037 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040038 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070039};
40
Mike Klein159a9592019-04-26 15:47:56 +000041static std::vector<DecoderProc>* decoders() {
42 static auto* decoders = new std::vector<DecoderProc> {
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040043 #ifdef SK_CODEC_DECODES_JPEG
Mike Klein159a9592019-04-26 15:47:56 +000044 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
45 #endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040046 #ifdef SK_CODEC_DECODES_WEBP
Mike Klein159a9592019-04-26 15:47:56 +000047 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
48 #endif
49 #ifdef SK_HAS_WUFFS_LIBRARY
50 { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
Hal Canary2dad9902019-11-20 16:01:31 -050051 #elif defined(SK_USE_LIBGIFCODEC)
Mike Klein159a9592019-04-26 15:47:56 +000052 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
53 #endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040054 #ifdef SK_CODEC_DECODES_PNG
Mike Klein159a9592019-04-26 15:47:56 +000055 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
56 #endif
57 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
58 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
Mike Klein159a9592019-04-26 15:47:56 +000059 };
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
Leon Scroggins III6154ac42019-08-14 11:29:29 -040069std::unique_ptr<SkCodec> SkCodec::MakeFromStream(
70 std::unique_ptr<SkStream> stream, Result* outResult,
71 SkPngChunkReader* chunkReader, SelectionPolicy selectionPolicy) {
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 III6154ac42019-08-14 11:29:29 -040082 if (selectionPolicy != SelectionPolicy::kPreferStillImage
83 && selectionPolicy != SelectionPolicy::kPreferAnimation) {
84 *outResult = kInvalidParameters;
85 return nullptr;
86 }
87
Leon Scroggins III04be2b52017-08-17 15:13:20 -040088 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080089
90 char buffer[bytesToRead];
91 size_t bytesRead = stream->peek(buffer, bytesToRead);
92
93 // It is also possible to have a complete image less than bytesToRead bytes
94 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
95 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
96 // than bytesToRead, so pass that directly to the decoder.
97 // It also is possible the stream uses too small a buffer for peeking, but
98 // we trust the caller to use a large enough buffer.
99
100 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800101 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
102 // printf could be useful to notice failures.
103 // SkCodecPrintf("Encoded image data failed to peek!\n");
104
scroggodb30be22015-12-08 18:54:13 -0800105 // It is possible the stream does not support peeking, but does support
106 // rewinding.
107 // Attempt to read() and pass the actual amount read to the decoder.
108 bytesRead = stream->read(buffer, bytesToRead);
109 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -0800110 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400111 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -0800112 return nullptr;
113 }
114 }
115
scroggocf98fa92015-11-23 08:14:40 -0800116 // PNG is special, since we want to be able to supply an SkPngChunkReader.
117 // But this code follows the same pattern as the loop.
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -0400118#ifdef SK_CODEC_DECODES_PNG
scroggodb30be22015-12-08 18:54:13 -0800119 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400120 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -0800121 } else
122#endif
123 {
Mike Klein159a9592019-04-26 15:47:56 +0000124 for (DecoderProc proc : *decoders()) {
scroggodb30be22015-12-08 18:54:13 -0800125 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400126 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800127 }
msarett74114382015-03-16 11:55:18 -0700128 }
yujieqin916de9f2016-01-25 08:26:16 -0800129
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400130#ifdef SK_HAS_HEIF_LIBRARY
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -0800131 SkEncodedImageFormat format;
132 if (SkHeifCodec::IsSupported(buffer, bytesRead, &format)) {
133 return SkHeifCodec::MakeFromStream(std::move(stream), selectionPolicy,
134 format, outResult);
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400135 }
136#endif
137
yujieqin916de9f2016-01-25 08:26:16 -0800138#ifdef SK_CODEC_DECODES_RAW
139 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400140 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800141#endif
scroggof24f2242015-03-03 08:59:20 -0800142 }
msarett8c8f22a2015-04-01 06:58:48 -0700143
Leon Scroggins III588fb042017-07-14 16:32:31 -0400144 if (bytesRead < bytesToRead) {
145 *outResult = kIncompleteInput;
146 } else {
147 *outResult = kUnimplemented;
148 }
149
msarettf44631b2016-01-13 10:54:20 -0800150 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800151}
152
Mike Reedede7bac2017-07-23 15:30:02 -0400153std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800154 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800156 }
Mike Reed847068c2017-07-26 11:35:53 -0400157 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800158}
159
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400160SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
161 SkEncodedOrigin origin)
162 : fEncodedInfo(std::move(info))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400163 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400164 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700165 , fNeedsRewind(false)
166 , fOrigin(origin)
167 , fDstInfo()
168 , fOptions()
169 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700170 , fStartedIncrementalDecode(false)
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500171 , fAndroidCodecHandlesFrameIndex(false)
msarett549ca322016-08-17 08:54:08 -0700172{}
173
scroggo9b2cdbf42015-07-10 12:07:02 -0700174SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700175
Brian Salomon59c60b02020-09-01 15:01:15 -0400176bool SkCodec::queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
177 SkYUVAPixmapInfo* yuvaPixmapInfo) const {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400178 if (!yuvaPixmapInfo) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400179 return false;
180 }
Brian Salomon59c60b02020-09-01 15:01:15 -0400181 return this->onQueryYUVAInfo(supportedDataTypes, yuvaPixmapInfo) &&
182 yuvaPixmapInfo->isSupported(supportedDataTypes);
Brian Salomon87d42e52020-08-24 09:18:16 -0400183}
184
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400185SkCodec::Result SkCodec::getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
186 if (!yuvaPixmaps.isValid()) {
Brian Salomon87d42e52020-08-24 09:18:16 -0400187 return kInvalidInput;
188 }
189 if (!this->rewindIfNeeded()) {
190 return kCouldNotRewind;
191 }
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400192 return this->onGetYUVAPlanes(yuvaPixmaps);
Brian Salomon87d42e52020-08-24 09:18:16 -0400193}
194
Leon Scroggins III712476e2018-10-03 15:47:00 -0400195bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400196 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
197 return false;
198 }
199
200 switch (dst.colorType()) {
201 case kRGBA_8888_SkColorType:
202 case kBGRA_8888_SkColorType:
Leon Scroggins III07418182017-08-15 12:24:02 -0400203 case kRGBA_F16_SkColorType:
Leon Scroggins III196f3192020-02-03 12:39:54 -0500204 return true;
Leon Scroggins III07418182017-08-15 12:24:02 -0400205 case kRGB_565_SkColorType:
206 return srcIsOpaque;
207 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400208 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500209 case kAlpha_8_SkColorType:
210 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400211 // all of those other conversions yet.
212 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400213 default:
214 return false;
215 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400216}
217
scroggob427db12015-08-12 07:24:13 -0700218bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800219 // Store the value of fNeedsRewind so we can update it. Next read will
220 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700221 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800222 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700223 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700224 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700225 }
scroggob427db12015-08-12 07:24:13 -0700226
scroggo46c57472015-09-30 08:57:13 -0700227 // startScanlineDecode will need to be called before decoding scanlines.
228 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700229 // startIncrementalDecode will need to be called before incrementalDecode.
230 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700231
scroggo19b91532016-10-24 09:03:26 -0700232 // Some codecs do not have a stream. They may hold onto their own data or another codec.
233 // They must handle rewinding themselves.
234 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700235 return false;
236 }
237
238 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800239}
scroggo05245902015-03-25 11:11:52 -0700240
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400241static SkIRect frame_rect_on_screen(SkIRect frameRect,
242 const SkIRect& screenRect) {
243 if (!frameRect.intersect(screenRect)) {
244 return SkIRect::MakeEmpty();
245 }
246
247 return frameRect;
248}
249
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400250bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
251 SkISize srcDimensions, SkIRect prevRect) {
252 const auto dimensions = dstInfo.dimensions();
253 if (dimensions != srcDimensions) {
254 SkRect src = SkRect::Make(srcDimensions);
255 SkRect dst = SkRect::Make(dimensions);
Mike Reed2ac6ce82021-01-15 12:26:22 -0500256 SkMatrix map = SkMatrix::RectToRect(src, dst);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400257 SkRect asRect = SkRect::Make(prevRect);
258 if (!map.mapRect(&asRect)) {
259 return false;
260 }
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500261 asRect.roundOut(&prevRect);
262 }
263
264 if (!prevRect.intersect(SkIRect::MakeSize(dimensions))) {
265 // Nothing to zero, due to scaling or bad frame rect.
266 return true;
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,
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500278 const Options& options, SkAndroidCodec* androidCodec) {
279 if (androidCodec) {
280 // This is never set back to false. If SkAndroidCodec is calling this method, its fCodec
281 // should never call it directly.
282 fAndroidCodecHandlesFrameIndex = true;
283 } else if (fAndroidCodecHandlesFrameIndex) {
284 return kSuccess;
285 }
286
287 if (!this->rewindIfNeeded()) {
288 return kCouldNotRewind;
289 }
290
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400291 const int index = options.fFrameIndex;
292 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400293 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
294 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400295 }
296
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400297 if (index < 0) {
298 return kInvalidParameters;
299 }
300
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500301 if (options.fSubset) {
302 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400303 // a kRestoreBGColor frame.
304 return kInvalidParameters;
305 }
306
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400307 if (index >= this->onGetFrameCount()) {
308 return kIncompleteInput;
309 }
310
311 const auto* frameHolder = this->getFrameHolder();
312 SkASSERT(frameHolder);
313
314 const auto* frame = frameHolder->getFrame(index);
315 SkASSERT(frame);
316
317 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000318 if (requiredFrame != kNoFrame) {
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500319 const SkFrame* preppedFrame = nullptr;
320 if (options.fPriorFrame == kNoFrame) {
321 Result result = kInternalError;
322 if (androidCodec) {
323#ifdef SK_HAS_ANDROID_CODEC
324 SkAndroidCodec::AndroidOptions prevFrameOptions(
325 reinterpret_cast<const SkAndroidCodec::AndroidOptions&>(options));
326 prevFrameOptions.fFrameIndex = requiredFrame;
327 result = androidCodec->getAndroidPixels(info, pixels, rowBytes, &prevFrameOptions);
328#endif
329 } else {
330 Options prevFrameOptions(options);
331 prevFrameOptions.fFrameIndex = requiredFrame;
332 result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
333 }
334 if (result != kSuccess) {
335 return result;
336 }
337 preppedFrame = frameHolder->getFrame(requiredFrame);
338 } else {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400339 // Check for a valid frame as a starting point. Alternatively, we could
340 // treat an invalid frame as not providing one, but rejecting it will
341 // make it easier to catch the mistake.
342 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400343 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400344 }
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500345 preppedFrame = frameHolder->getFrame(options.fPriorFrame);
346 }
347
348 SkASSERT(preppedFrame);
349 switch (preppedFrame->getDisposalMethod()) {
350 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
351 SkASSERT(options.fPriorFrame != kNoFrame);
352 return kInvalidParameters;
353 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
354 // If a frame after the required frame is provided, there is no
355 // need to clear, since it must be covered by the desired frame.
356 // FIXME: If the required frame is kRestoreBGColor, we don't actually need to decode
357 // it, since we'll just clear it to transparent. Instead, we could decode *its*
358 // required frame and then clear.
359 if (preppedFrame->frameId() == requiredFrame) {
360 SkIRect preppedRect = preppedFrame->frameRect();
361 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), preppedRect)) {
362 return kInternalError;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400363 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400364 }
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500365 break;
366 default:
367 break;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400368 }
369 }
370
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400371 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
372 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400373}
scroggo8e6c7ad2016-09-16 08:20:38 -0700374
Leon Scroggins III196f3192020-02-03 12:39:54 -0500375SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000376 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700377 if (kUnknown_SkColorType == info.colorType()) {
378 return kInvalidConversion;
379 }
halcanary96fcdcc2015-08-27 07:41:13 -0700380 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700381 return kInvalidParameters;
382 }
383 if (rowBytes < info.minRowBytes()) {
384 return kInvalidParameters;
385 }
386
scroggoeb602a52015-07-09 08:16:03 -0700387 // Default options.
388 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700389 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700390 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400391 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400392 if (options->fSubset) {
393 SkIRect subset(*options->fSubset);
394 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
395 // FIXME: How to differentiate between not supporting subset at all
396 // and not supporting this particular subset?
397 return kUnimplemented;
398 }
scroggoe7fc14b2015-10-02 13:14:46 -0700399 }
scroggoeb602a52015-07-09 08:16:03 -0700400 }
scroggoe7fc14b2015-10-02 13:14:46 -0700401
Leon Scroggins III07418182017-08-15 12:24:02 -0400402 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
403 *options);
404 if (frameIndexResult != kSuccess) {
405 return frameIndexResult;
406 }
407
scroggoe7fc14b2015-10-02 13:14:46 -0700408 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
409 // because it supports arbitrary scaling/subset combinations.
410 if (!this->dimensionsSupported(info.dimensions())) {
411 return kInvalidScale;
412 }
413
scroggo8e6c7ad2016-09-16 08:20:38 -0700414 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500415 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700416
msarette6dd0042015-10-09 11:07:34 -0700417 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
418 // successfully.
419 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000420 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700421
422 // A return value of kIncompleteInput indicates a truncated image stream.
423 // In this case, we will fill any uninitialized memory with a default value.
424 // Some subclasses will take care of filling any uninitialized memory on
425 // their own. They indicate that all of the memory has been filled by
426 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400427 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500428 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
429 // there is a subset. In that case, it will use the width of the subset. From here, the
430 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
431 // differenty from the other codecs, and it needs to use the width specified by the info.
432 // Set the subset to null so SkWebpCodec uses the correct width.
433 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700434 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
435 rowsDecoded);
436 }
437
scroggoeb602a52015-07-09 08:16:03 -0700438 return result;
439}
440
Mike Reed844beb52021-01-25 15:36:09 -0500441std::tuple<sk_sp<SkImage>, SkCodec::Result> SkCodec::getImage(const SkImageInfo& info,
442 const Options* options) {
443 SkBitmap bm;
444 if (!bm.tryAllocPixels(info)) {
445 return {nullptr, kInternalError};
446 }
447
448 Result result = this->getPixels(info, bm.getPixels(), bm.rowBytes(), options);
449 switch (result) {
450 case kSuccess:
451 case kIncompleteInput:
452 case kErrorInInput:
453 bm.setImmutable();
454 return {bm.asImage(), result};
455
456 default: break;
457 }
458 return {nullptr, result};
459}
460
461std::tuple<sk_sp<SkImage>, SkCodec::Result> SkCodec::getImage() {
462 return this->getImage(this->getInfo(), nullptr);
463}
464
Leon Scroggins III196f3192020-02-03 12:39:54 -0500465SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000466 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700467 fStartedIncrementalDecode = false;
468
469 if (kUnknown_SkColorType == info.colorType()) {
470 return kInvalidConversion;
471 }
472 if (nullptr == pixels) {
473 return kInvalidParameters;
474 }
475
scroggo8e6c7ad2016-09-16 08:20:38 -0700476 // Set options.
477 Options optsStorage;
478 if (nullptr == options) {
479 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400480 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400481 if (options->fSubset) {
482 SkIRect size = SkIRect::MakeSize(info.dimensions());
483 if (!size.contains(*options->fSubset)) {
484 return kInvalidParameters;
485 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700486
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400487 const int top = options->fSubset->top();
488 const int bottom = options->fSubset->bottom();
489 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
490 return kInvalidParameters;
491 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700492 }
493 }
494
Leon Scroggins III07418182017-08-15 12:24:02 -0400495 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
496 *options);
497 if (frameIndexResult != kSuccess) {
498 return frameIndexResult;
499 }
500
scroggo8e6c7ad2016-09-16 08:20:38 -0700501 if (!this->dimensionsSupported(info.dimensions())) {
502 return kInvalidScale;
503 }
504
505 fDstInfo = info;
506 fOptions = *options;
507
Leon Scroggins571b30f2017-07-11 17:35:31 +0000508 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700509 if (kSuccess == result) {
510 fStartedIncrementalDecode = true;
511 } else if (kUnimplemented == result) {
512 // FIXME: This is temporarily necessary, until we transition SkCodec
513 // implementations from scanline decoding to incremental decoding.
514 // SkAndroidCodec will first attempt to use incremental decoding, but
515 // will fall back to scanline decoding if incremental returns
516 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
517 // (after potentially rewinding), but we do not want the next call to
518 // startScanlineDecode() to do a rewind.
519 fNeedsRewind = false;
520 }
521 return result;
522}
523
524
Leon Scroggins III196f3192020-02-03 12:39:54 -0500525SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000526 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700527 // Reset fCurrScanline in case of failure.
528 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700529
530 // Set options.
531 Options optsStorage;
532 if (nullptr == options) {
533 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700534 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700535 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700536 if (!size.contains(*options->fSubset)) {
537 return kInvalidInput;
538 }
539
540 // We only support subsetting in the x-dimension for scanline decoder.
541 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700542 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700543 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700544 }
545 }
546
Leon Scroggins III07418182017-08-15 12:24:02 -0400547 // Scanline decoding only supports decoding the first frame.
548 if (options->fFrameIndex != 0) {
549 return kUnimplemented;
550 }
551
552 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
553 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
554 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
555 if (frameIndexResult != kSuccess) {
556 return frameIndexResult;
557 }
558
scroggoe7fc14b2015-10-02 13:14:46 -0700559 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700560 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700561 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700562 }
563
Leon Scroggins571b30f2017-07-11 17:35:31 +0000564 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700565 if (result != SkCodec::kSuccess) {
566 return result;
567 }
568
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500569 // FIXME: See startIncrementalDecode. That method set fNeedsRewind to false
570 // so that when onStartScanlineDecode calls rewindIfNeeded it would not
571 // rewind. But it also relies on that call to rewindIfNeeded to set
572 // fNeedsRewind to true for future decodes. When
573 // fAndroidCodecHandlesFrameIndex is true, that call to rewindIfNeeded is
574 // skipped, so this method sets it back to true.
575 SkASSERT(fAndroidCodecHandlesFrameIndex || fNeedsRewind);
576 fNeedsRewind = true;
577
scroggo46c57472015-09-30 08:57:13 -0700578 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700579 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700580 fOptions = *options;
581 return kSuccess;
582}
583
msarette6dd0042015-10-09 11:07:34 -0700584int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700585 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700586 return 0;
scroggo46c57472015-09-30 08:57:13 -0700587 }
588
589 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700590 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700591 return 0;
scroggo46c57472015-09-30 08:57:13 -0700592 }
593
msarette6dd0042015-10-09 11:07:34 -0700594 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
595 if (linesDecoded < countLines) {
596 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
597 countLines, linesDecoded);
598 }
599 fCurrScanline += countLines;
600 return linesDecoded;
601}
602
603bool SkCodec::skipScanlines(int countLines) {
604 if (fCurrScanline < 0) {
605 return false;
606 }
607
608 SkASSERT(!fDstInfo.isEmpty());
609 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
610 // Arguably, we could just skip the scanlines which are remaining,
611 // and return true. We choose to return false so the client
612 // can catch their bug.
613 return false;
614 }
615
616 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700617 fCurrScanline += countLines;
618 return result;
619}
620
msarette6dd0042015-10-09 11:07:34 -0700621int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400622 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700623 return this->onOutputScanline(inputScanline);
624}
scroggo46c57472015-09-30 08:57:13 -0700625
msarette6dd0042015-10-09 11:07:34 -0700626int SkCodec::onOutputScanline(int inputScanline) const {
627 switch (this->getScanlineOrder()) {
628 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700629 return inputScanline;
630 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400631 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700632 default:
633 // This case indicates an interlaced gif and is implemented by SkGifCodec.
634 SkASSERT(false);
635 return 0;
scroggo46c57472015-09-30 08:57:13 -0700636 }
msarette6dd0042015-10-09 11:07:34 -0700637}
scroggo46c57472015-09-30 08:57:13 -0700638
msarette6dd0042015-10-09 11:07:34 -0700639void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
640 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400641 if (kYes_ZeroInitialized == zeroInit) {
642 return;
643 }
msarette6dd0042015-10-09 11:07:34 -0700644
msarette6dd0042015-10-09 11:07:34 -0700645 const int linesRemaining = linesRequested - linesDecoded;
646 SkSampler* sampler = this->getSampler(false);
647
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400648 const int fillWidth = sampler ? sampler->fillWidth() :
649 fOptions.fSubset ? fOptions.fSubset->width() :
650 info.width() ;
651 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
652 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
653 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
654 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700655}
Matt Sarett313c4632016-10-20 12:35:23 -0400656
Leon Scroggins III179559f2018-12-10 12:30:12 -0500657bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
658 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400659 SkASSERT(outFormat);
660
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400661 switch (colorType) {
662 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400663 *outFormat = skcms_PixelFormat_RGBA_8888;
664 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400665 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400666 *outFormat = skcms_PixelFormat_BGRA_8888;
667 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400668 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400669 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400670#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400671 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400672#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400673 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400674#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400675 break;
676 }
677 *outFormat = skcms_PixelFormat_BGR_565;
678 break;
679 case kRGBA_F16_SkColorType:
680 *outFormat = skcms_PixelFormat_RGBA_hhhh;
681 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400682 case kGray_8_SkColorType:
683 *outFormat = skcms_PixelFormat_G_8;
684 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400685 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000686 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000687 }
Matt Sarett313c4632016-10-20 12:35:23 -0400688 return true;
689}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400690
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400691bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
692 bool srcIsOpaque) {
693 fXformTime = kNo_XformTime;
694 bool needsColorXform = false;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500695 if (this->usesColorXform()) {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400696 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400697 needsColorXform = true;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500698 if (dstInfo.colorSpace()) {
699 dstInfo.colorSpace()->toProfile(&fDstProfile);
700 } else {
701 // Use the srcProfile to avoid conversion.
702 const auto* srcProfile = fEncodedInfo.profile();
703 fDstProfile = srcProfile ? *srcProfile : *skcms_sRGB_profile();
704 }
705 } else if (dstInfo.colorSpace()) {
706 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400707 const auto* srcProfile = fEncodedInfo.profile();
708 if (!srcProfile) {
709 srcProfile = skcms_sRGB_profile();
710 }
711 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
712 needsColorXform = true;
713 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400714 }
715 }
716
Leon Scroggins III712476e2018-10-03 15:47:00 -0400717 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400718 return false;
719 }
720
721 if (needsColorXform) {
722 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
723 || kRGBA_F16_SkColorType == dstInfo.colorType()
724 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500725 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
726 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400727 return false;
728 }
729 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
730 && dstInfo.alphaType() == kPremul_SkAlphaType) {
731 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
732 } else {
733 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
734 }
735 }
736 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400737}
738
739void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400740 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400741 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400742 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
743 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
744 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400745}
746
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400747std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400748 const int frameCount = this->getFrameCount();
749 SkASSERT(frameCount >= 0);
750 if (frameCount <= 0) {
751 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400752 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400753
754 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
755 // Not animated.
756 return std::vector<FrameInfo>{};
757 }
758
759 std::vector<FrameInfo> result(frameCount);
760 for (int i = 0; i < frameCount; ++i) {
761 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
762 }
763 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400764}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500765
766const char* SkCodec::ResultToString(Result result) {
767 switch (result) {
768 case kSuccess:
769 return "success";
770 case kIncompleteInput:
771 return "incomplete input";
772 case kErrorInInput:
773 return "error in input";
774 case kInvalidConversion:
775 return "invalid conversion";
776 case kInvalidScale:
777 return "invalid scale";
778 case kInvalidParameters:
779 return "invalid parameters";
780 case kInvalidInput:
781 return "invalid input";
782 case kCouldNotRewind:
783 return "could not rewind";
784 case kInternalError:
785 return "internal error";
786 case kUnimplemented:
787 return "unimplemented";
788 default:
789 SkASSERT(false);
790 return "bogus result value";
791 }
792}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000793
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500794void SkFrame::fillIn(SkCodec::FrameInfo* frameInfo, bool fullyReceived) const {
795 SkASSERT(frameInfo);
796
797 frameInfo->fRequiredFrame = fRequiredFrame;
798 frameInfo->fDuration = fDuration;
799 frameInfo->fFullyReceived = fullyReceived;
800 frameInfo->fAlphaType = fHasAlpha ? kUnpremul_SkAlphaType
801 : kOpaque_SkAlphaType;
802 frameInfo->fHasAlphaWithinBounds = this->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
803 frameInfo->fDisposalMethod = fDisposalMethod;
804 frameInfo->fBlend = fBlend;
805 frameInfo->fFrameRect = fRect;
806}
807
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000808static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000809 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000810}
811
812static bool restore_bg(const SkFrame& frame) {
813 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
814}
815
Nigel Taob8ec7f12019-03-26 10:44:58 +1100816// As its name suggests, this method computes a frame's alpha (e.g. completely
817// opaque, unpremul, binary) and its required frame (a preceding frame that
818// this frame depends on, to draw the complete image at this frame's point in
819// the animation stream), and calls this frame's setter methods with that
820// computed information.
821//
822// A required frame of kNoFrame means that this frame is independent: drawing
823// the complete image at this frame's point in the animation stream does not
824// require first preparing the pixel buffer based on another frame. Instead,
825// drawing can start from an uninitialized pixel buffer.
826//
827// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
828// implementation, for independent frames, first party Skia code (in src/codec)
829// will typically fill the buffer with a uniform background color (e.g.
830// transparent black) before calling into third party codec-specific code (e.g.
831// libjpeg or libpng). Pixels outside of the frame's rect will remain this
832// background color after drawing this frame. For incomplete decodes, pixels
833// inside that rect may be (at least temporarily) set to that background color.
834// In an incremental decode, later passes may then overwrite that background
835// color.
836//
837// Determining kNoFrame or otherwise involves testing a number of conditions
838// sequentially. The first satisfied condition results in setting the required
839// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
840// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
841// labels also map to comments in the function body.
842//
843// - IND1: this frame is the first frame.
844// - IND2: this frame fills out the whole image, and it is completely opaque
845// or it overwrites (not blends with) the previous frame.
846// - IND3: all preceding frames' disposals are kRestorePrevious.
847// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
848// whole image or it is itself otherwise independent.
849// - DEP5: this frame reports alpha (it is not completely opaque) and it
850// blends with (not overwrites) the previous frame.
851// - IND6: this frame's rect covers the rects of all preceding frames back to
852// and including the most recent independent frame before this frame.
853// - DEP7: unconditional.
854//
855// The "prevFrame" variable initially points to the previous frame (also known
856// as the prior frame), but that variable may iterate further backwards over
857// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000858void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
859 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
860 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
861 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
862
863 const int i = frame->frameId();
864 if (0 == i) {
865 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100866 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000867 return;
868 }
869
870
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500871 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kSrcOver;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000872 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
873 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100874 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000875 return;
876 }
877
878 const SkFrame* prevFrame = this->getFrame(i-1);
879 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
880 const int prevId = prevFrame->frameId();
881 if (0 == prevId) {
882 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100883 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000884 return;
885 }
886
887 prevFrame = this->getFrame(prevId - 1);
888 }
889
890 const bool clearPrevFrame = restore_bg(*prevFrame);
891 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
892
893 if (clearPrevFrame) {
894 if (prevFrameRect == screenRect || independent(*prevFrame)) {
895 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100896 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000897 return;
898 }
899 }
900
901 if (reportsAlpha && blendWithPrevFrame) {
902 // Note: We could be more aggressive here. If prevFrame clears
903 // to background color and covers its required frame (and that
904 // frame is independent), prevFrame could be marked independent.
905 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100906 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000907 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
908 return;
909 }
910
911 while (frameRect.contains(prevFrameRect)) {
912 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000913 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100914 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000915 frame->setHasAlpha(true);
916 return;
917 }
918
919 prevFrame = this->getFrame(prevRequiredFrame);
920 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
921 }
922
Nigel Taob8ec7f12019-03-26 10:44:58 +1100923 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000924 if (restore_bg(*prevFrame)) {
925 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000926 return;
927 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000928 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000929 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
930}
931