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>
27 namespace canvas::tools
{
29 double ElapsedTime::getSystemTime()
31 return ::tools::Time::GetMonotonicTicks() / 1.0E6
;
34 ElapsedTime::ElapsedTime()
36 m_fLastQueriedTime( 0.0 ),
37 m_fStartTime( getSystemTime() ),
39 m_bInPauseMode( false ),
40 m_bInHoldMode( false )
44 ElapsedTime::ElapsedTime(
45 std::shared_ptr
<ElapsedTime
> pTimeBase
)
46 : m_pTimeBase(std::move( pTimeBase
)),
47 m_fLastQueriedTime( 0.0 ),
48 m_fStartTime( getCurrentTime() ),
50 m_bInPauseMode( false ),
51 m_bInHoldMode( false )
55 void ElapsedTime::reset()
57 m_fLastQueriedTime
= 0.0;
58 m_fStartTime
= getCurrentTime();
60 m_bInPauseMode
= false;
61 m_bInHoldMode
= false;
64 void ElapsedTime::adjustTimer( double fOffset
)
66 // to make getElapsedTime() become _larger_, have to reduce
68 m_fStartTime
-= fOffset
;
70 // also adjust frozen time, this method must _always_ affect the
71 // value returned by getElapsedTime()!
72 if (m_bInHoldMode
|| m_bInPauseMode
)
73 m_fFrozenTime
+= fOffset
;
76 double ElapsedTime::getCurrentTime() const
78 return m_pTimeBase
== nullptr ? getSystemTime() : m_pTimeBase
->getElapsedTimeImpl();
81 double ElapsedTime::getElapsedTime() const
83 m_fLastQueriedTime
= getElapsedTimeImpl();
84 return m_fLastQueriedTime
;
87 double ElapsedTime::getElapsedTimeImpl() const
89 if (m_bInHoldMode
|| m_bInPauseMode
)
92 return getCurrentTime() - m_fStartTime
;
95 void ElapsedTime::pauseTimer()
97 m_fFrozenTime
= getElapsedTimeImpl();
98 m_bInPauseMode
= true;
101 void ElapsedTime::continueTimer()
103 m_bInPauseMode
= false;
105 // stop pausing, time runs again. Note that
106 // getElapsedTimeImpl() honors hold mode, i.e. a
107 // continueTimer() in hold mode will preserve the latter
108 const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime
);
110 // adjust start time, such that subsequent getElapsedTime() calls
111 // will virtually start from m_fFrozenTime.
112 m_fStartTime
+= fPauseDuration
;
115 void ElapsedTime::holdTimer()
117 // when called during hold mode (e.g. more than once per time
118 // object), the original hold time will be maintained.
119 m_fFrozenTime
= getElapsedTimeImpl();
120 m_bInHoldMode
= true;
123 void ElapsedTime::releaseTimer()
125 m_bInHoldMode
= false;
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */