blob: b11c63c93c0c6361e4f42cb5993dcc0ac97f4fff [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
128 if (SkHeifCodec::IsHeif(buffer, bytesRead)) {
129 return SkHeifCodec::MakeFromStream(std::move(stream), selectionPolicy, outResult);
130 }
131#endif
132
yujieqin916de9f2016-01-25 08:26:16 -0800133#ifdef SK_CODEC_DECODES_RAW
134 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400135 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800136#endif
scroggof24f2242015-03-03 08:59:20 -0800137 }
msarett8c8f22a2015-04-01 06:58:48 -0700138
Leon Scroggins III588fb042017-07-14 16:32:31 -0400139 if (bytesRead < bytesToRead) {
140 *outResult = kIncompleteInput;
141 } else {
142 *outResult = kUnimplemented;
143 }
144
msarettf44631b2016-01-13 10:54:20 -0800145 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800146}
147
Mike Reedede7bac2017-07-23 15:30:02 -0400148std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800149 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700150 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800151 }
Mike Reed847068c2017-07-26 11:35:53 -0400152 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800153}
154
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400155SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
156 SkEncodedOrigin origin)
157 : fEncodedInfo(std::move(info))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400158 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400159 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700160 , fNeedsRewind(false)
161 , fOrigin(origin)
162 , fDstInfo()
163 , fOptions()
164 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700165 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700166{}
167
scroggo9b2cdbf42015-07-10 12:07:02 -0700168SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700169
Brian Salomon87d42e52020-08-24 09:18:16 -0400170bool SkCodec::queryYUVAInfo(SkYUVAInfo* yuvaInfo,
171 SkColorType colorTypes[SkYUVAInfo::kMaxPlanes],
172 size_t rowBytes[SkYUVAInfo::kMaxPlanes]) const {
173 if (!yuvaInfo || !colorTypes || !rowBytes) {
174 return false;
175 }
176 if (!this->onQueryYUVAInfo(yuvaInfo, colorTypes, rowBytes)) {
177 return false;
178 }
179 SkISize planeDimensions[SkYUVAInfo::kMaxPlanes];
180 int numPlanes = yuvaInfo->expectedPlaneDims(planeDimensions);
181 // Validate that the returned color types and row bytes are OK for the expected plane sizes.
182 for (int i = 0; i < numPlanes; ++i) {
183 SkImageInfo ii = SkImageInfo::Make(planeDimensions[i], colorTypes[i], kPremul_SkAlphaType);
184 if (!ii.validRowBytes(rowBytes[i])) {
185 return false;
186 }
187 }
188 return true;
189}
190
191SkCodec::Result SkCodec::getYUVAPlanes(const SkPixmap planes[SkYUVAInfo::kMaxPlanes]) {
192 if (!planes) {
193 return kInvalidInput;
194 }
195 if (!this->rewindIfNeeded()) {
196 return kCouldNotRewind;
197 }
198 return this->onGetYUVAPlanes(planes);
199}
200
Leon Scroggins III712476e2018-10-03 15:47:00 -0400201bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400202 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
203 return false;
204 }
205
206 switch (dst.colorType()) {
207 case kRGBA_8888_SkColorType:
208 case kBGRA_8888_SkColorType:
Leon Scroggins III07418182017-08-15 12:24:02 -0400209 case kRGBA_F16_SkColorType:
Leon Scroggins III196f3192020-02-03 12:39:54 -0500210 return true;
Leon Scroggins III07418182017-08-15 12:24:02 -0400211 case kRGB_565_SkColorType:
212 return srcIsOpaque;
213 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400214 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500215 case kAlpha_8_SkColorType:
216 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400217 // all of those other conversions yet.
218 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400219 default:
220 return false;
221 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400222}
223
scroggob427db12015-08-12 07:24:13 -0700224bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800225 // Store the value of fNeedsRewind so we can update it. Next read will
226 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700227 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800228 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700229 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700230 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700231 }
scroggob427db12015-08-12 07:24:13 -0700232
scroggo46c57472015-09-30 08:57:13 -0700233 // startScanlineDecode will need to be called before decoding scanlines.
234 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700235 // startIncrementalDecode will need to be called before incrementalDecode.
236 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700237
scroggo19b91532016-10-24 09:03:26 -0700238 // Some codecs do not have a stream. They may hold onto their own data or another codec.
239 // They must handle rewinding themselves.
240 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700241 return false;
242 }
243
244 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800245}
scroggo05245902015-03-25 11:11:52 -0700246
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400247static SkIRect frame_rect_on_screen(SkIRect frameRect,
248 const SkIRect& screenRect) {
249 if (!frameRect.intersect(screenRect)) {
250 return SkIRect::MakeEmpty();
251 }
252
253 return frameRect;
254}
255
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400256bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
257 SkISize srcDimensions, SkIRect prevRect) {
Leon Scroggins III19e3cd42019-08-07 16:42:04 -0400258 prevRect = frame_rect_on_screen(prevRect, SkIRect::MakeSize(srcDimensions));
259 if (prevRect.isEmpty()) {
260 return true;
261 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400262 const auto dimensions = dstInfo.dimensions();
263 if (dimensions != srcDimensions) {
264 SkRect src = SkRect::Make(srcDimensions);
265 SkRect dst = SkRect::Make(dimensions);
266 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
267 SkRect asRect = SkRect::Make(prevRect);
268 if (!map.mapRect(&asRect)) {
269 return false;
270 }
271 asRect.roundIn(&prevRect);
272 if (prevRect.isEmpty()) {
273 // Down-scaling shrank the empty portion to nothing,
274 // so nothing to zero.
275 return true;
276 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400277 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400278
Brian Salomon9241a6d2019-10-03 13:26:54 -0400279 const SkImageInfo info = dstInfo.makeDimensions(prevRect.size());
Mike Reed7fcfb622018-02-09 13:26:46 -0500280 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400281 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
282 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400283 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400284 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400285}
286
287SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
288 const Options& options) {
289 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) {
317 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400318 // Check for a valid frame as a starting point. Alternatively, we could
319 // treat an invalid frame as not providing one, but rejecting it will
320 // make it easier to catch the mistake.
321 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400322 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400323 }
324 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
325 switch (prevFrame->getDisposalMethod()) {
326 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
327 return kInvalidParameters;
328 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
329 // If a frame after the required frame is provided, there is no
330 // need to clear, since it must be covered by the desired frame.
331 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500332 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400333 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400334 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500335 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400336 }
337 break;
338 default:
339 break;
340 }
341 } else {
342 Options prevFrameOptions(options);
343 prevFrameOptions.fFrameIndex = requiredFrame;
344 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
345 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
346 if (result != kSuccess) {
347 return result;
348 }
349 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
350 const auto disposalMethod = prevFrame->getDisposalMethod();
351 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400352 auto prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400353 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400354 return kInternalError;
355 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400356 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400357 }
358 }
359
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400360 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
361 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400362}
scroggo8e6c7ad2016-09-16 08:20:38 -0700363
Leon Scroggins III196f3192020-02-03 12:39:54 -0500364SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000365 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700366 if (kUnknown_SkColorType == info.colorType()) {
367 return kInvalidConversion;
368 }
halcanary96fcdcc2015-08-27 07:41:13 -0700369 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700370 return kInvalidParameters;
371 }
372 if (rowBytes < info.minRowBytes()) {
373 return kInvalidParameters;
374 }
375
scroggo3a7701c2015-09-30 09:15:14 -0700376 if (!this->rewindIfNeeded()) {
377 return kCouldNotRewind;
378 }
379
scroggoeb602a52015-07-09 08:16:03 -0700380 // Default options.
381 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700382 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700383 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400384 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400385 if (options->fSubset) {
386 SkIRect subset(*options->fSubset);
387 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
388 // FIXME: How to differentiate between not supporting subset at all
389 // and not supporting this particular subset?
390 return kUnimplemented;
391 }
scroggoe7fc14b2015-10-02 13:14:46 -0700392 }
scroggoeb602a52015-07-09 08:16:03 -0700393 }
scroggoe7fc14b2015-10-02 13:14:46 -0700394
Leon Scroggins III07418182017-08-15 12:24:02 -0400395 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
396 *options);
397 if (frameIndexResult != kSuccess) {
398 return frameIndexResult;
399 }
400
scroggoe7fc14b2015-10-02 13:14:46 -0700401 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
402 // because it supports arbitrary scaling/subset combinations.
403 if (!this->dimensionsSupported(info.dimensions())) {
404 return kInvalidScale;
405 }
406
scroggo8e6c7ad2016-09-16 08:20:38 -0700407 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500408 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700409
msarette6dd0042015-10-09 11:07:34 -0700410 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
411 // successfully.
412 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000413 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700414
415 // A return value of kIncompleteInput indicates a truncated image stream.
416 // In this case, we will fill any uninitialized memory with a default value.
417 // Some subclasses will take care of filling any uninitialized memory on
418 // their own. They indicate that all of the memory has been filled by
419 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400420 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500421 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
422 // there is a subset. In that case, it will use the width of the subset. From here, the
423 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
424 // differenty from the other codecs, and it needs to use the width specified by the info.
425 // Set the subset to null so SkWebpCodec uses the correct width.
426 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700427 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
428 rowsDecoded);
429 }
430
scroggoeb602a52015-07-09 08:16:03 -0700431 return result;
432}
433
Leon Scroggins III196f3192020-02-03 12:39:54 -0500434SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000435 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700436 fStartedIncrementalDecode = false;
437
438 if (kUnknown_SkColorType == info.colorType()) {
439 return kInvalidConversion;
440 }
441 if (nullptr == pixels) {
442 return kInvalidParameters;
443 }
444
scroggo8e6c7ad2016-09-16 08:20:38 -0700445 // FIXME: If the rows come after the rows of a previous incremental decode,
446 // we might be able to skip the rewind, but only the implementation knows
447 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
448 // a bottom-up BMP could skip rewinding if the new rows are above the old
449 // rows.)
450 if (!this->rewindIfNeeded()) {
451 return kCouldNotRewind;
452 }
453
454 // Set options.
455 Options optsStorage;
456 if (nullptr == options) {
457 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400458 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400459 if (options->fSubset) {
460 SkIRect size = SkIRect::MakeSize(info.dimensions());
461 if (!size.contains(*options->fSubset)) {
462 return kInvalidParameters;
463 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700464
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400465 const int top = options->fSubset->top();
466 const int bottom = options->fSubset->bottom();
467 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
468 return kInvalidParameters;
469 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700470 }
471 }
472
Leon Scroggins III07418182017-08-15 12:24:02 -0400473 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
474 *options);
475 if (frameIndexResult != kSuccess) {
476 return frameIndexResult;
477 }
478
scroggo8e6c7ad2016-09-16 08:20:38 -0700479 if (!this->dimensionsSupported(info.dimensions())) {
480 return kInvalidScale;
481 }
482
483 fDstInfo = info;
484 fOptions = *options;
485
Leon Scroggins571b30f2017-07-11 17:35:31 +0000486 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700487 if (kSuccess == result) {
488 fStartedIncrementalDecode = true;
489 } else if (kUnimplemented == result) {
490 // FIXME: This is temporarily necessary, until we transition SkCodec
491 // implementations from scanline decoding to incremental decoding.
492 // SkAndroidCodec will first attempt to use incremental decoding, but
493 // will fall back to scanline decoding if incremental returns
494 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
495 // (after potentially rewinding), but we do not want the next call to
496 // startScanlineDecode() to do a rewind.
497 fNeedsRewind = false;
498 }
499 return result;
500}
501
502
Leon Scroggins III196f3192020-02-03 12:39:54 -0500503SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000504 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700505 // Reset fCurrScanline in case of failure.
506 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700507
scroggo3a7701c2015-09-30 09:15:14 -0700508 if (!this->rewindIfNeeded()) {
509 return kCouldNotRewind;
510 }
511
scroggo46c57472015-09-30 08:57:13 -0700512 // Set options.
513 Options optsStorage;
514 if (nullptr == options) {
515 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700516 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700517 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700518 if (!size.contains(*options->fSubset)) {
519 return kInvalidInput;
520 }
521
522 // We only support subsetting in the x-dimension for scanline decoder.
523 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700524 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700525 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700526 }
527 }
528
Leon Scroggins III07418182017-08-15 12:24:02 -0400529 // Scanline decoding only supports decoding the first frame.
530 if (options->fFrameIndex != 0) {
531 return kUnimplemented;
532 }
533
534 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
535 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
536 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
537 if (frameIndexResult != kSuccess) {
538 return frameIndexResult;
539 }
540
scroggoe7fc14b2015-10-02 13:14:46 -0700541 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700542 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700543 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700544 }
545
Leon Scroggins571b30f2017-07-11 17:35:31 +0000546 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700547 if (result != SkCodec::kSuccess) {
548 return result;
549 }
550
551 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700552 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700553 fOptions = *options;
554 return kSuccess;
555}
556
msarette6dd0042015-10-09 11:07:34 -0700557int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700558 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700559 return 0;
scroggo46c57472015-09-30 08:57:13 -0700560 }
561
562 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700563 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700564 return 0;
scroggo46c57472015-09-30 08:57:13 -0700565 }
566
msarette6dd0042015-10-09 11:07:34 -0700567 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
568 if (linesDecoded < countLines) {
569 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
570 countLines, linesDecoded);
571 }
572 fCurrScanline += countLines;
573 return linesDecoded;
574}
575
576bool SkCodec::skipScanlines(int countLines) {
577 if (fCurrScanline < 0) {
578 return false;
579 }
580
581 SkASSERT(!fDstInfo.isEmpty());
582 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
583 // Arguably, we could just skip the scanlines which are remaining,
584 // and return true. We choose to return false so the client
585 // can catch their bug.
586 return false;
587 }
588
589 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700590 fCurrScanline += countLines;
591 return result;
592}
593
msarette6dd0042015-10-09 11:07:34 -0700594int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400595 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700596 return this->onOutputScanline(inputScanline);
597}
scroggo46c57472015-09-30 08:57:13 -0700598
msarette6dd0042015-10-09 11:07:34 -0700599int SkCodec::onOutputScanline(int inputScanline) const {
600 switch (this->getScanlineOrder()) {
601 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700602 return inputScanline;
603 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400604 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700605 default:
606 // This case indicates an interlaced gif and is implemented by SkGifCodec.
607 SkASSERT(false);
608 return 0;
scroggo46c57472015-09-30 08:57:13 -0700609 }
msarette6dd0042015-10-09 11:07:34 -0700610}
scroggo46c57472015-09-30 08:57:13 -0700611
msarette6dd0042015-10-09 11:07:34 -0700612void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
613 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400614 if (kYes_ZeroInitialized == zeroInit) {
615 return;
616 }
msarette6dd0042015-10-09 11:07:34 -0700617
msarette6dd0042015-10-09 11:07:34 -0700618 const int linesRemaining = linesRequested - linesDecoded;
619 SkSampler* sampler = this->getSampler(false);
620
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400621 const int fillWidth = sampler ? sampler->fillWidth() :
622 fOptions.fSubset ? fOptions.fSubset->width() :
623 info.width() ;
624 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
625 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
626 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
627 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700628}
Matt Sarett313c4632016-10-20 12:35:23 -0400629
Leon Scroggins III179559f2018-12-10 12:30:12 -0500630bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
631 skcms_PixelFormat* outFormat) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400632 SkASSERT(outFormat);
633
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400634 switch (colorType) {
635 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400636 *outFormat = skcms_PixelFormat_RGBA_8888;
637 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400638 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400639 *outFormat = skcms_PixelFormat_BGRA_8888;
640 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400641 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400642 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400643#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400644 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400645#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400646 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400647#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400648 break;
649 }
650 *outFormat = skcms_PixelFormat_BGR_565;
651 break;
652 case kRGBA_F16_SkColorType:
653 *outFormat = skcms_PixelFormat_RGBA_hhhh;
654 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400655 case kGray_8_SkColorType:
656 *outFormat = skcms_PixelFormat_G_8;
657 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400658 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000659 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000660 }
Matt Sarett313c4632016-10-20 12:35:23 -0400661 return true;
662}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400663
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400664bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
665 bool srcIsOpaque) {
666 fXformTime = kNo_XformTime;
667 bool needsColorXform = false;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500668 if (this->usesColorXform()) {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400669 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400670 needsColorXform = true;
Leon Scroggins III196f3192020-02-03 12:39:54 -0500671 if (dstInfo.colorSpace()) {
672 dstInfo.colorSpace()->toProfile(&fDstProfile);
673 } else {
674 // Use the srcProfile to avoid conversion.
675 const auto* srcProfile = fEncodedInfo.profile();
676 fDstProfile = srcProfile ? *srcProfile : *skcms_sRGB_profile();
677 }
678 } else if (dstInfo.colorSpace()) {
679 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400680 const auto* srcProfile = fEncodedInfo.profile();
681 if (!srcProfile) {
682 srcProfile = skcms_sRGB_profile();
683 }
684 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
685 needsColorXform = true;
686 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400687 }
688 }
689
Leon Scroggins III712476e2018-10-03 15:47:00 -0400690 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400691 return false;
692 }
693
694 if (needsColorXform) {
695 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
696 || kRGBA_F16_SkColorType == dstInfo.colorType()
697 ? kDecodeRow_XformTime : kPalette_XformTime;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500698 if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
699 &fDstXformFormat)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400700 return false;
701 }
702 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
703 && dstInfo.alphaType() == kPremul_SkAlphaType) {
704 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
705 } else {
706 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
707 }
708 }
709 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400710}
711
712void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400713 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400714 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400715 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
716 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
717 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400718}
719
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400720std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400721 const int frameCount = this->getFrameCount();
722 SkASSERT(frameCount >= 0);
723 if (frameCount <= 0) {
724 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400725 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400726
727 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
728 // Not animated.
729 return std::vector<FrameInfo>{};
730 }
731
732 std::vector<FrameInfo> result(frameCount);
733 for (int i = 0; i < frameCount; ++i) {
734 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
735 }
736 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400737}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500738
739const char* SkCodec::ResultToString(Result result) {
740 switch (result) {
741 case kSuccess:
742 return "success";
743 case kIncompleteInput:
744 return "incomplete input";
745 case kErrorInInput:
746 return "error in input";
747 case kInvalidConversion:
748 return "invalid conversion";
749 case kInvalidScale:
750 return "invalid scale";
751 case kInvalidParameters:
752 return "invalid parameters";
753 case kInvalidInput:
754 return "invalid input";
755 case kCouldNotRewind:
756 return "could not rewind";
757 case kInternalError:
758 return "internal error";
759 case kUnimplemented:
760 return "unimplemented";
761 default:
762 SkASSERT(false);
763 return "bogus result value";
764 }
765}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000766
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000767static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000768 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000769}
770
771static bool restore_bg(const SkFrame& frame) {
772 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
773}
774
Nigel Taob8ec7f12019-03-26 10:44:58 +1100775// As its name suggests, this method computes a frame's alpha (e.g. completely
776// opaque, unpremul, binary) and its required frame (a preceding frame that
777// this frame depends on, to draw the complete image at this frame's point in
778// the animation stream), and calls this frame's setter methods with that
779// computed information.
780//
781// A required frame of kNoFrame means that this frame is independent: drawing
782// the complete image at this frame's point in the animation stream does not
783// require first preparing the pixel buffer based on another frame. Instead,
784// drawing can start from an uninitialized pixel buffer.
785//
786// "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
787// implementation, for independent frames, first party Skia code (in src/codec)
788// will typically fill the buffer with a uniform background color (e.g.
789// transparent black) before calling into third party codec-specific code (e.g.
790// libjpeg or libpng). Pixels outside of the frame's rect will remain this
791// background color after drawing this frame. For incomplete decodes, pixels
792// inside that rect may be (at least temporarily) set to that background color.
793// In an incremental decode, later passes may then overwrite that background
794// color.
795//
796// Determining kNoFrame or otherwise involves testing a number of conditions
797// sequentially. The first satisfied condition results in setting the required
798// frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
799// "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
800// labels also map to comments in the function body.
801//
802// - IND1: this frame is the first frame.
803// - IND2: this frame fills out the whole image, and it is completely opaque
804// or it overwrites (not blends with) the previous frame.
805// - IND3: all preceding frames' disposals are kRestorePrevious.
806// - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
807// whole image or it is itself otherwise independent.
808// - DEP5: this frame reports alpha (it is not completely opaque) and it
809// blends with (not overwrites) the previous frame.
810// - IND6: this frame's rect covers the rects of all preceding frames back to
811// and including the most recent independent frame before this frame.
812// - DEP7: unconditional.
813//
814// The "prevFrame" variable initially points to the previous frame (also known
815// as the prior frame), but that variable may iterate further backwards over
816// the course of this computation.
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000817void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
818 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
819 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
820 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
821
822 const int i = frame->frameId();
823 if (0 == i) {
824 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100825 frame->setRequiredFrame(SkCodec::kNoFrame); // IND1
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000826 return;
827 }
828
829
830 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
831 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
832 frame->setHasAlpha(reportsAlpha);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100833 frame->setRequiredFrame(SkCodec::kNoFrame); // IND2
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000834 return;
835 }
836
837 const SkFrame* prevFrame = this->getFrame(i-1);
838 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
839 const int prevId = prevFrame->frameId();
840 if (0 == prevId) {
841 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100842 frame->setRequiredFrame(SkCodec::kNoFrame); // IND3
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000843 return;
844 }
845
846 prevFrame = this->getFrame(prevId - 1);
847 }
848
849 const bool clearPrevFrame = restore_bg(*prevFrame);
850 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
851
852 if (clearPrevFrame) {
853 if (prevFrameRect == screenRect || independent(*prevFrame)) {
854 frame->setHasAlpha(true);
Nigel Taob8ec7f12019-03-26 10:44:58 +1100855 frame->setRequiredFrame(SkCodec::kNoFrame); // IND4
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000856 return;
857 }
858 }
859
860 if (reportsAlpha && blendWithPrevFrame) {
861 // Note: We could be more aggressive here. If prevFrame clears
862 // to background color and covers its required frame (and that
863 // frame is independent), prevFrame could be marked independent.
864 // Would this extra complexity be worth it?
Nigel Taob8ec7f12019-03-26 10:44:58 +1100865 frame->setRequiredFrame(prevFrame->frameId()); // DEP5
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000866 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
867 return;
868 }
869
870 while (frameRect.contains(prevFrameRect)) {
871 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000872 if (prevRequiredFrame == SkCodec::kNoFrame) {
Nigel Taob8ec7f12019-03-26 10:44:58 +1100873 frame->setRequiredFrame(SkCodec::kNoFrame); // IND6
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000874 frame->setHasAlpha(true);
875 return;
876 }
877
878 prevFrame = this->getFrame(prevRequiredFrame);
879 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
880 }
881
Nigel Taob8ec7f12019-03-26 10:44:58 +1100882 frame->setRequiredFrame(prevFrame->frameId()); // DEP7
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000883 if (restore_bg(*prevFrame)) {
884 frame->setHasAlpha(true);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000885 return;
886 }
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000887 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000888 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
889}
890