| 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 SkFrameHolder_DEFINED |
| 9 |
#define SkFrameHolder_DEFINED |
| 10 |
|
| 11 |
#include "include/codec/SkCodec.h" |
| 12 |
#include "include/codec/SkCodecAnimation.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkTypes.h" |
| 15 |
#include "include/private/SkEncodedInfo.h" |
| 16 |
#include "include/private/base/SkNoncopyable.h" |
| 17 |
|
| 18 |
/** |
| 19 |
* Base class for a single frame of an animated image. |
| 20 |
* |
| 21 |
* Separate from SkCodec::FrameInfo, which is a pared down |
| 22 |
* interface that only contains the info the client needs. |
| 23 |
*/ |
| 24 |
class SkFrame : public SkNoncopyable { |
| 25 |
public: |
| 26 |
explicit SkFrame(int id) |
| 27 |
: fId(id) |
| 28 |
, fHasAlpha(false) |
| 29 |
, fRequiredFrame(kUninitialized) |
| 30 |
, fDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep) |
| 31 |
, fDuration(0) |
| 32 |
, fBlend(SkCodecAnimation::Blend::kSrcOver) { |
| 33 |
fRect.setEmpty(); |
| 34 |
} |
| 35 |
|
| 36 |
virtual ~SkFrame() {} |
| 37 |
|
| 38 |
/** |
| 39 |
* An explicit move constructor, as |
| 40 |
* https://en.cppreference.com/w/cpp/language/move_constructor says that |
| 41 |
* there is no implicit move constructor if there are user-declared |
| 42 |
* destructors, and we have one, immediately above. |
| 43 |
* |
| 44 |
* Without a move constructor, it is harder to use an SkFrame, or an |
| 45 |
* SkFrame subclass, inside a std::vector. |
| 46 |
*/ |
| 47 |
SkFrame(SkFrame&&) = default; |
| 48 |
|
| 49 |
/** |
| 50 |
* 0-based index of the frame in the image sequence. |
| 51 |
*/ |
| 52 |
int frameId() const { return fId; } |
| 53 |
|
| 54 |
/** |
| 55 |
* How this frame reports its alpha. |
| 56 |
* |
| 57 |
* This only considers the rectangle of this frame, and |
| 58 |
* considers it to have alpha even if it is opaque once |
| 59 |
* blended with the frame behind it. |
| 60 |
*/ |
| 61 |
SkEncodedInfo::Alpha reportedAlpha() const { |
| 62 |
return this->onReportedAlpha(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Cached value representing whether the frame has alpha, |
| 67 |
* after compositing with the prior frame. |
| 68 |
*/ |
| 69 |
bool hasAlpha() const { return fHasAlpha; } |
| 70 |
|
| 71 |
/** |
| 72 |
* Cache whether the finished frame has alpha. |
| 73 |
*/ |
| 74 |
void setHasAlpha(bool alpha) { fHasAlpha = alpha; } |
| 75 |
|
| 76 |
/** |
| 77 |
* Whether enough of the frame has been read to determine |
| 78 |
* fRequiredFrame and fHasAlpha. |
| 79 |
*/ |
| 80 |
bool reachedStartOfData() const { return fRequiredFrame != kUninitialized; } |
| 81 |
|
| 82 |
/** |
| 83 |
* The frame this one depends on. |
| 84 |
* |
| 85 |
* Must not be called until fRequiredFrame has been set properly. |
| 86 |
*/ |
| 87 |
int getRequiredFrame() const { |
| 88 |
SkASSERT(this->reachedStartOfData()); |
| 89 |
return fRequiredFrame; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Set the frame that this frame depends on. |
| 94 |
*/ |
| 95 |
void setRequiredFrame(int req) { fRequiredFrame = req; } |
| 96 |
|
| 97 |
/** |
| 98 |
* Set the rectangle that is updated by this frame. |
| 99 |
*/ |
| 100 |
void setXYWH(int x, int y, int width, int height) { |
| 101 |
fRect.setXYWH(x, y, width, height); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* The rectangle that is updated by this frame. |
| 106 |
*/ |
| 107 |
SkIRect frameRect() const { return fRect; } |
| 108 |
|
| 109 |
int xOffset() const { return fRect.x(); } |
| 110 |
int yOffset() const { return fRect.y(); } |
| 111 |
int width() const { return fRect.width(); } |
| 112 |
int height() const { return fRect.height(); } |
| 113 |
|
| 114 |
SkCodecAnimation::DisposalMethod getDisposalMethod() const { |
| 115 |
return fDisposalMethod; |
| 116 |
} |
| 117 |
|
| 118 |
void setDisposalMethod(SkCodecAnimation::DisposalMethod disposalMethod) { |
| 119 |
fDisposalMethod = disposalMethod; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Set the duration (in ms) to show this frame. |
| 124 |
*/ |
| 125 |
void setDuration(int duration) { |
| 126 |
fDuration = duration; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Duration in ms to show this frame. |
| 131 |
*/ |
| 132 |
int getDuration() const { |
| 133 |
return fDuration; |
| 134 |
} |
| 135 |
|
| 136 |
void setBlend(SkCodecAnimation::Blend blend) { |
| 137 |
fBlend = blend; |
| 138 |
} |
| 139 |
|
| 140 |
SkCodecAnimation::Blend getBlend() const { |
| 141 |
return fBlend; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Fill in the FrameInfo with details from this object. |
| 146 |
*/ |
| 147 |
void fillIn(SkCodec::FrameInfo*, bool fullyReceived) const; |
| 148 |
|
| 149 |
protected: |
| 150 |
virtual SkEncodedInfo::Alpha onReportedAlpha() const = 0; |
| 151 |
|
| 152 |
private: |
| 153 |
inline static constexpr int kUninitialized = -2; |
| 154 |
|
| 155 |
const int fId; |
| 156 |
bool fHasAlpha; |
| 157 |
int fRequiredFrame; |
| 158 |
SkIRect fRect; |
| 159 |
SkCodecAnimation::DisposalMethod fDisposalMethod; |
| 160 |
int fDuration; |
| 161 |
SkCodecAnimation::Blend fBlend; |
| 162 |
}; |
| 163 |
|
| 164 |
/** |
| 165 |
* Base class for an object which holds the SkFrames of an |
| 166 |
* image sequence. |
| 167 |
*/ |
| 168 |
class SkFrameHolder : public SkNoncopyable { |
| 169 |
public: |
| 170 |
SkFrameHolder() |
| 171 |
: fScreenWidth(0) |
| 172 |
, fScreenHeight(0) |
| 173 |
{} |
| 174 |
|
| 175 |
virtual ~SkFrameHolder() {} |
| 176 |
|
| 177 |
/** |
| 178 |
* Size of the image. Each frame will be contained in |
| 179 |
* these dimensions (possibly after clipping). |
| 180 |
*/ |
| 181 |
int screenWidth() const { return fScreenWidth; } |
| 182 |
int screenHeight() const { return fScreenHeight; } |
| 183 |
|
| 184 |
/** |
| 185 |
* Compute the opacity and required frame, based on |
| 186 |
* the frame's reportedAlpha and how it blends |
| 187 |
* with prior frames. |
| 188 |
*/ |
| 189 |
void setAlphaAndRequiredFrame(SkFrame*); |
| 190 |
|
| 191 |
/** |
| 192 |
* Return the frame with frameId i. |
| 193 |
*/ |
| 194 |
const SkFrame* getFrame(int i) const { |
| 195 |
return this->onGetFrame(i); |
| 196 |
} |
| 197 |
|
| 198 |
protected: |
| 199 |
int fScreenWidth; |
| 200 |
int fScreenHeight; |
| 201 |
|
| 202 |
virtual const SkFrame* onGetFrame(int i) const = 0; |
| 203 |
}; |
| 204 |
|
| 205 |
#endif // SkFrameHolder_DEFINED |