Index: cc/animation/animation_host.cc |
diff --git a/cc/animation/animation_host.cc b/cc/animation/animation_host.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4a4f402fbd57feb08eb1c93f534e22601f6bea45 |
--- /dev/null |
+++ b/cc/animation/animation_host.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_host.h" |
+ |
+#include <algorithm> |
+ |
+#include "cc/animation/animation_player.h" |
+#include "cc/animation/animation_registrar.h" |
+#include "cc/animation/animation_timeline.h" |
+ |
+namespace cc { |
+ |
+scoped_ptr<AnimationHost> AnimationHost::Create( |
+ ThreadInstance thread_instance) { |
+ return make_scoped_ptr(new AnimationHost(thread_instance)); |
+} |
+ |
+AnimationHost::AnimationHost(ThreadInstance thread_instance) |
+ : animation_registrar_(AnimationRegistrar::Create()), |
+ layer_tree_mutators_client_(), |
+ thread_instance_(thread_instance) { |
+} |
+ |
+AnimationHost::~AnimationHost() { |
+ ClearTimelines(); |
+ DCHECK(!layer_tree_mutators_client()); |
+ DCHECK(layer_to_players_map_.empty()); |
+} |
+ |
+AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { |
+ for (auto& timeline : timelines_) |
+ if (timeline->id() == timeline_id) |
+ return timeline.get(); |
+ return nullptr; |
+} |
+ |
+void AnimationHost::ClearTimelines() { |
+ EraseTimelines(timelines_.begin(), timelines_.end()); |
+} |
+ |
+void AnimationHost::EraseTimelines(AnimationTimelineList::iterator begin, |
+ AnimationTimelineList::iterator end) { |
+ for (auto i = begin; i != end; ++i) { |
+ auto& timeline = *i; |
+ timeline->ClearPlayers(); |
+ timeline->SetAnimationHost(nullptr); |
+ } |
+ |
+ timelines_.erase(begin, end); |
+} |
+ |
+void AnimationHost::AddAnimationTimeline( |
+ scoped_refptr<AnimationTimeline> timeline) { |
+ timeline->SetAnimationHost(this); |
+ timelines_.push_back(timeline); |
+} |
+ |
+void AnimationHost::RemoveAnimationTimeline( |
+ scoped_refptr<AnimationTimeline> timeline) { |
+ for (auto iter = timelines_.begin(); iter != timelines_.end(); ++iter) { |
+ if (iter->get() != timeline) |
+ continue; |
+ |
+ EraseTimelines(iter, iter + 1); |
+ break; |
+ } |
+} |
+ |
+void AnimationHost::RegisterLayer(int layer_id, LayerTreeType tree_type) { |
+ const PlayersList* players_list = GetPlayersForLayerId(layer_id); |
+ if (!players_list) |
+ return; |
+ |
+ for (base::LinkNode<AnimationPlayer>* node = players_list->head(); |
+ node != players_list->end(); node = node->next()) { |
+ AnimationPlayer* player = node->value(); |
+ player->LayerRegistered(layer_id, tree_type); |
+ } |
+} |
+ |
+void AnimationHost::UnregisterLayer(int layer_id, LayerTreeType tree_type) { |
+ const PlayersList* players_list = GetPlayersForLayerId(layer_id); |
+ if (!players_list) |
+ return; |
+ |
+ for (base::LinkNode<AnimationPlayer>* node = players_list->head(); |
+ node != players_list->end(); node = node->next()) { |
+ AnimationPlayer* player = node->value(); |
+ player->LayerUnregistered(layer_id, tree_type); |
+ } |
+} |
+ |
+void AnimationHost::RegisterPlayerForLayer(int layer_id, |
+ AnimationPlayer* player) { |
+ DCHECK(layer_id); |
+ DCHECK(player); |
+ |
+ PlayersList* players_list = GetPlayersForLayerId(layer_id); |
+ if (!players_list) { |
+ // TODO(loyso): Make base::LinkedList movable to avoid this heap allocation. |
+ // Depends on C++11 library which is banned for now. crbug.com/360096 |
+ players_list = new PlayersList(); |
+ layer_to_players_map_.insert(std::make_pair(layer_id, players_list)); |
+ } |
+ |
+ DCHECK(players_list); |
+ players_list->Append(player); |
+} |
+ |
+void AnimationHost::UnregisterPlayerForLayer(int layer_id, |
+ AnimationPlayer* player) { |
+ DCHECK(layer_id); |
+ DCHECK(player); |
+ PlayersList* players_list = GetPlayersForLayerId(layer_id); |
+ DCHECK(players_list); |
+ |
+ for (base::LinkNode<AnimationPlayer>* node = players_list->head(); |
+ node != players_list->end(); node = node->next()) { |
+ AnimationPlayer* value = node->value(); |
+ if (value == player) { |
+ node->RemoveFromList(); |
+ if (players_list->empty()) { |
+ layer_to_players_map_.erase(layer_id); |
+ delete players_list; |
+ } else { |
+ AnimationPlayer* player_left = players_list->head()->value(); |
+ player_left->RefreshObservers(); |
+ } |
+ return; |
+ } |
+ } |
+} |
+ |
+void AnimationHost::SetLayerTreeMutatorsClient( |
+ LayerTreeMutatorsClient* client) { |
+ if (layer_tree_mutators_client_ == client) |
+ return; |
+ |
+ layer_tree_mutators_client_ = client; |
+} |
+ |
+void AnimationHost::SetNeedsCommit() { |
+ DCHECK(layer_tree_mutators_client_); |
+ layer_tree_mutators_client_->SetMutatorsNeedCommit(); |
+} |
+ |
+void AnimationHost::PushPropertiesTo(AnimationHost* host_impl) { |
+ PushTimelinesToImplThread(host_impl); |
+ RemoveTimelinesFromImplThread(host_impl); |
+ PushPropertiesToImplThread(host_impl); |
+} |
+ |
+void AnimationHost::PushTimelinesToImplThread(AnimationHost* host_impl) const { |
+ for (auto& timeline : timelines_) { |
+ AnimationTimeline* timeline_impl = |
+ host_impl->GetTimelineById(timeline->id()); |
+ if (timeline_impl) |
+ continue; |
+ |
+ scoped_refptr<AnimationTimeline> to_add = timeline->CreateImplInstance(); |
+ host_impl->AddAnimationTimeline(to_add.get()); |
+ } |
+} |
+ |
+void AnimationHost::RemoveTimelinesFromImplThread( |
+ AnimationHost* host_impl) const { |
+ AnimationTimelineList& timelines_impl = host_impl->timelines_; |
+ |
+ auto to_erase = |
+ std::partition(timelines_impl.begin(), timelines_impl.end(), |
+ [this](AnimationTimelineList::value_type timeline_impl) { |
+ return timeline_impl->is_impl_only() || |
+ GetTimelineById(timeline_impl->id()); |
+ }); |
+ |
+ host_impl->EraseTimelines(to_erase, timelines_impl.end()); |
+} |
+ |
+void AnimationHost::PushPropertiesToImplThread(AnimationHost* host_impl) { |
+ for (auto& timeline : timelines_) { |
+ AnimationTimeline* timeline_impl = |
+ host_impl->GetTimelineById(timeline->id()); |
+ if (timeline_impl) |
+ timeline->PushPropertiesTo(timeline_impl); |
+ } |
+} |
+ |
+LayerAnimationController* AnimationHost::GetControllerForLayerId( |
+ int layer_id) const { |
+ // TODO(loyso): Get rid of LayerAnimationController. |
+ const PlayersList* players_list = GetPlayersForLayerId(layer_id); |
+ if (!players_list) |
+ return nullptr; |
+ |
+ DCHECK(EnsureListPlayersShareController(*players_list)); |
+ DCHECK(!players_list->empty()); |
+ AnimationPlayer* player = players_list->head()->value(); |
+ return player->layer_animation_controller(); |
+} |
+ |
+bool AnimationHost::EnsureListPlayersShareController( |
+ const PlayersList& players_list) const { |
+ AnimationPlayer* prev_player = nullptr; |
+ for (base::LinkNode<AnimationPlayer>* node = players_list.head(); |
+ node != players_list.end(); node = node->next()) { |
+ AnimationPlayer* player = node->value(); |
+ if (prev_player && |
+ prev_player->layer_animation_controller() != |
+ player->layer_animation_controller()) |
+ return false; |
+ prev_player = player; |
+ } |
+ |
+ return true; |
+} |
+ |
+AnimationHost::PlayersList* AnimationHost::GetPlayersForLayerId( |
+ int layer_id) const { |
+ DCHECK(layer_id); |
+ auto iter = layer_to_players_map_.find(layer_id); |
+ return iter == layer_to_players_map_.end() ? nullptr : iter->second; |
+} |
+ |
+} // namespace cc |