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