Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
TimeUtils.h
Go to the documentation of this file.
1// Copyright 2019 Google LLC
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef TimeUtils_DEFINED
6#define TimeUtils_DEFINED
7
10
11#include <cmath>
12
13namespace TimeUtils {
14 // Returns 0 if the timer is stopped. Behavior is undefined if the timer
15 // has been running longer than SK_MSecMax.
16 static inline SkMSec NanosToMSec(double nanos) {
17 const double msec = nanos * 1e-6;
18 SkASSERT(SK_MSecMax >= msec);
19 return static_cast<SkMSec>(msec);
20 }
21
22 static inline double NanosToSeconds(double nanos) {
23 return nanos * 1e-9;
24 }
25
26 // Return the time scaled by "speed" and (if not zero) mod by period.
27 static inline float Scaled(float time, float speed, float period = 0) {
28 double value = time * speed;
29 if (period) {
30 value = ::fmod(value, (double)(period));
31 }
32 return (float)value;
33 }
34
35 // Transitions from ends->mid->ends linearly over period time. The phase
36 // specifies a phase shift in time units.
37 static inline float PingPong(double time,
38 float period,
39 float phase,
40 float ends,
41 float mid) {
42 double value = ::fmod(time + phase, period);
43 double half = period / 2.0;
44 double diff = ::fabs(value - half);
45 return (float)(ends + (1.0 - diff / half) * (mid - ends));
46 }
47
48 static inline float SineWave(double time,
49 float periodInSecs,
50 float phaseInSecs,
51 float min,
52 float max) {
53 if (periodInSecs < 0.f) {
54 return (min + max) / 2.f;
55 }
56 double t = NanosToSeconds(time) + phaseInSecs;
57 t *= 2 * SK_FloatPI / periodInSecs;
58 float halfAmplitude = (max - min) / 2.f;
59 return halfAmplitude * std::sin(t) + halfAmplitude + min;
60 }
61} // namespace TimeUtils
62#endif
#define SkASSERT(cond)
Definition SkAssert.h:116
constexpr float SK_FloatPI
static constexpr SkMSec SK_MSecMax
Definition SkTypes.h:188
uint32_t SkMSec
Definition SkTypes.h:184
uint8_t value
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48
static float PingPong(double time, float period, float phase, float ends, float mid)
Definition TimeUtils.h:37
static float Scaled(float time, float speed, float period=0)
Definition TimeUtils.h:27
static float SineWave(double time, float periodInSecs, float phaseInSecs, float min, float max)
Definition TimeUtils.h:48
static double NanosToSeconds(double nanos)
Definition TimeUtils.h:22
static SkMSec NanosToMSec(double nanos)
Definition TimeUtils.h:16