Commit 70fca987 authored by Pasi Keranen's avatar Pasi Keranen Committed by Pasi Keränen
Browse files

Added fps counter as read only attribute of Canvas3D.


User requested feature.

Change-Id: I1c6481cc58b39181f6592549d90aa76352a50f08
Reviewed-by: default avatarLaszlo Agocs <laszlo.agocs@theqtcompany.com>
Showing with 38 additions and 0 deletions
......@@ -50,6 +50,7 @@
#include <QtGui/QOpenGLFramebufferObject>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlContext>
#include <QtCore/QElapsedTimer>
QT_CANVAS3D_BEGIN_NAMESPACE
......@@ -89,6 +90,7 @@ Canvas::Canvas(QQuickItem *parent):
m_glContext(0),
m_glContextQt(0),
m_contextWindow(0),
m_fps(0),
m_maxSamples(0),
m_devicePixelRatio(1.0f),
m_isOpenGLES2(false),
......@@ -617,6 +619,16 @@ void Canvas::bindCurrentRenderTarget()
}
}
/*!
* \qmlproperty uint Canvas3D::fps
* This property specifies the current frames per seconds, the value is calculated every
* 500 ms.
*/
uint Canvas::fps()
{
return m_fps;
}
/*!
* \internal
*/
......@@ -692,6 +704,27 @@ void Canvas::renderNext()
// get unexpected results.
glFlush();
glFinish();
// Update frames per second reading after glFinish()
static QElapsedTimer timer;
static int frames;
if (frames == 0) {
timer.start();
} else if (timer.elapsed() > 500) {
qreal avgtime = timer.elapsed() / (qreal) frames;
uint avgFps = uint(1000.0 / avgtime);
if (avgFps != m_fps) {
m_fps = avgFps;
emit fpsChanged(avgFps);
}
timer.start();
frames = 0;
}
++frames;
// Swap
qSwap(m_renderFbo, m_displayFbo);
if (m_logAllCalls) qDebug() << "Canvas3D::" << __FUNCTION__
<< " Displaying texture: " << m_displayFbo->texture()
......
......@@ -66,6 +66,7 @@ class QT_CANVAS3D_EXPORT Canvas : public QQuickItem, QOpenGLFunctions
Q_PROPERTY(bool logAllCalls READ logAllCalls WRITE setLogAllCalls NOTIFY logAllCallsChanged)
Q_PROPERTY(bool logAllErrors READ logAllErrors WRITE setLogAllErrors NOTIFY logAllErrorsChanged)
Q_PROPERTY(float devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged)
Q_PROPERTY(uint fps READ fps NOTIFY fpsChanged)
public:
Canvas(QQuickItem *parent = 0);
......@@ -81,6 +82,8 @@ public:
void setLogAllErrors(bool logErrors);
bool logAllErrors() const;
uint fps();
Q_INVOKABLE CanvasContext *getContext(const QString &name);
Q_INVOKABLE CanvasContext *getContext(const QString &name, const QVariantMap &options);
CanvasContext *context();
......@@ -97,6 +100,7 @@ signals:
void logAllCallsChanged(bool logCalls);
void logAllErrorsChanged(bool logErrors);
void contextChanged(CanvasContext *context);
void fpsChanged(uint fps);
void initGL();
void renderGL();
......@@ -126,6 +130,7 @@ private:
QOpenGLContext *m_glContextQt;
QQuickWindow *m_contextWindow;
uint m_fps;
int m_maxSamples;
float m_devicePixelRatio;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment