diff options
author | Morten Sørvig <morten.sorvig@qt.io> | 2023-03-03 16:00:47 +0100 |
---|---|---|
committer | Morten Sørvig <morten.sorvig@qt.io> | 2023-04-11 17:13:14 +0100 |
commit | 3965d808c086af9a4e4740ab2c7efe1a7ef6cb01 (patch) | |
tree | e6ef7c027ea46d96382441539d8e0ad5abf2942a /src/gui/kernel/qwindow.cpp | |
parent | fc9853dcbf546c82a46cd526c04b80d4e59e99f0 (diff) |
QWindow: Cache the devicePixelRatio value
Make each QWindow instance cache the current DPR value.
This will make calling QWindow::devicePixelRatio() less
costly, since it now does not have to compute the DPR
value on each call.
The cache is invalidated when the DevicePixelRatioChange
event is sent. The common logic for handling this is
implemented in QWindowPrivate::updateDevicePixelRatio().
Change-Id: I97231a230347358d8e565d2fd62e8a398adaedfc
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/gui/kernel/qwindow.cpp')
-rw-r--r-- | src/gui/kernel/qwindow.cpp | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 2f775ec664..81a4469a9e 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -213,8 +213,10 @@ void QWindowPrivate::init(QScreen *targetScreen) isWindow = true; parentWindow = static_cast<QWindow *>(q->QObject::parent()); + QScreen *connectScreen = targetScreen ? targetScreen : QGuiApplication::primaryScreen(); + if (!parentWindow) - connectToScreen(targetScreen ? targetScreen : QGuiApplication::primaryScreen()); + connectToScreen(connectScreen); // If your application aborts here, you are probably creating a QWindow // before the screen list is populated. @@ -224,6 +226,7 @@ void QWindowPrivate::init(QScreen *targetScreen) QGuiApplicationPrivate::window_list.prepend(q); requestedFormat = QSurfaceFormat::defaultFormat(); + devicePixelRatio = connectScreen->devicePixelRatio(); } /*! @@ -505,8 +508,6 @@ void QWindowPrivate::create(bool recursive, WId nativeHandle) // the platformWindow, if there was one, is now gone, so make this flag reflect reality now updateRequestPending = false; - const qreal currentDevicePixelRatio = q->devicePixelRatio(); - if (q->parent()) q->parent()->create(); @@ -552,10 +553,7 @@ void QWindowPrivate::create(bool recursive, WId nativeHandle) QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceCreated); QGuiApplication::sendEvent(q, &e); - if (!qFuzzyCompare(currentDevicePixelRatio, q->devicePixelRatio())) { - QEvent dprChangeEvent(QEvent::DevicePixelRatioChange); - QGuiApplication::sendEvent(q, &dprChangeEvent); - } + updateDevicePixelRatio(); if (needsUpdate) q->requestUpdate(); @@ -1333,14 +1331,29 @@ Qt::ScreenOrientation QWindow::contentOrientation() const qreal QWindow::devicePixelRatio() const { Q_D(const QWindow); + return d->devicePixelRatio; +} + +/* + Updates the cached devicePixelRatio value by polling for a new value. + Sends QEvent::DevicePixelRatioChange to the window if the DPR has changed. +*/ +void QWindowPrivate::updateDevicePixelRatio() +{ + Q_Q(QWindow); // If there is no platform window use the associated screen's devicePixelRatio, // which typically is the primary screen and will be correct for single-display // systems (a very common case). - if (!d->platformWindow) - return screen()->devicePixelRatio(); + const qreal newDevicePixelRatio = platformWindow ? + platformWindow->devicePixelRatio() * QHighDpiScaling::factor(q) : q->screen()->devicePixelRatio(); + + if (newDevicePixelRatio == devicePixelRatio) + return; - return d->platformWindow->devicePixelRatio() * QHighDpiScaling::factor(this); + devicePixelRatio = newDevicePixelRatio; + QEvent dprChangeEvent(QEvent::DevicePixelRatioChange); + QGuiApplication::sendEvent(q, &dprChangeEvent); } Qt::WindowState QWindowPrivate::effectiveState(Qt::WindowStates state) |