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 .
20 #include <sal/config.h>
22 #include <canvas/elapsedtime.hxx>
24 #include <tools/time.hxx>
26 namespace canvas::tools
{
28 double ElapsedTime::getSystemTime()
30 return ::tools::Time::GetMonotonicTicks() / 1.0E6
;
33 ElapsedTime::ElapsedTime()
35 m_fLastQueriedTime( 0.0 ),
36 m_fStartTime( getSystemTime() ),
38 m_bInPauseMode( false ),
39 m_bInHoldMode( false )
43 ElapsedTime::ElapsedTime(
44 std::shared_ptr
<ElapsedTime
> const & pTimeBase
)
45 : m_pTimeBase( pTimeBase
),
46 m_fLastQueriedTime( 0.0 ),
47 m_fStartTime( getCurrentTime() ),
49 m_bInPauseMode( false ),
50 m_bInHoldMode( false )
54 void ElapsedTime::reset()
56 m_fLastQueriedTime
= 0.0;
57 m_fStartTime
= getCurrentTime();
59 m_bInPauseMode
= false;
60 m_bInHoldMode
= false;
63 void ElapsedTime::adjustTimer( double fOffset
)
65 // to make getElapsedTime() become _larger_, have to reduce
67 m_fStartTime
-= fOffset
;
69 // also adjust frozen time, this method must _always_ affect the
70 // value returned by getElapsedTime()!
71 if (m_bInHoldMode
|| m_bInPauseMode
)
72 m_fFrozenTime
+= fOffset
;
75 double ElapsedTime::getCurrentTime() const
77 return m_pTimeBase
== nullptr ? getSystemTime() : m_pTimeBase
->getElapsedTimeImpl();
80 double ElapsedTime::getElapsedTime() const
82 m_fLastQueriedTime
= getElapsedTimeImpl();
83 return m_fLastQueriedTime
;
86 double ElapsedTime::getElapsedTimeImpl() const
88 if (m_bInHoldMode
|| m_bInPauseMode
)
91 return getCurrentTime() - m_fStartTime
;
94 void ElapsedTime::pauseTimer()
96 m_fFrozenTime
= getElapsedTimeImpl();
97 m_bInPauseMode
= true;
100 void ElapsedTime::continueTimer()
102 m_bInPauseMode
= false;
104 // stop pausing, time runs again. Note that
105 // getElapsedTimeImpl() honors hold mode, i.e. a
106 // continueTimer() in hold mode will preserve the latter
107 const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime
);
109 // adjust start time, such that subsequent getElapsedTime() calls
110 // will virtually start from m_fFrozenTime.
111 m_fStartTime
+= fPauseDuration
;
114 void ElapsedTime::holdTimer()
116 // when called during hold mode (e.g. more than once per time
117 // object), the original hold time will be maintained.
118 m_fFrozenTime
= getElapsedTimeImpl();
119 m_bInHoldMode
= true;
122 void ElapsedTime::releaseTimer()
124 m_bInHoldMode
= false;
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */