-
Notifications
You must be signed in to change notification settings - Fork 4
/
frequencymonitor.cpp
65 lines (52 loc) · 1.27 KB
/
frequencymonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "frequencymonitor.h"
#include <QQuickWindow>
FrequencyMonitor::FrequencyMonitor(QQuickItem *parent) :
QQuickItem(parent),
m_counter(0),
m_refreshPeriod(1000)
{
connect(this, &FrequencyMonitor::windowChanged, this, &FrequencyMonitor::handleWindowChanged);
}
int FrequencyMonitor::fps() const
{
return m_fps;
}
void FrequencyMonitor::setFps(int fps)
{
if (m_fps == fps)
return;
m_fps = fps;
emit fpsChanged();
}
int FrequencyMonitor::refreshPeriod() const
{
return m_refreshPeriod;
}
void FrequencyMonitor::setRefreshPeriod(int msec)
{
if (m_refreshPeriod == msec)
return;
m_refreshPeriod = msec;
emit refreshPeriodChanged();
}
void FrequencyMonitor::handleAfterRendering()
{
const int elapsedMsec = m_time.elapsed();
m_counter++;
if (elapsedMsec >= m_refreshPeriod)
{
setFps(m_counter / (elapsedMsec / 1000.0));
m_counter = 0;
m_time.restart();
}
}
void FrequencyMonitor::handleWindowChanged(QQuickWindow *window)
{
if (m_windowConnection)
this->disconnect(m_windowConnection);
if (window)
{
m_time.restart();
m_windowConnection = connect(window, &QQuickWindow::afterRendering, this, &FrequencyMonitor::handleAfterRendering);
}
}