reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 21 | class SkAnimTimer { |
| 22 | public: |
| 23 | enum State { |
| 24 | kStopped_State, |
| 25 | kPaused_State, |
| 26 | kRunning_State |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * Class begins in the "stopped" state. |
| 31 | */ |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 32 | SkAnimTimer() : fBaseTimeNanos(0), fCurrTimeNanos(0), fState(kStopped_State) {} |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 33 | |
| 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) { |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 73 | fCurrTimeNanos = SkTime::GetNSecs(); |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return the time in milliseconds the timer has been in the running state. |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 79 | * Returns 0 if the timer is stopped. Behavior is undefined if the timer |
| 80 | * has been running longer than SK_MSecMax. |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 81 | */ |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 82 | SkMSec msec() const { |
| 83 | const double msec = (fCurrTimeNanos - fBaseTimeNanos) * 1e-6; |
| 84 | SkASSERT(SK_MSecMax >= msec); |
| 85 | return static_cast<SkMSec>(msec); |
| 86 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Return the time in seconds the timer has been in the running state. |
| 90 | * Returns 0 if the timer is stopped. |
| 91 | */ |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 92 | double secs() const { return (fCurrTimeNanos - fBaseTimeNanos) * 1e-9; } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 93 | |
| 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 | |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Transitions from ends->mid->ends linearly over period seconds. The phase specifies a phase |
| 109 | * shift in seconds. |
| 110 | */ |
| 111 | SkScalar pingPong(SkScalar period, SkScalar phase, SkScalar ends, SkScalar mid) const { |
| 112 | return PingPong(this->secs(), period, phase, ends, mid); |
| 113 | } |
| 114 | |
| 115 | /** Helper for computing a ping-pong value without a SkAnimTimer object. */ |
| 116 | static SkScalar PingPong(double t, SkScalar period, SkScalar phase, SkScalar ends, |
| 117 | SkScalar mid) { |
| 118 | double value = ::fmod(t + phase, period); |
| 119 | double half = period / 2.0; |
| 120 | double diff = ::fabs(value - half); |
| 121 | return SkDoubleToScalar(ends + (1.0 - diff / half) * (mid - ends)); |
| 122 | } |
| 123 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 124 | private: |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 125 | double fBaseTimeNanos; |
| 126 | double fCurrTimeNanos; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 127 | State fState; |
| 128 | |
| 129 | void setState(State newState) { |
| 130 | switch (newState) { |
| 131 | case kStopped_State: |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 132 | fBaseTimeNanos = fCurrTimeNanos = 0; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 133 | fState = kStopped_State; |
| 134 | break; |
| 135 | case kPaused_State: |
| 136 | if (kRunning_State == fState) { |
| 137 | fState = kPaused_State; |
| 138 | } // else stay stopped or paused |
| 139 | break; |
| 140 | case kRunning_State: |
| 141 | switch (fState) { |
| 142 | case kStopped_State: |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 143 | fBaseTimeNanos = fCurrTimeNanos = SkTime::GetNSecs(); |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 144 | break; |
| 145 | case kPaused_State: {// they want "resume" |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 146 | double now = SkTime::GetNSecs(); |
| 147 | fBaseTimeNanos += now - fCurrTimeNanos; |
| 148 | fCurrTimeNanos = now; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 149 | } break; |
| 150 | case kRunning_State: |
| 151 | break; |
| 152 | } |
| 153 | fState = kRunning_State; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | #endif |