diff options
7 files changed, 100 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 diff --git a/tests/auto/qml/debugger/qqmlpreview/tst_qqmlpreview.cpp b/tests/auto/qml/debugger/qqmlpreview/tst_qqmlpreview.cpp index 152a4a7f9f..9477603654 100644 --- a/tests/auto/qml/debugger/qqmlpreview/tst_qqmlpreview.cpp +++ b/tests/auto/qml/debugger/qqmlpreview/tst_qqmlpreview.cpp @@ -57,6 +57,7 @@ private: QStringList m_filesNotFound; QStringList m_directories; QStringList m_serviceErrors; + quint16 m_frames = 0; private slots: void cleanup() final; @@ -67,6 +68,7 @@ private slots: void blacklist(); void error(); void zoom(); + void fps(); }; QQmlDebugTest::ConnectResult tst_QQmlPreview::startQmlProcess(const QString &qmlFile) @@ -102,6 +104,9 @@ QList<QQmlDebugClient *> tst_QQmlPreview::createClients() QObject::connect(m_client, &QQmlPreviewClient::error, this, [this](const QString &error) { m_serviceErrors.append(error); }); + QObject::connect(m_client, &QQmlPreviewClient::fps, this, [this](quint16 frames) { + m_frames += frames; + }); return QList<QQmlDebugClient *>({m_client}); } @@ -135,6 +140,7 @@ void tst_QQmlPreview::cleanup() m_files.clear(); m_filesNotFound.clear(); m_serviceErrors.clear(); + m_frames = 0; } void tst_QQmlPreview::connect() @@ -323,6 +329,18 @@ void tst_QQmlPreview::zoom() QVERIFY(m_serviceErrors.isEmpty()); } +void tst_QQmlPreview::fps() +{ + const QString file("qtquick2.qml"); + QCOMPARE(startQmlProcess(file), ConnectSuccess); + QVERIFY(m_client); + m_client->triggerLoad(testFileUrl(file)); + if (QGuiApplication::platformName() != "offscreen") + QTRY_VERIFY(m_frames > 100); + else + QSKIP("offscreen rendering doesn't produce any frames"); +} + QTEST_MAIN(tst_QQmlPreview) #include "tst_qqmlpreview.moc" |