blob: 49357e913468d030bc6b060c183306f704ae9515 [file] [log] [blame]
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkWuffsCodec.h"
9
10#include "../private/SkMalloc.h"
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040011#include "SkBitmap.h"
12#include "SkDraw.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040013#include "SkFrameHolder.h"
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040014#include "SkMatrix.h"
15#include "SkPaint.h"
16#include "SkRasterClip.h"
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040017#include "SkSampler.h"
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040018#include "SkScalingCodec.h"
Nigel Tao0185b952018-11-08 10:47:24 +110019#include "SkUtils.h"
Nigel Taoa6766482019-01-07 13:41:53 +110020
21// Wuffs ships as a "single file C library" or "header file library" as per
22// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
23//
24// As we have not #define'd WUFFS_IMPLEMENTATION, the #include here is
25// including a header file, even though that file name ends in ".c".
Nigel Taoe66a0b22019-03-09 15:03:14 +110026#if defined(WUFFS_IMPLEMENTATION)
27#error "SkWuffsCodec should not #define WUFFS_IMPLEMENTATION"
28#endif
Nigel Taoa6766482019-01-07 13:41:53 +110029#include "wuffs-v0.2.c"
Nigel Tao10ca6ff2019-04-07 16:40:37 +100030#if WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT < 1675
Nigel Taoa6766482019-01-07 13:41:53 +110031#error "Wuffs version is too old. Upgrade to the latest version."
32#endif
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040033
34#define SK_WUFFS_CODEC_BUFFER_SIZE 4096
35
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -040036static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
37 b->compact();
38 size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
39 b->meta.wi += num_read;
40 b->meta.closed = s->isAtEnd();
41 return num_read > 0;
42}
43
44static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
45 // Try to re-position the io_buffer's meta.ri read-index first, which is
46 // cheaper than seeking in the backing SkStream.
47 if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
48 b->meta.ri = pos - b->meta.pos;
49 return true;
50 }
51 // Seek in the backing SkStream.
52 if ((pos > SIZE_MAX) || (!s->seek(pos))) {
53 return false;
54 }
55 b->meta.wi = 0;
56 b->meta.ri = 0;
57 b->meta.pos = pos;
58 b->meta.closed = false;
59 return true;
60}
61
62static SkEncodedInfo::Alpha wuffs_blend_to_skia_alpha(wuffs_base__animation_blend w) {
63 return (w == WUFFS_BASE__ANIMATION_BLEND__OPAQUE) ? SkEncodedInfo::kOpaque_Alpha
64 : SkEncodedInfo::kUnpremul_Alpha;
65}
66
67static SkCodecAnimation::Blend wuffs_blend_to_skia_blend(wuffs_base__animation_blend w) {
68 return (w == WUFFS_BASE__ANIMATION_BLEND__SRC) ? SkCodecAnimation::Blend::kBG
69 : SkCodecAnimation::Blend::kPriorFrame;
70}
71
72static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
73 wuffs_base__animation_disposal w) {
74 switch (w) {
75 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
76 return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
77 case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
78 return SkCodecAnimation::DisposalMethod::kRestorePrevious;
79 default:
80 return SkCodecAnimation::DisposalMethod::kKeep;
81 }
82}
83
84// -------------------------------- Class definitions
85
86class SkWuffsCodec;
87
88class SkWuffsFrame final : public SkFrame {
89public:
90 SkWuffsFrame(wuffs_base__frame_config* fc);
91
92 SkCodec::FrameInfo frameInfo(bool fullyReceived) const;
93 uint64_t ioPosition() const;
94
95 // SkFrame overrides.
96 SkEncodedInfo::Alpha onReportedAlpha() const override;
97
98private:
99 uint64_t fIOPosition;
100 SkEncodedInfo::Alpha fReportedAlpha;
101
102 typedef SkFrame INHERITED;
103};
104
105// SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
106// SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
107// inherit from both SkCodec and SkFrameHolder, and Skia style discourages
108// multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
109class SkWuffsFrameHolder final : public SkFrameHolder {
110public:
111 SkWuffsFrameHolder() : INHERITED() {}
112
113 void init(SkWuffsCodec* codec, int width, int height);
114
115 // SkFrameHolder overrides.
116 const SkFrame* onGetFrame(int i) const override;
117
118private:
119 const SkWuffsCodec* fCodec;
120
121 typedef SkFrameHolder INHERITED;
122};
123
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400124class SkWuffsCodec final : public SkScalingCodec {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400125public:
126 SkWuffsCodec(SkEncodedInfo&& encodedInfo,
127 std::unique_ptr<SkStream> stream,
128 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
129 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
130 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
131 size_t workbuf_len,
132 wuffs_base__image_config imgcfg,
133 wuffs_base__pixel_buffer pixbuf,
134 wuffs_base__io_buffer iobuf);
135
136 const SkWuffsFrame* frame(int i) const;
137
138private:
139 // SkCodec overrides.
140 SkEncodedImageFormat onGetEncodedFormat() const override;
141 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
142 const SkFrameHolder* getFrameHolder() const override;
143 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
144 void* dst,
145 size_t rowBytes,
146 const SkCodec::Options& options) override;
147 Result onIncrementalDecode(int* rowsDecoded) override;
148 int onGetFrameCount() override;
149 bool onGetFrameInfo(int, FrameInfo*) const override;
150 int onGetRepetitionCount() override;
151
152 void readFrames();
153 Result seekFrame(int frameIndex);
154
155 Result resetDecoder();
156 const char* decodeFrameConfig();
157 const char* decodeFrame();
158 void updateNumFullyReceivedFrames();
159
160 SkWuffsFrameHolder fFrameHolder;
161 std::unique_ptr<SkStream> fStream;
162 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoder;
163 std::unique_ptr<uint8_t, decltype(&sk_free)> fPixbufPtr;
164 std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
165 size_t fWorkbufLen;
166
167 const uint64_t fFirstFrameIOPosition;
168 wuffs_base__frame_config fFrameConfig;
169 wuffs_base__pixel_buffer fPixelBuffer;
170 wuffs_base__io_buffer fIOBuffer;
171
172 // Incremental decoding state.
Nigel Tao0185b952018-11-08 10:47:24 +1100173 uint8_t* fIncrDecDst;
Nigel Tao0185b952018-11-08 10:47:24 +1100174 size_t fIncrDecRowBytes;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400175 bool fFirstCallToIncrementalDecode;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400176
177 uint64_t fNumFullyReceivedFrames;
178 std::vector<SkWuffsFrame> fFrames;
179 bool fFramesComplete;
180
181 // If calling an fDecoder method returns an incomplete status, then
182 // fDecoder is suspended in a coroutine (i.e. waiting on I/O or halted on a
183 // non-recoverable error). To keep its internal proof-of-safety invariants
184 // consistent, there's only two things you can safely do with a suspended
185 // Wuffs object: resume the coroutine, or reset all state (memset to zero
186 // and start again).
187 //
188 // If fDecoderIsSuspended, and we aren't sure that we're going to resume
189 // the coroutine, then we will need to call this->resetDecoder before
190 // calling other fDecoder methods.
191 bool fDecoderIsSuspended;
192
193 uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
194
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400195 typedef SkScalingCodec INHERITED;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400196};
197
198// -------------------------------- SkWuffsFrame implementation
199
200SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
201 : INHERITED(fc->index()),
202 fIOPosition(fc->io_position()),
203 fReportedAlpha(wuffs_blend_to_skia_alpha(fc->blend())) {
204 wuffs_base__rect_ie_u32 r = fc->bounds();
205 this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
206 this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
207 this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
208 this->setBlend(wuffs_blend_to_skia_blend(fc->blend()));
209}
210
211SkCodec::FrameInfo SkWuffsFrame::frameInfo(bool fullyReceived) const {
Nigel Taoef40e332019-04-05 10:28:40 +1100212 SkCodec::FrameInfo ret;
213 ret.fRequiredFrame = getRequiredFrame();
214 ret.fDuration = getDuration();
215 ret.fFullyReceived = fullyReceived;
216 ret.fAlphaType = hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
217 ret.fDisposalMethod = getDisposalMethod();
218 return ret;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400219}
220
221uint64_t SkWuffsFrame::ioPosition() const {
222 return fIOPosition;
223}
224
225SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
226 return fReportedAlpha;
227}
228
229// -------------------------------- SkWuffsFrameHolder implementation
230
231void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
232 fCodec = codec;
233 // Initialize SkFrameHolder's (the superclass) fields.
234 fScreenWidth = width;
235 fScreenHeight = height;
236}
237
238const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
239 return fCodec->frame(i);
240};
241
242// -------------------------------- SkWuffsCodec implementation
243
244SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
245 std::unique_ptr<SkStream> stream,
246 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
247 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
248 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
249 size_t workbuf_len,
250 wuffs_base__image_config imgcfg,
251 wuffs_base__pixel_buffer pixbuf,
252 wuffs_base__io_buffer iobuf)
253 : INHERITED(std::move(encodedInfo),
254 skcms_PixelFormat_RGBA_8888,
255 // Pass a nullptr SkStream to the SkCodec constructor. We
256 // manage the stream ourselves, as the default SkCodec behavior
257 // is too trigger-happy on rewinding the stream.
258 nullptr),
Nigel Tao0185b952018-11-08 10:47:24 +1100259 fFrameHolder(),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400260 fStream(std::move(stream)),
261 fDecoder(std::move(dec)),
262 fPixbufPtr(std::move(pixbuf_ptr)),
263 fWorkbufPtr(std::move(workbuf_ptr)),
264 fWorkbufLen(workbuf_len),
265 fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
Nigel Tao48aa2212019-03-09 14:59:11 +1100266 fFrameConfig(wuffs_base__null_frame_config()),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400267 fPixelBuffer(pixbuf),
Nigel Tao48aa2212019-03-09 14:59:11 +1100268 fIOBuffer(wuffs_base__null_io_buffer()),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400269 fIncrDecDst(nullptr),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400270 fIncrDecRowBytes(0),
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400271 fFirstCallToIncrementalDecode(false),
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400272 fNumFullyReceivedFrames(0),
273 fFramesComplete(false),
274 fDecoderIsSuspended(false) {
275 fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
276
277 // Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
278 // fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
279 // this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
280 SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
281 memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
Nigel Tao48aa2212019-03-09 14:59:11 +1100282 fIOBuffer.data = wuffs_base__make_slice_u8(fBuffer, SK_WUFFS_CODEC_BUFFER_SIZE);
283 fIOBuffer.meta = iobuf.meta;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400284}
285
286const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
287 if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
288 return &fFrames[i];
289 }
290 return nullptr;
291}
292
293SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
294 return SkEncodedImageFormat::kGIF;
295}
296
297SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
298 void* dst,
299 size_t rowBytes,
300 const Options& options,
301 int* rowsDecoded) {
302 SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
303 if (result != kSuccess) {
304 return result;
305 }
306 return this->onIncrementalDecode(rowsDecoded);
307}
308
309const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
310 return &fFrameHolder;
311}
312
313SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
314 void* dst,
315 size_t rowBytes,
316 const SkCodec::Options& options) {
317 if (options.fSubset) {
318 return SkCodec::kUnimplemented;
319 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400320 if (options.fFrameIndex > 0 && SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
321 return SkCodec::kInvalidConversion;
322 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400323 SkCodec::Result result = this->seekFrame(options.fFrameIndex);
324 if (result != SkCodec::kSuccess) {
325 return result;
326 }
327
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500328 const char* status = this->decodeFrameConfig();
Nigel Taob7a1b512019-02-10 12:19:50 +1100329 if (status == wuffs_base__suspension__short_read) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500330 return SkCodec::kIncompleteInput;
Nigel Taob7a1b512019-02-10 12:19:50 +1100331 } else if (status != nullptr) {
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500332 SkCodecPrintf("decodeFrameConfig: %s", status);
333 return SkCodec::kErrorInInput;
334 }
Nigel Taob7a1b512019-02-10 12:19:50 +1100335
Nigel Tao490e6472019-02-14 14:50:53 +1100336 uint32_t src_bits_per_pixel =
337 wuffs_base__pixel_format__bits_per_pixel(fPixelBuffer.pixcfg.pixel_format());
338 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
339 return SkCodec::kInternalError;
340 }
341 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
Nigel Taob7a1b512019-02-10 12:19:50 +1100342
343 // Zero-initialize Wuffs' buffer covering the frame rect.
344 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
345 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
346 for (uint32_t y = frame_rect.min_incl_y; y < frame_rect.max_excl_y; y++) {
Nigel Tao490e6472019-02-14 14:50:53 +1100347 sk_bzero(pixels.ptr + (y * pixels.stride) + (frame_rect.min_incl_x * src_bytes_per_pixel),
348 frame_rect.width() * src_bytes_per_pixel);
Nigel Taob7a1b512019-02-10 12:19:50 +1100349 }
350
351 fIncrDecDst = static_cast<uint8_t*>(dst);
352 fIncrDecRowBytes = rowBytes;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400353 fFirstCallToIncrementalDecode = true;
Nigel Taob7a1b512019-02-10 12:19:50 +1100354 return SkCodec::kSuccess;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400355}
356
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400357static SkAlphaType to_alpha_type(bool opaque) {
358 return opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500359}
360
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400361SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
362 if (!fIncrDecDst) {
363 return SkCodec::kInternalError;
364 }
365
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500366 SkCodec::Result result = SkCodec::kSuccess;
367 const char* status = this->decodeFrame();
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400368 bool independent;
369 SkAlphaType alphaType;
370 const int index = options().fFrameIndex;
371 if (index == 0) {
372 independent = true;
373 alphaType = to_alpha_type(getEncodedInfo().opaque());
374 } else {
375 const SkWuffsFrame* f = this->frame(index);
376 independent = f->getRequiredFrame() == SkCodec::kNoFrame;
377 alphaType = to_alpha_type(f->reportedAlpha() == SkEncodedInfo::kOpaque_Alpha);
378 }
Leon Scroggins III699d41e2019-02-07 09:25:51 -0500379 if (status != nullptr) {
380 if (status == wuffs_base__suspension__short_read) {
381 result = SkCodec::kIncompleteInput;
382 } else {
383 SkCodecPrintf("decodeFrame: %s", status);
384 result = SkCodec::kErrorInInput;
385 }
386
387 if (!independent) {
388 // For a dependent frame, we cannot blend the partial result, since
389 // that will overwrite the contribution from prior frames.
390 return result;
391 }
392 }
393
Nigel Tao490e6472019-02-14 14:50:53 +1100394 uint32_t src_bits_per_pixel =
395 wuffs_base__pixel_format__bits_per_pixel(fPixelBuffer.pixcfg.pixel_format());
396 if ((src_bits_per_pixel == 0) || (src_bits_per_pixel % 8 != 0)) {
397 return SkCodec::kInternalError;
398 }
399 size_t src_bytes_per_pixel = src_bits_per_pixel / 8;
400
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500401 wuffs_base__rect_ie_u32 frame_rect = fFrameConfig.bounds();
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400402 if (fFirstCallToIncrementalDecode) {
Nigel Tao490e6472019-02-14 14:50:53 +1100403 if (frame_rect.width() > (SIZE_MAX / src_bytes_per_pixel)) {
404 return SkCodec::kInternalError;
405 }
406
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400407 auto bounds = SkIRect::MakeLTRB(frame_rect.min_incl_x, frame_rect.min_incl_y,
408 frame_rect.max_excl_x, frame_rect.max_excl_y);
409
Leon Scroggins IIIcb6b8842018-12-04 13:55:13 -0500410 // If the frame rect does not fill the output, ensure that those pixels are not
Nigel Taob7a1b512019-02-10 12:19:50 +1100411 // left uninitialized.
Leon Scroggins III44076362019-02-15 13:56:44 -0500412 if (independent && (bounds != this->bounds() || result != kSuccess)) {
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400413 SkSampler::Fill(dstInfo(), fIncrDecDst, fIncrDecRowBytes,
414 options().fZeroInitialized);
Leon Scroggins III9b0ba2c2018-11-19 14:52:37 -0500415 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400416 fFirstCallToIncrementalDecode = false;
417 } else {
418 // Existing clients intend to only show frames beyond the first if they
419 // are complete (based on FrameInfo::fFullyReceived), since it might
420 // look jarring to draw a partial frame over an existing frame. If they
421 // changed their behavior and expected to continue decoding a partial
422 // frame after the first one, we'll need to update our blending code.
423 // Otherwise, if the frame were interlaced and not independent, the
424 // second pass may have an overlapping dirty_rect with the first,
425 // resulting in blending with the first pass.
426 SkASSERT(index == 0);
Nigel Tao9859ef82019-02-13 13:20:02 +1100427 }
Nigel Tao0185b952018-11-08 10:47:24 +1100428
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400429 if (rowsDecoded) {
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400430 *rowsDecoded = dstInfo().height();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400431 }
432
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500433 // If the frame's dirty rect is empty, no need to swizzle.
Leon Scroggins III44076362019-02-15 13:56:44 -0500434 wuffs_base__rect_ie_u32 dirty_rect = fDecoder->frame_dirty_rect();
Leon Scroggins IIIdad4bfc2018-12-10 12:37:10 -0500435 if (!dirty_rect.is_empty()) {
Nigel Tao490e6472019-02-14 14:50:53 +1100436 wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500437
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400438 // The Wuffs model is that the dst buffer is the image, not the frame.
439 // The expectation is that you allocate the buffer once, but re-use it
440 // for the N frames, regardless of each frame's top-left co-ordinate.
441 //
442 // To get from the start (in the X-direction) of the image to the start
443 // of the dirty_rect, we adjust s by (dirty_rect.min_incl_x * src_bytes_per_pixel).
444 uint8_t* s = pixels.ptr + (dirty_rect.min_incl_y * pixels.stride)
445 + (dirty_rect.min_incl_x * src_bytes_per_pixel);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500446
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400447 // Currently, this is only used for GIF, which will never have an ICC profile. When it is
448 // used for other formats that might have one, we will need to transform from profiles that
449 // do not have corresponding SkColorSpaces.
450 SkASSERT(!getEncodedInfo().profile());
451
452 auto srcInfo = getInfo().makeWH(dirty_rect.width(), dirty_rect.height())
453 .makeAlphaType(alphaType);
454 SkBitmap src;
455 src.installPixels(srcInfo, s, pixels.stride);
456 SkPaint paint;
457 if (independent) {
458 paint.setBlendMode(SkBlendMode::kSrc);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500459 }
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400460
461 SkDraw draw;
462 draw.fDst.reset(dstInfo(), fIncrDecDst, fIncrDecRowBytes);
463 SkMatrix matrix = SkMatrix::MakeRectToRect(SkRect::Make(this->dimensions()),
464 SkRect::Make(this->dstInfo().dimensions()),
465 SkMatrix::kFill_ScaleToFit);
466 draw.fMatrix = &matrix;
467 SkRasterClip rc(SkIRect::MakeSize(this->dstInfo().dimensions()));
468 draw.fRC = &rc;
469
470 SkMatrix translate = SkMatrix::MakeTrans(dirty_rect.min_incl_x, dirty_rect.min_incl_y);
471 draw.drawBitmap(src, translate, nullptr, paint);
Leon Scroggins III7a3805c2018-12-07 09:21:30 -0500472 }
473
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400474 if (result == SkCodec::kSuccess) {
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400475 fIncrDecDst = nullptr;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400476 fIncrDecRowBytes = 0;
477 }
478 return result;
479}
480
481int SkWuffsCodec::onGetFrameCount() {
482 if (!fFramesComplete) {
483 this->readFrames();
484 this->updateNumFullyReceivedFrames();
485 }
486 return fFrames.size();
487}
488
489bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
490 const SkWuffsFrame* f = this->frame(i);
491 if (!f) {
492 return false;
493 }
494 if (frameInfo) {
495 *frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
496 }
497 return true;
498}
499
500int SkWuffsCodec::onGetRepetitionCount() {
501 // Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
502 // number is how many times to play the loop. Skia's int number is how many
503 // times to play the loop *after the first play*. Wuffs and Skia use 0 and
504 // kRepetitionCountInfinite respectively to mean loop forever.
Nigel Tao6af1edc2019-01-19 15:12:39 +1100505 uint32_t n = fDecoder->num_animation_loops();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400506 if (n == 0) {
507 return SkCodec::kRepetitionCountInfinite;
508 }
509 n--;
510 return n < INT_MAX ? n : INT_MAX;
511}
512
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400513void SkWuffsCodec::readFrames() {
514 size_t n = fFrames.size();
515 int i = n ? n - 1 : 0;
516 if (this->seekFrame(i) != SkCodec::kSuccess) {
517 return;
518 }
519
520 // Iterate through the frames, converting from Wuffs'
521 // wuffs_base__frame_config type to Skia's SkWuffsFrame type.
522 for (; i < INT_MAX; i++) {
523 const char* status = this->decodeFrameConfig();
524 if (status == nullptr) {
525 // No-op.
526 } else if (status == wuffs_base__warning__end_of_data) {
527 break;
528 } else {
529 return;
530 }
531
532 if (static_cast<size_t>(i) < fFrames.size()) {
533 continue;
534 }
535 fFrames.emplace_back(&fFrameConfig);
536 SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
537 fFrameHolder.setAlphaAndRequiredFrame(f);
538 }
539
540 fFramesComplete = true;
541}
542
543SkCodec::Result SkWuffsCodec::seekFrame(int frameIndex) {
544 if (fDecoderIsSuspended) {
545 SkCodec::Result res = this->resetDecoder();
546 if (res != SkCodec::kSuccess) {
547 return res;
548 }
549 }
550
551 uint64_t pos = 0;
552 if (frameIndex < 0) {
553 return SkCodec::kInternalError;
554 } else if (frameIndex == 0) {
555 pos = fFirstFrameIOPosition;
556 } else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
557 pos = fFrames[frameIndex].ioPosition();
558 } else {
559 return SkCodec::kInternalError;
560 }
561
562 if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
563 return SkCodec::kInternalError;
564 }
Nigel Tao6af1edc2019-01-19 15:12:39 +1100565 const char* status = fDecoder->restart_frame(frameIndex, fIOBuffer.reader_io_position());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400566 if (status != nullptr) {
567 return SkCodec::kInternalError;
568 }
569 return SkCodec::kSuccess;
570}
571
572// An overview of the Wuffs decoding API:
573//
574// An animated image (such as GIF) has an image header and then N frames. The
575// image header gives e.g. the overall image's width and height. Each frame
576// consists of a frame header (e.g. frame rectangle bounds, display duration)
577// and a payload (the pixels).
578//
579// In Wuffs terminology, there is one image config and then N pairs of
580// (frame_config, frame). To decode everything (without knowing N in advance)
581// sequentially:
582// - call wuffs_gif__decoder::decode_image_config
583// - while (true) {
584// - call wuffs_gif__decoder::decode_frame_config
585// - if that returned wuffs_base__warning__end_of_data, break
586// - call wuffs_gif__decoder::decode_frame
587// - }
588//
589// The first argument to each decode_foo method is the destination struct to
590// store the decoded information.
591//
592// For random (instead of sequential) access to an image's frames, call
593// wuffs_gif__decoder::restart_frame to prepare to decode the i'th frame.
594// Essentially, it restores the state to be at the top of the while loop above.
595// The wuffs_base__io_buffer's reader position will also need to be set at the
596// right point in the source data stream. The position for the i'th frame is
597// calculated by the i'th decode_frame_config call. You can only call
598// restart_frame after decode_image_config is called, explicitly or implicitly
599// (see below), as decoding a single frame might require for-all-frames
600// information like the overall image dimensions and the global palette.
601//
602// All of those decode_xxx calls are optional. For example, if
603// decode_image_config is not called, then the first decode_frame_config call
604// will implicitly parse and verify the image header, before parsing the first
605// frame's header. Similarly, you can call only decode_frame N times, without
606// calling decode_image_config or decode_frame_config, if you already know
607// metadata like N and each frame's rectangle bounds by some other means (e.g.
608// this is a first party, statically known image).
609//
610// Specifically, starting with an unknown (but re-windable) GIF image, if you
611// want to just find N (i.e. count the number of frames), you can loop calling
612// only the decode_frame_config method and avoid calling the more expensive
613// decode_frame method. In terms of the underlying GIF image format, this will
614// skip over the LZW-encoded pixel data, avoiding the costly LZW decompression.
615//
616// Those decode_xxx methods are also suspendible. They will return early (with
617// a status code that is_suspendible and therefore isn't is_complete) if there
618// isn't enough source data to complete the operation: an incremental decode.
619// Calling decode_xxx again with additional source data will resume the
620// previous operation, instead of starting a new operation. Calling decode_yyy
621// whilst decode_xxx is suspended will result in an error.
622//
623// Once an error is encountered, whether from invalid source data or from a
624// programming error such as calling decode_yyy while suspended in decode_xxx,
625// all subsequent calls will be no-ops that return an error. To reset the
626// decoder into something that does productive work, memset the entire struct
627// to zero, check the Wuffs version and then, in order to be able to call
628// restart_frame, call decode_image_config. The io_buffer and its associated
629// stream will also need to be rewound.
630
631static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
632 wuffs_base__image_config* imgcfg,
633 wuffs_base__io_buffer* b,
634 SkStream* s) {
Nigel Taoe39c8842019-02-27 15:57:34 +1100635 // Calling decoder->initialize will memset it to zero.
636 const char* status = decoder->initialize(sizeof__wuffs_gif__decoder(), WUFFS_VERSION, 0);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400637 if (status != nullptr) {
Nigel Taoe39c8842019-02-27 15:57:34 +1100638 SkCodecPrintf("initialize: %s", status);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400639 return SkCodec::kInternalError;
640 }
641 while (true) {
Nigel Tao6af1edc2019-01-19 15:12:39 +1100642 status = decoder->decode_image_config(imgcfg, b->reader());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400643 if (status == nullptr) {
Nigel Tao490e6472019-02-14 14:50:53 +1100644 break;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400645 } else if (status != wuffs_base__suspension__short_read) {
646 SkCodecPrintf("decode_image_config: %s", status);
647 return SkCodec::kErrorInInput;
648 } else if (!fill_buffer(b, s)) {
649 return SkCodec::kIncompleteInput;
650 }
651 }
Nigel Tao490e6472019-02-14 14:50:53 +1100652
653 // A GIF image's natural color model is indexed color: 1 byte per pixel,
654 // indexing a 256-element palette.
655 //
656 // For Skia, we override that to decode to 4 bytes per pixel, BGRA or RGBA.
657 wuffs_base__pixel_format pixfmt = 0;
658 switch (kN32_SkColorType) {
659 case kBGRA_8888_SkColorType:
660 pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
661 break;
662 case kRGBA_8888_SkColorType:
663 pixfmt = WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL;
664 break;
665 default:
666 return SkCodec::kInternalError;
667 }
Leon Scroggins III587edab2019-03-22 15:42:19 -0400668 if (imgcfg) {
669 imgcfg->pixcfg.set(pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, imgcfg->pixcfg.width(),
670 imgcfg->pixcfg.height());
671 }
Nigel Tao490e6472019-02-14 14:50:53 +1100672
673 return SkCodec::kSuccess;
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400674}
675
676SkCodec::Result SkWuffsCodec::resetDecoder() {
677 if (!fStream->rewind()) {
678 return SkCodec::kInternalError;
679 }
Nigel Tao48aa2212019-03-09 14:59:11 +1100680 fIOBuffer.meta = wuffs_base__null_io_buffer_meta();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400681
682 SkCodec::Result result =
683 reset_and_decode_image_config(fDecoder.get(), nullptr, &fIOBuffer, fStream.get());
684 if (result == SkCodec::kIncompleteInput) {
685 return SkCodec::kInternalError;
686 } else if (result != SkCodec::kSuccess) {
687 return result;
688 }
689
690 fDecoderIsSuspended = false;
691 return SkCodec::kSuccess;
692}
693
694const char* SkWuffsCodec::decodeFrameConfig() {
695 while (true) {
Nigel Tao6af1edc2019-01-19 15:12:39 +1100696 const char* status = fDecoder->decode_frame_config(&fFrameConfig, fIOBuffer.reader());
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400697 if ((status == wuffs_base__suspension__short_read) &&
698 fill_buffer(&fIOBuffer, fStream.get())) {
699 continue;
700 }
701 fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
702 this->updateNumFullyReceivedFrames();
703 return status;
704 }
705}
706
707const char* SkWuffsCodec::decodeFrame() {
708 while (true) {
Nigel Tao48aa2212019-03-09 14:59:11 +1100709 const char* status =
710 fDecoder->decode_frame(&fPixelBuffer, fIOBuffer.reader(),
711 wuffs_base__make_slice_u8(fWorkbufPtr.get(), fWorkbufLen), NULL);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400712 if ((status == wuffs_base__suspension__short_read) &&
713 fill_buffer(&fIOBuffer, fStream.get())) {
714 continue;
715 }
716 fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
717 this->updateNumFullyReceivedFrames();
718 return status;
719 }
720}
721
722void SkWuffsCodec::updateNumFullyReceivedFrames() {
Nigel Tao6af1edc2019-01-19 15:12:39 +1100723 // num_decoded_frames's return value, n, can change over time, both up and
724 // down, as we seek back and forth in the underlying stream.
725 // fNumFullyReceivedFrames is the highest n we've seen.
726 uint64_t n = fDecoder->num_decoded_frames();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400727 if (fNumFullyReceivedFrames < n) {
728 fNumFullyReceivedFrames = n;
729 }
730}
731
732// -------------------------------- SkWuffsCodec.h functions
733
734bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
735 constexpr const char* gif_ptr = "GIF8";
736 constexpr size_t gif_len = 4;
737 return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
738}
739
740std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
741 SkCodec::Result* result) {
Nigel Tao48aa2212019-03-09 14:59:11 +1100742 uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
743 wuffs_base__io_buffer iobuf =
744 wuffs_base__make_io_buffer(wuffs_base__make_slice_u8(buffer, SK_WUFFS_CODEC_BUFFER_SIZE),
745 wuffs_base__null_io_buffer_meta());
746 wuffs_base__image_config imgcfg = wuffs_base__null_image_config();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400747
748 // Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
749 // the wuffs_base__etc types, the sizeof a file format specific type like
750 // GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
751 // type wuffs_gif__decoder*, then the supported API treats p as a pointer
752 // to an opaque type: a private implementation detail. The API is always
753 // "set_foo(p, etc)" and not "p->foo = etc".
754 //
755 // See https://en.wikipedia.org/wiki/Opaque_pointer#C
756 //
757 // Thus, we don't use C++'s new operator (which requires knowing the sizeof
758 // the struct at compile time). Instead, we use sk_malloc_canfail, with
759 // sizeof__wuffs_gif__decoder returning the appropriate value for the
760 // (statically or dynamically) linked version of the Wuffs library.
761 //
762 // As a C (not C++) library, none of the Wuffs types have constructors or
763 // destructors.
764 //
765 // In RAII style, we can still use std::unique_ptr with these pointers, but
766 // we pair the pointer with sk_free instead of C++'s delete.
767 void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
768 if (!decoder_raw) {
769 *result = SkCodec::kInternalError;
770 return nullptr;
771 }
772 std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
773 reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
774
775 SkCodec::Result reset_result =
776 reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
777 if (reset_result != SkCodec::kSuccess) {
778 *result = reset_result;
779 return nullptr;
780 }
781
782 uint32_t width = imgcfg.pixcfg.width();
783 uint32_t height = imgcfg.pixcfg.height();
784 if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
785 *result = SkCodec::kInvalidInput;
786 return nullptr;
787 }
788
Nigel Tao6af1edc2019-01-19 15:12:39 +1100789 uint64_t workbuf_len = decoder->workbuf_len().max_incl;
Nigel Tao22e86242019-01-26 16:04:01 +1100790 void* workbuf_ptr_raw = nullptr;
791 if (workbuf_len) {
792 workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
793 if (!workbuf_ptr_raw) {
794 *result = SkCodec::kInternalError;
795 return nullptr;
796 }
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400797 }
798 std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
799 reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
800
801 uint64_t pixbuf_len = imgcfg.pixcfg.pixbuf_len();
802 void* pixbuf_ptr_raw = pixbuf_len <= SIZE_MAX ? sk_malloc_canfail(pixbuf_len) : nullptr;
803 if (!pixbuf_ptr_raw) {
804 *result = SkCodec::kInternalError;
805 return nullptr;
806 }
807 std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr(
808 reinterpret_cast<uint8_t*>(pixbuf_ptr_raw), &sk_free);
Nigel Tao48aa2212019-03-09 14:59:11 +1100809 wuffs_base__pixel_buffer pixbuf = wuffs_base__null_pixel_buffer();
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400810
Nigel Tao48aa2212019-03-09 14:59:11 +1100811 const char* status = pixbuf.set_from_slice(
812 &imgcfg.pixcfg, wuffs_base__make_slice_u8(pixbuf_ptr.get(), SkToSizeT(pixbuf_len)));
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400813 if (status != nullptr) {
814 SkCodecPrintf("set_from_slice: %s", status);
815 *result = SkCodec::kInternalError;
816 return nullptr;
817 }
818
Nigel Tao490e6472019-02-14 14:50:53 +1100819 SkEncodedInfo::Color color =
820 (imgcfg.pixcfg.pixel_format() == WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
821 ? SkEncodedInfo::kBGRA_Color
822 : SkEncodedInfo::kRGBA_Color;
823
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400824 // In Skia's API, the alpha we calculate here and return is only for the
825 // first frame.
826 SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
827 : SkEncodedInfo::kBinary_Alpha;
828
Nigel Tao490e6472019-02-14 14:50:53 +1100829 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(width, height, color, alpha, 8);
Leon Scroggins IIIe93ec682018-10-26 09:25:51 -0400830
831 *result = SkCodec::kSuccess;
832 return std::unique_ptr<SkCodec>(new SkWuffsCodec(
833 std::move(encodedInfo), std::move(stream), std::move(decoder), std::move(pixbuf_ptr),
834 std::move(workbuf_ptr), workbuf_len, imgcfg, pixbuf, iobuf));
835}