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