From f280efc6201adf8933c9d48a1d5b6983ee9fed9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Fri, 4 May 2012 13:10:41 +0200 Subject: Added QScreen::refreshRate() to get the vertical refresh rate. To give applications that want it the option to use a fixed timestep for animations, and to avoid having values of 60 hard-coded (we have a couple of those in qtdeclarative/src/quick already), we need to know the refresh rates of the screens we are rendering to. Change-Id: Ife49162e830440ad7eab563a27e8aebbbafc5fc5 Reviewed-by: Girish Ramakrishnan Reviewed-by: Friedemann Kleint Reviewed-by: Lars Knoll --- src/gui/kernel/qguiapplication.cpp | 19 +++++++++++++++++++ src/gui/kernel/qguiapplication_p.h | 1 + src/gui/kernel/qplatformscreen.h | 2 ++ src/gui/kernel/qplatformscreen_qpa.cpp | 11 +++++++++++ src/gui/kernel/qscreen.cpp | 10 ++++++++++ src/gui/kernel/qscreen.h | 4 ++++ src/gui/kernel/qscreen_p.h | 2 ++ src/gui/kernel/qwindowsysteminterface_qpa.cpp | 7 +++++++ src/gui/kernel/qwindowsysteminterface_qpa.h | 1 + src/gui/kernel/qwindowsysteminterface_qpa_p.h | 9 +++++++++ 10 files changed, 66 insertions(+) (limited to 'src/gui/kernel') diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 631592aace..62303eeea2 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1090,6 +1090,10 @@ void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePriv QGuiApplicationPrivate::reportLogicalDotsPerInchChange( static_cast(e)); break; + case QWindowSystemInterfacePrivate::ScreenRefreshRate: + QGuiApplicationPrivate::reportRefreshRateChange( + static_cast(e)); + break; case QWindowSystemInterfacePrivate::ThemeChange: QGuiApplicationPrivate::processThemeChanged( static_cast(e)); @@ -1750,6 +1754,21 @@ void QGuiApplicationPrivate::reportLogicalDotsPerInchChange(QWindowSystemInterfa emit s->logicalDotsPerInchChanged(s->logicalDotsPerInch()); } +void QGuiApplicationPrivate::reportRefreshRateChange(QWindowSystemInterfacePrivate::ScreenRefreshRateEvent *e) +{ + // This operation only makes sense after the QGuiApplication constructor runs + if (QCoreApplication::startingUp()) + return; + + if (!e->screen) + return; + + QScreen *s = e->screen.data(); + s->d_func()->refreshRate = e->rate; + + emit s->refreshRateChanged(s->refreshRate()); +} + void QGuiApplicationPrivate::processExposeEvent(QWindowSystemInterfacePrivate::ExposeEvent *e) { if (!e->exposed) diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index cb3587ba63..860c41d648 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -117,6 +117,7 @@ public: static void reportGeometryChange(QWindowSystemInterfacePrivate::ScreenGeometryEvent *e); static void reportAvailableGeometryChange(QWindowSystemInterfacePrivate::ScreenAvailableGeometryEvent *e); static void reportLogicalDotsPerInchChange(QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent *e); + static void reportRefreshRateChange(QWindowSystemInterfacePrivate::ScreenRefreshRateEvent *e); static void processThemeChanged(QWindowSystemInterfacePrivate::ThemeChangeEvent *tce); static void processExposeEvent(QWindowSystemInterfacePrivate::ExposeEvent *e); diff --git a/src/gui/kernel/qplatformscreen.h b/src/gui/kernel/qplatformscreen.h index 075dfb114b..1743024d91 100644 --- a/src/gui/kernel/qplatformscreen.h +++ b/src/gui/kernel/qplatformscreen.h @@ -101,6 +101,8 @@ public: virtual QSizeF physicalSize() const; virtual QDpi logicalDpi() const; + virtual qreal refreshRate() const; + virtual Qt::ScreenOrientation orientation() const; virtual QWindow *topLevelAt(const QPoint &point) const; diff --git a/src/gui/kernel/qplatformscreen_qpa.cpp b/src/gui/kernel/qplatformscreen_qpa.cpp index 6c2f98beb4..1fd96a80a5 100644 --- a/src/gui/kernel/qplatformscreen_qpa.cpp +++ b/src/gui/kernel/qplatformscreen_qpa.cpp @@ -160,6 +160,17 @@ QDpi QPlatformScreen::logicalDpi() const 25.4 * s.height() / ps.height()); } +/*! + Reimplement this function in subclass to return the vertical refresh rate + of the screen, in Hz. + + The default returns 60, a sensible default for modern displays. +*/ +qreal QPlatformScreen::refreshRate() const +{ + return 60; +} + /*! Reimplement this function in subclass to return the current orientation of the screen, for example based on accelerometer data to determine diff --git a/src/gui/kernel/qscreen.cpp b/src/gui/kernel/qscreen.cpp index 716caacb40..2e0df43e66 100644 --- a/src/gui/kernel/qscreen.cpp +++ b/src/gui/kernel/qscreen.cpp @@ -363,6 +363,16 @@ Qt::ScreenOrientation QScreen::orientation() const return d->orientation == Qt::PrimaryOrientation ? primaryOrientation() : d->orientation; } +/*! + \property QScreen::refreshRate + \brief the approximate vertical refresh rate of the screen in Hz +*/ +qreal QScreen::refreshRate() const +{ + Q_D(const QScreen); + return d->refreshRate; +} + /*! \property QScreen::primaryOrientation \brief the primary screen orientation diff --git a/src/gui/kernel/qscreen.h b/src/gui/kernel/qscreen.h index f69e04a595..17f3cd3d43 100644 --- a/src/gui/kernel/qscreen.h +++ b/src/gui/kernel/qscreen.h @@ -83,6 +83,7 @@ class Q_GUI_EXPORT QScreen : public QObject Q_PROPERTY(QRect availableGeometry READ availableGeometry NOTIFY availableGeometryChanged) Q_PROPERTY(Qt::ScreenOrientation primaryOrientation READ orientation NOTIFY primaryOrientationChanged) Q_PROPERTY(Qt::ScreenOrientation orientation READ orientation NOTIFY orientationChanged) + Q_PROPERTY(qreal refreshRate READ refreshRate NOTIFY refreshRateChanged) public: QPlatformScreen *handle() const; @@ -127,6 +128,8 @@ public: QPixmap grabWindow(WId window, int x, int y, int w, int h); + qreal refreshRate() const; + Q_SIGNALS: void sizeChanged(const QSize &size); void geometryChanged(const QRect &geometry); @@ -140,6 +143,7 @@ Q_SIGNALS: void availableGeometryChanged(const QRect &rect); void primaryOrientationChanged(Qt::ScreenOrientation orientation); void orientationChanged(Qt::ScreenOrientation orientation); + void refreshRateChanged(qreal refreshRate); private: explicit QScreen(QPlatformScreen *screen); diff --git a/src/gui/kernel/qscreen_p.h b/src/gui/kernel/qscreen_p.h index 0167384b01..b0e1b8671a 100644 --- a/src/gui/kernel/qscreen_p.h +++ b/src/gui/kernel/qscreen_p.h @@ -63,6 +63,7 @@ public: geometry = screen->geometry(); availableGeometry = screen->availableGeometry(); logicalDpi = screen->logicalDpi(); + refreshRate = screen->refreshRate(); updatePrimaryOrientation(); } @@ -74,6 +75,7 @@ public: QRect geometry; QRect availableGeometry; QDpi logicalDpi; + qreal refreshRate; QPlatformScreen *platformScreen; }; diff --git a/src/gui/kernel/qwindowsysteminterface_qpa.cpp b/src/gui/kernel/qwindowsysteminterface_qpa.cpp index 83cf8c338c..c3136b0b0a 100644 --- a/src/gui/kernel/qwindowsysteminterface_qpa.cpp +++ b/src/gui/kernel/qwindowsysteminterface_qpa.cpp @@ -434,6 +434,13 @@ void QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(QScreen *scree QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); } +void QWindowSystemInterface::handleScreenRefreshRateChange(QScreen *screen, qreal newRefreshRate) +{ + QWindowSystemInterfacePrivate::ScreenRefreshRateEvent *e = + new QWindowSystemInterfacePrivate::ScreenRefreshRateEvent(screen, newRefreshRate); + QWindowSystemInterfacePrivate::queueWindowSystemEvent(e); +} + void QWindowSystemInterface::handleThemeChange(QWindow *tlw) { QWindowSystemInterfacePrivate::ThemeChangeEvent *e = new QWindowSystemInterfacePrivate::ThemeChangeEvent(tlw); diff --git a/src/gui/kernel/qwindowsysteminterface_qpa.h b/src/gui/kernel/qwindowsysteminterface_qpa.h index 1fbf430bf9..7e0ebf0efc 100644 --- a/src/gui/kernel/qwindowsysteminterface_qpa.h +++ b/src/gui/kernel/qwindowsysteminterface_qpa.h @@ -144,6 +144,7 @@ public: static void handleScreenGeometryChange(QScreen *screen, const QRect &newGeometry); static void handleScreenAvailableGeometryChange(QScreen *screen, const QRect &newAvailableGeometry); static void handleScreenLogicalDotsPerInchChange(QScreen *screen, qreal newDpiX, qreal newDpiY); + static void handleScreenRefreshRateChange(QScreen *screen, qreal newRefreshRate); static void handleThemeChange(QWindow *tlw); diff --git a/src/gui/kernel/qwindowsysteminterface_qpa_p.h b/src/gui/kernel/qwindowsysteminterface_qpa_p.h index d7be7699e9..c7ad197b3c 100644 --- a/src/gui/kernel/qwindowsysteminterface_qpa_p.h +++ b/src/gui/kernel/qwindowsysteminterface_qpa_p.h @@ -66,6 +66,7 @@ public: ScreenGeometry, ScreenAvailableGeometry, ScreenLogicalDotsPerInch, + ScreenRefreshRate, ThemeChange, Expose }; @@ -233,6 +234,14 @@ public: qreal dpiY; }; + class ScreenRefreshRateEvent : public WindowSystemEvent { + public: + ScreenRefreshRateEvent(QScreen *s, qreal r) + : WindowSystemEvent(ScreenRefreshRate), screen(s), rate(r) { } + QWeakPointer screen; + qreal rate; + }; + class ThemeChangeEvent : public WindowSystemEvent { public: explicit ThemeChangeEvent(QWindow * w) -- cgit v1.2.3