oculus1

diff libovr/Src/Kernel/OVR_Timer.cpp @ 3:b069a5c27388

added a couple more stuff, fixed all the LibOVR line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:10:05 +0300
parents e2f9e4603129
children
line diff
     1.1 --- a/libovr/Src/Kernel/OVR_Timer.cpp	Sat Sep 14 17:51:03 2013 +0300
     1.2 +++ b/libovr/Src/Kernel/OVR_Timer.cpp	Sun Sep 15 04:10:05 2013 +0300
     1.3 @@ -1,1 +1,156 @@
     1.4 -/************************************************************************************
     1.5 
     1.6 Filename    :   OVR_Timer.cpp
     1.7 Content     :   Provides static functions for precise timing
     1.8 Created     :   September 19, 2012
     1.9 Notes       : 
    1.10 
    1.11 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
    1.12 
    1.13 Use of this software is subject to the terms of the Oculus license
    1.14 agreement provided at the time of installation or download, or which
    1.15 otherwise accompanies this software in either electronic or hard copy form.
    1.16 
    1.17 ************************************************************************************/
    1.18 
    1.19 #include "OVR_Timer.h"
    1.20 
    1.21 #if defined (OVR_OS_WIN32)
    1.22 #include <windows.h>
    1.23 
    1.24 #else
    1.25 #include <sys/time.h>
    1.26 #endif
    1.27 
    1.28 namespace OVR {
    1.29 
    1.30 //-----------------------------------------------------------------------------------
    1.31 // ***** Timer Class
    1.32 
    1.33 UInt64 Timer::GetProfileTicks()
    1.34 {
    1.35     return (GetRawTicks() * MksPerSecond) / GetRawFrequency();
    1.36 }
    1.37 double Timer::GetProfileSeconds()
    1.38 {
    1.39     static UInt64 StartTime = GetProfileTicks();
    1.40     return TicksToSeconds(GetProfileTicks()-StartTime);
    1.41 }
    1.42 
    1.43 
    1.44 //------------------------------------------------------------------------
    1.45 // *** Win32 Specific Timer
    1.46 
    1.47 #if (defined (OVR_OS_WIN32))
    1.48 
    1.49 CRITICAL_SECTION WinAPI_GetTimeCS;
    1.50 volatile UInt32  WinAPI_OldTime = 0;
    1.51 volatile UInt32  WinAPI_WrapCounter = 0;
    1.52 
    1.53 
    1.54 UInt32 Timer::GetTicksMs()
    1.55 {
    1.56     return timeGetTime();
    1.57 }
    1.58 
    1.59 UInt64 Timer::GetTicks()
    1.60 {
    1.61     DWORD  ticks = timeGetTime();
    1.62     UInt64 result;
    1.63 
    1.64     // On Win32 QueryPerformanceFrequency is unreliable due to SMP and
    1.65     // performance levels, so use this logic to detect wrapping and track
    1.66     // high bits.
    1.67     ::EnterCriticalSection(&WinAPI_GetTimeCS);
    1.68 
    1.69     if (WinAPI_OldTime > ticks)
    1.70         WinAPI_WrapCounter++;
    1.71     WinAPI_OldTime = ticks;
    1.72 
    1.73     result = (UInt64(WinAPI_WrapCounter) << 32) | ticks;
    1.74     ::LeaveCriticalSection(&WinAPI_GetTimeCS);
    1.75 
    1.76     return result * MksPerMs;
    1.77 }
    1.78 
    1.79 UInt64 Timer::GetRawTicks()
    1.80 {
    1.81     LARGE_INTEGER li;
    1.82     QueryPerformanceCounter(&li);
    1.83     return li.QuadPart;
    1.84 }
    1.85 
    1.86 UInt64 Timer::GetRawFrequency()
    1.87 {
    1.88     static UInt64 perfFreq = 0;
    1.89     if (perfFreq == 0)
    1.90     {
    1.91         LARGE_INTEGER freq;
    1.92         QueryPerformanceFrequency(&freq);
    1.93         perfFreq = freq.QuadPart;
    1.94     }
    1.95     return perfFreq;
    1.96 }
    1.97 
    1.98 void Timer::initializeTimerSystem()
    1.99 {
   1.100     timeBeginPeriod(1);
   1.101     InitializeCriticalSection(&WinAPI_GetTimeCS);
   1.102 
   1.103 }
   1.104 void Timer::shutdownTimerSystem()
   1.105 {
   1.106     DeleteCriticalSection(&WinAPI_GetTimeCS);
   1.107     timeEndPeriod(1);
   1.108 }
   1.109 
   1.110 #else   // !OVR_OS_WIN32
   1.111 
   1.112 
   1.113 //------------------------------------------------------------------------
   1.114 // *** Standard OS Timer     
   1.115 
   1.116 UInt32 Timer::GetTicksMs()
   1.117 {
   1.118     return (UInt32)(GetProfileTicks() / 1000);
   1.119 }
   1.120 // The profile ticks implementation is just fine for a normal timer.
   1.121 UInt64 Timer::GetTicks()
   1.122 {
   1.123     return GetProfileTicks();
   1.124 }
   1.125 
   1.126 void Timer::initializeTimerSystem()
   1.127 {
   1.128 }
   1.129 void Timer::shutdownTimerSystem()
   1.130 {
   1.131 }
   1.132 
   1.133 UInt64  Timer::GetRawTicks()
   1.134 {
   1.135     // TODO: prefer rdtsc when available?
   1.136 
   1.137     // Return microseconds.
   1.138     struct timeval tv;
   1.139     UInt64 result;
   1.140 
   1.141     gettimeofday(&tv, 0);
   1.142 
   1.143     result = (UInt64)tv.tv_sec * 1000000;
   1.144     result += tv.tv_usec;
   1.145 
   1.146     return result;
   1.147 }
   1.148 
   1.149 UInt64 Timer::GetRawFrequency()
   1.150 {
   1.151     return MksPerSecond;
   1.152 }
   1.153 
   1.154 #endif  // !OVR_OS_WIN32
   1.155 
   1.156 
   1.157 
   1.158 } // OVR
   1.159 
   1.160 \ No newline at end of file
   1.161 +/************************************************************************************
   1.162 +
   1.163 +Filename    :   OVR_Timer.cpp
   1.164 +Content     :   Provides static functions for precise timing
   1.165 +Created     :   September 19, 2012
   1.166 +Notes       : 
   1.167 +
   1.168 +Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
   1.169 +
   1.170 +Use of this software is subject to the terms of the Oculus license
   1.171 +agreement provided at the time of installation or download, or which
   1.172 +otherwise accompanies this software in either electronic or hard copy form.
   1.173 +
   1.174 +************************************************************************************/
   1.175 +
   1.176 +#include "OVR_Timer.h"
   1.177 +
   1.178 +#if defined (OVR_OS_WIN32)
   1.179 +#include <windows.h>
   1.180 +
   1.181 +#else
   1.182 +#include <sys/time.h>
   1.183 +#endif
   1.184 +
   1.185 +namespace OVR {
   1.186 +
   1.187 +//-----------------------------------------------------------------------------------
   1.188 +// ***** Timer Class
   1.189 +
   1.190 +UInt64 Timer::GetProfileTicks()
   1.191 +{
   1.192 +    return (GetRawTicks() * MksPerSecond) / GetRawFrequency();
   1.193 +}
   1.194 +double Timer::GetProfileSeconds()
   1.195 +{
   1.196 +    static UInt64 StartTime = GetProfileTicks();
   1.197 +    return TicksToSeconds(GetProfileTicks()-StartTime);
   1.198 +}
   1.199 +
   1.200 +
   1.201 +//------------------------------------------------------------------------
   1.202 +// *** Win32 Specific Timer
   1.203 +
   1.204 +#if (defined (OVR_OS_WIN32))
   1.205 +
   1.206 +CRITICAL_SECTION WinAPI_GetTimeCS;
   1.207 +volatile UInt32  WinAPI_OldTime = 0;
   1.208 +volatile UInt32  WinAPI_WrapCounter = 0;
   1.209 +
   1.210 +
   1.211 +UInt32 Timer::GetTicksMs()
   1.212 +{
   1.213 +    return timeGetTime();
   1.214 +}
   1.215 +
   1.216 +UInt64 Timer::GetTicks()
   1.217 +{
   1.218 +    DWORD  ticks = timeGetTime();
   1.219 +    UInt64 result;
   1.220 +
   1.221 +    // On Win32 QueryPerformanceFrequency is unreliable due to SMP and
   1.222 +    // performance levels, so use this logic to detect wrapping and track
   1.223 +    // high bits.
   1.224 +    ::EnterCriticalSection(&WinAPI_GetTimeCS);
   1.225 +
   1.226 +    if (WinAPI_OldTime > ticks)
   1.227 +        WinAPI_WrapCounter++;
   1.228 +    WinAPI_OldTime = ticks;
   1.229 +
   1.230 +    result = (UInt64(WinAPI_WrapCounter) << 32) | ticks;
   1.231 +    ::LeaveCriticalSection(&WinAPI_GetTimeCS);
   1.232 +
   1.233 +    return result * MksPerMs;
   1.234 +}
   1.235 +
   1.236 +UInt64 Timer::GetRawTicks()
   1.237 +{
   1.238 +    LARGE_INTEGER li;
   1.239 +    QueryPerformanceCounter(&li);
   1.240 +    return li.QuadPart;
   1.241 +}
   1.242 +
   1.243 +UInt64 Timer::GetRawFrequency()
   1.244 +{
   1.245 +    static UInt64 perfFreq = 0;
   1.246 +    if (perfFreq == 0)
   1.247 +    {
   1.248 +        LARGE_INTEGER freq;
   1.249 +        QueryPerformanceFrequency(&freq);
   1.250 +        perfFreq = freq.QuadPart;
   1.251 +    }
   1.252 +    return perfFreq;
   1.253 +}
   1.254 +
   1.255 +void Timer::initializeTimerSystem()
   1.256 +{
   1.257 +    timeBeginPeriod(1);
   1.258 +    InitializeCriticalSection(&WinAPI_GetTimeCS);
   1.259 +
   1.260 +}
   1.261 +void Timer::shutdownTimerSystem()
   1.262 +{
   1.263 +    DeleteCriticalSection(&WinAPI_GetTimeCS);
   1.264 +    timeEndPeriod(1);
   1.265 +}
   1.266 +
   1.267 +#else   // !OVR_OS_WIN32
   1.268 +
   1.269 +
   1.270 +//------------------------------------------------------------------------
   1.271 +// *** Standard OS Timer     
   1.272 +
   1.273 +UInt32 Timer::GetTicksMs()
   1.274 +{
   1.275 +    return (UInt32)(GetProfileTicks() / 1000);
   1.276 +}
   1.277 +// The profile ticks implementation is just fine for a normal timer.
   1.278 +UInt64 Timer::GetTicks()
   1.279 +{
   1.280 +    return GetProfileTicks();
   1.281 +}
   1.282 +
   1.283 +void Timer::initializeTimerSystem()
   1.284 +{
   1.285 +}
   1.286 +void Timer::shutdownTimerSystem()
   1.287 +{
   1.288 +}
   1.289 +
   1.290 +UInt64  Timer::GetRawTicks()
   1.291 +{
   1.292 +    // TODO: prefer rdtsc when available?
   1.293 +
   1.294 +    // Return microseconds.
   1.295 +    struct timeval tv;
   1.296 +    UInt64 result;
   1.297 +
   1.298 +    gettimeofday(&tv, 0);
   1.299 +
   1.300 +    result = (UInt64)tv.tv_sec * 1000000;
   1.301 +    result += tv.tv_usec;
   1.302 +
   1.303 +    return result;
   1.304 +}
   1.305 +
   1.306 +UInt64 Timer::GetRawFrequency()
   1.307 +{
   1.308 +    return MksPerSecond;
   1.309 +}
   1.310 +
   1.311 +#endif  // !OVR_OS_WIN32
   1.312 +
   1.313 +
   1.314 +
   1.315 +} // OVR
   1.316 +