blob: 2c8a723e5bb970d0799e1a802536ad5e6715b95a [file] [log] [blame]
reed76113a92015-02-02 12:55:02 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkTime.h"
9
10#ifndef SkAnimTimer_DEFINED
11#define SkAnimTimer_DEFINED
12
13/**
14 * Class to track a "timer". It supports 3 states: stopped, paused, running.
15 *
16 * The caller must call updateTime() to resync with the clock (typically just before
17 * using the timer). Forcing the caller to do this ensures that the timer's return values
18 * are consistent if called repeatedly, as they only reflect the time since the last
19 * calle to updateTimer().
20 */
21class SkAnimTimer {
22public:
23 enum State {
24 kStopped_State,
25 kPaused_State,
26 kRunning_State
27 };
28
29 /**
30 * Class begins in the "stopped" state.
31 */
benjaminwagnerec4d4d72016-03-25 12:59:53 -070032 SkAnimTimer() : fBaseTimeNanos(0), fCurrTimeNanos(0), fState(kStopped_State) {}
reed76113a92015-02-02 12:55:02 -080033
34 bool isStopped() const { return kStopped_State == fState; }
35 bool isRunning() const { return kRunning_State == fState; }
36 bool isPaused() const { return kPaused_State == fState; }
37
38 /**
39 * Stops the timer, and resets it, such that the next call to run or togglePauseResume
40 * will begin at time 0.
41 */
42 void stop() {
43 this->setState(kStopped_State);
44 }
45
46 /**
47 * If the timer is paused or stopped, it will resume (or start if it was stopped).
48 */
49 void run() {
50 this->setState(kRunning_State);
51 }
52
53 /**
54 * If the timer is stopped, this has no effect, else it toggles between paused and running.
55 */
56 void togglePauseResume() {
57 if (kRunning_State == fState) {
58 this->setState(kPaused_State);
59 } else {
60 this->setState(kRunning_State);
61 }
62 }
63
64 /**
65 * Call this each time you want to sample the clock for the timer. This is NOT done
66 * automatically, so that repeated calls to msec() or secs() will always return the
67 * same value.
68 *
69 * This may safely be called with the timer in any state.
70 */
71 void updateTime() {
72 if (kRunning_State == fState) {
benjaminwagnerec4d4d72016-03-25 12:59:53 -070073 fCurrTimeNanos = SkTime::GetNSecs();
reed76113a92015-02-02 12:55:02 -080074 }
75 }
76
77 /**
78 * Return the time in milliseconds the timer has been in the running state.
benjaminwagnerec4d4d72016-03-25 12:59:53 -070079 * Returns 0 if the timer is stopped. Behavior is undefined if the timer
80 * has been running longer than SK_MSecMax.
reed76113a92015-02-02 12:55:02 -080081 */
benjaminwagnerec4d4d72016-03-25 12:59:53 -070082 SkMSec msec() const {
83 const double msec = (fCurrTimeNanos - fBaseTimeNanos) * 1e-6;
84 SkASSERT(SK_MSecMax >= msec);
85 return static_cast<SkMSec>(msec);
86 }
reed76113a92015-02-02 12:55:02 -080087
88 /**
89 * Return the time in seconds the timer has been in the running state.
90 * Returns 0 if the timer is stopped.
91 */
benjaminwagnerec4d4d72016-03-25 12:59:53 -070092 double secs() const { return (fCurrTimeNanos - fBaseTimeNanos) * 1e-9; }
reed76113a92015-02-02 12:55:02 -080093
94 /**
95 * Return the time in seconds the timer has been in the running state,
96 * scaled by "speed" and (if not zero) mod by period.
97 * Returns 0 if the timer is stopped.
98 */
99 SkScalar scaled(SkScalar speed, SkScalar period = 0) const {
100 double value = this->secs() * speed;
101 if (period) {
102 value = ::fmod(value, SkScalarToDouble(period));
103 }
104 return SkDoubleToScalar(value);
105 }
106
107private:
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700108 double fBaseTimeNanos;
109 double fCurrTimeNanos;
reed76113a92015-02-02 12:55:02 -0800110 State fState;
111
112 void setState(State newState) {
113 switch (newState) {
114 case kStopped_State:
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700115 fBaseTimeNanos = fCurrTimeNanos = 0;
reed76113a92015-02-02 12:55:02 -0800116 fState = kStopped_State;
117 break;
118 case kPaused_State:
119 if (kRunning_State == fState) {
120 fState = kPaused_State;
121 } // else stay stopped or paused
122 break;
123 case kRunning_State:
124 switch (fState) {
125 case kStopped_State:
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700126 fBaseTimeNanos = fCurrTimeNanos = SkTime::GetNSecs();
reed76113a92015-02-02 12:55:02 -0800127 break;
128 case kPaused_State: {// they want "resume"
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700129 double now = SkTime::GetNSecs();
130 fBaseTimeNanos += now - fCurrTimeNanos;
131 fCurrTimeNanos = now;
reed76113a92015-02-02 12:55:02 -0800132 } break;
133 case kRunning_State:
134 break;
135 }
136 fState = kRunning_State;
137 break;
138 }
139 }
140};
141
142#endif