vpx_timer.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  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
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_PORTS_VPX_TIMER_H_
  11. #define VPX_PORTS_VPX_TIMER_H_
  12. #include "./vpx_config.h"
  13. #include "vpx/vpx_integer.h"
  14. #if CONFIG_OS_SUPPORT
  15. #if defined(_WIN32)
  16. /*
  17. * Win32 specific includes
  18. */
  19. #ifndef WIN32_LEAN_AND_MEAN
  20. #define WIN32_LEAN_AND_MEAN
  21. #endif
  22. #include <windows.h>
  23. #else
  24. /*
  25. * POSIX specific includes
  26. */
  27. #include <sys/time.h>
  28. /* timersub is not provided by msys at this time. */
  29. #ifndef timersub
  30. #define timersub(a, b, result) \
  31. do { \
  32. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  33. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  34. if ((result)->tv_usec < 0) { \
  35. --(result)->tv_sec; \
  36. (result)->tv_usec += 1000000; \
  37. } \
  38. } while (0)
  39. #endif
  40. #endif
  41. struct vpx_usec_timer {
  42. #if defined(_WIN32)
  43. LARGE_INTEGER begin, end;
  44. #else
  45. struct timeval begin, end;
  46. #endif
  47. };
  48. static INLINE void
  49. vpx_usec_timer_start(struct vpx_usec_timer *t) {
  50. #if defined(_WIN32)
  51. QueryPerformanceCounter(&t->begin);
  52. #else
  53. gettimeofday(&t->begin, NULL);
  54. #endif
  55. }
  56. static INLINE void
  57. vpx_usec_timer_mark(struct vpx_usec_timer *t) {
  58. #if defined(_WIN32)
  59. QueryPerformanceCounter(&t->end);
  60. #else
  61. gettimeofday(&t->end, NULL);
  62. #endif
  63. }
  64. static INLINE int64_t
  65. vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
  66. #if defined(_WIN32)
  67. LARGE_INTEGER freq, diff;
  68. diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
  69. QueryPerformanceFrequency(&freq);
  70. return diff.QuadPart * 1000000 / freq.QuadPart;
  71. #else
  72. struct timeval diff;
  73. timersub(&t->end, &t->begin, &diff);
  74. return diff.tv_sec * 1000000 + diff.tv_usec;
  75. #endif
  76. }
  77. #else /* CONFIG_OS_SUPPORT = 0*/
  78. /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
  79. #ifndef timersub
  80. #define timersub(a, b, result)
  81. #endif
  82. struct vpx_usec_timer {
  83. void *dummy;
  84. };
  85. static INLINE void
  86. vpx_usec_timer_start(struct vpx_usec_timer *t) { }
  87. static INLINE void
  88. vpx_usec_timer_mark(struct vpx_usec_timer *t) { }
  89. static INLINE int
  90. vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
  91. return 0;
  92. }
  93. #endif /* CONFIG_OS_SUPPORT */
  94. #endif // VPX_PORTS_VPX_TIMER_H_