blob: 4cdbf635970f5f2de06ca7d03f3664ef8ee84f13 [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
8#include "SkWuffsCodec.h"
9
10#include "../private/SkMalloc.h"
11#include "SkFrameHolder.h"
12#include "SkSampler.h"
Nigel Tao0185b952018-11-08 10:47:24 +110013#include "SkSwizzler.h"
14#include "SkUtils.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040015#include "wuffs-v0.2.h"
16
17#define SK_WUFFS_CODEC_BUFFER_SIZE 4096
18
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040019static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
20 b->compact();
21 size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
22 b->meta.wi += num_read;
23 b->meta.closed = s->isAtEnd();
24 return num_read > 0;
25}
26
27static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
28 // Try to re-position the io_buffer's meta.ri read-index first, which is
29 // cheaper than seeking in the backing SkStream.
30 if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
31 b->meta.ri = pos - b->meta.pos;
32 return true;
33 }
34 // Seek in the backing SkStream.
35 if ((pos > SIZE_MAX) || (!s->seek(pos))) {
36 return false;
37 }
38 b->meta.wi = 0;
39 b->meta.ri = 0;
40 b->meta.pos = pos;
41 b->meta.closed = false;
42 return true;
43}
44
45static SkEncodedInfo::Alpha wuffs_blend_to_skia_alpha(wuffs_base__animation_blend w) {
46 return (w == WUFFS_BASE__ANIMATION_BLEND__OPAQUE) ? SkEncodedInfo::kOpaque_Alpha
47 : SkEncodedInfo::kUnpremul_Alpha;
48}
49
50static SkCodecAnimation::Blend wuffs_blend_to_skia_blend(wuffs_base__animation_blend w) {
51 return (w == WUFFS_BASE__ANIMATION_BLEND__SRC) ? SkCodecAnimation::Blend::kBG
52 : SkCodecAnimation::Blend::kPriorFrame;
53}
54
55static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
56 wuffs_base__animation_disposal w) {
57 switch (w) {
58 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
59 return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
60 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
61 return SkCodecAnimation::DisposalMethod::kRestorePrevious;
62 default:
63 return SkCodecAnimation::DisposalMethod::kKeep;
64 }
65}
66
67// -------------------------------- Class definitions
68
69class SkWuffsCodec;
70
71class SkWuffsFrame final : public SkFrame {
72public:
73 SkWuffsFrame(wuffs_base__frame_config* fc);
74
75 SkCodec::FrameInfo frameInfo(bool fullyReceived) const;
76 uint64_t ioPosition() const;
77
78 // SkFrame overrides.
79 SkEncodedInfo::Alpha onReportedAlpha() const override;
80
81private:
82 uint64_t fIOPosition;
83 SkEncodedInfo::Alpha fReportedAlpha;
84
85 typedef SkFrame INHERITED;
86};
87
88// SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
89// SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
90// inherit from both SkCodec and SkFrameHolder, and Skia style discourages
91// multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
92class SkWuffsFrameHolder final : public SkFrameHolder {
93public:
94 SkWuffsFrameHolder() : INHERITED() {}
95
96 void init(SkWuffsCodec* codec, int width, int height);
97
98 // SkFrameHolder overrides.
99 const SkFrame* onGetFrame(int i) const override;
100
101private:
102 const SkWuffsCodec* fCodec;
103
104 typedef SkFrameHolder INHERITED;
105};
106
Nigel Tao0185b952018-11-08 10:47:24 +1100107// SkWuffsSpySampler is a placeholder SkSampler implementation. The Skia API
108// expects to manipulate the codec's sampler (i.e. call setSampleX and
109// setSampleY) in between the startIncrementalDecode (SID) and
110// incrementalDecode (ID) calls. But creating the SkSwizzler (the real sampler)
111// requires knowing the destination buffer's dimensions, i.e. the animation
112// frame's width and height. That width and height are decoded in ID, not SID.
113//
114// To break that circle, the SkWuffsSpySampler always exists, so its methods
115// can be called between SID and ID. It doesn't actually do any sampling, it
116// merely records the arguments given to setSampleX (explicitly) and setSampleY
117// (implicitly, via the superclass' implementation). Inside ID, those recorded
118// arguments are forwarded on to the SkSwizzler (the real sampler) when that
119// SkSwizzler is created, after the frame width and height are known.
120//
121// Roughly speaking, the SkWuffsSpySampler is an eager proxy for the lazily
122// constructed real sampler. But that laziness is out of necessity.
123//
124// The "Spy" name is because it records its arguments. See
125// https://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs
126class SkWuffsSpySampler final : public SkSampler {
127public:
128 SkWuffsSpySampler(int imageWidth)
129 : INHERITED(), fFillWidth(0), fImageWidth(imageWidth), fSampleX(1) {}
130
131 void reset();
132 int sampleX() const;
133
134 int fFillWidth;
135
136private:
137 // SkSampler overrides.
138 int fillWidth() const override;
139 int onSetSampleX(int sampleX) override;
140
141 const int fImageWidth;
142
143 int fSampleX;
144
145 typedef SkSampler INHERITED;
146};
147
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400148class SkWuffsCodec final : public SkCodec {
149public:
150 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
151 std::unique_ptr<SkStream> stream,
152 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
153 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
154 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
155 size_t workbuf_len,
156 wuffs_base__image_config imgcfg,
157 wuffs_base__pixel_buffer pixbuf,
158 wuffs_base__io_buffer iobuf);
159
160 const SkWuffsFrame* frame(int i) const;
161
162private:
163 // SkCodec overrides.
164 SkEncodedImageFormat onGetEncodedFormat() const override;
165 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
166 const SkFrameHolder* getFrameHolder() const override;
167 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
168 void* dst,
169 size_t rowBytes,
170 const SkCodec::Options& options) override;
171 Result onIncrementalDecode(int* rowsDecoded) override;
172 int onGetFrameCount() override;
173 bool onGetFrameInfo(int, FrameInfo*) const override;
174 int onGetRepetitionCount() override;
Nigel Tao0185b952018-11-08 10:47:24 +1100175 SkSampler* getSampler(bool createIfNecessary) override;
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500176 bool conversionSupported(const SkImageInfo& dst, bool, bool) override;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400177
178 void readFrames();
179 Result seekFrame(int frameIndex);
180
181 Result resetDecoder();
182 const char* decodeFrameConfig();
183 const char* decodeFrame();
184 void updateNumFullyReceivedFrames();
185
Nigel Tao0185b952018-11-08 10:47:24 +1100186 SkWuffsSpySampler fSpySampler;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400187 SkWuffsFrameHolder fFrameHolder;
188 std::unique_ptr<SkStream> fStream;
189 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoder;
190 std::unique_ptr<uint8_t, decltype(&sk_free)> fPixbufPtr;
191 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
192 size_t fWorkbufLen;
193
194 const uint64_t fFirstFrameIOPosition;
195 wuffs_base__frame_config fFrameConfig;
196 wuffs_base__pixel_buffer fPixelBuffer;
197 wuffs_base__io_buffer fIOBuffer;
198
199 // Incremental decoding state.
Nigel Tao0185b952018-11-08 10:47:24 +1100200 uint8_t* fIncrDecDst;
Nigel Tao0185b952018-11-08 10:47:24 +1100201 size_t fIncrDecRowBytes;
202
203 std::unique_ptr<SkSwizzler> fSwizzler;
204 SkPMColor fColorTable[256];
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500205 bool fColorTableFilled;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400206
207 uint64_t fNumFullyReceivedFrames;
208 std::vector<SkWuffsFrame> fFrames;
209 bool fFramesComplete;
210
211 // If calling an fDecoder method returns an incomplete status, then
212 // fDecoder is suspended in a coroutine (i.e. waiting on I/O or halted on a
213 // non-recoverable error). To keep its internal proof-of-safety invariants
214 // consistent, there's only two things you can safely do with a suspended
215 // Wuffs object: resume the coroutine, or reset all state (memset to zero
216 // and start again).
217 //
218 // If fDecoderIsSuspended, and we aren't sure that we're going to resume
219 // the coroutine, then we will need to call this->resetDecoder before
220 // calling other fDecoder methods.
221 bool fDecoderIsSuspended;
222
223 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
224
225 typedef SkCodec INHERITED;
226};
227
228// -------------------------------- SkWuffsFrame implementation
229
230SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
231 : INHERITED(fc->index()),
232 fIOPosition(fc->io_position()),
233 fReportedAlpha(wuffs_blend_to_skia_alpha(fc->blend())) {
234 wuffs_base__rect_ie_u32 r = fc->bounds();
235 this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
236 this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
237 this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
238 this->setBlend(wuffs_blend_to_skia_blend(fc->blend()));
239}
240
241SkCodec::FrameInfo SkWuffsFrame::frameInfo(bool fullyReceived) const {
242 return ((SkCodec::FrameInfo){
243 .fRequiredFrame = getRequiredFrame(),
244 .fDuration = getDuration(),
245 .fFullyReceived = fullyReceived,
246 .fAlphaType = hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType,
247 .fDisposalMethod = getDisposalMethod(),
248 });
249}
250
251uint64_t SkWuffsFrame::ioPosition() const {
252 return fIOPosition;
253}
254
255SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
256 return fReportedAlpha;
257}
258
259// -------------------------------- SkWuffsFrameHolder implementation
260
261void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
262 fCodec = codec;
263 // Initialize SkFrameHolder's (the superclass) fields.
264 fScreenWidth = width;
265 fScreenHeight = height;
266}
267
268const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
269 return fCodec->frame(i);
270};
271
Nigel Tao0185b952018-11-08 10:47:24 +1100272// -------------------------------- SkWuffsSpySampler implementation
273
274void SkWuffsSpySampler::reset() {
275 fFillWidth = 0;
276 fSampleX = 1;
277 this->setSampleY(1);
278}
279
280int SkWuffsSpySampler::sampleX() const {
281 return fSampleX;
282}
283
284int SkWuffsSpySampler::fillWidth() const {
285 return fFillWidth;
286}
287
288int SkWuffsSpySampler::onSetSampleX(int sampleX) {
289 fSampleX = sampleX;
290 return get_scaled_dimension(fImageWidth, sampleX);
291}
292
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400293// -------------------------------- SkWuffsCodec implementation
294
295SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
296 std::unique_ptr<SkStream> stream,
297 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
298 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
299 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
300 size_t workbuf_len,
301 wuffs_base__image_config imgcfg,
302 wuffs_base__pixel_buffer pixbuf,
303 wuffs_base__io_buffer iobuf)
304 : INHERITED(std::move(encodedInfo),
305 skcms_PixelFormat_RGBA_8888,
306 // Pass a nullptr SkStream to the SkCodec constructor. We
307 // manage the stream ourselves, as the default SkCodec behavior
308 // is too trigger-happy on rewinding the stream.
309 nullptr),
Nigel Tao0185b952018-11-08 10:47:24 +1100310 fSpySampler(imgcfg.pixcfg.width()),
311 fFrameHolder(),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400312 fStream(std::move(stream)),
313 fDecoder(std::move(dec)),
314 fPixbufPtr(std::move(pixbuf_ptr)),
315 fWorkbufPtr(std::move(workbuf_ptr)),
316 fWorkbufLen(workbuf_len),
317 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
318 fFrameConfig((wuffs_base__frame_config){}),
319 fPixelBuffer(pixbuf),
320 fIOBuffer((wuffs_base__io_buffer){}),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400321 fIncrDecDst(nullptr),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400322 fIncrDecRowBytes(0),
Nigel Tao0185b952018-11-08 10:47:24 +1100323 fSwizzler(nullptr),
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500324 fColorTableFilled(false),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400325 fNumFullyReceivedFrames(0),
326 fFramesComplete(false),
327 fDecoderIsSuspended(false) {
328 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
Nigel Tao0185b952018-11-08 10:47:24 +1100329 sk_memset32(fColorTable, 0, SK_ARRAY_COUNT(fColorTable));
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400330
331 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
332 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
333 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
334 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
335 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
336 fIOBuffer = ((wuffs_base__io_buffer){
337 .data = ((wuffs_base__slice_u8){
338 .ptr = fBuffer,
339 .len = SK_WUFFS_CODEC_BUFFER_SIZE,
340 }),
341 .meta = iobuf.meta,
342 });
343}
344
345const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
346 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
347 return &fFrames[i];
348 }
349 return nullptr;
350}
351
352SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
353 return SkEncodedImageFormat::kGIF;
354}
355
356SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
357 void* dst,
358 size_t rowBytes,
359 const Options& options,
360 int* rowsDecoded) {
361 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
362 if (result != kSuccess) {
363 return result;
364 }
365 return this->onIncrementalDecode(rowsDecoded);
366}
367
368const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
369 return &fFrameHolder;
370}
371
372SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
373 void* dst,
374 size_t rowBytes,
375 const SkCodec::Options& options) {
376 if (options.fSubset) {
377 return SkCodec::kUnimplemented;
378 }
379 SkCodec::Result result = this->seekFrame(options.fFrameIndex);
380 if (result != SkCodec::kSuccess) {
381 return result;
382 }
383
Nigel Tao0185b952018-11-08 10:47:24 +1100384 fSpySampler.reset();
Nigel Tao0185b952018-11-08 10:47:24 +1100385 fSwizzler = nullptr;
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500386 fColorTableFilled = false;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400387
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500388 const char* status = this->decodeFrameConfig();
389 if (status == nullptr) {
390 fIncrDecDst = static_cast<uint8_t*>(dst);
391 fIncrDecRowBytes = rowBytes;
392 return SkCodec::kSuccess;
393 } else if (status == wuffs_base__suspension__short_read) {
394 return SkCodec::kIncompleteInput;
395 } else {
396 SkCodecPrintf("decodeFrameConfig: %s", status);
397 return SkCodec::kErrorInInput;
398 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400399}
400
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500401static bool independent_frame(SkCodec* codec, int frameIndex) {
402 if (frameIndex == 0) {
403 return true;
404 }
405
406 SkCodec::FrameInfo frameInfo;
407 SkAssertResult(codec->getFrameInfo(frameIndex, &frameInfo));
408 return frameInfo.fRequiredFrame == SkCodec::kNoFrame;
409}
410
411static void blend(uint32_t* dst, const uint32_t* src, int width) {
412 while (width --> 0) {
413 if (*src != 0) {
414 *dst = *src;
415 }
416 src++;
417 dst++;
418 }
419}
420
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400421SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
422 if (!fIncrDecDst) {
423 return SkCodec::kInternalError;
424 }
425
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500426 // In Wuffs, a paletted image is always 1 byte per pixel.
427 static constexpr size_t src_bpp = 1;
428 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500429 int scaledHeight = dstInfo().height();
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500430 const bool independent = independent_frame(this, options().fFrameIndex);
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500431 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
Nigel Tao0185b952018-11-08 10:47:24 +1100432 if (!fSwizzler) {
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500433 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
434 frame_rect.max_excl_x, frame_rect.max_excl_y);
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500435 fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), fColorTable, dstInfo(),
436 this->options(), &bounds);
Nigel Tao0185b952018-11-08 10:47:24 +1100437 fSwizzler->setSampleX(fSpySampler.sampleX());
438 fSwizzler->setSampleY(fSpySampler.sampleY());
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500439 scaledHeight = get_scaled_dimension(dstInfo().height(), fSpySampler.sampleY());
Nigel Tao0185b952018-11-08 10:47:24 +1100440
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500441 // Zero-initialize wuffs' buffer covering the frame rect. This will later be used to
442 // determine how we write to the output, even if the image was incomplete. This ensures
443 // that we do not swizzle uninitialized memory.
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500444 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
445 uint8_t* s = pixels.ptr + (y * pixels.stride) + (frame_rect.min_incl_x * src_bpp);
446 sk_bzero(s, frame_rect.width() * src_bpp);
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500447 }
448
449 // If the frame rect does not fill the output, ensure that those pixels are not
450 // left uninitialized either.
451 if (independent && bounds != this->bounds()) {
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500452 auto fillInfo = dstInfo().makeWH(fSwizzler->fillWidth(), scaledHeight);
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500453 SkSampler::Fill(fillInfo, fIncrDecDst, fIncrDecRowBytes, options().fZeroInitialized);
454 }
Nigel Tao0185b952018-11-08 10:47:24 +1100455 }
456
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400457 // The semantics of *rowsDecoded is: say you have a 10 pixel high image
458 // (both the frame and the image). If you only decoded the first 3 rows,
459 // set this to 3, and then SkCodec (or the caller of incrementalDecode)
460 // would zero-initialize the remaining 7 (unless the memory was already
461 // zero-initialized).
462 //
463 // Now let's say that the image is still 10 pixels high, but the frame is
464 // from row 5 to 9. If you only decoded 3 rows, but you initialized the
465 // first 5, you could return 8, and the caller would zero-initialize the
466 // final 2. For GIF (where a frame can be smaller than the image and can be
467 // interlaced), we just zero-initialize all 10 rows ahead of time and
468 // return the height of the image, so the caller knows it doesn't need to
469 // do anything.
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500470 //
471 // Similarly, if the output is scaled, we zero-initialized all
472 // |scaledHeight| rows (the scaled image height), so we inform the caller
473 // that it doesn't need to do anything.
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400474 if (rowsDecoded) {
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500475 *rowsDecoded = scaledHeight;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400476 }
477
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500478 SkCodec::Result result = SkCodec::kSuccess;
479 const char* status = this->decodeFrame();
480 if (status != nullptr) {
481 if (status == wuffs_base__suspension__short_read) {
482 result = SkCodec::kIncompleteInput;
483 } else {
484 SkCodecPrintf("decodeFrame: %s", status);
485 result = SkCodec::kErrorInInput;
486 }
487
488 if (!independent) {
489 // For a dependent frame, we cannot blend the partial result, since
490 // that will overwrite the contribution from prior frames with all
491 // zeroes that were written to |pixels| above.
492 return result;
493 }
494 }
495
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500496 // If the frame's dirty rect is empty, no need to swizzle.
497 wuffs_base__rect_ie_u32 dirty_rect = wuffs_gif__decoder__frame_dirty_rect(fDecoder.get());
498 if (!dirty_rect.is_empty()) {
499 if (!fColorTableFilled) {
500 fColorTableFilled = true;
501 wuffs_base__slice_u8 palette = fPixelBuffer.palette();
502 SkASSERT(palette.len == 4 * 256);
503 auto proc = choose_pack_color_proc(false, dstInfo().colorType());
504 for (int i = 0; i < 256; i++) {
505 uint8_t* p = palette.ptr + 4 * i;
506 fColorTable[i] = proc(p[3], p[2], p[1], p[0]);
507 }
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500508 }
509
510 std::unique_ptr<uint8_t[]> tmpBuffer;
511 if (!independent) {
512 tmpBuffer.reset(new uint8_t[dstInfo().minRowBytes()]);
513 }
514 const int sampleY = fSwizzler->sampleY();
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500515 for (uint32_t y = dirty_rect.min_incl_y; y < dirty_rect.max_excl_y; y++) {
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500516 int dstY = y;
517 if (sampleY != 1) {
518 if (!fSwizzler->rowNeeded(y)) {
519 continue;
520 }
521 dstY /= sampleY;
522 if (dstY >= scaledHeight) {
523 break;
524 }
525 }
526
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500527 // We don't adjust d by (frame_rect.min_incl_x * dst_bpp) as we
528 // have already accounted for that in swizzleRect, above.
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500529 uint8_t* d = fIncrDecDst + (dstY * fIncrDecRowBytes);
530
531 // The Wuffs model is that the dst buffer is the image, not the frame.
532 // The expectation is that you allocate the buffer once, but re-use it
533 // for the N frames, regardless of each frame's top-left co-ordinate.
534 //
535 // To get from the start (in the X-direction) of the image to the start
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500536 // of the frame, we adjust s by (frame_rect.min_incl_x * src_bpp).
537 //
538 // We adjust (in the X-direction) by the frame rect, not the dirty
539 // rect, because the swizzler (which operates on rows) was
540 // configured with the frame rect's X range.
541 uint8_t* s = pixels.ptr + (y * pixels.stride) + (frame_rect.min_incl_x * src_bpp);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500542 if (independent) {
543 fSwizzler->swizzle(d, s);
544 } else {
545 SkASSERT(tmpBuffer.get());
546 fSwizzler->swizzle(tmpBuffer.get(), s);
547 d = SkTAddOffset<uint8_t>(d, fSwizzler->swizzleOffsetBytes());
548 const auto* swizzled = SkTAddOffset<uint32_t>(tmpBuffer.get(),
549 fSwizzler->swizzleOffsetBytes());
550 blend(reinterpret_cast<uint32_t*>(d), swizzled, fSwizzler->swizzleWidth());
551 }
552 }
553 }
554
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400555 if (result == SkCodec::kSuccess) {
Nigel Tao0185b952018-11-08 10:47:24 +1100556 fSpySampler.reset();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400557 fIncrDecDst = nullptr;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400558 fIncrDecRowBytes = 0;
Nigel Tao0185b952018-11-08 10:47:24 +1100559 fSwizzler = nullptr;
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500560 fColorTableFilled = false;
Nigel Tao0185b952018-11-08 10:47:24 +1100561 } else {
562 // Make fSpySampler return whatever fSwizzler would have for fillWidth.
563 fSpySampler.fFillWidth = fSwizzler->fillWidth();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400564 }
565 return result;
566}
567
568int SkWuffsCodec::onGetFrameCount() {
569 if (!fFramesComplete) {
570 this->readFrames();
571 this->updateNumFullyReceivedFrames();
572 }
573 return fFrames.size();
574}
575
576bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
577 const SkWuffsFrame* f = this->frame(i);
578 if (!f) {
579 return false;
580 }
581 if (frameInfo) {
582 *frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
583 }
584 return true;
585}
586
587int SkWuffsCodec::onGetRepetitionCount() {
588 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
589 // number is how many times to play the loop. Skia's int number is how many
590 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
591 // kRepetitionCountInfinite respectively to mean loop forever.
592 uint32_t n = wuffs_gif__decoder__num_animation_loops(fDecoder.get());
593 if (n == 0) {
594 return SkCodec::kRepetitionCountInfinite;
595 }
596 n--;
597 return n < INT_MAX ? n : INT_MAX;
598}
599
Nigel Tao0185b952018-11-08 10:47:24 +1100600SkSampler* SkWuffsCodec::getSampler(bool createIfNecessary) {
601 // fIncrDst being non-nullptr means that we are between an
602 // onStartIncrementalDecode call and the matching final (successful)
603 // onIncrementalDecode call.
604 if (createIfNecessary || fIncrDecDst) {
605 return &fSpySampler;
606 }
607 return nullptr;
608}
609
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500610bool SkWuffsCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
611 if (!this->INHERITED::conversionSupported(dst, srcIsOpaque, needsColorXform)) {
612 return false;
613 }
614
615 switch (dst.colorType()) {
616 case kRGBA_8888_SkColorType:
617 case kBGRA_8888_SkColorType:
618 return true;
619 default:
620 // FIXME: Add skcms to support F16
621 // FIXME: Add support for 565 on the first frame
622 return false;
623 }
624}
625
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400626void SkWuffsCodec::readFrames() {
627 size_t n = fFrames.size();
628 int i = n ? n - 1 : 0;
629 if (this->seekFrame(i) != SkCodec::kSuccess) {
630 return;
631 }
632
633 // Iterate through the frames, converting from Wuffs'
634 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
635 for (; i < INT_MAX; i++) {
636 const char* status = this->decodeFrameConfig();
637 if (status == nullptr) {
638 // No-op.
639 } else if (status == wuffs_base__warning__end_of_data) {
640 break;
641 } else {
642 return;
643 }
644
645 if (static_cast<size_t>(i) < fFrames.size()) {
646 continue;
647 }
648 fFrames.emplace_back(&fFrameConfig);
649 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
650 fFrameHolder.setAlphaAndRequiredFrame(f);
651 }
652
653 fFramesComplete = true;
654}
655
656SkCodec::Result SkWuffsCodec::seekFrame(int frameIndex) {
657 if (fDecoderIsSuspended) {
658 SkCodec::Result res = this->resetDecoder();
659 if (res != SkCodec::kSuccess) {
660 return res;
661 }
662 }
663
664 uint64_t pos = 0;
665 if (frameIndex < 0) {
666 return SkCodec::kInternalError;
667 } else if (frameIndex == 0) {
668 pos = fFirstFrameIOPosition;
669 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
670 pos = fFrames[frameIndex].ioPosition();
671 } else {
672 return SkCodec::kInternalError;
673 }
674
675 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
676 return SkCodec::kInternalError;
677 }
678 const char* status = wuffs_gif__decoder__restart_frame(fDecoder.get(), frameIndex,
679 fIOBuffer.reader_io_position());
680 if (status != nullptr) {
681 return SkCodec::kInternalError;
682 }
683 return SkCodec::kSuccess;
684}
685
686// An overview of the Wuffs decoding API:
687//
688// An animated image (such as GIF) has an image header and then N frames. The
689// image header gives e.g. the overall image's width and height. Each frame
690// consists of a frame header (e.g. frame rectangle bounds, display duration)
691// and a payload (the pixels).
692//
693// In Wuffs terminology, there is one image config and then N pairs of
694// (frame_config, frame). To decode everything (without knowing N in advance)
695// sequentially:
696// - call wuffs_gif__decoder::decode_image_config
697// - while (true) {
698// - call wuffs_gif__decoder::decode_frame_config
699// - if that returned wuffs_base__warning__end_of_data, break
700// - call wuffs_gif__decoder::decode_frame
701// - }
702//
703// The first argument to each decode_foo method is the destination struct to
704// store the decoded information.
705//
706// For random (instead of sequential) access to an image's frames, call
707// wuffs_gif__decoder::restart_frame to prepare to decode the i'th frame.
708// Essentially, it restores the state to be at the top of the while loop above.
709// The wuffs_base__io_buffer's reader position will also need to be set at the
710// right point in the source data stream. The position for the i'th frame is
711// calculated by the i'th decode_frame_config call. You can only call
712// restart_frame after decode_image_config is called, explicitly or implicitly
713// (see below), as decoding a single frame might require for-all-frames
714// information like the overall image dimensions and the global palette.
715//
716// All of those decode_xxx calls are optional. For example, if
717// decode_image_config is not called, then the first decode_frame_config call
718// will implicitly parse and verify the image header, before parsing the first
719// frame's header. Similarly, you can call only decode_frame N times, without
720// calling decode_image_config or decode_frame_config, if you already know
721// metadata like N and each frame's rectangle bounds by some other means (e.g.
722// this is a first party, statically known image).
723//
724// Specifically, starting with an unknown (but re-windable) GIF image, if you
725// want to just find N (i.e. count the number of frames), you can loop calling
726// only the decode_frame_config method and avoid calling the more expensive
727// decode_frame method. In terms of the underlying GIF image format, this will
728// skip over the LZW-encoded pixel data, avoiding the costly LZW decompression.
729//
730// Those decode_xxx methods are also suspendible. They will return early (with
731// a status code that is_suspendible and therefore isn't is_complete) if there
732// isn't enough source data to complete the operation: an incremental decode.
733// Calling decode_xxx again with additional source data will resume the
734// previous operation, instead of starting a new operation. Calling decode_yyy
735// whilst decode_xxx is suspended will result in an error.
736//
737// Once an error is encountered, whether from invalid source data or from a
738// programming error such as calling decode_yyy while suspended in decode_xxx,
739// all subsequent calls will be no-ops that return an error. To reset the
740// decoder into something that does productive work, memset the entire struct
741// to zero, check the Wuffs version and then, in order to be able to call
742// restart_frame, call decode_image_config. The io_buffer and its associated
743// stream will also need to be rewound.
744
745static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
746 wuffs_base__image_config* imgcfg,
747 wuffs_base__io_buffer* b,
748 SkStream* s) {
749 memset(decoder, 0, sizeof__wuffs_gif__decoder());
750 const char* status = wuffs_gif__decoder__check_wuffs_version(
751 decoder, sizeof__wuffs_gif__decoder(), WUFFS_VERSION);
752 if (status != nullptr) {
753 SkCodecPrintf("check_wuffs_version: %s", status);
754 return SkCodec::kInternalError;
755 }
756 while (true) {
757 status = wuffs_gif__decoder__decode_image_config(decoder, imgcfg, b->reader());
758 if (status == nullptr) {
759 return SkCodec::kSuccess;
760 } else if (status != wuffs_base__suspension__short_read) {
761 SkCodecPrintf("decode_image_config: %s", status);
762 return SkCodec::kErrorInInput;
763 } else if (!fill_buffer(b, s)) {
764 return SkCodec::kIncompleteInput;
765 }
766 }
767}
768
769SkCodec::Result SkWuffsCodec::resetDecoder() {
770 if (!fStream->rewind()) {
771 return SkCodec::kInternalError;
772 }
773 fIOBuffer.meta = ((wuffs_base__io_buffer_meta){});
774
775 SkCodec::Result result =
776 reset_and_decode_image_config(fDecoder.get(), nullptr, &fIOBuffer, fStream.get());
777 if (result == SkCodec::kIncompleteInput) {
778 return SkCodec::kInternalError;
779 } else if (result != SkCodec::kSuccess) {
780 return result;
781 }
782
783 fDecoderIsSuspended = false;
784 return SkCodec::kSuccess;
785}
786
787const char* SkWuffsCodec::decodeFrameConfig() {
788 while (true) {
789 const char* status = wuffs_gif__decoder__decode_frame_config(fDecoder.get(), &fFrameConfig,
790 fIOBuffer.reader());
791 if ((status == wuffs_base__suspension__short_read) &&
792 fill_buffer(&fIOBuffer, fStream.get())) {
793 continue;
794 }
795 fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
796 this->updateNumFullyReceivedFrames();
797 return status;
798 }
799}
800
801const char* SkWuffsCodec::decodeFrame() {
802 while (true) {
803 const char* status =
804 wuffs_gif__decoder__decode_frame(fDecoder.get(), &fPixelBuffer, fIOBuffer.reader(),
805 ((wuffs_base__slice_u8){
806 .ptr = fWorkbufPtr.get(),
807 .len = fWorkbufLen,
808 }),
809 NULL);
810 if ((status == wuffs_base__suspension__short_read) &&
811 fill_buffer(&fIOBuffer, fStream.get())) {
812 continue;
813 }
814 fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
815 this->updateNumFullyReceivedFrames();
816 return status;
817 }
818}
819
820void SkWuffsCodec::updateNumFullyReceivedFrames() {
821 // wuffs_gif__decoder__num_decoded_frames's return value, n, can change
822 // over time, both up and down, as we seek back and forth in the underlying
823 // stream. fNumFullyReceivedFrames is the highest n we've seen.
824 uint64_t n = wuffs_gif__decoder__num_decoded_frames(fDecoder.get());
825 if (fNumFullyReceivedFrames < n) {
826 fNumFullyReceivedFrames = n;
827 }
828}
829
830// -------------------------------- SkWuffsCodec.h functions
831
832bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
833 constexpr const char* gif_ptr = "GIF8";
834 constexpr size_t gif_len = 4;
835 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
836}
837
838std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
839 SkCodec::Result* result) {
840 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
841 wuffs_base__io_buffer iobuf = ((wuffs_base__io_buffer){
842 .data = ((wuffs_base__slice_u8){
843 .ptr = buffer,
844 .len = SK_WUFFS_CODEC_BUFFER_SIZE,
845 }),
846 .meta = ((wuffs_base__io_buffer_meta){}),
847 });
848 wuffs_base__image_config imgcfg = ((wuffs_base__image_config){});
849
850 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
851 // the wuffs_base__etc types, the sizeof a file format specific type like
852 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
853 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
854 // to an opaque type: a private implementation detail. The API is always
855 // "set_foo(p, etc)" and not "p->foo = etc".
856 //
857 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
858 //
859 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
860 // the struct at compile time). Instead, we use sk_malloc_canfail, with
861 // sizeof__wuffs_gif__decoder returning the appropriate value for the
862 // (statically or dynamically) linked version of the Wuffs library.
863 //
864 // As a C (not C++) library, none of the Wuffs types have constructors or
865 // destructors.
866 //
867 // In RAII style, we can still use std::unique_ptr with these pointers, but
868 // we pair the pointer with sk_free instead of C++'s delete.
869 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
870 if (!decoder_raw) {
871 *result = SkCodec::kInternalError;
872 return nullptr;
873 }
874 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
875 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
876
877 SkCodec::Result reset_result =
878 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
879 if (reset_result != SkCodec::kSuccess) {
880 *result = reset_result;
881 return nullptr;
882 }
883
884 uint32_t width = imgcfg.pixcfg.width();
885 uint32_t height = imgcfg.pixcfg.height();
886 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
887 *result = SkCodec::kInvalidInput;
888 return nullptr;
889 }
890
891 uint64_t workbuf_len = imgcfg.workbuf_len().max_incl;
892 void* workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
893 if (!workbuf_ptr_raw) {
894 *result = SkCodec::kInternalError;
895 return nullptr;
896 }
897 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
898 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
899
900 uint64_t pixbuf_len = imgcfg.pixcfg.pixbuf_len();
901 void* pixbuf_ptr_raw = pixbuf_len <= SIZE_MAX ? sk_malloc_canfail(pixbuf_len) : nullptr;
902 if (!pixbuf_ptr_raw) {
903 *result = SkCodec::kInternalError;
904 return nullptr;
905 }
906 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr(
907 reinterpret_cast<uint8_t*>(pixbuf_ptr_raw), &sk_free);
908 wuffs_base__pixel_buffer pixbuf = ((wuffs_base__pixel_buffer){});
909
910 const char* status = pixbuf.set_from_slice(&imgcfg.pixcfg, ((wuffs_base__slice_u8){
911 .ptr = pixbuf_ptr.get(),
912 .len = pixbuf_len,
913 }));
914 if (status != nullptr) {
915 SkCodecPrintf("set_from_slice: %s", status);
916 *result = SkCodec::kInternalError;
917 return nullptr;
918 }
919
920 // In Skia's API, the alpha we calculate here and return is only for the
921 // first frame.
922 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
923 : SkEncodedInfo::kBinary_Alpha;
924
925 SkEncodedInfo encodedInfo =
926 SkEncodedInfo::Make(width, height, SkEncodedInfo::kPalette_Color, alpha, 8);
927
928 *result = SkCodec::kSuccess;
929 return std::unique_ptr<SkCodec>(new SkWuffsCodec(
930 std::move(encodedInfo), std::move(stream), std::move(decoder), std::move(pixbuf_ptr),
931 std::move(workbuf_ptr), workbuf_len, imgcfg, pixbuf, iobuf));
932}