File: animation_container.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,025 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/animation/animation_container.h"

#include "base/functional/bind.h"
#include "ui/gfx/animation/animation_container_element.h"
#include "ui/gfx/animation/animation_container_observer.h"

using base::TimeTicks;

namespace gfx {

AnimationContainer::AnimationContainer() = default;

AnimationContainer::~AnimationContainer() {
  if (observer_)
    observer_->AnimationContainerShuttingDown(this);

  // The animations own us and stop themselves before being deleted. If they're
  // still running, something is wrong.
  DCHECK(!is_running());
}

void AnimationContainer::Start(AnimationContainerElement* element) {
  DCHECK(elements_.count(element) == 0);  // Start should only be invoked if the
                                          // element isn't running.

  if (!is_running()) {
    last_tick_time_ = base::TimeTicks::Now();
    SetMinTimerInterval(element->GetTimerInterval());
    min_timer_interval_count_ = 1;
  } else if (element->GetTimerInterval() < min_timer_interval_) {
    SetMinTimerInterval(element->GetTimerInterval());
    min_timer_interval_count_ = 1;
  } else if (element->GetTimerInterval() == min_timer_interval_) {
    min_timer_interval_count_++;
  }

  element->SetStartTime(last_tick_time_);
  elements_.insert(element);
}

void AnimationContainer::Stop(AnimationContainerElement* element) {
  DCHECK(elements_.count(element) > 0);  // The element must be running.

  base::TimeDelta interval = element->GetTimerInterval();
  elements_.erase(element);

  if (!is_running()) {
    runner_->Stop();
    min_timer_interval_count_ = 0;
    if (observer_)
      observer_->AnimationContainerEmpty(this);
  } else if (interval == min_timer_interval_) {
    min_timer_interval_count_--;

    // If the last element at the current (minimum) timer interval has been
    // removed then go find the new minimum and the number of elements at that
    // same minimum.
    if (min_timer_interval_count_ == 0) {
      std::pair<base::TimeDelta, size_t> interval_count =
          GetMinIntervalAndCount();
      DCHECK(interval_count.first > min_timer_interval_);
      SetMinTimerInterval(interval_count.first);
      min_timer_interval_count_ = interval_count.second;
    }
  }
}

void AnimationContainer::SetAnimationRunner(
    std::unique_ptr<AnimationRunner> runner) {
  has_custom_animation_runner_ = !!runner;
  runner_ = has_custom_animation_runner_
                ? std::move(runner)
                : AnimationRunner::CreateDefaultAnimationRunner();
  if (is_running())
    RestartTimer(base::TimeTicks::Now() - last_tick_time_);
}

void AnimationContainer::Run(base::TimeTicks current_time) {
  // We notify the observer after updating all the elements. If all the elements
  // are deleted as a result of updating then our ref count would go to zero and
  // we would be deleted before we notify our observer. We add a reference to
  // ourself here to make sure we're still valid after running all the elements.
  scoped_refptr<AnimationContainer> this_ref(this);

  last_tick_time_ = current_time;

  // Make a copy of the elements to iterate over so that if any elements are
  // removed as part of invoking Step there aren't any problems.
  Elements elements = elements_;

  for (Elements::const_iterator i = elements.begin();
       i != elements.end(); ++i) {
    // Make sure the element is still valid.
    if (elements_.find(*i) != elements_.end())
      (*i)->Step(current_time);
  }

  if (observer_)
    observer_->AnimationContainerProgressed(this);
}

void AnimationContainer::SetMinTimerInterval(base::TimeDelta delta) {
  // This doesn't take into account how far along the current element is, but
  // that shouldn't be a problem for uses of Animation/AnimationContainer.
  runner_->Stop();
  min_timer_interval_ = delta;
  RestartTimer(base::TimeDelta());
}

void AnimationContainer::RestartTimer(base::TimeDelta elapsed) {
  runner_->Start(
      min_timer_interval_, elapsed,
      base::BindRepeating(&AnimationContainer::Run, base::Unretained(this)));
}

std::pair<base::TimeDelta, size_t> AnimationContainer::GetMinIntervalAndCount()
    const {
  DCHECK(is_running());

  // Find the minimum interval and the number of elements sharing that same
  // interval. It is tempting to create a map of intervals -> counts in order to
  // make this O(log n) instead of O(n). However, profiling shows that this
  // offers no practical performance gain (the most common case is that all
  // elements in the set share the same interval).
  base::TimeDelta min;
  size_t count = 1;
  auto i = elements_.begin();
  min = (*i)->GetTimerInterval();
  for (++i; i != elements_.end(); ++i) {
    auto interval = (*i)->GetTimerInterval();
    if (interval < min) {
      min = interval;
      count = 1;
    } else if (interval == min) {
      count++;
    }
  }

  return std::make_pair(min, count);
}

}  // namespace gfx