From efdecff085d6e9d8c738bd681757c8cc18a3a451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 31 Aug 2016 18:15:52 +0200 Subject: multiwindow: Replace per window fps timers with unified approach The FPS is now calculated and output on the command line in a single place. The animated fps counter has been replaced with a vertical line which should make it easier to observe tearing issues when vsync is disabled. Change-Id: Id356fc1958c048d85aba48edfed59124454038d4 Reviewed-by: Simon Hausmann --- tests/manual/qopenglwindow/multiwindow/main.cpp | 64 ++++++++++++++++++------- 1 file changed, 46 insertions(+), 18 deletions(-) (limited to 'tests/manual/qopenglwindow/multiwindow/main.cpp') diff --git a/tests/manual/qopenglwindow/multiwindow/main.cpp b/tests/manual/qopenglwindow/multiwindow/main.cpp index 17442dbae5..8b17226366 100644 --- a/tests/manual/qopenglwindow/multiwindow/main.cpp +++ b/tests/manual/qopenglwindow/multiwindow/main.cpp @@ -60,7 +60,7 @@ class Window : public QOpenGLWindow { Q_OBJECT public: - Window(int index) : windowNumber(index + 1), y(0), fps(0) { + Window(int index) : windowNumber(index + 1), x(0), framesSwapped(0) { color = QColor::fromHsl((index * 30) % 360, 255, 127).toRgb(); @@ -69,7 +69,6 @@ public: setObjectName(QString("Window %1").arg(windowNumber)); connect(this, SIGNAL(frameSwapped()), SLOT(frameSwapped())); - fpsTimer.start(); } void paintGL() { @@ -77,23 +76,14 @@ public: f->glClearColor(color.redF(), color.greenF(), color.blueF(), 1); f->glClear(GL_COLOR_BUFFER_BIT); - QPainter p(this); - p.setPen(Qt::white); - p.drawText(QPoint(20, y), QString(QLatin1String("Window %1 (%2 FPS)")).arg(windowNumber).arg(fps)); - y += 1; - if (y > height() - 20) - y = 20; + QPainter painter(this); + painter.drawLine(x, 0, x, height()); + x = ++x % width(); } public slots: void frameSwapped() { ++framesSwapped; - if (fpsTimer.elapsed() > 1000) { - fps = qRound(framesSwapped * (1000.0 / fpsTimer.elapsed())); - framesSwapped = 0; - fpsTimer.restart(); - } - update(); } @@ -107,14 +97,46 @@ protected: private: int windowNumber; QColor color; - - int y; + int x; int framesSwapped; - QElapsedTimer fpsTimer; - int fps; + friend void printFps(); }; +static const qreal kFpsInterval = 500; + +void printFps() +{ + static QElapsedTimer timer; + if (!timer.isValid()) { + timer.start(); + return; + } + + const qreal frameFactor = (kFpsInterval / timer.elapsed()) * (1000.0 / kFpsInterval); + + QDebug output = qDebug().nospace(); + + qreal averageFps = 0; + const QWindowList windows = QGuiApplication::topLevelWindows(); + for (int i = 0; i < windows.size(); ++i) { + Window *w = qobject_cast(windows.at(i)); + Q_ASSERT(w); + + int fps = qRound(w->framesSwapped * frameFactor); + output << (i + 1) << "=" << fps << ", "; + + averageFps += fps; + w->framesSwapped = 0; + } + averageFps = qRound(averageFps / windows.size()); + qreal msPerFrame = 1000.0 / averageFps; + + output << "avg=" << averageFps << ", ms=" << msPerFrame; + + timer.restart(); +} + int main(int argc, char **argv) { QGuiApplication app(argc, argv); @@ -177,6 +199,12 @@ int main(int argc, char **argv) w->showNormal(); } + QTimer fpsTimer; + fpsTimer.setInterval(kFpsInterval); + fpsTimer.setTimerType(Qt::PreciseTimer); + QObject::connect(&fpsTimer, &QTimer::timeout, &printFps); + fpsTimer.start(); + int r = app.exec(); qDeleteAll(windows); return r; -- cgit v1.2.3