SkWuffsCodec.cpp 35 KB

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