John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | #ifndef VPX_TIMER_H |
| 13 | #define VPX_TIMER_H |
| 14 | |
Tero Rintaluoma | 11a222f | 2011-01-24 11:21:40 +0200 | [diff] [blame] | 15 | #if CONFIG_OS_SUPPORT |
| 16 | |
John Koleszar | daab4bc | 2010-09-02 12:03:51 -0400 | [diff] [blame] | 17 | #if defined(_WIN32) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 18 | /* |
| 19 | * Win32 specific includes |
| 20 | */ |
| 21 | #ifndef WIN32_LEAN_AND_MEAN |
| 22 | #define WIN32_LEAN_AND_MEAN |
| 23 | #endif |
| 24 | #include <windows.h> |
| 25 | #else |
| 26 | /* |
| 27 | * POSIX specific includes |
| 28 | */ |
| 29 | #include <sys/time.h> |
| 30 | |
| 31 | /* timersub is not provided by msys at this time. */ |
| 32 | #ifndef timersub |
| 33 | #define timersub(a, b, result) \ |
| 34 | do { \ |
| 35 | (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ |
| 36 | (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ |
| 37 | if ((result)->tv_usec < 0) { \ |
| 38 | --(result)->tv_sec; \ |
| 39 | (result)->tv_usec += 1000000; \ |
| 40 | } \ |
| 41 | } while (0) |
| 42 | #endif |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | struct vpx_usec_timer |
| 47 | { |
John Koleszar | daab4bc | 2010-09-02 12:03:51 -0400 | [diff] [blame] | 48 | #if defined(_WIN32) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 49 | LARGE_INTEGER begin, end; |
| 50 | #else |
| 51 | struct timeval begin, end; |
| 52 | #endif |
| 53 | }; |
| 54 | |
| 55 | |
John Koleszar | 5e34461 | 2010-06-24 09:02:48 -0400 | [diff] [blame] | 56 | static void |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 57 | vpx_usec_timer_start(struct vpx_usec_timer *t) |
| 58 | { |
John Koleszar | daab4bc | 2010-09-02 12:03:51 -0400 | [diff] [blame] | 59 | #if defined(_WIN32) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 60 | QueryPerformanceCounter(&t->begin); |
| 61 | #else |
| 62 | gettimeofday(&t->begin, NULL); |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | |
John Koleszar | 5e34461 | 2010-06-24 09:02:48 -0400 | [diff] [blame] | 67 | static void |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 68 | vpx_usec_timer_mark(struct vpx_usec_timer *t) |
| 69 | { |
John Koleszar | daab4bc | 2010-09-02 12:03:51 -0400 | [diff] [blame] | 70 | #if defined(_WIN32) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 71 | QueryPerformanceCounter(&t->end); |
| 72 | #else |
| 73 | gettimeofday(&t->end, NULL); |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | |
John Koleszar | 5e34461 | 2010-06-24 09:02:48 -0400 | [diff] [blame] | 78 | static long |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 79 | vpx_usec_timer_elapsed(struct vpx_usec_timer *t) |
| 80 | { |
John Koleszar | daab4bc | 2010-09-02 12:03:51 -0400 | [diff] [blame] | 81 | #if defined(_WIN32) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 82 | LARGE_INTEGER freq, diff; |
| 83 | |
| 84 | diff.QuadPart = t->end.QuadPart - t->begin.QuadPart; |
| 85 | |
| 86 | if (QueryPerformanceFrequency(&freq) && diff.QuadPart < freq.QuadPart) |
| 87 | return (long)(diff.QuadPart * 1000000 / freq.QuadPart); |
| 88 | |
| 89 | return 1000000; |
| 90 | #else |
| 91 | struct timeval diff; |
| 92 | |
| 93 | timersub(&t->end, &t->begin, &diff); |
| 94 | return diff.tv_sec ? 1000000 : diff.tv_usec; |
| 95 | #endif |
| 96 | } |
| 97 | |
Tero Rintaluoma | 11a222f | 2011-01-24 11:21:40 +0200 | [diff] [blame] | 98 | #else /* CONFIG_OS_SUPPORT = 0*/ |
| 99 | |
| 100 | /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */ |
| 101 | #ifndef timersub |
| 102 | #define timersub(a, b, result) |
| 103 | #endif |
| 104 | |
| 105 | struct vpx_usec_timer |
| 106 | { |
| 107 | void *dummy; |
| 108 | }; |
| 109 | |
| 110 | static void |
| 111 | vpx_usec_timer_start(struct vpx_usec_timer *t) { } |
| 112 | |
| 113 | static void |
| 114 | vpx_usec_timer_mark(struct vpx_usec_timer *t) { } |
| 115 | |
| 116 | static long |
| 117 | vpx_usec_timer_elapsed(struct vpx_usec_timer *t) { return 0; } |
| 118 | |
| 119 | #endif /* CONFIG_OS_SUPPORT */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 120 | |
| 121 | #endif |