[go: nahoru, domu]

blob: f2ea00f32021c0f13c4f13b56fcb3383ca8405da [file] [log] [blame]
Avi Drissman3f7a9d82022-09-08 20:55:421// Copyright 2015 The Chromium Authors
loysobb93bef2015-07-03 00:19:502// 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 Kastingb4ee3372021-07-06 23:02:298#include <utility>
9#include <vector>
loysobb93bef2015-07-03 00:19:5010
Peter Kastingb4ee3372021-07-06 23:02:2911#include "base/time/time.h"
Yi Guaa830ff2018-02-22 03:09:1112#include "cc/animation/animation.h"
loyso0bc588b2016-09-08 00:33:4513#include "cc/animation/animation_host.h"
Yi Guad72f3b2020-04-28 21:35:5914#include "cc/trees/property_tree.h"
loysobb93bef2015-07-03 00:19:5015
16namespace cc {
17
Kevin Ellis8bc26acd2023-02-07 17:52:4418scoped_refptr<AnimationTimeline> AnimationTimeline::Create(int id,
19 bool impl_only) {
20 return base::WrapRefCounted(new AnimationTimeline(id, impl_only));
loysobb93bef2015-07-03 00:19:5021}
22
Kevin Ellis8bc26acd2023-02-07 17:52:4423AnimationTimeline::AnimationTimeline(int id, bool is_impl_only)
loyso0bc588b2016-09-08 00:33:4524 : id_(id),
25 animation_host_(),
26 needs_push_properties_(false),
Kevin Ellis8bc26acd2023-02-07 17:52:4427 is_impl_only_(is_impl_only) {}
loysobb93bef2015-07-03 00:19:5028
29AnimationTimeline::~AnimationTimeline() {
Kevin Ellis8bc26acd2023-02-07 17:52:4430 for (auto& kv : id_to_animation_map_.Read(*this)) {
loyso10d62672016-02-09 06:48:2131 kv.second->SetAnimationTimeline(nullptr);
Kevin Ellis8bc26acd2023-02-07 17:52:4432 }
loysobb93bef2015-07-03 00:19:5033}
34
35scoped_refptr<AnimationTimeline> AnimationTimeline::CreateImplInstance() const {
36 scoped_refptr<AnimationTimeline> timeline = AnimationTimeline::Create(id());
37 return timeline;
38}
39
40void AnimationTimeline::SetAnimationHost(AnimationHost* animation_host) {
Kevin Ellis8bc26acd2023-02-07 17:52:4441 DCHECK(!animation_host || animation_host->IsOwnerThread());
42
loyso0bc588b2016-09-08 00:33:4543 if (animation_host_ == animation_host)
44 return;
45
Kevin Ellis8bc26acd2023-02-07 17:52:4446 WaitForProtectedSequenceCompletion();
47
loysobb93bef2015-07-03 00:19:5048 animation_host_ = animation_host;
Kevin Ellis8bc26acd2023-02-07 17:52:4449 for (auto& kv : id_to_animation_map_.Write(*this)) {
loyso10d62672016-02-09 06:48:2150 kv.second->SetAnimationHost(animation_host);
Kevin Ellis8bc26acd2023-02-07 17:52:4451 }
loyso0bc588b2016-09-08 00:33:4552
53 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5054}
55
Yi Guaa830ff2018-02-22 03:09:1156void AnimationTimeline::AttachAnimation(scoped_refptr<Animation> animation) {
57 DCHECK(animation->id());
Kevin Ellis8bc26acd2023-02-07 17:52:4458 animation->SetAnimationHost(animation_host());
Yi Guaa830ff2018-02-22 03:09:1159 animation->SetAnimationTimeline(this);
Kevin Ellis8bc26acd2023-02-07 17:52:4460 id_to_animation_map_.Write(*this).insert(
Yi Guaa830ff2018-02-22 03:09:1161 std::make_pair(animation->id(), std::move(animation)));
loyso0bc588b2016-09-08 00:33:4562
63 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5064}
65
Yi Guaa830ff2018-02-22 03:09:1166void AnimationTimeline::DetachAnimation(scoped_refptr<Animation> animation) {
67 DCHECK(animation->id());
68 EraseAnimation(animation);
Kevin Ellis8bc26acd2023-02-07 17:52:4469 id_to_animation_map_.Write(*this).erase(animation->id());
loyso0bc588b2016-09-08 00:33:4570
71 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5072}
73
Yi Guaa830ff2018-02-22 03:09:1174Animation* AnimationTimeline::GetAnimationById(int animation_id) const {
Kevin Ellis8bc26acd2023-02-07 17:52:4475 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();
loysobb93bef2015-07-03 00:19:5078}
79
Yi Guaa830ff2018-02-22 03:09:1180void AnimationTimeline::ClearAnimations() {
Kevin Ellis8bc26acd2023-02-07 17:52:4481 for (auto& kv : id_to_animation_map_.Write(*this)) {
Yi Guaa830ff2018-02-22 03:09:1182 EraseAnimation(kv.second);
Kevin Ellis8bc26acd2023-02-07 17:52:4483 }
84 id_to_animation_map_.Write(*this).clear();
loyso0bc588b2016-09-08 00:33:4585
86 SetNeedsPushProperties();
87}
88
Yi Guad72f3b2020-04-28 21:35:5989bool 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
112bool 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
loyso0bc588b2016-09-08 00:33:45119void AnimationTimeline::SetNeedsPushProperties() {
Kevin Ellis8bc26acd2023-02-07 17:52:44120 needs_push_properties_.Write(*this) = true;
121 if (animation_host()) {
122 animation_host()->SetNeedsPushProperties();
123 }
loysobb93bef2015-07-03 00:19:50124}
125
126void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) {
Kevin Ellis8bc26acd2023-02-07 17:52:44127 if (needs_push_properties_.Read(*this)) {
128 needs_push_properties_.Write(*this) = false;
Yi Guaa830ff2018-02-22 03:09:11129 PushAttachedAnimationsToImplThread(timeline_impl);
130 RemoveDetachedAnimationsFromImplThread(timeline_impl);
loyso0bc588b2016-09-08 00:33:45131 PushPropertiesToImplThread(timeline_impl);
132 }
loysobb93bef2015-07-03 00:19:50133}
134
Yi Guaa830ff2018-02-22 03:09:11135void AnimationTimeline::PushAttachedAnimationsToImplThread(
loysobb93bef2015-07-03 00:19:50136 AnimationTimeline* timeline_impl) const {
Kevin Ellis8bc26acd2023-02-07 17:52:44137 for (auto& kv : id_to_animation_map_.Read(*this)) {
Yi Guaa830ff2018-02-22 03:09:11138 auto& animation = kv.second;
139 Animation* animation_impl =
140 timeline_impl->GetAnimationById(animation->id());
141 if (animation_impl)
loysobb93bef2015-07-03 00:19:50142 continue;
143
Yi Guaa830ff2018-02-22 03:09:11144 scoped_refptr<Animation> to_add = animation->CreateImplInstance();
145 timeline_impl->AttachAnimation(to_add.get());
loysobb93bef2015-07-03 00:19:50146 }
147}
148
Yi Guaa830ff2018-02-22 03:09:11149void AnimationTimeline::RemoveDetachedAnimationsFromImplThread(
loysobb93bef2015-07-03 00:19:50150 AnimationTimeline* timeline_impl) const {
Kevin Ellis8bc26acd2023-02-07 17:52:44151 IdToAnimationMap& animations_impl =
152 timeline_impl->id_to_animation_map_.Write(*this);
loysobb93bef2015-07-03 00:19:50153
Yi Guaa830ff2018-02-22 03:09:11154 // 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())) {
loyso10d62672016-02-09 06:48:21157 ++it;
158 } else {
Yi Guaa830ff2018-02-22 03:09:11159 timeline_impl->EraseAnimation(it->second);
160 it = animations_impl.erase(it);
loyso10d62672016-02-09 06:48:21161 }
162 }
loysobb93bef2015-07-03 00:19:50163}
164
Yi Guaa830ff2018-02-22 03:09:11165void AnimationTimeline::EraseAnimation(scoped_refptr<Animation> animation) {
Yi Gub0422c42020-01-13 17:00:36166 if (animation->element_animations())
Yi Guaa830ff2018-02-22 03:09:11167 animation->DetachElement();
Robert Flackca12161ac2022-03-14 18:09:19168 animation->SetAnimationHost(nullptr);
Robert Flack670a727d2022-03-17 01:13:15169 animation->SetAnimationTimeline(nullptr);
loysobb93bef2015-07-03 00:19:50170}
171
172void AnimationTimeline::PushPropertiesToImplThread(
173 AnimationTimeline* timeline_impl) {
Kevin Ellis8bc26acd2023-02-07 17:52:44174 for (auto& kv : id_to_animation_map_.Read(*this)) {
Yi Guaa830ff2018-02-22 03:09:11175 Animation* animation = kv.second.get();
176 if (Animation* animation_impl =
177 timeline_impl->GetAnimationById(animation->id())) {
178 animation->PushPropertiesTo(animation_impl);
loyso0bc588b2016-09-08 00:33:45179 }
loysobb93bef2015-07-03 00:19:50180 }
181}
182
Yi Gu352fe2a42020-01-21 17:38:10183bool AnimationTimeline::IsScrollTimeline() const {
184 return false;
185}
186
Kevin Ellis8bc26acd2023-02-07 17:52:44187bool AnimationTimeline::IsOwnerThread() const {
188 return !animation_host_ || animation_host_->IsOwnerThread();
189}
190
191bool AnimationTimeline::InProtectedSequence() const {
192 return !animation_host_ || animation_host_->InProtectedSequence();
193}
194
195void AnimationTimeline::WaitForProtectedSequenceCompletion() const {
196 if (animation_host_) {
197 animation_host_->WaitForProtectedSequenceCompletion();
198 }
199}
200
loysobb93bef2015-07-03 00:19:50201} // namespace cc