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/element_animations.h" |
| 6 |
| 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" |
| 12 |
| 13 namespace cc { |
| 14 |
| 15 class ElementAnimations::ValueObserver : public LayerAnimationValueObserver { |
| 16 public: |
| 17 ValueObserver(ElementAnimations* element_animation, LayerTreeType tree_type) |
| 18 : element_animations_(element_animation), tree_type_(tree_type) { |
| 19 DCHECK(element_animations_); |
| 20 } |
| 21 |
| 22 // LayerAnimationValueObserver implementation. |
| 23 void OnFilterAnimated(const FilterOperations& filters) override { |
| 24 element_animations_->SetFilterMutated(tree_type_, filters); |
| 25 } |
| 26 |
| 27 void OnOpacityAnimated(float opacity) override { |
| 28 element_animations_->SetOpacityMutated(tree_type_, opacity); |
| 29 } |
| 30 |
| 31 void OnTransformAnimated(const gfx::Transform& transform) override { |
| 32 element_animations_->SetTransformMutated(tree_type_, transform); |
| 33 } |
| 34 |
| 35 void OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) override { |
| 36 element_animations_->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 ElementAnimations* element_animations_; |
| 47 const LayerTreeType tree_type_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(ValueObserver); |
| 50 }; |
| 51 |
| 52 scoped_ptr<ElementAnimations> ElementAnimations::Create(AnimationHost* host) { |
| 53 return make_scoped_ptr(new ElementAnimations(host)); |
| 54 } |
| 55 |
| 56 ElementAnimations::ElementAnimations(AnimationHost* host) |
| 57 : players_list_(make_scoped_ptr(new PlayersList())), animation_host_(host) { |
| 58 DCHECK(animation_host_); |
| 59 } |
| 60 |
| 61 ElementAnimations::~ElementAnimations() { |
| 62 DCHECK(!layer_animation_controller_); |
| 63 } |
| 64 |
| 65 void ElementAnimations::CreateLayerAnimationController(int layer_id) { |
| 66 DCHECK(layer_id); |
| 67 DCHECK(!layer_animation_controller_); |
| 68 DCHECK(animation_host_); |
| 69 |
| 70 AnimationRegistrar* registrar = animation_host_->animation_registrar(); |
| 71 DCHECK(registrar); |
| 72 |
| 73 layer_animation_controller_ = |
| 74 registrar->GetAnimationControllerForId(layer_id); |
| 75 layer_animation_controller_->SetAnimationRegistrar(registrar); |
| 76 layer_animation_controller_->set_layer_animation_delegate(this); |
| 77 layer_animation_controller_->set_value_provider(this); |
| 78 |
| 79 DCHECK(animation_host_->mutator_host_client()); |
| 80 if (animation_host_->mutator_host_client()->IsLayerInTree( |
| 81 layer_id, LayerTreeType::ACTIVE)) |
| 82 CreateActiveValueObserver(); |
| 83 if (animation_host_->mutator_host_client()->IsLayerInTree( |
| 84 layer_id, LayerTreeType::PENDING)) |
| 85 CreatePendingValueObserver(); |
| 86 } |
| 87 |
| 88 void ElementAnimations::DestroyLayerAnimationController() { |
| 89 DCHECK(animation_host_); |
| 90 |
| 91 DestroyPendingValueObserver(); |
| 92 DestroyActiveValueObserver(); |
| 93 |
| 94 if (layer_animation_controller_) { |
| 95 layer_animation_controller_->remove_value_provider(this); |
| 96 layer_animation_controller_->remove_layer_animation_delegate(this); |
| 97 layer_animation_controller_->SetAnimationRegistrar(nullptr); |
| 98 layer_animation_controller_ = nullptr; |
| 99 } |
| 100 } |
| 101 |
| 102 void ElementAnimations::LayerRegistered(int layer_id, LayerTreeType tree_type) { |
| 103 DCHECK(layer_animation_controller_); |
| 104 DCHECK_EQ(layer_animation_controller_->id(), layer_id); |
| 105 |
| 106 if (tree_type == LayerTreeType::ACTIVE) { |
| 107 if (!active_value_observer_) |
| 108 CreateActiveValueObserver(); |
| 109 } else { |
| 110 if (!pending_value_observer_) |
| 111 CreatePendingValueObserver(); |
| 112 } |
| 113 } |
| 114 |
| 115 void ElementAnimations::LayerUnregistered(int layer_id, |
| 116 LayerTreeType tree_type) { |
| 117 DCHECK_EQ(this->layer_id(), layer_id); |
| 118 tree_type == LayerTreeType::ACTIVE ? DestroyActiveValueObserver() |
| 119 : DestroyPendingValueObserver(); |
| 120 } |
| 121 |
| 122 void ElementAnimations::AddPlayer(AnimationPlayer* player) { |
| 123 players_list_->Append(player); |
| 124 } |
| 125 |
| 126 void ElementAnimations::RemovePlayer(AnimationPlayer* player) { |
| 127 for (PlayersListNode* node = players_list_->head(); |
| 128 node != players_list_->end(); node = node->next()) { |
| 129 if (node->value() == player) { |
| 130 node->RemoveFromList(); |
| 131 return; |
| 132 } |
| 133 } |
| 134 } |
| 135 |
| 136 bool ElementAnimations::IsEmpty() const { |
| 137 return players_list_->empty(); |
| 138 } |
| 139 |
| 140 void ElementAnimations::PushPropertiesTo( |
| 141 ElementAnimations* element_animations_impl) { |
| 142 DCHECK(layer_animation_controller_); |
| 143 DCHECK(element_animations_impl->layer_animation_controller()); |
| 144 |
| 145 layer_animation_controller_->PushAnimationUpdatesTo( |
| 146 element_animations_impl->layer_animation_controller()); |
| 147 } |
| 148 |
| 149 void ElementAnimations::SetFilterMutated(LayerTreeType tree_type, |
| 150 const FilterOperations& filters) { |
| 151 DCHECK(layer_id()); |
| 152 DCHECK(animation_host()); |
| 153 DCHECK(animation_host()->mutator_host_client()); |
| 154 animation_host()->mutator_host_client()->SetLayerFilterMutated( |
| 155 layer_id(), tree_type, filters); |
| 156 } |
| 157 |
| 158 void ElementAnimations::SetOpacityMutated(LayerTreeType tree_type, |
| 159 float opacity) { |
| 160 DCHECK(layer_id()); |
| 161 DCHECK(animation_host()); |
| 162 DCHECK(animation_host()->mutator_host_client()); |
| 163 animation_host()->mutator_host_client()->SetLayerOpacityMutated( |
| 164 layer_id(), tree_type, opacity); |
| 165 } |
| 166 |
| 167 void ElementAnimations::SetTransformMutated(LayerTreeType tree_type, |
| 168 const gfx::Transform& transform) { |
| 169 DCHECK(layer_id()); |
| 170 DCHECK(animation_host()); |
| 171 DCHECK(animation_host()->mutator_host_client()); |
| 172 animation_host()->mutator_host_client()->SetLayerTransformMutated( |
| 173 layer_id(), tree_type, transform); |
| 174 } |
| 175 |
| 176 void ElementAnimations::SetScrollOffsetMutated( |
| 177 LayerTreeType tree_type, |
| 178 const gfx::ScrollOffset& scroll_offset) { |
| 179 DCHECK(layer_id()); |
| 180 DCHECK(animation_host()); |
| 181 DCHECK(animation_host()->mutator_host_client()); |
| 182 animation_host()->mutator_host_client()->SetLayerScrollOffsetMutated( |
| 183 layer_id(), tree_type, scroll_offset); |
| 184 } |
| 185 |
| 186 void ElementAnimations::CreateActiveValueObserver() { |
| 187 DCHECK(layer_animation_controller_); |
| 188 DCHECK(!active_value_observer_); |
| 189 active_value_observer_ = |
| 190 make_scoped_ptr(new ValueObserver(this, LayerTreeType::ACTIVE)); |
| 191 layer_animation_controller_->AddValueObserver(active_value_observer_.get()); |
| 192 } |
| 193 |
| 194 void ElementAnimations::DestroyActiveValueObserver() { |
| 195 if (layer_animation_controller_ && active_value_observer_) |
| 196 layer_animation_controller_->RemoveValueObserver( |
| 197 active_value_observer_.get()); |
| 198 active_value_observer_ = nullptr; |
| 199 } |
| 200 |
| 201 void ElementAnimations::CreatePendingValueObserver() { |
| 202 DCHECK(layer_animation_controller_); |
| 203 DCHECK(!pending_value_observer_); |
| 204 pending_value_observer_ = |
| 205 make_scoped_ptr(new ValueObserver(this, LayerTreeType::PENDING)); |
| 206 layer_animation_controller_->AddValueObserver(pending_value_observer_.get()); |
| 207 } |
| 208 |
| 209 void ElementAnimations::DestroyPendingValueObserver() { |
| 210 if (layer_animation_controller_ && pending_value_observer_) |
| 211 layer_animation_controller_->RemoveValueObserver( |
| 212 pending_value_observer_.get()); |
| 213 pending_value_observer_ = nullptr; |
| 214 } |
| 215 |
| 216 void ElementAnimations::NotifyAnimationStarted( |
| 217 base::TimeTicks monotonic_time, |
| 218 Animation::TargetProperty target_property, |
| 219 int group) { |
| 220 for (PlayersListNode* node = players_list_->head(); |
| 221 node != players_list_->end(); node = node->next()) { |
| 222 AnimationPlayer* player = node->value(); |
| 223 player->NotifyAnimationStarted(monotonic_time, target_property, group); |
| 224 } |
| 225 } |
| 226 |
| 227 void ElementAnimations::NotifyAnimationFinished( |
| 228 base::TimeTicks monotonic_time, |
| 229 Animation::TargetProperty target_property, |
| 230 int group) { |
| 231 for (PlayersListNode* node = players_list_->head(); |
| 232 node != players_list_->end(); node = node->next()) { |
| 233 AnimationPlayer* player = node->value(); |
| 234 player->NotifyAnimationFinished(monotonic_time, target_property, group); |
| 235 } |
| 236 } |
| 237 |
| 238 gfx::ScrollOffset ElementAnimations::ScrollOffsetForAnimation() const { |
| 239 // TODO(loyso): implement it. |
| 240 return gfx::ScrollOffset(); |
| 241 } |
| 242 |
| 243 } // namespace cc |
OLD | NEW |