SkFrameHolder.h 5.2 KB

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