blob: 7b56e54c75740edca1a7add2d5eeb8cf4bb3f23e [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"
18#include "src/core/SkRasterClip.h"
19#include "src/core/SkUtils.h"
Nigel Taoa6766482019-01-07 13:41:53 +110020
Ben Wagner666a9f92019-05-02 17:45:00 -040021#include <limits.h>
22
Nigel Taoa6766482019-01-07 13:41:53 +110023// Wuffs ships as a "single file C library" or "header file library" as per
24// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
25//
26// As we have not #define'd WUFFS_IMPLEMENTATION, the #include here is
27// including a header file, even though that file name ends in ".c".
Nigel Taoe66a0b22019-03-09 15:03:14 +110028#if defined(WUFFS_IMPLEMENTATION)
29#error "SkWuffsCodec should not #define WUFFS_IMPLEMENTATION"
30#endif
Nigel Taoa6766482019-01-07 13:41:53 +110031#include "wuffs-v0.2.c"
Nigel Tao477f2212019-10-09 10:07:11 +110032#if WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT < 1942
Nigel Taoa6766482019-01-07 13:41:53 +110033#error "Wuffs version is too old. Upgrade to the latest version."
34#endif
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040035
36#define SK_WUFFS_CODEC_BUFFER_SIZE 4096
37
Nigel Tao3dc31212019-12-07 11:46:16 +110038// Configuring a Skia build with
39// SK_WUFFS_FAVORS_PERFORMANCE_OVER_ADDITIONAL_MEMORY_SAFETY can improve decode
40// performance by some fixed amount (independent of the image size), which can
41// be a noticeable proportional improvement if the input is relatively small.
42//
43// The Wuffs library is still memory-safe either way, in that there are no
44// out-of-bounds reads or writes, and the library endeavours not to read
45// uninitialized memory. There are just fewer compiler-enforced guarantees
46// against reading uninitialized memory. For more detail, see
47// https://github.com/google/wuffs/blob/master/doc/note/initialization.md#partial-zero-initialization
48#if defined(SK_WUFFS_FAVORS_PERFORMANCE_OVER_ADDITIONAL_MEMORY_SAFETY)
49#define SK_WUFFS_INITIALIZE_FLAGS WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED
50#else
51#define SK_WUFFS_INITIALIZE_FLAGS WUFFS_INITIALIZE__DEFAULT_OPTIONS
52#endif
53
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040054static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
55 b->compact();
56 size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
57 b->meta.wi += num_read;
58 b->meta.closed = s->isAtEnd();
59 return num_read > 0;
60}
61
62static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
63 // Try to re-position the io_buffer's meta.ri read-index first, which is
64 // cheaper than seeking in the backing SkStream.
65 if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
66 b->meta.ri = pos - b->meta.pos;
67 return true;
68 }
69 // Seek in the backing SkStream.
70 if ((pos > SIZE_MAX) || (!s->seek(pos))) {
71 return false;
72 }
73 b->meta.wi = 0;
74 b->meta.ri = 0;
75 b->meta.pos = pos;
76 b->meta.closed = false;
77 return true;
78}
79
80static SkEncodedInfo::Alpha wuffs_blend_to_skia_alpha(wuffs_base__animation_blend w) {
81 return (w == WUFFS_BASE__ANIMATION_BLEND__OPAQUE) ? SkEncodedInfo::kOpaque_Alpha
82 : SkEncodedInfo::kUnpremul_Alpha;
83}
84
85static SkCodecAnimation::Blend wuffs_blend_to_skia_blend(wuffs_base__animation_blend w) {
86 return (w == WUFFS_BASE__ANIMATION_BLEND__SRC) ? SkCodecAnimation::Blend::kBG
87 : SkCodecAnimation::Blend::kPriorFrame;
88}
89
90static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
91 wuffs_base__animation_disposal w) {
92 switch (w) {
93 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
94 return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
95 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
96 return SkCodecAnimation::DisposalMethod::kRestorePrevious;
97 default:
98 return SkCodecAnimation::DisposalMethod::kKeep;
99 }
100}
101
Nigel Tao4f32a292019-11-13 15:41:55 +1100102static SkAlphaType to_alpha_type(bool opaque) {
103 return opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
104}
105
Nigel Tao2777cd32019-10-29 11:10:25 +1100106static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
107 wuffs_base__image_config* imgcfg,
108 wuffs_base__io_buffer* b,
Nigel Tao4f32a292019-11-13 15:41:55 +1100109 SkStream* s) {
Nigel Tao3dc31212019-12-07 11:46:16 +1100110 // Calling decoder->initialize will memset most or all of it to zero,
111 // depending on SK_WUFFS_INITIALIZE_FLAGS.
112 const char* status =
113 decoder->initialize(sizeof__wuffs_gif__decoder(), WUFFS_VERSION, SK_WUFFS_INITIALIZE_FLAGS);
Nigel Tao4f32a292019-11-13 15:41:55 +1100114 if (status != nullptr) {
115 SkCodecPrintf("initialize: %s", status);
116 return SkCodec::kInternalError;
117 }
118 while (true) {
119 status = decoder->decode_image_config(imgcfg, b);
120 if (status == nullptr) {
121 break;
122 } else if (status != wuffs_base__suspension__short_read) {
123 SkCodecPrintf("decode_image_config: %s", status);
124 return SkCodec::kErrorInInput;
125 } else if (!fill_buffer(b, s)) {
126 return SkCodec::kIncompleteInput;
127 }
128 }
129
130 // A GIF image's natural color model is indexed color: 1 byte per pixel,
131 // indexing a 256-element palette.
132 //
133 // For Skia, we override that to decode to 4 bytes per pixel, BGRA or RGBA.
134 wuffs_base__pixel_format pixfmt = 0;
135 switch (kN32_SkColorType) {
136 case kBGRA_8888_SkColorType:
137 pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
138 break;
139 case kRGBA_8888_SkColorType:
140 pixfmt = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
141 break;
142 default:
143 return SkCodec::kInternalError;
144 }
145 if (imgcfg) {
146 imgcfg->pixcfg.set(pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, imgcfg->pixcfg.width(),
147 imgcfg->pixcfg.height());
148 }
149
150 return SkCodec::kSuccess;
151}
152
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400153// -------------------------------- Class definitions
154
155class SkWuffsCodec;
156
157class SkWuffsFrame final : public SkFrame {
158public:
159 SkWuffsFrame(wuffs_base__frame_config* fc);
160
161 SkCodec::FrameInfo frameInfo(bool fullyReceived) const;
162 uint64_t ioPosition() const;
163
164 // SkFrame overrides.
165 SkEncodedInfo::Alpha onReportedAlpha() const override;
166
167private:
168 uint64_t fIOPosition;
169 SkEncodedInfo::Alpha fReportedAlpha;
170
171 typedef SkFrame INHERITED;
172};
173
174// SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
175// SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
176// inherit from both SkCodec and SkFrameHolder, and Skia style discourages
177// multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
178class SkWuffsFrameHolder final : public SkFrameHolder {
179public:
180 SkWuffsFrameHolder() : INHERITED() {}
181
182 void init(SkWuffsCodec* codec, int width, int height);
183
184 // SkFrameHolder overrides.
185 const SkFrame* onGetFrame(int i) const override;
186
187private:
188 const SkWuffsCodec* fCodec;
189
190 typedef SkFrameHolder INHERITED;
191};
192
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400193class SkWuffsCodec final : public SkScalingCodec {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400194public:
195 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
196 std::unique_ptr<SkStream> stream,
197 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400198 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
199 size_t workbuf_len,
200 wuffs_base__image_config imgcfg,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400201 wuffs_base__io_buffer iobuf);
202
203 const SkWuffsFrame* frame(int i) const;
204
205private:
Nigel Tao2777cd32019-10-29 11:10:25 +1100206 // It is valid, in terms of the SkCodec API, to call SkCodec::getFrameCount
207 // while in an incremental decode (after onStartIncrementalDecode returns
208 // and before the rest of the image is decoded). Some Skia users expect
209 // getFrameCount to increase, and the SkStream to advance, when given more
210 // data.
211 //
212 // On the other hand, while in an incremental decode, the underlying Wuffs
213 // object is suspended in a coroutine. To keep its internal proof-of-safety
214 // invariants consistent, there's only two things you can safely do with a
215 // suspended Wuffs object: resume the coroutine, or reset all state (memset
216 // to zero and start again).
217 //
218 // The Wuffs API provides a limited, optional form of seeking, to the start
219 // of an animation frame's data, but does not provide arbitrary save and
220 // load of its internal state whilst in the middle of an animation frame.
221 //
222 // SkWuffsCodec therefore uses two Wuffs decoders: a primary decoder
223 // (kIncrDecode) to support startIncrementalDecode / incrementalDecode, and
224 // a secondary decoder (kFrameCount) to support getFrameCount. The two
225 // decoders' states can change independently.
226 //
227 // As of Wuffs version 0.2, both of these decoders have the same type. A
228 // future Wuffs version might let us use a different type for kFrameCount,
229 // one that is much lighter weight (in terms of memory requirements), as it
230 // doesn't have to handle decompressing pixel data.
231 enum WhichDecoder {
232 kIncrDecode,
233 kFrameCount,
234 kNumDecoders,
235 };
236
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400237 // SkCodec overrides.
238 SkEncodedImageFormat onGetEncodedFormat() const override;
239 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
240 const SkFrameHolder* getFrameHolder() const override;
241 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
242 void* dst,
243 size_t rowBytes,
244 const SkCodec::Options& options) override;
245 Result onIncrementalDecode(int* rowsDecoded) override;
246 int onGetFrameCount() override;
247 bool onGetFrameInfo(int, FrameInfo*) const override;
248 int onGetRepetitionCount() override;
249
Nigel Tao027f89b2019-12-06 10:56:24 +1100250 // Two separate implementations of onStartIncrementalDecode and
251 // onIncrementalDecode, named "one pass" and "two pass" decoding. One pass
252 // decoding writes directly from the Wuffs image decoder to the dst buffer
253 // (the dst argument to onStartIncrementalDecode). Two pass decoding first
254 // writes into an intermediate buffer, and then composites and transforms
255 // the intermediate buffer into the dst buffer.
256 //
257 // In the general case, we need the two pass decoder, because of Skia API
258 // features that Wuffs doesn't support (e.g. color correction, scaling,
259 // RGB565). But as an optimization, we use one pass decoding (it's faster
260 // and uses less memory) if applicable (see the assignment to
261 // fIncrDecOnePass that calculates when we can do so).
262 Result onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
263 uint8_t* dst,
264 size_t rowBytes,
265 const SkCodec::Options& options,
266 wuffs_base__pixel_format pixelFormat,
267 size_t bytesPerPixel);
268 Result onStartIncrementalDecodeTwoPass();
269 Result onIncrementalDecodeOnePass();
270 Result onIncrementalDecodeTwoPass();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400271
Nigel Tao027f89b2019-12-06 10:56:24 +1100272 void onGetFrameCountInternal();
273 Result seekFrame(WhichDecoder which, int frameIndex);
Nigel Tao2777cd32019-10-29 11:10:25 +1100274 Result resetDecoder(WhichDecoder which);
275 const char* decodeFrameConfig(WhichDecoder which);
276 const char* decodeFrame(WhichDecoder which);
277 void updateNumFullyReceivedFrames(WhichDecoder which);
278
279 SkWuffsFrameHolder fFrameHolder;
280 std::unique_ptr<SkStream> fStream;
Nigel Tao2777cd32019-10-29 11:10:25 +1100281 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
282 size_t fWorkbufLen;
283
284 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoders[WhichDecoder::kNumDecoders];
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400285
286 const uint64_t fFirstFrameIOPosition;
Nigel Tao2777cd32019-10-29 11:10:25 +1100287 wuffs_base__frame_config fFrameConfigs[WhichDecoder::kNumDecoders];
Nigel Tao027f89b2019-12-06 10:56:24 +1100288 wuffs_base__pixel_config fPixelConfig;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400289 wuffs_base__pixel_buffer fPixelBuffer;
290 wuffs_base__io_buffer fIOBuffer;
291
292 // Incremental decoding state.
Nigel Tao0185b952018-11-08 10:47:24 +1100293 uint8_t* fIncrDecDst;
Nigel Tao2777cd32019-10-29 11:10:25 +1100294 uint64_t fIncrDecReaderIOPosition;
Nigel Tao0185b952018-11-08 10:47:24 +1100295 size_t fIncrDecRowBytes;
Nigel Tao027f89b2019-12-06 10:56:24 +1100296 bool fIncrDecOnePass;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400297 bool fFirstCallToIncrementalDecode;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400298
Nigel Tao027f89b2019-12-06 10:56:24 +1100299 // Lazily allocated intermediate pixel buffer, for two pass decoding.
300 std::unique_ptr<uint8_t, decltype(&sk_free)> fTwoPassPixbufPtr;
301 size_t fTwoPassPixbufLen;
302
Nigel Tao3876a9f2019-11-14 09:04:45 +1100303 uint64_t fFrameCountReaderIOPosition;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400304 uint64_t fNumFullyReceivedFrames;
305 std::vector<SkWuffsFrame> fFrames;
306 bool fFramesComplete;
307
Nigel Tao2777cd32019-10-29 11:10:25 +1100308 // If calling an fDecoders[which] method returns an incomplete status, then
309 // fDecoders[which] is suspended in a coroutine (i.e. waiting on I/O or
310 // halted on a non-recoverable error). To keep its internal proof-of-safety
311 // invariants consistent, there's only two things you can safely do with a
312 // suspended Wuffs object: resume the coroutine, or reset all state (memset
313 // to zero and start again).
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400314 //
Nigel Tao2777cd32019-10-29 11:10:25 +1100315 // If fDecoderIsSuspended[which], and we aren't sure that we're going to
316 // resume the coroutine, then we will need to call this->resetDecoder
317 // before calling other fDecoders[which] methods.
318 bool fDecoderIsSuspended[WhichDecoder::kNumDecoders];
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400319
320 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
321
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400322 typedef SkScalingCodec INHERITED;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400323};
324
325// -------------------------------- SkWuffsFrame implementation
326
327SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
328 : INHERITED(fc->index()),
329 fIOPosition(fc->io_position()),
330 fReportedAlpha(wuffs_blend_to_skia_alpha(fc->blend())) {
331 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);
335 this->setBlend(wuffs_blend_to_skia_blend(fc->blend()));
336}
337
338SkCodec::FrameInfo SkWuffsFrame::frameInfo(bool fullyReceived) const {
Nigel Taoef40e332019-04-05 10:28:40 +1100339 SkCodec::FrameInfo ret;
340 ret.fRequiredFrame = getRequiredFrame();
341 ret.fDuration = getDuration();
342 ret.fFullyReceived = fullyReceived;
343 ret.fAlphaType = hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
344 ret.fDisposalMethod = getDisposalMethod();
345 return ret;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400346}
347
348uint64_t SkWuffsFrame::ioPosition() const {
349 return fIOPosition;
350}
351
352SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
353 return fReportedAlpha;
354}
355
356// -------------------------------- SkWuffsFrameHolder implementation
357
358void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
359 fCodec = codec;
360 // Initialize SkFrameHolder's (the superclass) fields.
361 fScreenWidth = width;
362 fScreenHeight = height;
363}
364
365const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
366 return fCodec->frame(i);
367};
368
369// -------------------------------- SkWuffsCodec implementation
370
371SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
372 std::unique_ptr<SkStream> stream,
373 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400374 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
375 size_t workbuf_len,
376 wuffs_base__image_config imgcfg,
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400377 wuffs_base__io_buffer iobuf)
378 : INHERITED(std::move(encodedInfo),
379 skcms_PixelFormat_RGBA_8888,
380 // Pass a nullptr SkStream to the SkCodec constructor. We
381 // manage the stream ourselves, as the default SkCodec behavior
382 // is too trigger-happy on rewinding the stream.
383 nullptr),
Nigel Tao0185b952018-11-08 10:47:24 +1100384 fFrameHolder(),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400385 fStream(std::move(stream)),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400386 fWorkbufPtr(std::move(workbuf_ptr)),
387 fWorkbufLen(workbuf_len),
Nigel Tao2777cd32019-10-29 11:10:25 +1100388 fDecoders{
389 std::move(dec),
390 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)>(nullptr, sk_free),
391 },
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400392 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
Nigel Tao2777cd32019-10-29 11:10:25 +1100393 fFrameConfigs{
394 wuffs_base__null_frame_config(),
395 wuffs_base__null_frame_config(),
396 },
Nigel Tao027f89b2019-12-06 10:56:24 +1100397 fPixelConfig(imgcfg.pixcfg),
398 fPixelBuffer(wuffs_base__null_pixel_buffer()),
Nigel Tao96c10a02019-09-25 11:08:42 +1000399 fIOBuffer(wuffs_base__empty_io_buffer()),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400400 fIncrDecDst(nullptr),
Nigel Tao2777cd32019-10-29 11:10:25 +1100401 fIncrDecReaderIOPosition(0),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400402 fIncrDecRowBytes(0),
Nigel Tao027f89b2019-12-06 10:56:24 +1100403 fIncrDecOnePass(false),
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400404 fFirstCallToIncrementalDecode(false),
Nigel Tao027f89b2019-12-06 10:56:24 +1100405 fTwoPassPixbufPtr(nullptr, &sk_free),
406 fTwoPassPixbufLen(0),
Nigel Tao3876a9f2019-11-14 09:04:45 +1100407 fFrameCountReaderIOPosition(0),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400408 fNumFullyReceivedFrames(0),
409 fFramesComplete(false),
Nigel Tao2777cd32019-10-29 11:10:25 +1100410 fDecoderIsSuspended{
411 false,
412 false,
413 } {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400414 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
415
416 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
417 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
418 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
419 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
420 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
Nigel Tao48aa2212019-03-09 14:59:11 +1100421 fIOBuffer.data = wuffs_base__make_slice_u8(fBuffer, SK_WUFFS_CODEC_BUFFER_SIZE);
422 fIOBuffer.meta = iobuf.meta;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400423}
424
425const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
426 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
427 return &fFrames[i];
428 }
429 return nullptr;
430}
431
432SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
433 return SkEncodedImageFormat::kGIF;
434}
435
436SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
437 void* dst,
438 size_t rowBytes,
439 const Options& options,
440 int* rowsDecoded) {
441 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
442 if (result != kSuccess) {
443 return result;
444 }
445 return this->onIncrementalDecode(rowsDecoded);
446}
447
448const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
449 return &fFrameHolder;
450}
451
452SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
453 void* dst,
454 size_t rowBytes,
455 const SkCodec::Options& options) {
Nigel Tao1f1cd1f2019-06-22 17:35:38 +1000456 if (!dst) {
457 return SkCodec::kInvalidParameters;
458 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400459 if (options.fSubset) {
460 return SkCodec::kUnimplemented;
461 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400462 if (options.fFrameIndex > 0 && SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
463 return SkCodec::kInvalidConversion;
464 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100465 SkCodec::Result result = this->seekFrame(WhichDecoder::kIncrDecode, options.fFrameIndex);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400466 if (result != SkCodec::kSuccess) {
467 return result;
468 }
469
Nigel Tao2777cd32019-10-29 11:10:25 +1100470 const char* status = this->decodeFrameConfig(WhichDecoder::kIncrDecode);
Nigel Taob7a1b512019-02-10 12:19:50 +1100471 if (status == wuffs_base__suspension__short_read) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500472 return SkCodec::kIncompleteInput;
Nigel Taob7a1b512019-02-10 12:19:50 +1100473 } else if (status != nullptr) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500474 SkCodecPrintf("decodeFrameConfig: %s", status);
475 return SkCodec::kErrorInInput;
476 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100477
Nigel Tao027f89b2019-12-06 10:56:24 +1100478 wuffs_base__pixel_format pixelFormat = WUFFS_BASE__PIXEL_FORMAT__INVALID;
479 size_t bytesPerPixel = 0;
480
481 switch (dstInfo.colorType()) {
482 case kBGRA_8888_SkColorType:
483 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
484 bytesPerPixel = 4;
485 break;
486 case kRGBA_8888_SkColorType:
487 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
488 bytesPerPixel = 4;
489 break;
490 default:
491 break;
492 }
493
494 // We can use "one pass" decoding if we have a Skia pixel format that Wuffs
495 // supports...
496 fIncrDecOnePass =
497 (pixelFormat != WUFFS_BASE__PIXEL_FORMAT__INVALID) &&
498 // ...and no color profile (as Wuffs does not support them)...
499 (!getEncodedInfo().profile()) &&
500 // ...and we have an independent frame (as Wuffs does not support the
501 // equivalent of SkBlendMode::kSrcOver)...
502 ((options.fFrameIndex == 0) ||
503 (this->frame(options.fFrameIndex)->getRequiredFrame() == SkCodec::kNoFrame)) &&
504 // ...and we use the identity transform (as Wuffs does not support
505 // scaling).
506 (this->dimensions() == dstInfo.dimensions());
507
508 result = fIncrDecOnePass ? this->onStartIncrementalDecodeOnePass(
509 dstInfo, static_cast<uint8_t*>(dst), rowBytes, options,
510 pixelFormat, bytesPerPixel)
511 : this->onStartIncrementalDecodeTwoPass();
512 if (result != SkCodec::kSuccess) {
513 return result;
514 }
515
516 fIncrDecDst = static_cast<uint8_t*>(dst);
517 fIncrDecReaderIOPosition = fIOBuffer.reader_io_position();
518 fIncrDecRowBytes = rowBytes;
519 fFirstCallToIncrementalDecode = true;
520 return SkCodec::kSuccess;
521}
522
523SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
524 uint8_t* dst,
525 size_t rowBytes,
526 const SkCodec::Options& options,
527 wuffs_base__pixel_format pixelFormat,
528 size_t bytesPerPixel) {
529 wuffs_base__pixel_config pixelConfig;
530 pixelConfig.set(pixelFormat, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, dstInfo.width(),
531 dstInfo.height());
532
533 wuffs_base__table_u8 table;
534 table.ptr = dst;
535 table.width = static_cast<size_t>(dstInfo.width()) * bytesPerPixel;
536 table.height = dstInfo.height();
537 table.stride = rowBytes;
538
539 const char* status = fPixelBuffer.set_from_table(&pixelConfig, table);
540 if (status != nullptr) {
541 SkCodecPrintf("set_from_table: %s", status);
Nigel Tao490e6472019-02-14 14:50:53 +1100542 return SkCodec::kInternalError;
543 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100544
Nigel Tao027f89b2019-12-06 10:56:24 +1100545 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
546 return SkCodec::kSuccess;
547}
548
549SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeTwoPass() {
550 // Either re-use the previously allocated "two pass" pixel buffer (and
551 // memset to zero), or allocate (and zero initialize) a new one.
552 bool already_zeroed = false;
553
554 if (!fTwoPassPixbufPtr) {
555 uint64_t pixbuf_len = fPixelConfig.pixbuf_len();
556 void* pixbuf_ptr_raw = (pixbuf_len <= SIZE_MAX)
557 ? sk_malloc_flags(pixbuf_len, SK_MALLOC_ZERO_INITIALIZE)
558 : nullptr;
559 if (!pixbuf_ptr_raw) {
560 return SkCodec::kInternalError;
561 }
562 fTwoPassPixbufPtr.reset(reinterpret_cast<uint8_t*>(pixbuf_ptr_raw));
563 fTwoPassPixbufLen = SkToSizeT(pixbuf_len);
564 already_zeroed = true;
565 }
566
567 const char* status = fPixelBuffer.set_from_slice(
568 &fPixelConfig, wuffs_base__make_slice_u8(fTwoPassPixbufPtr.get(), fTwoPassPixbufLen));
569 if (status != nullptr) {
570 SkCodecPrintf("set_from_slice: %s", status);
571 return SkCodec::kInternalError;
572 }
573
574 if (!already_zeroed) {
575 uint32_t src_bits_per_pixel =
576 wuffs_base__pixel_format__bits_per_pixel(fPixelConfig.pixel_format());
577 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
578 return SkCodec::kInternalError;
579 }
580 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
581
Nigel Tao39da10b2019-11-15 15:27:44 +1100582 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
583 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Nigel Tao6ec1b392019-11-20 12:28:50 +1100584
585 uint8_t* ptr = pixels.ptr + (frame_rect.min_incl_y * pixels.stride) +
586 (frame_rect.min_incl_x * src_bytes_per_pixel);
587 size_t len = frame_rect.width() * src_bytes_per_pixel;
588
589 // As an optimization, issue a single sk_bzero call, if possible.
590 // Otherwise, zero out each row separately.
591 if ((len == pixels.stride) && (frame_rect.min_incl_y < frame_rect.max_excl_y)) {
592 sk_bzero(ptr, len * (frame_rect.max_excl_y - frame_rect.min_incl_y));
593 } else {
594 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
595 sk_bzero(ptr, len);
596 ptr += pixels.stride;
597 }
Nigel Tao39da10b2019-11-15 15:27:44 +1100598 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100599 }
600
Nigel Taob7a1b512019-02-10 12:19:50 +1100601 return SkCodec::kSuccess;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400602}
603
604SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
605 if (!fIncrDecDst) {
606 return SkCodec::kInternalError;
607 }
608
Nigel Tao2777cd32019-10-29 11:10:25 +1100609 // If multiple SkCodec::incrementalDecode calls are made consecutively (or
610 // if SkCodec::incrementalDecode is called immediately after
611 // SkCodec::startIncrementalDecode), then this seek should be a no-op.
612 // However, it is possible to interleave SkCodec::getFrameCount calls in
613 // between SkCodec::incrementalDecode calls, and those other calls may
614 // advance the stream. This seek restores the stream to where the last
615 // SkCodec::startIncrementalDecode or SkCodec::incrementalDecode stopped.
616 if (!seek_buffer(&fIOBuffer, fStream.get(), fIncrDecReaderIOPosition)) {
617 return SkCodec::kInternalError;
618 }
619
Nigel Tao027f89b2019-12-06 10:56:24 +1100620 if (rowsDecoded) {
621 *rowsDecoded = dstInfo().height();
622 }
623
624 SkCodec::Result result =
625 fIncrDecOnePass ? this->onIncrementalDecodeOnePass() : this->onIncrementalDecodeTwoPass();
Nigel Tao2777cd32019-10-29 11:10:25 +1100626 if (result == SkCodec::kSuccess) {
627 fIncrDecDst = nullptr;
628 fIncrDecReaderIOPosition = 0;
629 fIncrDecRowBytes = 0;
Nigel Tao027f89b2019-12-06 10:56:24 +1100630 fIncrDecOnePass = false;
Nigel Tao2777cd32019-10-29 11:10:25 +1100631 } else {
632 fIncrDecReaderIOPosition = fIOBuffer.reader_io_position();
633 }
634 return result;
635}
636
Nigel Tao027f89b2019-12-06 10:56:24 +1100637SkCodec::Result SkWuffsCodec::onIncrementalDecodeOnePass() {
638 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
639 if (status != nullptr) {
640 if (status == wuffs_base__suspension__short_read) {
641 return SkCodec::kIncompleteInput;
642 } else {
643 SkCodecPrintf("decodeFrame: %s", status);
644 return SkCodec::kErrorInInput;
645 }
646 }
647 return SkCodec::kSuccess;
648}
649
650SkCodec::Result SkWuffsCodec::onIncrementalDecodeTwoPass() {
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500651 SkCodec::Result result = SkCodec::kSuccess;
Nigel Tao2777cd32019-10-29 11:10:25 +1100652 const char* status = this->decodeFrame(WhichDecoder::kIncrDecode);
653 bool independent;
654 SkAlphaType alphaType;
655 const int index = options().fFrameIndex;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400656 if (index == 0) {
657 independent = true;
658 alphaType = to_alpha_type(getEncodedInfo().opaque());
659 } else {
660 const SkWuffsFrame* f = this->frame(index);
661 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
662 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
663 }
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500664 if (status != nullptr) {
665 if (status == wuffs_base__suspension__short_read) {
666 result = SkCodec::kIncompleteInput;
667 } else {
668 SkCodecPrintf("decodeFrame: %s", status);
669 result = SkCodec::kErrorInInput;
670 }
671
672 if (!independent) {
673 // For a dependent frame, we cannot blend the partial result, since
674 // that will overwrite the contribution from prior frames.
675 return result;
676 }
677 }
678
Nigel Tao490e6472019-02-14 14:50:53 +1100679 uint32_t src_bits_per_pixel =
680 wuffs_base__pixel_format__bits_per_pixel(fPixelBuffer.pixcfg.pixel_format());
681 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
682 return SkCodec::kInternalError;
683 }
684 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
685
Nigel Tao2777cd32019-10-29 11:10:25 +1100686 wuffs_base__rect_ie_u32 frame_rect = fFrameConfigs[WhichDecoder::kIncrDecode].bounds();
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400687 if (fFirstCallToIncrementalDecode) {
Nigel Tao490e6472019-02-14 14:50:53 +1100688 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
689 return SkCodec::kInternalError;
690 }
691
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400692 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
693 frame_rect.max_excl_x, frame_rect.max_excl_y);
694
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500695 // If the frame rect does not fill the output, ensure that those pixels are not
Nigel Taob7a1b512019-02-10 12:19:50 +1100696 // left uninitialized.
Leon Scroggins III44076362019-02-15 13:56:44 -0500697 if (independent && (bounds != this->bounds() || result != kSuccess)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100698 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes, options().fZeroInitialized);
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500699 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400700 fFirstCallToIncrementalDecode = false;
701 } else {
702 // Existing clients intend to only show frames beyond the first if they
703 // are complete (based on FrameInfo::fFullyReceived), since it might
704 // look jarring to draw a partial frame over an existing frame. If they
705 // changed their behavior and expected to continue decoding a partial
706 // frame after the first one, we'll need to update our blending code.
707 // Otherwise, if the frame were interlaced and not independent, the
708 // second pass may have an overlapping dirty_rect with the first,
709 // resulting in blending with the first pass.
710 SkASSERT(index == 0);
Nigel Tao9859ef82019-02-13 13:20:02 +1100711 }
Nigel Tao0185b952018-11-08 10:47:24 +1100712
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500713 // If the frame's dirty rect is empty, no need to swizzle.
Nigel Tao2777cd32019-10-29 11:10:25 +1100714 wuffs_base__rect_ie_u32 dirty_rect = fDecoders[WhichDecoder::kIncrDecode]->frame_dirty_rect();
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500715 if (!dirty_rect.is_empty()) {
Nigel Tao490e6472019-02-14 14:50:53 +1100716 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500717
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400718 // The Wuffs model is that the dst buffer is the image, not the frame.
719 // The expectation is that you allocate the buffer once, but re-use it
720 // for the N frames, regardless of each frame's top-left co-ordinate.
721 //
722 // To get from the start (in the X-direction) of the image to the start
723 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
Nigel Tao2777cd32019-10-29 11:10:25 +1100724 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride) +
725 (dirty_rect.min_incl_x * src_bytes_per_pixel);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500726
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400727 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
728 // used for other formats that might have one, we will need to transform from profiles that
729 // do not have corresponding SkColorSpaces.
730 SkASSERT(!getEncodedInfo().profile());
731
Nigel Tao2777cd32019-10-29 11:10:25 +1100732 auto srcInfo =
733 getInfo().makeWH(dirty_rect.width(), dirty_rect.height()).makeAlphaType(alphaType);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400734 SkBitmap src;
735 src.installPixels(srcInfo, s, pixels.stride);
736 SkPaint paint;
737 if (independent) {
738 paint.setBlendMode(SkBlendMode::kSrc);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500739 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400740
741 SkDraw draw;
742 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
743 SkMatrix matrix = SkMatrix::MakeRectToRect(SkRect::Make(this->dimensions()),
744 SkRect::Make(this->dstInfo().dimensions()),
745 SkMatrix::kFill_ScaleToFit);
746 draw.fMatrix = &matrix;
747 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
748 draw.fRC = &rc;
749
750 SkMatrix translate = SkMatrix::MakeTrans(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
751 draw.drawBitmap(src, translate, nullptr, paint);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500752 }
Nigel Taof0148c42019-12-08 19:12:13 +1100753
754 if (result == SkCodec::kSuccess) {
755 // On success, we are done using the "two pass" pixel buffer for this
756 // frame. We have the option of releasing its memory, but there is a
757 // trade-off. If decoding a subsequent frame will also need "two pass"
758 // decoding, it would have to re-allocate the buffer instead of just
759 // re-using it. On the other hand, if there is no subsequent frame, and
760 // the SkWuffsCodec object isn't deleted soon, then we are holding
761 // megabytes of memory longer than we need to.
762 //
763 // For example, when the Chromium web browser decodes the <img> tags in
764 // a HTML page, the SkCodec object can live until navigating away from
765 // the page, which can be much longer than when the pixels are fully
766 // decoded, especially for a still (non-animated) image. Even for
767 // looping animations, caching the decoded frames (at the higher HTML
768 // renderer layer) may mean that each frame is only decoded once (at
769 // the lower SkCodec layer), in sequence.
770 //
771 // The heuristic we use here is to free the memory if we have decoded
772 // the last frame of the animation (or, for still images, the only
773 // frame). The output of the next decode request (if any) should be the
774 // same either way, but the steady state memory use should hopefully be
775 // lower than always keeping the fTwoPassPixbufPtr buffer up until the
776 // SkWuffsCodec destructor runs.
777 //
778 // This only applies to "two pass" decoding. "One pass" decoding does
779 // not allocate, free or otherwise use fTwoPassPixbufPtr.
780 if (fFramesComplete && (static_cast<size_t>(options().fFrameIndex) == fFrames.size() - 1)) {
781 fTwoPassPixbufPtr.reset(nullptr);
782 fTwoPassPixbufLen = 0;
783 }
784 }
785
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400786 return result;
787}
788
789int SkWuffsCodec::onGetFrameCount() {
Nigel Tao3876a9f2019-11-14 09:04:45 +1100790 if (!fFramesComplete && seek_buffer(&fIOBuffer, fStream.get(), fFrameCountReaderIOPosition)) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100791 this->onGetFrameCountInternal();
Nigel Tao3876a9f2019-11-14 09:04:45 +1100792 fFrameCountReaderIOPosition =
793 fDecoders[WhichDecoder::kFrameCount] ? fIOBuffer.reader_io_position() : 0;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400794 }
795 return fFrames.size();
796}
797
Nigel Tao2777cd32019-10-29 11:10:25 +1100798void SkWuffsCodec::onGetFrameCountInternal() {
Nigel Tao2777cd32019-10-29 11:10:25 +1100799 if (!fDecoders[WhichDecoder::kFrameCount]) {
800 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
801 if (!decoder_raw) {
802 return;
803 }
804 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
805 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
Nigel Tao3876a9f2019-11-14 09:04:45 +1100806 reset_and_decode_image_config(decoder.get(), nullptr, &fIOBuffer, fStream.get());
Nigel Tao2777cd32019-10-29 11:10:25 +1100807 fDecoders[WhichDecoder::kFrameCount] = std::move(decoder);
808 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400809
810 // Iterate through the frames, converting from Wuffs'
811 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
Nigel Tao3876a9f2019-11-14 09:04:45 +1100812 while (true) {
Nigel Tao2777cd32019-10-29 11:10:25 +1100813 const char* status = this->decodeFrameConfig(WhichDecoder::kFrameCount);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400814 if (status == nullptr) {
815 // No-op.
816 } else if (status == wuffs_base__warning__end_of_data) {
817 break;
818 } else {
819 return;
820 }
821
Nigel Tao3876a9f2019-11-14 09:04:45 +1100822 uint64_t i = fDecoders[WhichDecoder::kFrameCount]->num_decoded_frame_configs();
823 if (i > INT_MAX) {
824 break;
825 }
826 if ((i == 0) || (static_cast<size_t>(i - 1) != fFrames.size())) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400827 continue;
828 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100829 fFrames.emplace_back(&fFrameConfigs[WhichDecoder::kFrameCount]);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400830 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
831 fFrameHolder.setAlphaAndRequiredFrame(f);
832 }
833
834 fFramesComplete = true;
Nigel Tao5b271462019-11-12 21:02:20 +1100835
836 // We've seen the end of the animation. There'll be no more frames, so we
837 // no longer need the kFrameCount decoder. Releasing it earlier than the
838 // SkWuffsCodec destructor might help peak memory use.
839 fDecoders[WhichDecoder::kFrameCount].reset(nullptr);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400840}
841
Nigel Taocdc92382019-10-31 08:36:53 +1100842bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
843 const SkWuffsFrame* f = this->frame(i);
844 if (!f) {
845 return false;
846 }
847 if (frameInfo) {
848 *frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
849 }
850 return true;
851}
852
853int SkWuffsCodec::onGetRepetitionCount() {
854 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
855 // number is how many times to play the loop. Skia's int number is how many
856 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
857 // kRepetitionCountInfinite respectively to mean loop forever.
858 uint32_t n = fDecoders[WhichDecoder::kIncrDecode]->num_animation_loops();
859 if (n == 0) {
860 return SkCodec::kRepetitionCountInfinite;
861 }
862 n--;
863 return n < INT_MAX ? n : INT_MAX;
864}
865
Nigel Tao2777cd32019-10-29 11:10:25 +1100866SkCodec::Result SkWuffsCodec::seekFrame(WhichDecoder which, int frameIndex) {
867 if (fDecoderIsSuspended[which]) {
868 SkCodec::Result res = this->resetDecoder(which);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400869 if (res != SkCodec::kSuccess) {
870 return res;
871 }
872 }
873
874 uint64_t pos = 0;
875 if (frameIndex < 0) {
876 return SkCodec::kInternalError;
877 } else if (frameIndex == 0) {
878 pos = fFirstFrameIOPosition;
879 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
880 pos = fFrames[frameIndex].ioPosition();
881 } else {
882 return SkCodec::kInternalError;
883 }
884
885 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
886 return SkCodec::kInternalError;
887 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100888 const char* status =
889 fDecoders[which]->restart_frame(frameIndex, fIOBuffer.reader_io_position());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400890 if (status != nullptr) {
891 return SkCodec::kInternalError;
892 }
893 return SkCodec::kSuccess;
894}
895
896// An overview of the Wuffs decoding API:
897//
898// An animated image (such as GIF) has an image header and then N frames. The
899// image header gives e.g. the overall image's width and height. Each frame
900// consists of a frame header (e.g. frame rectangle bounds, display duration)
901// and a payload (the pixels).
902//
903// In Wuffs terminology, there is one image config and then N pairs of
904// (frame_config, frame). To decode everything (without knowing N in advance)
905// sequentially:
906// - call wuffs_gif__decoder::decode_image_config
907// - while (true) {
908// - call wuffs_gif__decoder::decode_frame_config
909// - if that returned wuffs_base__warning__end_of_data, break
910// - call wuffs_gif__decoder::decode_frame
911// - }
912//
913// The first argument to each decode_foo method is the destination struct to
914// store the decoded information.
915//
916// For random (instead of sequential) access to an image's frames, call
917// wuffs_gif__decoder::restart_frame to prepare to decode the i'th frame.
918// Essentially, it restores the state to be at the top of the while loop above.
919// The wuffs_base__io_buffer's reader position will also need to be set at the
920// right point in the source data stream. The position for the i'th frame is
921// calculated by the i'th decode_frame_config call. You can only call
922// restart_frame after decode_image_config is called, explicitly or implicitly
923// (see below), as decoding a single frame might require for-all-frames
924// information like the overall image dimensions and the global palette.
925//
926// All of those decode_xxx calls are optional. For example, if
927// decode_image_config is not called, then the first decode_frame_config call
928// will implicitly parse and verify the image header, before parsing the first
929// frame's header. Similarly, you can call only decode_frame N times, without
930// calling decode_image_config or decode_frame_config, if you already know
931// metadata like N and each frame's rectangle bounds by some other means (e.g.
932// this is a first party, statically known image).
933//
934// Specifically, starting with an unknown (but re-windable) GIF image, if you
935// want to just find N (i.e. count the number of frames), you can loop calling
936// only the decode_frame_config method and avoid calling the more expensive
937// decode_frame method. In terms of the underlying GIF image format, this will
938// skip over the LZW-encoded pixel data, avoiding the costly LZW decompression.
939//
940// Those decode_xxx methods are also suspendible. They will return early (with
941// a status code that is_suspendible and therefore isn't is_complete) if there
942// isn't enough source data to complete the operation: an incremental decode.
943// Calling decode_xxx again with additional source data will resume the
944// previous operation, instead of starting a new operation. Calling decode_yyy
945// whilst decode_xxx is suspended will result in an error.
946//
947// Once an error is encountered, whether from invalid source data or from a
948// programming error such as calling decode_yyy while suspended in decode_xxx,
949// all subsequent calls will be no-ops that return an error. To reset the
950// decoder into something that does productive work, memset the entire struct
951// to zero, check the Wuffs version and then, in order to be able to call
952// restart_frame, call decode_image_config. The io_buffer and its associated
953// stream will also need to be rewound.
954
Nigel Tao2777cd32019-10-29 11:10:25 +1100955SkCodec::Result SkWuffsCodec::resetDecoder(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400956 if (!fStream->rewind()) {
957 return SkCodec::kInternalError;
958 }
Nigel Tao96c10a02019-09-25 11:08:42 +1000959 fIOBuffer.meta = wuffs_base__empty_io_buffer_meta();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400960
961 SkCodec::Result result =
Nigel Tao2777cd32019-10-29 11:10:25 +1100962 reset_and_decode_image_config(fDecoders[which].get(), nullptr, &fIOBuffer, fStream.get());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400963 if (result == SkCodec::kIncompleteInput) {
964 return SkCodec::kInternalError;
965 } else if (result != SkCodec::kSuccess) {
966 return result;
967 }
968
Nigel Tao2777cd32019-10-29 11:10:25 +1100969 fDecoderIsSuspended[which] = false;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400970 return SkCodec::kSuccess;
971}
972
Nigel Tao2777cd32019-10-29 11:10:25 +1100973const char* SkWuffsCodec::decodeFrameConfig(WhichDecoder which) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400974 while (true) {
Nigel Tao48aa2212019-03-09 14:59:11 +1100975 const char* status =
Nigel Tao2777cd32019-10-29 11:10:25 +1100976 fDecoders[which]->decode_frame_config(&fFrameConfigs[which], &fIOBuffer);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400977 if ((status == wuffs_base__suspension__short_read) &&
978 fill_buffer(&fIOBuffer, fStream.get())) {
979 continue;
980 }
Nigel Tao2777cd32019-10-29 11:10:25 +1100981 fDecoderIsSuspended[which] = !wuffs_base__status__is_complete(status);
982 this->updateNumFullyReceivedFrames(which);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400983 return status;
984 }
985}
986
Nigel Tao2777cd32019-10-29 11:10:25 +1100987const char* SkWuffsCodec::decodeFrame(WhichDecoder which) {
988 while (true) {
989 const char* status = fDecoders[which]->decode_frame(
990 &fPixelBuffer, &fIOBuffer, wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen),
991 NULL);
992 if ((status == wuffs_base__suspension__short_read) &&
993 fill_buffer(&fIOBuffer, fStream.get())) {
994 continue;
995 }
996 fDecoderIsSuspended[which] = !wuffs_base__status__is_complete(status);
997 this->updateNumFullyReceivedFrames(which);
998 return status;
999 }
1000}
1001
1002void SkWuffsCodec::updateNumFullyReceivedFrames(WhichDecoder which) {
Nigel Tao6af1edc2019-01-19 15:12:39 +11001003 // num_decoded_frames's return value, n, can change over time, both up and
1004 // down, as we seek back and forth in the underlying stream.
1005 // fNumFullyReceivedFrames is the highest n we've seen.
Nigel Tao2777cd32019-10-29 11:10:25 +11001006 uint64_t n = fDecoders[which]->num_decoded_frames();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001007 if (fNumFullyReceivedFrames < n) {
1008 fNumFullyReceivedFrames = n;
1009 }
1010}
1011
1012// -------------------------------- SkWuffsCodec.h functions
1013
1014bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
1015 constexpr const char* gif_ptr = "GIF8";
1016 constexpr size_t gif_len = 4;
1017 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
1018}
1019
1020std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
1021 SkCodec::Result* result) {
Nigel Tao48aa2212019-03-09 14:59:11 +11001022 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
1023 wuffs_base__io_buffer iobuf =
1024 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
Nigel Tao96c10a02019-09-25 11:08:42 +10001025 wuffs_base__empty_io_buffer_meta());
Nigel Tao48aa2212019-03-09 14:59:11 +11001026 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001027
1028 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
1029 // the wuffs_base__etc types, the sizeof a file format specific type like
1030 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
1031 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
1032 // to an opaque type: a private implementation detail. The API is always
1033 // "set_foo(p, etc)" and not "p->foo = etc".
1034 //
1035 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
1036 //
1037 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
1038 // the struct at compile time). Instead, we use sk_malloc_canfail, with
1039 // sizeof__wuffs_gif__decoder returning the appropriate value for the
1040 // (statically or dynamically) linked version of the Wuffs library.
1041 //
1042 // As a C (not C++) library, none of the Wuffs types have constructors or
1043 // destructors.
1044 //
1045 // In RAII style, we can still use std::unique_ptr with these pointers, but
1046 // we pair the pointer with sk_free instead of C++'s delete.
1047 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
1048 if (!decoder_raw) {
1049 *result = SkCodec::kInternalError;
1050 return nullptr;
1051 }
1052 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
1053 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
1054
1055 SkCodec::Result reset_result =
1056 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
1057 if (reset_result != SkCodec::kSuccess) {
1058 *result = reset_result;
1059 return nullptr;
1060 }
1061
1062 uint32_t width = imgcfg.pixcfg.width();
1063 uint32_t height = imgcfg.pixcfg.height();
1064 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
1065 *result = SkCodec::kInvalidInput;
1066 return nullptr;
1067 }
1068
Nigel Tao6af1edc2019-01-19 15:12:39 +11001069 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
Nigel Tao22e86242019-01-26 16:04:01 +11001070 void* workbuf_ptr_raw = nullptr;
1071 if (workbuf_len) {
1072 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
1073 if (!workbuf_ptr_raw) {
1074 *result = SkCodec::kInternalError;
1075 return nullptr;
1076 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001077 }
1078 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
1079 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
1080
Nigel Tao490e6472019-02-14 14:50:53 +11001081 SkEncodedInfo::Color color =
1082 (imgcfg.pixcfg.pixel_format() == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
1083 ? SkEncodedInfo::kBGRA_Color
1084 : SkEncodedInfo::kRGBA_Color;
1085
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001086 // In Skia's API, the alpha we calculate here and return is only for the
1087 // first frame.
1088 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
1089 : SkEncodedInfo::kBinary_Alpha;
1090
Nigel Tao490e6472019-02-14 14:50:53 +11001091 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001092
1093 *result = SkCodec::kSuccess;
Nigel Tao027f89b2019-12-06 10:56:24 +11001094 return std::unique_ptr<SkCodec>(new SkWuffsCodec(std::move(encodedInfo), std::move(stream),
1095 std::move(decoder), std::move(workbuf_ptr),
1096 workbuf_len, imgcfg, iobuf));
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001097}