All instances of addresses as void* are changed to uintptr_t in
[chromium-blink-merge.git] / cc / animation / animation_player.cc
blobe4febeb242ace4204442b8dbc2cf0c9f3ba41bf3
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"
13 namespace cc {
15 scoped_refptr<AnimationPlayer> AnimationPlayer::Create(int id) {
16 return make_scoped_refptr(new AnimationPlayer(id));
19 AnimationPlayer::AnimationPlayer(int id)
20 : animation_host_(),
21 animation_timeline_(),
22 element_animations_(),
23 layer_animation_delegate_(),
24 id_(id),
25 layer_id_(0) {
26 DCHECK(id_);
29 AnimationPlayer::~AnimationPlayer() {
30 DCHECK(!animation_timeline_);
31 DCHECK(!element_animations_);
32 DCHECK(!layer_id_);
35 scoped_refptr<AnimationPlayer> AnimationPlayer::CreateImplInstance() const {
36 scoped_refptr<AnimationPlayer> player = AnimationPlayer::Create(id());
37 return player;
40 void AnimationPlayer::SetAnimationHost(AnimationHost* animation_host) {
41 animation_host_ = animation_host;
44 void AnimationPlayer::SetAnimationTimeline(AnimationTimeline* timeline) {
45 if (animation_timeline_ == timeline)
46 return;
48 // We need to unregister player to manage ElementAnimations and observers
49 // properly.
50 if (layer_id_ && element_animations_)
51 UnregisterPlayer();
53 animation_timeline_ = timeline;
55 // Register player only if layer AND host attached.
56 if (layer_id_ && animation_host_)
57 RegisterPlayer();
60 void AnimationPlayer::AttachLayer(int layer_id) {
61 DCHECK_EQ(layer_id_, 0);
62 DCHECK(layer_id);
64 layer_id_ = layer_id;
66 // Register player only if layer AND host attached.
67 if (animation_host_)
68 RegisterPlayer();
71 void AnimationPlayer::DetachLayer() {
72 DCHECK(layer_id_);
74 if (animation_host_)
75 UnregisterPlayer();
77 layer_id_ = 0;
80 void AnimationPlayer::RegisterPlayer() {
81 DCHECK(layer_id_);
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() {
92 DCHECK(layer_id_);
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())
112 SetNeedsCommit();
113 animations_.clear();
116 void AnimationPlayer::UnbindElementAnimations() {
117 element_animations_ = nullptr;
118 DCHECK(animations_.empty());
121 void AnimationPlayer::AddAnimation(scoped_ptr<Animation> animation) {
122 DCHECK_IMPLIES(
123 animation->target_property() == Animation::SCROLL_OFFSET,
124 animation_host_ && animation_host_->SupportsScrollAnimations());
126 if (element_animations_) {
127 element_animations_->layer_animation_controller()->AddAnimation(
128 animation.Pass());
129 SetNeedsCommit();
130 } else {
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));
139 SetNeedsCommit();
142 void AnimationPlayer::RemoveAnimation(int animation_id) {
143 if (element_animations_) {
144 element_animations_->layer_animation_controller()->RemoveAnimation(
145 animation_id);
146 SetNeedsCommit();
147 } else {
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();
158 return;
161 DCHECK(layer_id_);
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,
169 int group) {
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,
178 int group) {
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();
189 } // namespace cc