Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 1 | /* |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 2 | * Copyright 2020 The Android Open Source Project |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 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 <ostream> |
| 21 | #include <string> |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 22 | #include <type_traits> |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 23 | |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <utils/Timers.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 29 | // Frames per second, stored as floating-point frequency. Provides conversion from/to period in |
| 30 | // nanoseconds, and relational operators with precision threshold. |
| 31 | // |
| 32 | // const Fps fps = 60_Hz; |
| 33 | // |
| 34 | // using namespace fps_approx_ops; |
| 35 | // assert(fps == Fps::fromPeriodNsecs(16'666'667)); |
| 36 | // |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 37 | class Fps { |
| 38 | public: |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 39 | constexpr Fps() = default; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 40 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 41 | static constexpr Fps fromValue(float frequency) { |
| 42 | return frequency > 0.f ? Fps(frequency, static_cast<nsecs_t>(1e9f / frequency)) : Fps(); |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 45 | static constexpr Fps fromPeriodNsecs(nsecs_t period) { |
| 46 | return period > 0 ? Fps(1e9f / period, period) : Fps(); |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 49 | constexpr bool isValid() const { return mFrequency > 0.f; } |
| 50 | |
| 51 | constexpr float getValue() const { return mFrequency; } |
| 52 | int getIntValue() const { return static_cast<int>(std::round(mFrequency)); } |
| 53 | |
| 54 | constexpr nsecs_t getPeriodNsecs() const { return mPeriod; } |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 55 | |
| 56 | private: |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 57 | constexpr Fps(float frequency, nsecs_t period) : mFrequency(frequency), mPeriod(period) {} |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 58 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 59 | float mFrequency = 0.f; |
| 60 | nsecs_t mPeriod = 0; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static_assert(std::is_trivially_copyable_v<Fps>); |
| 64 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 65 | constexpr Fps operator""_Hz(unsigned long long frequency) { |
| 66 | return Fps::fromValue(static_cast<float>(frequency)); |
| 67 | } |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 68 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 69 | constexpr Fps operator""_Hz(long double frequency) { |
| 70 | return Fps::fromValue(static_cast<float>(frequency)); |
| 71 | } |
| 72 | |
| 73 | inline bool isStrictlyLess(Fps lhs, Fps rhs) { |
| 74 | return lhs.getValue() < rhs.getValue(); |
| 75 | } |
| 76 | |
| 77 | // Does not satisfy equivalence relation. |
| 78 | inline bool isApproxEqual(Fps lhs, Fps rhs) { |
| 79 | // TODO(b/185536303): Replace with ULP distance. |
| 80 | return std::abs(lhs.getValue() - rhs.getValue()) < 0.001f; |
| 81 | } |
| 82 | |
| 83 | // Does not satisfy strict weak order. |
| 84 | inline bool isApproxLess(Fps lhs, Fps rhs) { |
| 85 | return isStrictlyLess(lhs, rhs) && !isApproxEqual(lhs, rhs); |
| 86 | } |
| 87 | |
| 88 | namespace fps_approx_ops { |
| 89 | |
| 90 | inline bool operator==(Fps lhs, Fps rhs) { |
| 91 | return isApproxEqual(lhs, rhs); |
| 92 | } |
| 93 | |
| 94 | inline bool operator<(Fps lhs, Fps rhs) { |
| 95 | return isApproxLess(lhs, rhs); |
| 96 | } |
| 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(rhs, lhs); |
| 104 | } |
| 105 | |
| 106 | inline bool operator<=(Fps lhs, Fps rhs) { |
| 107 | return !isApproxLess(rhs, lhs); |
| 108 | } |
| 109 | |
| 110 | inline bool operator>=(Fps lhs, Fps rhs) { |
| 111 | return !isApproxLess(lhs, rhs); |
| 112 | } |
| 113 | |
| 114 | } // namespace fps_approx_ops |
| 115 | |
| 116 | struct FpsApproxEqual { |
| 117 | bool operator()(Fps lhs, Fps rhs) const { return isApproxEqual(lhs, rhs); } |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 118 | }; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 119 | |
| 120 | inline std::string to_string(Fps fps) { |
| 121 | return base::StringPrintf("%.2f Hz", fps.getValue()); |
| 122 | } |
| 123 | |
| 124 | inline std::ostream& operator<<(std::ostream& stream, Fps fps) { |
| 125 | return stream << to_string(fps); |
| 126 | } |
| 127 | |
| 128 | } // namespace android |