Chromium Code Reviews| Index: cc/animation/animation_player.cc |
| diff --git a/cc/animation/animation_player.cc b/cc/animation/animation_player.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8e315a5d0f83f368ef51b1b1d14e3c1b1ad84b11 |
| --- /dev/null |
| +++ b/cc/animation/animation_player.cc |
| @@ -0,0 +1,226 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cc/animation/animation_player.h" |
| + |
| +#include "base/atomic_sequence_num.h" |
| +#include "cc/animation/animation_timeline.h" |
| +#include "cc/layers/layer.h" |
| +#include "cc/trees/layer_tree_mutators_client.h" |
| + |
| +namespace cc { |
| + |
| +base::StaticAtomicSequenceNumber g_next_animation_player_id; |
| + |
| +scoped_refptr<AnimationPlayer> AnimationPlayer::Create(int id) { |
| + return make_scoped_refptr(new AnimationPlayer(id)); |
| +} |
| + |
| +AnimationPlayer::AnimationPlayer(int id) |
| + : animation_timeline_(), |
| + layer_animation_delegate_(), |
| + id_(id), |
| + layer_id_(0) { |
| + if (id_ == 0) |
| + id_ = g_next_animation_player_id.GetNext() + 1; |
|
ajuma
2015/02/23 16:36:44
Having a combination of provided and automatically
loyso (OOO)
2015/02/25 04:37:03
I agree. That's trivial.
|
| +} |
| + |
| +AnimationPlayer::~AnimationPlayer() { |
| + DCHECK(!animation_timeline()); |
| + if (layer_animation_controller_) |
| + DetachLayer(); |
| +} |
| + |
| +scoped_refptr<AnimationPlayer> AnimationPlayer::CreateImplInstance() const { |
| + scoped_refptr<AnimationPlayer> to_return = AnimationPlayer::Create(id()); |
| + to_return->pending_value_observer_ = |
| + make_scoped_ptr(new AnimationPlayerPendingValueObserver(to_return.get())); |
| + return to_return; |
| +} |
| + |
| +void AnimationPlayer::SetAnimationTimeline(AnimationTimeline* timeline) { |
| + animation_timeline_ = timeline; |
| + if (layer_animation_controller_) |
| + layer_animation_controller_->SetAnimationRegistrar( |
| + animation_timeline_ |
| + ? animation_timeline_->layer_tree_mutators_client() |
| + ->GetAnimationRegistrar() |
| + : nullptr); |
| +} |
| + |
| +void AnimationPlayer::set_layer_animation_delegate( |
| + AnimationDelegate* delegate) { |
| + layer_animation_delegate_ = delegate; |
| + if (layer_animation_controller_) |
| + layer_animation_controller_->set_layer_animation_delegate(delegate); |
| +} |
| + |
| +void AnimationPlayer::AttachLayer(int layer_id) { |
| + DCHECK_EQ(layer_id_, 0); |
| + DCHECK_NE(layer_id, 0); |
| + layer_id_ = layer_id; |
| + |
| + layer_animation_controller_ = LayerAnimationController::Create(layer_id_); |
| + layer_animation_controller_->AddValueObserver(this); |
| + layer_animation_controller_->set_value_provider(this); |
| + layer_animation_controller_->set_layer_animation_delegate( |
| + layer_animation_delegate_); |
| + if (animation_timeline_) |
| + layer_animation_controller_->SetAnimationRegistrar( |
| + animation_timeline_->layer_tree_mutators_client() |
| + ->GetAnimationRegistrar()); |
| + |
| + if (pending_value_observer_) |
| + layer_animation_controller_->AddValueObserver( |
| + pending_value_observer_.get()); |
|
ajuma
2015/02/23 16:36:44
The asymmetry between active and pending observers
loyso (OOO)
2015/02/25 04:37:03
This is messy intermediate solution. We get rid of
ajuma
2015/02/25 14:57:25
We should aim to keep things as clean and readable
loyso (OOO)
2015/02/26 03:04:44
No, not at all. We can spawn many nested classes.
|
| +} |
| + |
| +void AnimationPlayer::DetachLayer() { |
| + DCHECK_NE(layer_id_, 0); |
| + DCHECK(layer_animation_controller_); |
| + |
| + if (pending_value_observer_) |
| + layer_animation_controller_->RemoveValueObserver( |
| + pending_value_observer_.get()); |
| + |
| + layer_animation_controller_->RemoveValueObserver(this); |
| + layer_animation_controller_->remove_value_provider(this); |
| + layer_animation_controller_->set_layer_animation_delegate(nullptr); |
| + layer_animation_controller_->SetAnimationRegistrar(nullptr); |
| + layer_animation_controller_ = nullptr; |
| + |
| + layer_id_ = 0; |
| +} |
| + |
| +void AnimationPlayer::AddAnimation(scoped_ptr<Animation> animation) { |
| + DCHECK(layer_animation_controller_); |
| + layer_animation_controller_->AddAnimation(animation.Pass()); |
| + SetNeedsCommit(); |
| +} |
| + |
| +void AnimationPlayer::PauseAnimation(int animation_id, double time_offset) { |
| + DCHECK(layer_animation_controller_); |
| + layer_animation_controller_->PauseAnimation( |
| + animation_id, base::TimeDelta::FromSecondsD(time_offset)); |
| + SetNeedsCommit(); |
| +} |
| + |
| +void AnimationPlayer::RemoveAnimation(int animation_id) { |
| + DCHECK(layer_animation_controller_); |
| + layer_animation_controller_->RemoveAnimation(animation_id); |
| + SetNeedsCommit(); |
| +} |
| + |
| +void AnimationPlayer::PushPropertiesTo(AnimationPlayer* player_impl) { |
| + if (!layer_animation_controller()) { |
| + if (player_impl->layer_animation_controller()) |
| + player_impl->DetachLayer(); |
| + return; |
| + } |
| + |
| + DCHECK_NE(layer_id_, 0); |
| + if (!player_impl->layer_animation_controller()) |
| + player_impl->AttachLayer(layer_id_); |
| + |
| + DCHECK(player_impl->layer_animation_controller()); |
| + layer_animation_controller_->PushAnimationUpdatesTo( |
| + player_impl->layer_animation_controller()); |
| +} |
| + |
| +void AnimationPlayer::SetNeedsCommit() { |
| + DCHECK(animation_timeline_); |
| + animation_timeline_->layer_tree_mutators_client()->SetMutatorsNeedCommit(); |
| +} |
| + |
| +void AnimationPlayer::SetFilterMutated(bool active_tree, |
| + const FilterOperations& filters) { |
| + if (animation_timeline() && layer_id_) { |
|
ajuma
2015/02/23 16:36:44
Should the check for layer_id_ really be a DCHECK?
loyso (OOO)
2015/02/25 04:37:03
Yes, we can go with DCHECK here.
|
| + DCHECK(animation_timeline()->layer_tree_mutators_client()); |
| + animation_timeline()->layer_tree_mutators_client()->SetLayerFilterMutated( |
| + layer_id_, active_tree, filters); |
| + } |
| +} |
| + |
| +void AnimationPlayer::SetOpacityMutated(bool active_tree, float opacity) { |
| + if (animation_timeline() && layer_id_) { |
| + DCHECK(animation_timeline()->layer_tree_mutators_client()); |
| + animation_timeline()->layer_tree_mutators_client()->SetLayerOpacityMutated( |
| + layer_id_, active_tree, opacity); |
| + } |
| +} |
| + |
| +void AnimationPlayer::SetTransformMutated(bool active_tree, |
| + const gfx::Transform& transform) { |
| + if (animation_timeline() && layer_id_) { |
| + DCHECK(animation_timeline()->layer_tree_mutators_client()); |
| + animation_timeline() |
| + ->layer_tree_mutators_client() |
| + ->SetLayerTransformMutated(layer_id_, active_tree, transform); |
| + } |
| +} |
| + |
| +gfx::ScrollOffset AnimationPlayer::ScrollOffsetForAnimation() const { |
| + // TODO(loyso): implement it. |
| + return gfx::ScrollOffset(); |
| +} |
| + |
| +void AnimationPlayer::OnFilterAnimated(const FilterOperations& filters) { |
| + SetFilterMutated(true, filters); |
| +} |
| + |
| +void AnimationPlayer::OnOpacityAnimated(float opacity) { |
| + SetOpacityMutated(true, opacity); |
| +} |
| + |
| +void AnimationPlayer::OnTransformAnimated(const gfx::Transform& transform) { |
| + SetTransformMutated(true, transform); |
| +} |
| + |
| +void AnimationPlayer::OnScrollOffsetAnimated( |
| + const gfx::ScrollOffset& scroll_offset) { |
| + // TODO(loyso): implement it. |
| +} |
| + |
| +void AnimationPlayer::OnAnimationWaitingForDeletion() { |
| + // TODO(loyso): implement it. |
| +} |
| + |
| +bool AnimationPlayer::IsActive() const { |
| + return true; |
| +} |
| + |
| +AnimationPlayerPendingValueObserver::AnimationPlayerPendingValueObserver( |
| + AnimationPlayer* player) |
| + : player_(player) { |
| + DCHECK(player_); |
| +} |
| + |
| +void AnimationPlayerPendingValueObserver::OnFilterAnimated( |
| + const FilterOperations& filters) { |
| + player_->SetFilterMutated(false, filters); |
| +} |
| + |
| +void AnimationPlayerPendingValueObserver::OnOpacityAnimated(float opacity) { |
| + player_->SetOpacityMutated(false, opacity); |
| +} |
| + |
| +void AnimationPlayerPendingValueObserver::OnTransformAnimated( |
| + const gfx::Transform& transform) { |
| + player_->SetTransformMutated(false, transform); |
| +} |
| + |
| +void AnimationPlayerPendingValueObserver::OnScrollOffsetAnimated( |
| + const gfx::ScrollOffset& scroll_offset) { |
| + // TODO(loyso): implement it. |
| +} |
| + |
| +void AnimationPlayerPendingValueObserver::OnAnimationWaitingForDeletion() { |
| + // TODO(loyso): implement it. |
| +} |
| + |
| +bool AnimationPlayerPendingValueObserver::IsActive() const { |
| + return false; |
| +} |
| + |
| +} // namespace cc |