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 #include <sal/config.h>
21
22 #include <canvas/elapsedtime.hxx>
23
24 #include <tools/time.hxx>
25
26 namespace canvas::tools {
27
getSystemTime()28 double ElapsedTime::getSystemTime()
29 {
30 return ::tools::Time::GetMonotonicTicks() / 1.0E6;
31 }
32
ElapsedTime()33 ElapsedTime::ElapsedTime()
34 : m_pTimeBase(),
35 m_fLastQueriedTime( 0.0 ),
36 m_fStartTime( getSystemTime() ),
37 m_fFrozenTime( 0.0 ),
38 m_bInPauseMode( false ),
39 m_bInHoldMode( false )
40 {
41 }
42
ElapsedTime(std::shared_ptr<ElapsedTime> const & pTimeBase)43 ElapsedTime::ElapsedTime(
44 std::shared_ptr<ElapsedTime> const & pTimeBase )
45 : m_pTimeBase( pTimeBase ),
46 m_fLastQueriedTime( 0.0 ),
47 m_fStartTime( getCurrentTime() ),
48 m_fFrozenTime( 0.0 ),
49 m_bInPauseMode( false ),
50 m_bInHoldMode( false )
51 {
52 }
53
reset()54 void ElapsedTime::reset()
55 {
56 m_fLastQueriedTime = 0.0;
57 m_fStartTime = getCurrentTime();
58 m_fFrozenTime = 0.0;
59 m_bInPauseMode = false;
60 m_bInHoldMode = false;
61 }
62
adjustTimer(double fOffset)63 void ElapsedTime::adjustTimer( double fOffset )
64 {
65 // to make getElapsedTime() become _larger_, have to reduce
66 // m_fStartTime.
67 m_fStartTime -= fOffset;
68
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;
73 }
74
getCurrentTime() const75 double ElapsedTime::getCurrentTime() const
76 {
77 return m_pTimeBase == nullptr ? getSystemTime() : m_pTimeBase->getElapsedTimeImpl();
78 }
79
getElapsedTime() const80 double ElapsedTime::getElapsedTime() const
81 {
82 m_fLastQueriedTime = getElapsedTimeImpl();
83 return m_fLastQueriedTime;
84 }
85
getElapsedTimeImpl() const86 double ElapsedTime::getElapsedTimeImpl() const
87 {
88 if (m_bInHoldMode || m_bInPauseMode)
89 return m_fFrozenTime;
90
91 return getCurrentTime() - m_fStartTime;
92 }
93
pauseTimer()94 void ElapsedTime::pauseTimer()
95 {
96 m_fFrozenTime = getElapsedTimeImpl();
97 m_bInPauseMode = true;
98 }
99
continueTimer()100 void ElapsedTime::continueTimer()
101 {
102 m_bInPauseMode = false;
103
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 );
108
109 // adjust start time, such that subsequent getElapsedTime() calls
110 // will virtually start from m_fFrozenTime.
111 m_fStartTime += fPauseDuration;
112 }
113
holdTimer()114 void ElapsedTime::holdTimer()
115 {
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;
120 }
121
releaseTimer()122 void ElapsedTime::releaseTimer()
123 {
124 m_bInHoldMode = false;
125 }
126
127 } // namespace
128
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
130