1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: elapsedtime.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_canvas.hxx"
35 #include "osl/diagnose.h"
36 #include "canvas/elapsedtime.hxx"
38 #if defined(WIN) || defined(WNT)
41 #pragma warning(push,1)
45 // Awaiting corresponding functionality in OSL
47 #define WIN32_LEAN_AND_MEAN
64 #if defined(WIN) || defined(WNT)
65 // TODO(Q2): is 0 okay for the failure case here?
66 double ElapsedTime::getSystemTime()
69 // Awaiting corresponding functionality in OSL
72 // is there a performance counter available?
73 static bool bTimeSetupDone( false );
74 static bool bPerfTimerAvailable( false );
75 static LONGLONG nPerfCountFreq
;
77 // TODO(F1): This _might_ cause problems, as it prevents correct
78 // time handling for very long lifetimes of this class's
79 // surrounding component in memory. When the difference between
80 // current sys time and nInitialCount exceeds IEEE double's
81 // mantissa, time will start to run jerky.
82 static LONGLONG nInitialCount
;
86 if( QueryPerformanceFrequency(
87 reinterpret_cast<LARGE_INTEGER
*>(&nPerfCountFreq
) ) )
90 QueryPerformanceCounter(
91 reinterpret_cast<LARGE_INTEGER
*>(&nInitialCount
) );
92 bPerfTimerAvailable
= true;
94 bTimeSetupDone
= true;
97 if( bPerfTimerAvailable
)
100 QueryPerformanceCounter(
101 reinterpret_cast<LARGE_INTEGER
*>(&nCurrCount
) );
102 nCurrCount
-= nInitialCount
;
103 return double(nCurrCount
) / nPerfCountFreq
;
107 LONGLONG nCurrTime
= timeGetTime();
108 return double(nCurrTime
) / 1000.0;
114 // TODO(Q2): is 0 okay for the failure case here?
115 double ElapsedTime::getSystemTime()
118 if( osl_getSystemTime( &aTimeVal
) )
119 return ((aTimeVal
.Nanosec
* 10e-10) + aTimeVal
.Seconds
);
126 ElapsedTime::ElapsedTime()
128 m_fLastQueriedTime( 0.0 ),
129 m_fStartTime( getSystemTime() ),
130 m_fFrozenTime( 0.0 ),
131 m_bInPauseMode( false ),
132 m_bInHoldMode( false )
136 ElapsedTime::ElapsedTime(
137 boost::shared_ptr
<ElapsedTime
> const & pTimeBase
)
138 : m_pTimeBase( pTimeBase
),
139 m_fLastQueriedTime( 0.0 ),
140 m_fStartTime( getCurrentTime() ),
141 m_fFrozenTime( 0.0 ),
142 m_bInPauseMode( false ),
143 m_bInHoldMode( false )
147 boost::shared_ptr
<ElapsedTime
> const & ElapsedTime::getTimeBase() const
152 void ElapsedTime::reset()
154 m_fLastQueriedTime
= 0.0;
155 m_fStartTime
= getCurrentTime();
157 m_bInPauseMode
= false;
158 m_bInHoldMode
= false;
161 void ElapsedTime::adjustTimer( double fOffset
, bool /*bLimitToLastQueriedTime*/ )
163 // to make getElapsedTime() become _larger_, have to reduce
165 m_fStartTime
-= fOffset
;
167 // also adjust frozen time, this method must _always_ affect the
168 // value returned by getElapsedTime()!
169 if (m_bInHoldMode
|| m_bInPauseMode
)
170 m_fFrozenTime
+= fOffset
;
173 double ElapsedTime::getCurrentTime() const
175 return m_pTimeBase
.get() == 0
176 ? getSystemTime() : m_pTimeBase
->getElapsedTimeImpl();
179 double ElapsedTime::getElapsedTime() const
181 m_fLastQueriedTime
= getElapsedTimeImpl();
182 return m_fLastQueriedTime
;
185 double ElapsedTime::getElapsedTimeImpl() const
187 if (m_bInHoldMode
|| m_bInPauseMode
)
188 return m_fFrozenTime
;
190 return getCurrentTime() - m_fStartTime
;
193 void ElapsedTime::pauseTimer()
195 m_fFrozenTime
= getElapsedTimeImpl();
196 m_bInPauseMode
= true;
199 void ElapsedTime::continueTimer()
201 m_bInPauseMode
= false;
203 // stop pausing, time runs again. Note that
204 // getElapsedTimeImpl() honors hold mode, i.e. a
205 // continueTimer() in hold mode will preserve the latter
206 const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime
);
208 // adjust start time, such that subsequent getElapsedTime() calls
209 // will virtually start from m_fFrozenTime.
210 m_fStartTime
+= fPauseDuration
;
213 void ElapsedTime::holdTimer()
215 // when called during hold mode (e.g. more than once per time
216 // object), the original hold time will be maintained.
217 m_fFrozenTime
= getElapsedTimeImpl();
218 m_bInHoldMode
= true;
221 void ElapsedTime::releaseTimer()
223 m_bInHoldMode
= false;
227 } // namespace canvas