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

source code of qtbase/src/plugins/platforms/vkkhrdisplay/qvkkhrdisplayintegration.cpp