1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org 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
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_canvas.hxx"
32 #include "osl/diagnose.h"
33 #include "canvas/elapsedtime.hxx"
38 #pragma warning(push,1)
42 // Awaiting corresponding functionality in OSL
44 #define WIN32_LEAN_AND_MEAN
62 // TODO(Q2): is 0 okay for the failure case here?
63 double ElapsedTime::getSystemTime()
66 // Awaiting corresponding functionality in OSL
69 // is there a performance counter available?
70 static bool bTimeSetupDone( false );
71 static bool bPerfTimerAvailable( false );
72 static LONGLONG nPerfCountFreq
;
74 // TODO(F1): This _might_ cause problems, as it prevents correct
75 // time handling for very long lifetimes of this class's
76 // surrounding component in memory. When the difference between
77 // current sys time and nInitialCount exceeds IEEE double's
78 // mantissa, time will start to run jerky.
79 static LONGLONG nInitialCount
;
83 if( QueryPerformanceFrequency(
84 reinterpret_cast<LARGE_INTEGER
*>(&nPerfCountFreq
) ) )
87 QueryPerformanceCounter(
88 reinterpret_cast<LARGE_INTEGER
*>(&nInitialCount
) );
89 bPerfTimerAvailable
= true;
91 bTimeSetupDone
= true;
94 if( bPerfTimerAvailable
)
97 QueryPerformanceCounter(
98 reinterpret_cast<LARGE_INTEGER
*>(&nCurrCount
) );
99 nCurrCount
-= nInitialCount
;
100 return double(nCurrCount
) / nPerfCountFreq
;
104 LONGLONG nCurrTime
= timeGetTime();
105 return double(nCurrTime
) / 1000.0;
111 // TODO(Q2): is 0 okay for the failure case here?
112 double ElapsedTime::getSystemTime()
115 if( osl_getSystemTime( &aTimeVal
) )
116 return ((aTimeVal
.Nanosec
* 10e-10) + aTimeVal
.Seconds
);
123 ElapsedTime::ElapsedTime()
125 m_fLastQueriedTime( 0.0 ),
126 m_fStartTime( getSystemTime() ),
127 m_fFrozenTime( 0.0 ),
128 m_bInPauseMode( false ),
129 m_bInHoldMode( false )
133 ElapsedTime::ElapsedTime(
134 boost::shared_ptr
<ElapsedTime
> const & pTimeBase
)
135 : m_pTimeBase( pTimeBase
),
136 m_fLastQueriedTime( 0.0 ),
137 m_fStartTime( getCurrentTime() ),
138 m_fFrozenTime( 0.0 ),
139 m_bInPauseMode( false ),
140 m_bInHoldMode( false )
144 boost::shared_ptr
<ElapsedTime
> const & ElapsedTime::getTimeBase() const
149 void ElapsedTime::reset()
151 m_fLastQueriedTime
= 0.0;
152 m_fStartTime
= getCurrentTime();
154 m_bInPauseMode
= false;
155 m_bInHoldMode
= false;
158 void ElapsedTime::adjustTimer( double fOffset
, bool /*bLimitToLastQueriedTime*/ )
160 // to make getElapsedTime() become _larger_, have to reduce
162 m_fStartTime
-= fOffset
;
164 // also adjust frozen time, this method must _always_ affect the
165 // value returned by getElapsedTime()!
166 if (m_bInHoldMode
|| m_bInPauseMode
)
167 m_fFrozenTime
+= fOffset
;
170 double ElapsedTime::getCurrentTime() const
172 return m_pTimeBase
.get() == 0
173 ? getSystemTime() : m_pTimeBase
->getElapsedTimeImpl();
176 double ElapsedTime::getElapsedTime() const
178 m_fLastQueriedTime
= getElapsedTimeImpl();
179 return m_fLastQueriedTime
;
182 double ElapsedTime::getElapsedTimeImpl() const
184 if (m_bInHoldMode
|| m_bInPauseMode
)
185 return m_fFrozenTime
;
187 return getCurrentTime() - m_fStartTime
;
190 void ElapsedTime::pauseTimer()
192 m_fFrozenTime
= getElapsedTimeImpl();
193 m_bInPauseMode
= true;
196 void ElapsedTime::continueTimer()
198 m_bInPauseMode
= false;
200 // stop pausing, time runs again. Note that
201 // getElapsedTimeImpl() honors hold mode, i.e. a
202 // continueTimer() in hold mode will preserve the latter
203 const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime
);
205 // adjust start time, such that subsequent getElapsedTime() calls
206 // will virtually start from m_fFrozenTime.
207 m_fStartTime
+= fPauseDuration
;
210 void ElapsedTime::holdTimer()
212 // when called during hold mode (e.g. more than once per time
213 // object), the original hold time will be maintained.
214 m_fFrozenTime
= getElapsedTimeImpl();
215 m_bInHoldMode
= true;
218 void ElapsedTime::releaseTimer()
220 m_bInHoldMode
= false;
224 } // namespace canvas