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