summaryrefslogtreecommitdiffstats
path: root/src/monitor-lib/frametimer.cpp
blob: 90beb1100ba6d4861d42e8ec3c92500337e44407 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "frametimer.h"

#include <QQuickWindow>
#include <qqmlinfo.h>

#include "waylandwindow.h"
#include "inprocesswindow.h"

/*!
    \qmltype FrameTimer
    \inqmlmodule QtApplicationManager
    \ingroup common-instantiatable
    \brief Provides frame-rate information about a given window.

    FrameTimer is used to get frame-rate information for a given window. The window can be either
    a toplevel Window (from the QtQuick.Window module) or a WindowObject
    (from the QtApplicationManager.SystemUI module).

    The following snippet shows how to use FrameTimer to display the frame-rate of a Window:

    \qml
    import QtQuick 2.11
    import QtApplicationManager 2.0

    Window {
        id: toplevelWindow
        ...
        FrameTimer {
            id: frameTimer
            running: topLevelWindow.visible
            window: toplevelWindow
        }
        Text {
            text: "FPS: " + Number(frameTimer.averageFps).toLocaleString(Qt.locale("en_US"), 'f', 1)
        }
    }
    \endqml

    You can also use this component as a MonitorModel data source if you want to plot its
    previous values over time:

    \qml
    import QtQuick 2.11
    import QtApplicationManager 2.0

    Window {
        id: toplevelWindow
        ...
        MonitorModel {
            running: true
            FrameTimer {
                window: toplevelWindow
            }
        }
    }
    \endqml

    Please note that when using FrameTimer as a MonitorModel data source there's no need to set it
    to \l{FrameTimer::running}{running} as MonitorModel will already call update() as needed.
*/

QT_BEGIN_NAMESPACE_AM

const qreal FrameTimer::MicrosInSec = qreal(1000 * 1000);

FrameTimer::FrameTimer(QObject *parent)
    : QObject(parent)
{
    m_updateTimer.setInterval(1000);
    connect(&m_updateTimer, &QTimer::timeout, this, &FrameTimer::update);
}

void FrameTimer::newFrame()
{
    int frameTime = m_timer.isValid() ? qMax(1, int(m_timer.nsecsElapsed() / 1000)) : IdealFrameTime;
    m_timer.restart();

    m_count++;
    m_sum += frameTime;
    m_min = qMin(m_min, frameTime);
    m_max = qMax(m_max, frameTime);
    m_jitter += qAbs(MicrosInSec / IdealFrameTime - MicrosInSec / frameTime);
}

void FrameTimer::reset()
{
    m_count = m_sum = m_max = 0;
    m_jitter = 0;
    m_min = std::numeric_limits<int>::max();
}

/*!
    \qmlproperty real FrameTimer::averageFps
    \readonly

    The average frame rate of the given \l window, in frames per second, since update()
    was last called (either manually or automatically in case \l{FrameTimer::running}{running} is set to true).

    \sa window running update()
*/
qreal FrameTimer::averageFps() const
{
    return m_averageFps;
}

/*!
    \qmlproperty real FrameTimer::minimumFps
    \readonly

    The minimum frame rate of the given \l window, in frames per second, since update()
    was last called (either manually or automatically in case \l{FrameTimer::running}{running} is set to true).

    \sa window running update()
*/
qreal FrameTimer::minimumFps() const
{
    return m_minimumFps;
}

/*!
    \qmlproperty real FrameTimer::maximumFps
    \readonly

    The maximum frame rate of the given \l window, in frames per second, since update()
    was last called (either manually or automatically in case \l{FrameTimer::running}{running} is set to true).

    \sa window running update()
*/
qreal FrameTimer::maximumFps() const
{
    return m_maximumFps;
}

/*!
    \qmlproperty real FrameTimer::jitterFps
    \readonly

    The frame rate jitter of the given \l window, in frames per second, since update()
    was last called (either manually or automatically in case \l{FrameTimer::running}{running} is set to true).

    \sa window running update()
*/
qreal FrameTimer::jitterFps() const
{
    return m_jitterFps;
}

/*!
    \qmlproperty Object FrameTimer::window

    The window to be monitored, from which frame-rate information will be gathered.
    It can be either a toplevel Window (from the QtQuick.Window module) or a WindowObject
    (from the QtApplicationManager.SystemUI module).

    \sa WindowObject
*/
QObject *FrameTimer::window() const
{
    return m_window;
}

void FrameTimer::setWindow(QObject *value)
{
    if (m_window == value)
        return;

#if defined(AM_MULTI_PROCESS)
    disconnectFromWaylandSurface();
#endif

    if (m_window)
        disconnect(m_window, nullptr, this, nullptr);

    m_window = value;

    if (m_window) {
        if (!connectToQuickWindow() && !connectToAppManWindow())
            qmlWarning(this) << "The given window is neither a QQuickWindow nor a WindowObject.";
    }

    emit windowChanged();
}

bool FrameTimer::connectToQuickWindow()
{
    QQuickWindow *quickWindow = qobject_cast<QQuickWindow*>(m_window);
    if (!quickWindow)
        return false;

    connect(quickWindow, &QQuickWindow::frameSwapped, this, &FrameTimer::newFrame, Qt::UniqueConnection);
    return true;
}

bool FrameTimer::connectToAppManWindow()
{
    Window *appManWindow = qobject_cast<Window*>(m_window);
    if (!appManWindow)
        return false;

    if (qobject_cast<InProcessWindow*>(appManWindow)) {
        qmlWarning(this) << "It makes no sense to measure the FPS of a WindowObject in single-process mode."
                            " FrameTimer won't operate with the given window.";
        return true;
    }

#if defined(AM_MULTI_PROCESS)
    WaylandWindow *waylandWindow = qobject_cast<WaylandWindow*>(m_window);
    Q_ASSERT(waylandWindow);

    connect(waylandWindow, &WaylandWindow::waylandSurfaceChanged,
            this, &FrameTimer::connectToWaylandSurface, Qt::UniqueConnection);

    connectToWaylandSurface();
#endif

    return true;
}

#if defined(AM_MULTI_PROCESS)
void FrameTimer::connectToWaylandSurface()
{
    WaylandWindow *waylandWindow = qobject_cast<WaylandWindow*>(m_window);
    Q_ASSERT(waylandWindow);

    disconnectFromWaylandSurface();

    m_waylandSurface = waylandWindow->waylandSurface();
    if (m_waylandSurface)
        connect(m_waylandSurface, &QWaylandQuickSurface::redraw, this, &FrameTimer::newFrame, Qt::UniqueConnection);
}

void FrameTimer::disconnectFromWaylandSurface()
{
    if (!m_waylandSurface)
        return;

    disconnect(m_waylandSurface, nullptr, this, nullptr);

    m_waylandSurface = nullptr;
}
#endif

/*!
    \qmlproperty list<string> FrameTimer::roleNames
    \readonly

    Names of the roles provided by FrameTimer when used as a MonitorModel data source.

    \sa MonitorModel
*/
QStringList FrameTimer::roleNames() const
{
    return { qSL("averageFps"), qSL("minimumFps"), qSL("maximumFps"), qSL("jitterFps") };
}

/*!
    \qmlmethod FrameTimer::update

    Updates the properties averageFps, minimumFps, maximumFps and jitterFps. Then resets internal
    counters so that new numbers can be taken for the new time period starting from the moment
    this method is called.

    Note that you normally don't have to call this method directly, as FrameTimer does it automatically
    every \l interval milliseconds while \l{FrameTimer::running}{running} is set to true.

    \sa running
*/
void FrameTimer::update()
{
    m_averageFps = m_sum ? MicrosInSec * m_count / m_sum : qreal(0);
    m_minimumFps = m_max ? MicrosInSec / m_max : qreal(0);
    m_maximumFps = m_min ? MicrosInSec / m_min : qreal(0);
    m_jitterFps = m_count ? m_jitter / m_count :  qreal(0);

    // Start counting again for the next sampling period but keep m_timer running because
    // we still need the diff between the last rendered frame and the upcoming one.
    reset();

    emit updated();
}

/*!
    \qmlproperty bool FrameTimer::running

    If \c true, update() will get called automatically every \l interval milliseconds.

    When using FrameTimer as a MonitorModel data source, this property should be kept as \c false.

    \sa update() interval
*/
bool FrameTimer::running() const
{
    return m_updateTimer.isActive();
}

void FrameTimer::setRunning(bool value)
{
    if (value && !m_updateTimer.isActive()) {
        m_updateTimer.start();
        emit runningChanged();
    } else if (!value && m_updateTimer.isActive()) {
        m_updateTimer.stop();
        emit runningChanged();
    }
}

/*!
    \qmlproperty int FrameTimer::interval

    The interval, in milliseconds, between update() calls while \l{FrameTimer::running}{running} is \c true.

    \sa update() running
*/
int FrameTimer::interval() const
{
    return m_updateTimer.interval();
}

void FrameTimer::setInterval(int value)
{
    if (value != m_updateTimer.interval()) {
        m_updateTimer.setInterval(value);
        emit intervalChanged();
    }
}

QT_END_NAMESPACE_AM