Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdxgivsyncservice_p.h
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QDXGIVSYNCSERVICE_P_H
6#define QDXGIVSYNCSERVICE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QWindow>
20#include <QMutex>
21#include <QHash>
22
23#include <dxgi1_6.h>
24
25QT_BEGIN_NAMESPACE
26
27class QDxgiVSyncThread;
28
29class Q_GUI_EXPORT QDxgiVSyncService
30{
31public:
32 // The public functions can be called on any thread. (but not at all from inside a callback)
33
34 static QDxgiVSyncService *instance();
35
36 // We want to know what adapters are in use by all the active QRhi instances
37 // at any time. This is to avoid blind EnumAdapters calls to enumerate the
38 // world every time a window is created. To be called by create()/destroy()
39 // of the QRhi D3D11/12 backends. refAdapter() adds 'count' references to
40 // the one adapter identified by 'luid'.
41 void refAdapter(LUID luid, int count = 1);
42 void derefAdapter(LUID luid);
43
44 // To be called from QRhi's beginFrame. Used to check if the factory is stale.
45 void beginFrame(LUID luid);
46
47#ifdef QT_BUILD_INTERNAL
48 // For autotests. Re-creates the factory as if it was no longer current.
49 void simulateStaleFactory();
50#endif
51
52 // Registering the windows can be done either by the QRhi swapchain in the
53 // backends, or e.g. in the platformwindow implementation based on the
54 // surfaceType.
55 void registerWindow(QWindow *window);
56 void unregisterWindow(QWindow *window);
57
58 // The catch is that in some cases (using an adapter such as WARP) there are
59 // no IDXGIOutputs at all, by design (even though there is very definitely a
60 // QScreen etc. from our perspective), so none of this VSyncService is
61 // functional. Hence this can never be the only way to drive updates for
62 // D3D-based windows.
63 //
64 // A requestUpdate implementation can call this function on every request in
65 // order to decide if it can rely on the callback from this service, or it
66 // should use the traditional timer-based approach for a particular window.
67 // For windows not registered the result is always false.
68 bool supportsWindow(QWindow *window);
69
70 // Callbacks are invoked on a vsync watcher thread, or, for windows that
71 // lost their output, on the thread that re-created the DXGI factory, which
72 // may be the main thread. Whatever is done in there, it must be fast and
73 // cheap. Ideally fire a notification for the main thread somehow, and
74 // nothing else.
75 using CallbackWindowList = QVarLengthArray<QWindow *, 16>;
76 using Callback = std::function<void(const CallbackWindowList &windowList, qint64 timestampNs)>;
77 qsizetype registerCallback(Callback cb); // result is an id, always >= 1
78 void unregisterCallback(qsizetype id);
79
80private:
81 QDxgiVSyncService();
82 ~QDxgiVSyncService();
83 void destroy();
84 void teardownDxgi();
85 void recreateDxgi(QMutexLocker<QMutex> &lock);
86 void recreateDxgiIfStale(QMutexLocker<QMutex> &lock);
87 static void global_destroy();
88
89 struct AdapterData {
90 int ref;
91 LUID luid;
92 IDXGIAdapter *adapter;
93 struct NotifierData {
94 IDXGIOutput *output;
95 QDxgiVSyncThread *thread;
96 };
97 QHash<HMONITOR, NotifierData> notifiers;
98 };
99
100 struct WindowData {
101 IDXGIOutput *output;
102 HMONITOR monitor;
103 float reportedRefreshIntervalMs;
104 };
105
106 void cleanupAdapterData(AdapterData *a);
107 void cleanupWindowData(WindowData *w);
108 void updateWindowData(QWindow *window, WindowData *w);
109
110 bool disableService = false;
111 bool cleanupRegistered = false;
112 IDXGIFactory2 *dxgiFactory = nullptr;
113 QMutex mutex;
114 QVector<AdapterData> adapters;
115 QHash<QWindow *, WindowData> windows;
116 QVector<Callback> callbacks;
117};
118
119QT_END_NAMESPACE
120
121#endif
QDxgiVSyncThread(IDXGIOutput *output, float vsyncIntervalMsReportedForScreen, Callback callback)
void run() override
Combined button and popup list for selecting options.
static IDXGIOutput * outputForWindow(QWindow *w, IDXGIAdapter *adapter)
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)