lottie_multi_player.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_player.h"
  9. #include "base/timer.h"
  10. #include "base/algorithm.h"
  11. #include "base/flat_set.h"
  12. #include "base/flat_map.h"
  13. #include <rpl/event_stream.h>
  14. namespace Lottie {
  15. class Animation;
  16. class FrameRenderer;
  17. struct MultiUpdate {
  18. //std::variant<
  19. // std::pair<Animation*, Information>,
  20. // DisplayMultiFrameRequest,
  21. // std::pair<Animation*, Error>> data;
  22. };
  23. class MultiPlayer final : public Player {
  24. public:
  25. MultiPlayer(
  26. Quality quality = Quality::Default,
  27. std::shared_ptr<FrameRenderer> renderer = nullptr);
  28. ~MultiPlayer();
  29. void start(
  30. not_null<Animation*> animation,
  31. std::unique_ptr<SharedState> state) override;
  32. void failed(not_null<Animation*> animation, Error error) override;
  33. void updateFrameRequest(
  34. not_null<const Animation*> animation,
  35. const FrameRequest &request) override;
  36. bool markFrameShown() override;
  37. void checkStep() override;
  38. not_null<Animation*> append(
  39. const QByteArray &content,
  40. const FrameRequest &request);
  41. not_null<Animation*> append(
  42. FnMut<void(FnMut<void(QByteArray &&cached)>)> get, // Main thread.
  43. FnMut<void(QByteArray &&cached)> put, // Unknown thread.
  44. const QByteArray &content,
  45. const FrameRequest &request);
  46. rpl::producer<MultiUpdate> updates() const;
  47. void remove(not_null<Animation*> animation);
  48. void pause(not_null<Animation*> animation);
  49. void unpause(not_null<Animation*> animation);
  50. private:
  51. struct PausedInfo {
  52. not_null<SharedState*> state;
  53. crl::time pauseTime = kTimeUnknown;
  54. crl::time pauseDelay = kTimeUnknown;
  55. };
  56. struct StartingInfo {
  57. std::unique_ptr<SharedState> state;
  58. bool paused = false;
  59. };
  60. void addNewToActive(
  61. not_null<Animation*> animation,
  62. StartingInfo &&info);
  63. [[nodiscard]] int countFrameIndex(
  64. not_null<SharedState*> state,
  65. crl::time time,
  66. crl::time delay) const;
  67. void startAtRightTime(std::unique_ptr<SharedState> state);
  68. void processPending();
  69. void markFrameDisplayed(crl::time now);
  70. void addTimelineDelay(crl::time delayed);
  71. void checkNextFrameAvailability();
  72. void checkNextFrameRender();
  73. void unpauseFirst(
  74. not_null<Animation*> animation,
  75. not_null<SharedState*> state);
  76. void pauseAndSaveState(not_null<Animation*> animation);
  77. void unpauseAndKeepUp(not_null<Animation*> animation);
  78. void removeNow(not_null<Animation*> animation);
  79. Quality _quality = Quality::Default;
  80. base::Timer _timer;
  81. const std::shared_ptr<FrameRenderer> _renderer;
  82. std::vector<std::unique_ptr<Animation>> _animations;
  83. base::flat_map<not_null<Animation*>, not_null<SharedState*>> _active;
  84. base::flat_map<not_null<Animation*>, PausedInfo> _paused;
  85. base::flat_set<not_null<Animation*>> _pendingPause;
  86. base::flat_set<not_null<Animation*>> _pendingUnpause;
  87. base::flat_set<not_null<Animation*>> _pausedBeforeStart;
  88. base::flat_set<not_null<Animation*>> _pendingRemove;
  89. base::flat_map<not_null<Animation*>, StartingInfo> _pendingToStart;
  90. crl::time _started = kTimeUnknown;
  91. crl::time _lastSyncTime = kTimeUnknown;
  92. crl::time _delay = 0;
  93. crl::time _nextFrameTime = kTimeUnknown;
  94. rpl::event_stream<MultiUpdate> _updates;
  95. rpl::lifetime _lifetime;
  96. };
  97. } // namespace Lottie