| 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 |
|
| 51 |
SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kAVIF; } |
| 52 |
|
| 53 |
int onGetFrameCount() override; |
| 54 |
bool onGetFrameInfo(int, FrameInfo*) const override; |
| 55 |
int onGetRepetitionCount() override; |
| 56 |
IsAnimated onIsAnimated() override; |
| 57 |
const SkFrameHolder* getFrameHolder() const override { return &fFrameHolder; } |
| 58 |
|
| 59 |
private: |
| 60 |
SkAvifCodec(SkEncodedInfo&&, |
| 61 |
std::unique_ptr<SkStream>, |
| 62 |
sk_sp<const SkData>, |
| 63 |
AvifDecoder, |
| 64 |
SkEncodedOrigin, |
| 65 |
bool); |
| 66 |
|
| 67 |
// fAvifDecoder has a pointer to this data. This should not be freed until |
| 68 |
// the decode is completed. To ensure that, we declare this before |
| 69 |
// fAvifDecoder. |
| 70 |
sk_sp<const SkData> fData; |
| 71 |
|
| 72 |
AvifDecoder fAvifDecoder; |
| 73 |
bool fUseAnimation; |
| 74 |
|
| 75 |
class Frame : public SkFrame { |
| 76 |
public: |
| 77 |
Frame(int i, SkEncodedInfo::Alpha alpha) : INHERITED(i), fReportedAlpha(alpha) {} |
| 78 |
|
| 79 |
protected: |
| 80 |
SkEncodedInfo::Alpha onReportedAlpha() const override { return fReportedAlpha; } |
| 81 |
|
| 82 |
private: |
| 83 |
const SkEncodedInfo::Alpha fReportedAlpha; |
| 84 |
|
| 85 |
using INHERITED = SkFrame; |
| 86 |
}; |
| 87 |
|
| 88 |
class FrameHolder : public SkFrameHolder { |
| 89 |
public: |
| 90 |
~FrameHolder() override {} |
| 91 |
void setScreenSize(int w, int h) { |
| 92 |
fScreenWidth = w; |
| 93 |
fScreenHeight = h; |
| 94 |
} |
| 95 |
Frame* appendNewFrame(bool hasAlpha); |
| 96 |
const Frame* frame(int i) const; |
| 97 |
int size() const { return static_cast<int>(fFrames.size()); } |
| 98 |
void reserve(int size) { fFrames.reserve(size); } |
| 99 |
|
| 100 |
protected: |
| 101 |
const SkFrame* onGetFrame(int i) const override; |
| 102 |
|
| 103 |
private: |
| 104 |
std::vector<Frame> fFrames; |
| 105 |
}; |
| 106 |
|
| 107 |
FrameHolder fFrameHolder; |
| 108 |
using INHERITED = SkScalingCodec; |
| 109 |
}; |
| 110 |
|
| 111 |
#endif // SkAvifCodec_DEFINED |