1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <osl/diagnose.h>
23 #include <canvas/elapsedtime.hxx>
28 #pragma warning(push,1)
32 // Awaiting corresponding functionality in OSL
34 #define WIN32_LEAN_AND_MEAN
52 // TODO(Q2): is 0 okay for the failure case here?
53 double ElapsedTime::getSystemTime()
56 // Awaiting corresponding functionality in OSL
59 // is there a performance counter available?
60 static bool bTimeSetupDone( false );
61 static bool bPerfTimerAvailable( false );
62 static LONGLONG nPerfCountFreq
;
64 // TODO(F1): This _might_ cause problems, as it prevents correct
65 // time handling for very long lifetimes of this class's
66 // surrounding component in memory. When the difference between
67 // current sys time and nInitialCount exceeds IEEE double's
68 // mantissa, time will start to run jerky.
69 static LONGLONG nInitialCount
;
73 if( QueryPerformanceFrequency(
74 reinterpret_cast<LARGE_INTEGER
*>(&nPerfCountFreq
) ) )
77 QueryPerformanceCounter(
78 reinterpret_cast<LARGE_INTEGER
*>(&nInitialCount
) );
79 bPerfTimerAvailable
= true;
81 bTimeSetupDone
= true;
84 if( bPerfTimerAvailable
)
87 QueryPerformanceCounter(
88 reinterpret_cast<LARGE_INTEGER
*>(&nCurrCount
) );
89 nCurrCount
-= nInitialCount
;
90 return double(nCurrCount
) / nPerfCountFreq
;
94 LONGLONG nCurrTime
= timeGetTime();
95 return double(nCurrTime
) / 1000.0;
101 // TODO(Q2): is 0 okay for the failure case here?
102 double ElapsedTime::getSystemTime()
105 if( osl_getSystemTime( &aTimeVal
) )
106 return ((aTimeVal
.Nanosec
* 10e-10) + aTimeVal
.Seconds
);
113 ElapsedTime::ElapsedTime()
115 m_fLastQueriedTime( 0.0 ),
116 m_fStartTime( getSystemTime() ),
117 m_fFrozenTime( 0.0 ),
118 m_bInPauseMode( false ),
119 m_bInHoldMode( false )
123 ElapsedTime::ElapsedTime(
124 boost::shared_ptr
<ElapsedTime
> const & pTimeBase
)
125 : m_pTimeBase( pTimeBase
),
126 m_fLastQueriedTime( 0.0 ),
127 m_fStartTime( getCurrentTime() ),
128 m_fFrozenTime( 0.0 ),
129 m_bInPauseMode( false ),
130 m_bInHoldMode( false )
134 void ElapsedTime::reset()
136 m_fLastQueriedTime
= 0.0;
137 m_fStartTime
= getCurrentTime();
139 m_bInPauseMode
= false;
140 m_bInHoldMode
= false;
143 void ElapsedTime::adjustTimer( double fOffset
, bool /*bLimitToLastQueriedTime*/ )
145 // to make getElapsedTime() become _larger_, have to reduce
147 m_fStartTime
-= fOffset
;
149 // also adjust frozen time, this method must _always_ affect the
150 // value returned by getElapsedTime()!
151 if (m_bInHoldMode
|| m_bInPauseMode
)
152 m_fFrozenTime
+= fOffset
;
155 double ElapsedTime::getCurrentTime() const
157 return m_pTimeBase
.get() == 0
158 ? getSystemTime() : m_pTimeBase
->getElapsedTimeImpl();
161 double ElapsedTime::getElapsedTime() const
163 m_fLastQueriedTime
= getElapsedTimeImpl();
164 return m_fLastQueriedTime
;
167 double ElapsedTime::getElapsedTimeImpl() const
169 if (m_bInHoldMode
|| m_bInPauseMode
)
170 return m_fFrozenTime
;
172 return getCurrentTime() - m_fStartTime
;
175 void ElapsedTime::pauseTimer()
177 m_fFrozenTime
= getElapsedTimeImpl();
178 m_bInPauseMode
= true;
181 void ElapsedTime::continueTimer()
183 m_bInPauseMode
= false;
185 // stop pausing, time runs again. Note that
186 // getElapsedTimeImpl() honors hold mode, i.e. a
187 // continueTimer() in hold mode will preserve the latter
188 const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime
);
190 // adjust start time, such that subsequent getElapsedTime() calls
191 // will virtually start from m_fFrozenTime.
192 m_fStartTime
+= fPauseDuration
;
195 void ElapsedTime::holdTimer()
197 // when called during hold mode (e.g. more than once per time
198 // object), the original hold time will be maintained.
199 m_fFrozenTime
= getElapsedTimeImpl();
200 m_bInHoldMode
= true;
203 void ElapsedTime::releaseTimer()
205 m_bInHoldMode
= false;
209 } // namespace canvas
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */