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.
5 #include "cc/animation/element_animations.h"
7 #include "cc/animation/animation_host.h"
8 #include "cc/animation/animation_player.h"
9 #include "cc/animation/animation_registrar.h"
10 #include "cc/animation/layer_animation_value_observer.h"
11 #include "cc/trees/mutator_host_client.h"
15 class ElementAnimations::ValueObserver
: public LayerAnimationValueObserver
{
17 ValueObserver(ElementAnimations
* element_animation
, LayerTreeType tree_type
)
18 : element_animations_(element_animation
), tree_type_(tree_type
) {
19 DCHECK(element_animations_
);
22 // LayerAnimationValueObserver implementation.
23 void OnFilterAnimated(const FilterOperations
& filters
) override
{
24 element_animations_
->SetFilterMutated(tree_type_
, filters
);
27 void OnOpacityAnimated(float opacity
) override
{
28 element_animations_
->SetOpacityMutated(tree_type_
, opacity
);
31 void OnTransformAnimated(const gfx::Transform
& transform
) override
{
32 element_animations_
->SetTransformMutated(tree_type_
, transform
);
35 void OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) override
{
36 element_animations_
->SetScrollOffsetMutated(tree_type_
, scroll_offset
);
39 void OnAnimationWaitingForDeletion() override
{
40 // TODO(loyso): See Layer::OnAnimationWaitingForDeletion. But we always do
41 // PushProperties for AnimationTimelines for now.
44 bool IsActive() const override
{ return tree_type_
== LayerTreeType::ACTIVE
; }
47 ElementAnimations
* element_animations_
;
48 const LayerTreeType tree_type_
;
50 DISALLOW_COPY_AND_ASSIGN(ValueObserver
);
53 scoped_ptr
<ElementAnimations
> ElementAnimations::Create(AnimationHost
* host
) {
54 return make_scoped_ptr(new ElementAnimations(host
));
57 ElementAnimations::ElementAnimations(AnimationHost
* host
)
58 : players_list_(make_scoped_ptr(new PlayersList())), animation_host_(host
) {
59 DCHECK(animation_host_
);
62 ElementAnimations::~ElementAnimations() {
63 DCHECK(!layer_animation_controller_
);
66 void ElementAnimations::CreateLayerAnimationController(int layer_id
) {
68 DCHECK(!layer_animation_controller_
);
69 DCHECK(animation_host_
);
71 AnimationRegistrar
* registrar
= animation_host_
->animation_registrar();
74 layer_animation_controller_
=
75 registrar
->GetAnimationControllerForId(layer_id
);
76 layer_animation_controller_
->SetAnimationRegistrar(registrar
);
77 layer_animation_controller_
->set_layer_animation_delegate(this);
78 layer_animation_controller_
->set_value_provider(this);
80 DCHECK(animation_host_
->mutator_host_client());
81 if (animation_host_
->mutator_host_client()->IsLayerInTree(
82 layer_id
, LayerTreeType::ACTIVE
))
83 CreateActiveValueObserver();
84 if (animation_host_
->mutator_host_client()->IsLayerInTree(
85 layer_id
, LayerTreeType::PENDING
))
86 CreatePendingValueObserver();
89 void ElementAnimations::DestroyLayerAnimationController() {
90 DCHECK(animation_host_
);
92 DestroyPendingValueObserver();
93 DestroyActiveValueObserver();
95 if (layer_animation_controller_
) {
96 layer_animation_controller_
->remove_value_provider(this);
97 layer_animation_controller_
->remove_layer_animation_delegate(this);
98 layer_animation_controller_
->SetAnimationRegistrar(nullptr);
99 layer_animation_controller_
= nullptr;
103 void ElementAnimations::LayerRegistered(int layer_id
, LayerTreeType tree_type
) {
104 DCHECK(layer_animation_controller_
);
105 DCHECK_EQ(layer_animation_controller_
->id(), layer_id
);
107 if (tree_type
== LayerTreeType::ACTIVE
) {
108 if (!active_value_observer_
)
109 CreateActiveValueObserver();
111 if (!pending_value_observer_
)
112 CreatePendingValueObserver();
116 void ElementAnimations::LayerUnregistered(int layer_id
,
117 LayerTreeType tree_type
) {
118 DCHECK_EQ(this->layer_id(), layer_id
);
119 tree_type
== LayerTreeType::ACTIVE
? DestroyActiveValueObserver()
120 : DestroyPendingValueObserver();
123 void ElementAnimations::AddPlayer(AnimationPlayer
* player
) {
124 players_list_
->Append(player
);
127 void ElementAnimations::RemovePlayer(AnimationPlayer
* player
) {
128 for (PlayersListNode
* node
= players_list_
->head();
129 node
!= players_list_
->end(); node
= node
->next()) {
130 if (node
->value() == player
) {
131 node
->RemoveFromList();
137 bool ElementAnimations::IsEmpty() const {
138 return players_list_
->empty();
141 void ElementAnimations::PushPropertiesTo(
142 ElementAnimations
* element_animations_impl
) {
143 DCHECK(layer_animation_controller_
);
144 DCHECK(element_animations_impl
->layer_animation_controller());
146 layer_animation_controller_
->PushAnimationUpdatesTo(
147 element_animations_impl
->layer_animation_controller());
150 void ElementAnimations::SetFilterMutated(LayerTreeType tree_type
,
151 const FilterOperations
& filters
) {
153 DCHECK(animation_host());
154 DCHECK(animation_host()->mutator_host_client());
155 animation_host()->mutator_host_client()->SetLayerFilterMutated(
156 layer_id(), tree_type
, filters
);
159 void ElementAnimations::SetOpacityMutated(LayerTreeType tree_type
,
162 DCHECK(animation_host());
163 DCHECK(animation_host()->mutator_host_client());
164 animation_host()->mutator_host_client()->SetLayerOpacityMutated(
165 layer_id(), tree_type
, opacity
);
168 void ElementAnimations::SetTransformMutated(LayerTreeType tree_type
,
169 const gfx::Transform
& transform
) {
171 DCHECK(animation_host());
172 DCHECK(animation_host()->mutator_host_client());
173 animation_host()->mutator_host_client()->SetLayerTransformMutated(
174 layer_id(), tree_type
, transform
);
177 void ElementAnimations::SetScrollOffsetMutated(
178 LayerTreeType tree_type
,
179 const gfx::ScrollOffset
& scroll_offset
) {
181 DCHECK(animation_host());
182 DCHECK(animation_host()->mutator_host_client());
183 animation_host()->mutator_host_client()->SetLayerScrollOffsetMutated(
184 layer_id(), tree_type
, scroll_offset
);
187 void ElementAnimations::CreateActiveValueObserver() {
188 DCHECK(layer_animation_controller_
);
189 DCHECK(!active_value_observer_
);
190 active_value_observer_
=
191 make_scoped_ptr(new ValueObserver(this, LayerTreeType::ACTIVE
));
192 layer_animation_controller_
->AddValueObserver(active_value_observer_
.get());
195 void ElementAnimations::DestroyActiveValueObserver() {
196 if (layer_animation_controller_
&& active_value_observer_
)
197 layer_animation_controller_
->RemoveValueObserver(
198 active_value_observer_
.get());
199 active_value_observer_
= nullptr;
202 void ElementAnimations::CreatePendingValueObserver() {
203 DCHECK(layer_animation_controller_
);
204 DCHECK(!pending_value_observer_
);
205 pending_value_observer_
=
206 make_scoped_ptr(new ValueObserver(this, LayerTreeType::PENDING
));
207 layer_animation_controller_
->AddValueObserver(pending_value_observer_
.get());
210 void ElementAnimations::DestroyPendingValueObserver() {
211 if (layer_animation_controller_
&& pending_value_observer_
)
212 layer_animation_controller_
->RemoveValueObserver(
213 pending_value_observer_
.get());
214 pending_value_observer_
= nullptr;
217 void ElementAnimations::NotifyAnimationStarted(
218 base::TimeTicks monotonic_time
,
219 Animation::TargetProperty target_property
,
221 for (PlayersListNode
* node
= players_list_
->head();
222 node
!= players_list_
->end(); node
= node
->next()) {
223 AnimationPlayer
* player
= node
->value();
224 player
->NotifyAnimationStarted(monotonic_time
, target_property
, group
);
228 void ElementAnimations::NotifyAnimationFinished(
229 base::TimeTicks monotonic_time
,
230 Animation::TargetProperty target_property
,
232 for (PlayersListNode
* node
= players_list_
->head();
233 node
!= players_list_
->end(); node
= node
->next()) {
234 AnimationPlayer
* player
= node
->value();
235 player
->NotifyAnimationFinished(monotonic_time
, target_property
, group
);
239 gfx::ScrollOffset
ElementAnimations::ScrollOffsetForAnimation() const {
240 DCHECK(layer_animation_controller_
);
241 if (animation_host()) {
242 DCHECK(animation_host()->mutator_host_client());
243 return animation_host()->mutator_host_client()->GetScrollOffsetForAnimation(
247 return gfx::ScrollOffset();