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; 56 IsAnimated onIsAnimated() override; getFrameHolder()57 const SkFrameHolder* getFrameHolder() const override { 58 return &fFrameHolder; 59 } 60 61 bool conversionSupported(const SkImageInfo&, bool, bool) override; 62 63 bool onRewind() override; 64 65 private: 66 /* 67 * Creates an instance of the decoder 68 * Called only by NewFromStream 69 */ 70 SkHeifCodec(SkEncodedInfo&&, HeifDecoder*, SkEncodedOrigin, bool animation, 71 SkEncodedImageFormat); 72 73 void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options); 74 void allocateStorage(const SkImageInfo& dstInfo); 75 int readRows(const SkImageInfo& dstInfo, void* dst, 76 size_t rowBytes, int count, const Options&); 77 78 /* 79 * Scanline decoding. 80 */ 81 SkSampler* getSampler(bool createIfNecessary) override; 82 Result onStartScanlineDecode(const SkImageInfo& dstInfo, 83 const Options& options) override; 84 int onGetScanlines(void* dst, int count, size_t rowBytes) override; 85 bool onSkipScanlines(int count) override; 86 87 std::unique_ptr<HeifDecoder> fHeifDecoder; 88 HeifFrameInfo fFrameInfo; 89 skia_private::AutoTMalloc<uint8_t> fStorage; 90 uint8_t* fSwizzleSrcRow; 91 uint32_t* fColorXformSrcRow; 92 93 std::unique_ptr<SkSwizzler> fSwizzler; 94 bool fUseAnimation; 95 const SkEncodedImageFormat fFormat; 96 97 class Frame : public SkFrame { 98 public: Frame(int i)99 Frame(int i) : INHERITED(i) {} 100 101 protected: onReportedAlpha()102 SkEncodedInfo::Alpha onReportedAlpha() const override { 103 return SkEncodedInfo::Alpha::kOpaque_Alpha; 104 } 105 106 private: 107 using INHERITED = SkFrame; 108 }; 109 110 class FrameHolder : public SkFrameHolder { 111 public: ~FrameHolder()112 ~FrameHolder() override {} setScreenSize(int w,int h)113 void setScreenSize(int w, int h) { 114 fScreenWidth = w; 115 fScreenHeight = h; 116 } 117 Frame* appendNewFrame(); 118 const Frame* frame(int i) const; 119 Frame* editFrameAt(int i); size()120 int size() const { 121 return static_cast<int>(fFrames.size()); 122 } reserve(int size)123 void reserve(int size) { 124 fFrames.reserve(size); 125 } 126 127 protected: 128 const SkFrame* onGetFrame(int i) const override; 129 130 private: 131 std::vector<Frame> fFrames; 132 }; 133 134 FrameHolder fFrameHolder; 135 using INHERITED = SkCodec; 136 }; 137 138 #endif // SkHeifCodec_DEFINED 139