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 <ftl/enum.h>
27 #include <scheduler/Time.h>
28
29 namespace android {
30
31 // Frames per second, stored as floating-point frequency. Provides conversion from/to period in
32 // nanoseconds, and relational operators with precision threshold.
33 //
34 // const Fps fps = 60_Hz;
35 //
36 // using namespace fps_approx_ops;
37 // assert(fps == Fps::fromPeriodNsecs(16'666'667));
38 //
39 class Fps {
40 public:
41 constexpr Fps() = default;
42
fromValue(float frequency)43 static constexpr Fps fromValue(float frequency) {
44 return frequency > 0.f ? Fps(frequency, static_cast<nsecs_t>(1e9f / frequency)) : Fps();
45 }
46
fromPeriodNsecs(nsecs_t period)47 static constexpr Fps fromPeriodNsecs(nsecs_t period) {
48 return period > 0 ? Fps(1e9f / period, period) : Fps();
49 }
50
isValid()51 constexpr bool isValid() const { return mFrequency > 0.f; }
52
getValue()53 constexpr float getValue() const { return mFrequency; }
getIntValue()54 int getIntValue() const { return static_cast<int>(std::round(mFrequency)); }
55
getPeriod()56 constexpr Period getPeriod() const { return Period::fromNs(mPeriod); }
getPeriodNsecs()57 constexpr nsecs_t getPeriodNsecs() const { return mPeriod; }
58
59 private:
Fps(float frequency,nsecs_t period)60 constexpr Fps(float frequency, nsecs_t period) : mFrequency(frequency), mPeriod(period) {}
61
62 float mFrequency = 0.f;
63 nsecs_t mPeriod = 0;
64 };
65
66 struct FpsRange {
67 Fps min = Fps::fromValue(0.f);
68 Fps max = Fps::fromValue(std::numeric_limits<float>::max());
69
70 bool includes(Fps) const;
71 bool includes(FpsRange) const;
72 };
73
74 struct FpsRanges {
75 // The range of refresh rates that refers to the display mode setting.
76 FpsRange physical;
77
78 // the range of frame rates that refers to the render rate, which is
79 // the rate that frames are swapped.
80 FpsRange render;
81
82 bool valid() const;
83 };
84
85 // The frame rate category of a Layer.
86 enum class FrameRateCategory : int32_t {
87 Default,
88 NoPreference,
89 Low,
90 Normal,
91 HighHint,
92 High,
93
94 ftl_last = High
95 };
96
97 static_assert(std::is_trivially_copyable_v<Fps>);
98
99 constexpr Fps operator""_Hz(unsigned long long frequency) {
100 return Fps::fromValue(static_cast<float>(frequency));
101 }
102
103 constexpr Fps operator""_Hz(long double frequency) {
104 return Fps::fromValue(static_cast<float>(frequency));
105 }
106
isStrictlyLess(Fps lhs,Fps rhs)107 inline bool isStrictlyLess(Fps lhs, Fps rhs) {
108 return lhs.getValue() < rhs.getValue();
109 }
110
111 // Does not satisfy equivalence relation.
isApproxEqual(Fps lhs,Fps rhs)112 inline bool isApproxEqual(Fps lhs, Fps rhs) {
113 // TODO(b/185536303): Replace with ULP distance.
114 return std::abs(lhs.getValue() - rhs.getValue()) < 0.001f;
115 }
116
117 // Does not satisfy strict weak order.
isApproxLess(Fps lhs,Fps rhs)118 inline bool isApproxLess(Fps lhs, Fps rhs) {
119 return isStrictlyLess(lhs, rhs) && !isApproxEqual(lhs, rhs);
120 }
121
122 namespace fps_approx_ops {
123
124 inline bool operator==(Fps lhs, Fps rhs) {
125 return isApproxEqual(lhs, rhs);
126 }
127
128 inline bool operator<(Fps lhs, Fps rhs) {
129 return isApproxLess(lhs, rhs);
130 }
131
132 inline bool operator!=(Fps lhs, Fps rhs) {
133 return !isApproxEqual(lhs, rhs);
134 }
135
136 inline bool operator>(Fps lhs, Fps rhs) {
137 return isApproxLess(rhs, lhs);
138 }
139
140 inline bool operator<=(Fps lhs, Fps rhs) {
141 return !isApproxLess(rhs, lhs);
142 }
143
144 inline bool operator>=(Fps lhs, Fps rhs) {
145 return !isApproxLess(lhs, rhs);
146 }
147
148 inline bool operator==(FpsRange lhs, FpsRange rhs) {
149 return isApproxEqual(lhs.min, rhs.min) && isApproxEqual(lhs.max, rhs.max);
150 }
151
152 inline bool operator!=(FpsRange lhs, FpsRange rhs) {
153 return !(lhs == rhs);
154 }
155
156 inline bool operator==(const FpsRanges& lhs, const FpsRanges& rhs) {
157 return lhs.physical == rhs.physical && lhs.render == rhs.render;
158 }
159
160 inline bool operator!=(const FpsRanges& lhs, const FpsRanges& rhs) {
161 return !(lhs == rhs);
162 }
163
164 inline unsigned operator/(Fps lhs, Fps rhs) {
165 return static_cast<unsigned>(std::ceil(lhs.getValue() / rhs.getValue()));
166 }
167
168 } // namespace fps_approx_ops
169
170 constexpr Fps operator/(Fps fps, unsigned divisor) {
171 return Fps::fromPeriodNsecs(fps.getPeriodNsecs() * static_cast<nsecs_t>(divisor));
172 }
173
includes(Fps fps)174 inline bool FpsRange::includes(Fps fps) const {
175 using fps_approx_ops::operator<=;
176 return min <= fps && fps <= max;
177 }
178
includes(FpsRange range)179 inline bool FpsRange::includes(FpsRange range) const {
180 using namespace fps_approx_ops;
181 return min <= range.min && max >= range.max;
182 }
183
valid()184 inline bool FpsRanges::valid() const {
185 using fps_approx_ops::operator>=;
186 return physical.max >= render.max;
187 }
188
189 struct FpsApproxEqual {
operatorFpsApproxEqual190 bool operator()(Fps lhs, Fps rhs) const { return isApproxEqual(lhs, rhs); }
191 };
192
to_string(Fps fps)193 inline std::string to_string(Fps fps) {
194 return base::StringPrintf("%.2f Hz", fps.getValue());
195 }
196
197 inline std::ostream& operator<<(std::ostream& stream, Fps fps) {
198 return stream << to_string(fps);
199 }
200
to_string(FpsRange range)201 inline std::string to_string(FpsRange range) {
202 const auto [min, max] = range;
203 return base::StringPrintf("[%s, %s]", to_string(min).c_str(), to_string(max).c_str());
204 }
205
to_string(FpsRanges ranges)206 inline std::string to_string(FpsRanges ranges) {
207 const auto& [physical, render] = ranges;
208 return base::StringPrintf("{physical=%s, render=%s}", to_string(physical).c_str(),
209 to_string(render).c_str());
210 }
211
212 } // namespace android
213