Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 8 | #ifndef SkHeifCodec_DEFINED |
| 9 | #define SkHeifCodec_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/codec/SkCodec.h" |
| 12 | #include "include/codec/SkEncodedOrigin.h" |
| 13 | #include "include/core/SkImageInfo.h" |
| 14 | #include "include/core/SkStream.h" |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 15 | #include "src/codec/SkFrameHolder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/codec/SkSwizzler.h" |
Mike Klein | 36f182f | 2017-10-20 12:33:53 -0400 | [diff] [blame] | 17 | |
Mike Klein | 36f182f | 2017-10-20 12:33:53 -0400 | [diff] [blame] | 18 | #if __has_include("HeifDecoderAPI.h") |
| 19 | #include "HeifDecoderAPI.h" |
| 20 | #else |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/codec/SkStubHeifDecoderAPI.h" |
Mike Klein | 36f182f | 2017-10-20 12:33:53 -0400 | [diff] [blame] | 22 | #endif |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 23 | |
| 24 | class SkHeifCodec : public SkCodec { |
| 25 | public: |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 26 | /* |
| 27 | * Returns true if one of kHEIF or kAVIF images were detected. If |format| |
| 28 | * is not nullptr, it will contain the detected format. Returns false |
| 29 | * otherwise. |
| 30 | */ |
| 31 | static bool IsSupported(const void*, size_t, SkEncodedImageFormat* format); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 32 | |
| 33 | /* |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 34 | * Assumes IsSupported was called and it returned a non-nullopt value. |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 35 | */ |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 36 | static std::unique_ptr<SkCodec> MakeFromStream( |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 37 | std::unique_ptr<SkStream>, SkCodec::SelectionPolicy selectionPolicy, |
| 38 | SkEncodedImageFormat, Result*); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 39 | |
| 40 | protected: |
| 41 | |
| 42 | Result onGetPixels( |
| 43 | const SkImageInfo& dstInfo, |
| 44 | void* dst, size_t dstRowBytes, |
| 45 | const Options& options, |
| 46 | int* rowsDecoded) override; |
| 47 | |
| 48 | SkEncodedImageFormat onGetEncodedFormat() const override { |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 49 | return fFormat; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 52 | int onGetFrameCount() override; |
| 53 | bool onGetFrameInfo(int, FrameInfo*) const override; |
| 54 | int onGetRepetitionCount() override; |
| 55 | const SkFrameHolder* getFrameHolder() const override { |
| 56 | return &fFrameHolder; |
| 57 | } |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 58 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 59 | bool conversionSupported(const SkImageInfo&, bool, bool) override; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 60 | |
Leon Scroggins III | bcc7532 | 2019-04-15 10:34:53 -0400 | [diff] [blame] | 61 | bool onRewind() override; |
| 62 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 63 | private: |
| 64 | /* |
| 65 | * Creates an instance of the decoder |
| 66 | * Called only by NewFromStream |
| 67 | */ |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 68 | SkHeifCodec(SkEncodedInfo&&, HeifDecoder*, SkEncodedOrigin, bool animation, |
| 69 | SkEncodedImageFormat); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 70 | |
| 71 | void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options); |
| 72 | void allocateStorage(const SkImageInfo& dstInfo); |
| 73 | int readRows(const SkImageInfo& dstInfo, void* dst, |
| 74 | size_t rowBytes, int count, const Options&); |
| 75 | |
| 76 | /* |
| 77 | * Scanline decoding. |
| 78 | */ |
| 79 | SkSampler* getSampler(bool createIfNecessary) override; |
| 80 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 81 | const Options& options) override; |
| 82 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 83 | bool onSkipScanlines(int count) override; |
| 84 | |
| 85 | std::unique_ptr<HeifDecoder> fHeifDecoder; |
| 86 | HeifFrameInfo fFrameInfo; |
| 87 | SkAutoTMalloc<uint8_t> fStorage; |
| 88 | uint8_t* fSwizzleSrcRow; |
| 89 | uint32_t* fColorXformSrcRow; |
| 90 | |
| 91 | std::unique_ptr<SkSwizzler> fSwizzler; |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 92 | bool fUseAnimation; |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 93 | const SkEncodedImageFormat fFormat; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 94 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 95 | class Frame : public SkFrame { |
| 96 | public: |
| 97 | Frame(int i) : INHERITED(i) {} |
| 98 | |
| 99 | protected: |
| 100 | SkEncodedInfo::Alpha onReportedAlpha() const override { |
| 101 | return SkEncodedInfo::Alpha::kOpaque_Alpha; |
| 102 | } |
| 103 | |
| 104 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 105 | using INHERITED = SkFrame; |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | class FrameHolder : public SkFrameHolder { |
| 109 | public: |
| 110 | ~FrameHolder() override {} |
| 111 | void setScreenSize(int w, int h) { |
| 112 | fScreenWidth = w; |
| 113 | fScreenHeight = h; |
| 114 | } |
| 115 | Frame* appendNewFrame(); |
| 116 | const Frame* frame(int i) const; |
Chong Zhang | ea68eac | 2019-08-26 11:28:38 -0700 | [diff] [blame] | 117 | Frame* editFrameAt(int i); |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 118 | int size() const { |
| 119 | return static_cast<int>(fFrames.size()); |
| 120 | } |
| 121 | void reserve(int size) { |
| 122 | fFrames.reserve(size); |
| 123 | } |
| 124 | |
| 125 | protected: |
| 126 | const SkFrame* onGetFrame(int i) const override; |
| 127 | |
| 128 | private: |
| 129 | std::vector<Frame> fFrames; |
| 130 | }; |
| 131 | |
| 132 | FrameHolder fFrameHolder; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 133 | using INHERITED = SkCodec; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 134 | }; |
| 135 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 136 | #endif // SkHeifCodec_DEFINED |