Backed out 8 changesets (bug 1873776) for causing vendor failures. CLOSED TREE
[gecko.git] / dom / animation / AnimationTimeline.h
blob6226bbb17908e4a0a3c5fd7ec211efddba3f6429
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/. */
7 #ifndef mozilla_dom_AnimationTimeline_h
8 #define mozilla_dom_AnimationTimeline_h
10 #include "nsISupports.h"
11 #include "nsWrapperCache.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "mozilla/AnimationUtils.h"
14 #include "nsHashKeys.h"
15 #include "nsIGlobalObject.h"
16 #include "nsTHashSet.h"
18 namespace mozilla::dom {
20 class Animation;
21 class Document;
22 class ScrollTimeline;
24 class AnimationTimeline : public nsISupports, public nsWrapperCache {
25 public:
26 explicit AnimationTimeline(nsIGlobalObject* aWindow,
27 RTPCallerType aRTPCallerType);
29 // We want to synchronize non-geometric animations that are started at the
30 // same time as geometric ones (e.g., transform animations that are started at
31 // the same time as a width animation).
33 // We only synchronize animations with animations, and transitions with
34 // transitions.
36 // TODO: Remove all this once bug 1540906 rides the trains.
37 struct TickState {
38 AutoTArray<Animation*, 8> mStartedAnimations;
39 AutoTArray<Animation*, 8> mStartedTransitions;
40 bool mStartedAnyGeometricTransition = false;
41 bool mStartedAnyGeometricAnimation = false;
44 protected:
45 virtual ~AnimationTimeline();
47 // Tick animations and may remove them from the list if we don't need to tick
48 // it. Return true if any animations need to be ticked.
49 bool Tick(TickState&);
51 public:
52 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
53 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AnimationTimeline)
55 nsIGlobalObject* GetParentObject() const { return mWindow; }
57 // AnimationTimeline methods
58 virtual Nullable<TimeDuration> GetCurrentTimeAsDuration() const = 0;
60 // Wrapper functions for AnimationTimeline DOM methods when called from
61 // script.
62 Nullable<double> GetCurrentTimeAsDouble() const {
63 return AnimationUtils::TimeDurationToDouble(GetCurrentTimeAsDuration(),
64 mRTPCallerType);
67 TimeStamp GetCurrentTimeAsTimeStamp() const {
68 Nullable<TimeDuration> currentTime = GetCurrentTimeAsDuration();
69 return !currentTime.IsNull() ? ToTimeStamp(currentTime.Value())
70 : TimeStamp();
73 /**
74 * Returns true if the times returned by GetCurrentTimeAsDuration() are
75 * convertible to and from wallclock-based TimeStamp (e.g. from
76 * TimeStamp::Now()) values using ToTimelineTime() and ToTimeStamp().
78 * Typically this is true, but it will be false in the case when this
79 * timeline has no refresh driver or is tied to a refresh driver under test
80 * control.
82 virtual bool TracksWallclockTime() const = 0;
84 /**
85 * Converts a TimeStamp to the equivalent value in timeline time.
86 * Note that when TracksWallclockTime() is false, there is no correspondence
87 * between timeline time and wallclock time. In such a case, passing a
88 * timestamp from TimeStamp::Now() to this method will not return a
89 * meaningful result.
91 virtual Nullable<TimeDuration> ToTimelineTime(
92 const TimeStamp& aTimeStamp) const = 0;
94 virtual TimeStamp ToTimeStamp(const TimeDuration& aTimelineTime) const = 0;
96 /**
97 * Inform this timeline that |aAnimation| which is or was observing the
98 * timeline, has been updated. This serves as both the means to associate
99 * AND disassociate animations with a timeline. The timeline itself will
100 * determine if it needs to begin, continue or stop tracking this animation.
102 virtual void NotifyAnimationUpdated(Animation& aAnimation);
105 * Returns true if any CSS animations, CSS transitions or Web animations are
106 * currently associated with this timeline. As soon as an animation is
107 * applied to an element it is associated with the timeline even if it has a
108 * delayed start, so this includes animations that may not be active for some
109 * time.
111 bool HasAnimations() const { return !mAnimations.IsEmpty(); }
113 virtual void RemoveAnimation(Animation* aAnimation);
114 virtual void NotifyAnimationContentVisibilityChanged(Animation* aAnimation,
115 bool aIsVisible);
116 void UpdateHiddenByContentVisibility();
118 virtual Document* GetDocument() const = 0;
120 virtual bool IsMonotonicallyIncreasing() const = 0;
122 RTPCallerType GetRTPCallerType() const { return mRTPCallerType; }
124 virtual bool IsScrollTimeline() const { return false; }
125 virtual const ScrollTimeline* AsScrollTimeline() const { return nullptr; }
126 virtual bool IsViewTimeline() const { return false; }
128 // For a monotonic timeline, there is no upper bound on current time, and
129 // timeline duration is unresolved. For a non-monotonic (e.g. scroll)
130 // timeline, the duration has a fixed upper bound.
132 // https://drafts.csswg.org/web-animations-2/#timeline-duration
133 virtual Nullable<TimeDuration> TimelineDuration() const { return nullptr; }
135 protected:
136 nsCOMPtr<nsIGlobalObject> mWindow;
138 // Animations observing this timeline
140 // We store them in (a) a hashset for quick lookup, and (b) a LinkedList to
141 // maintain a fixed sampling order. Animations that are hidden by
142 // `content-visibility` are not sampled and will only be in the hashset.
143 // The LinkedList should always be a subset of the hashset.
145 // The hashset keeps a strong reference to each animation since
146 // dealing with addref/release with LinkedList is difficult.
147 using AnimationSet = nsTHashSet<nsRefPtrHashKey<dom::Animation>>;
148 AnimationSet mAnimations;
149 LinkedList<dom::Animation> mAnimationOrder;
151 // Whether the Timeline is System, ResistFingerprinting, or neither
152 enum RTPCallerType mRTPCallerType;
155 } // namespace mozilla::dom
157 #endif // mozilla_dom_AnimationTimeline_h