0032-Update-Dpi-and-scale-factor-computation.patch 20.2 KB
Newer Older
Boudewijn Rempt's avatar
Boudewijn Rempt committed
1
diff --git a/src/gui/kernel/qhighdpiscaling.cpp b/src/gui/kernel/qhighdpiscaling.cpp
Boudewijn Rempt's avatar
Boudewijn Rempt committed
2
index 22e46e085..cdf6c8669 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
3 4
--- a/src/gui/kernel/qhighdpiscaling.cpp
+++ b/src/gui/kernel/qhighdpiscaling.cpp
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
@@ -44,6 +44,9 @@
 #include "private/qscreen_p.h"
 
 #include <QtCore/qdebug.h>
+#include <QtCore/qmetaobject.h>
+
+#include <algorithm>
 
 QT_BEGIN_NAMESPACE
 
@@ -54,6 +57,18 @@ static const char legacyDevicePixelEnvVar[] = "QT_DEVICE_PIXEL_RATIO";
 static const char scaleFactorEnvVar[] = "QT_SCALE_FACTOR";
 static const char autoScreenEnvVar[] = "QT_AUTO_SCREEN_SCALE_FACTOR";
 static const char screenFactorsEnvVar[] = "QT_SCREEN_SCALE_FACTORS";
+static const char scaleFactorRoundingPolicyEnvVar[] = "QT_SCALE_FACTOR_ROUNDING_POLICY";
+static const char dpiAdjustmentPolicyEnvVar[] = "QT_DPI_ADJUSTMENT_POLICY";
+static const char usePhysicalDpiEnvVar[] = "QT_USE_PHYSICAL_DPI";
+
+// Reads and interprets the given environment variable as a bool,
+// returns the default value if not set.
+static bool qEnvironmentVariableAsBool(const char *name, bool defaultValue)
+{
+    bool ok = false;
+    int value = qEnvironmentVariableIntValue(name, &ok);
+    return ok ? value > 0 : defaultValue;
+}
 
 static inline qreal initialGlobalScaleFactor()
 {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
34
@@ -248,6 +263,191 @@ static inline bool usePixelDensity()
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
             qgetenv(legacyDevicePixelEnvVar).compare("auto", Qt::CaseInsensitive) == 0);
 }
 
+qreal QHighDpiScaling::rawScaleFactor(const QPlatformScreen *screen)
+{
+    // Determine if physical DPI should be used
+    static bool usePhysicalDpi = qEnvironmentVariableAsBool(usePhysicalDpiEnvVar, false);
+
+    // Calculate scale factor beased on platform screen DPI values
+    qreal factor;
+    QDpi platformBaseDpi = screen->logicalBaseDpi();
+    if (usePhysicalDpi) {
+        qreal platformPhysicalDpi = screen->screen()->physicalDotsPerInch();
+        factor = qreal(platformPhysicalDpi) / qreal(platformBaseDpi.first);
+    } else {
+        QDpi platformLogicalDpi = screen->logicalDpi();
+        factor = qreal(platformLogicalDpi.first) / qreal(platformBaseDpi.first);
+    }
+
+    return factor;
+}
+
+template <class EnumType>
+struct EnumLookup
+{
+    const char *name;
+    EnumType value;
+};
+
+template <class EnumType>
+static bool operator==(const EnumLookup<EnumType> &e1, const EnumLookup<EnumType> &e2)
+{
+    return qstricmp(e1.name, e2.name) == 0;
+}
+
+template <class EnumType>
+static QByteArray joinEnumValues(const EnumLookup<EnumType> *i1, const EnumLookup<EnumType> *i2)
+{
+    QByteArray result;
+    for (; i1 < i2; ++i1) {
+        if (!result.isEmpty())
+            result += QByteArrayLiteral(", ");
+        result += i1->name;
+    }
+    return result;
+}
+
+using ScaleFactorRoundingPolicyLookup = EnumLookup<QHighDpiScaling::HighDpiScaleFactorRoundingPolicy>;
+
+static const ScaleFactorRoundingPolicyLookup scaleFactorRoundingPolicyLookup[] =
+{
+    {"Round", QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::Round},
+    {"Ceil", QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::Ceil},
+    {"Floor", QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::Floor},
+    {"RoundPreferFloor", QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor},
+    {"PassThrough", QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::PassThrough}
+};
+
+static QHighDpiScaling::HighDpiScaleFactorRoundingPolicy
+    lookupScaleFactorRoundingPolicy(const QByteArray &v)
+{
+    auto end = std::end(scaleFactorRoundingPolicyLookup);
+    auto it = std::find(std::begin(scaleFactorRoundingPolicyLookup), end,
+                        ScaleFactorRoundingPolicyLookup{v.constData(), QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::NotSet});
+    return it != end ? it->value : QHighDpiScaling::HighDpiScaleFactorRoundingPolicy::NotSet;
+}
+
+using DpiAdjustmentPolicyLookup = EnumLookup<QHighDpiScaling::DpiAdjustmentPolicy>;
+
+static const DpiAdjustmentPolicyLookup dpiAdjustmentPolicyLookup[] =
+{
+    {"AdjustDpi", QHighDpiScaling::DpiAdjustmentPolicy::Enabled},
+    {"DontAdjustDpi", QHighDpiScaling::DpiAdjustmentPolicy::Disabled},
+    {"AdjustUpOnly", QHighDpiScaling::DpiAdjustmentPolicy::UpOnly}
+};
+
+static QHighDpiScaling::DpiAdjustmentPolicy
+    lookupDpiAdjustmentPolicy(const QByteArray &v)
+{
+    auto end = std::end(dpiAdjustmentPolicyLookup);
+    auto it = std::find(std::begin(dpiAdjustmentPolicyLookup), end,
+                        DpiAdjustmentPolicyLookup{v.constData(), QHighDpiScaling::DpiAdjustmentPolicy::NotSet});
+    return it != end ? it->value : QHighDpiScaling::DpiAdjustmentPolicy::NotSet;
+}
+
+qreal QHighDpiScaling::roundScaleFactor(qreal rawFactor)
+{
+    // Apply scale factor rounding policy. Using mathematically correct rounding
+    // may not give the most desirable visual results, especially for
+    // critical fractions like .5. In general, rounding down results in visual
+    // sizes that are smaller than the ideal size, and opposite for rounding up.
+    // Rounding down is then preferable since "small UI" is a more acceptable
+    // high-DPI experience than "large UI".
+    static auto scaleFactorRoundingPolicy = HighDpiScaleFactorRoundingPolicy::NotSet;
+
+    // Determine rounding policy
+    if (scaleFactorRoundingPolicy == HighDpiScaleFactorRoundingPolicy::NotSet) {
+        // Check environment
+        if (qEnvironmentVariableIsSet(scaleFactorRoundingPolicyEnvVar)) {
+            QByteArray policyText = qgetenv(scaleFactorRoundingPolicyEnvVar);
+            auto policyEnumValue = lookupScaleFactorRoundingPolicy(policyText);
+            if (policyEnumValue != HighDpiScaleFactorRoundingPolicy::NotSet) {
+                scaleFactorRoundingPolicy = policyEnumValue;
+            } else {
+                auto values = joinEnumValues(std::begin(scaleFactorRoundingPolicyLookup),
+                                             std::end(scaleFactorRoundingPolicyLookup));
+                qWarning("Unknown scale factor rounding policy: %s. Supported values are: %s.",
+                         policyText.constData(), values.constData());
+            }
+        } else {
+            // Set default policy if no environment variable is set.
+            scaleFactorRoundingPolicy = HighDpiScaleFactorRoundingPolicy::RoundPreferFloor;
+        }
+    }
+
+    // Apply rounding policy.
+    qreal roundedFactor = rawFactor;
+    switch (scaleFactorRoundingPolicy) {
+    case HighDpiScaleFactorRoundingPolicy::Round:
+        roundedFactor = qRound(rawFactor);
+        break;
+    case HighDpiScaleFactorRoundingPolicy::Ceil:
+        roundedFactor = qCeil(rawFactor);
+        break;
+    case HighDpiScaleFactorRoundingPolicy::Floor:
+        roundedFactor = qFloor(rawFactor);
+        break;
+    case HighDpiScaleFactorRoundingPolicy::RoundPreferFloor:
+        // Round up for .75 and higher. This favors "small UI" over "large UI".
+        roundedFactor = rawFactor - qFloor(rawFactor) < 0.75
+            ? qFloor(rawFactor) : qCeil(rawFactor);
+        break;
+    case HighDpiScaleFactorRoundingPolicy::PassThrough:
+    case HighDpiScaleFactorRoundingPolicy::NotSet:
+        break;
+    }
+
+    // Don't round down to to zero; clamp the minimum (rounded) factor to 1.
+    // This is not a common case but can happen if a display reports a very
+    // low DPI.
+    if (scaleFactorRoundingPolicy != HighDpiScaleFactorRoundingPolicy::PassThrough)
+        roundedFactor = qMax(roundedFactor, qreal(1));
+
+    return roundedFactor;
+}
+
+QDpi QHighDpiScaling::effectiveLogicalDpi(const QPlatformScreen *screen, qreal rawFactor, qreal roundedFactor)
+{
+    // Apply DPI adjustment policy, if needed. If enabled this will change
+    // the reported logical DPI to account for the difference between the
+    // rounded scale factor and the actual scale factor. The effect
+    // is that text size will be correct for the screen dpi, but may be (slightly)
+    // out of sync with the rest of the UI. The amount of out-of-synch-ness
+    // depends on how well user code handles a non-standard DPI values, but
+    // since the adjustment is small (typically +/- 48 max) this might be OK.
+    static auto dpiAdjustmentPolicy = DpiAdjustmentPolicy::NotSet;
+
+    // Determine adjustment policy.
+    if (dpiAdjustmentPolicy == DpiAdjustmentPolicy::NotSet) {
+        if (qEnvironmentVariableIsSet(dpiAdjustmentPolicyEnvVar)) {
+            QByteArray policyText = qgetenv(dpiAdjustmentPolicyEnvVar);
+            auto policyEnumValue = lookupDpiAdjustmentPolicy(policyText);
+            if (policyEnumValue != DpiAdjustmentPolicy::NotSet) {
+                dpiAdjustmentPolicy = policyEnumValue;
+            } else {
+                auto values = joinEnumValues(std::begin(dpiAdjustmentPolicyLookup),
+                                             std::end(dpiAdjustmentPolicyLookup));
+                qWarning("Unknown DPI adjustment policy: %s. Supported values are: %s.",
+                         policyText.constData(), values.constData());
+            }
+        }
+        if (dpiAdjustmentPolicy == DpiAdjustmentPolicy::NotSet)
+            dpiAdjustmentPolicy = DpiAdjustmentPolicy::UpOnly;
+    }
+
+    // Apply adjustment policy.
+    const QDpi baseDpi = screen->logicalBaseDpi();
+    const qreal dpiAdjustmentFactor = rawFactor / roundedFactor;
+
+    // Return the base DPI for cases where there is no adjustment
+    if (dpiAdjustmentPolicy == DpiAdjustmentPolicy::Disabled)
+        return baseDpi;
+    if (dpiAdjustmentPolicy == DpiAdjustmentPolicy::UpOnly && dpiAdjustmentFactor < 1)
+        return baseDpi;
+
+    return QDpi(baseDpi.first * dpiAdjustmentFactor, baseDpi.second * dpiAdjustmentFactor);
+}
+
 void QHighDpiScaling::initHighDpiScaling()
 {
     // Determine if there is a global scale factor set.
Boudewijn Rempt's avatar
Boudewijn Rempt committed
226
@@ -258,8 +458,6 @@ void QHighDpiScaling::initHighDpiScaling()
227 228 229 230 231 232 233 234
 
     m_pixelDensityScalingActive = false; //set in updateHighDpiScaling below
 
-    // we update m_active in updateHighDpiScaling, but while we create the
-    // screens, we have to assume that m_usePixelDensity implies scaling
     m_active = m_globalScalingActive || m_usePixelDensity;
 }
 
Boudewijn Rempt's avatar
Boudewijn Rempt committed
235
@@ -380,22 +578,8 @@ qreal QHighDpiScaling::screenSubfactor(const QPlatformScreen *screen)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
 {
     qreal factor = qreal(1.0);
     if (screen) {
-        if (m_usePixelDensity) {
-            qreal pixelDensity = screen->pixelDensity();
-
-            // Pixel density reported by the screen is sometimes not precise enough,
-            // so recalculate it: divide px (physical pixels) by dp (device-independent pixels)
-            // for both width and height, and then use the average if it is different from
-            // the one initially reported by the screen
-            QRect screenGeometry = screen->geometry();
-            qreal wFactor = qreal(screenGeometry.width()) / qRound(screenGeometry.width() / pixelDensity);
-            qreal hFactor = qreal(screenGeometry.height()) / qRound(screenGeometry.height() / pixelDensity);
-            qreal averageDensity = (wFactor + hFactor) / 2;
-            if (!qFuzzyCompare(pixelDensity, averageDensity))
-                pixelDensity = averageDensity;
-
-            factor *= pixelDensity;
-        }
+        if (m_usePixelDensity)
+            factor *= roundScaleFactor(rawScaleFactor(screen));
         if (m_screenFactorSet) {
             QVariant screenFactor = screen->screen()->property(scaleFactorProperty);
             if (screenFactor.isValid())
Boudewijn Rempt's avatar
Boudewijn Rempt committed
260
diff --git a/src/gui/kernel/qhighdpiscaling_p.h b/src/gui/kernel/qhighdpiscaling_p.h
Boudewijn Rempt's avatar
Boudewijn Rempt committed
261
index 83fc9452c..c664693a0 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
262 263
--- a/src/gui/kernel/qhighdpiscaling_p.h
+++ b/src/gui/kernel/qhighdpiscaling_p.h
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
@@ -71,7 +71,33 @@ typedef QPair<qreal, qreal> QDpi;
 
 #ifndef QT_NO_HIGHDPISCALING
 class Q_GUI_EXPORT QHighDpiScaling {
+    Q_GADGET
 public:
+    enum class HighDpiScaleFactorRoundingPolicy {
+        NotSet,
+        Round,
+        Ceil,
+        Floor,
+        RoundPreferFloor,
+        PassThrough
+    };
+    Q_ENUM(HighDpiScaleFactorRoundingPolicy)
+
+    enum class DpiAdjustmentPolicy {
+        NotSet,
+        Enabled,
+        Disabled,
+        UpOnly
+    };
+    Q_ENUM(DpiAdjustmentPolicy)
+
+    QHighDpiScaling() = delete;
+    ~QHighDpiScaling() = delete;
+    QHighDpiScaling(const QHighDpiScaling &) = delete;
+    QHighDpiScaling &operator=(const QHighDpiScaling &) = delete;
+    QHighDpiScaling(QHighDpiScaling &&) = delete;
+    QHighDpiScaling &operator=(QHighDpiScaling &&) = delete;
+
     static void initHighDpiScaling();
     static void updateHighDpiScaling();
     static void setGlobalFactor(qreal factor);
@@ -88,6 +114,9 @@ public:
Boudewijn Rempt's avatar
Boudewijn Rempt committed
299
     static QDpi logicalDpi();
300 301 302 303 304 305 306 307
 
 private:
+    static qreal rawScaleFactor(const QPlatformScreen *screen);
+    static qreal roundScaleFactor(qreal rawFactor);
+    static QDpi effectiveLogicalDpi(const QPlatformScreen *screen, qreal rawFactor, qreal roundedFactor);
     static qreal screenSubfactor(const QPlatformScreen *screen);
 
     static qreal m_factor;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
308
diff --git a/src/gui/kernel/qplatformscreen.cpp b/src/gui/kernel/qplatformscreen.cpp
Boudewijn Rempt's avatar
Boudewijn Rempt committed
309
index 21ae75ba8..ff76528a0 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
310 311
--- a/src/gui/kernel/qplatformscreen.cpp
+++ b/src/gui/kernel/qplatformscreen.cpp
Boudewijn Rempt's avatar
Boudewijn Rempt committed
312
@@ -197,6 +197,20 @@ QDpi QPlatformScreen::logicalDpi() const
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
                 25.4 * s.height() / ps.height());
 }
 
+/*!
+    Reimplement to return the base logical DPI for the platform. This
+    DPI value should correspond to a standard-DPI (1x) display. The
+    default implementation returns 96.
+
+    QtGui will use this value (together with logicalDpi) to compute
+    the scale factor when high-DPI scaling is enabled:
+        factor = logicalDPI / baseDPI
+*/
+QDpi QPlatformScreen::logicalBaseDpi() const
+{
+    return QDpi(96, 96);
+}
+
 /*!
     Reimplement this function in subclass to return the device pixel ratio
     for the screen. This is the ratio between physical pixels and the
Boudewijn Rempt's avatar
Boudewijn Rempt committed
333
diff --git a/src/gui/kernel/qplatformscreen.h b/src/gui/kernel/qplatformscreen.h
Boudewijn Rempt's avatar
Boudewijn Rempt committed
334
index e9d64c8a2..63b5d5a4a 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
335 336
--- a/src/gui/kernel/qplatformscreen.h
+++ b/src/gui/kernel/qplatformscreen.h
337 338 339 340 341 342 343 344
@@ -113,6 +113,7 @@ public:
 
     virtual QSizeF physicalSize() const;
     virtual QDpi logicalDpi() const;
+    virtual QDpi logicalBaseDpi() const;
     virtual qreal devicePixelRatio() const;
     virtual qreal pixelDensity()  const;
 
Boudewijn Rempt's avatar
Boudewijn Rempt committed
345
diff --git a/src/plugins/platforms/android/qandroidplatformscreen.cpp b/src/plugins/platforms/android/qandroidplatformscreen.cpp
Boudewijn Rempt's avatar
Boudewijn Rempt committed
346
index 7dc8bb808..80757c213 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
347 348
--- a/src/plugins/platforms/android/qandroidplatformscreen.cpp
+++ b/src/plugins/platforms/android/qandroidplatformscreen.cpp
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
@@ -401,15 +401,17 @@ void QAndroidPlatformScreen::doRedraw()
         m_dirtyRect = QRect();
 }
 
+static const int androidLogicalDpi = 72;
+
 QDpi QAndroidPlatformScreen::logicalDpi() const
 {
-    qreal lDpi = QtAndroid::scaledDensity() * 72;
+    qreal lDpi = QtAndroid::scaledDensity() * androidLogicalDpi;
     return QDpi(lDpi, lDpi);
 }
 
-qreal QAndroidPlatformScreen::pixelDensity() const
+QDpi QAndroidPlatformScreen::logicalBaseDpi() const
 {
-    return QtAndroid::pixelDensity();
+    return QDpi(androidLogicalDpi, androidLogicalDpi);
 }
 
 Qt::ScreenOrientation QAndroidPlatformScreen::orientation() const
Boudewijn Rempt's avatar
Boudewijn Rempt committed
370
diff --git a/src/plugins/platforms/android/qandroidplatformscreen.h b/src/plugins/platforms/android/qandroidplatformscreen.h
Boudewijn Rempt's avatar
Boudewijn Rempt committed
371
index f15aeae3f..5dc158e35 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
372 373
--- a/src/plugins/platforms/android/qandroidplatformscreen.h
+++ b/src/plugins/platforms/android/qandroidplatformscreen.h
374 375 376 377 378 379 380 381 382
@@ -103,7 +103,7 @@ protected:
 
 private:
     QDpi logicalDpi() const override;
-    qreal pixelDensity()  const override;
+    QDpi logicalBaseDpi() const override;
     Qt::ScreenOrientation orientation() const override;
     Qt::ScreenOrientation nativeOrientation() const override;
     void surfaceChanged(JNIEnv *env, jobject surface, int w, int h) override;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
383
diff --git a/src/plugins/platforms/cocoa/qcocoascreen.h b/src/plugins/platforms/cocoa/qcocoascreen.h
Boudewijn Rempt's avatar
Boudewijn Rempt committed
384
index 9ded98df3..a73b97c77 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
385 386
--- a/src/plugins/platforms/cocoa/qcocoascreen.h
+++ b/src/plugins/platforms/cocoa/qcocoascreen.h
387 388 389 390 391 392 393 394
@@ -64,6 +64,7 @@ public:
     qreal devicePixelRatio() const override;
     QSizeF physicalSize() const override { return m_physicalSize; }
     QDpi logicalDpi() const override { return m_logicalDpi; }
+    QDpi logicalBaseDpi() const override { return m_logicalDpi; }
     qreal refreshRate() const override { return m_refreshRate; }
     QString name() const override { return m_name; }
     QPlatformCursor *cursor() const override { return m_cursor; }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
395
diff --git a/src/plugins/platforms/windows/qwindowsscreen.cpp b/src/plugins/platforms/windows/qwindowsscreen.cpp
Boudewijn Rempt's avatar
Boudewijn Rempt committed
396
index 0520f8893..9b7b4630f 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
397 398
--- a/src/plugins/platforms/windows/qwindowsscreen.cpp
+++ b/src/plugins/platforms/windows/qwindowsscreen.cpp
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
@@ -254,15 +254,6 @@ QWindow *QWindowsScreen::windowAt(const QPoint &screenPoint, unsigned flags)
     return result;
 }
 
-qreal QWindowsScreen::pixelDensity() const
-{
-    // QTBUG-49195: Use logical DPI instead of physical DPI to calculate
-    // the pixel density since it is reflects the Windows UI scaling.
-    // High DPI auto scaling should be disabled when the user chooses
-    // small fonts on a High DPI monitor, resulting in lower logical DPI.
-    return qMax(1, qRound(logicalDpi().first / 96));
-}
-
 /*!
     \brief Determine siblings in a virtual desktop system.
 
Boudewijn Rempt's avatar
Boudewijn Rempt committed
415
diff --git a/src/plugins/platforms/windows/qwindowsscreen.h b/src/plugins/platforms/windows/qwindowsscreen.h
Boudewijn Rempt's avatar
Boudewijn Rempt committed
416
index 824bcb1ad..cd765f181 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
417 418
--- a/src/plugins/platforms/windows/qwindowsscreen.h
+++ b/src/plugins/platforms/windows/qwindowsscreen.h
419 420 421 422 423 424 425 426 427
@@ -87,7 +87,7 @@ public:
     QImage::Format format() const override { return m_data.format; }
     QSizeF physicalSize() const override { return m_data.physicalSizeMM; }
     QDpi logicalDpi() const override { return m_data.dpi; }
-    qreal pixelDensity() const override;
+    QDpi logicalBaseDpi() const override { return QDpi(96, 96); };
     qreal devicePixelRatio() const override { return 1.0; }
     qreal refreshRate() const override { return m_data.refreshRateHz; }
     QString name() const override { return m_data.name; }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
428
diff --git a/src/plugins/platforms/xcb/qxcbscreen.cpp b/src/plugins/platforms/xcb/qxcbscreen.cpp
Boudewijn Rempt's avatar
Boudewijn Rempt committed
429
index 57dbdc9be..9af0794d2 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
430 431
--- a/src/plugins/platforms/xcb/qxcbscreen.cpp
+++ b/src/plugins/platforms/xcb/qxcbscreen.cpp
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
@@ -671,11 +671,6 @@ QDpi QXcbScreen::logicalDpi() const
     return m_virtualDesktop->dpi();
 }
 
-qreal QXcbScreen::pixelDensity() const
-{
-    return m_pixelDensity;
-}
-
 QPlatformCursor *QXcbScreen::cursor() const
 {
     return m_cursor;
@@ -739,12 +734,6 @@ void QXcbScreen::updateGeometry(const QRect &geometry, uint8_t rotation)
     if (m_sizeMillimeters.isEmpty())
         m_sizeMillimeters = sizeInMillimeters(geometry.size(), m_virtualDesktop->dpi());
 
-    qreal dpi = geometry.width() / physicalSize().width() * qreal(25.4);
-
-    // Use 128 as a reference DPI on small screens. This favors "small UI" over "large UI".
-    qreal referenceDpi = physicalSize().width() <= 320 ? 128 : 96;
-
-    m_pixelDensity = qMax(1, qRound(dpi/referenceDpi));
     m_geometry = geometry;
     m_availableGeometry = geometry & m_virtualDesktop->workArea();
     QWindowSystemInterface::handleScreenGeometryChange(QPlatformScreen::screen(), m_geometry, m_availableGeometry);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
457
diff --git a/src/plugins/platforms/xcb/qxcbscreen.h b/src/plugins/platforms/xcb/qxcbscreen.h
Boudewijn Rempt's avatar
Boudewijn Rempt committed
458
index be6c45e41..3f619d71c 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
459 460
--- a/src/plugins/platforms/xcb/qxcbscreen.h
+++ b/src/plugins/platforms/xcb/qxcbscreen.h
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
@@ -161,7 +161,7 @@ public:
     QImage::Format format() const override;
     QSizeF physicalSize() const override { return m_sizeMillimeters; }
     QDpi logicalDpi() const override;
-    qreal pixelDensity() const override;
+    QDpi logicalBaseDpi() const override { return QDpi(96, 96); };
     QPlatformCursor *cursor() const override;
     qreal refreshRate() const override { return m_refreshRate; }
     Qt::ScreenOrientation orientation() const override { return m_orientation; }
@@ -226,7 +226,6 @@ private:
     Qt::ScreenOrientation m_orientation = Qt::PrimaryOrientation;
     QXcbCursor *m_cursor;
     int m_refreshRate = 60;
-    int m_pixelDensity = 1;
     QEdidParser m_edid;
 };
 
Boudewijn Rempt's avatar
Boudewijn Rempt committed
478
diff --git a/tests/manual/highdpi/highdpi.pro b/tests/manual/highdpi/highdpi.pro
Boudewijn Rempt's avatar
Boudewijn Rempt committed
479
index 9db083cd8..2de8ed3bb 100644
Boudewijn Rempt's avatar
Boudewijn Rempt committed
480 481
--- a/tests/manual/highdpi/highdpi.pro
+++ b/tests/manual/highdpi/highdpi.pro
482 483 484 485 486
@@ -15,3 +15,4 @@ HEADERS += \
 RESOURCES += \
     highdpi.qrc
 
+DEFINES += HAVE_SCREEN_BASE_DPI