Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / tools / elapsedtime.cxx
blob54b9bf6b77a21bb6c2dc6cf5880f1ffdd1456a21
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <osl/time.h>
22 #include <osl/diagnose.h>
23 #include <canvas/elapsedtime.hxx>
25 #if defined(WNT)
27 #if defined _MSC_VER
28 #pragma warning(push,1)
29 #endif
31 // TEMP!!!
32 // Awaiting corresponding functionality in OSL
34 #define WIN32_LEAN_AND_MEAN
35 #include <windows.h>
36 #include <winbase.h>
37 #include <mmsystem.h>
38 #endif
40 #if defined _MSC_VER
41 #pragma warning(pop)
42 #endif
44 #include <algorithm>
45 #include <limits>
47 namespace canvas {
48 namespace tools {
51 #if defined(WNT)
52 // TODO(Q2): is 0 okay for the failure case here?
53 double ElapsedTime::getSystemTime()
55 // TEMP!!!
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;
71 if( !bTimeSetupDone )
73 if( QueryPerformanceFrequency(
74 reinterpret_cast<LARGE_INTEGER *>(&nPerfCountFreq) ) )
76 // read initial time:
77 QueryPerformanceCounter(
78 reinterpret_cast<LARGE_INTEGER *>(&nInitialCount) );
79 bPerfTimerAvailable = true;
81 bTimeSetupDone = true;
84 if( bPerfTimerAvailable )
86 LONGLONG nCurrCount;
87 QueryPerformanceCounter(
88 reinterpret_cast<LARGE_INTEGER *>(&nCurrCount) );
89 nCurrCount -= nInitialCount;
90 return double(nCurrCount) / nPerfCountFreq;
92 else
94 LONGLONG nCurrTime = timeGetTime();
95 return double(nCurrTime) / 1000.0;
99 #else // ! WNT
101 // TODO(Q2): is 0 okay for the failure case here?
102 double ElapsedTime::getSystemTime()
104 TimeValue aTimeVal;
105 if( osl_getSystemTime( &aTimeVal ) )
106 return ((aTimeVal.Nanosec * 10e-10) + aTimeVal.Seconds);
107 else
108 return 0.0;
111 #endif
113 ElapsedTime::ElapsedTime()
114 : m_pTimeBase(),
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();
138 m_fFrozenTime = 0.0;
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
146 // m_fStartTime.
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;
208 } // namespace tools
209 } // namespace canvas
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */