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