1 /* 2 * Copyright 2022 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 SkAvifCodec_DEFINED 9 #define SkAvifCodec_DEFINED 10 11 #include "include/codec/SkEncodedImageFormat.h" 12 #include "include/codec/SkEncodedOrigin.h" 13 #include "include/core/SkData.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/private/SkEncodedInfo.h" 16 #include "src/codec/SkFrameHolder.h" 17 #include "src/codec/SkScalingCodec.h" 18 19 #include <cstddef> 20 #include <memory> 21 #include <vector> 22 23 class SkCodec; 24 class SkStream; 25 struct SkImageInfo; 26 struct avifDecoder; 27 struct AvifDecoderDeleter { 28 void operator()(avifDecoder* decoder) const; 29 }; 30 using AvifDecoder = std::unique_ptr<avifDecoder, AvifDecoderDeleter>; 31 32 class SkAvifCodec : public SkScalingCodec { 33 public: 34 /* 35 * Returns true if an AVIF image is detected. Returns false otherwise. 36 */ 37 static bool IsAvif(const void*, size_t); 38 39 /* 40 * Assumes IsAvif() was called and it returned true. 41 */ 42 static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); 43 44 protected: 45 Result onGetPixels(const SkImageInfo& dstInfo, 46 void* dst, 47 size_t dstRowBytes, 48 const Options& options, 49 int* rowsDecoded) override; 50 onGetEncodedFormat()51 SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kAVIF; } 52 53 int onGetFrameCount() override; 54 bool onGetFrameInfo(int, FrameInfo*) const override; 55 int onGetRepetitionCount() override; getFrameHolder()56 const SkFrameHolder* getFrameHolder() const override { return &fFrameHolder; } 57 58 private: 59 SkAvifCodec(SkEncodedInfo&&, 60 std::unique_ptr<SkStream>, 61 sk_sp<SkData>, 62 AvifDecoder, 63 SkEncodedOrigin, 64 bool); 65 66 // fAvifDecoder has a pointer to this data. This should not be freed until 67 // the decode is completed. To ensure that, we declare this before 68 // fAvifDecoder. 69 sk_sp<SkData> fData; 70 71 AvifDecoder fAvifDecoder; 72 bool fUseAnimation; 73 74 class Frame : public SkFrame { 75 public: Frame(int i,SkEncodedInfo::Alpha alpha)76 Frame(int i, SkEncodedInfo::Alpha alpha) : INHERITED(i), fReportedAlpha(alpha) {} 77 78 protected: onReportedAlpha()79 SkEncodedInfo::Alpha onReportedAlpha() const override { return fReportedAlpha; } 80 81 private: 82 const SkEncodedInfo::Alpha fReportedAlpha; 83 84 using INHERITED = SkFrame; 85 }; 86 87 class FrameHolder : public SkFrameHolder { 88 public: ~FrameHolder()89 ~FrameHolder() override {} setScreenSize(int w,int h)90 void setScreenSize(int w, int h) { 91 fScreenWidth = w; 92 fScreenHeight = h; 93 } 94 Frame* appendNewFrame(bool hasAlpha); 95 const Frame* frame(int i) const; size()96 int size() const { return static_cast<int>(fFrames.size()); } reserve(int size)97 void reserve(int size) { fFrames.reserve(size); } 98 99 protected: 100 const SkFrame* onGetFrame(int i) const override; 101 102 private: 103 std::vector<Frame> fFrames; 104 }; 105 106 FrameHolder fFrameHolder; 107 using INHERITED = SkScalingCodec; 108 }; 109 110 #endif // SkAvifCodec_DEFINED 111