vpx_timer.h 2.39 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */


12 13
#ifndef VPX_PORTS_VPX_TIMER_H_
#define VPX_PORTS_VPX_TIMER_H_
14 15 16

#include "./vpx_config.h"

17
#include "vpx/vpx_integer.h"
John Koleszar's avatar
John Koleszar committed
18

19 20
#if CONFIG_OS_SUPPORT

21
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Win32 specific includes
 */
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#else
/*
 * POSIX specific includes
 */
#include <sys/time.h>

/* timersub is not provided by msys at this time. */
#ifndef timersub
#define timersub(a, b, result) \
John Koleszar's avatar
John Koleszar committed
38 39 40 41 42 43 44 45
  do { \
    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
    if ((result)->tv_usec < 0) { \
      --(result)->tv_sec; \
      (result)->tv_usec += 1000000; \
    } \
  } while (0)
John Koleszar's avatar
John Koleszar committed
46 47 48 49
#endif
#endif


John Koleszar's avatar
John Koleszar committed
50
struct vpx_usec_timer {
51
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
52
  LARGE_INTEGER  begin, end;
John Koleszar's avatar
John Koleszar committed
53
#else
John Koleszar's avatar
John Koleszar committed
54
  struct timeval begin, end;
John Koleszar's avatar
John Koleszar committed
55 56 57 58
#endif
};


59
static INLINE void
John Koleszar's avatar
John Koleszar committed
60
vpx_usec_timer_start(struct vpx_usec_timer *t) {
61
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
62
  QueryPerformanceCounter(&t->begin);
John Koleszar's avatar
John Koleszar committed
63
#else
John Koleszar's avatar
John Koleszar committed
64
  gettimeofday(&t->begin, NULL);
John Koleszar's avatar
John Koleszar committed
65 66 67 68
#endif
}


69
static INLINE void
John Koleszar's avatar
John Koleszar committed
70
vpx_usec_timer_mark(struct vpx_usec_timer *t) {
71
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
72
  QueryPerformanceCounter(&t->end);
John Koleszar's avatar
John Koleszar committed
73
#else
John Koleszar's avatar
John Koleszar committed
74
  gettimeofday(&t->end, NULL);
John Koleszar's avatar
John Koleszar committed
75 76 77 78
#endif
}


79
static INLINE int64_t
John Koleszar's avatar
John Koleszar committed
80
vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
81
#if defined(_WIN32)
John Koleszar's avatar
John Koleszar committed
82
  LARGE_INTEGER freq, diff;
John Koleszar's avatar
John Koleszar committed
83

John Koleszar's avatar
John Koleszar committed
84
  diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
John Koleszar's avatar
John Koleszar committed
85

86 87
  QueryPerformanceFrequency(&freq);
  return diff.QuadPart * 1000000 / freq.QuadPart;
John Koleszar's avatar
John Koleszar committed
88
#else
John Koleszar's avatar
John Koleszar committed
89
  struct timeval diff;
John Koleszar's avatar
John Koleszar committed
90

John Koleszar's avatar
John Koleszar committed
91
  timersub(&t->end, &t->begin, &diff);
92
  return diff.tv_sec * 1000000 + diff.tv_usec;
John Koleszar's avatar
John Koleszar committed
93 94 95
#endif
}

96 97 98 99 100 101 102
#else /* CONFIG_OS_SUPPORT = 0*/

/* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
#ifndef timersub
#define timersub(a, b, result)
#endif

John Koleszar's avatar
John Koleszar committed
103 104
struct vpx_usec_timer {
  void *dummy;
105 106
};

107
static INLINE void
108 109
vpx_usec_timer_start(struct vpx_usec_timer *t) { }

110
static INLINE void
111 112
vpx_usec_timer_mark(struct vpx_usec_timer *t) { }

113
static INLINE int
John Koleszar's avatar
John Koleszar committed
114 115 116
vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
  return 0;
}
117 118

#endif /* CONFIG_OS_SUPPORT */
John Koleszar's avatar
John Koleszar committed
119

120
#endif  // VPX_PORTS_VPX_TIMER_H_