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