OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkTime.h" | 8 #include "SkTime.h" |
9 | 9 |
10 #ifndef SkAnimTimer_DEFINED | 10 #ifndef SkAnimTimer_DEFINED |
(...skipping 11 matching lines...) Expand all Loading... |
22 public: | 22 public: |
23 enum State { | 23 enum State { |
24 kStopped_State, | 24 kStopped_State, |
25 kPaused_State, | 25 kPaused_State, |
26 kRunning_State | 26 kRunning_State |
27 }; | 27 }; |
28 | 28 |
29 /** | 29 /** |
30 * Class begins in the "stopped" state. | 30 * Class begins in the "stopped" state. |
31 */ | 31 */ |
32 SkAnimTimer() : fBaseTime(0), fCurrTime(0), fState(kStopped_State) {} | 32 SkAnimTimer() : fBaseTimeNanos(0), fCurrTimeNanos(0), fState(kStopped_State)
{} |
33 | 33 |
34 bool isStopped() const { return kStopped_State == fState; } | 34 bool isStopped() const { return kStopped_State == fState; } |
35 bool isRunning() const { return kRunning_State == fState; } | 35 bool isRunning() const { return kRunning_State == fState; } |
36 bool isPaused() const { return kPaused_State == fState; } | 36 bool isPaused() const { return kPaused_State == fState; } |
37 | 37 |
38 /** | 38 /** |
39 * Stops the timer, and resets it, such that the next call to run or toggle
PauseResume | 39 * Stops the timer, and resets it, such that the next call to run or toggle
PauseResume |
40 * will begin at time 0. | 40 * will begin at time 0. |
41 */ | 41 */ |
42 void stop() { | 42 void stop() { |
(...skipping 20 matching lines...) Expand all Loading... |
63 | 63 |
64 /** | 64 /** |
65 * Call this each time you want to sample the clock for the timer. This is
NOT done | 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 re
turn the | 66 * automatically, so that repeated calls to msec() or secs() will always re
turn the |
67 * same value. | 67 * same value. |
68 * | 68 * |
69 * This may safely be called with the timer in any state. | 69 * This may safely be called with the timer in any state. |
70 */ | 70 */ |
71 void updateTime() { | 71 void updateTime() { |
72 if (kRunning_State == fState) { | 72 if (kRunning_State == fState) { |
73 fCurrTime = SkTime::GetMSecs(); | 73 fCurrTimeNanos = SkTime::GetNSecs(); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 /** | 77 /** |
78 * Return the time in milliseconds the timer has been in the running state. | 78 * Return the time in milliseconds the timer has been in the running state. |
79 * Returns 0 if the timer is stopped. | 79 * Returns 0 if the timer is stopped. Behavior is undefined if the timer |
| 80 * has been running longer than SK_MSecMax. |
80 */ | 81 */ |
81 SkMSec msec() const { return fCurrTime - fBaseTime; } | 82 SkMSec msec() const { |
| 83 const double msec = (fCurrTimeNanos - fBaseTimeNanos) * 1e-6; |
| 84 SkASSERT(SK_MSecMax >= msec); |
| 85 return static_cast<SkMSec>(msec); |
| 86 } |
82 | 87 |
83 /** | 88 /** |
84 * Return the time in seconds the timer has been in the running state. | 89 * Return the time in seconds the timer has been in the running state. |
85 * Returns 0 if the timer is stopped. | 90 * Returns 0 if the timer is stopped. |
86 */ | 91 */ |
87 double secs() const { | 92 double secs() const { return (fCurrTimeNanos - fBaseTimeNanos) * 1e-9; } |
88 return this->msec() * 0.001; | |
89 } | |
90 | 93 |
91 /** | 94 /** |
92 * Return the time in seconds the timer has been in the running state, | 95 * Return the time in seconds the timer has been in the running state, |
93 * scaled by "speed" and (if not zero) mod by period. | 96 * scaled by "speed" and (if not zero) mod by period. |
94 * Returns 0 if the timer is stopped. | 97 * Returns 0 if the timer is stopped. |
95 */ | 98 */ |
96 SkScalar scaled(SkScalar speed, SkScalar period = 0) const { | 99 SkScalar scaled(SkScalar speed, SkScalar period = 0) const { |
97 double value = this->secs() * speed; | 100 double value = this->secs() * speed; |
98 if (period) { | 101 if (period) { |
99 value = ::fmod(value, SkScalarToDouble(period)); | 102 value = ::fmod(value, SkScalarToDouble(period)); |
100 } | 103 } |
101 return SkDoubleToScalar(value); | 104 return SkDoubleToScalar(value); |
102 } | 105 } |
103 | 106 |
104 private: | 107 private: |
105 SkMSec fBaseTime; | 108 double fBaseTimeNanos; |
106 SkMSec fCurrTime; | 109 double fCurrTimeNanos; |
107 State fState; | 110 State fState; |
108 | 111 |
109 void setState(State newState) { | 112 void setState(State newState) { |
110 switch (newState) { | 113 switch (newState) { |
111 case kStopped_State: | 114 case kStopped_State: |
112 fBaseTime = fCurrTime = 0; | 115 fBaseTimeNanos = fCurrTimeNanos = 0; |
113 fState = kStopped_State; | 116 fState = kStopped_State; |
114 break; | 117 break; |
115 case kPaused_State: | 118 case kPaused_State: |
116 if (kRunning_State == fState) { | 119 if (kRunning_State == fState) { |
117 fState = kPaused_State; | 120 fState = kPaused_State; |
118 } // else stay stopped or paused | 121 } // else stay stopped or paused |
119 break; | 122 break; |
120 case kRunning_State: | 123 case kRunning_State: |
121 switch (fState) { | 124 switch (fState) { |
122 case kStopped_State: | 125 case kStopped_State: |
123 fBaseTime = fCurrTime = SkTime::GetMSecs(); | 126 fBaseTimeNanos = fCurrTimeNanos = SkTime::GetNSecs(); |
124 break; | 127 break; |
125 case kPaused_State: {// they want "resume" | 128 case kPaused_State: {// they want "resume" |
126 SkMSec now = SkTime::GetMSecs(); | 129 double now = SkTime::GetNSecs(); |
127 fBaseTime += now - fCurrTime; | 130 fBaseTimeNanos += now - fCurrTimeNanos; |
128 fCurrTime = now; | 131 fCurrTimeNanos = now; |
129 } break; | 132 } break; |
130 case kRunning_State: | 133 case kRunning_State: |
131 break; | 134 break; |
132 } | 135 } |
133 fState = kRunning_State; | 136 fState = kRunning_State; |
134 break; | 137 break; |
135 } | 138 } |
136 } | 139 } |
137 }; | 140 }; |
138 | 141 |
139 #endif | 142 #endif |
OLD | NEW |