1 /*
2  * Copyright 2022 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 #ifndef ANDROID_HWC_TIME_H
18 #define ANDROID_HWC_TIME_H
19 
20 #include <utils/Timers.h>
21 
22 #include <chrono>
23 
24 #include "Common.h"
25 
26 namespace aidl::android::hardware::graphics::composer3::impl {
27 
28 using Nanoseconds = std::chrono::nanoseconds;
29 
30 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
31 
asTimePoint(int64_t nanos)32 inline TimePoint asTimePoint(int64_t nanos) { return TimePoint(Nanoseconds(nanos)); }
33 
now()34 inline TimePoint now() { return asTimePoint(systemTime(SYSTEM_TIME_MONOTONIC)); }
35 
asNanosDuration(Nanoseconds duration)36 inline int64_t asNanosDuration(Nanoseconds duration) { return duration.count(); }
37 
asNanosTimePoint(TimePoint time)38 inline int64_t asNanosTimePoint(TimePoint time) {
39     TimePoint zero(Nanoseconds(0));
40     return static_cast<int64_t>(std::chrono::duration_cast<Nanoseconds>(time - zero).count());
41 }
42 
HertzToPeriodNanos(uint32_t hertz)43 constexpr int32_t HertzToPeriodNanos(uint32_t hertz) { return 1000 * 1000 * 1000 / hertz; }
44 
45 }  // namespace aidl::android::hardware::graphics::composer3::impl
46 
47 #endif
48