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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsAnimationManager_h_
7 #define nsAnimationManager_h_
9 #include "mozilla/Attributes.h"
10 #include "mozilla/ContentEvents.h"
11 #include "mozilla/EventForwards.h"
12 #include "AnimationCommon.h"
13 #include "mozilla/dom/Animation.h"
14 #include "mozilla/Keyframe.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/TimeStamp.h"
17 #include "nsISupportsImpl.h"
19 class nsIGlobalObject
;
20 class ServoComputedData
;
21 struct nsStyleDisplay
;
22 class ServoCSSAnimationBuilder
;
33 enum class PseudoStyleType
: uint8_t;
34 struct NonOwningAnimationTarget
;
38 class CSSAnimation final
: public Animation
{
40 explicit CSSAnimation(nsIGlobalObject
* aGlobal
, nsAtom
* aAnimationName
)
41 : dom::Animation(aGlobal
),
42 mAnimationName(aAnimationName
),
43 mIsStylePaused(false),
44 mPauseShouldStick(false),
45 mNeedsNewAnimationIndexWhenRun(false),
46 mPreviousPhase(ComputedTiming::AnimationPhase::Idle
),
47 mPreviousIteration(0) {
48 // We might need to drop this assertion once we add a script-accessible
49 // constructor but for animations generated from CSS markup the
50 // animation-name should never be empty.
51 MOZ_ASSERT(mAnimationName
!= nsGkAtoms::_empty
,
52 "animation-name should not be 'none'");
55 JSObject
* WrapObject(JSContext
* aCx
,
56 JS::Handle
<JSObject
*> aGivenProto
) override
;
58 CSSAnimation
* AsCSSAnimation() override
{ return this; }
59 const CSSAnimation
* AsCSSAnimation() const override
{ return this; }
61 // CSSAnimation interface
62 void GetAnimationName(nsString
& aRetVal
) const {
63 mAnimationName
->ToString(aRetVal
);
66 nsAtom
* AnimationName() const { return mAnimationName
; }
68 // Animation interface overrides
69 virtual Promise
* GetReady(ErrorResult
& aRv
) override
;
70 virtual void Play(ErrorResult
& aRv
, LimitBehavior aLimitBehavior
) override
;
71 virtual void Pause(ErrorResult
& aRv
) override
;
73 // NOTE: tabbrowser.xml currently relies on the fact that reading the
74 // currentTime of a CSSAnimation does *not* flush style (whereas reading the
75 // playState does). If CSS Animations 2 specifies that reading currentTime
76 // also flushes style we will need to find another way to detect canceled
77 // animations in tabbrowser.xml. On the other hand, if CSS Animations 2
78 // specifies that reading playState does *not* flush style (and we drop the
79 // following override), then we should update tabbrowser.xml to check
80 // the playState instead.
81 AnimationPlayState
PlayStateFromJS() const override
;
82 bool PendingFromJS() const override
;
83 void PlayFromJS(ErrorResult
& aRv
) override
;
86 void PauseFromStyle();
87 void CancelFromStyle(PostRestyleMode aPostRestyle
) {
88 // When an animation is disassociated with style it enters an odd state
89 // where its composite order is undefined until it first transitions
90 // out of the idle state.
92 // Even if the composite order isn't defined we don't want it to be random
93 // in case we need to determine the order to dispatch events associated
94 // with an animation in this state. To solve this we treat the animation as
95 // if it had been added to the end of the global animation list so that
96 // its sort order is defined. We'll update this index again once the
97 // animation leaves the idle state.
98 mAnimationIndex
= sNextAnimationIndex
++;
99 mNeedsNewAnimationIndexWhenRun
= true;
101 Animation::Cancel(aPostRestyle
);
103 // We need to do this *after* calling Cancel() since
104 // Cancel() might synchronously trigger a cancel event for which
105 // we need an owning element to target the event at.
106 mOwningElement
= OwningElementRef();
109 void Tick() override
;
111 const StickyTimeDuration
& aActiveTime
= StickyTimeDuration());
113 bool IsStylePaused() const { return mIsStylePaused
; }
115 bool HasLowerCompositeOrderThan(const CSSAnimation
& aOther
) const;
117 void SetAnimationIndex(uint64_t aIndex
) {
118 MOZ_ASSERT(IsTiedToMarkup());
119 if (IsRelevant() && mAnimationIndex
!= aIndex
) {
120 nsNodeUtils::AnimationChanged(this);
123 mAnimationIndex
= aIndex
;
126 // Sets the owning element which is used for determining the composite
127 // order of CSSAnimation objects generated from CSS markup.
129 // @see mOwningElement
130 void SetOwningElement(const OwningElementRef
& aElement
) {
131 mOwningElement
= aElement
;
133 // True for animations that are generated from CSS markup and continue to
134 // reflect changes to that markup.
135 bool IsTiedToMarkup() const { return mOwningElement
.IsSet(); }
137 void MaybeQueueCancelEvent(const StickyTimeDuration
& aActiveTime
) override
{
138 QueueEvents(aActiveTime
);
142 virtual ~CSSAnimation() {
143 MOZ_ASSERT(!mOwningElement
.IsSet(),
144 "Owning element should be cleared "
145 "before a CSS animation is destroyed");
148 // Animation overrides
149 void UpdateTiming(SeekFlag aSeekFlag
,
150 SyncNotifyFlag aSyncNotifyFlag
) override
;
152 // Returns the duration from the start of the animation's source effect's
153 // active interval to the point where the animation actually begins playback.
154 // This is zero unless the animation's source effect has a negative delay in
155 // which case it is the absolute value of that delay.
156 // This is used for setting the elapsedTime member of CSS AnimationEvents.
157 TimeDuration
InitialAdvance() const {
158 return mEffect
? std::max(TimeDuration(),
159 mEffect
->SpecifiedTiming().Delay() * -1)
163 RefPtr
<nsAtom
> mAnimationName
;
165 // The (pseudo-)element whose computed animation-name refers to this
166 // animation (if any).
168 // This is used for determining the relative composite order of animations
169 // generated from CSS markup.
171 // Typically this will be the same as the target element of the keyframe
172 // effect associated with this animation. However, it can differ in the
173 // following circumstances:
175 // a) If script removes or replaces the effect of this animation,
176 // b) If this animation is cancelled (e.g. by updating the
177 // animation-name property or removing the owning element from the
179 // c) If this object is generated from script using the CSSAnimation
182 // For (b) and (c) the owning element will return !IsSet().
183 OwningElementRef mOwningElement
;
185 // When combining animation-play-state with play() / pause() the following
187 // 1. pause() is sticky and always overrides the underlying
188 // animation-play-state
189 // 2. If animation-play-state is 'paused', play() will temporarily override
190 // it until animation-play-state next becomes 'running'.
191 // 3. Calls to play() trigger finishing behavior but setting the
192 // animation-play-state to 'running' does not.
194 // This leads to five distinct states:
197 // B. Running and temporarily overriding animation-play-state: paused
198 // C. Paused and sticky overriding animation-play-state: running
199 // D. Paused and sticky overriding animation-play-state: paused
200 // E. Paused by animation-play-state
202 // C and D may seem redundant but they differ in how to respond to the
203 // sequence: call play(), set animation-play-state: paused.
205 // C will transition to A then E leaving the animation paused.
206 // D will transition to B then B leaving the animation running.
208 // A state transition chart is as follows:
211 // ---------------------------
212 // play() A | B | A | B | B
213 // pause() C | D | C | D | D
214 // 'running' A | A | C | C | A
215 // 'paused' E | B | D | D | E
217 // The base class, Animation already provides a boolean value,
218 // mIsPaused which gives us two states. To this we add a further two booleans
219 // to represent the states as follows.
222 // (!mIsPaused; !mIsStylePaused; !mPauseShouldStick)
223 // B. Running and temporarily overriding animation-play-state: paused
224 // (!mIsPaused; mIsStylePaused; !mPauseShouldStick)
225 // C. Paused and sticky overriding animation-play-state: running
226 // (mIsPaused; !mIsStylePaused; mPauseShouldStick)
227 // D. Paused and sticky overriding animation-play-state: paused
228 // (mIsPaused; mIsStylePaused; mPauseShouldStick)
229 // E. Paused by animation-play-state
230 // (mIsPaused; mIsStylePaused; !mPauseShouldStick)
232 // (That leaves 3 combinations of the boolean values that we never set because
233 // they don't represent valid states.)
235 bool mPauseShouldStick
;
237 // When true, indicates that when this animation next leaves the idle state,
238 // its animation index should be updated.
239 bool mNeedsNewAnimationIndexWhenRun
;
241 // Phase and current iteration from the previous time we queued events.
242 // This is used to determine what new events to dispatch.
243 ComputedTiming::AnimationPhase mPreviousPhase
;
244 uint64_t mPreviousIteration
;
247 } /* namespace dom */
250 struct AnimationTypeTraits
<dom::CSSAnimation
> {
251 static nsAtom
* ElementPropertyAtom() { return nsGkAtoms::animationsProperty
; }
252 static nsAtom
* BeforePropertyAtom() {
253 return nsGkAtoms::animationsOfBeforeProperty
;
255 static nsAtom
* AfterPropertyAtom() {
256 return nsGkAtoms::animationsOfAfterProperty
;
258 static nsAtom
* MarkerPropertyAtom() {
259 return nsGkAtoms::animationsOfMarkerProperty
;
263 } /* namespace mozilla */
265 class nsAnimationManager final
266 : public mozilla::CommonAnimationManager
<mozilla::dom::CSSAnimation
> {
268 explicit nsAnimationManager(nsPresContext
* aPresContext
)
269 : mozilla::CommonAnimationManager
<mozilla::dom::CSSAnimation
>(
272 typedef mozilla::AnimationCollection
<mozilla::dom::CSSAnimation
>
273 CSSAnimationCollection
;
274 typedef nsTArray
<RefPtr
<mozilla::dom::CSSAnimation
>>
275 OwningCSSAnimationPtrArray
;
277 ~nsAnimationManager() override
= default;
280 * This function does the same thing as the above UpdateAnimations()
281 * but with servo's computed values.
283 void UpdateAnimations(mozilla::dom::Element
* aElement
,
284 mozilla::PseudoStyleType aPseudoType
,
285 const mozilla::ComputedStyle
* aComputedValues
);
287 // Utility function to walk through |aIter| to find the Keyframe with
288 // matching offset and timing function but stopping as soon as the offset
289 // differs from |aOffset| (i.e. it assumes a sorted iterator).
291 // If a matching Keyframe is found,
292 // Returns true and sets |aIndex| to the index of the matching Keyframe
295 // If no matching Keyframe is found,
296 // Returns false and sets |aIndex| to the index in the iterator of the
297 // first Keyframe with an offset differing to |aOffset| or, if the end
298 // of the iterator is reached, sets |aIndex| to the index after the last
300 template <class IterType
, class TimingFunctionType
>
301 static bool FindMatchingKeyframe(
302 IterType
&& aIter
, double aOffset
,
303 const TimingFunctionType
& aTimingFunctionToMatch
, size_t& aIndex
) {
305 for (mozilla::Keyframe
& keyframe
: aIter
) {
306 if (keyframe
.mOffset
.value() != aOffset
) {
309 if (keyframe
.mTimingFunction
== aTimingFunctionToMatch
) {
317 bool AnimationMayBeReferenced(nsAtom
* aName
) const {
318 return mMaybeReferencedAnimations
.Contains(aName
);
322 // This includes all animation names referenced regardless of whether a
323 // corresponding `@keyframes` rule is available.
325 // It may contain names which are no longer referenced, but it should always
326 // contain names which are currently referenced, so that it is usable for
327 // style invalidation.
328 nsTHashtable
<nsRefPtrHashKey
<nsAtom
>> mMaybeReferencedAnimations
;
330 void DoUpdateAnimations(const mozilla::NonOwningAnimationTarget
& aTarget
,
331 const nsStyleDisplay
& aStyleDisplay
,
332 ServoCSSAnimationBuilder
& aBuilder
);
335 #endif /* !defined(nsAnimationManager_h_) */