blob: 4fd2a0447f3733e7192fdcb74a8351577f435025 [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 }
116 while (true) {
117 status = decoder->decode_image_config(imgcfg, b);
118 if (status.repr == nullptr) {
119 break;
120 } else if (status.repr != wuffs_base__suspension__short_read) {
121 SkCodecPrintf("decode_image_config: %s", status.message());
122 return SkCodec::kErrorInInput;
123 } else if (!fill_buffer(b, s)) {
124 return SkCodec::kIncompleteInput;
125 }
126 }
Nigel Tao4f32a292019-11-13 15:41:55 +1100127
128 // A GIF image's natural color model is indexed color: 1 byte per pixel,
129 // indexing a 256-element palette.
130 //
131 // For Skia, we override that to decode to 4 bytes per pixel, BGRA or RGBA.
Nigel Taob54946b2020-06-18 23:36:27 +1000132 uint32_t pixfmt = WUFFS_BASE__PIXEL_FORMAT__INVALID;
Nigel Tao4f32a292019-11-13 15:41:55 +1100133 switch (kN32_SkColorType) {
134 case kBGRA_8888_SkColorType:
135 pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
136 break;
137 case kRGBA_8888_SkColorType:
138 pixfmt = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
139 break;
140 default:
141 return SkCodec::kInternalError;
142 }
143 if (imgcfg) {
144 imgcfg->pixcfg.set(pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, imgcfg->pixcfg.width(),
145 imgcfg->pixcfg.height());
146 }
147
148 return SkCodec::kSuccess;
149}
150
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400151// -------------------------------- Class definitions
152
153class SkWuffsCodec;
154
155class SkWuffsFrame final : public SkFrame {
156public:
157 SkWuffsFrame(wuffs_base__frame_config* fc);
158
159 SkCodec::FrameInfo frameInfo(bool fullyReceived) const;
160 uint64_t ioPosition() const;
161
162 // SkFrame overrides.
163 SkEncodedInfo::Alpha onReportedAlpha() const override;
164
165private:
166 uint64_t fIOPosition;
167 SkEncodedInfo::Alpha fReportedAlpha;
168
John Stiles7571f9e2020-09-02 22:42:33 -0400169 using INHERITED = SkFrame;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400170};
171
172// SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
173// SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
174// inherit from both SkCodec and SkFrameHolder, and Skia style discourages
175// multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
176class SkWuffsFrameHolder final : public SkFrameHolder {
177public:
178 SkWuffsFrameHolder() : INHERITED() {}
179
180 void init(SkWuffsCodec* codec, int width, int height);
181
182 // SkFrameHolder overrides.
183 const SkFrame* onGetFrame(int i) const override;
184
185private:
186 const SkWuffsCodec* fCodec;
187
John Stiles7571f9e2020-09-02 22:42:33 -0400188 using INHERITED = SkFrameHolder;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400189};
190
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400191class SkWuffsCodec final : public SkScalingCodec {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400192public:
193 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
194 std::unique_ptr<SkStream> stream,
195 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400196 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
197 size_t workbuf_len,
198 wuffs_base__image_config imgcfg,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400199 wuffs_base__io_buffer iobuf);
200
201 const SkWuffsFrame* frame(int i) const;
202
203private:
Nigel Tao2777cd32019-10-29 11:10:25 +1100204 // It is valid, in terms of the SkCodec API, to call SkCodec::getFrameCount
205 // while in an incremental decode (after onStartIncrementalDecode returns
206 // and before the rest of the image is decoded). Some Skia users expect
207 // getFrameCount to increase, and the SkStream to advance, when given more
208 // data.
209 //
210 // On the other hand, while in an incremental decode, the underlying Wuffs
211 // object is suspended in a coroutine. To keep its internal proof-of-safety
212 // invariants consistent, there's only two things you can safely do with a
213 // suspended Wuffs object: resume the coroutine, or reset all state (memset
214 // to zero and start again).
215 //
216 // The Wuffs API provides a limited, optional form of seeking, to the start
217 // of an animation frame's data, but does not provide arbitrary save and
218 // load of its internal state whilst in the middle of an animation frame.
219 //
220 // SkWuffsCodec therefore uses two Wuffs decoders: a primary decoder
221 // (kIncrDecode) to support startIncrementalDecode / incrementalDecode, and
222 // a secondary decoder (kFrameCount) to support getFrameCount. The two
223 // decoders' states can change independently.
224 //
225 // As of Wuffs version 0.2, both of these decoders have the same type. A
226 // future Wuffs version might let us use a different type for kFrameCount,
227 // one that is much lighter weight (in terms of memory requirements), as it
228 // doesn't have to handle decompressing pixel data.
229 enum WhichDecoder {
230 kIncrDecode,
231 kFrameCount,
232 kNumDecoders,
233 };
234
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400235 // SkCodec overrides.
236 SkEncodedImageFormat onGetEncodedFormat() const override;
237 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
238 const SkFrameHolder* getFrameHolder() const override;
239 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
240 void* dst,
241 size_t rowBytes,
242 const SkCodec::Options& options) override;
243 Result onIncrementalDecode(int* rowsDecoded) override;
244 int onGetFrameCount() override;
245 bool onGetFrameInfo(int, FrameInfo*) const override;
246 int onGetRepetitionCount() override;
247
Nigel Tao027f89b2019-12-06 10:56:24 +1100248 // Two separate implementations of onStartIncrementalDecode and
249 // onIncrementalDecode, named "one pass" and "two pass" decoding. One pass
250 // decoding writes directly from the Wuffs image decoder to the dst buffer
251 // (the dst argument to onStartIncrementalDecode). Two pass decoding first
252 // writes into an intermediate buffer, and then composites and transforms
253 // the intermediate buffer into the dst buffer.
254 //
255 // In the general case, we need the two pass decoder, because of Skia API
256 // features that Wuffs doesn't support (e.g. color correction, scaling,
257 // RGB565). But as an optimization, we use one pass decoding (it's faster
258 // and uses less memory) if applicable (see the assignment to
259 // fIncrDecOnePass that calculates when we can do so).
Nigel Taob54946b2020-06-18 23:36:27 +1000260 Result onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
261 uint8_t* dst,
262 size_t rowBytes,
263 const SkCodec::Options& options,
264 uint32_t pixelFormat,
265 size_t bytesPerPixel);
Nigel Tao027f89b2019-12-06 10:56:24 +1100266 Result onStartIncrementalDecodeTwoPass();
267 Result onIncrementalDecodeOnePass();
268 Result onIncrementalDecodeTwoPass();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400269
Nigel Tao027f89b2019-12-06 10:56:24 +1100270 void onGetFrameCountInternal();
271 Result seekFrame(WhichDecoder which, int frameIndex);
Nigel Tao2777cd32019-10-29 11:10:25 +1100272 Result resetDecoder(WhichDecoder which);
273 const char* decodeFrameConfig(WhichDecoder which);
274 const char* decodeFrame(WhichDecoder which);
275 void updateNumFullyReceivedFrames(WhichDecoder which);
276
277 SkWuffsFrameHolder fFrameHolder;
278 std::unique_ptr<SkStream> fStream;
Nigel Tao2777cd32019-10-29 11:10:25 +1100279 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
280 size_t fWorkbufLen;
281
282 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoders[WhichDecoder::kNumDecoders];
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400283
284 const uint64_t fFirstFrameIOPosition;
Nigel Tao2777cd32019-10-29 11:10:25 +1100285 wuffs_base__frame_config fFrameConfigs[WhichDecoder::kNumDecoders];
Nigel Tao027f89b2019-12-06 10:56:24 +1100286 wuffs_base__pixel_config fPixelConfig;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400287 wuffs_base__pixel_buffer fPixelBuffer;
288 wuffs_base__io_buffer fIOBuffer;
289
290 // Incremental decoding state.
Nigel Taobf4605f2020-09-28 21:53:07 +1000291 uint8_t* fIncrDecDst;
292 uint64_t fIncrDecReaderIOPosition;
293 size_t fIncrDecRowBytes;
294 wuffs_base__pixel_blend fIncrDecPixelBlend;
295 bool fIncrDecOnePass;
296 bool fFirstCallToIncrementalDecode;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400297
Nigel Tao027f89b2019-12-06 10:56:24 +1100298 // Lazily allocated intermediate pixel buffer, for two pass decoding.
299 std::unique_ptr<uint8_t, decltype(&sk_free)> fTwoPassPixbufPtr;
300 size_t fTwoPassPixbufLen;
301
Nigel Tao3876a9f2019-11-14 09:04:45 +1100302 uint64_t fFrameCountReaderIOPosition;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400303 uint64_t fNumFullyReceivedFrames;
304 std::vector<SkWuffsFrame> fFrames;
305 bool fFramesComplete;
306
Nigel Tao2777cd32019-10-29 11:10:25 +1100307 // If calling an fDecoders[which] method returns an incomplete status, then
308 // fDecoders[which] is suspended in a coroutine (i.e. waiting on I/O or
309 // halted on a non-recoverable error). To keep its internal proof-of-safety
310 // invariants consistent, there's only two things you can safely do with a
311 // suspended Wuffs object: resume the coroutine, or reset all state (memset
312 // to zero and start again).
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400313 //
Nigel Tao2777cd32019-10-29 11:10:25 +1100314 // If fDecoderIsSuspended[which], and we aren't sure that we're going to
315 // resume the coroutine, then we will need to call this->resetDecoder
316 // before calling other fDecoders[which] methods.
317 bool fDecoderIsSuspended[WhichDecoder::kNumDecoders];
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400318
319 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
320
John Stiles7571f9e2020-09-02 22:42:33 -0400321 using INHERITED = SkScalingCodec;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400322};
323
324// -------------------------------- SkWuffsFrame implementation
325
326SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
327 : INHERITED(fc->index()),
328 fIOPosition(fc->io_position()),
Nigel Taob54946b2020-06-18 23:36:27 +1000329 fReportedAlpha(fc->opaque_within_bounds() ? SkEncodedInfo::kOpaque_Alpha
Nigel Tao5cfa7192020-08-17 21:14:13 +1000330 : SkEncodedInfo::kUnpremul_Alpha) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400331 wuffs_base__rect_ie_u32 r = fc->bounds();
332 this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
333 this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
334 this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
Nigel Taob54946b2020-06-18 23:36:27 +1000335 this->setBlend(fc->overwrite_instead_of_blend() ? SkCodecAnimation::Blend::kBG
336 : SkCodecAnimation::Blend::kPriorFrame);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400337}
338
339SkCodec::FrameInfo SkWuffsFrame::frameInfo(bool fullyReceived) const {
Nigel Taoef40e332019-04-05 10:28:40 +1100340 SkCodec::FrameInfo ret;
341 ret.fRequiredFrame = getRequiredFrame();
342 ret.fDuration = getDuration();
343 ret.fFullyReceived = fullyReceived;
344 ret.fAlphaType = hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
345 ret.fDisposalMethod = getDisposalMethod();
346 return ret;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400347}
348
349uint64_t SkWuffsFrame::ioPosition() const {
350 return fIOPosition;
351}
352
353SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
354 return fReportedAlpha;
355}
356
357// -------------------------------- SkWuffsFrameHolder implementation
358
359void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
360 fCodec = codec;
361 // Initialize SkFrameHolder's (the superclass) fields.
362 fScreenWidth = width;
363 fScreenHeight = height;
364}
365
366const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
367 return fCodec->frame(i);
368};
369
370// -------------------------------- SkWuffsCodec implementation
371
372SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
373 std::unique_ptr<SkStream> stream,
374 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400375 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
376 size_t workbuf_len,
377 wuffs_base__image_config imgcfg,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400378 wuffs_base__io_buffer iobuf)
379 : INHERITED(std::move(encodedInfo),
380 skcms_PixelFormat_RGBA_8888,
381 // Pass a nullptr SkStream to the SkCodec constructor. We
382 // manage the stream ourselves, as the default SkCodec behavior
383 // is too trigger-happy on rewinding the stream.
384 nullptr),
Nigel Tao0185b952018-11-08 10:47:24 +1100385 fFrameHolder(),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400386 fStream(std::move(stream)),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400387 fWorkbufPtr(std::move(workbuf_ptr)),
388 fWorkbufLen(workbuf_len),
Nigel Tao2777cd32019-10-29 11:10:25 +1100389 fDecoders{
390 std::move(dec),
391 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)>(nullptr, sk_free),
392 },
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400393 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
Nigel Tao2777cd32019-10-29 11:10:25 +1100394 fFrameConfigs{
395 wuffs_base__null_frame_config(),
396 wuffs_base__null_frame_config(),
397 },
Nigel Tao027f89b2019-12-06 10:56:24 +1100398 fPixelConfig(imgcfg.pixcfg),
399 fPixelBuffer(wuffs_base__null_pixel_buffer()),
Nigel Tao96c10a02019-09-25 11:08:42 +1000400 fIOBuffer(wuffs_base__empty_io_buffer()),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400401 fIncrDecDst(nullptr),
Nigel Tao2777cd32019-10-29 11:10:25 +1100402 fIncrDecReaderIOPosition(0),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400403 fIncrDecRowBytes(0),
Nigel Taobf4605f2020-09-28 21:53:07 +1000404 fIncrDecPixelBlend(WUFFS_BASE__PIXEL_BLEND__SRC),
Nigel Tao027f89b2019-12-06 10:56:24 +1100405 fIncrDecOnePass(false),
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400406 fFirstCallToIncrementalDecode(false),
Nigel Tao027f89b2019-12-06 10:56:24 +1100407 fTwoPassPixbufPtr(nullptr, &sk_free),
408 fTwoPassPixbufLen(0),
Nigel Tao3876a9f2019-11-14 09:04:45 +1100409 fFrameCountReaderIOPosition(0),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400410 fNumFullyReceivedFrames(0),
411 fFramesComplete(false),
Nigel Tao2777cd32019-10-29 11:10:25 +1100412 fDecoderIsSuspended{
413 false,
414 false,
415 } {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400416 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
417
418 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
419 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
420 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
421 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
422 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
Nigel Tao48aa2212019-03-09 14:59:11 +1100423 fIOBuffer.data = wuffs_base__make_slice_u8(fBuffer, SK_WUFFS_CODEC_BUFFER_SIZE);
424 fIOBuffer.meta = iobuf.meta;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400425}
426
427const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
428 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
429 return &fFrames[i];
430 }
431 return nullptr;
432}
433
434SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
435 return SkEncodedImageFormat::kGIF;
436}
437
438SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
439 void* dst,
440 size_t rowBytes,
441 const Options& options,
442 int* rowsDecoded) {
443 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
444 if (result != kSuccess) {
445 return result;
446 }
447 return this->onIncrementalDecode(rowsDecoded);
448}
449
450const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
451 return &fFrameHolder;
452}
453
454SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
455 void* dst,
456 size_t rowBytes,
457 const SkCodec::Options& options) {
Nigel Tao1f1cd1f2019-06-22 17:35:38 +1000458 if (!dst) {
459 return SkCodec::kInvalidParameters;
460 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400461 if (options.fSubset) {
462 return SkCodec::kUnimplemented;
463 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100464 SkCodec::Result result = this->seekFrame(WhichDecoder::kIncrDecode, options.fFrameIndex);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400465 if (result != SkCodec::kSuccess) {
466 return result;
467 }
468
Nigel Tao2777cd32019-10-29 11:10:25 +1100469 const char* status = this->decodeFrameConfig(WhichDecoder::kIncrDecode);
Nigel Taob7a1b512019-02-10 12:19:50 +1100470 if (status == wuffs_base__suspension__short_read) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500471 return SkCodec::kIncompleteInput;
Nigel Taob7a1b512019-02-10 12:19:50 +1100472 } else if (status != nullptr) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500473 SkCodecPrintf("decodeFrameConfig: %s", status);
474 return SkCodec::kErrorInInput;
475 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100476
Nigel Taob54946b2020-06-18 23:36:27 +1000477 uint32_t pixelFormat = WUFFS_BASE__PIXEL_FORMAT__INVALID;
Nigel Tao5cfa7192020-08-17 21:14:13 +1000478 size_t bytesPerPixel = 0;
Nigel Tao027f89b2019-12-06 10:56:24 +1100479
480 switch (dstInfo.colorType()) {
Nigel Taof933e4f2020-10-29 09:42:11 +1100481 case kRGB_565_SkColorType:
482 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGR_565;
483 bytesPerPixel = 2;
484 break;
Nigel Tao027f89b2019-12-06 10:56:24 +1100485 case kBGRA_8888_SkColorType:
486 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
487 bytesPerPixel = 4;
488 break;
489 case kRGBA_8888_SkColorType:
490 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
491 bytesPerPixel = 4;
492 break;
493 default:
494 break;
495 }
496
497 // We can use "one pass" decoding if we have a Skia pixel format that Wuffs
498 // supports...
Nigel Taobf4605f2020-09-28 21:53:07 +1000499 fIncrDecOnePass = (pixelFormat != WUFFS_BASE__PIXEL_FORMAT__INVALID) &&
500 // ...and no color profile (as Wuffs does not support them)...
501 (!getEncodedInfo().profile()) &&
502 // ...and we use the identity transform (as Wuffs does
503 // not support scaling).
504 (this->dimensions() == dstInfo.dimensions());
Nigel Tao027f89b2019-12-06 10:56:24 +1100505
506 result = fIncrDecOnePass ? this->onStartIncrementalDecodeOnePass(
507 dstInfo, static_cast<uint8_t*>(dst), rowBytes, options,
508 pixelFormat, bytesPerPixel)
509 : this->onStartIncrementalDecodeTwoPass();
510 if (result != SkCodec::kSuccess) {
511 return result;
512 }
513
514 fIncrDecDst = static_cast<uint8_t*>(dst);
515 fIncrDecReaderIOPosition = fIOBuffer.reader_io_position();
516 fIncrDecRowBytes = rowBytes;
517 fFirstCallToIncrementalDecode = true;
518 return SkCodec::kSuccess;
519}
520
Nigel Taob54946b2020-06-18 23:36:27 +1000521SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
522 uint8_t* dst,
523 size_t rowBytes,
524 const SkCodec::Options& options,
525 uint32_t pixelFormat,
Nigel Tao027f89b2019-12-06 10:56:24 +1100526 size_t bytesPerPixel) {
527 wuffs_base__pixel_config pixelConfig;
528 pixelConfig.set(pixelFormat, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, dstInfo.width(),
529 dstInfo.height());
530
531 wuffs_base__table_u8 table;
532 table.ptr = dst;
533 table.width = static_cast<size_t>(dstInfo.width()) * bytesPerPixel;
534 table.height = dstInfo.height();
535 table.stride = rowBytes;
536
Nigel Taob54946b2020-06-18 23:36:27 +1000537 wuffs_base__status status = fPixelBuffer.set_from_table(&pixelConfig, table);
Nigel Taob54946b2020-06-18 23:36:27 +1000538 if (status.repr != nullptr) {
539 SkCodecPrintf("set_from_table: %s", status.message());
540 return SkCodec::kInternalError;
541 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100542
Nigel Taobf4605f2020-09-28 21:53:07 +1000543 // SRC is usually faster than SRC_OVER, but for a dependent frame, dst is
544 // assumed to hold the previous frame's pixels (after processing the
545 // DisposalMethod). For one-pass decoding, we therefore use SRC_OVER.
546 if ((options.fFrameIndex != 0) &&
547 (this->frame(options.fFrameIndex)->getRequiredFrame() != SkCodec::kNoFrame)) {
548 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC_OVER;
549 } else {
550 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
551 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
552 }
553
Nigel Tao027f89b2019-12-06 10:56:24 +1100554 return SkCodec::kSuccess;
555}
556
557SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeTwoPass() {
558 // Either re-use the previously allocated "two pass" pixel buffer (and
559 // memset to zero), or allocate (and zero initialize) a new one.
560 bool already_zeroed = false;
561
562 if (!fTwoPassPixbufPtr) {
563 uint64_t pixbuf_len = fPixelConfig.pixbuf_len();
564 void* pixbuf_ptr_raw = (pixbuf_len <= SIZE_MAX)
Nigel Tao5cfa7192020-08-17 21:14:13 +1000565 ? sk_malloc_flags(pixbuf_len, SK_MALLOC_ZERO_INITIALIZE)
566 : nullptr;
Nigel Tao027f89b2019-12-06 10:56:24 +1100567 if (!pixbuf_ptr_raw) {
568 return SkCodec::kInternalError;
569 }
570 fTwoPassPixbufPtr.reset(reinterpret_cast<uint8_t*>(pixbuf_ptr_raw));
571 fTwoPassPixbufLen = SkToSizeT(pixbuf_len);
572 already_zeroed = true;
573 }
574
Nigel Taob54946b2020-06-18 23:36:27 +1000575 wuffs_base__status status = fPixelBuffer.set_from_slice(
Nigel Tao027f89b2019-12-06 10:56:24 +1100576 &fPixelConfig, wuffs_base__make_slice_u8(fTwoPassPixbufPtr.get(), fTwoPassPixbufLen));
Nigel Taob54946b2020-06-18 23:36:27 +1000577 if (status.repr != nullptr) {
578 SkCodecPrintf("set_from_slice: %s", status.message());
579 return SkCodec::kInternalError;
580 }
Nigel Tao027f89b2019-12-06 10:56:24 +1100581
582 if (!already_zeroed) {
Nigel Taob54946b2020-06-18 23:36:27 +1000583 uint32_t src_bits_per_pixel = fPixelConfig.pixel_format().bits_per_pixel();
Nigel Tao027f89b2019-12-06 10:56:24 +1100584 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
585 return SkCodec::kInternalError;
586 }
587 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
588
Nigel Tao39da10b2019-11-15 15:27:44 +1100589 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
590 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Nigel Tao6ec1b392019-11-20 12:28:50 +1100591
592 uint8_t* ptr = pixels.ptr + (frame_rect.min_incl_y * pixels.stride) +
593 (frame_rect.min_incl_x * src_bytes_per_pixel);
594 size_t len = frame_rect.width() * src_bytes_per_pixel;
595
596 // As an optimization, issue a single sk_bzero call, if possible.
597 // Otherwise, zero out each row separately.
598 if ((len == pixels.stride) && (frame_rect.min_incl_y < frame_rect.max_excl_y)) {
599 sk_bzero(ptr, len * (frame_rect.max_excl_y - frame_rect.min_incl_y));
600 } else {
601 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
602 sk_bzero(ptr, len);
603 ptr += pixels.stride;
604 }
Nigel Tao39da10b2019-11-15 15:27:44 +1100605 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100606 }
607
Nigel Taobf4605f2020-09-28 21:53:07 +1000608 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
Nigel Taob7a1b512019-02-10 12:19:50 +1100609 return SkCodec::kSuccess;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400610}
611
612SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
613 if (!fIncrDecDst) {
614 return SkCodec::kInternalError;
615 }
616
Nigel Tao2777cd32019-10-29 11:10:25 +1100617 // If multiple SkCodec::incrementalDecode calls are made consecutively (or
618 // if SkCodec::incrementalDecode is called immediately after
619 // SkCodec::startIncrementalDecode), then this seek should be a no-op.
620 // However, it is possible to interleave SkCodec::getFrameCount calls in
621 // between SkCodec::incrementalDecode calls, and those other calls may
622 // advance the stream. This seek restores the stream to where the last
623 // SkCodec::startIncrementalDecode or SkCodec::incrementalDecode stopped.
624 if (!seek_buffer(&fIOBuffer, fStream.get(), fIncrDecReaderIOPosition)) {
625 return SkCodec::kInternalError;
626 }
627
Nigel Tao027f89b2019-12-06 10:56:24 +1100628 if (rowsDecoded) {
629 *rowsDecoded = dstInfo().height();
630 }
631
632 SkCodec::Result result =
633 fIncrDecOnePass ? this->onIncrementalDecodeOnePass() : this->onIncrementalDecodeTwoPass();
Nigel Tao2777cd32019-10-29 11:10:25 +1100634 if (result == SkCodec::kSuccess) {
635 fIncrDecDst = nullptr;
636 fIncrDecReaderIOPosition = 0;
637 fIncrDecRowBytes = 0;
Nigel Taobf4605f2020-09-28 21:53:07 +1000638 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
Nigel Tao027f89b2019-12-06 10:56:24 +1100639 fIncrDecOnePass = false;
Nigel Tao2777cd32019-10-29 11:10:25 +1100640 } else {
641 fIncrDecReaderIOPosition = fIOBuffer.reader_io_position();
642 }
643 return result;
644}
645
Nigel Tao027f89b2019-12-06 10:56:24 +1100646SkCodec::Result SkWuffsCodec::onIncrementalDecodeOnePass() {
647 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
648 if (status != nullptr) {
649 if (status == wuffs_base__suspension__short_read) {
650 return SkCodec::kIncompleteInput;
651 } else {
652 SkCodecPrintf("decodeFrame: %s", status);
653 return SkCodec::kErrorInInput;
654 }
655 }
656 return SkCodec::kSuccess;
657}
658
659SkCodec::Result SkWuffsCodec::onIncrementalDecodeTwoPass() {
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500660 SkCodec::Result result = SkCodec::kSuccess;
Nigel Tao2777cd32019-10-29 11:10:25 +1100661 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
662 bool independent;
663 SkAlphaType alphaType;
664 const int index = options().fFrameIndex;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400665 if (index == 0) {
666 independent = true;
667 alphaType = to_alpha_type(getEncodedInfo().opaque());
668 } else {
669 const SkWuffsFrame* f = this->frame(index);
670 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
671 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
672 }
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500673 if (status != nullptr) {
674 if (status == wuffs_base__suspension__short_read) {
675 result = SkCodec::kIncompleteInput;
676 } else {
677 SkCodecPrintf("decodeFrame: %s", status);
678 result = SkCodec::kErrorInInput;
679 }
680
681 if (!independent) {
682 // For a dependent frame, we cannot blend the partial result, since
683 // that will overwrite the contribution from prior frames.
684 return result;
685 }
686 }
687
Nigel Taob54946b2020-06-18 23:36:27 +1000688 uint32_t src_bits_per_pixel = fPixelBuffer.pixcfg.pixel_format().bits_per_pixel();
Nigel Tao490e6472019-02-14 14:50:53 +1100689 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
690 return SkCodec::kInternalError;
691 }
692 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
693
Nigel Tao2777cd32019-10-29 11:10:25 +1100694 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400695 if (fFirstCallToIncrementalDecode) {
Nigel Tao490e6472019-02-14 14:50:53 +1100696 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
697 return SkCodec::kInternalError;
698 }
699
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400700 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
701 frame_rect.max_excl_x, frame_rect.max_excl_y);
702
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500703 // If the frame rect does not fill the output, ensure that those pixels are not
Nigel Taob7a1b512019-02-10 12:19:50 +1100704 // left uninitialized.
Leon Scroggins III44076362019-02-15 13:56:44 -0500705 if (independent && (bounds != this->bounds() || result != kSuccess)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100706 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes, options().fZeroInitialized);
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500707 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400708 fFirstCallToIncrementalDecode = false;
709 } else {
710 // Existing clients intend to only show frames beyond the first if they
711 // are complete (based on FrameInfo::fFullyReceived), since it might
712 // look jarring to draw a partial frame over an existing frame. If they
713 // changed their behavior and expected to continue decoding a partial
714 // frame after the first one, we'll need to update our blending code.
715 // Otherwise, if the frame were interlaced and not independent, the
716 // second pass may have an overlapping dirty_rect with the first,
717 // resulting in blending with the first pass.
718 SkASSERT(index == 0);
Nigel Tao9859ef82019-02-13 13:20:02 +1100719 }
Nigel Tao0185b952018-11-08 10:47:24 +1100720
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500721 // If the frame's dirty rect is empty, no need to swizzle.
Nigel Tao2777cd32019-10-29 11:10:25 +1100722 wuffs_base__rect_ie_u32 dirty_rect = fDecoders[WhichDecoder::kIncrDecode]->frame_dirty_rect();
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500723 if (!dirty_rect.is_empty()) {
Nigel Tao490e6472019-02-14 14:50:53 +1100724 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500725
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400726 // The Wuffs model is that the dst buffer is the image, not the frame.
727 // The expectation is that you allocate the buffer once, but re-use it
728 // for the N frames, regardless of each frame's top-left co-ordinate.
729 //
730 // To get from the start (in the X-direction) of the image to the start
731 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
Nigel Tao2777cd32019-10-29 11:10:25 +1100732 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride) +
733 (dirty_rect.min_incl_x * src_bytes_per_pixel);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500734
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400735 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
736 // used for other formats that might have one, we will need to transform from profiles that
737 // do not have corresponding SkColorSpaces.
738 SkASSERT(!getEncodedInfo().profile());
739
Nigel Tao2777cd32019-10-29 11:10:25 +1100740 auto srcInfo =
741 getInfo().makeWH(dirty_rect.width(), dirty_rect.height()).makeAlphaType(alphaType);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400742 SkBitmap src;
743 src.installPixels(srcInfo, s, pixels.stride);
744 SkPaint paint;
745 if (independent) {
746 paint.setBlendMode(SkBlendMode::kSrc);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500747 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400748
749 SkDraw draw;
750 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
Nigel Tao5cfa7192020-08-17 21:14:13 +1000751 SkMatrix matrix = SkMatrix::MakeRectToRect(SkRect::Make(this->dimensions()),
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400752 SkRect::Make(this->dstInfo().dimensions()),
753 SkMatrix::kFill_ScaleToFit);
Brian Osman9aaec362020-05-08 14:54:37 -0400754 SkSimpleMatrixProvider matrixProvider(matrix);
755 draw.fMatrixProvider = &matrixProvider;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400756 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
757 draw.fRC = &rc;
758
Mike Reed1f607332020-05-21 12:11:27 -0400759 SkMatrix translate = SkMatrix::Translate(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400760 draw.drawBitmap(src, translate, nullptr, paint);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500761 }
Nigel Taof0148c42019-12-08 19:12:13 +1100762
763 if (result == SkCodec::kSuccess) {
764 // On success, we are done using the "two pass" pixel buffer for this
765 // frame. We have the option of releasing its memory, but there is a
766 // trade-off. If decoding a subsequent frame will also need "two pass"
767 // decoding, it would have to re-allocate the buffer instead of just
768 // re-using it. On the other hand, if there is no subsequent frame, and
769 // the SkWuffsCodec object isn't deleted soon, then we are holding
770 // megabytes of memory longer than we need to.
771 //
772 // For example, when the Chromium web browser decodes the <img> tags in
773 // a HTML page, the SkCodec object can live until navigating away from
774 // the page, which can be much longer than when the pixels are fully
775 // decoded, especially for a still (non-animated) image. Even for
776 // looping animations, caching the decoded frames (at the higher HTML
777 // renderer layer) may mean that each frame is only decoded once (at
778 // the lower SkCodec layer), in sequence.
779 //
780 // The heuristic we use here is to free the memory if we have decoded
781 // the last frame of the animation (or, for still images, the only
782 // frame). The output of the next decode request (if any) should be the
783 // same either way, but the steady state memory use should hopefully be
784 // lower than always keeping the fTwoPassPixbufPtr buffer up until the
785 // SkWuffsCodec destructor runs.
786 //
787 // This only applies to "two pass" decoding. "One pass" decoding does
788 // not allocate, free or otherwise use fTwoPassPixbufPtr.
789 if (fFramesComplete && (static_cast<size_t>(options().fFrameIndex) == fFrames.size() - 1)) {
790 fTwoPassPixbufPtr.reset(nullptr);
791 fTwoPassPixbufLen = 0;
792 }
793 }
794
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400795 return result;
796}
797
798int SkWuffsCodec::onGetFrameCount() {
Nigel Tao3876a9f2019-11-14 09:04:45 +1100799 if (!fFramesComplete && seek_buffer(&fIOBuffer, fStream.get(), fFrameCountReaderIOPosition)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100800 this->onGetFrameCountInternal();
Nigel Tao3876a9f2019-11-14 09:04:45 +1100801 fFrameCountReaderIOPosition =
802 fDecoders[WhichDecoder::kFrameCount] ? fIOBuffer.reader_io_position() : 0;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400803 }
804 return fFrames.size();
805}
806
Nigel Tao2777cd32019-10-29 11:10:25 +1100807void SkWuffsCodec::onGetFrameCountInternal() {
Nigel Tao2777cd32019-10-29 11:10:25 +1100808 if (!fDecoders[WhichDecoder::kFrameCount]) {
809 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
810 if (!decoder_raw) {
811 return;
812 }
813 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
814 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
Nigel Tao3876a9f2019-11-14 09:04:45 +1100815 reset_and_decode_image_config(decoder.get(), nullptr, &fIOBuffer, fStream.get());
Nigel Tao2777cd32019-10-29 11:10:25 +1100816 fDecoders[WhichDecoder::kFrameCount] = std::move(decoder);
817 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400818
819 // Iterate through the frames, converting from Wuffs'
820 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
Nigel Tao3876a9f2019-11-14 09:04:45 +1100821 while (true) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100822 const char* status = this->decodeFrameConfig(WhichDecoder::kFrameCount);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400823 if (status == nullptr) {
824 // No-op.
Nigel Taob54946b2020-06-18 23:36:27 +1000825 } else if (status == wuffs_base__note__end_of_data) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400826 break;
827 } else {
828 return;
829 }
830
Nigel Tao3876a9f2019-11-14 09:04:45 +1100831 uint64_t i = fDecoders[WhichDecoder::kFrameCount]->num_decoded_frame_configs();
832 if (i > INT_MAX) {
833 break;
834 }
835 if ((i == 0) || (static_cast<size_t>(i - 1) != fFrames.size())) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400836 continue;
837 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100838 fFrames.emplace_back(&fFrameConfigs[WhichDecoder::kFrameCount]);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400839 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
840 fFrameHolder.setAlphaAndRequiredFrame(f);
841 }
842
843 fFramesComplete = true;
Nigel Tao5b271462019-11-12 21:02:20 +1100844
845 // We've seen the end of the animation. There'll be no more frames, so we
846 // no longer need the kFrameCount decoder. Releasing it earlier than the
847 // SkWuffsCodec destructor might help peak memory use.
848 fDecoders[WhichDecoder::kFrameCount].reset(nullptr);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400849}
850
Nigel Taocdc92382019-10-31 08:36:53 +1100851bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
852 const SkWuffsFrame* f = this->frame(i);
853 if (!f) {
854 return false;
855 }
856 if (frameInfo) {
857 *frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
858 }
859 return true;
860}
861
862int SkWuffsCodec::onGetRepetitionCount() {
863 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
864 // number is how many times to play the loop. Skia's int number is how many
865 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
866 // kRepetitionCountInfinite respectively to mean loop forever.
867 uint32_t n = fDecoders[WhichDecoder::kIncrDecode]->num_animation_loops();
868 if (n == 0) {
869 return SkCodec::kRepetitionCountInfinite;
870 }
871 n--;
872 return n < INT_MAX ? n : INT_MAX;
873}
874
Nigel Tao2777cd32019-10-29 11:10:25 +1100875SkCodec::Result SkWuffsCodec::seekFrame(WhichDecoder which, int frameIndex) {
876 if (fDecoderIsSuspended[which]) {
877 SkCodec::Result res = this->resetDecoder(which);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400878 if (res != SkCodec::kSuccess) {
879 return res;
880 }
881 }
882
883 uint64_t pos = 0;
884 if (frameIndex < 0) {
885 return SkCodec::kInternalError;
886 } else if (frameIndex == 0) {
887 pos = fFirstFrameIOPosition;
888 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
889 pos = fFrames[frameIndex].ioPosition();
890 } else {
891 return SkCodec::kInternalError;
892 }
893
894 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
895 return SkCodec::kInternalError;
896 }
Nigel Taob54946b2020-06-18 23:36:27 +1000897 wuffs_base__status status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100898 fDecoders[which]->restart_frame(frameIndex, fIOBuffer.reader_io_position());
Nigel Taob54946b2020-06-18 23:36:27 +1000899 if (status.repr != nullptr) {
900 return SkCodec::kInternalError;
901 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400902 return SkCodec::kSuccess;
903}
904
Nigel Tao2777cd32019-10-29 11:10:25 +1100905SkCodec::Result SkWuffsCodec::resetDecoder(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400906 if (!fStream->rewind()) {
907 return SkCodec::kInternalError;
908 }
Nigel Tao96c10a02019-09-25 11:08:42 +1000909 fIOBuffer.meta = wuffs_base__empty_io_buffer_meta();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400910
911 SkCodec::Result result =
Nigel Tao2777cd32019-10-29 11:10:25 +1100912 reset_and_decode_image_config(fDecoders[which].get(), nullptr, &fIOBuffer, fStream.get());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400913 if (result == SkCodec::kIncompleteInput) {
914 return SkCodec::kInternalError;
915 } else if (result != SkCodec::kSuccess) {
916 return result;
917 }
918
Nigel Tao2777cd32019-10-29 11:10:25 +1100919 fDecoderIsSuspended[which] = false;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400920 return SkCodec::kSuccess;
921}
922
Nigel Tao2777cd32019-10-29 11:10:25 +1100923const char* SkWuffsCodec::decodeFrameConfig(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400924 while (true) {
Nigel Taob54946b2020-06-18 23:36:27 +1000925 wuffs_base__status status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100926 fDecoders[which]->decode_frame_config(&fFrameConfigs[which], &fIOBuffer);
Nigel Taob54946b2020-06-18 23:36:27 +1000927 if ((status.repr == wuffs_base__suspension__short_read) &&
928 fill_buffer(&fIOBuffer, fStream.get())) {
929 continue;
930 }
931 fDecoderIsSuspended[which] = !status.is_complete();
932 this->updateNumFullyReceivedFrames(which);
933 return status.repr;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400934 }
935}
936
Nigel Tao2777cd32019-10-29 11:10:25 +1100937const char* SkWuffsCodec::decodeFrame(WhichDecoder which) {
938 while (true) {
Nigel Taob54946b2020-06-18 23:36:27 +1000939 wuffs_base__status status = fDecoders[which]->decode_frame(
Nigel Taobf4605f2020-09-28 21:53:07 +1000940 &fPixelBuffer, &fIOBuffer, fIncrDecPixelBlend,
Nigel Tao09e541c2020-10-29 16:18:36 +1100941 wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen), nullptr);
Nigel Taob54946b2020-06-18 23:36:27 +1000942 if ((status.repr == wuffs_base__suspension__short_read) &&
943 fill_buffer(&fIOBuffer, fStream.get())) {
944 continue;
945 }
946 fDecoderIsSuspended[which] = !status.is_complete();
947 this->updateNumFullyReceivedFrames(which);
948 return status.repr;
Nigel Tao2777cd32019-10-29 11:10:25 +1100949 }
950}
951
952void SkWuffsCodec::updateNumFullyReceivedFrames(WhichDecoder which) {
Nigel Tao6af1edc2019-01-19 15:12:39 +1100953 // num_decoded_frames's return value, n, can change over time, both up and
954 // down, as we seek back and forth in the underlying stream.
955 // fNumFullyReceivedFrames is the highest n we've seen.
Nigel Tao2777cd32019-10-29 11:10:25 +1100956 uint64_t n = fDecoders[which]->num_decoded_frames();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400957 if (fNumFullyReceivedFrames < n) {
958 fNumFullyReceivedFrames = n;
959 }
960}
961
962// -------------------------------- SkWuffsCodec.h functions
963
964bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
965 constexpr const char* gif_ptr = "GIF8";
966 constexpr size_t gif_len = 4;
967 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
968}
969
970std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
971 SkCodec::Result* result) {
Nigel Tao48aa2212019-03-09 14:59:11 +1100972 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
973 wuffs_base__io_buffer iobuf =
974 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
Nigel Tao96c10a02019-09-25 11:08:42 +1000975 wuffs_base__empty_io_buffer_meta());
Nigel Tao48aa2212019-03-09 14:59:11 +1100976 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400977
978 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
979 // the wuffs_base__etc types, the sizeof a file format specific type like
980 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
981 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
982 // to an opaque type: a private implementation detail. The API is always
983 // "set_foo(p, etc)" and not "p->foo = etc".
984 //
985 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
986 //
987 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
988 // the struct at compile time). Instead, we use sk_malloc_canfail, with
989 // sizeof__wuffs_gif__decoder returning the appropriate value for the
990 // (statically or dynamically) linked version of the Wuffs library.
991 //
992 // As a C (not C++) library, none of the Wuffs types have constructors or
993 // destructors.
994 //
995 // In RAII style, we can still use std::unique_ptr with these pointers, but
996 // we pair the pointer with sk_free instead of C++'s delete.
997 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
998 if (!decoder_raw) {
999 *result = SkCodec::kInternalError;
1000 return nullptr;
1001 }
1002 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
1003 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
1004
1005 SkCodec::Result reset_result =
1006 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
1007 if (reset_result != SkCodec::kSuccess) {
1008 *result = reset_result;
1009 return nullptr;
1010 }
1011
1012 uint32_t width = imgcfg.pixcfg.width();
1013 uint32_t height = imgcfg.pixcfg.height();
1014 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
1015 *result = SkCodec::kInvalidInput;
1016 return nullptr;
1017 }
1018
Nigel Tao6af1edc2019-01-19 15:12:39 +11001019 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
Nigel Tao22e86242019-01-26 16:04:01 +11001020 void* workbuf_ptr_raw = nullptr;
1021 if (workbuf_len) {
1022 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
1023 if (!workbuf_ptr_raw) {
1024 *result = SkCodec::kInternalError;
1025 return nullptr;
1026 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001027 }
1028 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
1029 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
1030
Nigel Tao490e6472019-02-14 14:50:53 +11001031 SkEncodedInfo::Color color =
Nigel Taob54946b2020-06-18 23:36:27 +10001032 (imgcfg.pixcfg.pixel_format().repr == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
Nigel Tao490e6472019-02-14 14:50:53 +11001033 ? SkEncodedInfo::kBGRA_Color
1034 : SkEncodedInfo::kRGBA_Color;
1035
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001036 // In Skia's API, the alpha we calculate here and return is only for the
1037 // first frame.
1038 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
1039 : SkEncodedInfo::kBinary_Alpha;
1040
Nigel Tao490e6472019-02-14 14:50:53 +11001041 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001042
1043 *result = SkCodec::kSuccess;
Nigel Tao027f89b2019-12-06 10:56:24 +11001044 return std::unique_ptr<SkCodec>(new SkWuffsCodec(std::move(encodedInfo), std::move(stream),
1045 std::move(decoder), std::move(workbuf_ptr),
1046 workbuf_len, imgcfg, iobuf));
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001047}