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