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/dom/Animation.h"
10 namespace mozilla::dom
{
12 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AnimationTimeline
)
14 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AnimationTimeline
)
15 tmp
->mAnimationOrder
.clear();
16 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow
, mAnimations
)
17 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
18 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
20 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AnimationTimeline
)
21 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow
, mAnimations
)
22 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(AnimationTimeline
)
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(AnimationTimeline
)
27 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AnimationTimeline
)
28 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
29 NS_INTERFACE_MAP_ENTRY(nsISupports
)
32 AnimationTimeline::AnimationTimeline(nsIGlobalObject
* aWindow
,
33 RTPCallerType aRTPCallerType
)
34 : mWindow(aWindow
), mRTPCallerType(aRTPCallerType
) {
38 AnimationTimeline::~AnimationTimeline() { mAnimationOrder
.clear(); }
40 bool AnimationTimeline::Tick(TickState
& aState
) {
41 bool needsTicks
= false;
44 for (Animation
* animation
: mAnimationOrder
) {
45 MOZ_ASSERT(mAnimations
.Contains(animation
),
46 "The sampling order list should be a subset of the hashset");
47 MOZ_ASSERT(!animation
->IsHiddenByContentVisibility(),
48 "The sampling order list should not contain any animations "
49 "that are hidden by content-visibility");
53 for (Animation
* animation
:
54 ToTArray
<AutoTArray
<RefPtr
<Animation
>, 32>>(mAnimationOrder
)) {
55 // Skip any animations that are longer need associated with this timeline.
56 if (animation
->GetTimeline() != this) {
57 RemoveAnimation(animation
);
61 needsTicks
|= animation
->NeedsTicks();
62 // Even if |animation| doesn't need future ticks, we should still Tick it
63 // this time around since it might just need a one-off tick in order to
65 animation
->Tick(aState
);
66 if (!animation
->NeedsTicks()) {
67 RemoveAnimation(animation
);
74 void AnimationTimeline::NotifyAnimationUpdated(Animation
& aAnimation
) {
75 if (mAnimations
.EnsureInserted(&aAnimation
)) {
76 if (aAnimation
.GetTimeline() && aAnimation
.GetTimeline() != this) {
77 aAnimation
.GetTimeline()->RemoveAnimation(&aAnimation
);
79 if (!aAnimation
.IsHiddenByContentVisibility()) {
80 mAnimationOrder
.insertBack(&aAnimation
);
85 void AnimationTimeline::RemoveAnimation(Animation
* aAnimation
) {
86 if (static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->isInList() &&
87 MOZ_LIKELY(!aAnimation
->GetTimeline() ||
88 aAnimation
->GetTimeline() == this)) {
89 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->remove();
90 MOZ_ASSERT(mAnimations
.Contains(aAnimation
),
91 "The sampling order list should be a subset of the hashset");
93 mAnimations
.Remove(aAnimation
);
96 void AnimationTimeline::NotifyAnimationContentVisibilityChanged(
97 Animation
* aAnimation
, bool aIsVisible
) {
99 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->isInList();
100 MOZ_ASSERT(!inList
|| mAnimations
.Contains(aAnimation
),
101 "The sampling order list should be a subset of the hashset");
102 if (aIsVisible
&& !inList
&& mAnimations
.Contains(aAnimation
)) {
103 mAnimationOrder
.insertBack(aAnimation
);
104 } else if (!aIsVisible
&& inList
) {
105 static_cast<LinkedListElement
<Animation
>*>(aAnimation
)->remove();
109 void AnimationTimeline::UpdateHiddenByContentVisibility() {
110 for (Animation
* animation
: mAnimations
) {
111 animation
->UpdateHiddenByContentVisibility();
115 } // namespace mozilla::dom