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"
12 #include "ui/gfx/frame_time.h"
16 LayerAnimatorCollection::LayerAnimatorCollection(Compositor
* compositor
)
17 : compositor_(compositor
), last_tick_time_(gfx::FrameTime::Now()) {
20 LayerAnimatorCollection::~LayerAnimatorCollection() {
21 if (compositor_
&& compositor_
->HasAnimationObserver(this))
22 compositor_
->RemoveAnimationObserver(this);
25 void LayerAnimatorCollection::StartAnimator(
26 scoped_refptr
<LayerAnimator
> animator
) {
27 DCHECK_EQ(0U, animators_
.count(animator
));
28 if (!animators_
.size())
29 last_tick_time_
= gfx::FrameTime::Now();
30 animators_
.insert(animator
);
31 if (animators_
.size() == 1U && compositor_
)
32 compositor_
->AddAnimationObserver(this);
35 void LayerAnimatorCollection::StopAnimator(
36 scoped_refptr
<LayerAnimator
> animator
) {
37 DCHECK_GT(animators_
.count(animator
), 0U);
38 animators_
.erase(animator
);
39 if (animators_
.empty() && compositor_
)
40 compositor_
->RemoveAnimationObserver(this);
43 bool LayerAnimatorCollection::HasActiveAnimators() const {
44 return !animators_
.empty();
47 void LayerAnimatorCollection::OnAnimationStep(base::TimeTicks now
) {
48 last_tick_time_
= now
;
49 std::set
<scoped_refptr
<LayerAnimator
> > list
= animators_
;
50 for (std::set
<scoped_refptr
<LayerAnimator
> >::iterator iter
= list
.begin();
53 // Make sure the animator is still valid.
54 if (animators_
.count(*iter
) > 0)
57 if (!HasActiveAnimators() && compositor_
)
58 compositor_
->RemoveAnimationObserver(this);