lottie_animation.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "lottie/lottie_common.h"
  9. #include "base/weak_ptr.h"
  10. #include <QtGui/QImage>
  11. #include <variant>
  12. class QString;
  13. class QByteArray;
  14. namespace rlottie {
  15. class Animation;
  16. } // namespace rlottie
  17. namespace Lottie {
  18. class Player;
  19. class SharedState;
  20. class FrameRenderer;
  21. class FrameProvider;
  22. std::shared_ptr<FrameRenderer> MakeFrameRenderer();
  23. QImage ReadThumbnail(const QByteArray &content);
  24. namespace details {
  25. using InitData = std::variant<std::unique_ptr<SharedState>, Error>;
  26. } // namespace details
  27. class Animation final : public base::has_weak_ptr {
  28. public:
  29. struct FrameInfo {
  30. QImage image;
  31. int index = 0;
  32. };
  33. Animation(
  34. not_null<Player*> player,
  35. const QByteArray &content,
  36. const FrameRequest &request,
  37. Quality quality,
  38. const ColorReplacements *replacements = nullptr);
  39. Animation(
  40. not_null<Player*> player,
  41. FnMut<void(FnMut<void(QByteArray &&cached)>)> get, // Main thread.
  42. FnMut<void(QByteArray &&cached)> put, // Unknown thread.
  43. const QByteArray &content,
  44. const FrameRequest &request,
  45. Quality quality,
  46. const ColorReplacements *replacements = nullptr);
  47. Animation( // Multi-cache version.
  48. not_null<Player*> player,
  49. int keysCount,
  50. FnMut<void(int, FnMut<void(QByteArray &&)>)> get,
  51. FnMut<void(int, QByteArray &&)> put, // Unknown thread.
  52. const QByteArray &content,
  53. const FrameRequest &request,
  54. Quality quality,
  55. const ColorReplacements *replacements = nullptr);
  56. Animation( // Thread-safe version.
  57. not_null<Player*> player,
  58. std::shared_ptr<FrameProvider> provider,
  59. const FrameRequest &request);
  60. [[nodiscard]] bool ready() const;
  61. [[nodiscard]] QImage frame() const;
  62. [[nodiscard]] QImage frame(const FrameRequest &request) const;
  63. [[nodiscard]] FrameInfo frameInfo(const FrameRequest &request) const;
  64. [[nodiscard]] int frameIndex() const;
  65. [[nodiscard]] int framesCount() const;
  66. [[nodiscard]] Information information() const;
  67. private:
  68. void initDone(details::InitData &&data);
  69. void parseDone(std::unique_ptr<SharedState> state);
  70. void parseFailed(Error error);
  71. const not_null<Player*> _player;
  72. SharedState *_state = nullptr;
  73. };
  74. [[nodiscard]] std::optional<Error> ContentError(const QByteArray &content);
  75. } // namespace Lottie