1 /*
2 * Copyright (C) 2024 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 <chrono>
20 #include <cmath>
21 #include <cstdint>
22 #include "interface/Event.h"
23
clearBit(uint32_t & data,uint32_t bit)24 inline void clearBit(uint32_t& data, uint32_t bit) {
25 data &= ~(1L << (bit));
26 }
27
setBit(uint32_t & data,uint32_t bit)28 inline void setBit(uint32_t& data, uint32_t bit) {
29 data |= (1L << (bit));
30 }
31
setBitField(uint32_t & data,uint32_t value,uint32_t offset,uint32_t fieldMask)32 inline void setBitField(uint32_t& data, uint32_t value, uint32_t offset, uint32_t fieldMask) {
33 data = (data & ~fieldMask) | (((value << offset) & fieldMask));
34 }
35
36 namespace android::hardware::graphics::composer {
37
38 struct TimedEvent;
39
40 constexpr int64_t kMillisecondToNanoSecond = 1000000;
41
42 enum PresentFrameFlag {
43 kUpdateRefreshRateIndicatorLayerOnly = (1 << 0),
44 kIsYuv = (1 << 1),
45 kPresentingWhenDoze = (1 << 2),
46 };
47
48 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
roundDivide(T divident,T divisor)49 T roundDivide(T divident, T divisor) {
50 if (divident < 0 || divisor <= 0) {
51 return 0;
52 }
53 return (divident + (divisor / 2)) / divisor;
54 }
55
56 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
57 struct Fraction {
58 T mNum;
59 T mDen;
60
mNumFraction61 Fraction(T num = 0, T denom = 1) : mNum(num), mDen(denom) {
62 if (mDen < 0) {
63 mNum = -mNum;
64 mDen = -mDen;
65 }
66 }
67
roundFraction68 T round() { return roundDivide(mNum, mDen); }
69
70 bool operator<(const Fraction<T>& other) const { return mNum * other.mDen < other.mNum * mDen; }
71
72 bool operator==(const Fraction<T>& other) const {
73 return mNum * other.mDen == other.mNum * mDen;
74 }
75 };
76
77 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
freqToDurationNs(Fraction<T> freq)78 int64_t freqToDurationNs(Fraction<T> freq) {
79 return roundDivide(std::nano::den * static_cast<int64_t>(freq.mDen),
80 static_cast<int64_t>(freq.mNum));
81 }
82
83 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
durationNsToFreq(T durationNs)84 T durationNsToFreq(T durationNs) {
85 auto res = roundDivide(std::nano::den, static_cast<int64_t>(durationNs));
86 return static_cast<T>(res);
87 }
88
89 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
freqToDurationNs(T freq)90 T freqToDurationNs(T freq) {
91 auto res = roundDivide(std::nano::den, static_cast<int64_t>(freq));
92 return static_cast<T>(res);
93 }
94
95 int64_t getSteadyClockTimeMs();
96 int64_t getSteadyClockTimeNs();
97
98 int64_t getBootClockTimeMs();
99 int64_t getBootClockTimeNs();
100
101 int64_t steadyClockTimeToBootClockTimeNs(int64_t steadyClockTimeNs);
102
103 bool hasPresentFrameFlag(int flag, PresentFrameFlag target);
104
105 bool isPowerModeOff(int powerMode);
106
107 void setTimedEventWithAbsoluteTime(TimedEvent& event);
108
109 } // namespace android::hardware::graphics::composer
110