QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GraphicsSetup.cc
Go to the documentation of this file.
1#include "GraphicsSetup.h"
2
3#include <memory>
4#include <rhi/qrhi.h>
5
6#include <QtCore/QCoreApplication>
7#include <QtCore/QDir>
8#include <QtCore/QFileInfo>
9#include <QtCore/QRunnable>
10#include <QtCore/QStandardPaths>
11#include <QtCore/QString>
12#include <QtCore/QStringList>
13#include <QtGui/QPlatformSurfaceEvent>
14#include <QtQuick/QQuickGraphicsConfiguration>
15#include <QtQuick/QQuickGraphicsDevice>
16#include <QtQuick/QQuickWindow>
17
18#include "QGCLoggingCategory.h"
19
20QGC_LOGGING_CATEGORY(GraphicsSetupLog, "API.GraphicsSetup")
21
22namespace GraphicsSetup {
23
24namespace {
25
26constexpr const char* kEnvRhiDebug = "QGC_RHI_DEBUG";
27constexpr const char* kEnvRhiPipelineCache = "QGC_RHI_PIPELINE_CACHE";
28constexpr const char* kEnvHdrOutput = "QGC_HDR_OUTPUT";
29constexpr const char* kEnvForceVideoGpu = "QGC_FORCE_VIDEO_GPU";
30
31bool envFlag(const char* name)
32{
33 return qEnvironmentVariableIsSet(name) && (qEnvironmentVariable(name) != QLatin1String("0"));
34}
35
36bool envEnabledByDefault(const char* name)
37{
38 return !qEnvironmentVariableIsSet(name) || (qEnvironmentVariable(name) != QLatin1String("0"));
39}
40
41void configurePipelineCache(QQuickGraphicsConfiguration& config)
42{
43 if (!envEnabledByDefault(kEnvRhiPipelineCache)) {
44 return;
45 }
46
47 const QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
48 if (cacheDir.isEmpty()) {
49 qCWarning(GraphicsSetupLog) << "RHI pipeline cache disabled: no writable cache location";
50 return;
51 }
52
53 QDir dir;
54 if (!dir.mkpath(cacheDir)) {
55 qCWarning(GraphicsSetupLog) << "RHI pipeline cache disabled: failed to create" << cacheDir;
56 return;
57 }
58
59 const QString cacheFile = cacheDir + QStringLiteral("/qgc_rhi_pipeline.cache");
60 // Only load when the cache already exists: Qt 6.11 warns if the load file
61 // can't be opened, which is expected on first run. Always set the save file
62 // so the cache is written at shutdown and picked up on the next launch.
63 if (QFileInfo::exists(cacheFile)) {
64 config.setPipelineCacheLoadFile(cacheFile);
65 }
66 config.setPipelineCacheSaveFile(cacheFile);
67 qCDebug(GraphicsSetupLog) << "RHI pipeline cache:" << cacheFile;
68}
69
70#if defined(Q_OS_WIN)
71bool parseHex32(const QString& spec, quint32& value)
72{
73 QString token = spec.trimmed();
74 if (token.startsWith(QLatin1String("0x"), Qt::CaseInsensitive)) {
75 token.remove(0, 2);
76 }
77
78 if (token.isEmpty()) {
79 return false;
80 }
81
82 bool ok = false;
83 value = token.toUInt(&ok, 16);
84 return ok;
85}
86
87qint32 signExtend32(quint32 value)
88{
89 const qint64 signedValue =
90 (value <= 0x7FFFFFFFU) ? static_cast<qint64>(value) : (static_cast<qint64>(value) - 0x100000000LL);
91 return static_cast<qint32>(signedValue);
92}
93
94bool parseLuid(const QString& spec, quint32& low, qint32& high)
95{
96 const QStringList parts = spec.split(QLatin1Char(':'));
97 if (parts.size() != 2) {
98 return false;
99 }
100
101 quint32 highRaw = 0;
102 if (!parseHex32(parts.at(0), low) || !parseHex32(parts.at(1), highRaw)) {
103 return false;
104 }
105
106 high = signExtend32(highRaw);
107 return true;
108}
109
110void configureForcedD3DAdapter(QQuickWindow* window)
111{
112 if (!qEnvironmentVariableIsSet(kEnvForceVideoGpu)) {
113 return;
114 }
115
116 const QString spec = qEnvironmentVariable(kEnvForceVideoGpu);
117 quint32 luidLow = 0;
118 qint32 luidHigh = 0;
119 if (!parseLuid(spec, luidLow, luidHigh)) {
120 qCWarning(GraphicsSetupLog) << "QGC_FORCE_VIDEO_GPU malformed (expected hex 'low:high'):" << spec;
121 return;
122 }
123
124 // Must run before QRhi creation; Qt Quick owns the adapter after scene graph init.
125 window->setGraphicsDevice(QQuickGraphicsDevice::fromAdapter(luidLow, luidHigh));
126 qCWarning(GraphicsSetupLog).nospace()
127 << "QGC_FORCE_VIDEO_GPU: forcing adapter LUID low=0x" << QString::number(luidLow, 16) << " high=0x"
128 << QString::number(static_cast<quint32>(luidHigh), 16);
129}
130#else
131void warnForcedGpuUnsupported()
132{
133 if (qEnvironmentVariableIsSet(kEnvForceVideoGpu)) {
134 qCWarning(GraphicsSetupLog)
135 << "QGC_FORCE_VIDEO_GPU is Windows/D3D-only; ignoring LUID" << qEnvironmentVariable(kEnvForceVideoGpu);
136 }
137}
138#endif
139
140const char* hdrFormatName(QRhiSwapChain::Format f)
141{
142 switch (f) {
143 case QRhiSwapChain::SDR:
144 return "SDR";
145 case QRhiSwapChain::HDRExtendedSrgbLinear:
146 return "HDRExtendedSrgbLinear";
147 case QRhiSwapChain::HDR10:
148 return "HDR10";
149 case QRhiSwapChain::HDRExtendedDisplayP3Linear:
150 return "HDRExtendedDisplayP3Linear";
151 }
152 return "Unknown";
153}
154
155void probeHdr(QQuickWindow* window)
156{
157 QRhi* rhi = window->rhi();
158 if (!rhi) {
159 qCWarning(GraphicsSetupLog) << "HDR probe: no QRhi on render thread";
160 return;
161 }
162
163 std::unique_ptr<QRhiSwapChain> sc(rhi->newSwapChain());
164 if (!sc) {
165 qCWarning(GraphicsSetupLog) << "HDR probe: backend has no swapchain support";
166 return;
167 }
168 sc->setWindow(window);
169
170 const QRhiSwapChainHdrInfo info = sc->hdrInfo();
171 qCDebug(GraphicsSetupLog).nospace()
172 << "HDR display info: limitsType=" << static_cast<int>(info.limitsType)
173 << " luminanceBehavior=" << static_cast<int>(info.luminanceBehavior) << " sdrWhiteLevel=" << info.sdrWhiteLevel;
174
175 for (const QRhiSwapChain::Format f :
176 {QRhiSwapChain::HDRExtendedSrgbLinear, QRhiSwapChain::HDR10, QRhiSwapChain::HDRExtendedDisplayP3Linear}) {
177 qCDebug(GraphicsSetupLog) << "HDR format" << hdrFormatName(f) << "supported:" << sc->isFormatSupported(f);
178 }
179
180 if (envFlag(kEnvHdrOutput)) {
181 qCWarning(GraphicsSetupLog)
182 << "QGC_HDR_OUTPUT set but Qt Quick exposes no public swapchain-format setter in this"
183 << "Qt version; staying on SDR. (Diagnostic only.)";
184 }
185}
186
187void checkSoftwareDevice(QQuickWindow* window)
188{
189 const QRhi* const rhi = window->rhi();
190 if (!rhi) {
191 return;
192 }
193
194 const QRhiDriverInfo info = rhi->driverInfo();
195 if (info.deviceType != QRhiDriverInfo::CpuDevice) {
196 qCWarning(GraphicsSetupLog) << "Software rendering requested but not achieved; using" << rhi->backendName()
197 << info.deviceName;
198 }
199}
200
202class FirstWindowConfigurator final : public QObject
203{
204public:
205 using QObject::QObject;
206
207protected:
208 bool eventFilter(QObject* watched, QEvent* event) override
209 {
210 if ((event->type() == QEvent::PlatformSurface) &&
211 (static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated)) {
212 if (auto* const window = qobject_cast<QQuickWindow*>(watched)) {
213 QCoreApplication::instance()->removeEventFilter(this);
214 configureMainWindow(window);
215 deleteLater();
216 }
217 }
218 return false;
219 }
220};
221
222} // namespace
223
224void configureMainWindow(QQuickWindow* window)
225{
226 if (!window) {
227 qCWarning(GraphicsSetupLog) << "configureMainWindow: null window";
228 return;
229 }
230
231 // The window's own QRhi, not isSceneGraphInitialized(): the basic render loop shares one render context, so a new
232 // window reports an initialized scene graph once any other window has rendered.
233 if (window->rhi()) {
234 qCWarning(GraphicsSetupLog)
235 << "Scene graph already initialized before configureMainWindow; skipping RHI config";
236 } else {
237 QQuickGraphicsConfiguration config = window->graphicsConfiguration();
238
239 if (envFlag(kEnvRhiDebug)) {
240 config.setDebugLayer(true);
241 config.setDebugMarkers(true);
242 config.setTimestamps(true);
243 qCDebug(GraphicsSetupLog) << "RHI debug layer/markers/timestamps enabled";
244 }
245
246 configurePipelineCache(config);
247 window->setGraphicsConfiguration(config);
248
249#if defined(Q_OS_WIN)
250 configureForcedD3DAdapter(window);
251#else
252 warnForcedGpuUnsupported();
253#endif
254
255 if (config.prefersSoftwareDevice()) {
256 QObject::connect(
257 window, &QQuickWindow::sceneGraphInitialized, window, [window]() { checkSoftwareDevice(window); },
258 Qt::DirectConnection);
259 }
260 }
261
262 if (!envFlag(kEnvRhiDebug) && !envFlag(kEnvHdrOutput)) {
263 return;
264 }
265 QQuickWindow* win = window;
266 if (win->rhi()) {
267 win->scheduleRenderJob(QRunnable::create([win]() { probeHdr(win); }), QQuickWindow::BeforeSynchronizingStage);
268 win->update();
269 } else {
270 QObject::connect(
271 win, &QQuickWindow::sceneGraphInitialized, win, [win]() { probeHdr(win); }, Qt::DirectConnection);
272 }
273}
274
276{
277 QCoreApplication* const app = QCoreApplication::instance();
278 if (!app) {
279 return;
280 }
281 app->installEventFilter(new FirstWindowConfigurator(app));
282}
283
284} // namespace GraphicsSetup
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void configureMainWindow(QQuickWindow *window)
void configureMainWindowOnCreation()