Blob Blame History Raw
commit ff39a278ddccf61716a7dbcb575415801bbf8ded
Author: Tom Anderson <thomasanderson@chromium.org>
Date:   Wed May 31 18:31:09 2023 +0000

    [QT] Account for logical scale factor
    
    Previously we were only using the device pixel ratio.  But QT UI
    scales with both the device pixel ratio and the logical DPI.  This CL
    sets Chrome's scale factor to be the product of these two.
    
    R=thestig
    CC=jkummerow
    
    Change-Id: I9bd414046058e741450fabae4913d47a16ca48c3
    Bug: 1439149
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4575572
    Reviewed-by: Lei Zhang <thestig@chromium.org>
    Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
    Cr-Commit-Position: refs/heads/main@{#1151380}

diff --git a/ui/qt/qt_shim.cc b/ui/qt/qt_shim.cc
index 0aec9c3aed4ad..3780f8c3988b4 100644
--- a/ui/qt/qt_shim.cc
+++ b/ui/qt/qt_shim.cc
@@ -7,7 +7,7 @@
 
 #include "ui/qt/qt_shim.h"
 
-#include <stdio.h>
+#include <cmath>
 
 #include <QApplication>
 #include <QFont>
@@ -219,7 +219,24 @@ QtShim::QtShim(QtInterface::Delegate* delegate, int* argc, char** argv)
 QtShim::~QtShim() = default;
 
 double QtShim::GetScaleFactor() const {
-  return app_.devicePixelRatio();
+  constexpr double kDefaultPixelDpi = 96.0;
+  // Use the largest scale factor across all displays as the global scale
+  // factor.  This matches the behavior of `app_.devicePixelRatio()`, except
+  // this also takes into account the logical DPI.
+  // TODO(https://crbug.com/1450301): Unlike GTK, QT supports per-display
+  // scaling. Use this instead of the max scale factor.
+  double scale = 0.0;
+  for (QScreen* screen : app_.screens()) {
+    scale =
+        std::max(scale, screen->devicePixelRatio() *
+                            screen->logicalDotsPerInch() / kDefaultPixelDpi);
+  }
+  // Round to the nearest 16th so that UI can losslessly multiply and divide
+  // by the scale factor using floating point arithmetic.  GtkUi also rounds
+  // in this way, but to 1/64th.  1/16th is chosen here since that's what
+  // KDE settings uses.
+  scale = std::round(scale * 16) / 16;
+  return scale > 0 ? scale : 1.0;
 }
 
 FontRenderParams QtShim::GetFontRenderParams() const {