loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 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 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 9 | #include "cc/animation/animation.h" |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 10 | #include "cc/animation/animation_host.h" |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 11 | |
| 12 | namespace cc { |
| 13 | |
| 14 | scoped_refptr<AnimationTimeline> AnimationTimeline::Create(int id) { |
kylechar | 973a041 | 2017-09-26 18:40:29 | [diff] [blame] | 15 | return base::WrapRefCounted(new AnimationTimeline(id)); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | AnimationTimeline::AnimationTimeline(int id) |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 19 | : id_(id), |
| 20 | animation_host_(), |
| 21 | needs_push_properties_(false), |
| 22 | is_impl_only_(false) {} |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 23 | |
| 24 | AnimationTimeline::~AnimationTimeline() { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 25 | for (auto& kv : id_to_animation_map_) |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 26 | kv.second->SetAnimationTimeline(nullptr); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | scoped_refptr<AnimationTimeline> AnimationTimeline::CreateImplInstance() const { |
| 30 | scoped_refptr<AnimationTimeline> timeline = AnimationTimeline::Create(id()); |
| 31 | return timeline; |
| 32 | } |
| 33 | |
| 34 | void AnimationTimeline::SetAnimationHost(AnimationHost* animation_host) { |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 35 | if (animation_host_ == animation_host) |
| 36 | return; |
| 37 | |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 38 | animation_host_ = animation_host; |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 39 | for (auto& kv : id_to_animation_map_) |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 40 | kv.second->SetAnimationHost(animation_host); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 41 | |
| 42 | SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 43 | } |
| 44 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 45 | void AnimationTimeline::AttachAnimation(scoped_refptr<Animation> animation) { |
| 46 | DCHECK(animation->id()); |
| 47 | animation->SetAnimationHost(animation_host_); |
| 48 | animation->SetAnimationTimeline(this); |
| 49 | id_to_animation_map_.insert( |
| 50 | std::make_pair(animation->id(), std::move(animation))); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 51 | |
| 52 | SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 53 | } |
| 54 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 55 | void AnimationTimeline::DetachAnimation(scoped_refptr<Animation> animation) { |
| 56 | DCHECK(animation->id()); |
| 57 | EraseAnimation(animation); |
| 58 | id_to_animation_map_.erase(animation->id()); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 59 | |
| 60 | SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 61 | } |
| 62 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 63 | Animation* AnimationTimeline::GetAnimationById(int animation_id) const { |
| 64 | auto f = id_to_animation_map_.find(animation_id); |
| 65 | return f == id_to_animation_map_.end() ? nullptr : f->second.get(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 66 | } |
| 67 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 68 | void AnimationTimeline::ClearAnimations() { |
| 69 | for (auto& kv : id_to_animation_map_) |
| 70 | EraseAnimation(kv.second); |
| 71 | id_to_animation_map_.clear(); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 72 | |
| 73 | SetNeedsPushProperties(); |
| 74 | } |
| 75 | |
| 76 | void AnimationTimeline::SetNeedsPushProperties() { |
| 77 | needs_push_properties_ = true; |
| 78 | if (animation_host_) |
| 79 | animation_host_->SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) { |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 83 | if (needs_push_properties_) { |
| 84 | needs_push_properties_ = false; |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 85 | PushAttachedAnimationsToImplThread(timeline_impl); |
| 86 | RemoveDetachedAnimationsFromImplThread(timeline_impl); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 87 | PushPropertiesToImplThread(timeline_impl); |
| 88 | } |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 89 | } |
| 90 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 91 | void AnimationTimeline::PushAttachedAnimationsToImplThread( |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 92 | AnimationTimeline* timeline_impl) const { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 93 | for (auto& kv : id_to_animation_map_) { |
| 94 | auto& animation = kv.second; |
| 95 | Animation* animation_impl = |
| 96 | timeline_impl->GetAnimationById(animation->id()); |
| 97 | if (animation_impl) |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 98 | continue; |
| 99 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 100 | scoped_refptr<Animation> to_add = animation->CreateImplInstance(); |
| 101 | timeline_impl->AttachAnimation(to_add.get()); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 105 | void AnimationTimeline::RemoveDetachedAnimationsFromImplThread( |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 106 | AnimationTimeline* timeline_impl) const { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 107 | IdToAnimationMap& animations_impl = timeline_impl->id_to_animation_map_; |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 108 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 109 | // Erase all the impl animations which |this| doesn't have. |
| 110 | for (auto it = animations_impl.begin(); it != animations_impl.end();) { |
| 111 | if (GetAnimationById(it->second->id())) { |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 112 | ++it; |
| 113 | } else { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 114 | timeline_impl->EraseAnimation(it->second); |
| 115 | it = animations_impl.erase(it); |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 116 | } |
| 117 | } |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 118 | } |
| 119 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 120 | void AnimationTimeline::EraseAnimation(scoped_refptr<Animation> animation) { |
| 121 | if (animation->has_element_animations()) |
| 122 | animation->DetachElement(); |
| 123 | animation->SetAnimationTimeline(nullptr); |
| 124 | animation->SetAnimationHost(nullptr); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void AnimationTimeline::PushPropertiesToImplThread( |
| 128 | AnimationTimeline* timeline_impl) { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame^] | 129 | for (auto& kv : id_to_animation_map_) { |
| 130 | Animation* animation = kv.second.get(); |
| 131 | if (Animation* animation_impl = |
| 132 | timeline_impl->GetAnimationById(animation->id())) { |
| 133 | animation->PushPropertiesTo(animation_impl); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 134 | } |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | } // namespace cc |