layer_animator_collection.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "ui/compositor/layer_animator_collection.h"
  5. #include <set>
  6. #include "base/time/time.h"
  7. #include "ui/compositor/compositor.h"
  8. #include "ui/compositor/layer_animator.h"
  9. namespace ui {
  10. LayerAnimatorCollection::LayerAnimatorCollection(Compositor* compositor)
  11. : compositor_(compositor), last_tick_time_(base::TimeTicks::Now()) {
  12. // Do not check the active duration for the LayerAnimationCollection because
  13. // new animation can be added while running existing animation, which
  14. // extends the duration.
  15. set_check_active_duration(false);
  16. }
  17. LayerAnimatorCollection::~LayerAnimatorCollection() {
  18. if (compositor_)
  19. compositor_->RemoveAnimationObserver(this);
  20. }
  21. void LayerAnimatorCollection::StartAnimator(
  22. scoped_refptr<LayerAnimator> animator) {
  23. DCHECK_EQ(0U, animators_.count(animator));
  24. if (animators_.empty())
  25. last_tick_time_ = base::TimeTicks::Now();
  26. animators_.insert(animator);
  27. if (animators_.size() == 1U && compositor_)
  28. compositor_->AddAnimationObserver(this);
  29. }
  30. void LayerAnimatorCollection::StopAnimator(
  31. scoped_refptr<LayerAnimator> animator) {
  32. DCHECK_GT(animators_.count(animator), 0U);
  33. animators_.erase(animator);
  34. if (animators_.empty() && compositor_)
  35. compositor_->RemoveAnimationObserver(this);
  36. }
  37. bool LayerAnimatorCollection::HasActiveAnimators() const {
  38. return !animators_.empty();
  39. }
  40. void LayerAnimatorCollection::OnAnimationStep(base::TimeTicks now) {
  41. last_tick_time_ = now;
  42. std::set<scoped_refptr<LayerAnimator> > list = animators_;
  43. for (auto iter = list.begin(); iter != list.end(); ++iter) {
  44. // Make sure the animator is still valid.
  45. if (animators_.count(*iter) > 0)
  46. (*iter)->Step(now);
  47. }
  48. if (!HasActiveAnimators() && compositor_)
  49. compositor_->RemoveAnimationObserver(this);
  50. }
  51. void LayerAnimatorCollection::OnCompositingShuttingDown(
  52. Compositor* compositor) {
  53. DCHECK_EQ(compositor_, compositor);
  54. compositor_->RemoveAnimationObserver(this);
  55. compositor_ = nullptr;
  56. }
  57. } // namespace ui