1 | // Copyright (C) 2022 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "fpshelper.h" |
5 | |
6 | FpsHelper::FpsHelper() |
7 | { |
8 | setFlag(flag: QQuickItem::ItemHasContents); |
9 | |
10 | connect(sender: this, signal: &QQuickItem::enabledChanged, slot: [this]() { |
11 | if (isEnabled()) { |
12 | m_frames = 0; |
13 | m_timer.start(); |
14 | update(); |
15 | } |
16 | }); |
17 | |
18 | if (isEnabled()) |
19 | m_timer.start(); |
20 | } |
21 | |
22 | float FpsHelper::fps() const |
23 | { |
24 | return m_fps; |
25 | } |
26 | |
27 | QSGNode *FpsHelper::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) |
28 | { |
29 | m_frames++; |
30 | qint64 nsElapsed = m_timer.nsecsElapsed(); |
31 | float ms = float(nsElapsed / 1000000.0); |
32 | if (ms >= 1000.0f) { |
33 | float fps = m_frames / (ms / 1000.0f); |
34 | // Round to 0.2 accuracy |
35 | fps = std::round(x: fps * 5.0f) / 5.0f; |
36 | if (!qFuzzyCompare(p1: fps, p2: m_fps)) { |
37 | m_fps = fps; |
38 | Q_EMIT fpsChanged(); |
39 | } |
40 | m_frames = 0; |
41 | m_timer.restart(); |
42 | } |
43 | |
44 | // Call update in a loop if fps tracking is enabled |
45 | if (isEnabled()) |
46 | update(); |
47 | |
48 | return node; |
49 | } |
50 |