Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "osl/time.h"
31 : : #include "osl/diagnose.h"
32 : : #include "canvas/elapsedtime.hxx"
33 : :
34 : : #if defined(WNT)
35 : :
36 : : #if defined _MSC_VER
37 : : #pragma warning(push,1)
38 : : #endif
39 : :
40 : : // TEMP!!!
41 : : // Awaiting corresponding functionality in OSL
42 : : //
43 : : #define WIN32_LEAN_AND_MEAN
44 : : #include <windows.h>
45 : : #include <winbase.h>
46 : : #include <mmsystem.h>
47 : : #endif
48 : :
49 : : #if defined _MSC_VER
50 : : #pragma warning(pop)
51 : : #endif
52 : :
53 : : #include <algorithm>
54 : : #include <limits>
55 : :
56 : : namespace canvas {
57 : : namespace tools {
58 : :
59 : :
60 : : #if defined(WNT)
61 : : // TODO(Q2): is 0 okay for the failure case here?
62 : : double ElapsedTime::getSystemTime()
63 : : {
64 : : // TEMP!!!
65 : : // Awaiting corresponding functionality in OSL
66 : : //
67 : :
68 : : // is there a performance counter available?
69 : : static bool bTimeSetupDone( false );
70 : : static bool bPerfTimerAvailable( false );
71 : : static LONGLONG nPerfCountFreq;
72 : :
73 : : // TODO(F1): This _might_ cause problems, as it prevents correct
74 : : // time handling for very long lifetimes of this class's
75 : : // surrounding component in memory. When the difference between
76 : : // current sys time and nInitialCount exceeds IEEE double's
77 : : // mantissa, time will start to run jerky.
78 : : static LONGLONG nInitialCount;
79 : :
80 : : if( !bTimeSetupDone )
81 : : {
82 : : if( QueryPerformanceFrequency(
83 : : reinterpret_cast<LARGE_INTEGER *>(&nPerfCountFreq) ) )
84 : : {
85 : : // read initial time:
86 : : QueryPerformanceCounter(
87 : : reinterpret_cast<LARGE_INTEGER *>(&nInitialCount) );
88 : : bPerfTimerAvailable = true;
89 : : }
90 : : bTimeSetupDone = true;
91 : : }
92 : :
93 : : if( bPerfTimerAvailable )
94 : : {
95 : : LONGLONG nCurrCount;
96 : : QueryPerformanceCounter(
97 : : reinterpret_cast<LARGE_INTEGER *>(&nCurrCount) );
98 : : nCurrCount -= nInitialCount;
99 : : return double(nCurrCount) / nPerfCountFreq;
100 : : }
101 : : else
102 : : {
103 : : LONGLONG nCurrTime = timeGetTime();
104 : : return double(nCurrTime) / 1000.0;
105 : : }
106 : : }
107 : :
108 : : #else // ! WNT
109 : :
110 : : // TODO(Q2): is 0 okay for the failure case here?
111 : 130 : double ElapsedTime::getSystemTime()
112 : : {
113 : : TimeValue aTimeVal;
114 [ + - ][ + - ]: 130 : if( osl_getSystemTime( &aTimeVal ) )
115 : 130 : return ((aTimeVal.Nanosec * 10e-10) + aTimeVal.Seconds);
116 : : else
117 : 130 : return 0.0;
118 : : }
119 : :
120 : : #endif
121 : :
122 : 130 : ElapsedTime::ElapsedTime()
123 : : : m_pTimeBase(),
124 : : m_fLastQueriedTime( 0.0 ),
125 [ + - ]: 130 : m_fStartTime( getSystemTime() ),
126 : : m_fFrozenTime( 0.0 ),
127 : : m_bInPauseMode( false ),
128 : 130 : m_bInHoldMode( false )
129 : : {
130 : 130 : }
131 : :
132 : 0 : ElapsedTime::ElapsedTime(
133 : : boost::shared_ptr<ElapsedTime> const & pTimeBase )
134 : : : m_pTimeBase( pTimeBase ),
135 : : m_fLastQueriedTime( 0.0 ),
136 [ # # ]: 0 : m_fStartTime( getCurrentTime() ),
137 : : m_fFrozenTime( 0.0 ),
138 : : m_bInPauseMode( false ),
139 : 0 : m_bInHoldMode( false )
140 : : {
141 : 0 : }
142 : :
143 : 0 : void ElapsedTime::reset()
144 : : {
145 : 0 : m_fLastQueriedTime = 0.0;
146 : 0 : m_fStartTime = getCurrentTime();
147 : 0 : m_fFrozenTime = 0.0;
148 : 0 : m_bInPauseMode = false;
149 : 0 : m_bInHoldMode = false;
150 : 0 : }
151 : :
152 : 0 : void ElapsedTime::adjustTimer( double fOffset, bool /*bLimitToLastQueriedTime*/ )
153 : : {
154 : : // to make getElapsedTime() become _larger_, have to reduce
155 : : // m_fStartTime.
156 : 0 : m_fStartTime -= fOffset;
157 : :
158 : : // also adjust frozen time, this method must _always_ affect the
159 : : // value returned by getElapsedTime()!
160 [ # # ][ # # ]: 0 : if (m_bInHoldMode || m_bInPauseMode)
161 : 0 : m_fFrozenTime += fOffset;
162 : 0 : }
163 : :
164 : 0 : double ElapsedTime::getCurrentTime() const
165 : : {
166 : 0 : return m_pTimeBase.get() == 0
167 [ # # ]: 0 : ? getSystemTime() : m_pTimeBase->getElapsedTimeImpl();
168 : : }
169 : :
170 : 0 : double ElapsedTime::getElapsedTime() const
171 : : {
172 : 0 : m_fLastQueriedTime = getElapsedTimeImpl();
173 : 0 : return m_fLastQueriedTime;
174 : : }
175 : :
176 : 0 : double ElapsedTime::getElapsedTimeImpl() const
177 : : {
178 [ # # ][ # # ]: 0 : if (m_bInHoldMode || m_bInPauseMode)
179 : 0 : return m_fFrozenTime;
180 : :
181 : 0 : return getCurrentTime() - m_fStartTime;
182 : : }
183 : :
184 : 0 : void ElapsedTime::pauseTimer()
185 : : {
186 : 0 : m_fFrozenTime = getElapsedTimeImpl();
187 : 0 : m_bInPauseMode = true;
188 : 0 : }
189 : :
190 : 0 : void ElapsedTime::continueTimer()
191 : : {
192 : 0 : m_bInPauseMode = false;
193 : :
194 : : // stop pausing, time runs again. Note that
195 : : // getElapsedTimeImpl() honors hold mode, i.e. a
196 : : // continueTimer() in hold mode will preserve the latter
197 : 0 : const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime );
198 : :
199 : : // adjust start time, such that subsequent getElapsedTime() calls
200 : : // will virtually start from m_fFrozenTime.
201 : 0 : m_fStartTime += fPauseDuration;
202 : 0 : }
203 : :
204 : 0 : void ElapsedTime::holdTimer()
205 : : {
206 : : // when called during hold mode (e.g. more than once per time
207 : : // object), the original hold time will be maintained.
208 : 0 : m_fFrozenTime = getElapsedTimeImpl();
209 : 0 : m_bInHoldMode = true;
210 : 0 : }
211 : :
212 : 0 : void ElapsedTime::releaseTimer()
213 : : {
214 : 0 : m_bInHoldMode = false;
215 : 0 : }
216 : :
217 : : } // namespace tools
218 : : } // namespace canvas
219 : :
220 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|