1 /*
2     Copyright (C) 2020 by Pawel Soja <kernel32.pl@gmail.com>
3     FPS Meter
4 
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 
19 */
20 #include "fpsmeter.h"
21 
22 namespace INDI
23 {
24 
FPSMeter(double timeWindow)25 FPSMeter::FPSMeter(double timeWindow)
26     : mTimeWindow(timeWindow)
27 {
28     reset();
29 }
30 
newFrame()31 bool FPSMeter::newFrame()
32 {
33     mFrameTime2 = mFrameTime1;
34     mFrameTime1 = std::chrono::steady_clock::now();
35 
36     ++mTotalFrames;
37     ++mFramesPerElapsedTime;
38 
39     double dt = deltaTime();
40 
41     mElapsedTime += dt;
42     mTotalTime   += dt;
43 
44     if (mElapsedTime >= mTimeWindow)
45     {
46         mFramesPerSecond = mFramesPerElapsedTime / mElapsedTime * 1000;
47         mElapsedTime = 0;
48         mFramesPerElapsedTime = 0;
49         return true;
50     }
51 
52     return false;
53 }
54 
setTimeWindow(double timeWindow)55 void FPSMeter::setTimeWindow(double timeWindow)
56 {
57     mTimeWindow = timeWindow;
58 }
59 
framesPerSecond() const60 double FPSMeter::framesPerSecond() const
61 {
62     return mFramesPerSecond;
63 }
64 
deltaTime() const65 double FPSMeter::deltaTime() const
66 {
67     return std::chrono::duration<double>(mFrameTime1 - mFrameTime2).count() * 1000;
68 }
69 
totalFrames() const70 uint64_t FPSMeter::totalFrames() const
71 {
72     return mTotalFrames;
73 }
74 
totalTime() const75 double FPSMeter::totalTime() const
76 {
77     return mTotalTime;
78 }
79 
reset()80 void FPSMeter::reset()
81 {
82     mFramesPerElapsedTime = 0;
83     mElapsedTime = 0;
84     mFrameTime1 = std::chrono::steady_clock::now();
85     mFrameTime2 = mFrameTime1;
86     mFramesPerSecond = 0;
87     mTotalFrames = 0;
88     mTotalTime = 0;
89 }
90 
91 }
92