diff --git a/Qt_Performance_Benchmark/FpsItem.qml b/Qt_Performance_Benchmark/FpsItem.qml deleted file mode 100644 index aaad964..0000000 --- a/Qt_Performance_Benchmark/FpsItem.qml +++ /dev/null @@ -1,59 +0,0 @@ -import QtQuick 2.0 -import QtQuick.Window 2.2 - -Rectangle { - id: root - property int frameCounter: 0 - property int frameCounterAvg: 0 - property int counter: 0 - property int fps: 0 - property int fpsAvg: 0 - - readonly property real dp: Screen.pixelDensity * 25.4/160 - - color: "black" - width: childrenRect.width + 10*dp; - height: childrenRect.height + 10*dp; - - Image { - id: spinnerImage - anchors.verticalCenter: parent.verticalCenter - x: 4 * dp - width: 36 * dp - height: width - source: "images/spinner.png" - NumberAnimation on rotation { - from:0 - to: 360 - duration: 800 - loops: Animation.Infinite - } - onRotationChanged: frameCounter++; - } - - Text { - anchors.left: spinnerImage.right - anchors.leftMargin: 8 * dp - anchors.verticalCenter: spinnerImage.verticalCenter - color: "#c0c0c0" - font.pixelSize: 18 * dp - text: "Ø " + root.fpsAvg + " | " + root.fps + " fps" - } - - Timer { - interval: 2000 - repeat: true - running: true - onTriggered: { - frameCounterAvg += frameCounter; - root.fps = frameCounter/2; - counter++; - frameCounter = 0; - if (counter >= 3) { - root.fpsAvg = frameCounterAvg/(2*counter) - frameCounterAvg = 0; - counter = 0; - } - } - } -} diff --git a/Qt_Performance_Benchmark/Qt_Performance_Benchmark.pro b/Qt_Performance_Benchmark/Qt_Performance_Benchmark.pro index 34ba75e..5f6f437 100644 --- a/Qt_Performance_Benchmark/Qt_Performance_Benchmark.pro +++ b/Qt_Performance_Benchmark/Qt_Performance_Benchmark.pro @@ -7,6 +7,7 @@ CONFIG += c++11 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + frequencymonitor.cpp \ main.cpp RESOURCES += qml.qrc @@ -21,3 +22,6 @@ QML_DESIGNER_IMPORT_PATH = qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target + +HEADERS += \ + frequencymonitor.h diff --git a/Qt_Performance_Benchmark/frequencymonitor.cpp b/Qt_Performance_Benchmark/frequencymonitor.cpp new file mode 100644 index 0000000..3befea7 --- /dev/null +++ b/Qt_Performance_Benchmark/frequencymonitor.cpp @@ -0,0 +1,65 @@ +#include "frequencymonitor.h" +#include + +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); + } +} diff --git a/Qt_Performance_Benchmark/frequencymonitor.h b/Qt_Performance_Benchmark/frequencymonitor.h new file mode 100644 index 0000000..4ddc576 --- /dev/null +++ b/Qt_Performance_Benchmark/frequencymonitor.h @@ -0,0 +1,42 @@ +#ifndef FREQUENCYMONITOR_H +#define FREQUENCYMONITOR_H + +#include +#include +#include + +class QQuickWindow; + +class FrequencyMonitor : public QQuickItem +{ + Q_OBJECT + Q_PROPERTY(int fps READ fps NOTIFY fpsChanged) + Q_PROPERTY(int refreshPeriod READ refreshPeriod WRITE setRefreshPeriod NOTIFY refreshPeriodChanged) +public: + explicit FrequencyMonitor(QQuickItem *parent = 0); + + int fps() const; + void setFps(int fps); + + // by default refresh one time per 1000 msec + int refreshPeriod() const; + void setRefreshPeriod(int msec); + +signals: + void fpsChanged(); + void refreshPeriodChanged(); + +public slots: + void handleAfterRendering(); + void handleWindowChanged(QQuickWindow * window); + +protected: + QMetaObject::Connection m_windowConnection; + QTime m_time; + int m_fps; + int m_counter; + int m_refreshPeriod; + +}; + +#endif // FREQUENCYMONITOR_H diff --git a/Qt_Performance_Benchmark/main.cpp b/Qt_Performance_Benchmark/main.cpp index db8d3b8..4c3fbf9 100644 --- a/Qt_Performance_Benchmark/main.cpp +++ b/Qt_Performance_Benchmark/main.cpp @@ -1,12 +1,18 @@ #include #include +#include "frequencymonitor.h" + + + int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); - + + qmlRegisterType("be.mindgoo.tools", 0, 1, "FrequencyMonitor"); + QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, diff --git a/Qt_Performance_Benchmark/main.qml b/Qt_Performance_Benchmark/main.qml index 8ba518e..0f050e2 100644 --- a/Qt_Performance_Benchmark/main.qml +++ b/Qt_Performance_Benchmark/main.qml @@ -1,6 +1,7 @@ import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.3 +import be.mindgoo.tools 0.1 Window { @@ -54,13 +55,14 @@ Window { onClicked: rootwin.onChecked(checked) } - FpsItem { - id: fpsItem - anchors.verticalCenterOffset: 106 - anchors.horizontalCenterOffset: -239 - anchors.centerIn: parent + FrequencyMonitor { + id: monitor + refreshPeriod: 500 } + Text { + text: monitor.fps + } } /*##^## diff --git a/Qt_Performance_Benchmark/qml.qrc b/Qt_Performance_Benchmark/qml.qrc index ceda5c0..5f6483a 100644 --- a/Qt_Performance_Benchmark/qml.qrc +++ b/Qt_Performance_Benchmark/qml.qrc @@ -1,6 +1,5 @@ main.qml - FpsItem.qml