vpx_timer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_VPX_PORTS_VPX_TIMER_H_
  11. #define VPX_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. #undef NOMINMAX
  20. #define NOMINMAX
  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. /* timersub is not provided by msys at this time. */
  31. #ifndef timersub
  32. #define timersub(a, b, result) \
  33. do { \
  34. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  35. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  36. if ((result)->tv_usec < 0) { \
  37. --(result)->tv_sec; \
  38. (result)->tv_usec += 1000000; \
  39. } \
  40. } while (0)
  41. #endif
  42. #endif
  43. struct vpx_usec_timer {
  44. #if defined(_WIN32)
  45. LARGE_INTEGER begin, end;
  46. #else
  47. struct timeval begin, end;
  48. #endif
  49. };
  50. static INLINE void vpx_usec_timer_start(struct vpx_usec_timer *t) {
  51. #if defined(_WIN32)
  52. QueryPerformanceCounter(&t->begin);
  53. #else
  54. gettimeofday(&t->begin, NULL);
  55. #endif
  56. }
  57. static INLINE void 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 vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
  65. #if defined(_WIN32)
  66. LARGE_INTEGER freq, diff;
  67. diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
  68. QueryPerformanceFrequency(&freq);
  69. return diff.QuadPart * 1000000 / freq.QuadPart;
  70. #else
  71. struct timeval diff;
  72. timersub(&t->end, &t->begin, &diff);
  73. return (int64_t)diff.tv_sec * 1000000 + diff.tv_usec;
  74. #endif
  75. }
  76. #else /* CONFIG_OS_SUPPORT = 0*/
  77. /* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
  78. #ifndef timersub
  79. #define timersub(a, b, result)
  80. #endif
  81. struct vpx_usec_timer {
  82. void *dummy;
  83. };
  84. static INLINE void vpx_usec_timer_start(struct vpx_usec_timer *t) {}
  85. static INLINE void vpx_usec_timer_mark(struct vpx_usec_timer *t) {}
  86. static INLINE int vpx_usec_timer_elapsed(struct vpx_usec_timer *t) { return 0; }
  87. #endif /* CONFIG_OS_SUPPORT */
  88. #endif // VPX_VPX_PORTS_VPX_TIMER_H_