blob: 2f2e0831679f5b31acb6e8a2c52b056f9ef14622 [file] [log] [blame]
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001/*
2 * Copyright 2018 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 "src/codec/SkWuffsCodec.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkMatrix.h"
12#include "include/core/SkPaint.h"
13#include "include/private/SkMalloc.h"
14#include "src/codec/SkFrameHolder.h"
15#include "src/codec/SkSampler.h"
16#include "src/codec/SkScalingCodec.h"
17#include "src/core/SkDraw.h"
Brian Osman9aaec362020-05-08 14:54:37 -040018#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkRasterClip.h"
20#include "src/core/SkUtils.h"
Nigel Taoa6766482019-01-07 13:41:53 +110021
Ben Wagner666a9f92019-05-02 17:45:00 -040022#include <limits.h>
23
Nigel Tao7b8b0ec2019-12-16 17:41:25 +110024// Documentation on the Wuffs language and standard library (in general) and
25// its image decoding API (in particular) is at:
26//
27// - https://github.com/google/wuffs/tree/master/doc
28// - https://github.com/google/wuffs/blob/master/doc/std/image-decoders.md
29
Nigel Taoa6766482019-01-07 13:41:53 +110030// Wuffs ships as a "single file C library" or "header file library" as per
31// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
32//
33// As we have not #define'd WUFFS_IMPLEMENTATION, the #include here is
34// including a header file, even though that file name ends in ".c".
Nigel Taoe66a0b22019-03-09 15:03:14 +110035#if defined(WUFFS_IMPLEMENTATION)
36#error "SkWuffsCodec should not #define WUFFS_IMPLEMENTATION"
37#endif
Nigel Taob54946b2020-06-18 23:36:27 +100038#include "wuffs-v0.3.c"
Nigel Tao5cfa7192020-08-17 21:14:13 +100039// Commit count 2514 is Wuffs 0.3.0-alpha.4.
40#if WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT < 2514
Nigel Taoa6766482019-01-07 13:41:53 +110041#error "Wuffs version is too old. Upgrade to the latest version."
42#endif
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040043
44#define SK_WUFFS_CODEC_BUFFER_SIZE 4096
45
Nigel Tao3dc31212019-12-07 11:46:16 +110046// Configuring a Skia build with
47// SK_WUFFS_FAVORS_PERFORMANCE_OVER_ADDITIONAL_MEMORY_SAFETY can improve decode
48// performance by some fixed amount (independent of the image size), which can
49// be a noticeable proportional improvement if the input is relatively small.
50//
51// The Wuffs library is still memory-safe either way, in that there are no
52// out-of-bounds reads or writes, and the library endeavours not to read
53// uninitialized memory. There are just fewer compiler-enforced guarantees
54// against reading uninitialized memory. For more detail, see
55// https://github.com/google/wuffs/blob/master/doc/note/initialization.md#partial-zero-initialization
56#if defined(SK_WUFFS_FAVORS_PERFORMANCE_OVER_ADDITIONAL_MEMORY_SAFETY)
57#define SK_WUFFS_INITIALIZE_FLAGS WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED
58#else
59#define SK_WUFFS_INITIALIZE_FLAGS WUFFS_INITIALIZE__DEFAULT_OPTIONS
60#endif
61
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040062static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
63 b->compact();
64 size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
65 b->meta.wi += num_read;
66 b->meta.closed = s->isAtEnd();
67 return num_read > 0;
68}
69
70static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
71 // Try to re-position the io_buffer's meta.ri read-index first, which is
72 // cheaper than seeking in the backing SkStream.
73 if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
74 b->meta.ri = pos - b->meta.pos;
75 return true;
76 }
77 // Seek in the backing SkStream.
78 if ((pos > SIZE_MAX) || (!s->seek(pos))) {
79 return false;
80 }
81 b->meta.wi = 0;
82 b->meta.ri = 0;
83 b->meta.pos = pos;
84 b->meta.closed = false;
85 return true;
86}
87
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040088static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
89 wuffs_base__animation_disposal w) {
90 switch (w) {
91 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
92 return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
93 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
94 return SkCodecAnimation::DisposalMethod::kRestorePrevious;
95 default:
96 return SkCodecAnimation::DisposalMethod::kKeep;
97 }
98}
99
Nigel Tao4f32a292019-11-13 15:41:55 +1100100static SkAlphaType to_alpha_type(bool opaque) {
101 return opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
102}
103
Nigel Tao2777cd32019-10-29 11:10:25 +1100104static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
105 wuffs_base__image_config* imgcfg,
106 wuffs_base__io_buffer* b,
Nigel Tao4f32a292019-11-13 15:41:55 +1100107 SkStream* s) {
Nigel Tao3dc31212019-12-07 11:46:16 +1100108 // Calling decoder->initialize will memset most or all of it to zero,
109 // depending on SK_WUFFS_INITIALIZE_FLAGS.
Nigel Taob54946b2020-06-18 23:36:27 +1000110 wuffs_base__status status =
Nigel Tao3dc31212019-12-07 11:46:16 +1100111 decoder->initialize(sizeof__wuffs_gif__decoder(), WUFFS_VERSION, SK_WUFFS_INITIALIZE_FLAGS);
Nigel Taob54946b2020-06-18 23:36:27 +1000112 if (status.repr != nullptr) {
113 SkCodecPrintf("initialize: %s", status.message());
114 return SkCodec::kInternalError;
115 }
Leon Scroggins III816833d2021-06-01 13:23:13 -0400116
117 // See https://bugs.chromium.org/p/skia/issues/detail?id=12055
118 decoder->set_quirk_enabled(WUFFS_GIF__QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA, true);
119
Nigel Taob54946b2020-06-18 23:36:27 +1000120 while (true) {
121 status = decoder->decode_image_config(imgcfg, b);
122 if (status.repr == nullptr) {
123 break;
124 } else if (status.repr != wuffs_base__suspension__short_read) {
125 SkCodecPrintf("decode_image_config: %s", status.message());
126 return SkCodec::kErrorInInput;
127 } else if (!fill_buffer(b, s)) {
128 return SkCodec::kIncompleteInput;
129 }
130 }
Nigel Tao4f32a292019-11-13 15:41:55 +1100131
132 // A GIF image's natural color model is indexed color: 1 byte per pixel,
133 // indexing a 256-element palette.
134 //
135 // For Skia, we override that to decode to 4 bytes per pixel, BGRA or RGBA.
Nigel Taob54946b2020-06-18 23:36:27 +1000136 uint32_t pixfmt = WUFFS_BASE__PIXEL_FORMAT__INVALID;
Nigel Tao4f32a292019-11-13 15:41:55 +1100137 switch (kN32_SkColorType) {
138 case kBGRA_8888_SkColorType:
139 pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
140 break;
141 case kRGBA_8888_SkColorType:
142 pixfmt = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
143 break;
144 default:
145 return SkCodec::kInternalError;
146 }
147 if (imgcfg) {
148 imgcfg->pixcfg.set(pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, imgcfg->pixcfg.width(),
149 imgcfg->pixcfg.height());
150 }
151
152 return SkCodec::kSuccess;
153}
154
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400155// -------------------------------- Class definitions
156
157class SkWuffsCodec;
158
159class SkWuffsFrame final : public SkFrame {
160public:
161 SkWuffsFrame(wuffs_base__frame_config* fc);
162
Nigel Tao97771572020-12-15 20:28:32 +1100163 uint64_t ioPosition() const;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400164
165 // SkFrame overrides.
166 SkEncodedInfo::Alpha onReportedAlpha() const override;
167
168private:
169 uint64_t fIOPosition;
170 SkEncodedInfo::Alpha fReportedAlpha;
171
John Stiles7571f9e2020-09-02 22:42:33 -0400172 using INHERITED = SkFrame;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400173};
174
175// SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
176// SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
177// inherit from both SkCodec and SkFrameHolder, and Skia style discourages
178// multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
179class SkWuffsFrameHolder final : public SkFrameHolder {
180public:
181 SkWuffsFrameHolder() : INHERITED() {}
182
183 void init(SkWuffsCodec* codec, int width, int height);
184
185 // SkFrameHolder overrides.
186 const SkFrame* onGetFrame(int i) const override;
187
188private:
189 const SkWuffsCodec* fCodec;
190
John Stiles7571f9e2020-09-02 22:42:33 -0400191 using INHERITED = SkFrameHolder;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400192};
193
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400194class SkWuffsCodec final : public SkScalingCodec {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400195public:
196 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
197 std::unique_ptr<SkStream> stream,
198 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400199 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
200 size_t workbuf_len,
201 wuffs_base__image_config imgcfg,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400202 wuffs_base__io_buffer iobuf);
203
204 const SkWuffsFrame* frame(int i) const;
205
206private:
Nigel Tao97771572020-12-15 20:28:32 +1100207 // TODO: delete this enum and all of the "which" function arguments. The
208 // "array of 1 Foo" typed fields can also simplify to "Foo".
Nigel Tao2777cd32019-10-29 11:10:25 +1100209 enum WhichDecoder {
210 kIncrDecode,
Nigel Tao2777cd32019-10-29 11:10:25 +1100211 kNumDecoders,
212 };
213
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400214 // SkCodec overrides.
215 SkEncodedImageFormat onGetEncodedFormat() const override;
216 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
217 const SkFrameHolder* getFrameHolder() const override;
218 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
219 void* dst,
220 size_t rowBytes,
221 const SkCodec::Options& options) override;
222 Result onIncrementalDecode(int* rowsDecoded) override;
223 int onGetFrameCount() override;
224 bool onGetFrameInfo(int, FrameInfo*) const override;
225 int onGetRepetitionCount() override;
226
Nigel Tao027f89b2019-12-06 10:56:24 +1100227 // Two separate implementations of onStartIncrementalDecode and
228 // onIncrementalDecode, named "one pass" and "two pass" decoding. One pass
229 // decoding writes directly from the Wuffs image decoder to the dst buffer
230 // (the dst argument to onStartIncrementalDecode). Two pass decoding first
231 // writes into an intermediate buffer, and then composites and transforms
232 // the intermediate buffer into the dst buffer.
233 //
234 // In the general case, we need the two pass decoder, because of Skia API
235 // features that Wuffs doesn't support (e.g. color correction, scaling,
236 // RGB565). But as an optimization, we use one pass decoding (it's faster
237 // and uses less memory) if applicable (see the assignment to
238 // fIncrDecOnePass that calculates when we can do so).
Nigel Taob54946b2020-06-18 23:36:27 +1000239 Result onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
240 uint8_t* dst,
241 size_t rowBytes,
242 const SkCodec::Options& options,
243 uint32_t pixelFormat,
244 size_t bytesPerPixel);
Nigel Tao027f89b2019-12-06 10:56:24 +1100245 Result onStartIncrementalDecodeTwoPass();
246 Result onIncrementalDecodeOnePass();
247 Result onIncrementalDecodeTwoPass();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400248
Nigel Tao027f89b2019-12-06 10:56:24 +1100249 void onGetFrameCountInternal();
250 Result seekFrame(WhichDecoder which, int frameIndex);
Nigel Tao2777cd32019-10-29 11:10:25 +1100251 Result resetDecoder(WhichDecoder which);
252 const char* decodeFrameConfig(WhichDecoder which);
253 const char* decodeFrame(WhichDecoder which);
254 void updateNumFullyReceivedFrames(WhichDecoder which);
255
256 SkWuffsFrameHolder fFrameHolder;
257 std::unique_ptr<SkStream> fStream;
Nigel Tao2777cd32019-10-29 11:10:25 +1100258 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
259 size_t fWorkbufLen;
260
261 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoders[WhichDecoder::kNumDecoders];
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400262
263 const uint64_t fFirstFrameIOPosition;
Nigel Tao2777cd32019-10-29 11:10:25 +1100264 wuffs_base__frame_config fFrameConfigs[WhichDecoder::kNumDecoders];
Nigel Tao027f89b2019-12-06 10:56:24 +1100265 wuffs_base__pixel_config fPixelConfig;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400266 wuffs_base__pixel_buffer fPixelBuffer;
267 wuffs_base__io_buffer fIOBuffer;
268
269 // Incremental decoding state.
Nigel Taobf4605f2020-09-28 21:53:07 +1000270 uint8_t* fIncrDecDst;
Nigel Taobf4605f2020-09-28 21:53:07 +1000271 size_t fIncrDecRowBytes;
272 wuffs_base__pixel_blend fIncrDecPixelBlend;
273 bool fIncrDecOnePass;
274 bool fFirstCallToIncrementalDecode;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400275
Nigel Tao027f89b2019-12-06 10:56:24 +1100276 // Lazily allocated intermediate pixel buffer, for two pass decoding.
277 std::unique_ptr<uint8_t, decltype(&sk_free)> fTwoPassPixbufPtr;
278 size_t fTwoPassPixbufLen;
279
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400280 uint64_t fNumFullyReceivedFrames;
281 std::vector<SkWuffsFrame> fFrames;
282 bool fFramesComplete;
283
Nigel Tao2777cd32019-10-29 11:10:25 +1100284 // If calling an fDecoders[which] method returns an incomplete status, then
285 // fDecoders[which] is suspended in a coroutine (i.e. waiting on I/O or
286 // halted on a non-recoverable error). To keep its internal proof-of-safety
287 // invariants consistent, there's only two things you can safely do with a
288 // suspended Wuffs object: resume the coroutine, or reset all state (memset
289 // to zero and start again).
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400290 //
Nigel Tao2777cd32019-10-29 11:10:25 +1100291 // If fDecoderIsSuspended[which], and we aren't sure that we're going to
292 // resume the coroutine, then we will need to call this->resetDecoder
293 // before calling other fDecoders[which] methods.
294 bool fDecoderIsSuspended[WhichDecoder::kNumDecoders];
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400295
296 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
297
John Stiles7571f9e2020-09-02 22:42:33 -0400298 using INHERITED = SkScalingCodec;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400299};
300
301// -------------------------------- SkWuffsFrame implementation
302
303SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
304 : INHERITED(fc->index()),
305 fIOPosition(fc->io_position()),
Nigel Taob54946b2020-06-18 23:36:27 +1000306 fReportedAlpha(fc->opaque_within_bounds() ? SkEncodedInfo::kOpaque_Alpha
Nigel Tao5cfa7192020-08-17 21:14:13 +1000307 : SkEncodedInfo::kUnpremul_Alpha) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400308 wuffs_base__rect_ie_u32 r = fc->bounds();
309 this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
310 this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
311 this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500312 this->setBlend(fc->overwrite_instead_of_blend() ? SkCodecAnimation::Blend::kSrc
313 : SkCodecAnimation::Blend::kSrcOver);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400314}
315
316uint64_t SkWuffsFrame::ioPosition() const {
317 return fIOPosition;
318}
319
320SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
321 return fReportedAlpha;
322}
323
324// -------------------------------- SkWuffsFrameHolder implementation
325
326void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
327 fCodec = codec;
328 // Initialize SkFrameHolder's (the superclass) fields.
329 fScreenWidth = width;
330 fScreenHeight = height;
331}
332
333const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
334 return fCodec->frame(i);
335};
336
337// -------------------------------- SkWuffsCodec implementation
338
339SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
340 std::unique_ptr<SkStream> stream,
341 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400342 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
343 size_t workbuf_len,
344 wuffs_base__image_config imgcfg,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400345 wuffs_base__io_buffer iobuf)
346 : INHERITED(std::move(encodedInfo),
347 skcms_PixelFormat_RGBA_8888,
348 // Pass a nullptr SkStream to the SkCodec constructor. We
349 // manage the stream ourselves, as the default SkCodec behavior
350 // is too trigger-happy on rewinding the stream.
351 nullptr),
Nigel Tao0185b952018-11-08 10:47:24 +1100352 fFrameHolder(),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400353 fStream(std::move(stream)),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400354 fWorkbufPtr(std::move(workbuf_ptr)),
355 fWorkbufLen(workbuf_len),
Nigel Tao2777cd32019-10-29 11:10:25 +1100356 fDecoders{
357 std::move(dec),
Nigel Tao2777cd32019-10-29 11:10:25 +1100358 },
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400359 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
Nigel Tao2777cd32019-10-29 11:10:25 +1100360 fFrameConfigs{
361 wuffs_base__null_frame_config(),
Nigel Tao2777cd32019-10-29 11:10:25 +1100362 },
Nigel Tao027f89b2019-12-06 10:56:24 +1100363 fPixelConfig(imgcfg.pixcfg),
364 fPixelBuffer(wuffs_base__null_pixel_buffer()),
Nigel Tao96c10a02019-09-25 11:08:42 +1000365 fIOBuffer(wuffs_base__empty_io_buffer()),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400366 fIncrDecDst(nullptr),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400367 fIncrDecRowBytes(0),
Nigel Taobf4605f2020-09-28 21:53:07 +1000368 fIncrDecPixelBlend(WUFFS_BASE__PIXEL_BLEND__SRC),
Nigel Tao027f89b2019-12-06 10:56:24 +1100369 fIncrDecOnePass(false),
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400370 fFirstCallToIncrementalDecode(false),
Nigel Tao027f89b2019-12-06 10:56:24 +1100371 fTwoPassPixbufPtr(nullptr, &sk_free),
372 fTwoPassPixbufLen(0),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400373 fNumFullyReceivedFrames(0),
374 fFramesComplete(false),
Nigel Tao2777cd32019-10-29 11:10:25 +1100375 fDecoderIsSuspended{
376 false,
Nigel Tao2777cd32019-10-29 11:10:25 +1100377 } {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400378 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
379
380 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
381 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
382 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
383 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
384 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
Nigel Tao48aa2212019-03-09 14:59:11 +1100385 fIOBuffer.data = wuffs_base__make_slice_u8(fBuffer, SK_WUFFS_CODEC_BUFFER_SIZE);
386 fIOBuffer.meta = iobuf.meta;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400387}
388
389const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
390 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
391 return &fFrames[i];
392 }
393 return nullptr;
394}
395
396SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
397 return SkEncodedImageFormat::kGIF;
398}
399
400SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
401 void* dst,
402 size_t rowBytes,
403 const Options& options,
404 int* rowsDecoded) {
405 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
406 if (result != kSuccess) {
407 return result;
408 }
409 return this->onIncrementalDecode(rowsDecoded);
410}
411
412const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
413 return &fFrameHolder;
414}
415
416SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
417 void* dst,
418 size_t rowBytes,
419 const SkCodec::Options& options) {
Nigel Tao1f1cd1f2019-06-22 17:35:38 +1000420 if (!dst) {
421 return SkCodec::kInvalidParameters;
422 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400423 if (options.fSubset) {
424 return SkCodec::kUnimplemented;
425 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100426 SkCodec::Result result = this->seekFrame(WhichDecoder::kIncrDecode, options.fFrameIndex);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400427 if (result != SkCodec::kSuccess) {
428 return result;
429 }
430
Nigel Tao2777cd32019-10-29 11:10:25 +1100431 const char* status = this->decodeFrameConfig(WhichDecoder::kIncrDecode);
Nigel Taob7a1b512019-02-10 12:19:50 +1100432 if (status == wuffs_base__suspension__short_read) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500433 return SkCodec::kIncompleteInput;
Nigel Taob7a1b512019-02-10 12:19:50 +1100434 } else if (status != nullptr) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500435 SkCodecPrintf("decodeFrameConfig: %s", status);
436 return SkCodec::kErrorInInput;
437 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100438
Nigel Taob54946b2020-06-18 23:36:27 +1000439 uint32_t pixelFormat = WUFFS_BASE__PIXEL_FORMAT__INVALID;
Nigel Tao5cfa7192020-08-17 21:14:13 +1000440 size_t bytesPerPixel = 0;
Nigel Tao027f89b2019-12-06 10:56:24 +1100441
442 switch (dstInfo.colorType()) {
Nigel Taof933e4f2020-10-29 09:42:11 +1100443 case kRGB_565_SkColorType:
444 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGR_565;
445 bytesPerPixel = 2;
446 break;
Nigel Tao027f89b2019-12-06 10:56:24 +1100447 case kBGRA_8888_SkColorType:
448 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
449 bytesPerPixel = 4;
450 break;
451 case kRGBA_8888_SkColorType:
452 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
453 bytesPerPixel = 4;
454 break;
455 default:
456 break;
457 }
458
459 // We can use "one pass" decoding if we have a Skia pixel format that Wuffs
460 // supports...
Nigel Taobf4605f2020-09-28 21:53:07 +1000461 fIncrDecOnePass = (pixelFormat != WUFFS_BASE__PIXEL_FORMAT__INVALID) &&
462 // ...and no color profile (as Wuffs does not support them)...
463 (!getEncodedInfo().profile()) &&
464 // ...and we use the identity transform (as Wuffs does
465 // not support scaling).
466 (this->dimensions() == dstInfo.dimensions());
Nigel Tao027f89b2019-12-06 10:56:24 +1100467
468 result = fIncrDecOnePass ? this->onStartIncrementalDecodeOnePass(
469 dstInfo, static_cast<uint8_t*>(dst), rowBytes, options,
470 pixelFormat, bytesPerPixel)
471 : this->onStartIncrementalDecodeTwoPass();
472 if (result != SkCodec::kSuccess) {
473 return result;
474 }
475
476 fIncrDecDst = static_cast<uint8_t*>(dst);
Nigel Tao027f89b2019-12-06 10:56:24 +1100477 fIncrDecRowBytes = rowBytes;
478 fFirstCallToIncrementalDecode = true;
479 return SkCodec::kSuccess;
480}
481
Nigel Taob54946b2020-06-18 23:36:27 +1000482SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
483 uint8_t* dst,
484 size_t rowBytes,
485 const SkCodec::Options& options,
486 uint32_t pixelFormat,
Nigel Tao027f89b2019-12-06 10:56:24 +1100487 size_t bytesPerPixel) {
488 wuffs_base__pixel_config pixelConfig;
489 pixelConfig.set(pixelFormat, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, dstInfo.width(),
490 dstInfo.height());
491
492 wuffs_base__table_u8 table;
493 table.ptr = dst;
494 table.width = static_cast<size_t>(dstInfo.width()) * bytesPerPixel;
495 table.height = dstInfo.height();
496 table.stride = rowBytes;
497
Nigel Taob54946b2020-06-18 23:36:27 +1000498 wuffs_base__status status = fPixelBuffer.set_from_table(&pixelConfig, table);
Nigel Taob54946b2020-06-18 23:36:27 +1000499 if (status.repr != nullptr) {
500 SkCodecPrintf("set_from_table: %s", status.message());
501 return SkCodec::kInternalError;
502 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100503
Nigel Taobf4605f2020-09-28 21:53:07 +1000504 // SRC is usually faster than SRC_OVER, but for a dependent frame, dst is
505 // assumed to hold the previous frame's pixels (after processing the
506 // DisposalMethod). For one-pass decoding, we therefore use SRC_OVER.
507 if ((options.fFrameIndex != 0) &&
508 (this->frame(options.fFrameIndex)->getRequiredFrame() != SkCodec::kNoFrame)) {
509 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC_OVER;
510 } else {
511 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
512 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
513 }
514
Nigel Tao027f89b2019-12-06 10:56:24 +1100515 return SkCodec::kSuccess;
516}
517
518SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeTwoPass() {
519 // Either re-use the previously allocated "two pass" pixel buffer (and
520 // memset to zero), or allocate (and zero initialize) a new one.
521 bool already_zeroed = false;
522
523 if (!fTwoPassPixbufPtr) {
524 uint64_t pixbuf_len = fPixelConfig.pixbuf_len();
525 void* pixbuf_ptr_raw = (pixbuf_len <= SIZE_MAX)
Nigel Tao5cfa7192020-08-17 21:14:13 +1000526 ? sk_malloc_flags(pixbuf_len, SK_MALLOC_ZERO_INITIALIZE)
527 : nullptr;
Nigel Tao027f89b2019-12-06 10:56:24 +1100528 if (!pixbuf_ptr_raw) {
529 return SkCodec::kInternalError;
530 }
531 fTwoPassPixbufPtr.reset(reinterpret_cast<uint8_t*>(pixbuf_ptr_raw));
532 fTwoPassPixbufLen = SkToSizeT(pixbuf_len);
533 already_zeroed = true;
534 }
535
Nigel Taob54946b2020-06-18 23:36:27 +1000536 wuffs_base__status status = fPixelBuffer.set_from_slice(
Nigel Tao027f89b2019-12-06 10:56:24 +1100537 &fPixelConfig, wuffs_base__make_slice_u8(fTwoPassPixbufPtr.get(), fTwoPassPixbufLen));
Nigel Taob54946b2020-06-18 23:36:27 +1000538 if (status.repr != nullptr) {
539 SkCodecPrintf("set_from_slice: %s", status.message());
540 return SkCodec::kInternalError;
541 }
Nigel Tao027f89b2019-12-06 10:56:24 +1100542
543 if (!already_zeroed) {
Nigel Taob54946b2020-06-18 23:36:27 +1000544 uint32_t src_bits_per_pixel = fPixelConfig.pixel_format().bits_per_pixel();
Nigel Tao027f89b2019-12-06 10:56:24 +1100545 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
546 return SkCodec::kInternalError;
547 }
548 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
549
Nigel Tao39da10b2019-11-15 15:27:44 +1100550 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
551 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Nigel Tao6ec1b392019-11-20 12:28:50 +1100552
553 uint8_t* ptr = pixels.ptr + (frame_rect.min_incl_y * pixels.stride) +
554 (frame_rect.min_incl_x * src_bytes_per_pixel);
555 size_t len = frame_rect.width() * src_bytes_per_pixel;
556
557 // As an optimization, issue a single sk_bzero call, if possible.
558 // Otherwise, zero out each row separately.
559 if ((len == pixels.stride) && (frame_rect.min_incl_y < frame_rect.max_excl_y)) {
560 sk_bzero(ptr, len * (frame_rect.max_excl_y - frame_rect.min_incl_y));
561 } else {
562 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
563 sk_bzero(ptr, len);
564 ptr += pixels.stride;
565 }
Nigel Tao39da10b2019-11-15 15:27:44 +1100566 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100567 }
568
Nigel Taobf4605f2020-09-28 21:53:07 +1000569 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
Nigel Taob7a1b512019-02-10 12:19:50 +1100570 return SkCodec::kSuccess;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400571}
572
573SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
574 if (!fIncrDecDst) {
575 return SkCodec::kInternalError;
576 }
577
Nigel Tao027f89b2019-12-06 10:56:24 +1100578 if (rowsDecoded) {
579 *rowsDecoded = dstInfo().height();
580 }
581
582 SkCodec::Result result =
583 fIncrDecOnePass ? this->onIncrementalDecodeOnePass() : this->onIncrementalDecodeTwoPass();
Nigel Tao2777cd32019-10-29 11:10:25 +1100584 if (result == SkCodec::kSuccess) {
585 fIncrDecDst = nullptr;
Nigel Tao2777cd32019-10-29 11:10:25 +1100586 fIncrDecRowBytes = 0;
Nigel Taobf4605f2020-09-28 21:53:07 +1000587 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
Nigel Tao027f89b2019-12-06 10:56:24 +1100588 fIncrDecOnePass = false;
Nigel Tao2777cd32019-10-29 11:10:25 +1100589 }
590 return result;
591}
592
Nigel Tao027f89b2019-12-06 10:56:24 +1100593SkCodec::Result SkWuffsCodec::onIncrementalDecodeOnePass() {
594 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
595 if (status != nullptr) {
596 if (status == wuffs_base__suspension__short_read) {
597 return SkCodec::kIncompleteInput;
598 } else {
599 SkCodecPrintf("decodeFrame: %s", status);
600 return SkCodec::kErrorInInput;
601 }
602 }
603 return SkCodec::kSuccess;
604}
605
606SkCodec::Result SkWuffsCodec::onIncrementalDecodeTwoPass() {
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500607 SkCodec::Result result = SkCodec::kSuccess;
Nigel Tao2777cd32019-10-29 11:10:25 +1100608 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
609 bool independent;
610 SkAlphaType alphaType;
611 const int index = options().fFrameIndex;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400612 if (index == 0) {
613 independent = true;
614 alphaType = to_alpha_type(getEncodedInfo().opaque());
615 } else {
616 const SkWuffsFrame* f = this->frame(index);
617 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
618 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
619 }
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500620 if (status != nullptr) {
621 if (status == wuffs_base__suspension__short_read) {
622 result = SkCodec::kIncompleteInput;
623 } else {
624 SkCodecPrintf("decodeFrame: %s", status);
625 result = SkCodec::kErrorInInput;
626 }
627
628 if (!independent) {
629 // For a dependent frame, we cannot blend the partial result, since
630 // that will overwrite the contribution from prior frames.
631 return result;
632 }
633 }
634
Nigel Taob54946b2020-06-18 23:36:27 +1000635 uint32_t src_bits_per_pixel = fPixelBuffer.pixcfg.pixel_format().bits_per_pixel();
Nigel Tao490e6472019-02-14 14:50:53 +1100636 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
637 return SkCodec::kInternalError;
638 }
639 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
640
Nigel Tao2777cd32019-10-29 11:10:25 +1100641 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400642 if (fFirstCallToIncrementalDecode) {
Nigel Tao490e6472019-02-14 14:50:53 +1100643 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
644 return SkCodec::kInternalError;
645 }
646
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400647 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
648 frame_rect.max_excl_x, frame_rect.max_excl_y);
649
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500650 // If the frame rect does not fill the output, ensure that those pixels are not
Nigel Taob7a1b512019-02-10 12:19:50 +1100651 // left uninitialized.
Leon Scroggins III44076362019-02-15 13:56:44 -0500652 if (independent && (bounds != this->bounds() || result != kSuccess)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100653 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes, options().fZeroInitialized);
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500654 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400655 fFirstCallToIncrementalDecode = false;
656 } else {
657 // Existing clients intend to only show frames beyond the first if they
658 // are complete (based on FrameInfo::fFullyReceived), since it might
659 // look jarring to draw a partial frame over an existing frame. If they
660 // changed their behavior and expected to continue decoding a partial
661 // frame after the first one, we'll need to update our blending code.
662 // Otherwise, if the frame were interlaced and not independent, the
663 // second pass may have an overlapping dirty_rect with the first,
664 // resulting in blending with the first pass.
665 SkASSERT(index == 0);
Nigel Tao9859ef82019-02-13 13:20:02 +1100666 }
Nigel Tao0185b952018-11-08 10:47:24 +1100667
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500668 // If the frame's dirty rect is empty, no need to swizzle.
Nigel Tao2777cd32019-10-29 11:10:25 +1100669 wuffs_base__rect_ie_u32 dirty_rect = fDecoders[WhichDecoder::kIncrDecode]->frame_dirty_rect();
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500670 if (!dirty_rect.is_empty()) {
Nigel Tao490e6472019-02-14 14:50:53 +1100671 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500672
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400673 // The Wuffs model is that the dst buffer is the image, not the frame.
674 // The expectation is that you allocate the buffer once, but re-use it
675 // for the N frames, regardless of each frame's top-left co-ordinate.
676 //
677 // To get from the start (in the X-direction) of the image to the start
678 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
Nigel Tao2777cd32019-10-29 11:10:25 +1100679 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride) +
680 (dirty_rect.min_incl_x * src_bytes_per_pixel);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500681
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400682 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
683 // used for other formats that might have one, we will need to transform from profiles that
684 // do not have corresponding SkColorSpaces.
685 SkASSERT(!getEncodedInfo().profile());
686
Nigel Tao2777cd32019-10-29 11:10:25 +1100687 auto srcInfo =
688 getInfo().makeWH(dirty_rect.width(), dirty_rect.height()).makeAlphaType(alphaType);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400689 SkBitmap src;
690 src.installPixels(srcInfo, s, pixels.stride);
691 SkPaint paint;
692 if (independent) {
693 paint.setBlendMode(SkBlendMode::kSrc);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500694 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400695
696 SkDraw draw;
697 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
Mike Reed2ac6ce82021-01-15 12:26:22 -0500698 SkMatrix matrix = SkMatrix::RectToRect(SkRect::Make(this->dimensions()),
699 SkRect::Make(this->dstInfo().dimensions()));
Brian Osman9aaec362020-05-08 14:54:37 -0400700 SkSimpleMatrixProvider matrixProvider(matrix);
701 draw.fMatrixProvider = &matrixProvider;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400702 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
703 draw.fRC = &rc;
704
Mike Reed1f607332020-05-21 12:11:27 -0400705 SkMatrix translate = SkMatrix::Translate(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
Mike Reed172ba9e2020-12-17 15:57:55 -0500706 draw.drawBitmap(src, translate, nullptr, SkSamplingOptions(), paint);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500707 }
Nigel Taof0148c42019-12-08 19:12:13 +1100708
709 if (result == SkCodec::kSuccess) {
710 // On success, we are done using the "two pass" pixel buffer for this
711 // frame. We have the option of releasing its memory, but there is a
712 // trade-off. If decoding a subsequent frame will also need "two pass"
713 // decoding, it would have to re-allocate the buffer instead of just
714 // re-using it. On the other hand, if there is no subsequent frame, and
715 // the SkWuffsCodec object isn't deleted soon, then we are holding
716 // megabytes of memory longer than we need to.
717 //
718 // For example, when the Chromium web browser decodes the <img> tags in
719 // a HTML page, the SkCodec object can live until navigating away from
720 // the page, which can be much longer than when the pixels are fully
721 // decoded, especially for a still (non-animated) image. Even for
722 // looping animations, caching the decoded frames (at the higher HTML
723 // renderer layer) may mean that each frame is only decoded once (at
724 // the lower SkCodec layer), in sequence.
725 //
726 // The heuristic we use here is to free the memory if we have decoded
727 // the last frame of the animation (or, for still images, the only
728 // frame). The output of the next decode request (if any) should be the
729 // same either way, but the steady state memory use should hopefully be
730 // lower than always keeping the fTwoPassPixbufPtr buffer up until the
731 // SkWuffsCodec destructor runs.
732 //
733 // This only applies to "two pass" decoding. "One pass" decoding does
734 // not allocate, free or otherwise use fTwoPassPixbufPtr.
735 if (fFramesComplete && (static_cast<size_t>(options().fFrameIndex) == fFrames.size() - 1)) {
736 fTwoPassPixbufPtr.reset(nullptr);
737 fTwoPassPixbufLen = 0;
738 }
739 }
740
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400741 return result;
742}
743
744int SkWuffsCodec::onGetFrameCount() {
Nigel Tao97771572020-12-15 20:28:32 +1100745 // It is valid, in terms of the SkCodec API, to call SkCodec::getFrameCount
746 // while in an incremental decode (after onStartIncrementalDecode returns
747 // and before onIncrementalDecode returns kSuccess).
748 //
749 // We should not advance the SkWuffsCodec' stream while doing so, even
750 // though other SkCodec implementations can return increasing values from
751 // onGetFrameCount when given more data. If we tried to do so, the
752 // subsequent resume of the incremental decode would continue reading from
753 // a different position in the I/O stream, leading to an incorrect error.
754 //
755 // Other SkCodec implementations can move the stream forward during
756 // onGetFrameCount because they assume that the stream is rewindable /
757 // seekable. For example, an alternative GIF implementation may choose to
758 // store, for each frame walked past when merely counting the number of
759 // frames, the I/O position of each of the frame's GIF data blocks. (A GIF
760 // frame's compressed data can have multiple data blocks, each at most 255
761 // bytes in length). Obviously, this can require O(numberOfFrames) extra
762 // memory to store these I/O positions. The constant factor is small, but
763 // it's still O(N), not O(1).
764 //
765 // Wuffs and SkWuffsCodec try to minimize relying on the rewindable /
766 // seekable assumption. By design, Wuffs per se aims for O(1) memory use
767 // (after any pixel buffers are allocated) instead of O(N), and its I/O
768 // type, wuffs_base__io_buffer, is not necessarily rewindable or seekable.
769 //
770 // The Wuffs API provides a limited, optional form of seeking, to the start
771 // of an animation frame's data, but does not provide arbitrary save and
772 // load of its internal state whilst in the middle of an animation frame.
773 bool incrementalDecodeIsInProgress = fIncrDecDst != nullptr;
774
775 if (!fFramesComplete && !incrementalDecodeIsInProgress) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100776 this->onGetFrameCountInternal();
Nigel Tao97771572020-12-15 20:28:32 +1100777 this->updateNumFullyReceivedFrames(WhichDecoder::kIncrDecode);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400778 }
779 return fFrames.size();
780}
781
Nigel Tao2777cd32019-10-29 11:10:25 +1100782void SkWuffsCodec::onGetFrameCountInternal() {
Nigel Tao97771572020-12-15 20:28:32 +1100783 size_t n = fFrames.size();
784 int i = n ? n - 1 : 0;
785 if (this->seekFrame(WhichDecoder::kIncrDecode, i) != SkCodec::kSuccess) {
786 return;
Nigel Tao2777cd32019-10-29 11:10:25 +1100787 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400788
789 // Iterate through the frames, converting from Wuffs'
790 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
Nigel Tao97771572020-12-15 20:28:32 +1100791 for (; i < INT_MAX; i++) {
792 const char* status = this->decodeFrameConfig(WhichDecoder::kIncrDecode);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400793 if (status == nullptr) {
794 // No-op.
Nigel Taob54946b2020-06-18 23:36:27 +1000795 } else if (status == wuffs_base__note__end_of_data) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400796 break;
797 } else {
798 return;
799 }
800
Nigel Tao97771572020-12-15 20:28:32 +1100801 if (static_cast<size_t>(i) < fFrames.size()) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400802 continue;
803 }
Nigel Tao97771572020-12-15 20:28:32 +1100804 fFrames.emplace_back(&fFrameConfigs[WhichDecoder::kIncrDecode]);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400805 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
806 fFrameHolder.setAlphaAndRequiredFrame(f);
807 }
808
809 fFramesComplete = true;
810}
811
Nigel Taocdc92382019-10-31 08:36:53 +1100812bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
813 const SkWuffsFrame* f = this->frame(i);
814 if (!f) {
815 return false;
816 }
817 if (frameInfo) {
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500818 f->fillIn(frameInfo, static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
Nigel Taocdc92382019-10-31 08:36:53 +1100819 }
820 return true;
821}
822
823int SkWuffsCodec::onGetRepetitionCount() {
824 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
825 // number is how many times to play the loop. Skia's int number is how many
826 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
827 // kRepetitionCountInfinite respectively to mean loop forever.
828 uint32_t n = fDecoders[WhichDecoder::kIncrDecode]->num_animation_loops();
829 if (n == 0) {
830 return SkCodec::kRepetitionCountInfinite;
831 }
832 n--;
833 return n < INT_MAX ? n : INT_MAX;
834}
835
Nigel Tao2777cd32019-10-29 11:10:25 +1100836SkCodec::Result SkWuffsCodec::seekFrame(WhichDecoder which, int frameIndex) {
837 if (fDecoderIsSuspended[which]) {
838 SkCodec::Result res = this->resetDecoder(which);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400839 if (res != SkCodec::kSuccess) {
840 return res;
841 }
842 }
843
844 uint64_t pos = 0;
845 if (frameIndex < 0) {
846 return SkCodec::kInternalError;
847 } else if (frameIndex == 0) {
848 pos = fFirstFrameIOPosition;
849 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
850 pos = fFrames[frameIndex].ioPosition();
851 } else {
852 return SkCodec::kInternalError;
853 }
854
855 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
856 return SkCodec::kInternalError;
857 }
Nigel Taob54946b2020-06-18 23:36:27 +1000858 wuffs_base__status status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100859 fDecoders[which]->restart_frame(frameIndex, fIOBuffer.reader_io_position());
Nigel Taob54946b2020-06-18 23:36:27 +1000860 if (status.repr != nullptr) {
861 return SkCodec::kInternalError;
862 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400863 return SkCodec::kSuccess;
864}
865
Nigel Tao2777cd32019-10-29 11:10:25 +1100866SkCodec::Result SkWuffsCodec::resetDecoder(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400867 if (!fStream->rewind()) {
868 return SkCodec::kInternalError;
869 }
Nigel Tao96c10a02019-09-25 11:08:42 +1000870 fIOBuffer.meta = wuffs_base__empty_io_buffer_meta();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400871
872 SkCodec::Result result =
Nigel Tao2777cd32019-10-29 11:10:25 +1100873 reset_and_decode_image_config(fDecoders[which].get(), nullptr, &fIOBuffer, fStream.get());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400874 if (result == SkCodec::kIncompleteInput) {
875 return SkCodec::kInternalError;
876 } else if (result != SkCodec::kSuccess) {
877 return result;
878 }
879
Nigel Tao2777cd32019-10-29 11:10:25 +1100880 fDecoderIsSuspended[which] = false;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400881 return SkCodec::kSuccess;
882}
883
Nigel Tao2777cd32019-10-29 11:10:25 +1100884const char* SkWuffsCodec::decodeFrameConfig(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400885 while (true) {
Nigel Taob54946b2020-06-18 23:36:27 +1000886 wuffs_base__status status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100887 fDecoders[which]->decode_frame_config(&fFrameConfigs[which], &fIOBuffer);
Nigel Taob54946b2020-06-18 23:36:27 +1000888 if ((status.repr == wuffs_base__suspension__short_read) &&
889 fill_buffer(&fIOBuffer, fStream.get())) {
890 continue;
891 }
892 fDecoderIsSuspended[which] = !status.is_complete();
893 this->updateNumFullyReceivedFrames(which);
894 return status.repr;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400895 }
896}
897
Nigel Tao2777cd32019-10-29 11:10:25 +1100898const char* SkWuffsCodec::decodeFrame(WhichDecoder which) {
899 while (true) {
Nigel Taob54946b2020-06-18 23:36:27 +1000900 wuffs_base__status status = fDecoders[which]->decode_frame(
Nigel Taobf4605f2020-09-28 21:53:07 +1000901 &fPixelBuffer, &fIOBuffer, fIncrDecPixelBlend,
Nigel Tao09e541c2020-10-29 16:18:36 +1100902 wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen), nullptr);
Nigel Taob54946b2020-06-18 23:36:27 +1000903 if ((status.repr == wuffs_base__suspension__short_read) &&
904 fill_buffer(&fIOBuffer, fStream.get())) {
905 continue;
906 }
907 fDecoderIsSuspended[which] = !status.is_complete();
908 this->updateNumFullyReceivedFrames(which);
909 return status.repr;
Nigel Tao2777cd32019-10-29 11:10:25 +1100910 }
911}
912
913void SkWuffsCodec::updateNumFullyReceivedFrames(WhichDecoder which) {
Nigel Tao6af1edc2019-01-19 15:12:39 +1100914 // num_decoded_frames's return value, n, can change over time, both up and
915 // down, as we seek back and forth in the underlying stream.
916 // fNumFullyReceivedFrames is the highest n we've seen.
Nigel Tao2777cd32019-10-29 11:10:25 +1100917 uint64_t n = fDecoders[which]->num_decoded_frames();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400918 if (fNumFullyReceivedFrames < n) {
919 fNumFullyReceivedFrames = n;
920 }
921}
922
923// -------------------------------- SkWuffsCodec.h functions
924
925bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
926 constexpr const char* gif_ptr = "GIF8";
927 constexpr size_t gif_len = 4;
928 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
929}
930
931std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
932 SkCodec::Result* result) {
Nigel Tao48aa2212019-03-09 14:59:11 +1100933 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
934 wuffs_base__io_buffer iobuf =
935 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
Nigel Tao96c10a02019-09-25 11:08:42 +1000936 wuffs_base__empty_io_buffer_meta());
Nigel Tao48aa2212019-03-09 14:59:11 +1100937 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400938
939 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
940 // the wuffs_base__etc types, the sizeof a file format specific type like
941 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
942 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
943 // to an opaque type: a private implementation detail. The API is always
944 // "set_foo(p, etc)" and not "p->foo = etc".
945 //
946 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
947 //
948 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
949 // the struct at compile time). Instead, we use sk_malloc_canfail, with
950 // sizeof__wuffs_gif__decoder returning the appropriate value for the
951 // (statically or dynamically) linked version of the Wuffs library.
952 //
953 // As a C (not C++) library, none of the Wuffs types have constructors or
954 // destructors.
955 //
956 // In RAII style, we can still use std::unique_ptr with these pointers, but
957 // we pair the pointer with sk_free instead of C++'s delete.
958 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
959 if (!decoder_raw) {
960 *result = SkCodec::kInternalError;
961 return nullptr;
962 }
963 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
964 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
965
966 SkCodec::Result reset_result =
967 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
968 if (reset_result != SkCodec::kSuccess) {
969 *result = reset_result;
970 return nullptr;
971 }
972
973 uint32_t width = imgcfg.pixcfg.width();
974 uint32_t height = imgcfg.pixcfg.height();
975 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
976 *result = SkCodec::kInvalidInput;
977 return nullptr;
978 }
979
Nigel Tao6af1edc2019-01-19 15:12:39 +1100980 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
Nigel Tao22e86242019-01-26 16:04:01 +1100981 void* workbuf_ptr_raw = nullptr;
982 if (workbuf_len) {
983 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
984 if (!workbuf_ptr_raw) {
985 *result = SkCodec::kInternalError;
986 return nullptr;
987 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400988 }
989 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
990 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
991
Nigel Tao490e6472019-02-14 14:50:53 +1100992 SkEncodedInfo::Color color =
Nigel Taob54946b2020-06-18 23:36:27 +1000993 (imgcfg.pixcfg.pixel_format().repr == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
Nigel Tao490e6472019-02-14 14:50:53 +1100994 ? SkEncodedInfo::kBGRA_Color
995 : SkEncodedInfo::kRGBA_Color;
996
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400997 // In Skia's API, the alpha we calculate here and return is only for the
998 // first frame.
999 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
1000 : SkEncodedInfo::kBinary_Alpha;
1001
Nigel Tao490e6472019-02-14 14:50:53 +11001002 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001003
1004 *result = SkCodec::kSuccess;
Nigel Tao027f89b2019-12-06 10:56:24 +11001005 return std::unique_ptr<SkCodec>(new SkWuffsCodec(std::move(encodedInfo), std::move(stream),
1006 std::move(decoder), std::move(workbuf_ptr),
1007 workbuf_len, imgcfg, iobuf));
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001008}