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_timeline.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "cc/animation/animation_player.h" |
| 10 |
| 11 namespace cc { |
| 12 |
| 13 scoped_refptr<AnimationTimeline> AnimationTimeline::Create() { |
| 14 return make_scoped_refptr(new AnimationTimeline()); |
| 15 } |
| 16 |
| 17 AnimationTimeline::AnimationTimeline() : layer_tree_mutators_client_() { |
| 18 } |
| 19 |
| 20 AnimationTimeline::~AnimationTimeline() { |
| 21 DCHECK(!layer_tree_mutators_client()); |
| 22 for (auto& player : players_) |
| 23 player->SetAnimationTimeline(nullptr); |
| 24 } |
| 25 |
| 26 void AnimationTimeline::AttachPlayer(AnimationPlayer* player) { |
| 27 player->SetAnimationTimeline(this); |
| 28 players_.push_back(player); |
| 29 } |
| 30 |
| 31 void AnimationTimeline::DetachPlayer(AnimationPlayer* player) { |
| 32 for (AnimationPlayerList::iterator iter = players_.begin(); |
| 33 iter != players_.end(); ++iter) { |
| 34 if (iter->get() != player) |
| 35 continue; |
| 36 |
| 37 players_.erase(iter); |
| 38 player->SetAnimationTimeline(nullptr); |
| 39 break; |
| 40 } |
| 41 } |
| 42 |
| 43 AnimationPlayer* AnimationTimeline::GetPlayerById(int player_id) const { |
| 44 for (auto& player : players_) |
| 45 if (player->id() == player_id) |
| 46 return player.get(); |
| 47 return nullptr; |
| 48 } |
| 49 |
| 50 void AnimationTimeline::RegisterLayerImpl(int layer_id, bool is_active_tree) { |
| 51 AnimationPlayer* player = GetPlayerForLayerId(layer_id); |
| 52 if (player) |
| 53 player->LayerImplRegistered(layer_id, is_active_tree); |
| 54 } |
| 55 |
| 56 void AnimationTimeline::UnregisterLayerImpl(int layer_id, bool is_active_tree) { |
| 57 AnimationPlayer* player = GetPlayerForLayerId(layer_id); |
| 58 if (player) |
| 59 player->LayerImplUnregistered(layer_id, is_active_tree); |
| 60 } |
| 61 |
| 62 void AnimationTimeline::RegisterPlayerForLayer(int layer_id, |
| 63 AnimationPlayer* player) { |
| 64 DCHECK(layer_id); |
| 65 DCHECK(player); |
| 66 layer_to_player_map_[layer_id] = player; |
| 67 } |
| 68 |
| 69 void AnimationTimeline::UnregisterPlayerForLayer(int layer_id, |
| 70 AnimationPlayer* player) { |
| 71 DCHECK(layer_id); |
| 72 DCHECK(player); |
| 73 auto iter = layer_to_player_map_.find(layer_id); |
| 74 if (iter != layer_to_player_map_.end() && iter->second == player) |
| 75 layer_to_player_map_.erase(iter); |
| 76 } |
| 77 |
| 78 void AnimationTimeline::SetLayerTreeMutatorsClient( |
| 79 LayerTreeMutatorsClient* client) { |
| 80 if (layer_tree_mutators_client_ == client) |
| 81 return; |
| 82 |
| 83 layer_tree_mutators_client_ = client; |
| 84 } |
| 85 |
| 86 void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) { |
| 87 PushAttachedPlayersToImplThread(timeline_impl); |
| 88 RemoveDetachedPlayersFromImplThread(timeline_impl); |
| 89 PushPropertiesToImplThread(timeline_impl); |
| 90 } |
| 91 |
| 92 void AnimationTimeline::PushAttachedPlayersToImplThread( |
| 93 AnimationTimeline* timeline_impl) const { |
| 94 for (auto& player : players_) { |
| 95 AnimationPlayer* player_impl = timeline_impl->GetPlayerById(player->id()); |
| 96 if (player_impl) |
| 97 continue; |
| 98 |
| 99 scoped_refptr<AnimationPlayer> to_add = player->CreateImplInstance(); |
| 100 timeline_impl->AttachPlayer(to_add.get()); |
| 101 } |
| 102 } |
| 103 |
| 104 void AnimationTimeline::RemoveDetachedPlayersFromImplThread( |
| 105 AnimationTimeline* timeline_impl) const { |
| 106 AnimationPlayerList& players_impl = timeline_impl->players_; |
| 107 |
| 108 auto to_erase = |
| 109 std::partition(players_impl.begin(), players_impl.end(), |
| 110 [this](AnimationPlayerList::value_type player_impl) { |
| 111 return GetPlayerById(player_impl->id()); |
| 112 }); |
| 113 |
| 114 for (auto i = to_erase; i != players_impl.end(); ++i) { |
| 115 auto& player_impl = *i; |
| 116 if (player_impl->layer_animation_controller()) |
| 117 player_impl->DetachLayer(); |
| 118 player_impl->SetAnimationTimeline(nullptr); |
| 119 } |
| 120 |
| 121 players_impl.erase(to_erase, players_impl.end()); |
| 122 } |
| 123 |
| 124 void AnimationTimeline::PushPropertiesToImplThread( |
| 125 AnimationTimeline* timeline_impl) { |
| 126 for (auto& player : players_) { |
| 127 AnimationPlayer* player_impl = timeline_impl->GetPlayerById(player->id()); |
| 128 if (player_impl) |
| 129 player->PushPropertiesTo(player_impl); |
| 130 } |
| 131 } |
| 132 |
| 133 AnimationPlayer* AnimationTimeline::GetPlayerForLayerId(int layer_id) const { |
| 134 DCHECK(layer_id); |
| 135 auto iter = layer_to_player_map_.find(layer_id); |
| 136 return iter == layer_to_player_map_.end() ? nullptr : iter->second; |
| 137 } |
| 138 |
| 139 } // namespace cc |
OLD | NEW |