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