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_timeline.h" |
| 8 #include "cc/layers/layer.h" |
| 9 #include "cc/trees/layer_tree_mutators_client.h" |
| 10 |
| 11 namespace cc { |
| 12 |
| 13 class AnimationPlayer::ValueObserver : public LayerAnimationValueObserver { |
| 14 public: |
| 15 ValueObserver(AnimationPlayer* player, bool is_active) |
| 16 : player_(player), is_active_(is_active) { |
| 17 DCHECK(player_); |
| 18 } |
| 19 |
| 20 // LayerAnimationValueObserver implementation. |
| 21 void OnFilterAnimated(const FilterOperations& filters) override { |
| 22 player_->SetFilterMutated(is_active_, filters); |
| 23 } |
| 24 |
| 25 void OnOpacityAnimated(float opacity) override { |
| 26 player_->SetOpacityMutated(is_active_, opacity); |
| 27 } |
| 28 |
| 29 void OnTransformAnimated(const gfx::Transform& transform) override { |
| 30 player_->SetTransformMutated(is_active_, transform); |
| 31 } |
| 32 |
| 33 void OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) override { |
| 34 // TODO(loyso): implement it. |
| 35 } |
| 36 |
| 37 void OnAnimationWaitingForDeletion() override { |
| 38 // TODO(loyso): implement it. |
| 39 } |
| 40 |
| 41 bool IsActive() const override { return is_active_; } |
| 42 |
| 43 private: |
| 44 AnimationPlayer* player_; |
| 45 bool is_active_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(ValueObserver); |
| 48 }; |
| 49 |
| 50 scoped_refptr<AnimationPlayer> AnimationPlayer::Create(int id) { |
| 51 return make_scoped_refptr(new AnimationPlayer(id)); |
| 52 } |
| 53 |
| 54 AnimationPlayer::AnimationPlayer(int id) |
| 55 : animation_timeline_(), |
| 56 layer_animation_delegate_(), |
| 57 id_(id), |
| 58 layer_id_(0) { |
| 59 DCHECK(id_); |
| 60 } |
| 61 |
| 62 AnimationPlayer::~AnimationPlayer() { |
| 63 DCHECK(!animation_timeline_); |
| 64 DCHECK(!layer_animation_controller_); |
| 65 } |
| 66 |
| 67 scoped_refptr<AnimationPlayer> AnimationPlayer::CreateImplInstance() const { |
| 68 scoped_refptr<AnimationPlayer> to_return = AnimationPlayer::Create(id()); |
| 69 return to_return; |
| 70 } |
| 71 |
| 72 void AnimationPlayer::SetAnimationTimeline(AnimationTimeline* timeline) { |
| 73 animation_timeline_ = timeline; |
| 74 if (layer_animation_controller_) |
| 75 layer_animation_controller_->SetAnimationRegistrar( |
| 76 animation_timeline_ |
| 77 ? animation_timeline_->layer_tree_mutators_client() |
| 78 ->GetAnimationRegistrar() |
| 79 : nullptr); |
| 80 } |
| 81 |
| 82 void AnimationPlayer::set_layer_animation_delegate( |
| 83 AnimationDelegate* delegate) { |
| 84 layer_animation_delegate_ = delegate; |
| 85 if (layer_animation_controller_) |
| 86 layer_animation_controller_->set_layer_animation_delegate(delegate); |
| 87 } |
| 88 |
| 89 void AnimationPlayer::AttachLayer(int layer_id) { |
| 90 DCHECK_EQ(layer_id_, 0); |
| 91 DCHECK_NE(layer_id, 0); |
| 92 DCHECK(animation_timeline_); |
| 93 layer_id_ = layer_id; |
| 94 |
| 95 layer_animation_controller_ = LayerAnimationController::Create(layer_id_); |
| 96 layer_animation_controller_->SetAnimationRegistrar( |
| 97 animation_timeline_->layer_tree_mutators_client() |
| 98 ->GetAnimationRegistrar()); |
| 99 |
| 100 if (animation_timeline_->layer_tree_mutators_client()->IsLayerInActiveTree( |
| 101 layer_id_)) |
| 102 CreateActiveValueObserver(); |
| 103 if (animation_timeline_->layer_tree_mutators_client()->IsLayerInPendingTree( |
| 104 layer_id_)) |
| 105 CreatePendingValueObserver(); |
| 106 |
| 107 animation_timeline_->RegisterPlayerForLayer(layer_id_, this); |
| 108 } |
| 109 |
| 110 void AnimationPlayer::DetachLayer() { |
| 111 DCHECK_NE(layer_id_, 0); |
| 112 DCHECK(layer_animation_controller_); |
| 113 |
| 114 if (animation_timeline_) |
| 115 animation_timeline_->UnregisterPlayerForLayer(layer_id_, this); |
| 116 |
| 117 DestroyPendingValueObserver(); |
| 118 DestroyActiveValueObserver(); |
| 119 |
| 120 layer_animation_controller_ = nullptr; |
| 121 layer_id_ = 0; |
| 122 } |
| 123 |
| 124 void AnimationPlayer::LayerImplRegistered(int layer_id, bool is_active_tree) { |
| 125 is_active_tree ? CreateActiveValueObserver() : CreatePendingValueObserver(); |
| 126 } |
| 127 |
| 128 void AnimationPlayer::LayerImplUnregistered(int layer_id, bool is_active_tree) { |
| 129 is_active_tree ? DestroyActiveValueObserver() : DestroyPendingValueObserver(); |
| 130 } |
| 131 |
| 132 void AnimationPlayer::AddAnimation(scoped_ptr<Animation> animation) { |
| 133 DCHECK(layer_animation_controller_); |
| 134 layer_animation_controller_->AddAnimation(animation.Pass()); |
| 135 SetNeedsCommit(); |
| 136 } |
| 137 |
| 138 void AnimationPlayer::PauseAnimation(int animation_id, double time_offset) { |
| 139 DCHECK(layer_animation_controller_); |
| 140 layer_animation_controller_->PauseAnimation( |
| 141 animation_id, base::TimeDelta::FromSecondsD(time_offset)); |
| 142 SetNeedsCommit(); |
| 143 } |
| 144 |
| 145 void AnimationPlayer::RemoveAnimation(int animation_id) { |
| 146 DCHECK(layer_animation_controller_); |
| 147 layer_animation_controller_->RemoveAnimation(animation_id); |
| 148 SetNeedsCommit(); |
| 149 } |
| 150 |
| 151 void AnimationPlayer::PushPropertiesTo(AnimationPlayer* player_impl) { |
| 152 if (!layer_animation_controller()) { |
| 153 if (player_impl->layer_animation_controller()) |
| 154 player_impl->DetachLayer(); |
| 155 return; |
| 156 } |
| 157 |
| 158 DCHECK_NE(layer_id_, 0); |
| 159 if (!player_impl->layer_animation_controller()) |
| 160 player_impl->AttachLayer(layer_id_); |
| 161 |
| 162 DCHECK(player_impl->layer_animation_controller()); |
| 163 layer_animation_controller_->PushAnimationUpdatesTo( |
| 164 player_impl->layer_animation_controller()); |
| 165 } |
| 166 |
| 167 void AnimationPlayer::SetNeedsCommit() { |
| 168 DCHECK(animation_timeline_); |
| 169 animation_timeline_->layer_tree_mutators_client()->SetMutatorsNeedCommit(); |
| 170 } |
| 171 |
| 172 void AnimationPlayer::SetFilterMutated(bool active_tree, |
| 173 const FilterOperations& filters) { |
| 174 DCHECK(layer_id_); |
| 175 if (animation_timeline()) { |
| 176 DCHECK(animation_timeline()->layer_tree_mutators_client()); |
| 177 animation_timeline()->layer_tree_mutators_client()->SetLayerFilterMutated( |
| 178 layer_id_, active_tree, filters); |
| 179 } |
| 180 } |
| 181 |
| 182 void AnimationPlayer::SetOpacityMutated(bool active_tree, float opacity) { |
| 183 DCHECK(layer_id_); |
| 184 if (animation_timeline()) { |
| 185 DCHECK(animation_timeline()->layer_tree_mutators_client()); |
| 186 animation_timeline()->layer_tree_mutators_client()->SetLayerOpacityMutated( |
| 187 layer_id_, active_tree, opacity); |
| 188 } |
| 189 } |
| 190 |
| 191 void AnimationPlayer::SetTransformMutated(bool active_tree, |
| 192 const gfx::Transform& transform) { |
| 193 DCHECK(layer_id_); |
| 194 if (animation_timeline()) { |
| 195 DCHECK(animation_timeline()->layer_tree_mutators_client()); |
| 196 animation_timeline() |
| 197 ->layer_tree_mutators_client() |
| 198 ->SetLayerTransformMutated(layer_id_, active_tree, transform); |
| 199 } |
| 200 } |
| 201 |
| 202 void AnimationPlayer::CreateActiveValueObserver() { |
| 203 DCHECK(layer_animation_controller_); |
| 204 DCHECK(!active_value_observer_); |
| 205 active_value_observer_ = make_scoped_ptr(new ValueObserver(this, true)); |
| 206 layer_animation_controller_->AddValueObserver(active_value_observer_.get()); |
| 207 |
| 208 layer_animation_controller_->set_value_provider(this); |
| 209 layer_animation_controller_->set_layer_animation_delegate( |
| 210 layer_animation_delegate_); |
| 211 } |
| 212 |
| 213 void AnimationPlayer::DestroyActiveValueObserver() { |
| 214 DCHECK(layer_animation_controller_); |
| 215 if (active_value_observer_) |
| 216 layer_animation_controller_->RemoveValueObserver( |
| 217 active_value_observer_.get()); |
| 218 active_value_observer_ = nullptr; |
| 219 |
| 220 layer_animation_controller_->remove_value_provider(this); |
| 221 layer_animation_controller_->remove_layer_animation_delegate( |
| 222 layer_animation_delegate_); |
| 223 } |
| 224 |
| 225 void AnimationPlayer::CreatePendingValueObserver() { |
| 226 DCHECK(layer_animation_controller_); |
| 227 DCHECK(!pending_value_observer_); |
| 228 pending_value_observer_ = make_scoped_ptr(new ValueObserver(this, false)); |
| 229 layer_animation_controller_->AddValueObserver(pending_value_observer_.get()); |
| 230 } |
| 231 |
| 232 void AnimationPlayer::DestroyPendingValueObserver() { |
| 233 DCHECK(layer_animation_controller_); |
| 234 if (pending_value_observer_) |
| 235 layer_animation_controller_->RemoveValueObserver( |
| 236 pending_value_observer_.get()); |
| 237 pending_value_observer_ = nullptr; |
| 238 } |
| 239 |
| 240 gfx::ScrollOffset AnimationPlayer::ScrollOffsetForAnimation() const { |
| 241 // TODO(loyso): implement it. |
| 242 return gfx::ScrollOffset(); |
| 243 } |
| 244 |
| 245 } // namespace cc |
OLD | NEW |