xref: /aosp_15_r20/external/libaom/aom_ports/aom_timer.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_PORTS_AOM_TIMER_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_PORTS_AOM_TIMER_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_OS_SUPPORT
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #include <stddef.h>
20*77c1e3ccSAndroid Build Coastguard Worker #include <stdint.h>
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #if defined(_WIN32)
23*77c1e3ccSAndroid Build Coastguard Worker /*
24*77c1e3ccSAndroid Build Coastguard Worker  * Win32 specific includes
25*77c1e3ccSAndroid Build Coastguard Worker  */
26*77c1e3ccSAndroid Build Coastguard Worker #undef NOMINMAX
27*77c1e3ccSAndroid Build Coastguard Worker #define NOMINMAX
28*77c1e3ccSAndroid Build Coastguard Worker #undef WIN32_LEAN_AND_MEAN
29*77c1e3ccSAndroid Build Coastguard Worker #define WIN32_LEAN_AND_MEAN
30*77c1e3ccSAndroid Build Coastguard Worker #include <windows.h>
31*77c1e3ccSAndroid Build Coastguard Worker #else
32*77c1e3ccSAndroid Build Coastguard Worker /*
33*77c1e3ccSAndroid Build Coastguard Worker  * POSIX specific includes
34*77c1e3ccSAndroid Build Coastguard Worker  */
35*77c1e3ccSAndroid Build Coastguard Worker #include <sys/time.h>
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker /* timersub is not provided by msys at this time. */
38*77c1e3ccSAndroid Build Coastguard Worker #ifndef timersub
39*77c1e3ccSAndroid Build Coastguard Worker #define timersub(a, b, result)                       \
40*77c1e3ccSAndroid Build Coastguard Worker   do {                                               \
41*77c1e3ccSAndroid Build Coastguard Worker     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
42*77c1e3ccSAndroid Build Coastguard Worker     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
43*77c1e3ccSAndroid Build Coastguard Worker     if ((result)->tv_usec < 0) {                     \
44*77c1e3ccSAndroid Build Coastguard Worker       --(result)->tv_sec;                            \
45*77c1e3ccSAndroid Build Coastguard Worker       (result)->tv_usec += 1000000;                  \
46*77c1e3ccSAndroid Build Coastguard Worker     }                                                \
47*77c1e3ccSAndroid Build Coastguard Worker   } while (0)
48*77c1e3ccSAndroid Build Coastguard Worker #endif
49*77c1e3ccSAndroid Build Coastguard Worker #endif
50*77c1e3ccSAndroid Build Coastguard Worker 
51*77c1e3ccSAndroid Build Coastguard Worker struct aom_usec_timer {
52*77c1e3ccSAndroid Build Coastguard Worker #if defined(_WIN32)
53*77c1e3ccSAndroid Build Coastguard Worker   LARGE_INTEGER begin, end;
54*77c1e3ccSAndroid Build Coastguard Worker #else
55*77c1e3ccSAndroid Build Coastguard Worker   struct timeval begin, end;
56*77c1e3ccSAndroid Build Coastguard Worker #endif
57*77c1e3ccSAndroid Build Coastguard Worker };
58*77c1e3ccSAndroid Build Coastguard Worker 
aom_usec_timer_start(struct aom_usec_timer * t)59*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_usec_timer_start(struct aom_usec_timer *t) {
60*77c1e3ccSAndroid Build Coastguard Worker #if defined(_WIN32)
61*77c1e3ccSAndroid Build Coastguard Worker   QueryPerformanceCounter(&t->begin);
62*77c1e3ccSAndroid Build Coastguard Worker #else
63*77c1e3ccSAndroid Build Coastguard Worker   gettimeofday(&t->begin, NULL);
64*77c1e3ccSAndroid Build Coastguard Worker #endif
65*77c1e3ccSAndroid Build Coastguard Worker }
66*77c1e3ccSAndroid Build Coastguard Worker 
aom_usec_timer_mark(struct aom_usec_timer * t)67*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_usec_timer_mark(struct aom_usec_timer *t) {
68*77c1e3ccSAndroid Build Coastguard Worker #if defined(_WIN32)
69*77c1e3ccSAndroid Build Coastguard Worker   QueryPerformanceCounter(&t->end);
70*77c1e3ccSAndroid Build Coastguard Worker #else
71*77c1e3ccSAndroid Build Coastguard Worker   gettimeofday(&t->end, NULL);
72*77c1e3ccSAndroid Build Coastguard Worker #endif
73*77c1e3ccSAndroid Build Coastguard Worker }
74*77c1e3ccSAndroid Build Coastguard Worker 
aom_usec_timer_elapsed(struct aom_usec_timer * t)75*77c1e3ccSAndroid Build Coastguard Worker static inline int64_t aom_usec_timer_elapsed(struct aom_usec_timer *t) {
76*77c1e3ccSAndroid Build Coastguard Worker #if defined(_WIN32)
77*77c1e3ccSAndroid Build Coastguard Worker   LARGE_INTEGER freq, diff;
78*77c1e3ccSAndroid Build Coastguard Worker 
79*77c1e3ccSAndroid Build Coastguard Worker   diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
80*77c1e3ccSAndroid Build Coastguard Worker 
81*77c1e3ccSAndroid Build Coastguard Worker   QueryPerformanceFrequency(&freq);
82*77c1e3ccSAndroid Build Coastguard Worker   return diff.QuadPart * 1000000 / freq.QuadPart;
83*77c1e3ccSAndroid Build Coastguard Worker #else
84*77c1e3ccSAndroid Build Coastguard Worker   struct timeval diff;
85*77c1e3ccSAndroid Build Coastguard Worker 
86*77c1e3ccSAndroid Build Coastguard Worker   timersub(&t->end, &t->begin, &diff);
87*77c1e3ccSAndroid Build Coastguard Worker   return ((int64_t)diff.tv_sec) * 1000000 + diff.tv_usec;
88*77c1e3ccSAndroid Build Coastguard Worker #endif
89*77c1e3ccSAndroid Build Coastguard Worker }
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker #else /* CONFIG_OS_SUPPORT = 0*/
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
94*77c1e3ccSAndroid Build Coastguard Worker #ifndef timersub
95*77c1e3ccSAndroid Build Coastguard Worker #define timersub(a, b, result)
96*77c1e3ccSAndroid Build Coastguard Worker #endif
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker struct aom_usec_timer {
99*77c1e3ccSAndroid Build Coastguard Worker   void *dummy;
100*77c1e3ccSAndroid Build Coastguard Worker };
101*77c1e3ccSAndroid Build Coastguard Worker 
aom_usec_timer_start(struct aom_usec_timer * t)102*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_usec_timer_start(struct aom_usec_timer *t) { (void)t; }
103*77c1e3ccSAndroid Build Coastguard Worker 
aom_usec_timer_mark(struct aom_usec_timer * t)104*77c1e3ccSAndroid Build Coastguard Worker static inline void aom_usec_timer_mark(struct aom_usec_timer *t) { (void)t; }
105*77c1e3ccSAndroid Build Coastguard Worker 
aom_usec_timer_elapsed(struct aom_usec_timer * t)106*77c1e3ccSAndroid Build Coastguard Worker static inline int aom_usec_timer_elapsed(struct aom_usec_timer *t) {
107*77c1e3ccSAndroid Build Coastguard Worker   (void)t;
108*77c1e3ccSAndroid Build Coastguard Worker   return 0;
109*77c1e3ccSAndroid Build Coastguard Worker }
110*77c1e3ccSAndroid Build Coastguard Worker 
111*77c1e3ccSAndroid Build Coastguard Worker #endif /* CONFIG_OS_SUPPORT */
112*77c1e3ccSAndroid Build Coastguard Worker 
113*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_PORTS_AOM_TIMER_H_
114