[go: nahoru, domu]

blob: 0d57f6396d7cf4b607256ce14d04c936644c2f73 [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>
8
Yi Guaa830ff2018-02-22 03:09:119#include "cc/animation/animation.h"
loyso0bc588b2016-09-08 00:33:4510#include "cc/animation/animation_host.h"
loysobb93bef2015-07-03 00:19:5011
12namespace cc {
13
14scoped_refptr<AnimationTimeline> AnimationTimeline::Create(int id) {
kylechar973a0412017-09-26 18:40:2915 return base::WrapRefCounted(new AnimationTimeline(id));
loysobb93bef2015-07-03 00:19:5016}
17
18AnimationTimeline::AnimationTimeline(int id)
loyso0bc588b2016-09-08 00:33:4519 : id_(id),
20 animation_host_(),
21 needs_push_properties_(false),
22 is_impl_only_(false) {}
loysobb93bef2015-07-03 00:19:5023
24AnimationTimeline::~AnimationTimeline() {
Yi Guaa830ff2018-02-22 03:09:1125 for (auto& kv : id_to_animation_map_)
loyso10d62672016-02-09 06:48:2126 kv.second->SetAnimationTimeline(nullptr);
loysobb93bef2015-07-03 00:19:5027}
28
29scoped_refptr<AnimationTimeline> AnimationTimeline::CreateImplInstance() const {
30 scoped_refptr<AnimationTimeline> timeline = AnimationTimeline::Create(id());
31 return timeline;
32}
33
34void AnimationTimeline::SetAnimationHost(AnimationHost* animation_host) {
loyso0bc588b2016-09-08 00:33:4535 if (animation_host_ == animation_host)
36 return;
37
loysobb93bef2015-07-03 00:19:5038 animation_host_ = animation_host;
Yi Guaa830ff2018-02-22 03:09:1139 for (auto& kv : id_to_animation_map_)
loyso10d62672016-02-09 06:48:2140 kv.second->SetAnimationHost(animation_host);
loyso0bc588b2016-09-08 00:33:4541
42 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5043}
44
Yi Guaa830ff2018-02-22 03:09:1145void AnimationTimeline::AttachAnimation(scoped_refptr<Animation> animation) {
46 DCHECK(animation->id());
47 animation->SetAnimationHost(animation_host_);
48 animation->SetAnimationTimeline(this);
49 id_to_animation_map_.insert(
50 std::make_pair(animation->id(), std::move(animation)));
loyso0bc588b2016-09-08 00:33:4551
52 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5053}
54
Yi Guaa830ff2018-02-22 03:09:1155void AnimationTimeline::DetachAnimation(scoped_refptr<Animation> animation) {
56 DCHECK(animation->id());
57 EraseAnimation(animation);
58 id_to_animation_map_.erase(animation->id());
loyso0bc588b2016-09-08 00:33:4559
60 SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5061}
62
Yi Guaa830ff2018-02-22 03:09:1163Animation* AnimationTimeline::GetAnimationById(int animation_id) const {
64 auto f = id_to_animation_map_.find(animation_id);
65 return f == id_to_animation_map_.end() ? nullptr : f->second.get();
loysobb93bef2015-07-03 00:19:5066}
67
Yi Guaa830ff2018-02-22 03:09:1168void AnimationTimeline::ClearAnimations() {
69 for (auto& kv : id_to_animation_map_)
70 EraseAnimation(kv.second);
71 id_to_animation_map_.clear();
loyso0bc588b2016-09-08 00:33:4572
73 SetNeedsPushProperties();
74}
75
76void AnimationTimeline::SetNeedsPushProperties() {
77 needs_push_properties_ = true;
78 if (animation_host_)
79 animation_host_->SetNeedsPushProperties();
loysobb93bef2015-07-03 00:19:5080}
81
82void AnimationTimeline::PushPropertiesTo(AnimationTimeline* timeline_impl) {
loyso0bc588b2016-09-08 00:33:4583 if (needs_push_properties_) {
84 needs_push_properties_ = false;
Yi Guaa830ff2018-02-22 03:09:1185 PushAttachedAnimationsToImplThread(timeline_impl);
86 RemoveDetachedAnimationsFromImplThread(timeline_impl);
loyso0bc588b2016-09-08 00:33:4587 PushPropertiesToImplThread(timeline_impl);
88 }
loysobb93bef2015-07-03 00:19:5089}
90
Yi Guaa830ff2018-02-22 03:09:1191void AnimationTimeline::PushAttachedAnimationsToImplThread(
loysobb93bef2015-07-03 00:19:5092 AnimationTimeline* timeline_impl) const {
Yi Guaa830ff2018-02-22 03:09:1193 for (auto& kv : id_to_animation_map_) {
94 auto& animation = kv.second;
95 Animation* animation_impl =
96 timeline_impl->GetAnimationById(animation->id());
97 if (animation_impl)
loysobb93bef2015-07-03 00:19:5098 continue;
99
Yi Guaa830ff2018-02-22 03:09:11100 scoped_refptr<Animation> to_add = animation->CreateImplInstance();
101 timeline_impl->AttachAnimation(to_add.get());
loysobb93bef2015-07-03 00:19:50102 }
103}
104
Yi Guaa830ff2018-02-22 03:09:11105void AnimationTimeline::RemoveDetachedAnimationsFromImplThread(
loysobb93bef2015-07-03 00:19:50106 AnimationTimeline* timeline_impl) const {
Yi Guaa830ff2018-02-22 03:09:11107 IdToAnimationMap& animations_impl = timeline_impl->id_to_animation_map_;
loysobb93bef2015-07-03 00:19:50108
Yi Guaa830ff2018-02-22 03:09:11109 // Erase all the impl animations which |this| doesn't have.
110 for (auto it = animations_impl.begin(); it != animations_impl.end();) {
111 if (GetAnimationById(it->second->id())) {
loyso10d62672016-02-09 06:48:21112 ++it;
113 } else {
Yi Guaa830ff2018-02-22 03:09:11114 timeline_impl->EraseAnimation(it->second);
115 it = animations_impl.erase(it);
loyso10d62672016-02-09 06:48:21116 }
117 }
loysobb93bef2015-07-03 00:19:50118}
119
Yi Guaa830ff2018-02-22 03:09:11120void AnimationTimeline::EraseAnimation(scoped_refptr<Animation> animation) {
121 if (animation->has_element_animations())
122 animation->DetachElement();
123 animation->SetAnimationTimeline(nullptr);
124 animation->SetAnimationHost(nullptr);
loysobb93bef2015-07-03 00:19:50125}
126
127void AnimationTimeline::PushPropertiesToImplThread(
128 AnimationTimeline* timeline_impl) {
Yi Guaa830ff2018-02-22 03:09:11129 for (auto& kv : id_to_animation_map_) {
130 Animation* animation = kv.second.get();
131 if (Animation* animation_impl =
132 timeline_impl->GetAnimationById(animation->id())) {
133 animation->PushPropertiesTo(animation_impl);
loyso0bc588b2016-09-08 00:33:45134 }
loysobb93bef2015-07-03 00:19:50135 }
136}
137
138} // namespace cc