Debian Codegraph @

src/plugins/platforms/eglfs/api/qeglfsintegration.cpp

raw
1 // Copyright (C) 2016 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 <QtCore/qtextstream.h>
5 #include <QtGui/private/qguiapplication_p.h>
6
7 #include <qpa/qplatformwindow.h>
8 #include <QtGui/QSurfaceFormat>
9 #include <QtGui/QScreen>
10 #ifndef QT_NO_OPENGL
11 # include <QtGui/QOpenGLContext>
12 # include <QtGui/QOffscreenSurface>
13 #endif
14 #include <QtGui/QWindow>
15 #include <QtCore/QLoggingCategory>
16 #include <qpa/qwindowsysteminterface.h>
17 #include <qpa/qplatforminputcontextfactory_p.h>
18
19 #include "qeglfsintegration_p.h"
20 #include "qeglfswindow_p.h"
21 #include "qeglfshooks_p.h"
22 #ifndef QT_NO_OPENGL
23 # include "qeglfscontext_p.h"
24 # include "qeglfscursor_p.h"
25 #endif
26 #include "qeglfsoffscreenwindow_p.h"
27
28 #include <QtGui/private/qeglconvenience_p.h>
29 #ifndef QT_NO_OPENGL
30 # include <QtGui/private/qeglplatformcontext_p.h>
31 # include <QtGui/private/qeglpbuffer_p.h>
32 #endif
33
34 #include <QtGui/private/qgenericunixfontdatabase_p.h>
35 #include <QtGui/private/qgenericunixtheme_p.h>
36 #include <QtGui/private/qgenericunixeventdispatcher_p.h>
37 #include <QtFbSupport/private/qfbvthandler_p.h>
38 #ifndef QT_NO_OPENGL
39 # include <QtOpenGL/private/qopenglcompositorbackingstore_p.h>
40 #endif
41 #include <qpa/qplatformservices.h>
42
43 #if QT_CONFIG(libinput)
44 #include <QtInputSupport/private/qlibinputhandler_p.h>
45 #endif
46
47 #if QT_CONFIG(evdev)
48 #include <QtInputSupport/private/qevdevmousemanager_p.h>
49 #include <QtInputSupport/private/qevdevkeyboardmanager_p.h>
50 #include <QtInputSupport/private/qevdevtouchmanager_p.h>
51 #elif QT_CONFIG(vxworksevdev)
52 #include <QtInputSupport/private/qvxkeyboardmanager_p.h>
53 #include <QtInputSupport/private/qvxmousemanager_p.h>
54 #include <QtInputSupport/private/qvxtouchmanager_p.h>
55 #endif
56
57 #if QT_CONFIG(tslib)
58 #include <QtInputSupport/private/qtslib_p.h>
59 #endif
60
61 #if QT_CONFIG(integrityhid)
62 #include <QtInputSupport/qintegrityhidmanager.h>
63 #endif
64
65 static void initResources()
66 {
67 #ifndef QT_NO_CURSOR
68 Q_INIT_RESOURCE(cursor);
69 #endif
70 }
71
72 QT_BEGIN_NAMESPACE
73
74 using namespace Qt::StringLiterals;
75
76 QEglFSIntegration::QEglFSIntegration()
77 : m_display(EGL_NO_DISPLAY),
78 m_inputContext(nullptr),
79 m_fontDb(new QGenericUnixFontDatabase),
80 m_disableInputHandlers(false)
81 {
82 m_disableInputHandlers = qEnvironmentVariableIntValue("QT_QPA_EGLFS_DISABLE_INPUT");
83
84 initResources();
85 }
86
87 void QEglFSIntegration::initialize()
88 {
89 qt_egl_device_integration()->platformInit();
90
91 m_display = qt_egl_device_integration()->createDisplay(nativeDisplay());
92 if (Q_UNLIKELY(m_display == EGL_NO_DISPLAY))
93 qFatal("Could not open egl display");
94
95 EGLint major, minor;
96 if (Q_UNLIKELY(!eglInitialize(m_display, &major, &minor)))
97 qFatal("Could not initialize egl display");
98
99 m_inputContext = QPlatformInputContextFactory::create();
100
101 m_vtHandler.reset(new QFbVtHandler);
102
103 if (qt_egl_device_integration()->usesDefaultScreen())
104 QWindowSystemInterface::handleScreenAdded(new QEglFSScreen(display()));
105 else
106 qt_egl_device_integration()->screenInit();
107
108 // Input code may rely on the screens, so do it only after the screen init.
109 if (!m_disableInputHandlers)
110 createInputHandlers();
111 }
112
113 void QEglFSIntegration::destroy()
114 {
115 const auto toplevels = qGuiApp->topLevelWindows();
116 for (QWindow *w : toplevels)
117 w->destroy();
118
119 qt_egl_device_integration()->screenDestroy();
120
121 if (m_display != EGL_NO_DISPLAY)
122 eglTerminate(m_display);
123
124 qt_egl_device_integration()->platformDestroy();
125 }
126
127 QAbstractEventDispatcher *QEglFSIntegration::createEventDispatcher() const
128 {
129 return createUnixEventDispatcher();
130 }
131
132 QPlatformServices *QEglFSIntegration::services() const
133 {
134 if (m_services.isNull())
135 m_services.reset(new QPlatformServices);
136
137 return m_services.data();
138 }
139
140 QPlatformFontDatabase *QEglFSIntegration::fontDatabase() const
141 {
142 return m_fontDb.data();
143 }
144
145 QPlatformTheme *QEglFSIntegration::createPlatformTheme(const QString &name) const
146 {
147 return QGenericUnixTheme::createUnixTheme(name);
148 }
149
150 QPlatformBackingStore *QEglFSIntegration::createPlatformBackingStore(QWindow *window) const
151 {
152 #ifndef QT_NO_OPENGL
153 QOpenGLCompositorBackingStore *bs = new QOpenGLCompositorBackingStore(window);
154 if (!window->handle())
155 window->create();
156 static_cast<QEglFSWindow *>(window->handle())->setBackingStore(bs);
157 return bs;
158 #else
159 Q_UNUSED(window);
160 return nullptr;
161 #endif
162 }
163
164 QPlatformWindow *QEglFSIntegration::createPlatformWindow(QWindow *window) const
165 {
166 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
167 QEglFSWindow *w = qt_egl_device_integration()->createWindow(window);
168 w->create();
169
170 const auto showWithoutActivating = window->property("_q_showWithoutActivating");
171 if (showWithoutActivating.isValid() && showWithoutActivating.toBool())
172 return w;
173
174 // Activate only the window for the primary screen to make input work
175 if (window->type() != Qt::ToolTip && window->screen() == QGuiApplication::primaryScreen())
176 w->requestActivateWindow();
177
178 return w;
179 }
180
181 #ifndef QT_NO_OPENGL
182 QPlatformOpenGLContext *QEglFSIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
183 {
184 EGLDisplay dpy = context->screen() ? static_cast<QEglFSScreen *>(context->screen()->handle())->display() : display();
185 QPlatformOpenGLContext *share = context->shareHandle();
186
187 QEglFSContext *ctx;
188 QSurfaceFormat adjustedFormat = qt_egl_device_integration()->surfaceFormatFor(context->format());
189 EGLConfig config = QEglFSDeviceIntegration::chooseConfig(dpy, adjustedFormat);
190 ctx = new QEglFSContext(adjustedFormat, share, dpy, &config);
191
192 return ctx;
193 }
194
195 QOpenGLContext *QEglFSIntegration::createOpenGLContext(EGLContext context, EGLDisplay contextDisplay, QOpenGLContext *shareContext) const
196 {
197 return QEGLPlatformContext::createFrom<QEglFSContext>(context, contextDisplay, display(), shareContext);
198 }
199
200 QPlatformOffscreenSurface *QEglFSIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
201 {
202 EGLDisplay dpy = surface->screen() ? static_cast<QEglFSScreen *>(surface->screen()->handle())->display() : display();
203 QSurfaceFormat fmt = qt_egl_device_integration()->surfaceFormatFor(surface->requestedFormat());
204 if (qt_egl_device_integration()->supportsPBuffers()) {
205 QEGLPlatformContext::Flags flags;
206 if (!qt_egl_device_integration()->supportsSurfacelessContexts())
207 flags |= QEGLPlatformContext::NoSurfaceless;
208 return new QEGLPbuffer(dpy, fmt, surface, flags);
209 } else {
210 return new QEglFSOffscreenWindow(dpy, fmt, surface);
211 }
212 // Never return null. Multiple QWindows are not supported by this plugin.
213 }
214 #endif // QT_NO_OPENGL
215
216 bool QEglFSIntegration::hasCapability(QPlatformIntegration::Capability cap) const
217 {
218 // We assume that devices will have more and not less capabilities
219 if (qt_egl_device_integration()->hasCapability(cap))
220 return true;
221
222 switch (cap) {
223 case ThreadedPixmaps: return true;
224 #ifndef QT_NO_OPENGL
225 case OpenGL: return true;
226 case ThreadedOpenGL: return true;
227 case RasterGLSurface: return true;
228 #else
229 case OpenGL: return false;
230 case ThreadedOpenGL: return false;
231 case RasterGLSurface: return false;
232 #endif
233 case WindowManagement: return false;
234 case OpenGLOnRasterSurface: return true;
235 case OffscreenSurface: return true;
236 default: return QPlatformIntegration::hasCapability(cap);
237 }
238 }
239
240 QPlatformNativeInterface *QEglFSIntegration::nativeInterface() const
241 {
242 return const_cast<QEglFSIntegration *>(this);
243 }
244
245 enum ResourceType {
246 EglDisplay,
247 EglWindow,
248 EglContext,
249 EglConfig,
250 NativeDisplay,
251 XlibDisplay,
252 WaylandDisplay,
253 EglSurface,
254 VkSurface
255 };
256
257 static int resourceType(const QByteArray &key)
258 {
259 static const QByteArray names[] = { // match ResourceType
260 QByteArrayLiteral("egldisplay"),
261 QByteArrayLiteral("eglwindow"),
262 QByteArrayLiteral("eglcontext"),
263 QByteArrayLiteral("eglconfig"),
264 QByteArrayLiteral("nativedisplay"),
265 QByteArrayLiteral("display"),
266 QByteArrayLiteral("server_wl_display"),
267 QByteArrayLiteral("eglsurface"),
268 QByteArrayLiteral("vksurface")
269 };
270 const QByteArray *end = names + sizeof(names) / sizeof(names[0]);
271 const QByteArray *result = std::find(names, end, key);
272 if (result == end)
273 result = std::find(names, end, key.toLower());
274 return int(result - names);
275 }
276
277 void *QEglFSIntegration::nativeResourceForIntegration(const QByteArray &resource)
278 {
279 void *result = nullptr;
280
281 switch (resourceType(resource)) {
282 case EglDisplay:
283 result = display();
284 break;
285 case NativeDisplay:
286 result = reinterpret_cast<void*>(nativeDisplay());
287 break;
288 case WaylandDisplay:
289 result = qt_egl_device_integration()->wlDisplay();
290 break;
291 default:
292 result = qt_egl_device_integration()->nativeResourceForIntegration(resource);
293 break;
294 }
295
296 return result;
297 }
298
299 void *QEglFSIntegration::nativeResourceForScreen(const QByteArray &resource, QScreen *screen)
300 {
301 void *result = nullptr;
302
303 switch (resourceType(resource)) {
304 case XlibDisplay:
305 // Play nice when using the x11 hooks: Be compatible with xcb that allows querying
306 // the X Display pointer, which is nothing but our native display.
307 result = reinterpret_cast<void*>(nativeDisplay());
308 break;
309 default:
310 result = qt_egl_device_integration()->nativeResourceForScreen(resource, screen);
311 break;
312 }
313
314 return result;
315 }
316
317 void *QEglFSIntegration::nativeResourceForWindow(const QByteArray &resource, QWindow *window)
318 {
319 void *result = nullptr;
320
321 switch (resourceType(resource)) {
322 case EglDisplay:
323 if (window && window->handle())
324 result = static_cast<QEglFSScreen *>(window->handle()->screen())->display();
325 else
326 result = display();
327 break;
328 case EglWindow:
329 if (window && window->handle())
330 result = reinterpret_cast<void*>(static_cast<QEglFSWindow *>(window->handle())->eglWindow());
331 break;
332 case EglSurface:
333 if (window && window->handle())
334 result = reinterpret_cast<void*>(static_cast<QEglFSWindow *>(window->handle())->surface());
335 break;
336 default:
337 break;
338 }
339
340 return result;
341 }
342
343 #ifndef QT_NO_OPENGL
344 void *QEglFSIntegration::nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context)
345 {
346 void *result = nullptr;
347
348 switch (resourceType(resource)) {
349 case EglContext:
350 if (context->handle())
351 result = static_cast<QEglFSContext *>(context->handle())->eglContext();
352 break;
353 case EglConfig:
354 if (context->handle())
355 result = static_cast<QEglFSContext *>(context->handle())->eglConfig();
356 break;
357 case EglDisplay:
358 if (context->handle())
359 result = static_cast<QEglFSContext *>(context->handle())->eglDisplay();
360 break;
361 default:
362 break;
363 }
364
365 return result;
366 }
367
368 static void *eglContextForContext(QOpenGLContext *context)
369 {
370 Q_ASSERT(context);
371
372 QEglFSContext *handle = static_cast<QEglFSContext *>(context->handle());
373 if (!handle)
374 return nullptr;
375
376 return handle->eglContext();
377 }
378 #endif
379
380 QPlatformNativeInterface::NativeResourceForContextFunction QEglFSIntegration::nativeResourceFunctionForContext(const QByteArray &resource)
381 {
382 #ifndef QT_NO_OPENGL
383 if (resource.compare("get_egl_context", Qt::CaseInsensitive) == 0)
384 return NativeResourceForContextFunction(eglContextForContext);
385 #else
386 Q_UNUSED(resource);
387 #endif
388 return nullptr;
389 }
390
391 QFunctionPointer QEglFSIntegration::platformFunction(const QByteArray &function) const
392 {
393 return qt_egl_device_integration()->platformFunction(function);
394 }
395
396 QVariant QEglFSIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
397 {
398 if (hint == QPlatformIntegration::ShowIsFullScreen)
399 return true;
400
401 return QPlatformIntegration::styleHint(hint);
402 }
403
404 #if QT_CONFIG(evdev) || QT_CONFIG(vxworksevdev)
405 void QEglFSIntegration::loadKeymap(const QString &filename)
406 {
407 if (m_kbdMgr)
408 m_kbdMgr->loadKeymap(filename);
409 else
410 qWarning("QEglFSIntegration: Cannot load keymap, no keyboard handler found");
411 }
412
413 void QEglFSIntegration::switchLang()
414 {
415 if (m_kbdMgr)
416 m_kbdMgr->switchLang();
417 else
418 qWarning("QEglFSIntegration: Cannot switch language, no keyboard handler found");
419 }
420 #endif
421
422 void QEglFSIntegration::createInputHandlers()
423 {
424 #if QT_CONFIG(libinput)
425 if (!qEnvironmentVariableIntValue("QT_QPA_EGLFS_NO_LIBINPUT")) {
426 new QLibInputHandler("libinput"_L1, QString());
427 return;
428 }
429 #endif
430
431 #if QT_CONFIG(tslib)
432 bool useTslib = qEnvironmentVariableIntValue("QT_QPA_EGLFS_TSLIB");
433 if (useTslib)
434 new QTsLibMouseHandler("TsLib"_L1, QString() /* spec */);
435 #endif
436
437 #if QT_CONFIG(evdev)
438 m_kbdMgr = new QEvdevKeyboardManager("EvdevKeyboard"_L1, QString() /* spec */, this);
439 new QEvdevMouseManager("EvdevMouse"_L1, QString() /* spec */, this);
440 #if QT_CONFIG(tslib)
441 if (!useTslib)
442 #endif
443 new QEvdevTouchManager("EvdevTouch"_L1, QString() /* spec */, this);
444 #elif QT_CONFIG(vxworksevdev)
445 m_kbdMgr = new QVxKeyboardManager("VxKeyboard"_L1, QString() /* spec */, this);
446 new QVxMouseManager("VxMouse"_L1, QString() /* spec */, this);
447 new QVxTouchManager("VxTouch"_L1, QString() /* spec */, this);
448 #endif
449
450 #if QT_CONFIG(integrityhid)
451 new QIntegrityHIDManager("HID", "", this);
452 #endif
453 }
454
455 EGLNativeDisplayType QEglFSIntegration::nativeDisplay() const
456 {
457 return qt_egl_device_integration()->platformDisplay();
458 }
459
460 QT_END_NAMESPACE