41248071ac
gm, slides, and samples no longer need to know about the implementation details of AnimTimer. This virtual bool onAnimate(const AnimTimer&); becomes this: virtual bool onAnimate(double /*nanoseconds*/); which is much easier to reason about. AnimTimer itself is now part of viewer. Change-Id: Ib70bf7a0798b1991f25204ae84f70463cdbeb358 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/226838 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Hal Canary <halcanary@google.com>
48 lines
1.5 KiB
C++
48 lines
1.5 KiB
C++
// Copyright 2019 Google LLC
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef TimeUtils_DEFINED
|
|
#define TimeUtils_DEFINED
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include <cmath>
|
|
|
|
namespace TimeUtils {
|
|
// Returns 0 if the timer is stopped. Behavior is undefined if the timer
|
|
// has been running longer than SK_MSecMax.
|
|
static inline SkMSec NanosToMSec(double nanos) {
|
|
const double msec = nanos * 1e-6;
|
|
SkASSERT(SK_MSecMax >= msec);
|
|
return static_cast<SkMSec>(msec);
|
|
}
|
|
|
|
static inline double NanosToSeconds(double nanos) {
|
|
return nanos * 1e-9;
|
|
}
|
|
|
|
// Return the time scaled by "speed" and (if not zero) mod by period.
|
|
static inline float Scaled(float time, float speed, float period = 0) {
|
|
double value = time * speed;
|
|
if (period) {
|
|
value = ::fmod(value, (double)(period));
|
|
}
|
|
return (float)value;
|
|
}
|
|
|
|
// Transitions from ends->mid->ends linearly over period time. The phase
|
|
// specifies a phase shift in time units.
|
|
static inline float PingPong(double time,
|
|
float period,
|
|
float phase,
|
|
float ends,
|
|
float mid) {
|
|
double value = ::fmod(time + phase, period);
|
|
double half = period / 2.0;
|
|
double diff = ::fabs(value - half);
|
|
return (float)(ends + (1.0 - diff / half) * (mid - ends));
|
|
}
|
|
} // namespace TimeUtils
|
|
#endif
|