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(int id) { |
| 14 return make_scoped_refptr(new AnimationTimeline(id)); |
| 15 } |
| 16 |
| 17 AnimationTimeline::AnimationTimeline(int id) |
| 18 : id_(id), animation_host_(), is_impl_only_(false) { |
| 19 } |
| 20 |
| 21 AnimationTimeline::~AnimationTimeline() { |
| 22 for (auto& player : players_) |
| 23 player->SetAnimationTimeline(nullptr); |
| 24 } |
| 25 |
| 26 scoped_refptr<AnimationTimeline> AnimationTimeline::CreateImplInstance() const { |
| 27 scoped_refptr<AnimationTimeline> to_return = AnimationTimeline::Create(id()); |
| 28 return to_return; |
| 29 } |
| 30 |
| 31 void AnimationTimeline::SetAnimationHost(AnimationHost* animation_host) { |
| 32 animation_host_ = animation_host; |
| 33 for (auto& player : players_) |
| 34 player->SetAnimationHost(animation_host); |
| 35 } |
| 36 |
| 37 void AnimationTimeline::AttachPlayer(AnimationPlayer* player) { |
| 38 DCHECK(animation_host_); |
| 39 player->SetAnimationHost(animation_host_); |
| 40 player->SetAnimationTimeline(this); |
| 41 players_.push_back(player); |
| 42 } |
| 43 |
| 44 void AnimationTimeline::DetachPlayer(AnimationPlayer* player) { |
| 45 for (AnimationPlayerList::iterator iter = players_.begin(); |
| 46 iter != players_.end(); ++iter) { |
| 47 if (iter->get() != player) |
| 48 continue; |
| 49 |
| 50 ErasePlayers(iter, iter + 1); |
| 51 break; |
| 52 } |
| 53 |
| 54 player->SetAnimationHost(nullptr); |
| 55 } |
| 56 |
| 57 AnimationPlayer* AnimationTimeline::GetPlayerById(int player_id) const { |
| 58 for (auto& player : players_) |
| 59 if (player->id() == player_id) |
| 60 return player.get(); |
| 61 return nullptr; |
| 62 } |
| 63 |
| 64 void AnimationTimeline::ClearPlayers() { |
| 65 ErasePlayers(players_.begin(), players_.end()); |
| 66 } |
| 67 |
| 68 void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) { |
| 69 PushAttachedPlayersToImplThread(timeline_impl); |
| 70 RemoveDetachedPlayersFromImplThread(timeline_impl); |
| 71 PushPropertiesToImplThread(timeline_impl); |
| 72 } |
| 73 |
| 74 void AnimationTimeline::PushAttachedPlayersToImplThread( |
| 75 AnimationTimeline* timeline_impl) const { |
| 76 for (auto& player : players_) { |
| 77 AnimationPlayer* player_impl = timeline_impl->GetPlayerById(player->id()); |
| 78 if (player_impl) |
| 79 continue; |
| 80 |
| 81 scoped_refptr<AnimationPlayer> to_add = player->CreateImplInstance(); |
| 82 timeline_impl->AttachPlayer(to_add.get()); |
| 83 } |
| 84 } |
| 85 |
| 86 void AnimationTimeline::RemoveDetachedPlayersFromImplThread( |
| 87 AnimationTimeline* timeline_impl) const { |
| 88 AnimationPlayerList& players_impl = timeline_impl->players_; |
| 89 |
| 90 auto to_erase = |
| 91 std::partition(players_impl.begin(), players_impl.end(), |
| 92 [this](AnimationPlayerList::value_type player_impl) { |
| 93 return GetPlayerById(player_impl->id()); |
| 94 }); |
| 95 |
| 96 timeline_impl->ErasePlayers(to_erase, players_impl.end()); |
| 97 } |
| 98 |
| 99 void AnimationTimeline::ErasePlayers(AnimationPlayerList::iterator begin, |
| 100 AnimationPlayerList::iterator end) { |
| 101 for (auto i = begin; i != end; ++i) { |
| 102 auto& player = *i; |
| 103 if (player->layer_animation_controller()) |
| 104 player->DetachLayer(); |
| 105 player->SetAnimationTimeline(nullptr); |
| 106 } |
| 107 |
| 108 players_.erase(begin, end); |
| 109 } |
| 110 |
| 111 void AnimationTimeline::PushPropertiesToImplThread( |
| 112 AnimationTimeline* timeline_impl) { |
| 113 for (auto& player : players_) { |
| 114 AnimationPlayer* player_impl = timeline_impl->GetPlayerById(player->id()); |
| 115 if (player_impl) |
| 116 player->PushPropertiesTo(player_impl); |
| 117 } |
| 118 } |
| 119 |
| 120 } // namespace cc |
OLD | NEW |