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