AnimationTimeline.h (5482B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_AnimationTimeline_h 8 #define mozilla_dom_AnimationTimeline_h 9 10 #include "mozilla/AnimationUtils.h" 11 #include "nsCycleCollectionParticipant.h" 12 #include "nsHashKeys.h" 13 #include "nsIGlobalObject.h" 14 #include "nsISupports.h" 15 #include "nsTHashSet.h" 16 #include "nsWrapperCache.h" 17 18 namespace mozilla::dom { 19 20 class Animation; 21 class Document; 22 class ScrollTimeline; 23 24 class AnimationTimeline : public nsISupports, public nsWrapperCache { 25 public: 26 AnimationTimeline(nsIGlobalObject* aWindow, RTPCallerType); 27 28 struct TickState { 29 TickState() = default; 30 // Nothing here, but this might be useful in the future. 31 }; 32 33 protected: 34 virtual ~AnimationTimeline(); 35 36 // Tick animations and may remove them from the list if we don't need to tick 37 // it. Return true if any animations need to be ticked. 38 bool Tick(TickState&); 39 40 public: 41 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 42 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AnimationTimeline) 43 44 nsIGlobalObject* GetParentObject() const { return mWindow; } 45 46 // AnimationTimeline methods 47 virtual Nullable<TimeDuration> GetCurrentTimeAsDuration() const = 0; 48 49 // Wrapper functions for AnimationTimeline DOM methods when called from 50 // script. 51 Nullable<double> GetCurrentTimeAsDouble() const { 52 return AnimationUtils::TimeDurationToDouble(GetCurrentTimeAsDuration(), 53 mRTPCallerType); 54 } 55 56 TimeStamp GetCurrentTimeAsTimeStamp() const { 57 Nullable<TimeDuration> currentTime = GetCurrentTimeAsDuration(); 58 return !currentTime.IsNull() ? ToTimeStamp(currentTime.Value()) 59 : TimeStamp(); 60 } 61 62 /** 63 * Returns true if the times returned by GetCurrentTimeAsDuration() are 64 * convertible to and from wallclock-based TimeStamp (e.g. from 65 * TimeStamp::Now()) values using ToTimelineTime() and ToTimeStamp(). 66 * 67 * Typically this is true, but it will be false in the case when this 68 * timeline has no refresh driver or is tied to a refresh driver under test 69 * control. 70 */ 71 virtual bool TracksWallclockTime() const = 0; 72 73 /** 74 * Converts a TimeStamp to the equivalent value in timeline time. 75 * Note that when TracksWallclockTime() is false, there is no correspondence 76 * between timeline time and wallclock time. In such a case, passing a 77 * timestamp from TimeStamp::Now() to this method will not return a 78 * meaningful result. 79 */ 80 virtual Nullable<TimeDuration> ToTimelineTime( 81 const TimeStamp& aTimeStamp) const = 0; 82 83 virtual TimeStamp ToTimeStamp(const TimeDuration& aTimelineTime) const = 0; 84 85 /** 86 * Inform this timeline that |aAnimation| which is or was observing the 87 * timeline, has been updated. This serves as both the means to associate 88 * AND disassociate animations with a timeline. The timeline itself will 89 * determine if it needs to begin, continue or stop tracking this animation. 90 */ 91 virtual void NotifyAnimationUpdated(Animation& aAnimation); 92 93 /** 94 * Returns true if any CSS animations, CSS transitions or Web animations are 95 * currently associated with this timeline. As soon as an animation is 96 * applied to an element it is associated with the timeline even if it has a 97 * delayed start, so this includes animations that may not be active for some 98 * time. 99 */ 100 bool HasAnimations() const { return !mAnimations.IsEmpty(); } 101 102 void RemoveAnimation(Animation* aAnimation); 103 virtual void NotifyAnimationContentVisibilityChanged(Animation* aAnimation, 104 bool aIsVisible); 105 void UpdateHiddenByContentVisibility(); 106 107 virtual Document* GetDocument() const = 0; 108 109 virtual bool IsMonotonicallyIncreasing() const = 0; 110 111 RTPCallerType GetRTPCallerType() const { return mRTPCallerType; } 112 113 virtual bool IsScrollTimeline() const { return false; } 114 virtual const ScrollTimeline* AsScrollTimeline() const { return nullptr; } 115 virtual bool IsViewTimeline() const { return false; } 116 117 // For a monotonic timeline, there is no upper bound on current time, and 118 // timeline duration is unresolved. For a non-monotonic (e.g. scroll) 119 // timeline, the duration has a fixed upper bound. 120 // 121 // https://drafts.csswg.org/web-animations-2/#timeline-duration 122 virtual Nullable<TimeDuration> TimelineDuration() const { return nullptr; } 123 124 protected: 125 nsCOMPtr<nsIGlobalObject> mWindow; 126 127 // Animations observing this timeline 128 // 129 // We store them in (a) a hashset for quick lookup, and (b) a LinkedList to 130 // maintain a fixed sampling order. Animations that are hidden by 131 // `content-visibility` are not sampled and will only be in the hashset. 132 // The LinkedList should always be a subset of the hashset. 133 // 134 // The hashset keeps a strong reference to each animation since 135 // dealing with addref/release with LinkedList is difficult. 136 using AnimationSet = nsTHashSet<nsRefPtrHashKey<dom::Animation>>; 137 AnimationSet mAnimations; 138 LinkedList<dom::Animation> mAnimationOrder; 139 140 // Whether the Timeline is System, ResistFingerprinting, or neither 141 enum RTPCallerType mRTPCallerType; 142 }; 143 144 } // namespace mozilla::dom 145 146 #endif // mozilla_dom_AnimationTimeline_h