Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
time_delta.h
Go to the documentation of this file.
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
14namespace fml {
15
16using namespace std::chrono_literals;
17
18using Milliseconds = std::chrono::duration<double, std::milli>;
19
20// Default to 60fps.
22
23template <typename T>
25 return Milliseconds(1s) / refresh_rate;
26}
27
28// A TimeDelta represents the difference between two time points.
29class 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(micros * 1000);
45 }
46 static constexpr TimeDelta FromMilliseconds(int64_t millis) {
47 return FromMicroseconds(millis * 1000);
48 }
49 static constexpr TimeDelta FromSeconds(int64_t seconds) {
50 return FromMilliseconds(seconds * 1000);
51 }
52
53 static constexpr TimeDelta FromSecondsF(double seconds) {
54 return FromNanoseconds(seconds * (1000.0 * 1000.0 * 1000.0));
55 }
56
57 static constexpr TimeDelta FromMillisecondsF(double millis) {
58 return FromNanoseconds(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(delta_ - other.delta_);
77 }
78
79 constexpr TimeDelta operator+(TimeDelta other) const {
80 return TimeDelta::FromNanoseconds(delta_ + other.delta_);
81 }
82
83 constexpr TimeDelta operator/(int64_t divisor) const {
84 return TimeDelta::FromNanoseconds(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(delta_ * multiplier);
93 }
94
95 constexpr TimeDelta operator%(TimeDelta other) const {
96 return TimeDelta::FromNanoseconds(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(ts.tv_sec) +
108 TimeDelta::FromNanoseconds(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_
struct timespec ToTimespec()
Definition time_delta.h:110
bool operator<(TimeDelta other) const
Definition time_delta.h:101
static constexpr TimeDelta FromSeconds(int64_t seconds)
Definition time_delta.h:49
constexpr TimeDelta operator%(TimeDelta other) const
Definition time_delta.h:95
constexpr double ToNanosecondsF() const
Definition time_delta.h:66
constexpr double ToMicrosecondsF() const
Definition time_delta.h:67
static constexpr TimeDelta FromTimespec(struct timespec ts)
Definition time_delta.h:106
constexpr int64_t ToMicroseconds() const
Definition time_delta.h:62
bool operator!=(TimeDelta other) const
Definition time_delta.h:100
bool operator<=(TimeDelta other) const
Definition time_delta.h:102
static constexpr TimeDelta FromMillisecondsF(double millis)
Definition time_delta.h:57
constexpr TimeDelta operator+(TimeDelta other) const
Definition time_delta.h:79
bool operator>=(TimeDelta other) const
Definition time_delta.h:104
static constexpr TimeDelta FromNanoseconds(int64_t nanos)
Definition time_delta.h:40
constexpr TimeDelta operator/(int64_t divisor) const
Definition time_delta.h:83
constexpr TimeDelta operator*(int64_t multiplier) const
Definition time_delta.h:91
static constexpr TimeDelta FromSecondsF(double seconds)
Definition time_delta.h:53
constexpr double ToSecondsF() const
Definition time_delta.h:71
constexpr int64_t ToMilliseconds() const
Definition time_delta.h:63
constexpr double ToMillisecondsF() const
Definition time_delta.h:68
constexpr TimeDelta()=default
static constexpr TimeDelta FromMilliseconds(int64_t millis)
Definition time_delta.h:46
constexpr int64_t ToNanoseconds() const
Definition time_delta.h:61
constexpr int64_t operator/(TimeDelta other) const
Definition time_delta.h:87
bool operator>(TimeDelta other) const
Definition time_delta.h:103
static constexpr TimeDelta Min()
Definition time_delta.h:34
constexpr TimeDelta operator-(TimeDelta other) const
Definition time_delta.h:75
static constexpr TimeDelta Max()
Definition time_delta.h:37
constexpr int64_t ToSeconds() const
Definition time_delta.h:64
bool operator==(TimeDelta other) const
Definition time_delta.h:99
static constexpr TimeDelta FromMicroseconds(int64_t micros)
Definition time_delta.h:43
static constexpr TimeDelta Zero()
Definition time_delta.h:33
struct MyStruct s
std::chrono::duration< double, std::milli > Milliseconds
Definition time_delta.h:18
constexpr Milliseconds kDefaultFrameBudget
Definition time_delta.h:21
Milliseconds RefreshRateToFrameBudget(T refresh_rate)
Definition time_delta.h:24