1 /*
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 "src/codec/SkWuffsCodec.h"
9
10 #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"
20
21 #include <limits.h>
22
23 // 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".
28 #if defined(WUFFS_IMPLEMENTATION)
29 #error "SkWuffsCodec should not #define WUFFS_IMPLEMENTATION"
30 #endif
31 #include "wuffs-v0.2.c"
32 #if WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT < 1776
33 #error "Wuffs version is too old. Upgrade to the latest version."
34 #endif
35
36 #define SK_WUFFS_CODEC_BUFFER_SIZE 4096
37
fill_buffer(wuffs_base__io_buffer * b,SkStream * s)38 static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
39 b->compact();
40 size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
41 b->meta.wi += num_read;
42 b->meta.closed = s->isAtEnd();
43 return num_read > 0;
44 }
45
seek_buffer(wuffs_base__io_buffer * b,SkStream * s,uint64_t pos)46 static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
47 // Try to re-position the io_buffer's meta.ri read-index first, which is
48 // cheaper than seeking in the backing SkStream.
49 if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
50 b->meta.ri = pos - b->meta.pos;
51 return true;
52 }
53 // Seek in the backing SkStream.
54 if ((pos > SIZE_MAX) || (!s->seek(pos))) {
55 return false;
56 }
57 b->meta.wi = 0;
58 b->meta.ri = 0;
59 b->meta.pos = pos;
60 b->meta.closed = false;
61 return true;
62 }
63
wuffs_blend_to_skia_alpha(wuffs_base__animation_blend w)64 static SkEncodedInfo::Alpha wuffs_blend_to_skia_alpha(wuffs_base__animation_blend w) {
65 return (w == WUFFS_BASE__ANIMATION_BLEND__OPAQUE) ? SkEncodedInfo::kOpaque_Alpha
66 : SkEncodedInfo::kUnpremul_Alpha;
67 }
68
wuffs_blend_to_skia_blend(wuffs_base__animation_blend w)69 static SkCodecAnimation::Blend wuffs_blend_to_skia_blend(wuffs_base__animation_blend w) {
70 return (w == WUFFS_BASE__ANIMATION_BLEND__SRC) ? SkCodecAnimation::Blend::kBG
71 : SkCodecAnimation::Blend::kPriorFrame;
72 }
73
wuffs_disposal_to_skia_disposal(wuffs_base__animation_disposal w)74 static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
75 wuffs_base__animation_disposal w) {
76 switch (w) {
77 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
78 return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
79 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
80 return SkCodecAnimation::DisposalMethod::kRestorePrevious;
81 default:
82 return SkCodecAnimation::DisposalMethod::kKeep;
83 }
84 }
85
86 // -------------------------------- Class definitions
87
88 class SkWuffsCodec;
89
90 class SkWuffsFrame final : public SkFrame {
91 public:
92 SkWuffsFrame(wuffs_base__frame_config* fc);
93
94 SkCodec::FrameInfo frameInfo(bool fullyReceived) const;
95 uint64_t ioPosition() const;
96
97 // SkFrame overrides.
98 SkEncodedInfo::Alpha onReportedAlpha() const override;
99
100 private:
101 uint64_t fIOPosition;
102 SkEncodedInfo::Alpha fReportedAlpha;
103
104 typedef SkFrame INHERITED;
105 };
106
107 // SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
108 // SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
109 // inherit from both SkCodec and SkFrameHolder, and Skia style discourages
110 // multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
111 class SkWuffsFrameHolder final : public SkFrameHolder {
112 public:
SkWuffsFrameHolder()113 SkWuffsFrameHolder() : INHERITED() {}
114
115 void init(SkWuffsCodec* codec, int width, int height);
116
117 // SkFrameHolder overrides.
118 const SkFrame* onGetFrame(int i) const override;
119
120 private:
121 const SkWuffsCodec* fCodec;
122
123 typedef SkFrameHolder INHERITED;
124 };
125
126 class SkWuffsCodec final : public SkScalingCodec {
127 public:
128 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
129 std::unique_ptr<SkStream> stream,
130 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
131 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
132 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
133 size_t workbuf_len,
134 wuffs_base__image_config imgcfg,
135 wuffs_base__pixel_buffer pixbuf,
136 wuffs_base__io_buffer iobuf);
137
138 const SkWuffsFrame* frame(int i) const;
139
140 private:
141 // SkCodec overrides.
142 SkEncodedImageFormat onGetEncodedFormat() const override;
143 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
144 const SkFrameHolder* getFrameHolder() const override;
145 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
146 void* dst,
147 size_t rowBytes,
148 const SkCodec::Options& options) override;
149 Result onIncrementalDecode(int* rowsDecoded) override;
150 int onGetFrameCount() override;
151 bool onGetFrameInfo(int, FrameInfo*) const override;
152 int onGetRepetitionCount() override;
153
154 void readFrames();
155 Result seekFrame(int frameIndex);
156
157 Result resetDecoder();
158 const char* decodeFrameConfig();
159 const char* decodeFrame();
160 void updateNumFullyReceivedFrames();
161
162 SkWuffsFrameHolder fFrameHolder;
163 std::unique_ptr<SkStream> fStream;
164 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoder;
165 std::unique_ptr<uint8_t, decltype(&sk_free)> fPixbufPtr;
166 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
167 size_t fWorkbufLen;
168
169 const uint64_t fFirstFrameIOPosition;
170 wuffs_base__frame_config fFrameConfig;
171 wuffs_base__pixel_buffer fPixelBuffer;
172 wuffs_base__io_buffer fIOBuffer;
173
174 // Incremental decoding state.
175 uint8_t* fIncrDecDst;
176 size_t fIncrDecRowBytes;
177 bool fFirstCallToIncrementalDecode;
178
179 uint64_t fNumFullyReceivedFrames;
180 std::vector<SkWuffsFrame> fFrames;
181 bool fFramesComplete;
182
183 // If calling an fDecoder method returns an incomplete status, then
184 // fDecoder is suspended in a coroutine (i.e. waiting on I/O or halted on a
185 // non-recoverable error). To keep its internal proof-of-safety invariants
186 // consistent, there's only two things you can safely do with a suspended
187 // Wuffs object: resume the coroutine, or reset all state (memset to zero
188 // and start again).
189 //
190 // If fDecoderIsSuspended, and we aren't sure that we're going to resume
191 // the coroutine, then we will need to call this->resetDecoder before
192 // calling other fDecoder methods.
193 bool fDecoderIsSuspended;
194
195 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
196
197 typedef SkScalingCodec INHERITED;
198 };
199
200 // -------------------------------- SkWuffsFrame implementation
201
SkWuffsFrame(wuffs_base__frame_config * fc)202 SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
203 : INHERITED(fc->index()),
204 fIOPosition(fc->io_position()),
205 fReportedAlpha(wuffs_blend_to_skia_alpha(fc->blend())) {
206 wuffs_base__rect_ie_u32 r = fc->bounds();
207 this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
208 this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
209 this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
210 this->setBlend(wuffs_blend_to_skia_blend(fc->blend()));
211 }
212
frameInfo(bool fullyReceived) const213 SkCodec::FrameInfo SkWuffsFrame::frameInfo(bool fullyReceived) const {
214 SkCodec::FrameInfo ret;
215 ret.fRequiredFrame = getRequiredFrame();
216 ret.fDuration = getDuration();
217 ret.fFullyReceived = fullyReceived;
218 ret.fAlphaType = hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
219 ret.fDisposalMethod = getDisposalMethod();
220 return ret;
221 }
222
ioPosition() const223 uint64_t SkWuffsFrame::ioPosition() const {
224 return fIOPosition;
225 }
226
onReportedAlpha() const227 SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
228 return fReportedAlpha;
229 }
230
231 // -------------------------------- SkWuffsFrameHolder implementation
232
init(SkWuffsCodec * codec,int width,int height)233 void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
234 fCodec = codec;
235 // Initialize SkFrameHolder's (the superclass) fields.
236 fScreenWidth = width;
237 fScreenHeight = height;
238 }
239
onGetFrame(int i) const240 const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
241 return fCodec->frame(i);
242 };
243
244 // -------------------------------- SkWuffsCodec implementation
245
SkWuffsCodec(SkEncodedInfo && encodedInfo,std::unique_ptr<SkStream> stream,std::unique_ptr<wuffs_gif__decoder,decltype(& sk_free) > dec,std::unique_ptr<uint8_t,decltype(& sk_free) > pixbuf_ptr,std::unique_ptr<uint8_t,decltype(& sk_free) > workbuf_ptr,size_t workbuf_len,wuffs_base__image_config imgcfg,wuffs_base__pixel_buffer pixbuf,wuffs_base__io_buffer iobuf)246 SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
247 std::unique_ptr<SkStream> stream,
248 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
249 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
250 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
251 size_t workbuf_len,
252 wuffs_base__image_config imgcfg,
253 wuffs_base__pixel_buffer pixbuf,
254 wuffs_base__io_buffer iobuf)
255 : INHERITED(std::move(encodedInfo),
256 skcms_PixelFormat_RGBA_8888,
257 // Pass a nullptr SkStream to the SkCodec constructor. We
258 // manage the stream ourselves, as the default SkCodec behavior
259 // is too trigger-happy on rewinding the stream.
260 nullptr),
261 fFrameHolder(),
262 fStream(std::move(stream)),
263 fDecoder(std::move(dec)),
264 fPixbufPtr(std::move(pixbuf_ptr)),
265 fWorkbufPtr(std::move(workbuf_ptr)),
266 fWorkbufLen(workbuf_len),
267 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
268 fFrameConfig(wuffs_base__null_frame_config()),
269 fPixelBuffer(pixbuf),
270 fIOBuffer(wuffs_base__null_io_buffer()),
271 fIncrDecDst(nullptr),
272 fIncrDecRowBytes(0),
273 fFirstCallToIncrementalDecode(false),
274 fNumFullyReceivedFrames(0),
275 fFramesComplete(false),
276 fDecoderIsSuspended(false) {
277 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
278
279 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
280 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
281 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
282 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
283 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
284 fIOBuffer.data = wuffs_base__make_slice_u8(fBuffer, SK_WUFFS_CODEC_BUFFER_SIZE);
285 fIOBuffer.meta = iobuf.meta;
286 }
287
frame(int i) const288 const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
289 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
290 return &fFrames[i];
291 }
292 return nullptr;
293 }
294
onGetEncodedFormat() const295 SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
296 return SkEncodedImageFormat::kGIF;
297 }
298
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,const Options & options,int * rowsDecoded)299 SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
300 void* dst,
301 size_t rowBytes,
302 const Options& options,
303 int* rowsDecoded) {
304 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
305 if (result != kSuccess) {
306 return result;
307 }
308 return this->onIncrementalDecode(rowsDecoded);
309 }
310
getFrameHolder() const311 const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
312 return &fFrameHolder;
313 }
314
onStartIncrementalDecode(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,const SkCodec::Options & options)315 SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
316 void* dst,
317 size_t rowBytes,
318 const SkCodec::Options& options) {
319 if (!dst) {
320 return SkCodec::kInvalidParameters;
321 }
322 if (options.fSubset) {
323 return SkCodec::kUnimplemented;
324 }
325 if (options.fFrameIndex > 0 && SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
326 return SkCodec::kInvalidConversion;
327 }
328 SkCodec::Result result = this->seekFrame(options.fFrameIndex);
329 if (result != SkCodec::kSuccess) {
330 return result;
331 }
332
333 const char* status = this->decodeFrameConfig();
334 if (status == wuffs_base__suspension__short_read) {
335 return SkCodec::kIncompleteInput;
336 } else if (status != nullptr) {
337 SkCodecPrintf("decodeFrameConfig: %s", status);
338 return SkCodec::kErrorInInput;
339 }
340
341 uint32_t src_bits_per_pixel =
342 wuffs_base__pixel_format__bits_per_pixel(fPixelBuffer.pixcfg.pixel_format());
343 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
344 return SkCodec::kInternalError;
345 }
346 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
347
348 // Zero-initialize Wuffs' buffer covering the frame rect.
349 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
350 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
351 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
352 sk_bzero(pixels.ptr + (y * pixels.stride) + (frame_rect.min_incl_x * src_bytes_per_pixel),
353 frame_rect.width() * src_bytes_per_pixel);
354 }
355
356 fIncrDecDst = static_cast<uint8_t*>(dst);
357 fIncrDecRowBytes = rowBytes;
358 fFirstCallToIncrementalDecode = true;
359 return SkCodec::kSuccess;
360 }
361
to_alpha_type(bool opaque)362 static SkAlphaType to_alpha_type(bool opaque) {
363 return opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
364 }
365
onIncrementalDecode(int * rowsDecoded)366 SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
367 if (!fIncrDecDst) {
368 return SkCodec::kInternalError;
369 }
370
371 SkCodec::Result result = SkCodec::kSuccess;
372 const char* status = this->decodeFrame();
373 bool independent;
374 SkAlphaType alphaType;
375 const int index = options().fFrameIndex;
376 if (index == 0) {
377 independent = true;
378 alphaType = to_alpha_type(getEncodedInfo().opaque());
379 } else {
380 const SkWuffsFrame* f = this->frame(index);
381 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
382 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
383 }
384 if (status != nullptr) {
385 if (status == wuffs_base__suspension__short_read) {
386 result = SkCodec::kIncompleteInput;
387 } else {
388 SkCodecPrintf("decodeFrame: %s", status);
389 result = SkCodec::kErrorInInput;
390 }
391
392 if (!independent) {
393 // For a dependent frame, we cannot blend the partial result, since
394 // that will overwrite the contribution from prior frames.
395 return result;
396 }
397 }
398
399 uint32_t src_bits_per_pixel =
400 wuffs_base__pixel_format__bits_per_pixel(fPixelBuffer.pixcfg.pixel_format());
401 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
402 return SkCodec::kInternalError;
403 }
404 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
405
406 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
407 if (fFirstCallToIncrementalDecode) {
408 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
409 return SkCodec::kInternalError;
410 }
411
412 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
413 frame_rect.max_excl_x, frame_rect.max_excl_y);
414
415 // If the frame rect does not fill the output, ensure that those pixels are not
416 // left uninitialized.
417 if (independent && (bounds != this->bounds() || result != kSuccess)) {
418 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes,
419 options().fZeroInitialized);
420 }
421 fFirstCallToIncrementalDecode = false;
422 } else {
423 // Existing clients intend to only show frames beyond the first if they
424 // are complete (based on FrameInfo::fFullyReceived), since it might
425 // look jarring to draw a partial frame over an existing frame. If they
426 // changed their behavior and expected to continue decoding a partial
427 // frame after the first one, we'll need to update our blending code.
428 // Otherwise, if the frame were interlaced and not independent, the
429 // second pass may have an overlapping dirty_rect with the first,
430 // resulting in blending with the first pass.
431 SkASSERT(index == 0);
432 }
433
434 if (rowsDecoded) {
435 *rowsDecoded = dstInfo().height();
436 }
437
438 // If the frame's dirty rect is empty, no need to swizzle.
439 wuffs_base__rect_ie_u32 dirty_rect = fDecoder->frame_dirty_rect();
440 if (!dirty_rect.is_empty()) {
441 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
442
443 // The Wuffs model is that the dst buffer is the image, not the frame.
444 // The expectation is that you allocate the buffer once, but re-use it
445 // for the N frames, regardless of each frame's top-left co-ordinate.
446 //
447 // To get from the start (in the X-direction) of the image to the start
448 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
449 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride)
450 + (dirty_rect.min_incl_x * src_bytes_per_pixel);
451
452 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
453 // used for other formats that might have one, we will need to transform from profiles that
454 // do not have corresponding SkColorSpaces.
455 SkASSERT(!getEncodedInfo().profile());
456
457 auto srcInfo = getInfo().makeWH(dirty_rect.width(), dirty_rect.height())
458 .makeAlphaType(alphaType);
459 SkBitmap src;
460 src.installPixels(srcInfo, s, pixels.stride);
461 SkPaint paint;
462 if (independent) {
463 paint.setBlendMode(SkBlendMode::kSrc);
464 }
465
466 SkDraw draw;
467 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
468 SkMatrix matrix = SkMatrix::MakeRectToRect(SkRect::Make(this->dimensions()),
469 SkRect::Make(this->dstInfo().dimensions()),
470 SkMatrix::kFill_ScaleToFit);
471 draw.fMatrix = &matrix;
472 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
473 draw.fRC = &rc;
474
475 SkMatrix translate = SkMatrix::MakeTrans(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
476 draw.drawBitmap(src, translate, nullptr, paint);
477 }
478
479 if (result == SkCodec::kSuccess) {
480 fIncrDecDst = nullptr;
481 fIncrDecRowBytes = 0;
482 }
483 return result;
484 }
485
onGetFrameCount()486 int SkWuffsCodec::onGetFrameCount() {
487 // It is valid, in terms of the SkCodec API, to call SkCodec::getFrameCount
488 // while in an incremental decode (after onStartIncrementalDecode returns
489 // and before onIncrementalDecode returns kSuccess).
490 //
491 // We should not advance the SkWuffsCodec' stream while doing so, even
492 // though other SkCodec implementations can return increasing values from
493 // onGetFrameCount when given more data. If we tried to do so, the
494 // subsequent resume of the incremental decode would continue reading from
495 // a different position in the I/O stream, leading to an incorrect error.
496 //
497 // Other SkCodec implementations can move the stream forward during
498 // onGetFrameCount because they assume that the stream is rewindable /
499 // seekable. For example, an alternative GIF implementation may choose to
500 // store, for each frame walked past when merely counting the number of
501 // frames, the I/O position of each of the frame's GIF data blocks. (A GIF
502 // frame's compressed data can have multiple data blocks, each at most 255
503 // bytes in length). Obviously, this can require O(numberOfFrames) extra
504 // memory to store these I/O positions. The constant factor is small, but
505 // it's still O(N), not O(1).
506 //
507 // Wuffs and SkWuffsCodec tries to minimize relying on the rewindable /
508 // seekable assumption. By design, Wuffs per se aims for O(1) memory use
509 // (after any pixel buffers are allocated) instead of O(N), and its I/O
510 // type, wuffs_base__io_buffer, is not necessarily rewindable or seekable.
511 //
512 // The Wuffs API provides a limited, optional form of seeking, to the start
513 // of an animation frame's data, but does not provide arbitrary save and
514 // load of its internal state whilst in the middle of an animation frame.
515 bool incrementalDecodeIsInProgress = fIncrDecDst != nullptr;
516
517 if (!fFramesComplete && !incrementalDecodeIsInProgress) {
518 this->readFrames();
519 this->updateNumFullyReceivedFrames();
520 }
521 return fFrames.size();
522 }
523
onGetFrameInfo(int i,SkCodec::FrameInfo * frameInfo) const524 bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
525 const SkWuffsFrame* f = this->frame(i);
526 if (!f) {
527 return false;
528 }
529 if (frameInfo) {
530 *frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
531 }
532 return true;
533 }
534
onGetRepetitionCount()535 int SkWuffsCodec::onGetRepetitionCount() {
536 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
537 // number is how many times to play the loop. Skia's int number is how many
538 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
539 // kRepetitionCountInfinite respectively to mean loop forever.
540 uint32_t n = fDecoder->num_animation_loops();
541 if (n == 0) {
542 return SkCodec::kRepetitionCountInfinite;
543 }
544 n--;
545 return n < INT_MAX ? n : INT_MAX;
546 }
547
readFrames()548 void SkWuffsCodec::readFrames() {
549 size_t n = fFrames.size();
550 int i = n ? n - 1 : 0;
551 if (this->seekFrame(i) != SkCodec::kSuccess) {
552 return;
553 }
554
555 // Iterate through the frames, converting from Wuffs'
556 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
557 for (; i < INT_MAX; i++) {
558 const char* status = this->decodeFrameConfig();
559 if (status == nullptr) {
560 // No-op.
561 } else if (status == wuffs_base__warning__end_of_data) {
562 break;
563 } else {
564 return;
565 }
566
567 if (static_cast<size_t>(i) < fFrames.size()) {
568 continue;
569 }
570 fFrames.emplace_back(&fFrameConfig);
571 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
572 fFrameHolder.setAlphaAndRequiredFrame(f);
573 }
574
575 fFramesComplete = true;
576 }
577
seekFrame(int frameIndex)578 SkCodec::Result SkWuffsCodec::seekFrame(int frameIndex) {
579 if (fDecoderIsSuspended) {
580 SkCodec::Result res = this->resetDecoder();
581 if (res != SkCodec::kSuccess) {
582 return res;
583 }
584 }
585
586 uint64_t pos = 0;
587 if (frameIndex < 0) {
588 return SkCodec::kInternalError;
589 } else if (frameIndex == 0) {
590 pos = fFirstFrameIOPosition;
591 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
592 pos = fFrames[frameIndex].ioPosition();
593 } else {
594 return SkCodec::kInternalError;
595 }
596
597 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
598 return SkCodec::kInternalError;
599 }
600 const char* status = fDecoder->restart_frame(frameIndex, fIOBuffer.reader_io_position());
601 if (status != nullptr) {
602 return SkCodec::kInternalError;
603 }
604 return SkCodec::kSuccess;
605 }
606
607 // An overview of the Wuffs decoding API:
608 //
609 // An animated image (such as GIF) has an image header and then N frames. The
610 // image header gives e.g. the overall image's width and height. Each frame
611 // consists of a frame header (e.g. frame rectangle bounds, display duration)
612 // and a payload (the pixels).
613 //
614 // In Wuffs terminology, there is one image config and then N pairs of
615 // (frame_config, frame). To decode everything (without knowing N in advance)
616 // sequentially:
617 // - call wuffs_gif__decoder::decode_image_config
618 // - while (true) {
619 // - call wuffs_gif__decoder::decode_frame_config
620 // - if that returned wuffs_base__warning__end_of_data, break
621 // - call wuffs_gif__decoder::decode_frame
622 // - }
623 //
624 // The first argument to each decode_foo method is the destination struct to
625 // store the decoded information.
626 //
627 // For random (instead of sequential) access to an image's frames, call
628 // wuffs_gif__decoder::restart_frame to prepare to decode the i'th frame.
629 // Essentially, it restores the state to be at the top of the while loop above.
630 // The wuffs_base__io_buffer's reader position will also need to be set at the
631 // right point in the source data stream. The position for the i'th frame is
632 // calculated by the i'th decode_frame_config call. You can only call
633 // restart_frame after decode_image_config is called, explicitly or implicitly
634 // (see below), as decoding a single frame might require for-all-frames
635 // information like the overall image dimensions and the global palette.
636 //
637 // All of those decode_xxx calls are optional. For example, if
638 // decode_image_config is not called, then the first decode_frame_config call
639 // will implicitly parse and verify the image header, before parsing the first
640 // frame's header. Similarly, you can call only decode_frame N times, without
641 // calling decode_image_config or decode_frame_config, if you already know
642 // metadata like N and each frame's rectangle bounds by some other means (e.g.
643 // this is a first party, statically known image).
644 //
645 // Specifically, starting with an unknown (but re-windable) GIF image, if you
646 // want to just find N (i.e. count the number of frames), you can loop calling
647 // only the decode_frame_config method and avoid calling the more expensive
648 // decode_frame method. In terms of the underlying GIF image format, this will
649 // skip over the LZW-encoded pixel data, avoiding the costly LZW decompression.
650 //
651 // Those decode_xxx methods are also suspendible. They will return early (with
652 // a status code that is_suspendible and therefore isn't is_complete) if there
653 // isn't enough source data to complete the operation: an incremental decode.
654 // Calling decode_xxx again with additional source data will resume the
655 // previous operation, instead of starting a new operation. Calling decode_yyy
656 // whilst decode_xxx is suspended will result in an error.
657 //
658 // Once an error is encountered, whether from invalid source data or from a
659 // programming error such as calling decode_yyy while suspended in decode_xxx,
660 // all subsequent calls will be no-ops that return an error. To reset the
661 // decoder into something that does productive work, memset the entire struct
662 // to zero, check the Wuffs version and then, in order to be able to call
663 // restart_frame, call decode_image_config. The io_buffer and its associated
664 // stream will also need to be rewound.
665
reset_and_decode_image_config(wuffs_gif__decoder * decoder,wuffs_base__image_config * imgcfg,wuffs_base__io_buffer * b,SkStream * s)666 static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
667 wuffs_base__image_config* imgcfg,
668 wuffs_base__io_buffer* b,
669 SkStream* s) {
670 // Calling decoder->initialize will memset it to zero.
671 const char* status = decoder->initialize(sizeof__wuffs_gif__decoder(), WUFFS_VERSION, 0);
672 if (status != nullptr) {
673 SkCodecPrintf("initialize: %s", status);
674 return SkCodec::kInternalError;
675 }
676 while (true) {
677 status = decoder->decode_image_config(imgcfg, b->reader());
678 if (status == nullptr) {
679 break;
680 } else if (status != wuffs_base__suspension__short_read) {
681 SkCodecPrintf("decode_image_config: %s", status);
682 return SkCodec::kErrorInInput;
683 } else if (!fill_buffer(b, s)) {
684 return SkCodec::kIncompleteInput;
685 }
686 }
687
688 // A GIF image's natural color model is indexed color: 1 byte per pixel,
689 // indexing a 256-element palette.
690 //
691 // For Skia, we override that to decode to 4 bytes per pixel, BGRA or RGBA.
692 wuffs_base__pixel_format pixfmt = 0;
693 switch (kN32_SkColorType) {
694 case kBGRA_8888_SkColorType:
695 pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
696 break;
697 case kRGBA_8888_SkColorType:
698 pixfmt = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
699 break;
700 default:
701 return SkCodec::kInternalError;
702 }
703 if (imgcfg) {
704 imgcfg->pixcfg.set(pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, imgcfg->pixcfg.width(),
705 imgcfg->pixcfg.height());
706 }
707
708 return SkCodec::kSuccess;
709 }
710
resetDecoder()711 SkCodec::Result SkWuffsCodec::resetDecoder() {
712 if (!fStream->rewind()) {
713 return SkCodec::kInternalError;
714 }
715 fIOBuffer.meta = wuffs_base__null_io_buffer_meta();
716
717 SkCodec::Result result =
718 reset_and_decode_image_config(fDecoder.get(), nullptr, &fIOBuffer, fStream.get());
719 if (result == SkCodec::kIncompleteInput) {
720 return SkCodec::kInternalError;
721 } else if (result != SkCodec::kSuccess) {
722 return result;
723 }
724
725 fDecoderIsSuspended = false;
726 return SkCodec::kSuccess;
727 }
728
decodeFrameConfig()729 const char* SkWuffsCodec::decodeFrameConfig() {
730 while (true) {
731 const char* status = fDecoder->decode_frame_config(&fFrameConfig, &fIOBuffer);
732 if ((status == wuffs_base__suspension__short_read) &&
733 fill_buffer(&fIOBuffer, fStream.get())) {
734 continue;
735 }
736 fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
737 this->updateNumFullyReceivedFrames();
738 return status;
739 }
740 }
741
decodeFrame()742 const char* SkWuffsCodec::decodeFrame() {
743 while (true) {
744 const char* status =
745 fDecoder->decode_frame(&fPixelBuffer, &fIOBuffer,
746 wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen), NULL);
747 if ((status == wuffs_base__suspension__short_read) &&
748 fill_buffer(&fIOBuffer, fStream.get())) {
749 continue;
750 }
751 fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
752 this->updateNumFullyReceivedFrames();
753 return status;
754 }
755 }
756
updateNumFullyReceivedFrames()757 void SkWuffsCodec::updateNumFullyReceivedFrames() {
758 // num_decoded_frames's return value, n, can change over time, both up and
759 // down, as we seek back and forth in the underlying stream.
760 // fNumFullyReceivedFrames is the highest n we've seen.
761 uint64_t n = fDecoder->num_decoded_frames();
762 if (fNumFullyReceivedFrames < n) {
763 fNumFullyReceivedFrames = n;
764 }
765 }
766
767 // -------------------------------- SkWuffsCodec.h functions
768
SkWuffsCodec_IsFormat(const void * buf,size_t bytesRead)769 bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
770 constexpr const char* gif_ptr = "GIF8";
771 constexpr size_t gif_len = 4;
772 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
773 }
774
SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,SkCodec::Result * result)775 std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
776 SkCodec::Result* result) {
777 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
778 wuffs_base__io_buffer iobuf =
779 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
780 wuffs_base__null_io_buffer_meta());
781 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
782
783 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
784 // the wuffs_base__etc types, the sizeof a file format specific type like
785 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
786 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
787 // to an opaque type: a private implementation detail. The API is always
788 // "set_foo(p, etc)" and not "p->foo = etc".
789 //
790 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
791 //
792 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
793 // the struct at compile time). Instead, we use sk_malloc_canfail, with
794 // sizeof__wuffs_gif__decoder returning the appropriate value for the
795 // (statically or dynamically) linked version of the Wuffs library.
796 //
797 // As a C (not C++) library, none of the Wuffs types have constructors or
798 // destructors.
799 //
800 // In RAII style, we can still use std::unique_ptr with these pointers, but
801 // we pair the pointer with sk_free instead of C++'s delete.
802 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
803 if (!decoder_raw) {
804 *result = SkCodec::kInternalError;
805 return nullptr;
806 }
807 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
808 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
809
810 SkCodec::Result reset_result =
811 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
812 if (reset_result != SkCodec::kSuccess) {
813 *result = reset_result;
814 return nullptr;
815 }
816
817 uint32_t width = imgcfg.pixcfg.width();
818 uint32_t height = imgcfg.pixcfg.height();
819 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
820 *result = SkCodec::kInvalidInput;
821 return nullptr;
822 }
823
824 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
825 void* workbuf_ptr_raw = nullptr;
826 if (workbuf_len) {
827 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
828 if (!workbuf_ptr_raw) {
829 *result = SkCodec::kInternalError;
830 return nullptr;
831 }
832 }
833 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
834 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
835
836 uint64_t pixbuf_len = imgcfg.pixcfg.pixbuf_len();
837 void* pixbuf_ptr_raw = pixbuf_len <= SIZE_MAX ? sk_malloc_canfail(pixbuf_len) : nullptr;
838 if (!pixbuf_ptr_raw) {
839 *result = SkCodec::kInternalError;
840 return nullptr;
841 }
842 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr(
843 reinterpret_cast<uint8_t*>(pixbuf_ptr_raw), &sk_free);
844 wuffs_base__pixel_buffer pixbuf = wuffs_base__null_pixel_buffer();
845
846 const char* status = pixbuf.set_from_slice(
847 &imgcfg.pixcfg, wuffs_base__make_slice_u8(pixbuf_ptr.get(), SkToSizeT(pixbuf_len)));
848 if (status != nullptr) {
849 SkCodecPrintf("set_from_slice: %s", status);
850 *result = SkCodec::kInternalError;
851 return nullptr;
852 }
853
854 SkEncodedInfo::Color color =
855 (imgcfg.pixcfg.pixel_format() == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
856 ? SkEncodedInfo::kBGRA_Color
857 : SkEncodedInfo::kRGBA_Color;
858
859 // In Skia's API, the alpha we calculate here and return is only for the
860 // first frame.
861 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
862 : SkEncodedInfo::kBinary_Alpha;
863
864 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
865
866 *result = SkCodec::kSuccess;
867 return std::unique_ptr<SkCodec>(new SkWuffsCodec(
868 std::move(encodedInfo), std::move(stream), std::move(decoder), std::move(pixbuf_ptr),
869 std::move(workbuf_ptr), workbuf_len, imgcfg, pixbuf, iobuf));
870 }
871