PageRenderTime 30ms CodeModel.GetById 21ms app.highlight 6ms 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
  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
 26#include "linden_common.h"
 27
 28#include "u64.h"
 29
 30#include "llframetimer.h"
 31
 32// Static members
 33//LLTimer	LLFrameTimer::sInternalTimer;
 34U64 LLFrameTimer::sStartTotalTime = totalTime();
 35F64 LLFrameTimer::sFrameTime = 0.0;
 36U64 LLFrameTimer::sTotalTime = 0;
 37F64 LLFrameTimer::sTotalSeconds = 0.0;
 38S32 LLFrameTimer::sFrameCount = 0;
 39U64 LLFrameTimer::sFrameDeltaTime = 0;
 40const F64 USEC_PER_SECOND = 1000000.0;
 41const F64 USEC_TO_SEC_F64 = 0.000001;
 42
 43// static
 44void LLFrameTimer::updateFrameTime()
 45{
 46	U64 total_time = totalTime();
 47	sFrameDeltaTime = total_time - sTotalTime;
 48	sTotalTime = total_time;
 49	sTotalSeconds = U64_to_F64(sTotalTime) * USEC_TO_SEC_F64;
 50	sFrameTime = U64_to_F64(sTotalTime - sStartTotalTime) * USEC_TO_SEC_F64;
 51} 
 52
 53void LLFrameTimer::start()
 54{
 55	reset();
 56	mStarted = TRUE;
 57}
 58
 59void LLFrameTimer::stop()
 60{
 61	mStarted = FALSE;
 62}
 63
 64void LLFrameTimer::reset()
 65{
 66	mStartTime = sFrameTime;
 67	mExpiry = sFrameTime;
 68}
 69
 70void LLFrameTimer::resetWithExpiry(F32 expiration)
 71{
 72	reset();
 73	setTimerExpirySec(expiration);
 74}
 75
 76// Don't combine pause/unpause with start/stop
 77// Useage:
 78//  LLFrameTime foo; // starts automatically
 79//  foo.unpause(); // noop but safe
 80//  foo.pause(); // pauses timer
 81//  foo.unpause() // unpauses
 82//  F32 elapsed = foo.getElapsedTimeF32() // does not include time between pause() and unpause()
 83//  Note: elapsed would also be valid with no unpause() call (= time run until pause() called)
 84void LLFrameTimer::pause()
 85{
 86	if (mStarted)
 87		mStartTime = sFrameTime - mStartTime; // save dtime
 88	mStarted = FALSE;
 89}
 90
 91void LLFrameTimer::unpause()
 92{
 93	if (!mStarted)
 94		mStartTime = sFrameTime - mStartTime; // restore dtime
 95	mStarted = TRUE;
 96}
 97
 98void LLFrameTimer::setTimerExpirySec(F32 expiration)
 99{
100	mExpiry = expiration + mStartTime;
101}
102
103void LLFrameTimer::setExpiryAt(F64 seconds_since_epoch)
104{
105	mStartTime = sFrameTime;
106	mExpiry = seconds_since_epoch - (USEC_TO_SEC_F64 * sStartTotalTime);
107}
108
109F64 LLFrameTimer::expiresAt() const
110{
111	F64 expires_at = U64_to_F64(sStartTotalTime) * USEC_TO_SEC_F64;
112	expires_at += mExpiry;
113	return expires_at;
114}
115
116BOOL LLFrameTimer::checkExpirationAndReset(F32 expiration)
117{
118	//llinfos << "LLFrameTimer::checkExpirationAndReset()" << llendl;
119	//llinfos << "  mStartTime:" << mStartTime << llendl;
120	//llinfos << "  sFrameTime:" << sFrameTime << llendl;
121	//llinfos << "  mExpiry:   " <<  mExpiry << llendl;
122
123	if(hasExpired())
124	{
125		reset();
126		setTimerExpirySec(expiration);
127		return TRUE;
128	}
129	return FALSE;
130}
131
132// static
133F32 LLFrameTimer::getFrameDeltaTimeF32()
134{
135	return (F32)(U64_to_F64(sFrameDeltaTime) * USEC_TO_SEC_F64); 
136}
137
138
139//	static 
140// Return seconds since the current frame started
141F32  LLFrameTimer::getCurrentFrameTime()
142{
143	U64 frame_time = totalTime() - sTotalTime;
144	return (F32)(U64_to_F64(frame_time) * USEC_TO_SEC_F64); 
145}
146
147// Glue code to avoid full class .h file #includes
148F32  getCurrentFrameTime()
149{
150	return (F32)(LLFrameTimer::getCurrentFrameTime());
151}