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 11 #include "include/codec/SkCodec.h" 12 #include "include/codec/SkEncodedOrigin.h" 13 #include "include/core/SkImageInfo.h" 14 #include "include/core/SkStream.h" 15 #include "src/codec/SkFrameHolder.h" 16 #include "src/codec/SkSwizzler.h" 17 18 #if __has_include("HeifDecoderAPI.h") 19 #include "HeifDecoderAPI.h" 20 #else 21 #include "src/codec/SkStubHeifDecoderAPI.h" 22 #endif 23 24 class SkHeifCodec : public SkCodec { 25 public: 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); 32 33 /* 34 * Assumes IsSupported was called and it returned a non-nullopt value. 35 */ 36 static std::unique_ptr<SkCodec> MakeFromStream( 37 std::unique_ptr<SkStream>, SkCodec::SelectionPolicy selectionPolicy, 38 SkEncodedImageFormat, Result*); 39 getHeifContext()40 void *getHeifContext() override { 41 return fHeifDecoder.get(); 42 } 43 44 protected: 45 46 Result onGetPixels( 47 const SkImageInfo& dstInfo, 48 void* dst, size_t dstRowBytes, 49 const Options& options, 50 int* rowsDecoded) override; 51 onGetEncodedFormat()52 SkEncodedImageFormat onGetEncodedFormat() const override { 53 return fFormat; 54 } 55 56 int onGetFrameCount() override; 57 bool onGetFrameInfo(int, FrameInfo*) const override; 58 int onGetRepetitionCount() override; getFrameHolder()59 const SkFrameHolder* getFrameHolder() const override { 60 return &fFrameHolder; 61 } 62 63 bool conversionSupported(const SkImageInfo&, bool, bool) override; 64 65 bool onRewind() override; 66 67 private: 68 /* 69 * Creates an instance of the decoder 70 * Called only by NewFromStream 71 */ 72 SkHeifCodec(SkEncodedInfo&&, HeifDecoder*, SkEncodedOrigin, bool animation, 73 SkEncodedImageFormat); 74 75 void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options); 76 void allocateStorage(const SkImageInfo& dstInfo); 77 int readRows(const SkImageInfo& dstInfo, void* dst, 78 size_t rowBytes, int count, const Options&); 79 80 /* 81 * Scanline decoding. 82 */ 83 SkSampler* getSampler(bool createIfNecessary) override; 84 Result onStartScanlineDecode(const SkImageInfo& dstInfo, 85 const Options& options) override; 86 int onGetScanlines(void* dst, int count, size_t rowBytes) override; 87 bool onSkipScanlines(int count) override; 88 static HeifDecoder* createHeifDecoder(); 89 90 std::unique_ptr<HeifDecoder> fHeifDecoder; 91 HeifFrameInfo fFrameInfo; 92 SkAutoTMalloc<uint8_t> fStorage; 93 uint8_t* fSwizzleSrcRow; 94 uint32_t* fColorXformSrcRow; 95 96 std::unique_ptr<SkSwizzler> fSwizzler; 97 bool fUseAnimation; 98 const SkEncodedImageFormat fFormat; 99 static void *heifImplHandle; 100 101 class Frame : public SkFrame { 102 public: Frame(int i)103 Frame(int i) : INHERITED(i) {} 104 105 protected: onReportedAlpha()106 SkEncodedInfo::Alpha onReportedAlpha() const override { 107 return SkEncodedInfo::Alpha::kOpaque_Alpha; 108 } 109 110 private: 111 using INHERITED = SkFrame; 112 }; 113 114 class FrameHolder : public SkFrameHolder { 115 public: ~FrameHolder()116 ~FrameHolder() override {} setScreenSize(int w,int h)117 void setScreenSize(int w, int h) { 118 fScreenWidth = w; 119 fScreenHeight = h; 120 } 121 Frame* appendNewFrame(); 122 const Frame* frame(int i) const; 123 Frame* editFrameAt(int i); size()124 int size() const { 125 return static_cast<int>(fFrames.size()); 126 } reserve(int size)127 void reserve(int size) { 128 fFrames.reserve(size); 129 } 130 131 protected: 132 const SkFrame* onGetFrame(int i) const override; 133 134 private: 135 std::vector<Frame> fFrames; 136 }; 137 138 FrameHolder fFrameHolder; 139 using INHERITED = SkCodec; 140 }; 141 142 #endif // SkHeifCodec_DEFINED 143