SkAnimatedImage.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2018 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 SkAnimatedImage_DEFINED
  8. #define SkAnimatedImage_DEFINED
  9. #include "SkBitmap.h"
  10. #include "SkCodecAnimation.h"
  11. #include "SkDrawable.h"
  12. #include "SkMatrix.h"
  13. #include "SkRect.h"
  14. class SkAndroidCodec;
  15. class SkPicture;
  16. /**
  17. * Thread unsafe drawable for drawing animated images (e.g. GIF).
  18. */
  19. class SK_API SkAnimatedImage : public SkDrawable {
  20. public:
  21. /**
  22. * Create an SkAnimatedImage from the SkAndroidCodec.
  23. *
  24. * Returns null on failure to allocate pixels. On success, this will
  25. * decode the first frame.
  26. *
  27. * @param scaledSize Size to draw the image, possibly requiring scaling.
  28. * @param cropRect Rectangle to crop to after scaling.
  29. * @param postProcess Picture to apply after scaling and cropping.
  30. */
  31. static sk_sp<SkAnimatedImage> Make(std::unique_ptr<SkAndroidCodec>,
  32. SkISize scaledSize, SkIRect cropRect, sk_sp<SkPicture> postProcess);
  33. /**
  34. * Simpler version that uses the default size, no cropping, and no postProcess.
  35. */
  36. static sk_sp<SkAnimatedImage> Make(std::unique_ptr<SkAndroidCodec>);
  37. ~SkAnimatedImage() override;
  38. /**
  39. * Reset the animation to the beginning.
  40. */
  41. void reset();
  42. /**
  43. * Whether the animation completed.
  44. *
  45. * Returns true after all repetitions are complete, or an error stops the
  46. * animation. Gets reset to false if the animation is restarted.
  47. */
  48. bool isFinished() const { return fFinished; }
  49. /**
  50. * Returned by decodeNextFrame and currentFrameDuration if the animation
  51. * is not running.
  52. */
  53. static constexpr int kFinished = -1;
  54. /**
  55. * Decode the next frame.
  56. *
  57. * If the animation is on the last frame or has hit an error, returns
  58. * kFinished.
  59. */
  60. int decodeNextFrame();
  61. /**
  62. * How long to display the current frame.
  63. *
  64. * Useful for the first frame, for which decodeNextFrame is called
  65. * internally.
  66. */
  67. int currentFrameDuration() {
  68. return fCurrentFrameDuration;
  69. }
  70. /**
  71. * Change the repetition count.
  72. *
  73. * By default, the image will repeat the number of times indicated in the
  74. * encoded data.
  75. *
  76. * Use SkCodec::kRepetitionCountInfinite for infinite, and 0 to show all
  77. * frames once and then stop.
  78. */
  79. void setRepetitionCount(int count);
  80. /**
  81. * Return the currently set repetition count.
  82. */
  83. int getRepetitionCount() const {
  84. return fRepetitionCount;
  85. }
  86. protected:
  87. SkRect onGetBounds() override;
  88. void onDraw(SkCanvas*) override;
  89. private:
  90. struct Frame {
  91. SkBitmap fBitmap;
  92. int fIndex;
  93. SkCodecAnimation::DisposalMethod fDisposalMethod;
  94. // init() may have to create a new SkPixelRef, if the
  95. // current one is already in use by another owner (e.g.
  96. // an SkPicture). This determines whether to copy the
  97. // existing one to the new one.
  98. enum class OnInit {
  99. // Restore the image from the old SkPixelRef to the
  100. // new one.
  101. kRestoreIfNecessary,
  102. // No need to restore.
  103. kNoRestore,
  104. };
  105. Frame();
  106. bool init(const SkImageInfo& info, OnInit);
  107. bool copyTo(Frame*) const;
  108. };
  109. std::unique_ptr<SkAndroidCodec> fCodec;
  110. const SkISize fScaledSize;
  111. const SkImageInfo fDecodeInfo;
  112. const SkIRect fCropRect;
  113. const sk_sp<SkPicture> fPostProcess;
  114. const int fFrameCount;
  115. const bool fSimple; // no crop, scale, or postprocess
  116. SkMatrix fMatrix; // used only if !fSimple
  117. bool fFinished;
  118. int fCurrentFrameDuration;
  119. Frame fDisplayFrame;
  120. Frame fDecodingFrame;
  121. Frame fRestoreFrame;
  122. int fRepetitionCount;
  123. int fRepetitionsCompleted;
  124. SkAnimatedImage(std::unique_ptr<SkAndroidCodec>, SkISize scaledSize,
  125. SkImageInfo decodeInfo, SkIRect cropRect, sk_sp<SkPicture> postProcess);
  126. SkAnimatedImage(std::unique_ptr<SkAndroidCodec>);
  127. int computeNextFrame(int current, bool* animationEnded);
  128. double finish();
  129. typedef SkDrawable INHERITED;
  130. };
  131. #endif // SkAnimatedImage_DEFINED