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