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 #include "AnimationTimeline.h"
8 #include "mozilla/AnimationComparator.h"
9 #include "mozilla/dom/Animation.h"
11 namespace mozilla::dom
{
13 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AnimationTimeline
)
15 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AnimationTimeline
)
16 tmp
->mAnimationOrder
.clear();
17 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow
, mAnimations
)
18 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
19 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
21 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AnimationTimeline
)
22 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow
, mAnimations
)
23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
25 NS_IMPL_CYCLE_COLLECTING_ADDREF(AnimationTimeline
)
26 NS_IMPL_CYCLE_COLLECTING_RELEASE(AnimationTimeline
)
28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AnimationTimeline
)
29 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
30 NS_INTERFACE_MAP_ENTRY(nsISupports
)
33 AnimationTimeline::AnimationTimeline(nsIGlobalObject
* aWindow
,
34 RTPCallerType aRTPCallerType
)
35 : mWindow(aWindow
), mRTPCallerType(aRTPCallerType
) {
39 AnimationTimeline::~AnimationTimeline() { mAnimationOrder
.clear(); }
41 bool AnimationTimeline::Tick() {
42 bool needsTicks
= false;
44 nsTArray
<Animation
*> animationsToRemove
;
46 for (Animation
* animation
= mAnimationOrder
.getFirst(); animation
;
48 static_cast<LinkedListElement
<Animation
>*>(animation
)->getNext()) {
49 MOZ_ASSERT(mAnimations
.Contains(animation
),
50 "The sampling order list should be a subset of the hashset");
51 MOZ_ASSERT(!animation
->IsHiddenByContentVisibility(),
52 "The sampling order list should not contain any animations "
53 "that are hidden by content-visibility");
55 // Skip any animations that are longer need associated with this timeline.
56 if (animation
->GetTimeline() != this) {
57 // If animation has some other timeline, it better not be also in the
58 // animation list of this timeline object!
59 MOZ_ASSERT(!animation
->GetTimeline());
60 animationsToRemove
.AppendElement(animation
);
64 needsTicks
|= animation
->NeedsTicks();
65 // Even if |animation| doesn't need future ticks, we should still
66 // Tick it this time around since it might just need a one-off tick in
67 // order to dispatch events.
70 if (!animation
->NeedsTicks()) {
71 animationsToRemove
.AppendElement(animation
);
75 for (Animation
* animation
: animationsToRemove
) {
76 RemoveAnimation(animation
);
82 void AnimationTimeline::NotifyAnimationUpdated(Animation
& aAnimation
) {
83 if (mAnimations
.EnsureInserted(&aAnimation
)) {
84 if (aAnimation
.GetTimeline() && aAnimation
.GetTimeline() != this) {
85 aAnimation
.GetTimeline()->RemoveAnimation(&aAnimation
);
87 if (!aAnimation
.IsHiddenByContentVisibility()) {
88 mAnimationOrder
.insertBack(&aAnimation
);
93 void AnimationTimeline::RemoveAnimation(Animation
* aAnimation
) {
94 MOZ_ASSERT(!aAnimation
->GetTimeline() || aAnimation
->GetTimeline() == this);
95 if (static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->isInList()) {
96 MOZ_ASSERT(mAnimations
.Contains(aAnimation
),
97 "The sampling order list should be a subset of the hashset");
98 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->remove();
100 mAnimations
.Remove(aAnimation
);
103 void AnimationTimeline::NotifyAnimationContentVisibilityChanged(
104 Animation
* aAnimation
, bool aIsVisible
) {
106 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->isInList();
107 MOZ_ASSERT(!inList
|| mAnimations
.Contains(aAnimation
),
108 "The sampling order list should be a subset of the hashset");
109 if (aIsVisible
&& !inList
&& mAnimations
.Contains(aAnimation
)) {
110 mAnimationOrder
.insertBack(aAnimation
);
111 } else if (!aIsVisible
&& inList
) {
112 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->remove();
116 } // namespace mozilla::dom