llframetimer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @file llframetimer.h
  3. * @brief A lightweight timer that measures seconds and is only
  4. * updated once per frame.
  5. *
  6. * $LicenseInfo:firstyear=2002&license=viewergpl$
  7. *
  8. * Copyright (c) 2002-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLFRAMETIMER_H
  34. #define LL_LLFRAMETIMER_H
  35. #include "lltimer.h"
  36. class LLFrameTimer
  37. {
  38. // This class is the only one that should be allowed to call stepFrame()
  39. friend class LLApp;
  40. protected:
  41. LOG_CLASS(LLFrameTimer);
  42. public:
  43. LLFrameTimer()
  44. : mStartTime(sFrameTime),
  45. mExpiry(0),
  46. mStarted(true)
  47. {
  48. }
  49. // Returns the number of seconds since the start of this application
  50. // instance.
  51. LL_INLINE static F64 getElapsedSeconds()
  52. {
  53. // Loses msec precision after ~4.5 hours...
  54. return sFrameTime;
  55. }
  56. // Returns a low precision usec since epoch
  57. LL_INLINE static U64 getTotalTime()
  58. {
  59. return sTotalTime ? sTotalTime : LLTimer::totalTime();
  60. }
  61. // Returns a low precision seconds since epoch
  62. LL_INLINE static F64 getTotalSeconds()
  63. {
  64. return sTotalSeconds;
  65. }
  66. LL_INLINE static U32 getFrameCount() { return sFrameCount; }
  67. static F32 getFrameDeltaTimeF32();
  68. // Return seconds since the current frame started
  69. static F32 getCurrentFrameTime();
  70. LL_INLINE void start()
  71. {
  72. reset();
  73. mStarted = true;
  74. }
  75. LL_INLINE void stop() { mStarted = false; }
  76. LL_INLINE void reset()
  77. {
  78. mStartTime = sFrameTime;
  79. mExpiry = sFrameTime;
  80. }
  81. // Do not combine pause/unpause with start/stop. Usage:
  82. // LLFrameTime foo; (starts automatically)
  83. // foo.unpause(); (noop but safe)
  84. // foo.pause(); (pauses timer)
  85. // foo.unpause(); (unpauses)
  86. // F32 elapsed = foo.getElapsedTimeF32(); (does not include time between
  87. // pause() and unpause())
  88. // Note: elapsed would also be valid with no unpause() call (= time run
  89. // until pause() called)
  90. LL_INLINE void pause()
  91. {
  92. if (mStarted)
  93. {
  94. mStartTime = sFrameTime - mStartTime;
  95. mStarted = false;
  96. }
  97. }
  98. LL_INLINE void unpause()
  99. {
  100. if (!mStarted)
  101. {
  102. mStartTime = sFrameTime - mStartTime;
  103. mStarted = true;
  104. }
  105. }
  106. LL_INLINE void setTimerExpirySec(F32 delay) { mExpiry = delay + mStartTime; }
  107. LL_INLINE void resetWithExpiry(F32 delay)
  108. {
  109. reset();
  110. mExpiry = delay + mStartTime;
  111. }
  112. void setExpiryAt(F64 seconds_since_epoch);
  113. bool checkExpirationAndReset(F32 expiration);
  114. LL_INLINE F32 getElapsedTimeAndResetF32()
  115. {
  116. F32 t = F32(sFrameTime - mStartTime);
  117. reset();
  118. return t;
  119. }
  120. LL_INLINE void setAge(F64 age) { mStartTime = sFrameTime - age; }
  121. LL_INLINE bool hasExpired() const { return sFrameTime >= mExpiry; }
  122. LL_INLINE F32 getTimeToExpireF32() const { return (F32)(mExpiry - sFrameTime); }
  123. LL_INLINE F32 getElapsedTimeF32() const { return mStarted ? (F32)(sFrameTime - mStartTime) : (F32)mStartTime; }
  124. LL_INLINE bool getStarted() const { return mStarted; }
  125. // Returns the seconds since epoch when this timer will expire.
  126. F64 expiresAt() const;
  127. private:
  128. // Called exclusively by LLApp::stepFrame()
  129. static void stepFrame();
  130. protected:
  131. // Number of seconds after application start when this timer was started.
  132. // Set equal to sFrameTime when reset.
  133. F64 mStartTime;
  134. // Timer expires this many seconds after application start time.
  135. F64 mExpiry;
  136. // Useful bit of state usually associated with timers, but does not affect
  137. // actual functionality
  138. bool mStarted;
  139. // Time at which the viewer session started in micro-seconds since epoch
  140. static U64 sStartTotalTime;
  141. //
  142. // Data updated at each frame
  143. //
  144. // Seconds since application start
  145. static F64 sFrameTime;
  146. // Time that has elapsed since last call to updateFrameTime()
  147. static U64 sFrameDeltaTime;
  148. // Total microseconds since epoch.
  149. static U64 sTotalTime;
  150. // Seconds since epoch.
  151. static F64 sTotalSeconds;
  152. // Total number of frames elapsed in application
  153. static S32 sFrameCount;
  154. };
  155. #endif // LL_LLFRAMETIMER_H