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