llframetimer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @file llframetimer.cpp
  3. *
  4. * $LicenseInfo:firstyear=2002&license=viewergpl$
  5. *
  6. * Copyright (c) 2002-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "linden_common.h"
  32. #include "llcommonmath.h"
  33. #include "llframetimer.h"
  34. // Static members
  35. U64 LLFrameTimer::sStartTotalTime = LLTimer::totalTime();
  36. F64 LLFrameTimer::sFrameTime = 0.0;
  37. U64 LLFrameTimer::sTotalTime = 0;
  38. F64 LLFrameTimer::sTotalSeconds = 0.0;
  39. S32 LLFrameTimer::sFrameCount = 0;
  40. U64 LLFrameTimer::sFrameDeltaTime = 0;
  41. constexpr F64 USEC_TO_SEC_F64 = 0.000001;
  42. //static
  43. void LLFrameTimer::stepFrame()
  44. {
  45. U64 total_time = LLTimer::totalTime();
  46. if (total_time < sTotalTime)
  47. {
  48. llwarns << "Clock went backwards. Adjusting start time accordingly."
  49. << llendl;
  50. sFrameDeltaTime = 0;
  51. // Let's approximate the adjusted start time +/- last frame time
  52. sStartTotalTime += total_time;
  53. sStartTotalTime -= sTotalTime;
  54. }
  55. else
  56. {
  57. sFrameDeltaTime = total_time - sTotalTime;
  58. }
  59. sTotalTime = total_time;
  60. sTotalSeconds = U64_to_F64(sTotalTime) * USEC_TO_SEC_F64;
  61. sFrameTime = U64_to_F64(sTotalTime - sStartTotalTime) * USEC_TO_SEC_F64;
  62. ++sFrameCount;
  63. }
  64. void LLFrameTimer::setExpiryAt(F64 seconds_since_epoch)
  65. {
  66. mStartTime = sFrameTime;
  67. mExpiry = seconds_since_epoch - (USEC_TO_SEC_F64 * sStartTotalTime);
  68. }
  69. F64 LLFrameTimer::expiresAt() const
  70. {
  71. F64 expires_at = U64_to_F64(sStartTotalTime) * USEC_TO_SEC_F64;
  72. expires_at += mExpiry;
  73. return expires_at;
  74. }
  75. bool LLFrameTimer::checkExpirationAndReset(F32 expiration)
  76. {
  77. if (hasExpired())
  78. {
  79. reset();
  80. setTimerExpirySec(expiration);
  81. return true;
  82. }
  83. return false;
  84. }
  85. //static
  86. F32 LLFrameTimer::getFrameDeltaTimeF32()
  87. {
  88. return (F32)(U64_to_F64(sFrameDeltaTime) * USEC_TO_SEC_F64);
  89. }
  90. // Return seconds since the current frame started
  91. //static
  92. F32 LLFrameTimer::getCurrentFrameTime()
  93. {
  94. U64 frame_time = LLTimer::totalTime() - sTotalTime;
  95. return (F32)(U64_to_F64(frame_time) * USEC_TO_SEC_F64);
  96. }
  97. // Glue code to avoid full class .h file #includes
  98. F32 getCurrentFrameTime()
  99. {
  100. return (F32)(LLFrameTimer::getCurrentFrameTime());
  101. }