ambient_animation_progress_tracker.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_AMBIENT_UI_AMBIENT_ANIMATION_PROGRESS_TRACKER_H_
  5. #define ASH_AMBIENT_UI_AMBIENT_ANIMATION_PROGRESS_TRACKER_H_
  6. #include "ash/ash_export.h"
  7. #include "base/containers/flat_set.h"
  8. #include "base/scoped_multi_source_observation.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. #include "ui/lottie/animation.h"
  11. #include "ui/lottie/animation_observer.h"
  12. namespace ash {
  13. // Observes the progress of the N active lottie animations rendering during
  14. // ambient mode and condenses all of their progress into one global number
  15. // representative of the current progress of all N animations. The method for
  16. // condensing the N timestamps into one is an internal implementation detail.
  17. //
  18. // AmbientAnimationProgressTracker is meant to be a long-lived class whose
  19. // lifetime can span multiple ambient mode sessions if desired.
  20. class ASH_EXPORT AmbientAnimationProgressTracker
  21. : public lottie::AnimationObserver {
  22. public:
  23. struct Progress {
  24. // The number of cycles the animation has completed thus far.
  25. int num_completed_cycles = 0;
  26. // The normalized timestamp of the most recent frame that the animation
  27. // has painted within the animation's current cycle.
  28. float current_timestamp = 0.f;
  29. };
  30. // Parameters for the currently active ambient session that do not change for
  31. // the duration of the session. All animations in the session are guaranteed
  32. // to share the same values for these parameters. A fatal error is raised if a
  33. // discrepancy is detected.
  34. struct ImmutableParams {
  35. ImmutableParams();
  36. ImmutableParams(const ImmutableParams& other);
  37. ImmutableParams& operator=(const ImmutableParams& other);
  38. ~ImmutableParams();
  39. // The total duration of the animation file.
  40. base::TimeDelta total_duration;
  41. // A subset of lottie::Animation::PlaybackConfig that should be the same for
  42. // all N animations.
  43. std::vector<lottie::Animation::CycleBoundaries> scheduled_cycles;
  44. lottie::Animation::Style style = lottie::Animation::Style::kLoop;
  45. };
  46. AmbientAnimationProgressTracker();
  47. AmbientAnimationProgressTracker(const AmbientAnimationProgressTracker&) =
  48. delete;
  49. AmbientAnimationProgressTracker& operator=(
  50. const AmbientAnimationProgressTracker&) = delete;
  51. ~AmbientAnimationProgressTracker() override;
  52. // Registers an |animation| to track. The |animation| may be destroyed either
  53. // before or after AmbientAnimationProgressTracker. The tracker automatically
  54. // stops tracking the |animation| when the animation is destroyed.
  55. void RegisterAnimation(lottie::Animation* animation);
  56. // Whether the tracker has detected at least one animation in the current
  57. // ambient session.
  58. bool HasActiveAnimations() const;
  59. // HasActiveAnimations() must be true before calling the methods below:
  60. //
  61. // The condensed progress of all active animations.
  62. Progress GetGlobalProgress() const;
  63. ImmutableParams GetImmutableParams() const;
  64. private:
  65. // lottie::AnimationObserver implementation:
  66. void AnimationWillStartPlaying(const lottie::Animation* animation) override;
  67. void AnimationIsDeleting(const lottie::Animation* animation) override;
  68. void VerifyAnimationImmutableParams(const lottie::Animation& animation) const;
  69. base::ScopedMultiSourceObservation<lottie::Animation,
  70. lottie::AnimationObserver>
  71. animation_observations_{this};
  72. // Registered animations that have been Start()ed.
  73. base::flat_set<const lottie::Animation*> started_animations_;
  74. // Registered animations that have not been Start()ed yet.
  75. base::flat_set<const lottie::Animation*> uninitialized_animations_;
  76. };
  77. } // namespace ash
  78. #endif // ASH_AMBIENT_UI_AMBIENT_ANIMATION_PROGRESS_TRACKER_H_