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_host.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "cc/animation/animation_player.h" |
| 10 #include "cc/animation/animation_registrar.h" |
| 11 #include "cc/animation/animation_timeline.h" |
| 12 |
| 13 namespace cc { |
| 14 |
| 15 scoped_ptr<AnimationHost> AnimationHost::Create( |
| 16 ThreadInstance thread_instance) { |
| 17 return make_scoped_ptr(new AnimationHost(thread_instance)); |
| 18 } |
| 19 |
| 20 AnimationHost::AnimationHost(ThreadInstance thread_instance) |
| 21 : animation_registrar_(AnimationRegistrar::Create()), |
| 22 layer_tree_mutators_client_(), |
| 23 thread_instance_(thread_instance) { |
| 24 } |
| 25 |
| 26 AnimationHost::~AnimationHost() { |
| 27 ClearTimelines(); |
| 28 DCHECK(!layer_tree_mutators_client()); |
| 29 DCHECK(layer_to_players_map_.empty()); |
| 30 } |
| 31 |
| 32 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { |
| 33 for (auto& timeline : timelines_) |
| 34 if (timeline->id() == timeline_id) |
| 35 return timeline.get(); |
| 36 return nullptr; |
| 37 } |
| 38 |
| 39 void AnimationHost::ClearTimelines() { |
| 40 EraseTimelines(timelines_.begin(), timelines_.end()); |
| 41 } |
| 42 |
| 43 void AnimationHost::EraseTimelines(AnimationTimelineList::iterator begin, |
| 44 AnimationTimelineList::iterator end) { |
| 45 for (auto i = begin; i != end; ++i) { |
| 46 auto& timeline = *i; |
| 47 timeline->ClearPlayers(); |
| 48 timeline->SetAnimationHost(nullptr); |
| 49 } |
| 50 |
| 51 timelines_.erase(begin, end); |
| 52 } |
| 53 |
| 54 void AnimationHost::AddAnimationTimeline( |
| 55 scoped_refptr<AnimationTimeline> timeline) { |
| 56 timeline->SetAnimationHost(this); |
| 57 timelines_.push_back(timeline); |
| 58 } |
| 59 |
| 60 void AnimationHost::RemoveAnimationTimeline( |
| 61 scoped_refptr<AnimationTimeline> timeline) { |
| 62 for (auto iter = timelines_.begin(); iter != timelines_.end(); ++iter) { |
| 63 if (iter->get() != timeline) |
| 64 continue; |
| 65 |
| 66 EraseTimelines(iter, iter + 1); |
| 67 break; |
| 68 } |
| 69 } |
| 70 |
| 71 void AnimationHost::RegisterLayer(int layer_id, LayerTreeType tree_type) { |
| 72 const PlayersList* players_list = GetPlayersForLayerId(layer_id); |
| 73 if (!players_list) |
| 74 return; |
| 75 |
| 76 for (base::LinkNode<AnimationPlayer>* node = players_list->head(); |
| 77 node != players_list->end(); node = node->next()) { |
| 78 AnimationPlayer* player = node->value(); |
| 79 player->LayerRegistered(layer_id, tree_type); |
| 80 } |
| 81 } |
| 82 |
| 83 void AnimationHost::UnregisterLayer(int layer_id, LayerTreeType tree_type) { |
| 84 const PlayersList* players_list = GetPlayersForLayerId(layer_id); |
| 85 if (!players_list) |
| 86 return; |
| 87 |
| 88 for (base::LinkNode<AnimationPlayer>* node = players_list->head(); |
| 89 node != players_list->end(); node = node->next()) { |
| 90 AnimationPlayer* player = node->value(); |
| 91 player->LayerUnregistered(layer_id, tree_type); |
| 92 } |
| 93 } |
| 94 |
| 95 void AnimationHost::RegisterPlayerForLayer(int layer_id, |
| 96 AnimationPlayer* player) { |
| 97 DCHECK(layer_id); |
| 98 DCHECK(player); |
| 99 |
| 100 PlayersList* players_list = GetPlayersForLayerId(layer_id); |
| 101 if (!players_list) { |
| 102 // TODO(loyso): Make base::LinkedList movable to avoid this heap allocation. |
| 103 // Depends on C++11 library which is banned for now. crbug.com/360096 |
| 104 players_list = new PlayersList(); |
| 105 layer_to_players_map_.insert(std::make_pair(layer_id, players_list)); |
| 106 } |
| 107 |
| 108 DCHECK(players_list); |
| 109 players_list->Append(player); |
| 110 } |
| 111 |
| 112 void AnimationHost::UnregisterPlayerForLayer(int layer_id, |
| 113 AnimationPlayer* player) { |
| 114 DCHECK(layer_id); |
| 115 DCHECK(player); |
| 116 PlayersList* players_list = GetPlayersForLayerId(layer_id); |
| 117 DCHECK(players_list); |
| 118 |
| 119 for (base::LinkNode<AnimationPlayer>* node = players_list->head(); |
| 120 node != players_list->end(); node = node->next()) { |
| 121 AnimationPlayer* value = node->value(); |
| 122 if (value == player) { |
| 123 node->RemoveFromList(); |
| 124 if (players_list->empty()) { |
| 125 layer_to_players_map_.erase(layer_id); |
| 126 delete players_list; |
| 127 } else { |
| 128 AnimationPlayer* player_left = players_list->head()->value(); |
| 129 player_left->RefreshObservers(); |
| 130 } |
| 131 return; |
| 132 } |
| 133 } |
| 134 } |
| 135 |
| 136 void AnimationHost::SetLayerTreeMutatorsClient( |
| 137 LayerTreeMutatorsClient* client) { |
| 138 if (layer_tree_mutators_client_ == client) |
| 139 return; |
| 140 |
| 141 layer_tree_mutators_client_ = client; |
| 142 } |
| 143 |
| 144 void AnimationHost::SetNeedsCommit() { |
| 145 DCHECK(layer_tree_mutators_client_); |
| 146 layer_tree_mutators_client_->SetMutatorsNeedCommit(); |
| 147 } |
| 148 |
| 149 void AnimationHost::PushPropertiesTo(AnimationHost* host_impl) { |
| 150 PushTimelinesToImplThread(host_impl); |
| 151 RemoveTimelinesFromImplThread(host_impl); |
| 152 PushPropertiesToImplThread(host_impl); |
| 153 } |
| 154 |
| 155 void AnimationHost::PushTimelinesToImplThread(AnimationHost* host_impl) const { |
| 156 for (auto& timeline : timelines_) { |
| 157 AnimationTimeline* timeline_impl = |
| 158 host_impl->GetTimelineById(timeline->id()); |
| 159 if (timeline_impl) |
| 160 continue; |
| 161 |
| 162 scoped_refptr<AnimationTimeline> to_add = timeline->CreateImplInstance(); |
| 163 host_impl->AddAnimationTimeline(to_add.get()); |
| 164 } |
| 165 } |
| 166 |
| 167 void AnimationHost::RemoveTimelinesFromImplThread( |
| 168 AnimationHost* host_impl) const { |
| 169 AnimationTimelineList& timelines_impl = host_impl->timelines_; |
| 170 |
| 171 auto to_erase = |
| 172 std::partition(timelines_impl.begin(), timelines_impl.end(), |
| 173 [this](AnimationTimelineList::value_type timeline_impl) { |
| 174 return timeline_impl->is_impl_only() || |
| 175 GetTimelineById(timeline_impl->id()); |
| 176 }); |
| 177 |
| 178 host_impl->EraseTimelines(to_erase, timelines_impl.end()); |
| 179 } |
| 180 |
| 181 void AnimationHost::PushPropertiesToImplThread(AnimationHost* host_impl) { |
| 182 for (auto& timeline : timelines_) { |
| 183 AnimationTimeline* timeline_impl = |
| 184 host_impl->GetTimelineById(timeline->id()); |
| 185 if (timeline_impl) |
| 186 timeline->PushPropertiesTo(timeline_impl); |
| 187 } |
| 188 } |
| 189 |
| 190 LayerAnimationController* AnimationHost::GetControllerForLayerId( |
| 191 int layer_id) const { |
| 192 // TODO(loyso): Get rid of LayerAnimationController. |
| 193 const PlayersList* players_list = GetPlayersForLayerId(layer_id); |
| 194 if (!players_list) |
| 195 return nullptr; |
| 196 |
| 197 DCHECK(EnsureListPlayersShareController(*players_list)); |
| 198 DCHECK(!players_list->empty()); |
| 199 AnimationPlayer* player = players_list->head()->value(); |
| 200 return player->layer_animation_controller(); |
| 201 } |
| 202 |
| 203 bool AnimationHost::EnsureListPlayersShareController( |
| 204 const PlayersList& players_list) const { |
| 205 AnimationPlayer* prev_player = nullptr; |
| 206 for (base::LinkNode<AnimationPlayer>* node = players_list.head(); |
| 207 node != players_list.end(); node = node->next()) { |
| 208 AnimationPlayer* player = node->value(); |
| 209 if (prev_player && |
| 210 prev_player->layer_animation_controller() != |
| 211 player->layer_animation_controller()) |
| 212 return false; |
| 213 prev_player = player; |
| 214 } |
| 215 |
| 216 return true; |
| 217 } |
| 218 |
| 219 AnimationHost::PlayersList* AnimationHost::GetPlayersForLayerId( |
| 220 int layer_id) const { |
| 221 DCHECK(layer_id); |
| 222 auto iter = layer_to_players_map_.find(layer_id); |
| 223 return iter == layer_to_players_map_.end() ? nullptr : iter->second; |
| 224 } |
| 225 |
| 226 } // namespace cc |
OLD | NEW |