diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2018-07-11 09:12:00 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2018-07-18 07:45:24 +0000 |
commit | dde49f989e6f77a8db87d7fafc2d4ced34113135 (patch) | |
tree | 5684c1e0e166e5dd5d575697fde728f81f00c289 /src | |
parent | 7bf74dbddb25d48e267788c02ee7d06288bfada9 (diff) |
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 <simon.hausmann@qt.io>
Diffstat (limited to 'src')
6 files changed, 82 insertions, 16 deletions
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<QWindow *>(object)) { - m_currentWindow = qobject_cast<QQuickWindow *>(window); + setCurrentWindow(qobject_cast<QQuickWindow *>(window)); for (QWindow *otherWindow : QGuiApplication::allWindows()) { if (QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(otherWindow)) { if (quickWindow == m_currentWindow) @@ -259,7 +262,7 @@ void QQmlPreviewHandler::showObject(QObject *object) } } } else if (QQuickItem *item = qobject_cast<QQuickItem *>(object)) { - m_currentWindow = nullptr; + setCurrentWindow(nullptr); for (QWindow *window : QGuiApplication::allWindows()) { if (QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(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<QQuickItem> m_dummyItem; QList<QQmlEngine *> m_engines; @@ -85,6 +90,9 @@ private: QPointer<QQuickWindow> 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<qint8>(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); diff --git a/src/qmldebug/qqmlpreviewclient.cpp b/src/qmldebug/qqmlpreviewclient.cpp index fa040e226b..33a2f53ca4 100644 --- a/src/qmldebug/qqmlpreviewclient.cpp +++ b/src/qmldebug/qqmlpreviewclient.cpp @@ -61,18 +61,29 @@ void QQmlPreviewClient::messageReceived(const QByteArray &message) qint8 command; packet >> command; - if (command == Error) { + switch (command) { + case Error: { QString seviceError; packet >> seviceError; emit error(seviceError); - return; + break; + } + case Request: { + QString fileName; + packet >> fileName; + emit request(fileName); + break; + } + case Fps: { + quint16 frames; + packet >> frames; + emit fps(frames); + break; + } + default: + emit error(QString::fromLatin1("Unknown command received: %1").arg(command)); + break; } - - Q_ASSERT(command == Request); - - QString fileName; - packet >> fileName; - emit request(fileName); } void QQmlPreviewClient::sendDirectory(const QString &path, const QStringList &entries) diff --git a/src/qmldebug/qqmlpreviewclient_p.h b/src/qmldebug/qqmlpreviewclient_p.h index 49d147c866..d79ecefe32 100644 --- a/src/qmldebug/qqmlpreviewclient_p.h +++ b/src/qmldebug/qqmlpreviewclient_p.h @@ -71,7 +71,8 @@ public: Rerun, Directory, ClearCache, - Zoom + Zoom, + Fps }; QQmlPreviewClient(QQmlDebugConnection *parent); @@ -88,6 +89,7 @@ public: signals: void request(const QString &path); void error(const QString &message); + void fps(quint16 frames); }; QT_END_NAMESPACE |