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 element_animations = new ElementAnimations(this); |
| 95 layer_to_element_animations_map_.insert( |
| 96 std::make_pair(layer_id, element_animations)); |
| 97 element_animations->CreateLayerAnimationController(layer_id); |
| 98 } |
| 99 |
| 100 DCHECK(element_animations); |
| 101 element_animations->AddPlayer(player); |
| 102 } |
| 103 |
| 104 void AnimationHost::UnregisterPlayerForLayer(int layer_id, |
| 105 AnimationPlayer* player) { |
| 106 DCHECK(layer_id); |
| 107 DCHECK(player); |
| 108 |
| 109 ElementAnimations* element_animations = |
| 110 GetElementAnimationsForLayerId(layer_id); |
| 111 DCHECK(element_animations); |
| 112 element_animations->RemovePlayer(player); |
| 113 |
| 114 if (element_animations->IsEmpty()) { |
| 115 element_animations->DestroyLayerAnimationController(); |
| 116 layer_to_element_animations_map_.erase(layer_id); |
| 117 delete element_animations; |
| 118 element_animations = nullptr; |
| 119 } |
| 120 } |
| 121 |
| 122 void AnimationHost::SetMutatorHostClient(MutatorHostClient* client) { |
| 123 if (mutator_host_client_ == client) |
| 124 return; |
| 125 |
| 126 mutator_host_client_ = client; |
| 127 } |
| 128 |
| 129 void AnimationHost::SetNeedsCommit() { |
| 130 DCHECK(mutator_host_client_); |
| 131 mutator_host_client_->SetMutatorsNeedCommit(); |
| 132 } |
| 133 |
| 134 void AnimationHost::PushPropertiesTo(AnimationHost* host_impl) { |
| 135 PushTimelinesToImplThread(host_impl); |
| 136 RemoveTimelinesFromImplThread(host_impl); |
| 137 PushPropertiesToImplThread(host_impl); |
| 138 } |
| 139 |
| 140 void AnimationHost::PushTimelinesToImplThread(AnimationHost* host_impl) const { |
| 141 for (auto& timeline : timelines_) { |
| 142 AnimationTimeline* timeline_impl = |
| 143 host_impl->GetTimelineById(timeline->id()); |
| 144 if (timeline_impl) |
| 145 continue; |
| 146 |
| 147 scoped_refptr<AnimationTimeline> to_add = timeline->CreateImplInstance(); |
| 148 host_impl->AddAnimationTimeline(to_add.get()); |
| 149 } |
| 150 } |
| 151 |
| 152 void AnimationHost::RemoveTimelinesFromImplThread( |
| 153 AnimationHost* host_impl) const { |
| 154 AnimationTimelineList& timelines_impl = host_impl->timelines_; |
| 155 |
| 156 auto to_erase = |
| 157 std::partition(timelines_impl.begin(), timelines_impl.end(), |
| 158 [this](AnimationTimelineList::value_type timeline_impl) { |
| 159 return timeline_impl->is_impl_only() || |
| 160 GetTimelineById(timeline_impl->id()); |
| 161 }); |
| 162 |
| 163 host_impl->EraseTimelines(to_erase, timelines_impl.end()); |
| 164 } |
| 165 |
| 166 void AnimationHost::PushPropertiesToImplThread(AnimationHost* host_impl) { |
| 167 // Firstly, sync all players with impl thread to create ElementAnimations and |
| 168 // layer animation controllers. |
| 169 for (auto& timeline : timelines_) { |
| 170 AnimationTimeline* timeline_impl = |
| 171 host_impl->GetTimelineById(timeline->id()); |
| 172 if (timeline_impl) |
| 173 timeline->PushPropertiesTo(timeline_impl); |
| 174 } |
| 175 |
| 176 // Secondly, sync properties for created layer animation controllers. |
| 177 for (auto& kv : layer_to_element_animations_map_) { |
| 178 ElementAnimations* element_animations = kv.second; |
| 179 ElementAnimations* element_animations_impl = |
| 180 host_impl->GetElementAnimationsForLayerId(kv.first); |
| 181 if (element_animations_impl) |
| 182 element_animations->PushPropertiesTo(element_animations_impl); |
| 183 } |
| 184 } |
| 185 |
| 186 LayerAnimationController* AnimationHost::GetControllerForLayerId( |
| 187 int layer_id) const { |
| 188 const ElementAnimations* element_animations = |
| 189 GetElementAnimationsForLayerId(layer_id); |
| 190 if (!element_animations) |
| 191 return nullptr; |
| 192 |
| 193 return element_animations->layer_animation_controller(); |
| 194 } |
| 195 |
| 196 ElementAnimations* AnimationHost::GetElementAnimationsForLayerId( |
| 197 int layer_id) const { |
| 198 DCHECK(layer_id); |
| 199 auto iter = layer_to_element_animations_map_.find(layer_id); |
| 200 return iter == layer_to_element_animations_map_.end() ? nullptr |
| 201 : iter->second; |
| 202 } |
| 203 |
| 204 } // namespace cc |
OLD | NEW |