OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "cc/animation/animation_player.h" |
| 6 |
| 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" |
| 12 |
| 13 namespace cc { |
| 14 |
| 15 scoped_refptr<AnimationPlayer> AnimationPlayer::Create(int id) { |
| 16 return make_scoped_refptr(new AnimationPlayer(id)); |
| 17 } |
| 18 |
| 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_); |
| 27 } |
| 28 |
| 29 AnimationPlayer::~AnimationPlayer() { |
| 30 DCHECK(!animation_timeline_); |
| 31 DCHECK(!element_animations_); |
| 32 DCHECK(!layer_id_); |
| 33 } |
| 34 |
| 35 scoped_refptr<AnimationPlayer> AnimationPlayer::CreateImplInstance() const { |
| 36 scoped_refptr<AnimationPlayer> player = AnimationPlayer::Create(id()); |
| 37 return player; |
| 38 } |
| 39 |
| 40 void AnimationPlayer::SetAnimationHost(AnimationHost* animation_host) { |
| 41 animation_host_ = animation_host; |
| 42 } |
| 43 |
| 44 void AnimationPlayer::SetAnimationTimeline(AnimationTimeline* timeline) { |
| 45 if (animation_timeline_ == timeline) |
| 46 return; |
| 47 |
| 48 // We need to unregister player to manage ElementAnimations and observers |
| 49 // properly. |
| 50 if (layer_id_ && element_animations_) |
| 51 UnregisterPlayer(); |
| 52 |
| 53 animation_timeline_ = timeline; |
| 54 |
| 55 // Register player only if layer AND host attached. |
| 56 if (layer_id_ && animation_host_) |
| 57 RegisterPlayer(); |
| 58 } |
| 59 |
| 60 void AnimationPlayer::AttachLayer(int layer_id) { |
| 61 DCHECK_EQ(layer_id_, 0); |
| 62 DCHECK(layer_id); |
| 63 |
| 64 layer_id_ = layer_id; |
| 65 |
| 66 // Register player only if layer AND host attached. |
| 67 if (animation_host_) |
| 68 RegisterPlayer(); |
| 69 } |
| 70 |
| 71 void AnimationPlayer::DetachLayer() { |
| 72 DCHECK(layer_id_); |
| 73 |
| 74 if (animation_host_) |
| 75 UnregisterPlayer(); |
| 76 |
| 77 layer_id_ = 0; |
| 78 } |
| 79 |
| 80 void AnimationPlayer::RegisterPlayer() { |
| 81 DCHECK(layer_id_); |
| 82 DCHECK(animation_host_); |
| 83 DCHECK(!element_animations_); |
| 84 |
| 85 // Create LAC or re-use existing. |
| 86 animation_host_->RegisterPlayerForLayer(layer_id_, this); |
| 87 // Get local reference to shared LAC. |
| 88 BindElementAnimations(); |
| 89 } |
| 90 |
| 91 void AnimationPlayer::UnregisterPlayer() { |
| 92 DCHECK(layer_id_); |
| 93 DCHECK(animation_host_); |
| 94 DCHECK(element_animations_); |
| 95 |
| 96 UnbindElementAnimations(); |
| 97 // Destroy LAC or release it if it's still needed. |
| 98 animation_host_->UnregisterPlayerForLayer(layer_id_, this); |
| 99 } |
| 100 |
| 101 void AnimationPlayer::BindElementAnimations() { |
| 102 DCHECK(!element_animations_); |
| 103 element_animations_ = |
| 104 animation_host_->GetElementAnimationsForLayerId(layer_id_); |
| 105 DCHECK(element_animations_); |
| 106 |
| 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(); |
| 114 } |
| 115 |
| 116 void AnimationPlayer::UnbindElementAnimations() { |
| 117 element_animations_ = nullptr; |
| 118 DCHECK(animations_.empty()); |
| 119 } |
| 120 |
| 121 void AnimationPlayer::AddAnimation(scoped_ptr<Animation> animation) { |
| 122 if (element_animations_) { |
| 123 element_animations_->layer_animation_controller()->AddAnimation( |
| 124 animation.Pass()); |
| 125 SetNeedsCommit(); |
| 126 } else { |
| 127 animations_.push_back(animation.Pass()); |
| 128 } |
| 129 } |
| 130 |
| 131 void AnimationPlayer::PauseAnimation(int animation_id, double time_offset) { |
| 132 DCHECK(element_animations_); |
| 133 element_animations_->layer_animation_controller()->PauseAnimation( |
| 134 animation_id, base::TimeDelta::FromSecondsD(time_offset)); |
| 135 SetNeedsCommit(); |
| 136 } |
| 137 |
| 138 void AnimationPlayer::RemoveAnimation(int animation_id) { |
| 139 if (element_animations_) { |
| 140 element_animations_->layer_animation_controller()->RemoveAnimation( |
| 141 animation_id); |
| 142 SetNeedsCommit(); |
| 143 } else { |
| 144 auto animations_to_remove = animations_.remove_if([animation_id]( |
| 145 Animation* animation) { return animation->id() == animation_id; }); |
| 146 animations_.erase(animations_to_remove, animations_.end()); |
| 147 } |
| 148 } |
| 149 |
| 150 void AnimationPlayer::PushPropertiesTo(AnimationPlayer* player_impl) { |
| 151 if (!element_animations_) { |
| 152 if (player_impl->element_animations()) |
| 153 player_impl->DetachLayer(); |
| 154 return; |
| 155 } |
| 156 |
| 157 DCHECK(layer_id_); |
| 158 if (!player_impl->element_animations()) |
| 159 player_impl->AttachLayer(layer_id_); |
| 160 } |
| 161 |
| 162 void AnimationPlayer::NotifyAnimationStarted( |
| 163 base::TimeTicks monotonic_time, |
| 164 Animation::TargetProperty target_property, |
| 165 int group) { |
| 166 if (layer_animation_delegate_) |
| 167 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time, |
| 168 target_property, group); |
| 169 } |
| 170 |
| 171 void AnimationPlayer::NotifyAnimationFinished( |
| 172 base::TimeTicks monotonic_time, |
| 173 Animation::TargetProperty target_property, |
| 174 int group) { |
| 175 if (layer_animation_delegate_) |
| 176 layer_animation_delegate_->NotifyAnimationFinished(monotonic_time, |
| 177 target_property, group); |
| 178 } |
| 179 |
| 180 void AnimationPlayer::SetNeedsCommit() { |
| 181 DCHECK(animation_host_); |
| 182 animation_host_->SetNeedsCommit(); |
| 183 } |
| 184 |
| 185 } // namespace cc |
OLD | NEW |