From a705b4ec1f6f7133390054f8b6b8077ef0550311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 22 Jun 2015 12:32:11 +0200 Subject: Introduce cross platform high-dpi scaling Add a coordinate scaling layer to QtGui, which supports 'devicePixelRatio' type high-dpi on all platforms, in production and also for development and testing purposes. High-DPI scaling is opt-in, by setting environment variables: QT_SCALE_FACTOR - sets a global scale factor QT_AUTO_SCREEN_SCALE_FACTOR - sets per-screen scale factors, where the scale factors are provided by the platform plugin. This QtGui scaling can be used instead of or in addition to scaling done by the window system. This distinction is not visible to applications [when they use Qt API], which will see a change in the value returned by the devicePixelRatio() accessors as usual. Introduce a new (private to Qt) coordinate system: native pixels. The coordinate system stack now looks like: device-independent pixels (app, upper parts of Qt) native pixels (lower parts of Qt Gui, platform plugins) device pixels (backing stores and OpenGL) Add private QHighDpi namespace with scaling functions that convert between device-independent pixels and native pixels: T toNativePixels(T, QWindow *); T fromNativePixels(T, QWindow *); Add scaling calls the QWindow (and friends) cross-platform implementation, around the calls to QPlatformWindow functions. QPlatformWindow now uses native coordinates - platform code remains largely unchanged since native coordinates are window system coordinates. QWindow now uses (possibly) scaled coordinates. This means that platform plugins no longer can rely on QWindow::geometry() and related functions. QPlatformWindow::windowGeometry() and other convenience functions have been added for use when the platform plugin needs to convert scaled geometry to native geometry. Add Qt::AA_NoHighDpiScaling, which can be use to disable any scaling in QtGui, effectively ignoring the environment variables. (Note that this does not disable any scaling done by the window system.) Contributions from Friedemann and Paul. Task-number: QTBUG-46615 Change-Id: I673bbd69c130e73b13cce83be11bfb28f580bf60 Reviewed-by: Lars Knoll --- src/gui/kernel/qwindow.cpp | 55 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 12 deletions(-) (limited to 'src/gui/kernel/qwindow.cpp') diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 81e00c8d95..e93e964c6b 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -47,6 +47,7 @@ #ifndef QT_NO_ACCESSIBILITY # include "qaccessible.h" #endif +#include "qhighdpiscaling_p.h" #include @@ -1085,13 +1086,13 @@ qreal QWindow::devicePixelRatio() const { Q_D(const QWindow); - // If there is no platform window, do the second best thing and - // return the app global devicePixelRatio. This is the highest - // devicePixelRatio found on the system screens, and will be - // correct for single-display systems (a very common case). + // If there is no platform window use the app global devicePixelRatio, + // which is the the highest devicePixelRatio found on the system + // screens, and will be correct for single-display systems (a very common case). if (!d->platformWindow) return qApp->devicePixelRatio(); - return d->platformWindow->devicePixelRatio(); + + return d->platformWindow->devicePixelRatio() * QHighDpiScaling::factor(this); } /*! @@ -1431,7 +1432,13 @@ void QWindow::setGeometry(const QRect &rect) d->positionPolicy = QWindowPrivate::WindowFrameExclusive; if (d->platformWindow) { - d->platformWindow->setGeometry(rect); + QRect nativeRect; + QScreen *newScreen = d->screenForGeometry(rect); + if (newScreen && isTopLevel()) + nativeRect = QHighDpi::toNativePixels(rect, newScreen); + else + nativeRect = QHighDpi::toNativePixels(rect, this); + d->platformWindow->setGeometry(nativeRect); } else { d->geometry = rect; @@ -1446,6 +1453,30 @@ void QWindow::setGeometry(const QRect &rect) } } +/* + This is equivalent to QPlatformWindow::screenForGeometry, but in platform + independent coordinates. The duplication is unfortunate, but there is a + chicken and egg problem here: we cannot convert to native coordinates + before we know which screen we are on. +*/ +QScreen *QWindowPrivate::screenForGeometry(const QRect &newGeometry) +{ + Q_Q(QWindow); + QScreen *currentScreen = q->screen(); + QScreen *fallback = currentScreen; + QPoint center = newGeometry.center(); + if (!q->parent() && currentScreen && !currentScreen->geometry().contains(center)) { + Q_FOREACH (QScreen* screen, currentScreen->virtualSiblings()) { + if (screen->geometry().contains(center)) + return screen; + if (screen->geometry().intersects(newGeometry)) + fallback = screen; + } + } + return fallback; +} + + /*! Returns the geometry of the window, excluding its window frame. @@ -1455,7 +1486,7 @@ QRect QWindow::geometry() const { Q_D(const QWindow); if (d->platformWindow) - return d->platformWindow->geometry(); + return QHighDpi::fromNativePixels(d->platformWindow->geometry(), this); return d->geometry; } @@ -1468,7 +1499,7 @@ QMargins QWindow::frameMargins() const { Q_D(const QWindow); if (d->platformWindow) - return d->platformWindow->frameMargins(); + return QHighDpi::fromNativePixels(d->platformWindow->frameMargins(), this); return QMargins(); } @@ -1482,7 +1513,7 @@ QRect QWindow::frameGeometry() const Q_D(const QWindow); if (d->platformWindow) { QMargins m = frameMargins(); - return d->platformWindow->geometry().adjusted(-m.left(), -m.top(), m.right(), m.bottom()); + return QHighDpi::fromNativePixels(d->platformWindow->geometry(), this).adjusted(-m.left(), -m.top(), m.right(), m.bottom()); } return d->geometry; } @@ -1499,7 +1530,7 @@ QPoint QWindow::framePosition() const Q_D(const QWindow); if (d->platformWindow) { QMargins margins = frameMargins(); - return d->platformWindow->geometry().topLeft() - QPoint(margins.left(), margins.top()); + return QHighDpi::fromNativePixels(d->platformWindow->geometry().topLeft(), this) - QPoint(margins.left(), margins.top()); } return d->geometry.topLeft(); } @@ -1515,7 +1546,7 @@ void QWindow::setFramePosition(const QPoint &point) d->positionPolicy = QWindowPrivate::WindowFrameInclusive; d->positionAutomatic = false; if (d->platformWindow) { - d->platformWindow->setGeometry(QRect(point, size())); + d->platformWindow->setGeometry(QHighDpi::toNativePixels(QRect(point, size()), this)); } else { d->geometry.moveTopLeft(point); } @@ -1575,7 +1606,7 @@ void QWindow::resize(const QSize &newSize) { Q_D(QWindow); if (d->platformWindow) { - d->platformWindow->setGeometry(QRect(position(), newSize)); + d->platformWindow->setGeometry(QHighDpi::toNativePixels(QRect(position(), newSize), this)); } else { const QSize oldSize = d->geometry.size(); d->geometry.setSize(newSize); -- cgit v1.2.3