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_host.h" |
| 8 #include "cc/animation/animation_registrar.h" |
| 9 #include "cc/animation/animation_timeline.h" |
| 10 #include "cc/layers/layer.h" |
| 11 #include "cc/trees/layer_tree_mutators_client.h" |
| 12 |
| 13 namespace cc { |
| 14 |
| 15 class AnimationPlayer::ValueObserver : public LayerAnimationValueObserver { |
| 16 public: |
| 17 ValueObserver(AnimationPlayer* player, LayerTreeType tree_type) |
| 18 : player_(player), tree_type_(tree_type) { |
| 19 DCHECK(player_); |
| 20 } |
| 21 |
| 22 // LayerAnimationValueObserver implementation. |
| 23 void OnFilterAnimated(const FilterOperations& filters) override { |
| 24 player_->SetFilterMutated(tree_type_, filters); |
| 25 } |
| 26 |
| 27 void OnOpacityAnimated(float opacity) override { |
| 28 player_->SetOpacityMutated(tree_type_, opacity); |
| 29 } |
| 30 |
| 31 void OnTransformAnimated(const gfx::Transform& transform) override { |
| 32 player_->SetTransformMutated(tree_type_, transform); |
| 33 } |
| 34 |
| 35 void OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) override { |
| 36 player_->SetScrollOffsetMutated(tree_type_, scroll_offset); |
| 37 } |
| 38 |
| 39 void OnAnimationWaitingForDeletion() override { |
| 40 // TODO(loyso): implement it. |
| 41 } |
| 42 |
| 43 bool IsActive() const override { return tree_type_ == LayerTreeType::ACTIVE; } |
| 44 |
| 45 private: |
| 46 AnimationPlayer* player_; |
| 47 LayerTreeType tree_type_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(ValueObserver); |
| 50 }; |
| 51 |
| 52 scoped_refptr<AnimationPlayer> AnimationPlayer::Create(int id) { |
| 53 return make_scoped_refptr(new AnimationPlayer(id)); |
| 54 } |
| 55 |
| 56 AnimationPlayer::AnimationPlayer(int id) |
| 57 : animation_host_(), |
| 58 animation_timeline_(), |
| 59 layer_animation_delegate_(), |
| 60 id_(id), |
| 61 layer_id_(0) { |
| 62 DCHECK(id_); |
| 63 } |
| 64 |
| 65 AnimationPlayer::~AnimationPlayer() { |
| 66 DCHECK(!animation_timeline_); |
| 67 DCHECK(!layer_animation_controller_); |
| 68 DCHECK(!layer_id_); |
| 69 } |
| 70 |
| 71 scoped_refptr<AnimationPlayer> AnimationPlayer::CreateImplInstance() const { |
| 72 scoped_refptr<AnimationPlayer> to_return = AnimationPlayer::Create(id()); |
| 73 return to_return; |
| 74 } |
| 75 |
| 76 void AnimationPlayer::SetAnimationHost(AnimationHost* animation_host) { |
| 77 animation_host_ = animation_host; |
| 78 } |
| 79 |
| 80 void AnimationPlayer::SetAnimationTimeline(AnimationTimeline* timeline) { |
| 81 if (animation_timeline_ == timeline) |
| 82 return; |
| 83 |
| 84 // We need to remove the controller to manage observers properly. |
| 85 if (layer_id_ && animation_timeline_) |
| 86 RemoveControllerFromTimeline(); |
| 87 |
| 88 animation_timeline_ = timeline; |
| 89 |
| 90 // Create controller only if layer and timeline attached. |
| 91 if (layer_id_ && animation_timeline_) |
| 92 AddControllerToTimeline(); |
| 93 } |
| 94 |
| 95 void AnimationPlayer::set_layer_animation_delegate( |
| 96 AnimationDelegate* delegate) { |
| 97 layer_animation_delegate_ = delegate; |
| 98 if (layer_animation_controller_) |
| 99 layer_animation_controller_->set_layer_animation_delegate(delegate); |
| 100 } |
| 101 |
| 102 void AnimationPlayer::AttachLayer(int layer_id) { |
| 103 DCHECK_EQ(layer_id_, 0); |
| 104 DCHECK_NE(layer_id, 0); |
| 105 DCHECK(!layer_animation_controller_); |
| 106 |
| 107 layer_id_ = layer_id; |
| 108 |
| 109 if (animation_timeline_) |
| 110 AddControllerToTimeline(); |
| 111 } |
| 112 |
| 113 void AnimationPlayer::DetachLayer() { |
| 114 DCHECK_NE(layer_id_, 0); |
| 115 |
| 116 if (animation_timeline_) |
| 117 RemoveControllerFromTimeline(); |
| 118 |
| 119 layer_animation_controller_ = nullptr; |
| 120 layer_id_ = 0; |
| 121 } |
| 122 |
| 123 void AnimationPlayer::AddControllerToTimeline() { |
| 124 DCHECK(layer_id_); |
| 125 DCHECK(animation_host_); |
| 126 |
| 127 AnimationRegistrar* registrar = animation_host_->animation_registrar(); |
| 128 DCHECK(registrar); |
| 129 |
| 130 layer_animation_controller_ = |
| 131 registrar->GetAnimationControllerForId(layer_id_); |
| 132 layer_animation_controller_->SetAnimationRegistrar(registrar); |
| 133 |
| 134 DCHECK(animation_host_->layer_tree_mutators_client()); |
| 135 if (animation_host_->layer_tree_mutators_client()->IsLayerInTree( |
| 136 layer_id_, LayerTreeType::ACTIVE)) |
| 137 CreateActiveValueObserver(); |
| 138 if (animation_host_->layer_tree_mutators_client()->IsLayerInTree( |
| 139 layer_id_, LayerTreeType::PENDING)) |
| 140 CreatePendingValueObserver(); |
| 141 |
| 142 animation_host_->RegisterPlayerForLayer(layer_id_, this); |
| 143 } |
| 144 |
| 145 void AnimationPlayer::RemoveControllerFromTimeline() { |
| 146 DCHECK(layer_id_); |
| 147 DCHECK(animation_host_); |
| 148 animation_host_->UnregisterPlayerForLayer(layer_id_, this); |
| 149 |
| 150 DestroyPendingValueObserver(); |
| 151 DestroyActiveValueObserver(); |
| 152 if (layer_animation_controller_) |
| 153 layer_animation_controller_->SetAnimationRegistrar(nullptr); |
| 154 layer_animation_controller_ = nullptr; |
| 155 } |
| 156 |
| 157 void AnimationPlayer::LayerRegistered(int layer_id, LayerTreeType tree_type) { |
| 158 DCHECK_EQ(layer_id_, layer_id); |
| 159 if (!layer_animation_controller_) |
| 160 return; |
| 161 |
| 162 if (tree_type == LayerTreeType::ACTIVE) { |
| 163 if (!active_value_observer_) |
| 164 CreateActiveValueObserver(); |
| 165 } else { |
| 166 if (!pending_value_observer_) |
| 167 CreatePendingValueObserver(); |
| 168 } |
| 169 } |
| 170 |
| 171 void AnimationPlayer::LayerUnregistered(int layer_id, LayerTreeType tree_type) { |
| 172 DCHECK_EQ(layer_id_, layer_id); |
| 173 tree_type == LayerTreeType::ACTIVE ? DestroyActiveValueObserver() |
| 174 : DestroyPendingValueObserver(); |
| 175 } |
| 176 |
| 177 void AnimationPlayer::AddAnimation(scoped_ptr<Animation> animation) { |
| 178 DCHECK(layer_animation_controller_); |
| 179 layer_animation_controller_->AddAnimation(animation.Pass()); |
| 180 SetNeedsCommit(); |
| 181 } |
| 182 |
| 183 void AnimationPlayer::PauseAnimation(int animation_id, double time_offset) { |
| 184 DCHECK(layer_animation_controller_); |
| 185 layer_animation_controller_->PauseAnimation( |
| 186 animation_id, base::TimeDelta::FromSecondsD(time_offset)); |
| 187 SetNeedsCommit(); |
| 188 } |
| 189 |
| 190 void AnimationPlayer::RemoveAnimation(int animation_id) { |
| 191 if (layer_animation_controller_) { |
| 192 layer_animation_controller_->RemoveAnimation(animation_id); |
| 193 SetNeedsCommit(); |
| 194 } |
| 195 } |
| 196 |
| 197 void AnimationPlayer::PushPropertiesTo(AnimationPlayer* player_impl) { |
| 198 if (!layer_animation_controller()) { |
| 199 if (player_impl->layer_animation_controller()) |
| 200 player_impl->DetachLayer(); |
| 201 return; |
| 202 } |
| 203 |
| 204 DCHECK_NE(layer_id_, 0); |
| 205 if (!player_impl->layer_animation_controller()) |
| 206 player_impl->AttachLayer(layer_id_); |
| 207 |
| 208 DCHECK(player_impl->layer_animation_controller()); |
| 209 layer_animation_controller_->PushAnimationUpdatesTo( |
| 210 player_impl->layer_animation_controller()); |
| 211 } |
| 212 |
| 213 void AnimationPlayer::SetNeedsCommit() { |
| 214 DCHECK(animation_host_); |
| 215 animation_host_->SetNeedsCommit(); |
| 216 } |
| 217 |
| 218 void AnimationPlayer::SetFilterMutated(LayerTreeType tree_type, |
| 219 const FilterOperations& filters) { |
| 220 DCHECK(layer_id_); |
| 221 if (animation_host()) { |
| 222 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 223 animation_host()->layer_tree_mutators_client()->SetLayerFilterMutated( |
| 224 layer_id_, tree_type, filters); |
| 225 } |
| 226 } |
| 227 |
| 228 void AnimationPlayer::SetOpacityMutated(LayerTreeType tree_type, |
| 229 float opacity) { |
| 230 DCHECK(layer_id_); |
| 231 if (animation_host()) { |
| 232 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 233 animation_host()->layer_tree_mutators_client()->SetLayerOpacityMutated( |
| 234 layer_id_, tree_type, opacity); |
| 235 } |
| 236 } |
| 237 |
| 238 void AnimationPlayer::SetTransformMutated(LayerTreeType tree_type, |
| 239 const gfx::Transform& transform) { |
| 240 DCHECK(layer_id_); |
| 241 if (animation_host()) { |
| 242 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 243 animation_host()->layer_tree_mutators_client()->SetLayerTransformMutated( |
| 244 layer_id_, tree_type, transform); |
| 245 } |
| 246 } |
| 247 |
| 248 void AnimationPlayer::SetScrollOffsetMutated( |
| 249 LayerTreeType tree_type, |
| 250 const gfx::ScrollOffset& scroll_offset) { |
| 251 DCHECK(layer_id_); |
| 252 if (animation_host()) { |
| 253 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 254 animation_host()->layer_tree_mutators_client()->SetLayerScrollOffsetMutated( |
| 255 layer_id_, tree_type, scroll_offset); |
| 256 } |
| 257 } |
| 258 |
| 259 void AnimationPlayer::CreateActiveValueObserver() { |
| 260 DCHECK(layer_animation_controller_); |
| 261 DCHECK(!active_value_observer_); |
| 262 active_value_observer_ = |
| 263 make_scoped_ptr(new ValueObserver(this, LayerTreeType::ACTIVE)); |
| 264 layer_animation_controller_->AddValueObserver(active_value_observer_.get()); |
| 265 |
| 266 layer_animation_controller_->set_value_provider(this); |
| 267 layer_animation_controller_->set_layer_animation_delegate( |
| 268 layer_animation_delegate_); |
| 269 } |
| 270 |
| 271 void AnimationPlayer::DestroyActiveValueObserver() { |
| 272 if (layer_animation_controller_ && active_value_observer_) |
| 273 layer_animation_controller_->RemoveValueObserver( |
| 274 active_value_observer_.get()); |
| 275 active_value_observer_ = nullptr; |
| 276 |
| 277 if (layer_animation_controller_) { |
| 278 layer_animation_controller_->remove_value_provider(this); |
| 279 layer_animation_controller_->remove_layer_animation_delegate( |
| 280 layer_animation_delegate_); |
| 281 } |
| 282 } |
| 283 |
| 284 void AnimationPlayer::CreatePendingValueObserver() { |
| 285 DCHECK(layer_animation_controller_); |
| 286 DCHECK(!pending_value_observer_); |
| 287 pending_value_observer_ = |
| 288 make_scoped_ptr(new ValueObserver(this, LayerTreeType::PENDING)); |
| 289 layer_animation_controller_->AddValueObserver(pending_value_observer_.get()); |
| 290 } |
| 291 |
| 292 void AnimationPlayer::DestroyPendingValueObserver() { |
| 293 if (layer_animation_controller_ && pending_value_observer_) |
| 294 layer_animation_controller_->RemoveValueObserver( |
| 295 pending_value_observer_.get()); |
| 296 pending_value_observer_ = nullptr; |
| 297 } |
| 298 |
| 299 void AnimationPlayer::RefreshObservers() { |
| 300 if (layer_animation_controller_ && active_value_observer_) { |
| 301 layer_animation_controller_->set_value_provider(this); |
| 302 layer_animation_controller_->set_layer_animation_delegate( |
| 303 layer_animation_delegate_); |
| 304 } |
| 305 } |
| 306 |
| 307 gfx::ScrollOffset AnimationPlayer::ScrollOffsetForAnimation() const { |
| 308 // TODO(loyso): implement it. |
| 309 return gfx::ScrollOffset(); |
| 310 } |
| 311 |
| 312 } // namespace cc |
OLD | NEW |