Home | History | Annotate | Download | only in compositor
      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.
      4 
      5 #include "ui/compositor/layer_animator_collection.h"
      6 
      7 #include <set>
      8 
      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"
     13 
     14 namespace ui {
     15 
     16 LayerAnimatorCollection::LayerAnimatorCollection(Compositor* compositor)
     17     : compositor_(compositor), last_tick_time_(gfx::FrameTime::Now()) {
     18 }
     19 
     20 LayerAnimatorCollection::~LayerAnimatorCollection() {
     21   if (compositor_ && compositor_->HasAnimationObserver(this))
     22     compositor_->RemoveAnimationObserver(this);
     23 }
     24 
     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);
     33 }
     34 
     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);
     41 }
     42 
     43 bool LayerAnimatorCollection::HasActiveAnimators() const {
     44   return !animators_.empty();
     45 }
     46 
     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();
     51        iter != list.end();
     52        ++iter) {
     53     // Make sure the animator is still valid.
     54     if (animators_.count(*iter) > 0)
     55       (*iter)->Step(now);
     56   }
     57   if (!HasActiveAnimators() && compositor_)
     58     compositor_->RemoveAnimationObserver(this);
     59 }
     60 
     61 }  // namespace ui
     62