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 AnimationTimeline::~AnimationTimeline() { mAnimationOrder
.clear(); }
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AnimationTimeline
)
17 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AnimationTimeline
)
18 tmp
->mAnimationOrder
.clear();
19 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow
, mAnimations
)
20 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
21 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AnimationTimeline
)
24 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow
, mAnimations
)
25 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
27 NS_IMPL_CYCLE_COLLECTING_ADDREF(AnimationTimeline
)
28 NS_IMPL_CYCLE_COLLECTING_RELEASE(AnimationTimeline
)
30 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AnimationTimeline
)
31 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
32 NS_INTERFACE_MAP_ENTRY(nsISupports
)
35 bool AnimationTimeline::Tick() {
36 bool needsTicks
= false;
38 nsTArray
<Animation
*> animationsToRemove
;
40 for (Animation
* animation
= mAnimationOrder
.getFirst(); animation
;
42 static_cast<LinkedListElement
<Animation
>*>(animation
)->getNext()) {
43 MOZ_ASSERT(!animation
->IsHiddenByContentVisibility());
45 // Skip any animations that are longer need associated with this timeline.
46 if (animation
->GetTimeline() != this) {
47 // If animation has some other timeline, it better not be also in the
48 // animation list of this timeline object!
49 MOZ_ASSERT(!animation
->GetTimeline());
50 animationsToRemove
.AppendElement(animation
);
54 needsTicks
|= animation
->NeedsTicks();
55 // Even if |animation| doesn't need future ticks, we should still
56 // Tick it this time around since it might just need a one-off tick in
57 // order to dispatch events.
60 if (!animation
->NeedsTicks()) {
61 animationsToRemove
.AppendElement(animation
);
65 for (Animation
* animation
: animationsToRemove
) {
66 RemoveAnimation(animation
);
72 void AnimationTimeline::NotifyAnimationUpdated(Animation
& aAnimation
) {
73 if (mAnimations
.EnsureInserted(&aAnimation
)) {
74 if (aAnimation
.GetTimeline() && aAnimation
.GetTimeline() != this) {
75 aAnimation
.GetTimeline()->RemoveAnimation(&aAnimation
);
77 if (!aAnimation
.IsHiddenByContentVisibility()) {
78 mAnimationOrder
.insertBack(&aAnimation
);
83 void AnimationTimeline::RemoveAnimation(Animation
* aAnimation
) {
84 MOZ_ASSERT(!aAnimation
->GetTimeline() || aAnimation
->GetTimeline() == this);
85 if (static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->isInList()) {
86 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->remove();
88 mAnimations
.Remove(aAnimation
);
91 void AnimationTimeline::NotifyAnimationContentVisibilityChanged(
92 Animation
* aAnimation
, bool visible
) {
94 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->isInList();
95 if (visible
&& !inList
) {
96 mAnimationOrder
.insertBack(aAnimation
);
97 } else if (!visible
&& inList
) {
98 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->remove();
102 } // namespace mozilla::dom