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