blob: 7c966c3f09a06372aae245b37a47d03725f0ea13 [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 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400464 if (options.fFrameIndex > 0 && SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
465 return SkCodec::kInvalidConversion;
466 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100467 SkCodec::Result result = this->seekFrame(WhichDecoder::kIncrDecode, options.fFrameIndex);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400468 if (result != SkCodec::kSuccess) {
469 return result;
470 }
471
Nigel Tao2777cd32019-10-29 11:10:25 +1100472 const char* status = this->decodeFrameConfig(WhichDecoder::kIncrDecode);
Nigel Taob7a1b512019-02-10 12:19:50 +1100473 if (status == wuffs_base__suspension__short_read) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500474 return SkCodec::kIncompleteInput;
Nigel Taob7a1b512019-02-10 12:19:50 +1100475 } else if (status != nullptr) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500476 SkCodecPrintf("decodeFrameConfig: %s", status);
477 return SkCodec::kErrorInInput;
478 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100479
Nigel Taob54946b2020-06-18 23:36:27 +1000480 uint32_t pixelFormat = WUFFS_BASE__PIXEL_FORMAT__INVALID;
Nigel Tao5cfa7192020-08-17 21:14:13 +1000481 size_t bytesPerPixel = 0;
Nigel Tao027f89b2019-12-06 10:56:24 +1100482
483 switch (dstInfo.colorType()) {
484 case kBGRA_8888_SkColorType:
485 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
486 bytesPerPixel = 4;
487 break;
488 case kRGBA_8888_SkColorType:
489 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
490 bytesPerPixel = 4;
491 break;
492 default:
493 break;
494 }
495
496 // We can use "one pass" decoding if we have a Skia pixel format that Wuffs
497 // supports...
Nigel Taobf4605f2020-09-28 21:53:07 +1000498 fIncrDecOnePass = (pixelFormat != WUFFS_BASE__PIXEL_FORMAT__INVALID) &&
499 // ...and no color profile (as Wuffs does not support them)...
500 (!getEncodedInfo().profile()) &&
501 // ...and we use the identity transform (as Wuffs does
502 // not support scaling).
503 (this->dimensions() == dstInfo.dimensions());
Nigel Tao027f89b2019-12-06 10:56:24 +1100504
505 result = fIncrDecOnePass ? this->onStartIncrementalDecodeOnePass(
506 dstInfo, static_cast<uint8_t*>(dst), rowBytes, options,
507 pixelFormat, bytesPerPixel)
508 : this->onStartIncrementalDecodeTwoPass();
509 if (result != SkCodec::kSuccess) {
510 return result;
511 }
512
513 fIncrDecDst = static_cast<uint8_t*>(dst);
514 fIncrDecReaderIOPosition = fIOBuffer.reader_io_position();
515 fIncrDecRowBytes = rowBytes;
516 fFirstCallToIncrementalDecode = true;
517 return SkCodec::kSuccess;
518}
519
Nigel Taob54946b2020-06-18 23:36:27 +1000520SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
521 uint8_t* dst,
522 size_t rowBytes,
523 const SkCodec::Options& options,
524 uint32_t pixelFormat,
Nigel Tao027f89b2019-12-06 10:56:24 +1100525 size_t bytesPerPixel) {
526 wuffs_base__pixel_config pixelConfig;
527 pixelConfig.set(pixelFormat, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, dstInfo.width(),
528 dstInfo.height());
529
530 wuffs_base__table_u8 table;
531 table.ptr = dst;
532 table.width = static_cast<size_t>(dstInfo.width()) * bytesPerPixel;
533 table.height = dstInfo.height();
534 table.stride = rowBytes;
535
Nigel Taob54946b2020-06-18 23:36:27 +1000536 wuffs_base__status status = fPixelBuffer.set_from_table(&pixelConfig, table);
Nigel Taob54946b2020-06-18 23:36:27 +1000537 if (status.repr != nullptr) {
538 SkCodecPrintf("set_from_table: %s", status.message());
539 return SkCodec::kInternalError;
540 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100541
Nigel Taobf4605f2020-09-28 21:53:07 +1000542 // SRC is usually faster than SRC_OVER, but for a dependent frame, dst is
543 // assumed to hold the previous frame's pixels (after processing the
544 // DisposalMethod). For one-pass decoding, we therefore use SRC_OVER.
545 if ((options.fFrameIndex != 0) &&
546 (this->frame(options.fFrameIndex)->getRequiredFrame() != SkCodec::kNoFrame)) {
547 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC_OVER;
548 } else {
549 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
550 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
551 }
552
Nigel Tao027f89b2019-12-06 10:56:24 +1100553 return SkCodec::kSuccess;
554}
555
556SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeTwoPass() {
557 // Either re-use the previously allocated "two pass" pixel buffer (and
558 // memset to zero), or allocate (and zero initialize) a new one.
559 bool already_zeroed = false;
560
561 if (!fTwoPassPixbufPtr) {
562 uint64_t pixbuf_len = fPixelConfig.pixbuf_len();
563 void* pixbuf_ptr_raw = (pixbuf_len <= SIZE_MAX)
Nigel Tao5cfa7192020-08-17 21:14:13 +1000564 ? sk_malloc_flags(pixbuf_len, SK_MALLOC_ZERO_INITIALIZE)
565 : nullptr;
Nigel Tao027f89b2019-12-06 10:56:24 +1100566 if (!pixbuf_ptr_raw) {
567 return SkCodec::kInternalError;
568 }
569 fTwoPassPixbufPtr.reset(reinterpret_cast<uint8_t*>(pixbuf_ptr_raw));
570 fTwoPassPixbufLen = SkToSizeT(pixbuf_len);
571 already_zeroed = true;
572 }
573
Nigel Taob54946b2020-06-18 23:36:27 +1000574 wuffs_base__status status = fPixelBuffer.set_from_slice(
Nigel Tao027f89b2019-12-06 10:56:24 +1100575 &fPixelConfig, wuffs_base__make_slice_u8(fTwoPassPixbufPtr.get(), fTwoPassPixbufLen));
Nigel Taob54946b2020-06-18 23:36:27 +1000576 if (status.repr != nullptr) {
577 SkCodecPrintf("set_from_slice: %s", status.message());
578 return SkCodec::kInternalError;
579 }
Nigel Tao027f89b2019-12-06 10:56:24 +1100580
581 if (!already_zeroed) {
Nigel Taob54946b2020-06-18 23:36:27 +1000582 uint32_t src_bits_per_pixel = fPixelConfig.pixel_format().bits_per_pixel();
Nigel Tao027f89b2019-12-06 10:56:24 +1100583 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
584 return SkCodec::kInternalError;
585 }
586 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
587
Nigel Tao39da10b2019-11-15 15:27:44 +1100588 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
589 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Nigel Tao6ec1b392019-11-20 12:28:50 +1100590
591 uint8_t* ptr = pixels.ptr + (frame_rect.min_incl_y * pixels.stride) +
592 (frame_rect.min_incl_x * src_bytes_per_pixel);
593 size_t len = frame_rect.width() * src_bytes_per_pixel;
594
595 // As an optimization, issue a single sk_bzero call, if possible.
596 // Otherwise, zero out each row separately.
597 if ((len == pixels.stride) && (frame_rect.min_incl_y < frame_rect.max_excl_y)) {
598 sk_bzero(ptr, len * (frame_rect.max_excl_y - frame_rect.min_incl_y));
599 } else {
600 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
601 sk_bzero(ptr, len);
602 ptr += pixels.stride;
603 }
Nigel Tao39da10b2019-11-15 15:27:44 +1100604 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100605 }
606
Nigel Taobf4605f2020-09-28 21:53:07 +1000607 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
Nigel Taob7a1b512019-02-10 12:19:50 +1100608 return SkCodec::kSuccess;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400609}
610
611SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
612 if (!fIncrDecDst) {
613 return SkCodec::kInternalError;
614 }
615
Nigel Tao2777cd32019-10-29 11:10:25 +1100616 // If multiple SkCodec::incrementalDecode calls are made consecutively (or
617 // if SkCodec::incrementalDecode is called immediately after
618 // SkCodec::startIncrementalDecode), then this seek should be a no-op.
619 // However, it is possible to interleave SkCodec::getFrameCount calls in
620 // between SkCodec::incrementalDecode calls, and those other calls may
621 // advance the stream. This seek restores the stream to where the last
622 // SkCodec::startIncrementalDecode or SkCodec::incrementalDecode stopped.
623 if (!seek_buffer(&fIOBuffer, fStream.get(), fIncrDecReaderIOPosition)) {
624 return SkCodec::kInternalError;
625 }
626
Nigel Tao027f89b2019-12-06 10:56:24 +1100627 if (rowsDecoded) {
628 *rowsDecoded = dstInfo().height();
629 }
630
631 SkCodec::Result result =
632 fIncrDecOnePass ? this->onIncrementalDecodeOnePass() : this->onIncrementalDecodeTwoPass();
Nigel Tao2777cd32019-10-29 11:10:25 +1100633 if (result == SkCodec::kSuccess) {
634 fIncrDecDst = nullptr;
635 fIncrDecReaderIOPosition = 0;
636 fIncrDecRowBytes = 0;
Nigel Taobf4605f2020-09-28 21:53:07 +1000637 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
Nigel Tao027f89b2019-12-06 10:56:24 +1100638 fIncrDecOnePass = false;
Nigel Tao2777cd32019-10-29 11:10:25 +1100639 } else {
640 fIncrDecReaderIOPosition = fIOBuffer.reader_io_position();
641 }
642 return result;
643}
644
Nigel Tao027f89b2019-12-06 10:56:24 +1100645SkCodec::Result SkWuffsCodec::onIncrementalDecodeOnePass() {
646 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
647 if (status != nullptr) {
648 if (status == wuffs_base__suspension__short_read) {
649 return SkCodec::kIncompleteInput;
650 } else {
651 SkCodecPrintf("decodeFrame: %s", status);
652 return SkCodec::kErrorInInput;
653 }
654 }
655 return SkCodec::kSuccess;
656}
657
658SkCodec::Result SkWuffsCodec::onIncrementalDecodeTwoPass() {
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500659 SkCodec::Result result = SkCodec::kSuccess;
Nigel Tao2777cd32019-10-29 11:10:25 +1100660 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
661 bool independent;
662 SkAlphaType alphaType;
663 const int index = options().fFrameIndex;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400664 if (index == 0) {
665 independent = true;
666 alphaType = to_alpha_type(getEncodedInfo().opaque());
667 } else {
668 const SkWuffsFrame* f = this->frame(index);
669 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
670 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
671 }
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500672 if (status != nullptr) {
673 if (status == wuffs_base__suspension__short_read) {
674 result = SkCodec::kIncompleteInput;
675 } else {
676 SkCodecPrintf("decodeFrame: %s", status);
677 result = SkCodec::kErrorInInput;
678 }
679
680 if (!independent) {
681 // For a dependent frame, we cannot blend the partial result, since
682 // that will overwrite the contribution from prior frames.
683 return result;
684 }
685 }
686
Nigel Taob54946b2020-06-18 23:36:27 +1000687 uint32_t src_bits_per_pixel = fPixelBuffer.pixcfg.pixel_format().bits_per_pixel();
Nigel Tao490e6472019-02-14 14:50:53 +1100688 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
689 return SkCodec::kInternalError;
690 }
691 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
692
Nigel Tao2777cd32019-10-29 11:10:25 +1100693 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400694 if (fFirstCallToIncrementalDecode) {
Nigel Tao490e6472019-02-14 14:50:53 +1100695 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
696 return SkCodec::kInternalError;
697 }
698
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400699 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
700 frame_rect.max_excl_x, frame_rect.max_excl_y);
701
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500702 // If the frame rect does not fill the output, ensure that those pixels are not
Nigel Taob7a1b512019-02-10 12:19:50 +1100703 // left uninitialized.
Leon Scroggins III44076362019-02-15 13:56:44 -0500704 if (independent && (bounds != this->bounds() || result != kSuccess)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100705 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes, options().fZeroInitialized);
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500706 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400707 fFirstCallToIncrementalDecode = false;
708 } else {
709 // Existing clients intend to only show frames beyond the first if they
710 // are complete (based on FrameInfo::fFullyReceived), since it might
711 // look jarring to draw a partial frame over an existing frame. If they
712 // changed their behavior and expected to continue decoding a partial
713 // frame after the first one, we'll need to update our blending code.
714 // Otherwise, if the frame were interlaced and not independent, the
715 // second pass may have an overlapping dirty_rect with the first,
716 // resulting in blending with the first pass.
717 SkASSERT(index == 0);
Nigel Tao9859ef82019-02-13 13:20:02 +1100718 }
Nigel Tao0185b952018-11-08 10:47:24 +1100719
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500720 // If the frame's dirty rect is empty, no need to swizzle.
Nigel Tao2777cd32019-10-29 11:10:25 +1100721 wuffs_base__rect_ie_u32 dirty_rect = fDecoders[WhichDecoder::kIncrDecode]->frame_dirty_rect();
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500722 if (!dirty_rect.is_empty()) {
Nigel Tao490e6472019-02-14 14:50:53 +1100723 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500724
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400725 // The Wuffs model is that the dst buffer is the image, not the frame.
726 // The expectation is that you allocate the buffer once, but re-use it
727 // for the N frames, regardless of each frame's top-left co-ordinate.
728 //
729 // To get from the start (in the X-direction) of the image to the start
730 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
Nigel Tao2777cd32019-10-29 11:10:25 +1100731 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride) +
732 (dirty_rect.min_incl_x * src_bytes_per_pixel);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500733
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400734 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
735 // used for other formats that might have one, we will need to transform from profiles that
736 // do not have corresponding SkColorSpaces.
737 SkASSERT(!getEncodedInfo().profile());
738
Nigel Tao2777cd32019-10-29 11:10:25 +1100739 auto srcInfo =
740 getInfo().makeWH(dirty_rect.width(), dirty_rect.height()).makeAlphaType(alphaType);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400741 SkBitmap src;
742 src.installPixels(srcInfo, s, pixels.stride);
743 SkPaint paint;
744 if (independent) {
745 paint.setBlendMode(SkBlendMode::kSrc);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500746 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400747
748 SkDraw draw;
749 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
Nigel Tao5cfa7192020-08-17 21:14:13 +1000750 SkMatrix matrix = SkMatrix::MakeRectToRect(SkRect::Make(this->dimensions()),
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400751 SkRect::Make(this->dstInfo().dimensions()),
752 SkMatrix::kFill_ScaleToFit);
Brian Osman9aaec362020-05-08 14:54:37 -0400753 SkSimpleMatrixProvider matrixProvider(matrix);
754 draw.fMatrixProvider = &matrixProvider;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400755 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
756 draw.fRC = &rc;
757
Mike Reed1f607332020-05-21 12:11:27 -0400758 SkMatrix translate = SkMatrix::Translate(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400759 draw.drawBitmap(src, translate, nullptr, paint);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500760 }
Nigel Taof0148c42019-12-08 19:12:13 +1100761
762 if (result == SkCodec::kSuccess) {
763 // On success, we are done using the "two pass" pixel buffer for this
764 // frame. We have the option of releasing its memory, but there is a
765 // trade-off. If decoding a subsequent frame will also need "two pass"
766 // decoding, it would have to re-allocate the buffer instead of just
767 // re-using it. On the other hand, if there is no subsequent frame, and
768 // the SkWuffsCodec object isn't deleted soon, then we are holding
769 // megabytes of memory longer than we need to.
770 //
771 // For example, when the Chromium web browser decodes the <img> tags in
772 // a HTML page, the SkCodec object can live until navigating away from
773 // the page, which can be much longer than when the pixels are fully
774 // decoded, especially for a still (non-animated) image. Even for
775 // looping animations, caching the decoded frames (at the higher HTML
776 // renderer layer) may mean that each frame is only decoded once (at
777 // the lower SkCodec layer), in sequence.
778 //
779 // The heuristic we use here is to free the memory if we have decoded
780 // the last frame of the animation (or, for still images, the only
781 // frame). The output of the next decode request (if any) should be the
782 // same either way, but the steady state memory use should hopefully be
783 // lower than always keeping the fTwoPassPixbufPtr buffer up until the
784 // SkWuffsCodec destructor runs.
785 //
786 // This only applies to "two pass" decoding. "One pass" decoding does
787 // not allocate, free or otherwise use fTwoPassPixbufPtr.
788 if (fFramesComplete && (static_cast<size_t>(options().fFrameIndex) == fFrames.size() - 1)) {
789 fTwoPassPixbufPtr.reset(nullptr);
790 fTwoPassPixbufLen = 0;
791 }
792 }
793
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400794 return result;
795}
796
797int SkWuffsCodec::onGetFrameCount() {
Nigel Tao3876a9f2019-11-14 09:04:45 +1100798 if (!fFramesComplete && seek_buffer(&fIOBuffer, fStream.get(), fFrameCountReaderIOPosition)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100799 this->onGetFrameCountInternal();
Nigel Tao3876a9f2019-11-14 09:04:45 +1100800 fFrameCountReaderIOPosition =
801 fDecoders[WhichDecoder::kFrameCount] ? fIOBuffer.reader_io_position() : 0;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400802 }
803 return fFrames.size();
804}
805
Nigel Tao2777cd32019-10-29 11:10:25 +1100806void SkWuffsCodec::onGetFrameCountInternal() {
Nigel Tao2777cd32019-10-29 11:10:25 +1100807 if (!fDecoders[WhichDecoder::kFrameCount]) {
808 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
809 if (!decoder_raw) {
810 return;
811 }
812 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
813 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
Nigel Tao3876a9f2019-11-14 09:04:45 +1100814 reset_and_decode_image_config(decoder.get(), nullptr, &fIOBuffer, fStream.get());
Nigel Tao2777cd32019-10-29 11:10:25 +1100815 fDecoders[WhichDecoder::kFrameCount] = std::move(decoder);
816 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400817
818 // Iterate through the frames, converting from Wuffs'
819 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
Nigel Tao3876a9f2019-11-14 09:04:45 +1100820 while (true) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100821 const char* status = this->decodeFrameConfig(WhichDecoder::kFrameCount);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400822 if (status == nullptr) {
823 // No-op.
Nigel Taob54946b2020-06-18 23:36:27 +1000824 } else if (status == wuffs_base__note__end_of_data) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400825 break;
826 } else {
827 return;
828 }
829
Nigel Tao3876a9f2019-11-14 09:04:45 +1100830 uint64_t i = fDecoders[WhichDecoder::kFrameCount]->num_decoded_frame_configs();
831 if (i > INT_MAX) {
832 break;
833 }
834 if ((i == 0) || (static_cast<size_t>(i - 1) != fFrames.size())) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400835 continue;
836 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100837 fFrames.emplace_back(&fFrameConfigs[WhichDecoder::kFrameCount]);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400838 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
839 fFrameHolder.setAlphaAndRequiredFrame(f);
840 }
841
842 fFramesComplete = true;
Nigel Tao5b271462019-11-12 21:02:20 +1100843
844 // We've seen the end of the animation. There'll be no more frames, so we
845 // no longer need the kFrameCount decoder. Releasing it earlier than the
846 // SkWuffsCodec destructor might help peak memory use.
847 fDecoders[WhichDecoder::kFrameCount].reset(nullptr);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400848}
849
Nigel Taocdc92382019-10-31 08:36:53 +1100850bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
851 const SkWuffsFrame* f = this->frame(i);
852 if (!f) {
853 return false;
854 }
855 if (frameInfo) {
856 *frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
857 }
858 return true;
859}
860
861int SkWuffsCodec::onGetRepetitionCount() {
862 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
863 // number is how many times to play the loop. Skia's int number is how many
864 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
865 // kRepetitionCountInfinite respectively to mean loop forever.
866 uint32_t n = fDecoders[WhichDecoder::kIncrDecode]->num_animation_loops();
867 if (n == 0) {
868 return SkCodec::kRepetitionCountInfinite;
869 }
870 n--;
871 return n < INT_MAX ? n : INT_MAX;
872}
873
Nigel Tao2777cd32019-10-29 11:10:25 +1100874SkCodec::Result SkWuffsCodec::seekFrame(WhichDecoder which, int frameIndex) {
875 if (fDecoderIsSuspended[which]) {
876 SkCodec::Result res = this->resetDecoder(which);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400877 if (res != SkCodec::kSuccess) {
878 return res;
879 }
880 }
881
882 uint64_t pos = 0;
883 if (frameIndex < 0) {
884 return SkCodec::kInternalError;
885 } else if (frameIndex == 0) {
886 pos = fFirstFrameIOPosition;
887 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
888 pos = fFrames[frameIndex].ioPosition();
889 } else {
890 return SkCodec::kInternalError;
891 }
892
893 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
894 return SkCodec::kInternalError;
895 }
Nigel Taob54946b2020-06-18 23:36:27 +1000896 wuffs_base__status status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100897 fDecoders[which]->restart_frame(frameIndex, fIOBuffer.reader_io_position());
Nigel Taob54946b2020-06-18 23:36:27 +1000898 if (status.repr != nullptr) {
899 return SkCodec::kInternalError;
900 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400901 return SkCodec::kSuccess;
902}
903
Nigel Tao2777cd32019-10-29 11:10:25 +1100904SkCodec::Result SkWuffsCodec::resetDecoder(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400905 if (!fStream->rewind()) {
906 return SkCodec::kInternalError;
907 }
Nigel Tao96c10a02019-09-25 11:08:42 +1000908 fIOBuffer.meta = wuffs_base__empty_io_buffer_meta();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400909
910 SkCodec::Result result =
Nigel Tao2777cd32019-10-29 11:10:25 +1100911 reset_and_decode_image_config(fDecoders[which].get(), nullptr, &fIOBuffer, fStream.get());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400912 if (result == SkCodec::kIncompleteInput) {
913 return SkCodec::kInternalError;
914 } else if (result != SkCodec::kSuccess) {
915 return result;
916 }
917
Nigel Tao2777cd32019-10-29 11:10:25 +1100918 fDecoderIsSuspended[which] = false;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400919 return SkCodec::kSuccess;
920}
921
Nigel Tao2777cd32019-10-29 11:10:25 +1100922const char* SkWuffsCodec::decodeFrameConfig(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400923 while (true) {
Nigel Taob54946b2020-06-18 23:36:27 +1000924 wuffs_base__status status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100925 fDecoders[which]->decode_frame_config(&fFrameConfigs[which], &fIOBuffer);
Nigel Taob54946b2020-06-18 23:36:27 +1000926 if ((status.repr == wuffs_base__suspension__short_read) &&
927 fill_buffer(&fIOBuffer, fStream.get())) {
928 continue;
929 }
930 fDecoderIsSuspended[which] = !status.is_complete();
931 this->updateNumFullyReceivedFrames(which);
932 return status.repr;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400933 }
934}
935
Nigel Tao2777cd32019-10-29 11:10:25 +1100936const char* SkWuffsCodec::decodeFrame(WhichDecoder which) {
937 while (true) {
Nigel Taob54946b2020-06-18 23:36:27 +1000938 wuffs_base__status status = fDecoders[which]->decode_frame(
Nigel Taobf4605f2020-09-28 21:53:07 +1000939 &fPixelBuffer, &fIOBuffer, fIncrDecPixelBlend,
Nigel Taob54946b2020-06-18 23:36:27 +1000940 wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen), NULL);
941 if ((status.repr == wuffs_base__suspension__short_read) &&
942 fill_buffer(&fIOBuffer, fStream.get())) {
943 continue;
944 }
945 fDecoderIsSuspended[which] = !status.is_complete();
946 this->updateNumFullyReceivedFrames(which);
947 return status.repr;
Nigel Tao2777cd32019-10-29 11:10:25 +1100948 }
949}
950
951void SkWuffsCodec::updateNumFullyReceivedFrames(WhichDecoder which) {
Nigel Tao6af1edc2019-01-19 15:12:39 +1100952 // num_decoded_frames's return value, n, can change over time, both up and
953 // down, as we seek back and forth in the underlying stream.
954 // fNumFullyReceivedFrames is the highest n we've seen.
Nigel Tao2777cd32019-10-29 11:10:25 +1100955 uint64_t n = fDecoders[which]->num_decoded_frames();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400956 if (fNumFullyReceivedFrames < n) {
957 fNumFullyReceivedFrames = n;
958 }
959}
960
961// -------------------------------- SkWuffsCodec.h functions
962
963bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
964 constexpr const char* gif_ptr = "GIF8";
965 constexpr size_t gif_len = 4;
966 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
967}
968
969std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
970 SkCodec::Result* result) {
Nigel Tao48aa2212019-03-09 14:59:11 +1100971 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
972 wuffs_base__io_buffer iobuf =
973 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
Nigel Tao96c10a02019-09-25 11:08:42 +1000974 wuffs_base__empty_io_buffer_meta());
Nigel Tao48aa2212019-03-09 14:59:11 +1100975 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400976
977 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
978 // the wuffs_base__etc types, the sizeof a file format specific type like
979 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
980 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
981 // to an opaque type: a private implementation detail. The API is always
982 // "set_foo(p, etc)" and not "p->foo = etc".
983 //
984 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
985 //
986 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
987 // the struct at compile time). Instead, we use sk_malloc_canfail, with
988 // sizeof__wuffs_gif__decoder returning the appropriate value for the
989 // (statically or dynamically) linked version of the Wuffs library.
990 //
991 // As a C (not C++) library, none of the Wuffs types have constructors or
992 // destructors.
993 //
994 // In RAII style, we can still use std::unique_ptr with these pointers, but
995 // we pair the pointer with sk_free instead of C++'s delete.
996 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
997 if (!decoder_raw) {
998 *result = SkCodec::kInternalError;
999 return nullptr;
1000 }
1001 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
1002 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
1003
1004 SkCodec::Result reset_result =
1005 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
1006 if (reset_result != SkCodec::kSuccess) {
1007 *result = reset_result;
1008 return nullptr;
1009 }
1010
1011 uint32_t width = imgcfg.pixcfg.width();
1012 uint32_t height = imgcfg.pixcfg.height();
1013 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
1014 *result = SkCodec::kInvalidInput;
1015 return nullptr;
1016 }
1017
Nigel Tao6af1edc2019-01-19 15:12:39 +11001018 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
Nigel Tao22e86242019-01-26 16:04:01 +11001019 void* workbuf_ptr_raw = nullptr;
1020 if (workbuf_len) {
1021 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
1022 if (!workbuf_ptr_raw) {
1023 *result = SkCodec::kInternalError;
1024 return nullptr;
1025 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001026 }
1027 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
1028 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
1029
Nigel Tao490e6472019-02-14 14:50:53 +11001030 SkEncodedInfo::Color color =
Nigel Taob54946b2020-06-18 23:36:27 +10001031 (imgcfg.pixcfg.pixel_format().repr == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
Nigel Tao490e6472019-02-14 14:50:53 +11001032 ? SkEncodedInfo::kBGRA_Color
1033 : SkEncodedInfo::kRGBA_Color;
1034
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001035 // In Skia's API, the alpha we calculate here and return is only for the
1036 // first frame.
1037 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
1038 : SkEncodedInfo::kBinary_Alpha;
1039
Nigel Tao490e6472019-02-14 14:50:53 +11001040 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001041
1042 *result = SkCodec::kSuccess;
Nigel Tao027f89b2019-12-06 10:56:24 +11001043 return std::unique_ptr<SkCodec>(new SkWuffsCodec(std::move(encodedInfo), std::move(stream),
1044 std::move(decoder), std::move(workbuf_ptr),
1045 workbuf_len, imgcfg, iobuf));
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001046}