From dde49f989e6f77a8db87d7fafc2d4ced34113135 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 11 Jul 2018 09:12:00 +0200 Subject: QML Preview: Add a frames per second counter It is instructive to the client to know how many frames per second the current QML can achieve in the preview. Change-Id: I8b73e2b5218410d903a07dfe27c038663c84fdee Reviewed-by: Simon Hausmann --- .../qmldbg_preview/qqmlpreviewhandler.cpp | 45 +++++++++++++++++++--- .../qmltooling/qmldbg_preview/qqmlpreviewhandler.h | 8 ++++ .../qmldbg_preview/qqmlpreviewservice.cpp | 10 ++++- .../qmltooling/qmldbg_preview/qqmlpreviewservice.h | 4 +- 4 files changed, 60 insertions(+), 7 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.cpp b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.cpp index fdfa9dcc34..c6b28316ac 100644 --- a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.cpp +++ b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.cpp @@ -72,6 +72,9 @@ QQmlPreviewHandler::QQmlPreviewHandler(QObject *parent) : QObject(parent) || platformName == QStringLiteral("wayland")); QCoreApplication::instance()->installEventFilter(this); + + m_fpsTimer.setInterval(1000); + connect(&m_fpsTimer, &QTimer::timeout, this, &QQmlPreviewHandler::fpsTimerHit); } QQmlPreviewHandler::~QQmlPreviewHandler() @@ -227,7 +230,7 @@ void QQmlPreviewHandler::clear() { qDeleteAll(m_createdObjects); m_createdObjects.clear(); - m_currentWindow = nullptr; + setCurrentWindow(nullptr); } Qt::WindowFlags fixFlags(Qt::WindowFlags flags) @@ -249,7 +252,7 @@ Qt::WindowFlags fixFlags(Qt::WindowFlags flags) void QQmlPreviewHandler::showObject(QObject *object) { if (QWindow *window = qobject_cast(object)) { - m_currentWindow = qobject_cast(window); + setCurrentWindow(qobject_cast(window)); for (QWindow *otherWindow : QGuiApplication::allWindows()) { if (QQuickWindow *quickWindow = qobject_cast(otherWindow)) { if (quickWindow == m_currentWindow) @@ -259,7 +262,7 @@ void QQmlPreviewHandler::showObject(QObject *object) } } } else if (QQuickItem *item = qobject_cast(object)) { - m_currentWindow = nullptr; + setCurrentWindow(nullptr); for (QWindow *window : QGuiApplication::allWindows()) { if (QQuickWindow *quickWindow = qobject_cast(window)) { if (m_currentWindow != nullptr) { @@ -267,7 +270,7 @@ void QQmlPreviewHandler::showObject(QObject *object) "decide which one to use.")); return; } - m_currentWindow = quickWindow; + setCurrentWindow(quickWindow); } else { window->setVisible(false); window->setFlag(Qt::WindowStaysOnTopHint, false); @@ -275,7 +278,7 @@ void QQmlPreviewHandler::showObject(QObject *object) } if (m_currentWindow == nullptr) { - m_currentWindow = new QQuickWindow; + setCurrentWindow(new QQuickWindow); m_createdObjects.append(m_currentWindow.data()); } @@ -301,6 +304,38 @@ void QQmlPreviewHandler::showObject(QObject *object) } } +void QQmlPreviewHandler::setCurrentWindow(QQuickWindow *window) +{ + if (window == m_currentWindow.data()) + return; + + if (m_currentWindow) { + disconnect(m_currentWindow.data(), &QQuickWindow::frameSwapped, + this, &QQmlPreviewHandler::frameSwapped); + m_fpsTimer.stop(); + m_frames = 0; + } + + m_currentWindow = window; + + if (m_currentWindow) { + connect(m_currentWindow.data(), &QQuickWindow::frameSwapped, + this, &QQmlPreviewHandler::frameSwapped); + m_fpsTimer.start(); + } +} + +void QQmlPreviewHandler::frameSwapped() +{ + ++m_frames; +} + +void QQmlPreviewHandler::fpsTimerHit() +{ + emit fps(m_frames); + m_frames = 0; +} + void QQmlPreviewHandler::tryCreateObject() { if (!m_supportsMultipleWindows) diff --git a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.h b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.h index 78099fb1e5..53855b0f62 100644 --- a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.h +++ b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewhandler.h @@ -72,11 +72,16 @@ public: signals: void error(const QString &message); + void fps(quint16 frames); + protected: bool eventFilter(QObject *obj, QEvent *event); private: void tryCreateObject(); void showObject(QObject *object); + void setCurrentWindow(QQuickWindow *window); + void frameSwapped(); + void fpsTimerHit(); QScopedPointer m_dummyItem; QList m_engines; @@ -85,6 +90,9 @@ private: QPointer m_currentWindow; bool m_supportsMultipleWindows; QQmlPreviewPosition m_lastPosition; + + QTimer m_fpsTimer; + quint16 m_frames = 0; }; QT_END_NAMESPACE diff --git a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.cpp b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.cpp index 70e895e78c..5f78ce7752 100644 --- a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.cpp +++ b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.cpp @@ -55,7 +55,8 @@ QQmlPreviewServiceImpl::QQmlPreviewServiceImpl(QObject *parent) : connect(this, &QQmlPreviewServiceImpl::zoom, &m_handler, &QQmlPreviewHandler::zoom); connect(&m_handler, &QQmlPreviewHandler::error, this, &QQmlPreviewServiceImpl::forwardError, Qt::DirectConnection); - + connect(&m_handler, &QQmlPreviewHandler::fps, this, &QQmlPreviewServiceImpl::forwardFps, + Qt::DirectConnection); } QQmlPreviewServiceImpl::~QQmlPreviewServiceImpl() @@ -162,4 +163,11 @@ void QQmlPreviewServiceImpl::forwardError(const QString &error) emit messageToClient(name(), packet.data()); } +void QQmlPreviewServiceImpl::forwardFps(quint16 frames) +{ + QQmlDebugPacket packet; + packet << static_cast(Fps) << frames; + emit messageToClient(name(), packet.data()); +} + QT_END_NAMESPACE diff --git a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.h b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.h index 6c35ef52dd..e439b8ae69 100644 --- a/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.h +++ b/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewservice.h @@ -61,7 +61,8 @@ public: Rerun, Directory, ClearCache, - Zoom + Zoom, + Fps }; static const QString s_key; @@ -76,6 +77,7 @@ public: void forwardRequest(const QString &file); void forwardError(const QString &error); + void forwardFps(quint16 frames); signals: void error(const QString &file); -- cgit v1.2.3