Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "osl/time.h"
22 : #include "osl/diagnose.h"
23 : #include "canvas/elapsedtime.hxx"
24 :
25 : #if defined(WNT)
26 :
27 : #if defined _MSC_VER
28 : #pragma warning(push,1)
29 : #endif
30 :
31 : // TEMP!!!
32 : // Awaiting corresponding functionality in OSL
33 : //
34 : #define WIN32_LEAN_AND_MEAN
35 : #include <windows.h>
36 : #include <winbase.h>
37 : #include <mmsystem.h>
38 : #endif
39 :
40 : #if defined _MSC_VER
41 : #pragma warning(pop)
42 : #endif
43 :
44 : #include <algorithm>
45 : #include <limits>
46 :
47 : namespace canvas {
48 : namespace tools {
49 :
50 :
51 : #if defined(WNT)
52 : // TODO(Q2): is 0 okay for the failure case here?
53 : double ElapsedTime::getSystemTime()
54 : {
55 : // TEMP!!!
56 : // Awaiting corresponding functionality in OSL
57 : //
58 :
59 : // is there a performance counter available?
60 : static bool bTimeSetupDone( false );
61 : static bool bPerfTimerAvailable( false );
62 : static LONGLONG nPerfCountFreq;
63 :
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;
70 :
71 : if( !bTimeSetupDone )
72 : {
73 : if( QueryPerformanceFrequency(
74 : reinterpret_cast<LARGE_INTEGER *>(&nPerfCountFreq) ) )
75 : {
76 : // read initial time:
77 : QueryPerformanceCounter(
78 : reinterpret_cast<LARGE_INTEGER *>(&nInitialCount) );
79 : bPerfTimerAvailable = true;
80 : }
81 : bTimeSetupDone = true;
82 : }
83 :
84 : if( bPerfTimerAvailable )
85 : {
86 : LONGLONG nCurrCount;
87 : QueryPerformanceCounter(
88 : reinterpret_cast<LARGE_INTEGER *>(&nCurrCount) );
89 : nCurrCount -= nInitialCount;
90 : return double(nCurrCount) / nPerfCountFreq;
91 : }
92 : else
93 : {
94 : LONGLONG nCurrTime = timeGetTime();
95 : return double(nCurrTime) / 1000.0;
96 : }
97 : }
98 :
99 : #else // ! WNT
100 :
101 : // TODO(Q2): is 0 okay for the failure case here?
102 0 : double ElapsedTime::getSystemTime()
103 : {
104 : TimeValue aTimeVal;
105 0 : if( osl_getSystemTime( &aTimeVal ) )
106 0 : return ((aTimeVal.Nanosec * 10e-10) + aTimeVal.Seconds);
107 : else
108 0 : return 0.0;
109 : }
110 :
111 : #endif
112 :
113 0 : ElapsedTime::ElapsedTime()
114 : : m_pTimeBase(),
115 : m_fLastQueriedTime( 0.0 ),
116 0 : m_fStartTime( getSystemTime() ),
117 : m_fFrozenTime( 0.0 ),
118 : m_bInPauseMode( false ),
119 0 : m_bInHoldMode( false )
120 : {
121 0 : }
122 :
123 0 : ElapsedTime::ElapsedTime(
124 : boost::shared_ptr<ElapsedTime> const & pTimeBase )
125 : : m_pTimeBase( pTimeBase ),
126 : m_fLastQueriedTime( 0.0 ),
127 0 : m_fStartTime( getCurrentTime() ),
128 : m_fFrozenTime( 0.0 ),
129 : m_bInPauseMode( false ),
130 0 : m_bInHoldMode( false )
131 : {
132 0 : }
133 :
134 0 : void ElapsedTime::reset()
135 : {
136 0 : m_fLastQueriedTime = 0.0;
137 0 : m_fStartTime = getCurrentTime();
138 0 : m_fFrozenTime = 0.0;
139 0 : m_bInPauseMode = false;
140 0 : m_bInHoldMode = false;
141 0 : }
142 :
143 0 : void ElapsedTime::adjustTimer( double fOffset, bool /*bLimitToLastQueriedTime*/ )
144 : {
145 : // to make getElapsedTime() become _larger_, have to reduce
146 : // m_fStartTime.
147 0 : m_fStartTime -= fOffset;
148 :
149 : // also adjust frozen time, this method must _always_ affect the
150 : // value returned by getElapsedTime()!
151 0 : if (m_bInHoldMode || m_bInPauseMode)
152 0 : m_fFrozenTime += fOffset;
153 0 : }
154 :
155 0 : double ElapsedTime::getCurrentTime() const
156 : {
157 0 : return m_pTimeBase.get() == 0
158 0 : ? getSystemTime() : m_pTimeBase->getElapsedTimeImpl();
159 : }
160 :
161 0 : double ElapsedTime::getElapsedTime() const
162 : {
163 0 : m_fLastQueriedTime = getElapsedTimeImpl();
164 0 : return m_fLastQueriedTime;
165 : }
166 :
167 0 : double ElapsedTime::getElapsedTimeImpl() const
168 : {
169 0 : if (m_bInHoldMode || m_bInPauseMode)
170 0 : return m_fFrozenTime;
171 :
172 0 : return getCurrentTime() - m_fStartTime;
173 : }
174 :
175 0 : void ElapsedTime::pauseTimer()
176 : {
177 0 : m_fFrozenTime = getElapsedTimeImpl();
178 0 : m_bInPauseMode = true;
179 0 : }
180 :
181 0 : void ElapsedTime::continueTimer()
182 : {
183 0 : m_bInPauseMode = false;
184 :
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 0 : const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime );
189 :
190 : // adjust start time, such that subsequent getElapsedTime() calls
191 : // will virtually start from m_fFrozenTime.
192 0 : m_fStartTime += fPauseDuration;
193 0 : }
194 :
195 0 : void ElapsedTime::holdTimer()
196 : {
197 : // when called during hold mode (e.g. more than once per time
198 : // object), the original hold time will be maintained.
199 0 : m_fFrozenTime = getElapsedTimeImpl();
200 0 : m_bInHoldMode = true;
201 0 : }
202 :
203 0 : void ElapsedTime::releaseTimer()
204 : {
205 0 : m_bInHoldMode = false;
206 0 : }
207 :
208 : } // namespace tools
209 : } // namespace canvas
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|