PageRenderTime 14ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llframetimer.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 151 lines | 88 code | 20 blank | 43 comment | 3 complexity | 407668d5e190c61a22a63a64c2e7d300 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llframetimer.cpp
  3. *
  4. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "u64.h"
  27. #include "llframetimer.h"
  28. // Static members
  29. //LLTimer LLFrameTimer::sInternalTimer;
  30. U64 LLFrameTimer::sStartTotalTime = totalTime();
  31. F64 LLFrameTimer::sFrameTime = 0.0;
  32. U64 LLFrameTimer::sTotalTime = 0;
  33. F64 LLFrameTimer::sTotalSeconds = 0.0;
  34. S32 LLFrameTimer::sFrameCount = 0;
  35. U64 LLFrameTimer::sFrameDeltaTime = 0;
  36. const F64 USEC_PER_SECOND = 1000000.0;
  37. const F64 USEC_TO_SEC_F64 = 0.000001;
  38. // static
  39. void LLFrameTimer::updateFrameTime()
  40. {
  41. U64 total_time = totalTime();
  42. sFrameDeltaTime = total_time - sTotalTime;
  43. sTotalTime = total_time;
  44. sTotalSeconds = U64_to_F64(sTotalTime) * USEC_TO_SEC_F64;
  45. sFrameTime = U64_to_F64(sTotalTime - sStartTotalTime) * USEC_TO_SEC_F64;
  46. }
  47. void LLFrameTimer::start()
  48. {
  49. reset();
  50. mStarted = TRUE;
  51. }
  52. void LLFrameTimer::stop()
  53. {
  54. mStarted = FALSE;
  55. }
  56. void LLFrameTimer::reset()
  57. {
  58. mStartTime = sFrameTime;
  59. mExpiry = sFrameTime;
  60. }
  61. void LLFrameTimer::resetWithExpiry(F32 expiration)
  62. {
  63. reset();
  64. setTimerExpirySec(expiration);
  65. }
  66. // Don't combine pause/unpause with start/stop
  67. // Useage:
  68. // LLFrameTime foo; // starts automatically
  69. // foo.unpause(); // noop but safe
  70. // foo.pause(); // pauses timer
  71. // foo.unpause() // unpauses
  72. // F32 elapsed = foo.getElapsedTimeF32() // does not include time between pause() and unpause()
  73. // Note: elapsed would also be valid with no unpause() call (= time run until pause() called)
  74. void LLFrameTimer::pause()
  75. {
  76. if (mStarted)
  77. mStartTime = sFrameTime - mStartTime; // save dtime
  78. mStarted = FALSE;
  79. }
  80. void LLFrameTimer::unpause()
  81. {
  82. if (!mStarted)
  83. mStartTime = sFrameTime - mStartTime; // restore dtime
  84. mStarted = TRUE;
  85. }
  86. void LLFrameTimer::setTimerExpirySec(F32 expiration)
  87. {
  88. mExpiry = expiration + mStartTime;
  89. }
  90. void LLFrameTimer::setExpiryAt(F64 seconds_since_epoch)
  91. {
  92. mStartTime = sFrameTime;
  93. mExpiry = seconds_since_epoch - (USEC_TO_SEC_F64 * sStartTotalTime);
  94. }
  95. F64 LLFrameTimer::expiresAt() const
  96. {
  97. F64 expires_at = U64_to_F64(sStartTotalTime) * USEC_TO_SEC_F64;
  98. expires_at += mExpiry;
  99. return expires_at;
  100. }
  101. BOOL LLFrameTimer::checkExpirationAndReset(F32 expiration)
  102. {
  103. //llinfos << "LLFrameTimer::checkExpirationAndReset()" << llendl;
  104. //llinfos << " mStartTime:" << mStartTime << llendl;
  105. //llinfos << " sFrameTime:" << sFrameTime << llendl;
  106. //llinfos << " mExpiry: " << mExpiry << llendl;
  107. if(hasExpired())
  108. {
  109. reset();
  110. setTimerExpirySec(expiration);
  111. return TRUE;
  112. }
  113. return FALSE;
  114. }
  115. // static
  116. F32 LLFrameTimer::getFrameDeltaTimeF32()
  117. {
  118. return (F32)(U64_to_F64(sFrameDeltaTime) * USEC_TO_SEC_F64);
  119. }
  120. // static
  121. // Return seconds since the current frame started
  122. F32 LLFrameTimer::getCurrentFrameTime()
  123. {
  124. U64 frame_time = totalTime() - sTotalTime;
  125. return (F32)(U64_to_F64(frame_time) * USEC_TO_SEC_F64);
  126. }
  127. // Glue code to avoid full class .h file #includes
  128. F32 getCurrentFrameTime()
  129. {
  130. return (F32)(LLFrameTimer::getCurrentFrameTime());
  131. }