30 #if defined (OVR_OS_WIN32)
32 #elif defined(OVR_OS_ANDROID)
34 #include <android/log.h>
77 #if defined(OVR_OS_ANDROID)
86 const int status = clock_gettime(CLOCK_MONOTONIC, &tp);
100 #elif defined (OVR_OS_WIN32)
106 struct PerformanceTimer
109 : OldMMTimeMs(0), MMTimeWrapCounter(0), PrefFrequency(0),
110 LastResultNanos(0), PerfMinusTicksDeltaNanos(0)
114 MMTimerResolutionNanos = 1000000
123 UINT64 getFrequency()
125 if (PrefFrequency == 0)
128 QueryPerformanceFrequency(&freq);
129 PrefFrequency = freq.QuadPart;
131 return PrefFrequency;
135 CRITICAL_SECTION TimeCS;
144 UInt64 PerfMinusTicksDeltaNanos;
149 PerformanceTimer Win32_PerfTimer;
152 void PerformanceTimer::Initialize()
155 InitializeCriticalSection(&TimeCS);
156 MMTimeWrapCounter = 0;
160 void PerformanceTimer::Shutdown()
162 DeleteCriticalSection(&TimeCS);
166 UInt64 PerformanceTimer::GetTimeNanos()
175 ::EnterCriticalSection(&TimeCS);
178 mmTimeMs = timeGetTime();
179 QueryPerformanceCounter(&li);
181 if (OldMMTimeMs > mmTimeMs)
183 OldMMTimeMs = mmTimeMs;
186 UInt64 mmCounterNanos = ((
UInt64(MMTimeWrapCounter) << 32) | mmTimeMs) * 1000000;
187 UInt64 frequency = getFrequency();
188 UInt64 perfCounterSeconds =
UInt64(li.QuadPart) / frequency;
189 UInt64 perfRemainderNanos = ( (
UInt64(li.QuadPart) - perfCounterSeconds * frequency) *
193 if (PerfMinusTicksDeltaNanos == 0)
194 PerfMinusTicksDeltaNanos = perfCounterNanos - mmCounterNanos;
206 resultNanos = perfCounterNanos - PerfMinusTicksDeltaNanos;
211 if (resultNanos > (mmCounterNanos + MMTimerResolutionNanos*2))
213 resultNanos = mmCounterNanos + MMTimerResolutionNanos*2;
214 if (resultNanos < LastResultNanos)
215 resultNanos = LastResultNanos;
216 PerfMinusTicksDeltaNanos = perfCounterNanos - resultNanos;
218 else if (resultNanos < (mmCounterNanos - MMTimerResolutionNanos))
220 resultNanos = mmCounterNanos - MMTimerResolutionNanos;
221 if (resultNanos < LastResultNanos)
222 resultNanos = LastResultNanos;
223 PerfMinusTicksDeltaNanos = perfCounterNanos - resultNanos;
226 LastResultNanos = resultNanos;
227 ::LeaveCriticalSection(&TimeCS);
230 static UInt64 initial_time = 0;
231 if (!initial_time) initial_time = resultNanos;
232 resultNanos -= initial_time;
245 return Win32_PerfTimer.GetTimeNanos();
249 Win32_PerfTimer.Initialize();
254 Win32_PerfTimer.Shutdown();
257 #else // !OVR_OS_WIN32 && !OVR_OS_ANDROID
274 gettimeofday(&tv, 0);
279 return result * 1000;
282 #endif // OS-specific
static UInt64 OVR_STDCALL GetTicksNanos()
static bool useFakeSeconds
static void initializeTimerSystem()
static double FakeSeconds
static double OVR_STDCALL GetSeconds()
#define OVR_DEBUG_LOG(args)
static void shutdownTimerSystem()