00001 /************************************************************************************ 00002 00003 PublicHeader: OVR 00004 Filename : OVR_Timer.h 00005 Content : Provides static functions for precise timing 00006 Created : September 19, 2012 00007 Notes : 00008 00009 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 00010 00011 Use of this software is subject to the terms of the Oculus license 00012 agreement provided at the time of installation or download, or which 00013 otherwise accompanies this software in either electronic or hard copy form. 00014 00015 ************************************************************************************/ 00016 00017 #ifndef OVR_Timer_h 00018 #define OVR_Timer_h 00019 00020 #include "OVR_Types.h" 00021 00022 namespace OVR { 00023 00024 //----------------------------------------------------------------------------------- 00025 // ***** Timer 00026 00027 // Timer class defines a family of static functions used for application 00028 // timing and profiling. 00029 00030 class Timer 00031 { 00032 public: 00033 enum { 00034 MsPerSecond = 1000, // Milliseconds in one second. 00035 MksPerMs = 1000, // Microseconds in one millisecond. 00036 MksPerSecond = MsPerSecond * MksPerMs 00037 }; 00038 00039 00040 // ***** Timing APIs for Application 00041 // These APIs should be used to guide animation and other program functions 00042 // that require precision. 00043 00044 // Returns ticks in milliseconds, as a 32-bit number. May wrap around every 00045 // 49.2 days. Use either time difference of two values of GetTicks to avoid 00046 // wrap-around. GetTicksMs may perform better then GetTicks. 00047 static UInt32 OVR_STDCALL GetTicksMs(); 00048 00049 // GetTicks returns general-purpose high resolution application timer value, 00050 // measured in microseconds (mks, or 1/1000000 of a second). The actual precision 00051 // is system-specific and may be much lower, such as 1 ms. 00052 static UInt64 OVR_STDCALL GetTicks(); 00053 00054 00055 // ***** Profiling APIs. 00056 // These functions should be used for profiling, but may have system specific 00057 // artifacts that make them less appropriate for general system use. 00058 // On Win32, for example these rely on QueryPerformanceConter may have 00059 // problems with thread-core switching and power modes. 00060 00061 // Return a hi-res timer value in mks (1/1000000 of a sec). 00062 // Generally you want to call this at the start and end of an 00063 // operation, and pass the difference to 00064 // TicksToSeconds() to find out how long the operation took. 00065 static UInt64 OVR_STDCALL GetProfileTicks(); 00066 00067 // More convenient zero-based profile timer in seconds. First call initializes 00068 // the "zero" value; future calls return the difference. Not thread safe for first call. 00069 // Due to low precision of Double, may malfunction after long runtime. 00070 static double OVR_STDCALL GetProfileSeconds(); 00071 00072 // Get the raw cycle counter value, providing the maximum possible timer resolution. 00073 static UInt64 OVR_STDCALL GetRawTicks(); 00074 static UInt64 OVR_STDCALL GetRawFrequency(); 00075 00076 00077 // ***** Tick and time unit conversion. 00078 00079 // Convert micro-second ticks value into seconds value. 00080 static inline double TicksToSeconds(UInt64 ticks) 00081 { 00082 return static_cast<double>(ticks) * (1.0 / (double)MksPerSecond); 00083 } 00084 // Convert Raw or frequency-unit ticks to seconds based on specified frequency. 00085 static inline double RawTicksToSeconds(UInt64 rawTicks, UInt64 rawFrequency) 00086 { 00087 return static_cast<double>(rawTicks) * rawFrequency; 00088 } 00089 00090 private: 00091 friend class System; 00092 // System called during program startup/shutdown. 00093 static void initializeTimerSystem(); 00094 static void shutdownTimerSystem(); 00095 }; 00096 00097 00098 } // Scaleform::Timer 00099 00100 #endif