1 // 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.
5 #include "cc/animation/animation_timeline.h"
9 #include "cc/animation/animation_player.h"
13 scoped_refptr
<AnimationTimeline
> AnimationTimeline::Create(int id
) {
14 return make_scoped_refptr(new AnimationTimeline(id
));
17 AnimationTimeline::AnimationTimeline(int id
)
18 : id_(id
), animation_host_(), is_impl_only_(false) {
21 AnimationTimeline::~AnimationTimeline() {
22 for (auto& player
: players_
)
23 player
->SetAnimationTimeline(nullptr);
26 scoped_refptr
<AnimationTimeline
> AnimationTimeline::CreateImplInstance() const {
27 scoped_refptr
<AnimationTimeline
> timeline
= AnimationTimeline::Create(id());
31 void AnimationTimeline::SetAnimationHost(AnimationHost
* animation_host
) {
32 animation_host_
= animation_host
;
33 for (auto& player
: players_
)
34 player
->SetAnimationHost(animation_host
);
37 void AnimationTimeline::AttachPlayer(scoped_refptr
<AnimationPlayer
> player
) {
38 DCHECK(animation_host_
);
39 player
->SetAnimationHost(animation_host_
);
40 player
->SetAnimationTimeline(this);
41 players_
.push_back(player
);
44 void AnimationTimeline::DetachPlayer(scoped_refptr
<AnimationPlayer
> player
) {
45 for (AnimationPlayerList::iterator iter
= players_
.begin();
46 iter
!= players_
.end(); ++iter
) {
47 if (iter
->get() != player
)
50 ErasePlayers(iter
, iter
+ 1);
54 player
->SetAnimationHost(nullptr);
57 AnimationPlayer
* AnimationTimeline::GetPlayerById(int player_id
) const {
58 for (auto& player
: players_
)
59 if (player
->id() == player_id
)
64 void AnimationTimeline::ClearPlayers() {
65 ErasePlayers(players_
.begin(), players_
.end());
68 void AnimationTimeline::PushPropertiesTo(AnimationTimeline
* timeline_impl
) {
69 PushAttachedPlayersToImplThread(timeline_impl
);
70 RemoveDetachedPlayersFromImplThread(timeline_impl
);
71 PushPropertiesToImplThread(timeline_impl
);
74 void AnimationTimeline::PushAttachedPlayersToImplThread(
75 AnimationTimeline
* timeline_impl
) const {
76 for (auto& player
: players_
) {
77 AnimationPlayer
* player_impl
= timeline_impl
->GetPlayerById(player
->id());
81 scoped_refptr
<AnimationPlayer
> to_add
= player
->CreateImplInstance();
82 timeline_impl
->AttachPlayer(to_add
.get());
86 void AnimationTimeline::RemoveDetachedPlayersFromImplThread(
87 AnimationTimeline
* timeline_impl
) const {
88 AnimationPlayerList
& players_impl
= timeline_impl
->players_
;
91 std::partition(players_impl
.begin(), players_impl
.end(),
92 [this](AnimationPlayerList::value_type player_impl
) {
93 return GetPlayerById(player_impl
->id());
96 timeline_impl
->ErasePlayers(to_erase
, players_impl
.end());
99 void AnimationTimeline::ErasePlayers(AnimationPlayerList::iterator begin
,
100 AnimationPlayerList::iterator end
) {
101 for (auto i
= begin
; i
!= end
; ++i
) {
103 if (player
->element_animations())
104 player
->DetachLayer();
105 player
->SetAnimationTimeline(nullptr);
108 players_
.erase(begin
, end
);
111 void AnimationTimeline::PushPropertiesToImplThread(
112 AnimationTimeline
* timeline_impl
) {
113 for (auto& player
: players_
) {
114 AnimationPlayer
* player_impl
= timeline_impl
->GetPlayerById(player
->id());
116 player
->PushPropertiesTo(player_impl
);