1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <cmath>
20 #include <limits>
21 #include <ostream>
22 #include <string>
23 #include <type_traits>
24
25 #include <android-base/stringprintf.h>
26 #include <utils/Timers.h>
27
28 namespace android {
29
30 // Frames per second, stored as floating-point frequency. Provides conversion from/to period in
31 // nanoseconds, and relational operators with precision threshold.
32 //
33 // const Fps fps = 60_Hz;
34 //
35 // using namespace fps_approx_ops;
36 // assert(fps == Fps::fromPeriodNsecs(16'666'667));
37 //
38 class Fps {
39 public:
40 constexpr Fps() = default;
41
fromValue(float frequency)42 static constexpr Fps fromValue(float frequency) {
43 return frequency > 0.f ? Fps(frequency, static_cast<nsecs_t>(1e9f / frequency)) : Fps();
44 }
45
fromPeriodNsecs(nsecs_t period)46 static constexpr Fps fromPeriodNsecs(nsecs_t period) {
47 return period > 0 ? Fps(1e9f / period, period) : Fps();
48 }
49
isValid()50 constexpr bool isValid() const { return mFrequency > 0.f; }
51
getValue()52 constexpr float getValue() const { return mFrequency; }
getIntValue()53 int getIntValue() const { return static_cast<int>(std::round(mFrequency)); }
54
getPeriodNsecs()55 constexpr nsecs_t getPeriodNsecs() const { return mPeriod; }
56
57 private:
Fps(float frequency,nsecs_t period)58 constexpr Fps(float frequency, nsecs_t period) : mFrequency(frequency), mPeriod(period) {}
59
60 float mFrequency = 0.f;
61 nsecs_t mPeriod = 0;
62 };
63
64 struct FpsRange {
65 Fps min = Fps::fromValue(0.f);
66 Fps max = Fps::fromValue(std::numeric_limits<float>::max());
67
68 bool includes(Fps) const;
69 };
70
71 static_assert(std::is_trivially_copyable_v<Fps>);
72
73 constexpr Fps operator""_Hz(unsigned long long frequency) {
74 return Fps::fromValue(static_cast<float>(frequency));
75 }
76
77 constexpr Fps operator""_Hz(long double frequency) {
78 return Fps::fromValue(static_cast<float>(frequency));
79 }
80
isStrictlyLess(Fps lhs,Fps rhs)81 inline bool isStrictlyLess(Fps lhs, Fps rhs) {
82 return lhs.getValue() < rhs.getValue();
83 }
84
85 // Does not satisfy equivalence relation.
isApproxEqual(Fps lhs,Fps rhs)86 inline bool isApproxEqual(Fps lhs, Fps rhs) {
87 // TODO(b/185536303): Replace with ULP distance.
88 return std::abs(lhs.getValue() - rhs.getValue()) < 0.001f;
89 }
90
91 // Does not satisfy strict weak order.
isApproxLess(Fps lhs,Fps rhs)92 inline bool isApproxLess(Fps lhs, Fps rhs) {
93 return isStrictlyLess(lhs, rhs) && !isApproxEqual(lhs, rhs);
94 }
95
96 namespace fps_approx_ops {
97
98 inline bool operator==(Fps lhs, Fps rhs) {
99 return isApproxEqual(lhs, rhs);
100 }
101
102 inline bool operator<(Fps lhs, Fps rhs) {
103 return isApproxLess(lhs, rhs);
104 }
105
106 inline bool operator!=(Fps lhs, Fps rhs) {
107 return !isApproxEqual(lhs, rhs);
108 }
109
110 inline bool operator>(Fps lhs, Fps rhs) {
111 return isApproxLess(rhs, lhs);
112 }
113
114 inline bool operator<=(Fps lhs, Fps rhs) {
115 return !isApproxLess(rhs, lhs);
116 }
117
118 inline bool operator>=(Fps lhs, Fps rhs) {
119 return !isApproxLess(lhs, rhs);
120 }
121
122 inline bool operator==(FpsRange lhs, FpsRange rhs) {
123 return isApproxEqual(lhs.min, rhs.min) && isApproxEqual(lhs.max, rhs.max);
124 }
125
126 inline bool operator!=(FpsRange lhs, FpsRange rhs) {
127 return !(lhs == rhs);
128 }
129
130 } // namespace fps_approx_ops
131
includes(Fps fps)132 inline bool FpsRange::includes(Fps fps) const {
133 using fps_approx_ops::operator<=;
134 return min <= fps && fps <= max;
135 }
136
137 struct FpsApproxEqual {
operatorFpsApproxEqual138 bool operator()(Fps lhs, Fps rhs) const { return isApproxEqual(lhs, rhs); }
139 };
140
to_string(Fps fps)141 inline std::string to_string(Fps fps) {
142 return base::StringPrintf("%.2f Hz", fps.getValue());
143 }
144
145 inline std::ostream& operator<<(std::ostream& stream, Fps fps) {
146 return stream << to_string(fps);
147 }
148
to_string(FpsRange range)149 inline std::string to_string(FpsRange range) {
150 const auto [min, max] = range;
151 return base::StringPrintf("[%s, %s]", to_string(min).c_str(), to_string(max).c_str());
152 }
153
154 } // namespace android
155