[go: nahoru, domu]

blob: ac3a92e9c2c4d7358c08d1d729fef7abed0185f8 [file] [log] [blame]
loysobb93bef2015-07-03 00:19:501// 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 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
18scoped_refptr<AnimationTimeline> AnimationTimeline::Create(int id) {
kylechar973a0412017-09-26 18:40:2919 return base::WrapRefCounted(new AnimationTimeline(id));
loysobb93bef2015-07-03 00:19:5020}
21
22AnimationTimeline::AnimationTimeline(int id)
loyso0bc588b2016-09-08 00:33:4523 : id_(id),
24 animation_host_(),
25 needs_push_properties_(false),
26 is_impl_only_(false) {}
loysobb93bef2015-07-03 00:19:5027
28AnimationTimeline::~AnimationTimeline() {
Yi Guaa830ff2018-02-22 03:09:1129 for (auto& kv : id_to_animation_map_)
loyso10d62672016-02-09 06:48:2130 kv.second->SetAnimationTimeline(nullptr);
loysobb93bef2015-07-03 00:19:5031}
32
33scoped_refptr<AnimationTimeline> AnimationTimeline::CreateImplInstance() const {
34 scoped_refptr<AnimationTimeline> timeline = AnimationTimeline::Create(id());
35 return timeline;
36}
37
38void AnimationTimeline::SetAnimationHost(AnimationHost* animation_host) {
loyso0bc588b2016-09-08 00:33:4539 if (animation_host_ == animation_host)
40 return;
41
loysobb93bef2015-07-03 00:19:5042 animation_host_ = animation_host;
Yi Guaa830ff2018-02-22 03:09:1143 for (auto& kv : id_to_animation_map_)
loyso10d62672016-02-09 06:48:2144 kv.second->SetAnimationHost(animation_host);
loyso0bc588b2016-09-08 00:33:4545
46 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5047}
48
Yi Guaa830ff2018-02-22 03:09:1149void 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)));
loyso0bc588b2016-09-08 00:33:4555
56 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5057}
58
Yi Guaa830ff2018-02-22 03:09:1159void AnimationTimeline::DetachAnimation(scoped_refptr<Animation> animation) {
60 DCHECK(animation->id());
61 EraseAnimation(animation);
62 id_to_animation_map_.erase(animation->id());
loyso0bc588b2016-09-08 00:33:4563
64 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5065}
66
Yi Guaa830ff2018-02-22 03:09:1167Animation* 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();
loysobb93bef2015-07-03 00:19:5070}
71
Yi Guaa830ff2018-02-22 03:09:1172void AnimationTimeline::ClearAnimations() {
73 for (auto& kv : id_to_animation_map_)
74 EraseAnimation(kv.second);
75 id_to_animation_map_.clear();
loyso0bc588b2016-09-08 00:33:4576
77 SetNeedsPushProperties();
78}
79
Yi Guad72f3b2020-04-28 21:35:5980bool 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
103bool 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
loyso0bc588b2016-09-08 00:33:45110void AnimationTimeline::SetNeedsPushProperties() {
111 needs_push_properties_ = true;
112 if (animation_host_)
113 animation_host_->SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:50114}
115
116void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) {
loyso0bc588b2016-09-08 00:33:45117 if (needs_push_properties_) {
118 needs_push_properties_ = false;
Yi Guaa830ff2018-02-22 03:09:11119 PushAttachedAnimationsToImplThread(timeline_impl);
120 RemoveDetachedAnimationsFromImplThread(timeline_impl);
loyso0bc588b2016-09-08 00:33:45121 PushPropertiesToImplThread(timeline_impl);
122 }
loysobb93bef2015-07-03 00:19:50123}
124
Yi Guaa830ff2018-02-22 03:09:11125void AnimationTimeline::PushAttachedAnimationsToImplThread(
loysobb93bef2015-07-03 00:19:50126 AnimationTimeline* timeline_impl) const {
Yi Guaa830ff2018-02-22 03:09:11127 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)
loysobb93bef2015-07-03 00:19:50132 continue;
133
Yi Guaa830ff2018-02-22 03:09:11134 scoped_refptr<Animation> to_add = animation->CreateImplInstance();
135 timeline_impl->AttachAnimation(to_add.get());
loysobb93bef2015-07-03 00:19:50136 }
137}
138
Yi Guaa830ff2018-02-22 03:09:11139void AnimationTimeline::RemoveDetachedAnimationsFromImplThread(
loysobb93bef2015-07-03 00:19:50140 AnimationTimeline* timeline_impl) const {
Yi Guaa830ff2018-02-22 03:09:11141 IdToAnimationMap& animations_impl = timeline_impl->id_to_animation_map_;
loysobb93bef2015-07-03 00:19:50142
Yi Guaa830ff2018-02-22 03:09:11143 // 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())) {
loyso10d62672016-02-09 06:48:21146 ++it;
147 } else {
Yi Guaa830ff2018-02-22 03:09:11148 timeline_impl->EraseAnimation(it->second);
149 it = animations_impl.erase(it);
loyso10d62672016-02-09 06:48:21150 }
151 }
loysobb93bef2015-07-03 00:19:50152}
153
Yi Guaa830ff2018-02-22 03:09:11154void AnimationTimeline::EraseAnimation(scoped_refptr<Animation> animation) {
Yi Gub0422c42020-01-13 17:00:36155 if (animation->element_animations())
Yi Guaa830ff2018-02-22 03:09:11156 animation->DetachElement();
157 animation->SetAnimationTimeline(nullptr);
158 animation->SetAnimationHost(nullptr);
loysobb93bef2015-07-03 00:19:50159}
160
161void AnimationTimeline::PushPropertiesToImplThread(
162 AnimationTimeline* timeline_impl) {
Yi Guaa830ff2018-02-22 03:09:11163 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);
loyso0bc588b2016-09-08 00:33:45168 }
loysobb93bef2015-07-03 00:19:50169 }
170}
171
Yi Gu352fe2a42020-01-21 17:38:10172bool AnimationTimeline::IsScrollTimeline() const {
173 return false;
174}
175
loysobb93bef2015-07-03 00:19:50176} // namespace cc