Chromium Code Reviews| 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 #include "cc/trees/layer_tree_mutators_client.h" | |
| 13 | |
| 14 namespace cc { | |
| 15 | |
| 16 scoped_ptr<AnimationHost> AnimationHost::Create(bool is_impl_instance) { | |
| 17 return make_scoped_ptr(new AnimationHost(is_impl_instance)); | |
| 18 } | |
| 19 | |
| 20 AnimationHost::AnimationHost(bool is_impl_instance) | |
| 21 : animation_registrar_(AnimationRegistrar::Create()), | |
| 22 layer_tree_mutators_client_(), | |
| 23 is_impl_instance_(is_impl_instance) { | |
| 24 } | |
| 25 | |
| 26 AnimationHost::~AnimationHost() { | |
| 27 ClearTimelines(); | |
| 28 DCHECK(!layer_tree_mutators_client()); | |
| 29 } | |
| 30 | |
| 31 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { | |
| 32 for (auto& timeline : timelines_) | |
| 33 if (timeline->id() == timeline_id) | |
| 34 return timeline.get(); | |
| 35 return nullptr; | |
| 36 } | |
| 37 | |
| 38 void AnimationHost::ClearTimelines() { | |
| 39 EraseTimelines(timelines_.begin(), timelines_.end()); | |
| 40 } | |
| 41 | |
| 42 void AnimationHost::EraseTimelines(AnimationTimelineList::iterator begin, | |
| 43 AnimationTimelineList::iterator end) { | |
| 44 for (auto i = begin; i != end; ++i) { | |
| 45 auto& timeline = *i; | |
| 46 timeline->ClearPlayers(); | |
| 47 timeline->SetAnimationHost(nullptr); | |
| 48 } | |
| 49 | |
| 50 timelines_.erase(begin, end); | |
| 51 } | |
| 52 | |
| 53 void AnimationHost::AddAnimationTimeline(AnimationTimeline* timeline) { | |
| 54 timeline->SetAnimationHost(this); | |
| 55 timelines_.push_back(timeline); | |
| 56 } | |
| 57 | |
| 58 void AnimationHost::RemoveAnimationTimeline(AnimationTimeline* timeline) { | |
| 59 for (auto iter = timelines_.begin(); iter != timelines_.end(); ++iter) { | |
| 60 if (iter->get() != timeline) | |
| 61 continue; | |
| 62 | |
| 63 EraseTimelines(iter, iter + 1); | |
| 64 break; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void AnimationHost::RegisterLayer(int layer_id, bool is_active_tree) { | |
| 69 AnimationPlayer* player = GetPlayerForLayerId(layer_id); | |
| 70 if (player) | |
| 71 player->LayerRegistered(layer_id, is_active_tree); | |
| 72 } | |
| 73 | |
| 74 void AnimationHost::UnregisterLayer(int layer_id, bool is_active_tree) { | |
| 75 AnimationPlayer* player = GetPlayerForLayerId(layer_id); | |
| 76 if (player) | |
| 77 player->LayerUnregistered(layer_id, is_active_tree); | |
| 78 } | |
| 79 | |
| 80 void AnimationHost::RegisterPlayerForLayer(int layer_id, | |
| 81 AnimationPlayer* player) { | |
| 82 DCHECK(layer_id); | |
| 83 DCHECK(player); | |
| 84 layer_to_player_map_[layer_id] = player; | |
| 85 } | |
| 86 | |
| 87 void AnimationHost::UnregisterPlayerForLayer(int layer_id, | |
| 88 AnimationPlayer* player) { | |
| 89 DCHECK(layer_id); | |
| 90 DCHECK(player); | |
| 91 auto iter = layer_to_player_map_.find(layer_id); | |
| 92 if (iter != layer_to_player_map_.end() && iter->second == player) | |
|
Ian Vollick
2015/05/05 03:52:06
when would iter->second != player?
loyso (OOO)
2015/05/05 06:51:18
Good point! We should go with DCHECK here and in R
loyso (OOO)
2015/06/22 07:48:51
Well, in fact this is an intermediate state of thi
Ian Vollick
2015/06/22 13:58:39
That sounds reasonable. Thanks for the details.
| |
| 93 layer_to_player_map_.erase(iter); | |
| 94 } | |
| 95 | |
| 96 void AnimationHost::SetLayerTreeMutatorsClient( | |
| 97 LayerTreeMutatorsClient* client) { | |
| 98 if (layer_tree_mutators_client_ == client) | |
| 99 return; | |
| 100 | |
| 101 layer_tree_mutators_client_ = client; | |
| 102 } | |
| 103 | |
| 104 void AnimationHost::SetNeedsCommit() { | |
| 105 DCHECK(layer_tree_mutators_client_); | |
| 106 layer_tree_mutators_client_->SetMutatorsNeedCommit(); | |
| 107 } | |
| 108 | |
| 109 void AnimationHost::PushPropertiesTo(AnimationHost* host_impl) { | |
| 110 PushTimelinesToImplThread(host_impl); | |
| 111 RemoveTimelinesFromImplThread(host_impl); | |
| 112 PushPropertiesToImplThread(host_impl); | |
| 113 } | |
| 114 | |
| 115 void AnimationHost::PushTimelinesToImplThread(AnimationHost* host_impl) const { | |
| 116 for (auto& timeline : timelines_) { | |
| 117 AnimationTimeline* timeline_impl = | |
| 118 host_impl->GetTimelineById(timeline->id()); | |
| 119 if (timeline_impl) | |
| 120 continue; | |
| 121 | |
| 122 scoped_refptr<AnimationTimeline> to_add = timeline->CreateImplInstance(); | |
| 123 host_impl->AddAnimationTimeline(to_add.get()); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void AnimationHost::RemoveTimelinesFromImplThread( | |
| 128 AnimationHost* host_impl) const { | |
| 129 AnimationTimelineList& timelines_impl = host_impl->timelines_; | |
| 130 | |
| 131 auto to_erase = | |
| 132 std::partition(timelines_impl.begin(), timelines_impl.end(), | |
| 133 [this](AnimationTimelineList::value_type timeline_impl) { | |
| 134 return timeline_impl->is_impl_only() || | |
| 135 GetTimelineById(timeline_impl->id()); | |
| 136 }); | |
| 137 | |
| 138 host_impl->EraseTimelines(to_erase, timelines_impl.end()); | |
| 139 } | |
| 140 | |
| 141 void AnimationHost::PushPropertiesToImplThread(AnimationHost* host_impl) { | |
| 142 for (auto& timeline : timelines_) { | |
| 143 AnimationTimeline* timeline_impl = | |
| 144 host_impl->GetTimelineById(timeline->id()); | |
| 145 if (timeline_impl) | |
| 146 timeline->PushPropertiesTo(timeline_impl); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 AnimationPlayer* AnimationHost::GetPlayerForLayerId(int layer_id) const { | |
| 151 DCHECK(layer_id); | |
| 152 auto iter = layer_to_player_map_.find(layer_id); | |
| 153 return iter == layer_to_player_map_.end() ? nullptr : iter->second; | |
| 154 } | |
| 155 | |
| 156 } // namespace cc | |
| OLD | NEW |