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/codec/SkCodecAnimation.h"
11 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkBitmap.h"
13 #include "include/core/SkBlendMode.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkEncodedImageFormat.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkPixmap.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkSamplingOptions.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkStream.h"
24 #include "include/core/SkTypes.h"
25 #include "include/private/SkEncodedInfo.h"
26 #include "include/private/base/SkMalloc.h"
27 #include "include/private/base/SkTo.h"
28 #include "modules/skcms/skcms.h"
29 #include "src/codec/SkCodecPriv.h"
30 #include "src/codec/SkFrameHolder.h"
31 #include "src/codec/SkSampler.h"
32 #include "src/codec/SkScalingCodec.h"
33 #include "src/core/SkDraw.h"
34 #include "src/core/SkMatrixProvider.h"
35 #include "src/core/SkRasterClip.h"
36 #include "src/core/SkStreamPriv.h"
37
38 #include <climits>
39 #include <cstdint>
40 #include <cstring>
41 #include <utility>
42 #include <vector>
43
44 // Documentation on the Wuffs language and standard library (in general) and
45 // its image decoding API (in particular) is at:
46 //
47 // - https://github.com/google/wuffs/tree/master/doc
48 // - https://github.com/google/wuffs/blob/master/doc/std/image-decoders.md
49
50 // Wuffs ships as a "single file C library" or "header file library" as per
51 // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
52 //
53 // As we have not #define'd WUFFS_IMPLEMENTATION, the #include here is
54 // including a header file, even though that file name ends in ".c".
55 #if defined(WUFFS_IMPLEMENTATION)
56 #error "SkWuffsCodec should not #define WUFFS_IMPLEMENTATION"
57 #endif
58 #include "wuffs-v0.3.c"
59 // Commit count 2514 is Wuffs 0.3.0-alpha.4.
60 #if WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT < 2514
61 #error "Wuffs version is too old. Upgrade to the latest version."
62 #endif
63
64 #define SK_WUFFS_CODEC_BUFFER_SIZE 4096
65
66 // Configuring a Skia build with
67 // SK_WUFFS_FAVORS_PERFORMANCE_OVER_ADDITIONAL_MEMORY_SAFETY can improve decode
68 // performance by some fixed amount (independent of the image size), which can
69 // be a noticeable proportional improvement if the input is relatively small.
70 //
71 // The Wuffs library is still memory-safe either way, in that there are no
72 // out-of-bounds reads or writes, and the library endeavours not to read
73 // uninitialized memory. There are just fewer compiler-enforced guarantees
74 // against reading uninitialized memory. For more detail, see
75 // https://github.com/google/wuffs/blob/master/doc/note/initialization.md#partial-zero-initialization
76 #if defined(SK_WUFFS_FAVORS_PERFORMANCE_OVER_ADDITIONAL_MEMORY_SAFETY)
77 #define SK_WUFFS_INITIALIZE_FLAGS WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED
78 #else
79 #define SK_WUFFS_INITIALIZE_FLAGS WUFFS_INITIALIZE__DEFAULT_OPTIONS
80 #endif
81
fill_buffer(wuffs_base__io_buffer * b,SkStream * s)82 static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
83 b->compact();
84 size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
85 b->meta.wi += num_read;
86 b->meta.closed = s->isAtEnd();
87 return num_read > 0;
88 }
89
seek_buffer(wuffs_base__io_buffer * b,SkStream * s,uint64_t pos)90 static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
91 // Try to re-position the io_buffer's meta.ri read-index first, which is
92 // cheaper than seeking in the backing SkStream.
93 if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
94 b->meta.ri = pos - b->meta.pos;
95 return true;
96 }
97 // Seek in the backing SkStream.
98 if ((pos > SIZE_MAX) || (!s->seek(pos))) {
99 return false;
100 }
101 b->meta.wi = 0;
102 b->meta.ri = 0;
103 b->meta.pos = pos;
104 b->meta.closed = false;
105 return true;
106 }
107
wuffs_disposal_to_skia_disposal(wuffs_base__animation_disposal w)108 static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
109 wuffs_base__animation_disposal w) {
110 switch (w) {
111 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
112 return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
113 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
114 return SkCodecAnimation::DisposalMethod::kRestorePrevious;
115 default:
116 return SkCodecAnimation::DisposalMethod::kKeep;
117 }
118 }
119
to_alpha_type(bool opaque)120 static SkAlphaType to_alpha_type(bool opaque) {
121 return opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
122 }
123
reset_and_decode_image_config(wuffs_gif__decoder * decoder,wuffs_base__image_config * imgcfg,wuffs_base__io_buffer * b,SkStream * s)124 static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
125 wuffs_base__image_config* imgcfg,
126 wuffs_base__io_buffer* b,
127 SkStream* s) {
128 // Calling decoder->initialize will memset most or all of it to zero,
129 // depending on SK_WUFFS_INITIALIZE_FLAGS.
130 wuffs_base__status status =
131 decoder->initialize(sizeof__wuffs_gif__decoder(), WUFFS_VERSION, SK_WUFFS_INITIALIZE_FLAGS);
132 if (status.repr != nullptr) {
133 SkCodecPrintf("initialize: %s", status.message());
134 return SkCodec::kInternalError;
135 }
136
137 // See https://bugs.chromium.org/p/skia/issues/detail?id=12055
138 decoder->set_quirk_enabled(WUFFS_GIF__QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA, true);
139
140 while (true) {
141 status = decoder->decode_image_config(imgcfg, b);
142 if (status.repr == nullptr) {
143 break;
144 } else if (status.repr != wuffs_base__suspension__short_read) {
145 SkCodecPrintf("decode_image_config: %s", status.message());
146 return SkCodec::kErrorInInput;
147 } else if (!fill_buffer(b, s)) {
148 return SkCodec::kIncompleteInput;
149 }
150 }
151
152 // A GIF image's natural color model is indexed color: 1 byte per pixel,
153 // indexing a 256-element palette.
154 //
155 // For Skia, we override that to decode to 4 bytes per pixel, BGRA or RGBA.
156 uint32_t pixfmt = WUFFS_BASE__PIXEL_FORMAT__INVALID;
157 switch (kN32_SkColorType) {
158 case kBGRA_8888_SkColorType:
159 pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
160 break;
161 case kRGBA_8888_SkColorType:
162 pixfmt = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
163 break;
164 default:
165 return SkCodec::kInternalError;
166 }
167 if (imgcfg) {
168 imgcfg->pixcfg.set(pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, imgcfg->pixcfg.width(),
169 imgcfg->pixcfg.height());
170 }
171
172 return SkCodec::kSuccess;
173 }
174
175 // -------------------------------- Class definitions
176
177 class SkWuffsCodec;
178
179 class SkWuffsFrame final : public SkFrame {
180 public:
181 SkWuffsFrame(wuffs_base__frame_config* fc);
182
183 uint64_t ioPosition() const;
184
185 // SkFrame overrides.
186 SkEncodedInfo::Alpha onReportedAlpha() const override;
187
188 private:
189 uint64_t fIOPosition;
190 SkEncodedInfo::Alpha fReportedAlpha;
191
192 using INHERITED = SkFrame;
193 };
194
195 // SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
196 // SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
197 // inherit from both SkCodec and SkFrameHolder, and Skia style discourages
198 // multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
199 class SkWuffsFrameHolder final : public SkFrameHolder {
200 public:
SkWuffsFrameHolder()201 SkWuffsFrameHolder() : INHERITED() {}
202
203 void init(SkWuffsCodec* codec, int width, int height);
204
205 // SkFrameHolder overrides.
206 const SkFrame* onGetFrame(int i) const override;
207
208 private:
209 const SkWuffsCodec* fCodec;
210
211 using INHERITED = SkFrameHolder;
212 };
213
214 class SkWuffsCodec final : public SkScalingCodec {
215 public:
216 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
217 std::unique_ptr<SkStream> stream,
218 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
219 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
220 size_t workbuf_len,
221 wuffs_base__image_config imgcfg,
222 wuffs_base__io_buffer iobuf);
223
224 const SkWuffsFrame* frame(int i) const;
225
226 private:
227 // SkCodec overrides.
228 SkEncodedImageFormat onGetEncodedFormat() const override;
229 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
230 const SkFrameHolder* getFrameHolder() const override;
231 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
232 void* dst,
233 size_t rowBytes,
234 const SkCodec::Options& options) override;
235 Result onIncrementalDecode(int* rowsDecoded) override;
236 int onGetFrameCount() override;
237 bool onGetFrameInfo(int, FrameInfo*) const override;
238 int onGetRepetitionCount() override;
239
240 // Two separate implementations of onStartIncrementalDecode and
241 // onIncrementalDecode, named "one pass" and "two pass" decoding. One pass
242 // decoding writes directly from the Wuffs image decoder to the dst buffer
243 // (the dst argument to onStartIncrementalDecode). Two pass decoding first
244 // writes into an intermediate buffer, and then composites and transforms
245 // the intermediate buffer into the dst buffer.
246 //
247 // In the general case, we need the two pass decoder, because of Skia API
248 // features that Wuffs doesn't support (e.g. color correction, scaling,
249 // RGB565). But as an optimization, we use one pass decoding (it's faster
250 // and uses less memory) if applicable (see the assignment to
251 // fIncrDecOnePass that calculates when we can do so).
252 Result onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
253 uint8_t* dst,
254 size_t rowBytes,
255 const SkCodec::Options& options,
256 uint32_t pixelFormat,
257 size_t bytesPerPixel);
258 Result onStartIncrementalDecodeTwoPass();
259 Result onIncrementalDecodeOnePass();
260 Result onIncrementalDecodeTwoPass();
261
262 void onGetFrameCountInternal();
263 Result seekFrame(int frameIndex);
264 Result resetDecoder();
265 const char* decodeFrameConfig();
266 const char* decodeFrame();
267 void updateNumFullyReceivedFrames();
268
269 SkWuffsFrameHolder fFrameHolder;
270 std::unique_ptr<SkStream> fStream;
271 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
272 size_t fWorkbufLen;
273
274 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoder;
275
276 const uint64_t fFirstFrameIOPosition;
277 wuffs_base__frame_config fFrameConfig;
278 wuffs_base__pixel_config fPixelConfig;
279 wuffs_base__pixel_buffer fPixelBuffer;
280 wuffs_base__io_buffer fIOBuffer;
281
282 // Incremental decoding state.
283 uint8_t* fIncrDecDst;
284 size_t fIncrDecRowBytes;
285 wuffs_base__pixel_blend fIncrDecPixelBlend;
286 bool fIncrDecOnePass;
287 bool fFirstCallToIncrementalDecode;
288
289 // Lazily allocated intermediate pixel buffer, for two pass decoding.
290 std::unique_ptr<uint8_t, decltype(&sk_free)> fTwoPassPixbufPtr;
291 size_t fTwoPassPixbufLen;
292
293 uint64_t fNumFullyReceivedFrames;
294 std::vector<SkWuffsFrame> fFrames;
295 bool fFramesComplete;
296
297 // If calling an fDecoder method returns an incomplete status, then
298 // fDecoder is suspended in a coroutine (i.e. waiting on I/O or halted on a
299 // non-recoverable error). To keep its internal proof-of-safety invariants
300 // consistent, there's only two things you can safely do with a suspended
301 // Wuffs object: resume the coroutine, or reset all state (memset to zero
302 // and start again).
303 //
304 // If fDecoderIsSuspended, and we aren't sure that we're going to resume
305 // the coroutine, then we will need to call this->resetDecoder before
306 // calling other fDecoder methods.
307 bool fDecoderIsSuspended;
308
309 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
310
311 using INHERITED = SkScalingCodec;
312 };
313
314 // -------------------------------- SkWuffsFrame implementation
315
SkWuffsFrame(wuffs_base__frame_config * fc)316 SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
317 : INHERITED(fc->index()),
318 fIOPosition(fc->io_position()),
319 fReportedAlpha(fc->opaque_within_bounds() ? SkEncodedInfo::kOpaque_Alpha
320 : SkEncodedInfo::kUnpremul_Alpha) {
321 wuffs_base__rect_ie_u32 r = fc->bounds();
322 this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
323 this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
324 this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
325 this->setBlend(fc->overwrite_instead_of_blend() ? SkCodecAnimation::Blend::kSrc
326 : SkCodecAnimation::Blend::kSrcOver);
327 }
328
ioPosition() const329 uint64_t SkWuffsFrame::ioPosition() const {
330 return fIOPosition;
331 }
332
onReportedAlpha() const333 SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
334 return fReportedAlpha;
335 }
336
337 // -------------------------------- SkWuffsFrameHolder implementation
338
init(SkWuffsCodec * codec,int width,int height)339 void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
340 fCodec = codec;
341 // Initialize SkFrameHolder's (the superclass) fields.
342 fScreenWidth = width;
343 fScreenHeight = height;
344 }
345
onGetFrame(int i) const346 const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
347 return fCodec->frame(i);
348 }
349
350 // -------------------------------- SkWuffsCodec implementation
351
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) > workbuf_ptr,size_t workbuf_len,wuffs_base__image_config imgcfg,wuffs_base__io_buffer iobuf)352 SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
353 std::unique_ptr<SkStream> stream,
354 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
355 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
356 size_t workbuf_len,
357 wuffs_base__image_config imgcfg,
358 wuffs_base__io_buffer iobuf)
359 : INHERITED(std::move(encodedInfo),
360 skcms_PixelFormat_RGBA_8888,
361 // Pass a nullptr SkStream to the SkCodec constructor. We
362 // manage the stream ourselves, as the default SkCodec behavior
363 // is too trigger-happy on rewinding the stream.
364 nullptr),
365 fFrameHolder(),
366 fStream(std::move(stream)),
367 fWorkbufPtr(std::move(workbuf_ptr)),
368 fWorkbufLen(workbuf_len),
369 fDecoder(std::move(dec)),
370 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
371 fFrameConfig(wuffs_base__null_frame_config()),
372 fPixelConfig(imgcfg.pixcfg),
373 fPixelBuffer(wuffs_base__null_pixel_buffer()),
374 fIOBuffer(wuffs_base__empty_io_buffer()),
375 fIncrDecDst(nullptr),
376 fIncrDecRowBytes(0),
377 fIncrDecPixelBlend(WUFFS_BASE__PIXEL_BLEND__SRC),
378 fIncrDecOnePass(false),
379 fFirstCallToIncrementalDecode(false),
380 fTwoPassPixbufPtr(nullptr, &sk_free),
381 fTwoPassPixbufLen(0),
382 fNumFullyReceivedFrames(0),
383 fFramesComplete(false),
384 fDecoderIsSuspended(false) {
385 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
386
387 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
388 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
389 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
390 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
391 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
392 fIOBuffer.data = wuffs_base__make_slice_u8(fBuffer, SK_WUFFS_CODEC_BUFFER_SIZE);
393 fIOBuffer.meta = iobuf.meta;
394 }
395
frame(int i) const396 const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
397 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
398 return &fFrames[i];
399 }
400 return nullptr;
401 }
402
onGetEncodedFormat() const403 SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
404 return SkEncodedImageFormat::kGIF;
405 }
406
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,const Options & options,int * rowsDecoded)407 SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
408 void* dst,
409 size_t rowBytes,
410 const Options& options,
411 int* rowsDecoded) {
412 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
413 if (result != kSuccess) {
414 return result;
415 }
416 return this->onIncrementalDecode(rowsDecoded);
417 }
418
getFrameHolder() const419 const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
420 return &fFrameHolder;
421 }
422
onStartIncrementalDecode(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,const SkCodec::Options & options)423 SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
424 void* dst,
425 size_t rowBytes,
426 const SkCodec::Options& options) {
427 if (!dst) {
428 return SkCodec::kInvalidParameters;
429 }
430 if (options.fSubset) {
431 return SkCodec::kUnimplemented;
432 }
433 SkCodec::Result result = this->seekFrame(options.fFrameIndex);
434 if (result != SkCodec::kSuccess) {
435 return result;
436 }
437
438 const char* status = this->decodeFrameConfig();
439 if (status == wuffs_base__suspension__short_read) {
440 return SkCodec::kIncompleteInput;
441 } else if (status != nullptr) {
442 SkCodecPrintf("decodeFrameConfig: %s", status);
443 return SkCodec::kErrorInInput;
444 }
445
446 uint32_t pixelFormat = WUFFS_BASE__PIXEL_FORMAT__INVALID;
447 size_t bytesPerPixel = 0;
448
449 switch (dstInfo.colorType()) {
450 case kRGB_565_SkColorType:
451 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGR_565;
452 bytesPerPixel = 2;
453 break;
454 case kBGRA_8888_SkColorType:
455 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
456 bytesPerPixel = 4;
457 break;
458 case kRGBA_8888_SkColorType:
459 pixelFormat = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
460 bytesPerPixel = 4;
461 break;
462 default:
463 break;
464 }
465
466 // We can use "one pass" decoding if we have a Skia pixel format that Wuffs
467 // supports...
468 fIncrDecOnePass = (pixelFormat != WUFFS_BASE__PIXEL_FORMAT__INVALID) &&
469 // ...and no color profile (as Wuffs does not support them)...
470 (!getEncodedInfo().profile()) &&
471 // ...and we use the identity transform (as Wuffs does
472 // not support scaling).
473 (this->dimensions() == dstInfo.dimensions());
474
475 result = fIncrDecOnePass ? this->onStartIncrementalDecodeOnePass(
476 dstInfo, static_cast<uint8_t*>(dst), rowBytes, options,
477 pixelFormat, bytesPerPixel)
478 : this->onStartIncrementalDecodeTwoPass();
479 if (result != SkCodec::kSuccess) {
480 return result;
481 }
482
483 fIncrDecDst = static_cast<uint8_t*>(dst);
484 fIncrDecRowBytes = rowBytes;
485 fFirstCallToIncrementalDecode = true;
486 return SkCodec::kSuccess;
487 }
488
onStartIncrementalDecodeOnePass(const SkImageInfo & dstInfo,uint8_t * dst,size_t rowBytes,const SkCodec::Options & options,uint32_t pixelFormat,size_t bytesPerPixel)489 SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeOnePass(const SkImageInfo& dstInfo,
490 uint8_t* dst,
491 size_t rowBytes,
492 const SkCodec::Options& options,
493 uint32_t pixelFormat,
494 size_t bytesPerPixel) {
495 wuffs_base__pixel_config pixelConfig;
496 pixelConfig.set(pixelFormat, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, dstInfo.width(),
497 dstInfo.height());
498
499 wuffs_base__table_u8 table;
500 table.ptr = dst;
501 table.width = static_cast<size_t>(dstInfo.width()) * bytesPerPixel;
502 table.height = dstInfo.height();
503 table.stride = rowBytes;
504
505 wuffs_base__status status = fPixelBuffer.set_from_table(&pixelConfig, table);
506 if (status.repr != nullptr) {
507 SkCodecPrintf("set_from_table: %s", status.message());
508 return SkCodec::kInternalError;
509 }
510
511 // SRC is usually faster than SRC_OVER, but for a dependent frame, dst is
512 // assumed to hold the previous frame's pixels (after processing the
513 // DisposalMethod). For one-pass decoding, we therefore use SRC_OVER.
514 if ((options.fFrameIndex != 0) &&
515 (this->frame(options.fFrameIndex)->getRequiredFrame() != SkCodec::kNoFrame)) {
516 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC_OVER;
517 } else {
518 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
519 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
520 }
521
522 return SkCodec::kSuccess;
523 }
524
onStartIncrementalDecodeTwoPass()525 SkCodec::Result SkWuffsCodec::onStartIncrementalDecodeTwoPass() {
526 // Either re-use the previously allocated "two pass" pixel buffer (and
527 // memset to zero), or allocate (and zero initialize) a new one.
528 bool already_zeroed = false;
529
530 if (!fTwoPassPixbufPtr) {
531 uint64_t pixbuf_len = fPixelConfig.pixbuf_len();
532 void* pixbuf_ptr_raw = (pixbuf_len <= SIZE_MAX)
533 ? sk_malloc_flags(pixbuf_len, SK_MALLOC_ZERO_INITIALIZE)
534 : nullptr;
535 if (!pixbuf_ptr_raw) {
536 return SkCodec::kInternalError;
537 }
538 fTwoPassPixbufPtr.reset(reinterpret_cast<uint8_t*>(pixbuf_ptr_raw));
539 fTwoPassPixbufLen = SkToSizeT(pixbuf_len);
540 already_zeroed = true;
541 }
542
543 wuffs_base__status status = fPixelBuffer.set_from_slice(
544 &fPixelConfig, wuffs_base__make_slice_u8(fTwoPassPixbufPtr.get(), fTwoPassPixbufLen));
545 if (status.repr != nullptr) {
546 SkCodecPrintf("set_from_slice: %s", status.message());
547 return SkCodec::kInternalError;
548 }
549
550 if (!already_zeroed) {
551 uint32_t src_bits_per_pixel = fPixelConfig.pixel_format().bits_per_pixel();
552 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
553 return SkCodec::kInternalError;
554 }
555 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
556
557 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
558 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
559
560 uint8_t* ptr = pixels.ptr + (frame_rect.min_incl_y * pixels.stride) +
561 (frame_rect.min_incl_x * src_bytes_per_pixel);
562 size_t len = frame_rect.width() * src_bytes_per_pixel;
563
564 // As an optimization, issue a single sk_bzero call, if possible.
565 // Otherwise, zero out each row separately.
566 if ((len == pixels.stride) && (frame_rect.min_incl_y < frame_rect.max_excl_y)) {
567 sk_bzero(ptr, len * (frame_rect.max_excl_y - frame_rect.min_incl_y));
568 } else {
569 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
570 sk_bzero(ptr, len);
571 ptr += pixels.stride;
572 }
573 }
574 }
575
576 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
577 return SkCodec::kSuccess;
578 }
579
onIncrementalDecode(int * rowsDecoded)580 SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
581 if (!fIncrDecDst) {
582 return SkCodec::kInternalError;
583 }
584
585 if (rowsDecoded) {
586 *rowsDecoded = dstInfo().height();
587 }
588
589 SkCodec::Result result =
590 fIncrDecOnePass ? this->onIncrementalDecodeOnePass() : this->onIncrementalDecodeTwoPass();
591 if (result == SkCodec::kSuccess) {
592 fIncrDecDst = nullptr;
593 fIncrDecRowBytes = 0;
594 fIncrDecPixelBlend = WUFFS_BASE__PIXEL_BLEND__SRC;
595 fIncrDecOnePass = false;
596 }
597 return result;
598 }
599
onIncrementalDecodeOnePass()600 SkCodec::Result SkWuffsCodec::onIncrementalDecodeOnePass() {
601 const char* status = this->decodeFrame();
602 if (status != nullptr) {
603 if (status == wuffs_base__suspension__short_read) {
604 return SkCodec::kIncompleteInput;
605 } else {
606 SkCodecPrintf("decodeFrame: %s", status);
607 return SkCodec::kErrorInInput;
608 }
609 }
610 return SkCodec::kSuccess;
611 }
612
onIncrementalDecodeTwoPass()613 SkCodec::Result SkWuffsCodec::onIncrementalDecodeTwoPass() {
614 SkCodec::Result result = SkCodec::kSuccess;
615 const char* status = this->decodeFrame();
616 bool independent;
617 SkAlphaType alphaType;
618 const int index = options().fFrameIndex;
619 if (index == 0) {
620 independent = true;
621 alphaType = to_alpha_type(getEncodedInfo().opaque());
622 } else {
623 const SkWuffsFrame* f = this->frame(index);
624 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
625 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
626 }
627 if (status != nullptr) {
628 if (status == wuffs_base__suspension__short_read) {
629 result = SkCodec::kIncompleteInput;
630 } else {
631 SkCodecPrintf("decodeFrame: %s", status);
632 result = SkCodec::kErrorInInput;
633 }
634
635 if (!independent) {
636 // For a dependent frame, we cannot blend the partial result, since
637 // that will overwrite the contribution from prior frames.
638 return result;
639 }
640 }
641
642 uint32_t src_bits_per_pixel = fPixelBuffer.pixcfg.pixel_format().bits_per_pixel();
643 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
644 return SkCodec::kInternalError;
645 }
646 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
647
648 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
649 if (fFirstCallToIncrementalDecode) {
650 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
651 return SkCodec::kInternalError;
652 }
653
654 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
655 frame_rect.max_excl_x, frame_rect.max_excl_y);
656
657 // If the frame rect does not fill the output, ensure that those pixels are not
658 // left uninitialized.
659 if (independent && (bounds != this->bounds() || result != kSuccess)) {
660 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes, options().fZeroInitialized);
661 }
662 fFirstCallToIncrementalDecode = false;
663 } else {
664 // Existing clients intend to only show frames beyond the first if they
665 // are complete (based on FrameInfo::fFullyReceived), since it might
666 // look jarring to draw a partial frame over an existing frame. If they
667 // changed their behavior and expected to continue decoding a partial
668 // frame after the first one, we'll need to update our blending code.
669 // Otherwise, if the frame were interlaced and not independent, the
670 // second pass may have an overlapping dirty_rect with the first,
671 // resulting in blending with the first pass.
672 SkASSERT(index == 0);
673 }
674
675 // If the frame's dirty rect is empty, no need to swizzle.
676 wuffs_base__rect_ie_u32 dirty_rect = fDecoder->frame_dirty_rect();
677 if (!dirty_rect.is_empty()) {
678 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
679
680 // The Wuffs model is that the dst buffer is the image, not the frame.
681 // The expectation is that you allocate the buffer once, but re-use it
682 // for the N frames, regardless of each frame's top-left co-ordinate.
683 //
684 // To get from the start (in the X-direction) of the image to the start
685 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
686 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride) +
687 (dirty_rect.min_incl_x * src_bytes_per_pixel);
688
689 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
690 // used for other formats that might have one, we will need to transform from profiles that
691 // do not have corresponding SkColorSpaces.
692 SkASSERT(!getEncodedInfo().profile());
693
694 auto srcInfo =
695 getInfo().makeWH(dirty_rect.width(), dirty_rect.height()).makeAlphaType(alphaType);
696 SkBitmap src;
697 src.installPixels(srcInfo, s, pixels.stride);
698 SkPaint paint;
699 if (independent) {
700 paint.setBlendMode(SkBlendMode::kSrc);
701 }
702
703 SkDraw draw;
704 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
705 SkMatrix matrix = SkMatrix::RectToRect(SkRect::Make(this->dimensions()),
706 SkRect::Make(this->dstInfo().dimensions()));
707 SkMatrixProvider matrixProvider(matrix);
708 draw.fMatrixProvider = &matrixProvider;
709 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
710 draw.fRC = &rc;
711
712 SkMatrix translate = SkMatrix::Translate(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
713 draw.drawBitmap(src, translate, nullptr, SkSamplingOptions(), paint);
714 }
715
716 if (result == SkCodec::kSuccess) {
717 // On success, we are done using the "two pass" pixel buffer for this
718 // frame. We have the option of releasing its memory, but there is a
719 // trade-off. If decoding a subsequent frame will also need "two pass"
720 // decoding, it would have to re-allocate the buffer instead of just
721 // re-using it. On the other hand, if there is no subsequent frame, and
722 // the SkWuffsCodec object isn't deleted soon, then we are holding
723 // megabytes of memory longer than we need to.
724 //
725 // For example, when the Chromium web browser decodes the <img> tags in
726 // a HTML page, the SkCodec object can live until navigating away from
727 // the page, which can be much longer than when the pixels are fully
728 // decoded, especially for a still (non-animated) image. Even for
729 // looping animations, caching the decoded frames (at the higher HTML
730 // renderer layer) may mean that each frame is only decoded once (at
731 // the lower SkCodec layer), in sequence.
732 //
733 // The heuristic we use here is to free the memory if we have decoded
734 // the last frame of the animation (or, for still images, the only
735 // frame). The output of the next decode request (if any) should be the
736 // same either way, but the steady state memory use should hopefully be
737 // lower than always keeping the fTwoPassPixbufPtr buffer up until the
738 // SkWuffsCodec destructor runs.
739 //
740 // This only applies to "two pass" decoding. "One pass" decoding does
741 // not allocate, free or otherwise use fTwoPassPixbufPtr.
742 if (fFramesComplete && (static_cast<size_t>(options().fFrameIndex) == fFrames.size() - 1)) {
743 fTwoPassPixbufPtr.reset(nullptr);
744 fTwoPassPixbufLen = 0;
745 }
746 }
747
748 return result;
749 }
750
onGetFrameCount()751 int SkWuffsCodec::onGetFrameCount() {
752 // It is valid, in terms of the SkCodec API, to call SkCodec::getFrameCount
753 // while in an incremental decode (after onStartIncrementalDecode returns
754 // and before onIncrementalDecode returns kSuccess).
755 //
756 // We should not advance the SkWuffsCodec' stream while doing so, even
757 // though other SkCodec implementations can return increasing values from
758 // onGetFrameCount when given more data. If we tried to do so, the
759 // subsequent resume of the incremental decode would continue reading from
760 // a different position in the I/O stream, leading to an incorrect error.
761 //
762 // Other SkCodec implementations can move the stream forward during
763 // onGetFrameCount because they assume that the stream is rewindable /
764 // seekable. For example, an alternative GIF implementation may choose to
765 // store, for each frame walked past when merely counting the number of
766 // frames, the I/O position of each of the frame's GIF data blocks. (A GIF
767 // frame's compressed data can have multiple data blocks, each at most 255
768 // bytes in length). Obviously, this can require O(numberOfFrames) extra
769 // memory to store these I/O positions. The constant factor is small, but
770 // it's still O(N), not O(1).
771 //
772 // Wuffs and SkWuffsCodec try to minimize relying on the rewindable /
773 // seekable assumption. By design, Wuffs per se aims for O(1) memory use
774 // (after any pixel buffers are allocated) instead of O(N), and its I/O
775 // type, wuffs_base__io_buffer, is not necessarily rewindable or seekable.
776 //
777 // The Wuffs API provides a limited, optional form of seeking, to the start
778 // of an animation frame's data, but does not provide arbitrary save and
779 // load of its internal state whilst in the middle of an animation frame.
780 bool incrementalDecodeIsInProgress = fIncrDecDst != nullptr;
781
782 if (!fFramesComplete && !incrementalDecodeIsInProgress) {
783 this->onGetFrameCountInternal();
784 this->updateNumFullyReceivedFrames();
785 }
786 return fFrames.size();
787 }
788
onGetFrameCountInternal()789 void SkWuffsCodec::onGetFrameCountInternal() {
790 size_t n = fFrames.size();
791 int i = n ? n - 1 : 0;
792 if (this->seekFrame(i) != SkCodec::kSuccess) {
793 return;
794 }
795
796 // Iterate through the frames, converting from Wuffs'
797 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
798 for (; i < INT_MAX; i++) {
799 const char* status = this->decodeFrameConfig();
800 if (status == nullptr) {
801 // No-op.
802 } else if (status == wuffs_base__note__end_of_data) {
803 break;
804 } else {
805 return;
806 }
807
808 if (static_cast<size_t>(i) < fFrames.size()) {
809 continue;
810 }
811 fFrames.emplace_back(&fFrameConfig);
812 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
813 fFrameHolder.setAlphaAndRequiredFrame(f);
814 }
815
816 fFramesComplete = true;
817 }
818
onGetFrameInfo(int i,SkCodec::FrameInfo * frameInfo) const819 bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
820 const SkWuffsFrame* f = this->frame(i);
821 if (!f) {
822 return false;
823 }
824 if (frameInfo) {
825 f->fillIn(frameInfo, static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
826 }
827 return true;
828 }
829
onGetRepetitionCount()830 int SkWuffsCodec::onGetRepetitionCount() {
831 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
832 // number is how many times to play the loop. Skia's int number is how many
833 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
834 // kRepetitionCountInfinite respectively to mean loop forever.
835 uint32_t n = fDecoder->num_animation_loops();
836 if (n == 0) {
837 return SkCodec::kRepetitionCountInfinite;
838 }
839 n--;
840 return n < INT_MAX ? n : INT_MAX;
841 }
842
seekFrame(int frameIndex)843 SkCodec::Result SkWuffsCodec::seekFrame(int frameIndex) {
844 if (fDecoderIsSuspended) {
845 SkCodec::Result res = this->resetDecoder();
846 if (res != SkCodec::kSuccess) {
847 return res;
848 }
849 }
850
851 uint64_t pos = 0;
852 if (frameIndex < 0) {
853 return SkCodec::kInternalError;
854 } else if (frameIndex == 0) {
855 pos = fFirstFrameIOPosition;
856 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
857 pos = fFrames[frameIndex].ioPosition();
858 } else {
859 return SkCodec::kInternalError;
860 }
861
862 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
863 return SkCodec::kInternalError;
864 }
865 wuffs_base__status status =
866 fDecoder->restart_frame(frameIndex, fIOBuffer.reader_io_position());
867 if (status.repr != nullptr) {
868 return SkCodec::kInternalError;
869 }
870 return SkCodec::kSuccess;
871 }
872
resetDecoder()873 SkCodec::Result SkWuffsCodec::resetDecoder() {
874 if (!fStream->rewind()) {
875 return SkCodec::kInternalError;
876 }
877 fIOBuffer.meta = wuffs_base__empty_io_buffer_meta();
878
879 SkCodec::Result result =
880 reset_and_decode_image_config(fDecoder.get(), nullptr, &fIOBuffer, fStream.get());
881 if (result == SkCodec::kIncompleteInput) {
882 return SkCodec::kInternalError;
883 } else if (result != SkCodec::kSuccess) {
884 return result;
885 }
886
887 fDecoderIsSuspended = false;
888 return SkCodec::kSuccess;
889 }
890
decodeFrameConfig()891 const char* SkWuffsCodec::decodeFrameConfig() {
892 while (true) {
893 wuffs_base__status status =
894 fDecoder->decode_frame_config(&fFrameConfig, &fIOBuffer);
895 if ((status.repr == wuffs_base__suspension__short_read) &&
896 fill_buffer(&fIOBuffer, fStream.get())) {
897 continue;
898 }
899 fDecoderIsSuspended = !status.is_complete();
900 this->updateNumFullyReceivedFrames();
901 return status.repr;
902 }
903 }
904
decodeFrame()905 const char* SkWuffsCodec::decodeFrame() {
906 while (true) {
907 wuffs_base__status status = fDecoder->decode_frame(
908 &fPixelBuffer, &fIOBuffer, fIncrDecPixelBlend,
909 wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen), nullptr);
910 if ((status.repr == wuffs_base__suspension__short_read) &&
911 fill_buffer(&fIOBuffer, fStream.get())) {
912 continue;
913 }
914 fDecoderIsSuspended = !status.is_complete();
915 this->updateNumFullyReceivedFrames();
916 return status.repr;
917 }
918 }
919
updateNumFullyReceivedFrames()920 void SkWuffsCodec::updateNumFullyReceivedFrames() {
921 // num_decoded_frames's return value, n, can change over time, both up and
922 // down, as we seek back and forth in the underlying stream.
923 // fNumFullyReceivedFrames is the highest n we've seen.
924 uint64_t n = fDecoder->num_decoded_frames();
925 if (fNumFullyReceivedFrames < n) {
926 fNumFullyReceivedFrames = n;
927 }
928 }
929
930 // -------------------------------- SkWuffsCodec.h functions
931
SkWuffsCodec_IsFormat(const void * buf,size_t bytesRead)932 bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
933 constexpr const char* gif_ptr = "GIF8";
934 constexpr size_t gif_len = 4;
935 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
936 }
937
SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,SkCodec::Result * result)938 std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
939 SkCodec::Result* result) {
940 // Some clients (e.g. Android) need to be able to seek the stream, but may
941 // not provide a seekable stream. Copy the stream to one that can seek.
942 if (!stream->hasPosition() || !stream->hasLength()) {
943 auto data = SkCopyStreamToData(stream.get());
944 stream = std::make_unique<SkMemoryStream>(std::move(data));
945 }
946
947 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
948 wuffs_base__io_buffer iobuf =
949 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
950 wuffs_base__empty_io_buffer_meta());
951 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
952
953 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
954 // the wuffs_base__etc types, the sizeof a file format specific type like
955 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
956 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
957 // to an opaque type: a private implementation detail. The API is always
958 // "set_foo(p, etc)" and not "p->foo = etc".
959 //
960 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
961 //
962 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
963 // the struct at compile time). Instead, we use sk_malloc_canfail, with
964 // sizeof__wuffs_gif__decoder returning the appropriate value for the
965 // (statically or dynamically) linked version of the Wuffs library.
966 //
967 // As a C (not C++) library, none of the Wuffs types have constructors or
968 // destructors.
969 //
970 // In RAII style, we can still use std::unique_ptr with these pointers, but
971 // we pair the pointer with sk_free instead of C++'s delete.
972 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
973 if (!decoder_raw) {
974 *result = SkCodec::kInternalError;
975 return nullptr;
976 }
977 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
978 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
979
980 SkCodec::Result reset_result =
981 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
982 if (reset_result != SkCodec::kSuccess) {
983 *result = reset_result;
984 return nullptr;
985 }
986
987 uint32_t width = imgcfg.pixcfg.width();
988 uint32_t height = imgcfg.pixcfg.height();
989 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
990 *result = SkCodec::kInvalidInput;
991 return nullptr;
992 }
993
994 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
995 void* workbuf_ptr_raw = nullptr;
996 if (workbuf_len) {
997 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
998 if (!workbuf_ptr_raw) {
999 *result = SkCodec::kInternalError;
1000 return nullptr;
1001 }
1002 }
1003 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
1004 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
1005
1006 SkEncodedInfo::Color color =
1007 (imgcfg.pixcfg.pixel_format().repr == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
1008 ? SkEncodedInfo::kBGRA_Color
1009 : SkEncodedInfo::kRGBA_Color;
1010
1011 // In Skia's API, the alpha we calculate here and return is only for the
1012 // first frame.
1013 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
1014 : SkEncodedInfo::kBinary_Alpha;
1015
1016 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
1017
1018 *result = SkCodec::kSuccess;
1019 return std::unique_ptr<SkCodec>(new SkWuffsCodec(std::move(encodedInfo), std::move(stream),
1020 std::move(decoder), std::move(workbuf_ptr),
1021 workbuf_len, imgcfg, iobuf));
1022 }
1023