Debian Codegraph @

wayland/dwayland/dhighdpi.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2017 - 2022 Uniontech Software Technology Co.,Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dhighdpi.h"
6 #include "vtablehook.h"
7 #include "dxsettings.h"
8 #include "dwaylandinterfacehook.h"
9
10 #include <QtWaylandClient/private/qwaylandscreen_p.h>
11
12 #include <private/qhighdpiscaling_p.h>
13 #include <private/qguiapplication_p.h>
14 #include <QGuiApplication>
15 #include <QPainter>
16 #include <QDebug>
17
18 #include <QLoggingCategory>
19
20 #ifndef QT_DEBUG
21 Q_LOGGING_CATEGORY(dwhdpi, "dtk.wayland.plugin.hdpi" , QtInfoMsg);
22 #else
23 Q_LOGGING_CATEGORY(dwhdpi, "dtk.wayland.plugin.hdpi");
24 #endif
25
26 DPP_BEGIN_NAMESPACE
27
28 bool DHighDpi::active = false;
29 QDpi DHighDpi::oldDpi = QDpi(0.0, 0.0);
30 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
31 QHash<QPlatformScreen*, qreal> DHighDpi::screenFactorMap;
32 #endif
33 QPointF DHighDpi::fromNativePixels(const QPointF &pixelPoint, const QWindow *window)
34 {
35 return QHighDpi::fromNativePixels(pixelPoint, window);
36 }
37
38 __attribute__((constructor)) // 在库被加载时就执行此函数
39 static void init()
40 {
41 DHighDpi::init();
42 }
43
44 void DHighDpi::init()
45 {
46 if (QGuiApplication::testAttribute(Qt::AA_DisableHighDpiScaling)
47 // 可以禁用此行为
48 || qEnvironmentVariableIsSet("D_DXCB_DISABLE_OVERRIDE_HIDPI")
49 // 无有效的xsettings时禁用
50 || !dXSettings->getOwner()
51 || (qEnvironmentVariableIsSet("QT_SCALE_FACTOR_ROUNDING_POLICY")
52 && qgetenv("QT_SCALE_FACTOR_ROUNDING_POLICY") != "PassThrough")) {
53 return;
54 }
55
56 // 禁用platform theme中的缩放机制
57 qputenv("D_DISABLE_RT_SCREEN_SCALE", "1");
58 // 设置为完全控制缩放比例,避免被Qt“4舍5入”了缩放比
59 qputenv("QT_SCALE_FACTOR_ROUNDING_POLICY", "PassThrough");
60 // 强制开启使用DXCB的缩放机制,此时移除会影响此功能的所有Qt环境变量
61 qputenv("D_DXCB_FORCE_OVERRIDE_HIDPI", "1");
62
63 if (qEnvironmentVariableIsSet("D_DXCB_FORCE_OVERRIDE_HIDPI")) {
64 qunsetenv("QT_AUTO_SCREEN_SCALE_FACTOR");
65 qunsetenv("QT_SCALE_FACTOR");
66 qunsetenv("QT_SCREEN_SCALE_FACTORS");
67 qunsetenv("QT_ENABLE_HIGHDPI_SCALING");
68 qunsetenv("QT_USE_PHYSICAL_DPI");
69 }
70
71 if (!QGuiApplication::testAttribute(Qt::AA_EnableHighDpiScaling)) {
72 QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
73 QHighDpiScaling::initHighDpiScaling();
74 }
75
76 //qDebug()<<QHighDpiScaling::isActive();
77 QObject::connect(qApp, &QGuiApplication::screenRemoved, &DHighDpi::removeScreenFactorCache);
78
79 active = HookOverride(&QtWaylandClient::QWaylandScreen::logicalDpi, logicalDpi);
80 }
81
82 bool DHighDpi::isActive()
83 {
84 return active;
85 }
86
87 bool DHighDpi::overrideBackingStore()
88 {
89 // 默认不开启,会降低绘图效率
90 static bool enabled = qEnvironmentVariableIsSet("D_DXCB_HIDPI_BACKINGSTORE");
91 return enabled;
92 }
93
94 QDpi DHighDpi::logicalDpi(QtWaylandClient::QWaylandScreen *s)
95 {
96 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
97 if (screenFactorMap.contains(s)) {
98 return {screenFactorMap[s], screenFactorMap[s]};
99 }
100 #endif
101
102 static bool dpi_env_set = qEnvironmentVariableIsSet("QT_FONT_DPI");
103 // 遵循环境变量的设置
104 if (dpi_env_set) {
105 return s->QtWaylandClient::QWaylandScreen::logicalDpi();
106 }
107
108 int dpi = 0;
109
110 QVariant value = dXSettings->globalSettings()->setting("Qt/DPI/" + s->name().toLocal8Bit());
111
112 bool ok = false;
113
114 dpi = value.toInt(&ok);
115
116 // fallback
117 if (!ok) {
118 value = dXSettings->globalSettings()->setting("Xft/DPI");
119 dpi = value.toInt(&ok);
120 }
121
122 // fallback
123 if (!ok || dpi == 0) {
124 qWarning() << "dpi is invalid got from xsettings(Qt/DPI/ and Xft/DPI), "
125 "fallback to get dpi from QWaylandScreen::logicalDpi()";
126 return s->QtWaylandClient::QWaylandScreen::logicalDpi();
127 }
128
129 qreal d = dpi / 1024.0;
130
131 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
132 if (!screenFactorMap.contains(s)) {
133 qDebug(dwhdpi()) << "add screen to cache" << s->model() << s->devicePixelRatio();
134 screenFactorMap[s] = d;
135 }
136 #endif
137
138 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
139 // QWaylandScreen 没有实现 pixelDensity,为了不影响兼容性,不能采用 hook pixelDensity 的方式
140 QHighDpiScaling::setScreenFactor(s->screen(), d / 96.0);
141 #endif
142
143 return QDpi(d, d);
144 }
145
146 qreal DHighDpi::devicePixelRatio(QPlatformWindow *w)
147 {
148 qreal base_factor = QHighDpiScaling::factor(w->screen());
149 return qCeil(base_factor) / base_factor;
150 }
151
152 void DHighDpi::removeScreenFactorCache(QScreen *screen)
153 {
154 // 清理过期的屏幕缩放值
155 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
156 if (screen) {
157 qCDebug(dwhdpi()) << "remove screen from cache" << screen->model() << screen->devicePixelRatio();
158 screenFactorMap.remove(screen->handle());
159 } else {
160 screenFactorMap.clear();
161 #else
162 {
163 #endif
164 // 刷新所有窗口
165 for (QWindow *window : qGuiApp->allWindows()) {
166 if (window->type() == Qt::Desktop)
167 continue;
168
169 // qDebug()<<window->devicePixelRatio();
170 // 更新窗口大小
171 if (window->handle()) {
172 QWindowSystemInterfacePrivate::GeometryChangeEvent gce(window, QHighDpi::fromNativePixels(window->handle()->geometry(), window));
173 QGuiApplicationPrivate::processGeometryChangeEvent(&gce);
174 }
175 }
176 }
177 }
178
179 DPP_END_NAMESPACE