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, bool is_active) |
| 18 : player_(player), is_active_(is_active) { |
| 19 DCHECK(player_); |
| 20 } |
| 21 |
| 22 // LayerAnimationValueObserver implementation. |
| 23 void OnFilterAnimated(const FilterOperations& filters) override { |
| 24 player_->SetFilterMutated(is_active_, filters); |
| 25 } |
| 26 |
| 27 void OnOpacityAnimated(float opacity) override { |
| 28 player_->SetOpacityMutated(is_active_, opacity); |
| 29 } |
| 30 |
| 31 void OnTransformAnimated(const gfx::Transform& transform) override { |
| 32 player_->SetTransformMutated(is_active_, transform); |
| 33 } |
| 34 |
| 35 void OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) override { |
| 36 player_->SetScrollOffsetMutated(is_active_, scroll_offset); |
| 37 } |
| 38 |
| 39 void OnAnimationWaitingForDeletion() override { |
| 40 // TODO(loyso): implement it. |
| 41 } |
| 42 |
| 43 bool IsActive() const override { return is_active_; } |
| 44 |
| 45 private: |
| 46 AnimationPlayer* player_; |
| 47 bool is_active_; |
| 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 if (layer_id_ && animation_timeline_) |
| 85 RemoveControllerFromTimeline(); |
| 86 |
| 87 animation_timeline_ = timeline; |
| 88 |
| 89 if (layer_id_ && animation_timeline_) |
| 90 AddControllerToTimeline(); |
| 91 } |
| 92 |
| 93 void AnimationPlayer::set_layer_animation_delegate( |
| 94 AnimationDelegate* delegate) { |
| 95 layer_animation_delegate_ = delegate; |
| 96 if (layer_animation_controller_) |
| 97 layer_animation_controller_->set_layer_animation_delegate(delegate); |
| 98 } |
| 99 |
| 100 void AnimationPlayer::AttachLayer(int layer_id) { |
| 101 DCHECK_EQ(layer_id_, 0); |
| 102 DCHECK_NE(layer_id, 0); |
| 103 DCHECK(!layer_animation_controller_); |
| 104 |
| 105 layer_id_ = layer_id; |
| 106 |
| 107 if (animation_timeline_) |
| 108 AddControllerToTimeline(); |
| 109 } |
| 110 |
| 111 void AnimationPlayer::DetachLayer() { |
| 112 DCHECK_NE(layer_id_, 0); |
| 113 |
| 114 if (animation_timeline_) |
| 115 RemoveControllerFromTimeline(); |
| 116 |
| 117 layer_animation_controller_ = nullptr; |
| 118 layer_id_ = 0; |
| 119 } |
| 120 |
| 121 void AnimationPlayer::AddControllerToTimeline() { |
| 122 DCHECK(layer_id_); |
| 123 DCHECK(animation_host_); |
| 124 |
| 125 AnimationRegistrar* registrar = animation_host_->animation_registrar(); |
| 126 DCHECK(registrar); |
| 127 |
| 128 layer_animation_controller_ = |
| 129 registrar->GetAnimationControllerForId(layer_id_); |
| 130 layer_animation_controller_->SetAnimationRegistrar(registrar); |
| 131 |
| 132 DCHECK(animation_host_->layer_tree_mutators_client()); |
| 133 if (animation_host_->layer_tree_mutators_client()->IsLayerInActiveTree( |
| 134 layer_id_)) |
| 135 CreateActiveValueObserver(); |
| 136 if (animation_host_->layer_tree_mutators_client()->IsLayerInPendingTree( |
| 137 layer_id_)) |
| 138 CreatePendingValueObserver(); |
| 139 |
| 140 animation_host_->RegisterPlayerForLayer(layer_id_, this); |
| 141 } |
| 142 |
| 143 void AnimationPlayer::RemoveControllerFromTimeline() { |
| 144 DCHECK(layer_id_); |
| 145 DCHECK(animation_host_); |
| 146 animation_host_->UnregisterPlayerForLayer(layer_id_, this); |
| 147 |
| 148 DestroyPendingValueObserver(); |
| 149 DestroyActiveValueObserver(); |
| 150 if (layer_animation_controller_) |
| 151 layer_animation_controller_->SetAnimationRegistrar(nullptr); |
| 152 layer_animation_controller_ = nullptr; |
| 153 } |
| 154 |
| 155 void AnimationPlayer::LayerImplRegistered(int layer_id, bool is_active_tree) { |
| 156 is_active_tree ? CreateActiveValueObserver() : CreatePendingValueObserver(); |
| 157 } |
| 158 |
| 159 void AnimationPlayer::LayerImplUnregistered(int layer_id, bool is_active_tree) { |
| 160 is_active_tree ? DestroyActiveValueObserver() : DestroyPendingValueObserver(); |
| 161 } |
| 162 |
| 163 void AnimationPlayer::AddAnimation(scoped_ptr<Animation> animation) { |
| 164 DCHECK(layer_animation_controller_); |
| 165 layer_animation_controller_->AddAnimation(animation.Pass()); |
| 166 SetNeedsCommit(); |
| 167 } |
| 168 |
| 169 void AnimationPlayer::PauseAnimation(int animation_id, double time_offset) { |
| 170 DCHECK(layer_animation_controller_); |
| 171 layer_animation_controller_->PauseAnimation( |
| 172 animation_id, base::TimeDelta::FromSecondsD(time_offset)); |
| 173 SetNeedsCommit(); |
| 174 } |
| 175 |
| 176 void AnimationPlayer::RemoveAnimation(int animation_id) { |
| 177 if (layer_animation_controller_) { |
| 178 layer_animation_controller_->RemoveAnimation(animation_id); |
| 179 SetNeedsCommit(); |
| 180 } |
| 181 } |
| 182 |
| 183 void AnimationPlayer::PushPropertiesTo(AnimationPlayer* player_impl) { |
| 184 if (!layer_animation_controller()) { |
| 185 if (player_impl->layer_animation_controller()) |
| 186 player_impl->DetachLayer(); |
| 187 return; |
| 188 } |
| 189 |
| 190 DCHECK_NE(layer_id_, 0); |
| 191 if (!player_impl->layer_animation_controller()) |
| 192 player_impl->AttachLayer(layer_id_); |
| 193 |
| 194 DCHECK(player_impl->layer_animation_controller()); |
| 195 layer_animation_controller_->PushAnimationUpdatesTo( |
| 196 player_impl->layer_animation_controller()); |
| 197 } |
| 198 |
| 199 void AnimationPlayer::SetNeedsCommit() { |
| 200 DCHECK(animation_host_); |
| 201 animation_host_->SetNeedsCommit(); |
| 202 } |
| 203 |
| 204 void AnimationPlayer::SetFilterMutated(bool active_tree, |
| 205 const FilterOperations& filters) { |
| 206 DCHECK(layer_id_); |
| 207 if (animation_host()) { |
| 208 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 209 animation_host()->layer_tree_mutators_client()->SetLayerFilterMutated( |
| 210 layer_id_, active_tree, filters); |
| 211 } |
| 212 } |
| 213 |
| 214 void AnimationPlayer::SetOpacityMutated(bool active_tree, float opacity) { |
| 215 DCHECK(layer_id_); |
| 216 if (animation_host()) { |
| 217 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 218 animation_host()->layer_tree_mutators_client()->SetLayerOpacityMutated( |
| 219 layer_id_, active_tree, opacity); |
| 220 } |
| 221 } |
| 222 |
| 223 void AnimationPlayer::SetTransformMutated(bool active_tree, |
| 224 const gfx::Transform& transform) { |
| 225 DCHECK(layer_id_); |
| 226 if (animation_host()) { |
| 227 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 228 animation_host()->layer_tree_mutators_client()->SetLayerTransformMutated( |
| 229 layer_id_, active_tree, transform); |
| 230 } |
| 231 } |
| 232 |
| 233 void AnimationPlayer::SetScrollOffsetMutated( |
| 234 bool active_tree, |
| 235 const gfx::ScrollOffset& scroll_offset) { |
| 236 DCHECK(layer_id_); |
| 237 if (animation_host()) { |
| 238 DCHECK(animation_host()->layer_tree_mutators_client()); |
| 239 animation_host()->layer_tree_mutators_client()->SetLayerScrollOffsetMutated( |
| 240 layer_id_, active_tree, scroll_offset); |
| 241 } |
| 242 } |
| 243 |
| 244 void AnimationPlayer::CreateActiveValueObserver() { |
| 245 DCHECK(layer_animation_controller_); |
| 246 DCHECK(!active_value_observer_); |
| 247 active_value_observer_ = make_scoped_ptr(new ValueObserver(this, true)); |
| 248 layer_animation_controller_->AddValueObserver(active_value_observer_.get()); |
| 249 |
| 250 layer_animation_controller_->set_value_provider(this); |
| 251 layer_animation_controller_->set_layer_animation_delegate( |
| 252 layer_animation_delegate_); |
| 253 } |
| 254 |
| 255 void AnimationPlayer::DestroyActiveValueObserver() { |
| 256 if (layer_animation_controller_ && active_value_observer_) |
| 257 layer_animation_controller_->RemoveValueObserver( |
| 258 active_value_observer_.get()); |
| 259 active_value_observer_ = nullptr; |
| 260 |
| 261 if (layer_animation_controller_) { |
| 262 layer_animation_controller_->remove_value_provider(this); |
| 263 layer_animation_controller_->remove_layer_animation_delegate( |
| 264 layer_animation_delegate_); |
| 265 } |
| 266 } |
| 267 |
| 268 void AnimationPlayer::CreatePendingValueObserver() { |
| 269 DCHECK(layer_animation_controller_); |
| 270 DCHECK(!pending_value_observer_); |
| 271 pending_value_observer_ = make_scoped_ptr(new ValueObserver(this, false)); |
| 272 layer_animation_controller_->AddValueObserver(pending_value_observer_.get()); |
| 273 } |
| 274 |
| 275 void AnimationPlayer::DestroyPendingValueObserver() { |
| 276 if (layer_animation_controller_ && pending_value_observer_) |
| 277 layer_animation_controller_->RemoveValueObserver( |
| 278 pending_value_observer_.get()); |
| 279 pending_value_observer_ = nullptr; |
| 280 } |
| 281 |
| 282 gfx::ScrollOffset AnimationPlayer::ScrollOffsetForAnimation() const { |
| 283 // TODO(loyso): implement it. |
| 284 return gfx::ScrollOffset(); |
| 285 } |
| 286 |
| 287 } // namespace cc |
OLD | NEW |