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