summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/core/animation/animation_clock.h
blob: 8f10d8b68529c5333f0d88c84e17ce8f26c74473 (plain)
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
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_CLOCK_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_CLOCK_H_

#include <limits>

#include "base/macros.h"
#include "base/time/default_tick_clock.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

namespace blink {

// Maintains a stationary clock time during script execution. Tracks the glass
// time of the beginning of the current animation frame (i.e. the moment photons
// left the screen for the previous frame).
class CORE_EXPORT AnimationClock {
  DISALLOW_NEW();

 public:
  AnimationClock()
      : time_(),
        can_dynamically_update_time_(false),
        clock_(base::DefaultTickClock::GetInstance()),
        task_for_which_time_was_calculated_(
            std::numeric_limits<unsigned>::max()) {}

  void UpdateTime(base::TimeTicks time);
  base::TimeTicks CurrentTime();

  // The HTML spec says that the clock for animations is only updated once per
  // rendering lifecycle, at the start. However the spec also assumes that the
  // user agent runs rendering lifecycles constantly, back-to-back. In Blink we
  // attempt to *not* run rendering lifecycles as much as possible, to avoid
  // unnecessary CPU usage.
  //
  // As such, when outside a rendering lifecycle (for example, if a setInterval
  // triggers) we allow the AnimationClock to dynamically adjust its time to
  // look like it is being updated by the rendering lifecycles that never
  // happened.
  //
  // TODO(crbug.com/995806): Allowing the AnimationClock to update itself is
  // error prone. We should instead get the latest impl-frame time from the
  // compositor when outside of a Blink rendering lifecycle (whilst still
  // not changing within the same task).
  void SetAllowedToDynamicallyUpdateTime(bool can_dynamically_update_time) {
    can_dynamically_update_time_ = can_dynamically_update_time;
  }

  // When using our dynamically update behavior outside rendering lifecycles, we
  // still do not want the time to move forward within the same task (e.g.
  // within a single setInterval callback). To achieve this we track the task in
  // which the time was last updated, and don't update it again until we are in
  // a new task.
  static void NotifyTaskStart() { ++currently_running_task_; }

  void ResetTimeForTesting();
  // The caller owns the passed in clock, which must outlive the AnimationClock.
  void OverrideDynamicClockForTesting(const base::TickClock*);

 private:
  base::TimeTicks time_;

  // See |SetAllowedToDynamicallyUpdateTime| documentation for these members.
  bool can_dynamically_update_time_;
  const base::TickClock* clock_;

  // See |NotifyTaskStart| documentation for these members.
  unsigned task_for_which_time_was_calculated_;
  static unsigned currently_running_task_;

  DISALLOW_COPY_AND_ASSIGN(AnimationClock);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_ANIMATION_CLOCK_H_