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/animation/element_animations.h" |
| 13 |
| 14 namespace cc { |
| 15 |
| 16 scoped_ptr<AnimationHost> AnimationHost::Create( |
| 17 ThreadInstance thread_instance) { |
| 18 return make_scoped_ptr(new AnimationHost(thread_instance)); |
| 19 } |
| 20 |
| 21 AnimationHost::AnimationHost(ThreadInstance thread_instance) |
| 22 : animation_registrar_(AnimationRegistrar::Create()), |
| 23 mutator_host_client_(), |
| 24 thread_instance_(thread_instance) { |
| 25 } |
| 26 |
| 27 AnimationHost::~AnimationHost() { |
| 28 ClearTimelines(); |
| 29 DCHECK(!mutator_host_client()); |
| 30 DCHECK(layer_to_element_animations_map_.empty()); |
| 31 } |
| 32 |
| 33 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { |
| 34 for (auto& timeline : timelines_) |
| 35 if (timeline->id() == timeline_id) |
| 36 return timeline.get(); |
| 37 return nullptr; |
| 38 } |
| 39 |
| 40 void AnimationHost::ClearTimelines() { |
| 41 EraseTimelines(timelines_.begin(), timelines_.end()); |
| 42 } |
| 43 |
| 44 void AnimationHost::EraseTimelines(AnimationTimelineList::iterator begin, |
| 45 AnimationTimelineList::iterator end) { |
| 46 for (auto i = begin; i != end; ++i) { |
| 47 auto& timeline = *i; |
| 48 timeline->ClearPlayers(); |
| 49 timeline->SetAnimationHost(nullptr); |
| 50 } |
| 51 |
| 52 timelines_.erase(begin, end); |
| 53 } |
| 54 |
| 55 void AnimationHost::AddAnimationTimeline( |
| 56 scoped_refptr<AnimationTimeline> timeline) { |
| 57 timeline->SetAnimationHost(this); |
| 58 timelines_.push_back(timeline); |
| 59 } |
| 60 |
| 61 void AnimationHost::RemoveAnimationTimeline( |
| 62 scoped_refptr<AnimationTimeline> timeline) { |
| 63 for (auto iter = timelines_.begin(); iter != timelines_.end(); ++iter) { |
| 64 if (iter->get() != timeline) |
| 65 continue; |
| 66 |
| 67 EraseTimelines(iter, iter + 1); |
| 68 break; |
| 69 } |
| 70 } |
| 71 |
| 72 void AnimationHost::RegisterLayer(int layer_id, LayerTreeType tree_type) { |
| 73 ElementAnimations* element_animations = |
| 74 GetElementAnimationsForLayerId(layer_id); |
| 75 if (element_animations) |
| 76 element_animations->LayerRegistered(layer_id, tree_type); |
| 77 } |
| 78 |
| 79 void AnimationHost::UnregisterLayer(int layer_id, LayerTreeType tree_type) { |
| 80 ElementAnimations* element_animations = |
| 81 GetElementAnimationsForLayerId(layer_id); |
| 82 if (element_animations) |
| 83 element_animations->LayerUnregistered(layer_id, tree_type); |
| 84 } |
| 85 |
| 86 void AnimationHost::RegisterPlayerForLayer(int layer_id, |
| 87 AnimationPlayer* player) { |
| 88 DCHECK(layer_id); |
| 89 DCHECK(player); |
| 90 |
| 91 ElementAnimations* element_animations = |
| 92 GetElementAnimationsForLayerId(layer_id); |
| 93 if (!element_animations) { |
| 94 auto new_element_animations = ElementAnimations::Create(this); |
| 95 element_animations = new_element_animations.get(); |
| 96 |
| 97 layer_to_element_animations_map_.add(layer_id, |
| 98 new_element_animations.Pass()); |
| 99 element_animations->CreateLayerAnimationController(layer_id); |
| 100 } |
| 101 |
| 102 DCHECK(element_animations); |
| 103 element_animations->AddPlayer(player); |
| 104 } |
| 105 |
| 106 void AnimationHost::UnregisterPlayerForLayer(int layer_id, |
| 107 AnimationPlayer* player) { |
| 108 DCHECK(layer_id); |
| 109 DCHECK(player); |
| 110 |
| 111 ElementAnimations* element_animations = |
| 112 GetElementAnimationsForLayerId(layer_id); |
| 113 DCHECK(element_animations); |
| 114 element_animations->RemovePlayer(player); |
| 115 |
| 116 if (element_animations->IsEmpty()) { |
| 117 element_animations->DestroyLayerAnimationController(); |
| 118 layer_to_element_animations_map_.erase(layer_id); |
| 119 element_animations = nullptr; |
| 120 } |
| 121 } |
| 122 |
| 123 void AnimationHost::SetMutatorHostClient(MutatorHostClient* client) { |
| 124 if (mutator_host_client_ == client) |
| 125 return; |
| 126 |
| 127 mutator_host_client_ = client; |
| 128 } |
| 129 |
| 130 void AnimationHost::SetNeedsCommit() { |
| 131 DCHECK(mutator_host_client_); |
| 132 mutator_host_client_->SetMutatorsNeedCommit(); |
| 133 } |
| 134 |
| 135 void AnimationHost::PushPropertiesTo(AnimationHost* host_impl) { |
| 136 PushTimelinesToImplThread(host_impl); |
| 137 RemoveTimelinesFromImplThread(host_impl); |
| 138 PushPropertiesToImplThread(host_impl); |
| 139 } |
| 140 |
| 141 void AnimationHost::PushTimelinesToImplThread(AnimationHost* host_impl) const { |
| 142 for (auto& timeline : timelines_) { |
| 143 AnimationTimeline* timeline_impl = |
| 144 host_impl->GetTimelineById(timeline->id()); |
| 145 if (timeline_impl) |
| 146 continue; |
| 147 |
| 148 scoped_refptr<AnimationTimeline> to_add = timeline->CreateImplInstance(); |
| 149 host_impl->AddAnimationTimeline(to_add.get()); |
| 150 } |
| 151 } |
| 152 |
| 153 void AnimationHost::RemoveTimelinesFromImplThread( |
| 154 AnimationHost* host_impl) const { |
| 155 AnimationTimelineList& timelines_impl = host_impl->timelines_; |
| 156 |
| 157 auto to_erase = |
| 158 std::partition(timelines_impl.begin(), timelines_impl.end(), |
| 159 [this](AnimationTimelineList::value_type timeline_impl) { |
| 160 return timeline_impl->is_impl_only() || |
| 161 GetTimelineById(timeline_impl->id()); |
| 162 }); |
| 163 |
| 164 host_impl->EraseTimelines(to_erase, timelines_impl.end()); |
| 165 } |
| 166 |
| 167 void AnimationHost::PushPropertiesToImplThread(AnimationHost* host_impl) { |
| 168 // Firstly, sync all players with impl thread to create ElementAnimations and |
| 169 // layer animation controllers. |
| 170 for (auto& timeline : timelines_) { |
| 171 AnimationTimeline* timeline_impl = |
| 172 host_impl->GetTimelineById(timeline->id()); |
| 173 if (timeline_impl) |
| 174 timeline->PushPropertiesTo(timeline_impl); |
| 175 } |
| 176 |
| 177 // Secondly, sync properties for created layer animation controllers. |
| 178 for (auto& kv : layer_to_element_animations_map_) { |
| 179 ElementAnimations* element_animations = kv.second; |
| 180 ElementAnimations* element_animations_impl = |
| 181 host_impl->GetElementAnimationsForLayerId(kv.first); |
| 182 if (element_animations_impl) |
| 183 element_animations->PushPropertiesTo(element_animations_impl); |
| 184 } |
| 185 } |
| 186 |
| 187 LayerAnimationController* AnimationHost::GetControllerForLayerId( |
| 188 int layer_id) const { |
| 189 const ElementAnimations* element_animations = |
| 190 GetElementAnimationsForLayerId(layer_id); |
| 191 if (!element_animations) |
| 192 return nullptr; |
| 193 |
| 194 return element_animations->layer_animation_controller(); |
| 195 } |
| 196 |
| 197 ElementAnimations* AnimationHost::GetElementAnimationsForLayerId( |
| 198 int layer_id) const { |
| 199 DCHECK(layer_id); |
| 200 auto iter = layer_to_element_animations_map_.find(layer_id); |
| 201 return iter == layer_to_element_animations_map_.end() ? nullptr |
| 202 : iter->second; |
| 203 } |
| 204 |
| 205 } // namespace cc |
OLD | NEW |