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
qvkkhrdisplayintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
7
8#include <qpa/qplatformwindow.h>
9#include <qpa/qplatformbackingstore.h>
10#include <qpa/qplatforminputcontextfactory_p.h>
11#include <qpa/qwindowsysteminterface.h>
12
13#include <QtGui/private/qguiapplication_p.h>
14#include <QtGui/private/qwindow_p.h>
15#include <QtGui/private/qgenericunixeventdispatcher_p.h>
16#include <QtGui/private/qgenericunixfontdatabase_p.h>
17#include <QtGui/private/qgenericunixtheme_p.h>
18#include <qpa/qplatformservices.h>
19
20#include <QtFbSupport/private/qfbvthandler_p.h>
21
22#if QT_CONFIG(libinput)
23#include <QtInputSupport/private/qlibinputhandler_p.h>
24#endif
25
26#if QT_CONFIG(evdev)
27#include <QtInputSupport/private/qevdevmousemanager_p.h>
28#include <QtInputSupport/private/qevdevkeyboardmanager_p.h>
29#include <QtInputSupport/private/qevdevtouchmanager_p.h>
30#endif
31
32#if QT_CONFIG(tslib)
33#include <QtInputSupport/private/qtslib_p.h>
34#endif
35
37
38using namespace Qt::StringLiterals;
39
41{
42public:
43 QRect geometry() const override { return m_geometry; }
44 int depth() const override { return m_depth; }
45 QImage::Format format() const override { return m_format; }
46 void setVk(QVkKhrDisplayVulkanInstance *inst);
47
48private:
49 QVkKhrDisplayVulkanInstance *m_vk = nullptr;
50 QRect m_geometry;
51 int m_depth = 32;
52 QImage::Format m_format = QImage::Format_ARGB32_Premultiplied;
54};
55
56void QVkKhrDisplayScreen::setVk(QVkKhrDisplayVulkanInstance *inst)
57{
58 m_vk = inst;
59 m_geometry = QRect(QPoint(0, 0), m_vk->displaySize());
60 QWindowSystemInterface::handleScreenGeometryChange(screen(), m_geometry, m_geometry);
61 qDebug() << "Screen will report geometry" << m_geometry;
62
63 // Thanks to this deferred screen setup, a QWindow with a size based on the
64 // dummy screen size may already exist. Try to resize it.
65 QScreen *thisScreen = screen();
66 for (QWindow *window : QGuiApplication::allWindows()) {
67 if (window->isTopLevel() && window->screen() == thisScreen)
68 window->handle()->setGeometry(QRect()); // set fullscreen geometry
69 }
70}
71
73{
74public:
77
79
80 void setGeometry(const QRect &r) override;
81
82private:
83 VkSurfaceKHR m_surface = VK_NULL_HANDLE;
84};
85
87{
88 if (m_surface) {
89 QVulkanInstance *inst = window()->vulkanInstance();
90 if (inst)
91 static_cast<QVkKhrDisplayVulkanInstance *>(inst->handle())->destroySurface(m_surface);
92 }
93}
94
96{
97 if (m_surface)
98 return &m_surface;
99
100 QVulkanInstance *inst = window()->vulkanInstance();
101 if (!inst) {
102 qWarning("Attempted to create Vulkan surface without an instance; was QWindow::setVulkanInstance() called?");
103 return nullptr;
104 }
105 QVkKhrDisplayVulkanInstance *vkdinst = static_cast<QVkKhrDisplayVulkanInstance *>(inst->handle());
106 m_surface = vkdinst->createSurface(window());
107
108 return &m_surface;
109}
110
112{
113 // We only support full-screen windows
114 QRect rect(screen()->availableGeometry());
115 QWindowSystemInterface::handleGeometryChange(window(), rect);
116 QPlatformWindow::setGeometry(rect);
117
118 const QRect lastReportedGeometry = qt_window_private(window())->geometry;
119 if (rect != lastReportedGeometry)
120 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(0, 0), rect.size()));
121}
122
123// does not actually support raster content, just paint into a QImage and that's it for now
125{
126public:
128
129 QPaintDevice *paintDevice() override { return &m_image; }
130 void flush(QWindow *window, const QRegion &region, const QPoint &offset) override {
131 Q_UNUSED(window);
132 Q_UNUSED(region);
133 Q_UNUSED(offset);
134 }
135 void resize(const QSize &size, const QRegion &staticContents) override {
136 Q_UNUSED(staticContents);
137 QImage::Format format = QGuiApplication::primaryScreen()->handle()->format();
138 if (m_image.size() != size)
139 m_image = QImage(size, format);
140 }
141
142private:
143 QImage m_image;
144};
145
147{
148 Q_UNUSED(parameters);
149}
150
152{
153 QWindowSystemInterface::handleScreenRemoved(m_primaryScreen);
154 delete m_services;
155 delete m_fontDatabase;
156 delete m_vtHandler;
157}
158
159bool QVkKhrDisplayIntegration::hasCapability(QPlatformIntegration::Capability cap) const
160{
161 switch (cap) {
162 case ThreadedPixmaps: return true;
163 case WindowManagement: return false;
164 default: return QPlatformIntegration::hasCapability(cap);
165 }
166}
167
169{
170 m_primaryScreen = new QVkKhrDisplayScreen;
171
172 // The real values are only known when the QVulkanInstance initializes, use
173 // dummy values until then.
174 m_primaryScreen->m_geometry = QRect(0, 0, 1920, 1080);
175 m_primaryScreen->m_depth = 32;
176 m_primaryScreen->m_format = QImage::Format_ARGB32_Premultiplied;
177
178 QWindowSystemInterface::handleScreenAdded(m_primaryScreen);
179
180 m_inputContext = QPlatformInputContextFactory::create();
181
182 m_vtHandler = new QFbVtHandler;
183
184 if (!qEnvironmentVariableIntValue("QT_QPA_DISABLE_INPUT"))
185 createInputHandlers();
186}
187
189{
190 if (!m_fontDatabase)
191 m_fontDatabase = new QGenericUnixFontDatabase;
192
193 return m_fontDatabase;
194}
195
197{
198 if (!m_services)
199 m_services = new QPlatformServices;
200
201 return m_services;
202}
203
205{
206 return m_inputContext;
207}
208
210{
211 return QGenericUnixTheme::createUnixTheme(name);
212}
213
218
220{
221 if (window->surfaceType() != QSurface::VulkanSurface) {
222 qWarning("vkkhrdisplay platform plugin only supports QWindow with surfaceType == VulkanSurface");
223 // Assume VulkanSurface, better than crashing. Consider e.g. an autotest
224 // creating a default QWindow just to have something to be used with
225 // QRhi's Null backend. Continuing to set up a Vulkan window (even
226 // though the request was Raster or something) is better than failing to
227 // create a platform window, and may even be sufficient in some cases.
228 }
229
231 w->setGeometry(QRect()); // set fullscreen geometry
232 w->requestActivateWindow();
233 return w;
234}
235
240
242{
243 return createUnixEventDispatcher();
244}
245
246void QVkKhrDisplayIntegration::handleInstanceCreated(QVkKhrDisplayVulkanInstance *inst, void *userData)
247{
248 QVkKhrDisplayIntegration *self = static_cast<QVkKhrDisplayIntegration *>(userData);
249 self->m_primaryScreen->setVk(inst);
250}
251
253{
254 QVkKhrDisplayVulkanInstance *inst = new QVkKhrDisplayVulkanInstance(instance);
255 inst->setCreatedCallback(handleInstanceCreated, const_cast<QVkKhrDisplayIntegration *>(this));
256 return inst;
257}
258
262
263static int resourceType(const QByteArray &key)
264{
265 static const QByteArray names[] = { // match ResourceType
266 QByteArrayLiteral("vksurface")
267 };
268 const QByteArray *end = names + sizeof(names) / sizeof(names[0]);
269 const QByteArray *result = std::find(names, end, key);
270 if (result == end)
271 result = std::find(names, end, key.toLower());
272 return int(result - names);
273}
274
275void *QVkKhrDisplayIntegration::nativeResourceForWindow(const QByteArray &resource, QWindow *window)
276{
277 void *result = nullptr;
278
279 switch (resourceType(resource)) {
280 case VkSurface:
281 if (window && window->handle() && window->surfaceType() == QSurface::VulkanSurface)
282 result = static_cast<QVkKhrDisplayWindow *>(window->handle())->vulkanSurfacePtr();
283 break;
284 default:
285 break;
286 }
287
288 return result;
289}
290
291void QVkKhrDisplayIntegration::createInputHandlers()
292{
293#if QT_CONFIG(libinput)
294 if (!qEnvironmentVariableIntValue("QT_QPA_NO_LIBINPUT")) {
295 new QLibInputHandler("libinput"_L1, QString());
296 return;
297 }
298#endif
299
300#if QT_CONFIG(tslib)
301 bool useTslib = qEnvironmentVariableIntValue("QT_QPA_TSLIB");
302 if (useTslib)
303 new QTsLibMouseHandler("TsLib"_L1, QString());
304#endif
305
306#if QT_CONFIG(evdev)
307 new QEvdevKeyboardManager("EvdevKeyboard"_L1, QString(), this);
308 new QEvdevMouseManager("EvdevMouse"_L1, QString(), this);
309#if QT_CONFIG(tslib)
310 if (!useTslib)
311#endif
312 new QEvdevTouchManager("EvdevTouch"_L1, QString() /* spec */, this);
313#endif
314}
315
316QT_END_NAMESPACE
void flush(QWindow *window, const QRegion &region, const QPoint &offset) override
Flushes the given region from the specified window.
QPaintDevice * paintDevice() override
Implement this function to return the appropriate paint device.
void resize(const QSize &size, const QRegion &staticContents) override
bool hasCapability(QPlatformIntegration::Capability cap) const override
QPlatformBackingStore * createPlatformBackingStore(QWindow *window) const override
Factory function for QPlatformBackingStore.
QPlatformTheme * createPlatformTheme(const QString &name) const override
QVkKhrDisplayIntegration(const QStringList &parameters)
QPlatformVulkanInstance * createPlatformVulkanInstance(QVulkanInstance *instance) const override
QPlatformServices * services() const override
QPlatformInputContext * inputContext() const override
Returns the platforms input context.
QPlatformNativeInterface * nativeInterface() const override
QPlatformFontDatabase * fontDatabase() const override
Accessor for the platform integration's fontdatabase.
QAbstractEventDispatcher * createEventDispatcher() const override
Factory function for the GUI event dispatcher.
void initialize() override
Performs initialization steps that depend on having an event dispatcher available.
void * nativeResourceForWindow(const QByteArray &resource, QWindow *window) override
QPlatformWindow * createPlatformWindow(QWindow *window) const override
Factory function for QPlatformWindow.
QRect geometry() const override
Reimplement in subclass to return the pixel geometry of the screen.
QImage::Format format() const override
Reimplement in subclass to return the image format which corresponds to the screen format.
void setVk(QVkKhrDisplayVulkanInstance *inst)
int depth() const override
Reimplement in subclass to return current depth of the screen.
void setGeometry(const QRect &r) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
Combined button and popup list for selecting options.
static int resourceType(const QByteArray &key)