1 // Copyright 2014 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 "ui/compositor/layer_animator_collection.h"
9 #include "base/time/time.h"
10 #include "ui/compositor/compositor.h"
11 #include "ui/compositor/layer_animator.h"
15 LayerAnimatorCollection::LayerAnimatorCollection(Compositor
* compositor
)
16 : compositor_(compositor
), last_tick_time_(base::TimeTicks::Now()) {
19 LayerAnimatorCollection::~LayerAnimatorCollection() {
21 compositor_
->RemoveAnimationObserver(this);
24 void LayerAnimatorCollection::StartAnimator(
25 scoped_refptr
<LayerAnimator
> animator
) {
26 DCHECK_EQ(0U, animators_
.count(animator
));
27 if (!animators_
.size())
28 last_tick_time_
= base::TimeTicks::Now();
29 animators_
.insert(animator
);
30 if (animators_
.size() == 1U && compositor_
)
31 compositor_
->AddAnimationObserver(this);
34 void LayerAnimatorCollection::StopAnimator(
35 scoped_refptr
<LayerAnimator
> animator
) {
36 DCHECK_GT(animators_
.count(animator
), 0U);
37 animators_
.erase(animator
);
38 if (animators_
.empty() && compositor_
)
39 compositor_
->RemoveAnimationObserver(this);
42 bool LayerAnimatorCollection::HasActiveAnimators() const {
43 return !animators_
.empty();
46 void LayerAnimatorCollection::OnAnimationStep(base::TimeTicks now
) {
47 last_tick_time_
= now
;
48 std::set
<scoped_refptr
<LayerAnimator
> > list
= animators_
;
49 for (std::set
<scoped_refptr
<LayerAnimator
> >::iterator iter
= list
.begin();
52 // Make sure the animator is still valid.
53 if (animators_
.count(*iter
) > 0)
56 if (!HasActiveAnimators() && compositor_
)
57 compositor_
->RemoveAnimationObserver(this);
60 void LayerAnimatorCollection::OnCompositingShuttingDown(
61 Compositor
* compositor
) {
62 DCHECK_EQ(compositor_
, compositor
);
63 compositor_
->RemoveAnimationObserver(this);
64 compositor_
= nullptr;