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