SDL_systimer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_TIMER_WINDOWS
  20. #include "../../core/windows/SDL_windows.h"
  21. #include <mmsystem.h>
  22. #include "SDL_timer.h"
  23. #include "SDL_hints.h"
  24. /* The first (low-resolution) ticks value of the application */
  25. static DWORD start = 0;
  26. static BOOL ticks_started = FALSE;
  27. /* The first high-resolution ticks value of the application */
  28. static LARGE_INTEGER start_ticks;
  29. /* The number of ticks per second of the high-resolution performance counter */
  30. static LARGE_INTEGER ticks_per_second;
  31. static void
  32. SDL_SetSystemTimerResolution(const UINT uPeriod)
  33. {
  34. #ifndef __WINRT__
  35. static UINT timer_period = 0;
  36. if (uPeriod != timer_period) {
  37. if (timer_period) {
  38. timeEndPeriod(timer_period);
  39. }
  40. timer_period = uPeriod;
  41. if (timer_period) {
  42. timeBeginPeriod(timer_period);
  43. }
  44. }
  45. #endif
  46. }
  47. static void SDLCALL
  48. SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  49. {
  50. UINT uPeriod;
  51. /* Unless the hint says otherwise, let's have good sleep precision */
  52. if (hint && *hint) {
  53. uPeriod = SDL_atoi(hint);
  54. } else {
  55. uPeriod = 1;
  56. }
  57. if (uPeriod || oldValue != hint) {
  58. SDL_SetSystemTimerResolution(uPeriod);
  59. }
  60. }
  61. void
  62. SDL_TicksInit(void)
  63. {
  64. BOOL rc;
  65. if (ticks_started) {
  66. return;
  67. }
  68. ticks_started = SDL_TRUE;
  69. /* if we didn't set a precision, set it high. This affects lots of things
  70. on Windows besides the SDL timers, like audio callbacks, etc. */
  71. SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
  72. SDL_TimerResolutionChanged, NULL);
  73. /* Set first ticks value */
  74. /* QueryPerformanceCounter allegedly is always available and reliable as of WinXP,
  75. so we'll rely on it here.
  76. */
  77. rc = QueryPerformanceFrequency(&ticks_per_second);
  78. SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
  79. QueryPerformanceCounter(&start_ticks);
  80. }
  81. void
  82. SDL_TicksQuit(void)
  83. {
  84. SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
  85. SDL_TimerResolutionChanged, NULL);
  86. SDL_SetSystemTimerResolution(0); /* always release our timer resolution request. */
  87. start = 0;
  88. ticks_started = SDL_FALSE;
  89. }
  90. Uint64
  91. SDL_GetTicks64(void)
  92. {
  93. LARGE_INTEGER now;
  94. BOOL rc;
  95. if (!ticks_started) {
  96. SDL_TicksInit();
  97. }
  98. rc = QueryPerformanceCounter(&now);
  99. SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
  100. return (Uint64) (((now.QuadPart - start_ticks.QuadPart) * 1000) / ticks_per_second.QuadPart);
  101. }
  102. Uint64
  103. SDL_GetPerformanceCounter(void)
  104. {
  105. LARGE_INTEGER counter;
  106. const BOOL rc = QueryPerformanceCounter(&counter);
  107. SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
  108. return (Uint64) counter.QuadPart;
  109. }
  110. Uint64
  111. SDL_GetPerformanceFrequency(void)
  112. {
  113. LARGE_INTEGER frequency;
  114. const BOOL rc = QueryPerformanceFrequency(&frequency);
  115. SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
  116. return (Uint64) frequency.QuadPart;
  117. }
  118. void
  119. SDL_Delay(Uint32 ms)
  120. {
  121. /* Sleep() is not publicly available to apps in early versions of WinRT.
  122. *
  123. * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
  124. * Windows Phone 8.1.
  125. *
  126. * Use the compiler version to determine availability.
  127. *
  128. * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
  129. * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
  130. * Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
  131. * apps and libraries.
  132. */
  133. #if defined(__WINRT__) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
  134. static HANDLE mutex = 0;
  135. if (!mutex) {
  136. mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
  137. }
  138. WaitForSingleObjectEx(mutex, ms, FALSE);
  139. #else
  140. if (!ticks_started) {
  141. SDL_TicksInit();
  142. }
  143. Sleep(ms);
  144. #endif
  145. }
  146. #endif /* SDL_TIMER_WINDOWS */
  147. /* vi: set ts=4 sw=4 expandtab: */