| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
|---|---|
| 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 FLUTTER_FML_TIME_TIME_DELTA_H_ |
| 6 | #define FLUTTER_FML_TIME_TIME_DELTA_H_ |
| 7 | |
| 8 | #include <chrono> |
| 9 | #include <cstdint> |
| 10 | #include <ctime> |
| 11 | #include <iosfwd> |
| 12 | #include <limits> |
| 13 | |
| 14 | namespace fml { |
| 15 | |
| 16 | using namespace std::chrono_literals; |
| 17 | |
| 18 | using Milliseconds = std::chrono::duration<double, std::milli>; |
| 19 | |
| 20 | // Default to 60fps. |
| 21 | constexpr Milliseconds kDefaultFrameBudget = Milliseconds(1s) / 60; |
| 22 | |
| 23 | template <typename T> |
| 24 | Milliseconds RefreshRateToFrameBudget(T refresh_rate) { |
| 25 | return Milliseconds(1s) / refresh_rate; |
| 26 | } |
| 27 | |
| 28 | // A TimeDelta represents the difference between two time points. |
| 29 | class TimeDelta { |
| 30 | public: |
| 31 | constexpr TimeDelta() = default; |
| 32 | |
| 33 | static constexpr TimeDelta Zero() { return TimeDelta(); } |
| 34 | static constexpr TimeDelta Min() { |
| 35 | return TimeDelta(std::numeric_limits<int64_t>::min()); |
| 36 | } |
| 37 | static constexpr TimeDelta Max() { |
| 38 | return TimeDelta(std::numeric_limits<int64_t>::max()); |
| 39 | } |
| 40 | static constexpr TimeDelta FromNanoseconds(int64_t nanos) { |
| 41 | return TimeDelta(nanos); |
| 42 | } |
| 43 | static constexpr TimeDelta FromMicroseconds(int64_t micros) { |
| 44 | return FromNanoseconds(nanos: micros * 1000); |
| 45 | } |
| 46 | static constexpr TimeDelta FromMilliseconds(int64_t millis) { |
| 47 | return FromMicroseconds(micros: millis * 1000); |
| 48 | } |
| 49 | static constexpr TimeDelta FromSeconds(int64_t seconds) { |
| 50 | return FromMilliseconds(millis: seconds * 1000); |
| 51 | } |
| 52 | |
| 53 | static constexpr TimeDelta FromSecondsF(double seconds) { |
| 54 | return FromNanoseconds(nanos: seconds * (1000.0 * 1000.0 * 1000.0)); |
| 55 | } |
| 56 | |
| 57 | static constexpr TimeDelta FromMillisecondsF(double millis) { |
| 58 | return FromNanoseconds(nanos: millis * (1000.0 * 1000.0)); |
| 59 | } |
| 60 | |
| 61 | constexpr int64_t ToNanoseconds() const { return delta_; } |
| 62 | constexpr int64_t ToMicroseconds() const { return ToNanoseconds() / 1000; } |
| 63 | constexpr int64_t ToMilliseconds() const { return ToMicroseconds() / 1000; } |
| 64 | constexpr int64_t ToSeconds() const { return ToMilliseconds() / 1000; } |
| 65 | |
| 66 | constexpr double ToNanosecondsF() const { return delta_; } |
| 67 | constexpr double ToMicrosecondsF() const { return delta_ / 1000.0; } |
| 68 | constexpr double ToMillisecondsF() const { |
| 69 | return delta_ / (1000.0 * 1000.0); |
| 70 | } |
| 71 | constexpr double ToSecondsF() const { |
| 72 | return delta_ / (1000.0 * 1000.0 * 1000.0); |
| 73 | } |
| 74 | |
| 75 | constexpr TimeDelta operator-(TimeDelta other) const { |
| 76 | return TimeDelta::FromNanoseconds(nanos: delta_ - other.delta_); |
| 77 | } |
| 78 | |
| 79 | constexpr TimeDelta operator+(TimeDelta other) const { |
| 80 | return TimeDelta::FromNanoseconds(nanos: delta_ + other.delta_); |
| 81 | } |
| 82 | |
| 83 | constexpr TimeDelta operator/(int64_t divisor) const { |
| 84 | return TimeDelta::FromNanoseconds(nanos: delta_ / divisor); |
| 85 | } |
| 86 | |
| 87 | constexpr int64_t operator/(TimeDelta other) const { |
| 88 | return delta_ / other.delta_; |
| 89 | } |
| 90 | |
| 91 | constexpr TimeDelta operator*(int64_t multiplier) const { |
| 92 | return TimeDelta::FromNanoseconds(nanos: delta_ * multiplier); |
| 93 | } |
| 94 | |
| 95 | constexpr TimeDelta operator%(TimeDelta other) const { |
| 96 | return TimeDelta::FromNanoseconds(nanos: delta_ % other.delta_); |
| 97 | } |
| 98 | |
| 99 | bool operator==(TimeDelta other) const { return delta_ == other.delta_; } |
| 100 | bool operator!=(TimeDelta other) const { return delta_ != other.delta_; } |
| 101 | bool operator<(TimeDelta other) const { return delta_ < other.delta_; } |
| 102 | bool operator<=(TimeDelta other) const { return delta_ <= other.delta_; } |
| 103 | bool operator>(TimeDelta other) const { return delta_ > other.delta_; } |
| 104 | bool operator>=(TimeDelta other) const { return delta_ >= other.delta_; } |
| 105 | |
| 106 | static constexpr TimeDelta FromTimespec(struct timespec ts) { |
| 107 | return TimeDelta::FromSeconds(seconds: ts.tv_sec) + |
| 108 | TimeDelta::FromNanoseconds(nanos: ts.tv_nsec); |
| 109 | } |
| 110 | struct timespec ToTimespec() { |
| 111 | struct timespec ts; |
| 112 | constexpr int64_t kNanosecondsPerSecond = 1000000000ll; |
| 113 | ts.tv_sec = static_cast<time_t>(ToSeconds()); |
| 114 | ts.tv_nsec = delta_ % kNanosecondsPerSecond; |
| 115 | return ts; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | // Private, use one of the FromFoo() types |
| 120 | explicit constexpr TimeDelta(int64_t delta) : delta_(delta) {} |
| 121 | |
| 122 | int64_t delta_ = 0; |
| 123 | }; |
| 124 | |
| 125 | } // namespace fml |
| 126 | |
| 127 | #endif // FLUTTER_FML_TIME_TIME_DELTA_H_ |
| 128 |
