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