1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/animation/animation_player.h"
7 #include "cc/animation/animation_delegate.h"
8 #include "cc/animation/animation_host.h"
9 #include "cc/animation/animation_timeline.h"
10 #include "cc/animation/element_animations.h"
11 #include "cc/animation/layer_animation_controller.h"
15 scoped_refptr
<AnimationPlayer
> AnimationPlayer::Create(int id
) {
16 return make_scoped_refptr(new AnimationPlayer(id
));
19 AnimationPlayer::AnimationPlayer(int id
)
21 animation_timeline_(),
22 element_animations_(),
23 layer_animation_delegate_(),
29 AnimationPlayer::~AnimationPlayer() {
30 DCHECK(!animation_timeline_
);
31 DCHECK(!element_animations_
);
35 scoped_refptr
<AnimationPlayer
> AnimationPlayer::CreateImplInstance() const {
36 scoped_refptr
<AnimationPlayer
> player
= AnimationPlayer::Create(id());
40 void AnimationPlayer::SetAnimationHost(AnimationHost
* animation_host
) {
41 animation_host_
= animation_host
;
44 void AnimationPlayer::SetAnimationTimeline(AnimationTimeline
* timeline
) {
45 if (animation_timeline_
== timeline
)
48 // We need to unregister player to manage ElementAnimations and observers
50 if (layer_id_
&& element_animations_
)
53 animation_timeline_
= timeline
;
55 // Register player only if layer AND host attached.
56 if (layer_id_
&& animation_host_
)
60 void AnimationPlayer::AttachLayer(int layer_id
) {
61 DCHECK_EQ(layer_id_
, 0);
66 // Register player only if layer AND host attached.
71 void AnimationPlayer::DetachLayer() {
80 void AnimationPlayer::RegisterPlayer() {
82 DCHECK(animation_host_
);
83 DCHECK(!element_animations_
);
85 // Create LAC or re-use existing.
86 animation_host_
->RegisterPlayerForLayer(layer_id_
, this);
87 // Get local reference to shared LAC.
88 BindElementAnimations();
91 void AnimationPlayer::UnregisterPlayer() {
93 DCHECK(animation_host_
);
94 DCHECK(element_animations_
);
96 UnbindElementAnimations();
97 // Destroy LAC or release it if it's still needed.
98 animation_host_
->UnregisterPlayerForLayer(layer_id_
, this);
101 void AnimationPlayer::BindElementAnimations() {
102 DCHECK(!element_animations_
);
103 element_animations_
=
104 animation_host_
->GetElementAnimationsForLayerId(layer_id_
);
105 DCHECK(element_animations_
);
107 // Pass all accumulated animations to LAC.
108 for (auto it
= animations_
.begin(); it
!= animations_
.end(); ++it
)
109 element_animations_
->layer_animation_controller()->AddAnimation(
110 animations_
.take(it
));
111 if (!animations_
.empty())
116 void AnimationPlayer::UnbindElementAnimations() {
117 element_animations_
= nullptr;
118 DCHECK(animations_
.empty());
121 void AnimationPlayer::AddAnimation(scoped_ptr
<Animation
> animation
) {
123 animation
->target_property() == Animation::SCROLL_OFFSET
,
124 animation_host_
&& animation_host_
->SupportsScrollAnimations());
126 if (element_animations_
) {
127 element_animations_
->layer_animation_controller()->AddAnimation(
131 animations_
.push_back(animation
.Pass());
135 void AnimationPlayer::PauseAnimation(int animation_id
, double time_offset
) {
136 DCHECK(element_animations_
);
137 element_animations_
->layer_animation_controller()->PauseAnimation(
138 animation_id
, base::TimeDelta::FromSecondsD(time_offset
));
142 void AnimationPlayer::RemoveAnimation(int animation_id
) {
143 if (element_animations_
) {
144 element_animations_
->layer_animation_controller()->RemoveAnimation(
148 auto animations_to_remove
= animations_
.remove_if([animation_id
](
149 Animation
* animation
) { return animation
->id() == animation_id
; });
150 animations_
.erase(animations_to_remove
, animations_
.end());
154 void AnimationPlayer::PushPropertiesTo(AnimationPlayer
* player_impl
) {
155 if (!element_animations_
) {
156 if (player_impl
->element_animations())
157 player_impl
->DetachLayer();
162 if (!player_impl
->element_animations())
163 player_impl
->AttachLayer(layer_id_
);
166 void AnimationPlayer::NotifyAnimationStarted(
167 base::TimeTicks monotonic_time
,
168 Animation::TargetProperty target_property
,
170 if (layer_animation_delegate_
)
171 layer_animation_delegate_
->NotifyAnimationStarted(monotonic_time
,
172 target_property
, group
);
175 void AnimationPlayer::NotifyAnimationFinished(
176 base::TimeTicks monotonic_time
,
177 Animation::TargetProperty target_property
,
179 if (layer_animation_delegate_
)
180 layer_animation_delegate_
->NotifyAnimationFinished(monotonic_time
,
181 target_property
, group
);
184 void AnimationPlayer::SetNeedsCommit() {
185 DCHECK(animation_host_
);
186 animation_host_
->SetNeedsCommit();