PendingAnimationTracker.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_PendingAnimationTracker_h
  6. #define mozilla_dom_PendingAnimationTracker_h
  7. #include "mozilla/dom/Animation.h"
  8. #include "nsCycleCollectionParticipant.h"
  9. #include "nsIDocument.h"
  10. #include "nsTHashtable.h"
  11. class nsIFrame;
  12. namespace mozilla {
  13. class PendingAnimationTracker final
  14. {
  15. public:
  16. explicit PendingAnimationTracker(nsIDocument* aDocument)
  17. : mDocument(aDocument)
  18. { }
  19. NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(PendingAnimationTracker)
  20. NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(PendingAnimationTracker)
  21. void AddPlayPending(dom::Animation& aAnimation)
  22. {
  23. MOZ_ASSERT(!IsWaitingToPause(aAnimation),
  24. "Animation is already waiting to pause");
  25. AddPending(aAnimation, mPlayPendingSet);
  26. }
  27. void RemovePlayPending(dom::Animation& aAnimation)
  28. {
  29. RemovePending(aAnimation, mPlayPendingSet);
  30. }
  31. bool IsWaitingToPlay(const dom::Animation& aAnimation) const
  32. {
  33. return IsWaiting(aAnimation, mPlayPendingSet);
  34. }
  35. void AddPausePending(dom::Animation& aAnimation)
  36. {
  37. MOZ_ASSERT(!IsWaitingToPlay(aAnimation),
  38. "Animation is already waiting to play");
  39. AddPending(aAnimation, mPausePendingSet);
  40. }
  41. void RemovePausePending(dom::Animation& aAnimation)
  42. {
  43. RemovePending(aAnimation, mPausePendingSet);
  44. }
  45. bool IsWaitingToPause(const dom::Animation& aAnimation) const
  46. {
  47. return IsWaiting(aAnimation, mPausePendingSet);
  48. }
  49. void TriggerPendingAnimationsOnNextTick(const TimeStamp& aReadyTime);
  50. void TriggerPendingAnimationsNow();
  51. bool HasPendingAnimations() const {
  52. return mPlayPendingSet.Count() > 0 || mPausePendingSet.Count() > 0;
  53. }
  54. private:
  55. ~PendingAnimationTracker() { }
  56. void EnsurePaintIsScheduled();
  57. typedef nsTHashtable<nsRefPtrHashKey<dom::Animation>> AnimationSet;
  58. void AddPending(dom::Animation& aAnimation, AnimationSet& aSet);
  59. void RemovePending(dom::Animation& aAnimation, AnimationSet& aSet);
  60. bool IsWaiting(const dom::Animation& aAnimation,
  61. const AnimationSet& aSet) const;
  62. AnimationSet mPlayPendingSet;
  63. AnimationSet mPausePendingSet;
  64. nsCOMPtr<nsIDocument> mDocument;
  65. };
  66. } // namespace mozilla
  67. #endif // mozilla_dom_PendingAnimationTracker_h