/indra/llcommon/llframetimer.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 152 lines · 54 code · 31 blank · 67 comment · 0 complexity · 584c6e79ceb6bfce1eb6e1c7791f3ea0 MD5 · raw file

  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=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLFRAMETIMER_H
  28. #define LL_LLFRAMETIMER_H
  29. /**
  30. * *NOTE: Because of limitations on linux which we do not really have
  31. * time to explore, the total time is derived from the frame time
  32. * and is recsynchronized on every frame.
  33. */
  34. #include "lltimer.h"
  35. #include "timing.h"
  36. class LL_COMMON_API LLFrameTimer
  37. {
  38. public:
  39. LLFrameTimer() : mStartTime( sFrameTime ), mExpiry(0), mStarted(TRUE) {}
  40. // Return the number of seconds since the start of this
  41. // application instance.
  42. static F64 getElapsedSeconds()
  43. {
  44. // Loses msec precision after ~4.5 hours...
  45. return sFrameTime;
  46. }
  47. // Return a low precision usec since epoch
  48. static U64 getTotalTime()
  49. {
  50. return sTotalTime ? sTotalTime : totalTime();
  51. }
  52. // Return a low precision seconds since epoch
  53. static F64 getTotalSeconds()
  54. {
  55. return sTotalSeconds;
  56. }
  57. // Call this method once per frame to update the current frame time. This is actually called
  58. // at some other times as well
  59. static void updateFrameTime();
  60. // Call this method once, and only once, per frame to update the current frame count.
  61. static void updateFrameCount() { sFrameCount++; }
  62. static U32 getFrameCount() { return sFrameCount; }
  63. static F32 getFrameDeltaTimeF32();
  64. // Return seconds since the current frame started
  65. static F32 getCurrentFrameTime();
  66. // MANIPULATORS
  67. void start();
  68. void stop();
  69. void reset();
  70. void resetWithExpiry(F32 expiration);
  71. void pause();
  72. void unpause();
  73. void setTimerExpirySec(F32 expiration);
  74. void setExpiryAt(F64 seconds_since_epoch);
  75. BOOL checkExpirationAndReset(F32 expiration);
  76. F32 getElapsedTimeAndResetF32() { F32 t = F32(sFrameTime - mStartTime); reset(); return t; }
  77. void setAge(const F64 age) { mStartTime = sFrameTime - age; }
  78. // ACCESSORS
  79. BOOL hasExpired() const { return (sFrameTime >= mExpiry); }
  80. F32 getTimeToExpireF32() const { return (F32)(mExpiry - sFrameTime); }
  81. F32 getElapsedTimeF32() const { return mStarted ? (F32)(sFrameTime - mStartTime) : (F32)mStartTime; }
  82. BOOL getStarted() const { return mStarted; }
  83. // return the seconds since epoch when this timer will expire.
  84. F64 expiresAt() const;
  85. protected:
  86. // A single, high resolution timer that drives all LLFrameTimers
  87. // *NOTE: no longer used.
  88. //static LLTimer sInternalTimer;
  89. //
  90. // Aplication constants
  91. //
  92. // Start time of opp in usec since epoch
  93. static U64 sStartTotalTime;
  94. //
  95. // Data updated per frame
  96. //
  97. // Seconds since application start
  98. static F64 sFrameTime;
  99. // Time that has elapsed since last call to updateFrameTime()
  100. static U64 sFrameDeltaTime;
  101. // Total microseconds since epoch.
  102. static U64 sTotalTime;
  103. // Seconds since epoch.
  104. static F64 sTotalSeconds;
  105. // Total number of frames elapsed in application
  106. static S32 sFrameCount;
  107. //
  108. // Member data
  109. //
  110. // Number of seconds after application start when this timer was
  111. // started. Set equal to sFrameTime when reset.
  112. F64 mStartTime;
  113. // Timer expires this many seconds after application start time.
  114. F64 mExpiry;
  115. // Useful bit of state usually associated with timers, but does
  116. // not affect actual functionality
  117. BOOL mStarted;
  118. };
  119. // Glue code for Havok (or anything else that doesn't want the full .h files)
  120. extern F32 getCurrentFrameTime();
  121. #endif // LL_LLFRAMETIMER_H