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