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> |
Peter Kasting | b4ee337 | 2021-07-06 23:02:29 | [diff] [blame^] | 8 | #include <utility> |
| 9 | #include <vector> |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 10 | |
Peter Kasting | b4ee337 | 2021-07-06 23:02:29 | [diff] [blame^] | 11 | #include "base/time/time.h" |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 12 | #include "cc/animation/animation.h" |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 13 | #include "cc/animation/animation_host.h" |
Yi Gu | ad72f3b | 2020-04-28 21:35:59 | [diff] [blame] | 14 | #include "cc/trees/property_tree.h" |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 15 | |
| 16 | namespace cc { |
| 17 | |
| 18 | scoped_refptr<AnimationTimeline> AnimationTimeline::Create(int id) { |
kylechar | 973a041 | 2017-09-26 18:40:29 | [diff] [blame] | 19 | return base::WrapRefCounted(new AnimationTimeline(id)); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | AnimationTimeline::AnimationTimeline(int id) |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 23 | : id_(id), |
| 24 | animation_host_(), |
| 25 | needs_push_properties_(false), |
| 26 | is_impl_only_(false) {} |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 27 | |
| 28 | AnimationTimeline::~AnimationTimeline() { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 29 | for (auto& kv : id_to_animation_map_) |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 30 | kv.second->SetAnimationTimeline(nullptr); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | scoped_refptr<AnimationTimeline> AnimationTimeline::CreateImplInstance() const { |
| 34 | scoped_refptr<AnimationTimeline> timeline = AnimationTimeline::Create(id()); |
| 35 | return timeline; |
| 36 | } |
| 37 | |
| 38 | void AnimationTimeline::SetAnimationHost(AnimationHost* animation_host) { |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 39 | if (animation_host_ == animation_host) |
| 40 | return; |
| 41 | |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 42 | animation_host_ = animation_host; |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 43 | for (auto& kv : id_to_animation_map_) |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 44 | kv.second->SetAnimationHost(animation_host); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 45 | |
| 46 | SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 47 | } |
| 48 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 49 | void AnimationTimeline::AttachAnimation(scoped_refptr<Animation> animation) { |
| 50 | DCHECK(animation->id()); |
| 51 | animation->SetAnimationHost(animation_host_); |
| 52 | animation->SetAnimationTimeline(this); |
| 53 | id_to_animation_map_.insert( |
| 54 | std::make_pair(animation->id(), std::move(animation))); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 55 | |
| 56 | SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 57 | } |
| 58 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 59 | void AnimationTimeline::DetachAnimation(scoped_refptr<Animation> animation) { |
| 60 | DCHECK(animation->id()); |
| 61 | EraseAnimation(animation); |
| 62 | id_to_animation_map_.erase(animation->id()); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 63 | |
| 64 | SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 65 | } |
| 66 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 67 | Animation* AnimationTimeline::GetAnimationById(int animation_id) const { |
| 68 | auto f = id_to_animation_map_.find(animation_id); |
| 69 | return f == id_to_animation_map_.end() ? nullptr : f->second.get(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 70 | } |
| 71 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 72 | void AnimationTimeline::ClearAnimations() { |
| 73 | for (auto& kv : id_to_animation_map_) |
| 74 | EraseAnimation(kv.second); |
| 75 | id_to_animation_map_.clear(); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 76 | |
| 77 | SetNeedsPushProperties(); |
| 78 | } |
| 79 | |
Yi Gu | ad72f3b | 2020-04-28 21:35:59 | [diff] [blame] | 80 | bool AnimationTimeline::TickTimeLinkedAnimations( |
| 81 | const std::vector<scoped_refptr<Animation>>& ticking_animations, |
| 82 | base::TimeTicks monotonic_time) { |
| 83 | DCHECK(!IsScrollTimeline()); |
| 84 | |
| 85 | bool animated = false; |
| 86 | for (auto& animation : ticking_animations) { |
| 87 | if (animation->animation_timeline() != this) |
| 88 | continue; |
| 89 | // Worklet animations are ticked separately by AnimationHost. |
| 90 | if (animation->IsWorkletAnimation()) |
| 91 | continue; |
| 92 | |
| 93 | // Scroll-linked animations are ticked separately. |
| 94 | if (animation->IsScrollLinkedAnimation()) |
| 95 | continue; |
| 96 | |
| 97 | animation->Tick(monotonic_time); |
| 98 | animated = true; |
| 99 | } |
| 100 | return animated; |
| 101 | } |
| 102 | |
| 103 | bool AnimationTimeline::TickScrollLinkedAnimations( |
| 104 | const std::vector<scoped_refptr<Animation>>& ticking_animations, |
| 105 | const ScrollTree& scroll_tree, |
| 106 | bool is_active_tree) { |
| 107 | return false; |
| 108 | } |
| 109 | |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 110 | void AnimationTimeline::SetNeedsPushProperties() { |
| 111 | needs_push_properties_ = true; |
| 112 | if (animation_host_) |
| 113 | animation_host_->SetNeedsPushProperties(); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) { |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 117 | if (needs_push_properties_) { |
| 118 | needs_push_properties_ = false; |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 119 | PushAttachedAnimationsToImplThread(timeline_impl); |
| 120 | RemoveDetachedAnimationsFromImplThread(timeline_impl); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 121 | PushPropertiesToImplThread(timeline_impl); |
| 122 | } |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 123 | } |
| 124 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 125 | void AnimationTimeline::PushAttachedAnimationsToImplThread( |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 126 | AnimationTimeline* timeline_impl) const { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 127 | for (auto& kv : id_to_animation_map_) { |
| 128 | auto& animation = kv.second; |
| 129 | Animation* animation_impl = |
| 130 | timeline_impl->GetAnimationById(animation->id()); |
| 131 | if (animation_impl) |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 132 | continue; |
| 133 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 134 | scoped_refptr<Animation> to_add = animation->CreateImplInstance(); |
| 135 | timeline_impl->AttachAnimation(to_add.get()); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 139 | void AnimationTimeline::RemoveDetachedAnimationsFromImplThread( |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 140 | AnimationTimeline* timeline_impl) const { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 141 | IdToAnimationMap& animations_impl = timeline_impl->id_to_animation_map_; |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 142 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 143 | // Erase all the impl animations which |this| doesn't have. |
| 144 | for (auto it = animations_impl.begin(); it != animations_impl.end();) { |
| 145 | if (GetAnimationById(it->second->id())) { |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 146 | ++it; |
| 147 | } else { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 148 | timeline_impl->EraseAnimation(it->second); |
| 149 | it = animations_impl.erase(it); |
loyso | 10d6267 | 2016-02-09 06:48:21 | [diff] [blame] | 150 | } |
| 151 | } |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 152 | } |
| 153 | |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 154 | void AnimationTimeline::EraseAnimation(scoped_refptr<Animation> animation) { |
Yi Gu | b0422c4 | 2020-01-13 17:00:36 | [diff] [blame] | 155 | if (animation->element_animations()) |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 156 | animation->DetachElement(); |
| 157 | animation->SetAnimationTimeline(nullptr); |
| 158 | animation->SetAnimationHost(nullptr); |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void AnimationTimeline::PushPropertiesToImplThread( |
| 162 | AnimationTimeline* timeline_impl) { |
Yi Gu | aa830ff | 2018-02-22 03:09:11 | [diff] [blame] | 163 | for (auto& kv : id_to_animation_map_) { |
| 164 | Animation* animation = kv.second.get(); |
| 165 | if (Animation* animation_impl = |
| 166 | timeline_impl->GetAnimationById(animation->id())) { |
| 167 | animation->PushPropertiesTo(animation_impl); |
loyso | 0bc588b | 2016-09-08 00:33:45 | [diff] [blame] | 168 | } |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Yi Gu | 352fe2a4 | 2020-01-21 17:38:10 | [diff] [blame] | 172 | bool AnimationTimeline::IsScrollTimeline() const { |
| 173 | return false; |
| 174 | } |
| 175 | |
loyso | bb93bef | 2015-07-03 00:19:50 | [diff] [blame] | 176 | } // namespace cc |