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
qeglfsintegration.cpp
Go to the documentation of this file.
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
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
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
65static void initResources()
66{
67#ifndef QT_NO_CURSOR
68 Q_INIT_RESOURCE(cursor);
69#endif
70}
71
72QT_BEGIN_NAMESPACE
73
74using namespace Qt::StringLiterals;
75
76QEglFSIntegration::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
87void 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
113void 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
127QAbstractEventDispatcher *QEglFSIntegration::createEventDispatcher() const
128{
129 return createUnixEventDispatcher();
130}
131
132QPlatformServices *QEglFSIntegration::services() const
133{
134 if (m_services.isNull())
135 m_services.reset(new QPlatformServices);
136
137 return m_services.data();
138}
139
140QPlatformFontDatabase *QEglFSIntegration::fontDatabase() const
141{
142 return m_fontDb.data();
143}
144
145QPlatformTheme *QEglFSIntegration::createPlatformTheme(const QString &name) const
146{
147 return QGenericUnixTheme::createUnixTheme(name);
148}
149
150QPlatformBackingStore *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 m_bs = bs;
158 return bs;
159#else
160 Q_UNUSED(window);
161 return nullptr;
162#endif
163}
164
165QPlatformWindow *QEglFSIntegration::createPlatformWindow(QWindow *window) const
166{
167 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
168 QEglFSWindow *w = qt_egl_device_integration()->createWindow(window);
169 w->create();
170
171 const auto showWithoutActivating = window->property("_q_showWithoutActivating");
172 if (showWithoutActivating.isValid() && showWithoutActivating.toBool())
173 return w;
174
175 // Activate only the window for the primary screen to make input work
176 if (window->type() != Qt::ToolTip && window->screen() == QGuiApplication::primaryScreen())
177 w->requestActivateWindow();
178
179 if (window->isTopLevel())
180 w->setBackingStore(static_cast<QOpenGLCompositorBackingStore *>(m_bs));
181
182 return w;
183}
184
185#ifndef QT_NO_OPENGL
186QPlatformOpenGLContext *QEglFSIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
187{
188 EGLDisplay dpy = context->screen() ? static_cast<QEglFSScreen *>(context->screen()->handle())->display() : display();
189 QPlatformOpenGLContext *share = context->shareHandle();
190
191 QEglFSContext *ctx;
192 QSurfaceFormat adjustedFormat = qt_egl_device_integration()->surfaceFormatFor(context->format());
193 EGLConfig config = QEglFSDeviceIntegration::chooseConfig(dpy, adjustedFormat);
194 ctx = new QEglFSContext(adjustedFormat, share, dpy, &config);
195
196 return ctx;
197}
198
199QOpenGLContext *QEglFSIntegration::createOpenGLContext(EGLContext context, EGLDisplay contextDisplay, QOpenGLContext *shareContext) const
200{
201 return QEGLPlatformContext::createFrom<QEglFSContext>(context, contextDisplay, display(), shareContext);
202}
203
204QPlatformOffscreenSurface *QEglFSIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
205{
206 EGLDisplay dpy = surface->screen() ? static_cast<QEglFSScreen *>(surface->screen()->handle())->display() : display();
207 QSurfaceFormat fmt = qt_egl_device_integration()->surfaceFormatFor(surface->requestedFormat());
208 if (qt_egl_device_integration()->supportsPBuffers()) {
209 QEGLPlatformContext::Flags flags;
210 if (!qt_egl_device_integration()->supportsSurfacelessContexts())
211 flags |= QEGLPlatformContext::NoSurfaceless;
212 return new QEGLPbuffer(dpy, fmt, surface, flags);
213 } else {
214 return new QEglFSOffscreenWindow(dpy, fmt, surface);
215 }
216 // Never return null. Multiple QWindows are not supported by this plugin.
217}
218#endif // QT_NO_OPENGL
219
220bool QEglFSIntegration::hasCapability(QPlatformIntegration::Capability cap) const
221{
222 // We assume that devices will have more and not less capabilities
223 if (qt_egl_device_integration()->hasCapability(cap))
224 return true;
225
226 switch (cap) {
227 case ThreadedPixmaps: return true;
228#ifndef QT_NO_OPENGL
229 case OpenGL: return true;
230 case ThreadedOpenGL: return true;
231#else
232 case OpenGL: return false;
233 case ThreadedOpenGL: return false;
234#endif
235 case WindowManagement: return false;
236 case OpenGLOnRasterSurface: return true;
237 default: return QPlatformIntegration::hasCapability(cap);
238 }
239}
240
241QPlatformNativeInterface *QEglFSIntegration::nativeInterface() const
242{
243 return const_cast<QEglFSIntegration *>(this);
244}
245
257
258static int resourceType(const QByteArray &key)
259{
260 static const QByteArray names[] = { // match ResourceType
261 QByteArrayLiteral("egldisplay"),
262 QByteArrayLiteral("eglwindow"),
263 QByteArrayLiteral("eglcontext"),
264 QByteArrayLiteral("eglconfig"),
265 QByteArrayLiteral("nativedisplay"),
266 QByteArrayLiteral("display"),
267 QByteArrayLiteral("server_wl_display"),
268 QByteArrayLiteral("eglsurface"),
269 QByteArrayLiteral("vksurface")
270 };
271 const QByteArray *end = names + sizeof(names) / sizeof(names[0]);
272 const QByteArray *result = std::find(names, end, key);
273 if (result == end)
274 result = std::find(names, end, key.toLower());
275 return int(result - names);
276}
277
278void *QEglFSIntegration::nativeResourceForIntegration(const QByteArray &resource)
279{
280 void *result = nullptr;
281
282 switch (resourceType(resource)) {
283 case EglDisplay:
284 result = display();
285 break;
286 case NativeDisplay:
287 result = reinterpret_cast<void*>(nativeDisplay());
288 break;
289 case WaylandDisplay:
290 result = qt_egl_device_integration()->wlDisplay();
291 break;
292 default:
293 result = qt_egl_device_integration()->nativeResourceForIntegration(resource);
294 break;
295 }
296
297 return result;
298}
299
300void *QEglFSIntegration::nativeResourceForScreen(const QByteArray &resource, QScreen *screen)
301{
302 void *result = nullptr;
303
304 switch (resourceType(resource)) {
305 case XlibDisplay:
306 // Play nice when using the x11 hooks: Be compatible with xcb that allows querying
307 // the X Display pointer, which is nothing but our native display.
308 result = reinterpret_cast<void*>(nativeDisplay());
309 break;
310 default:
311 result = qt_egl_device_integration()->nativeResourceForScreen(resource, screen);
312 break;
313 }
314
315 return result;
316}
317
318void *QEglFSIntegration::nativeResourceForWindow(const QByteArray &resource, QWindow *window)
319{
320 void *result = nullptr;
321
322 switch (resourceType(resource)) {
323 case EglDisplay:
324 if (window && window->handle())
325 result = static_cast<QEglFSScreen *>(window->handle()->screen())->display();
326 else
327 result = display();
328 break;
329 case EglWindow:
330 if (window && window->handle())
331 result = reinterpret_cast<void*>(static_cast<QEglFSWindow *>(window->handle())->eglWindow());
332 break;
333 case EglSurface:
334 if (window && window->handle())
335 result = reinterpret_cast<void*>(static_cast<QEglFSWindow *>(window->handle())->surface());
336 break;
337 default:
338 break;
339 }
340
341 return result;
342}
343
344#ifndef QT_NO_OPENGL
345void *QEglFSIntegration::nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context)
346{
347 void *result = nullptr;
348
349 switch (resourceType(resource)) {
350 case EglContext:
351 if (context->handle())
352 result = static_cast<QEglFSContext *>(context->handle())->eglContext();
353 break;
354 case EglConfig:
355 if (context->handle())
356 result = static_cast<QEglFSContext *>(context->handle())->eglConfig();
357 break;
358 case EglDisplay:
359 if (context->handle())
360 result = static_cast<QEglFSContext *>(context->handle())->eglDisplay();
361 break;
362 default:
363 break;
364 }
365
366 return result;
367}
368
369static void *eglContextForContext(QOpenGLContext *context)
370{
371 Q_ASSERT(context);
372
373 QEglFSContext *handle = static_cast<QEglFSContext *>(context->handle());
374 if (!handle)
375 return nullptr;
376
377 return handle->eglContext();
378}
379#endif
380
381QPlatformNativeInterface::NativeResourceForContextFunction QEglFSIntegration::nativeResourceFunctionForContext(const QByteArray &resource)
382{
383#ifndef QT_NO_OPENGL
384 if (resource.compare("get_egl_context", Qt::CaseInsensitive) == 0)
385 return NativeResourceForContextFunction(eglContextForContext);
386#else
387 Q_UNUSED(resource);
388#endif
389 return nullptr;
390}
391
392QFunctionPointer QEglFSIntegration::platformFunction(const QByteArray &function) const
393{
394 return qt_egl_device_integration()->platformFunction(function);
395}
396
397QVariant QEglFSIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
398{
399 if (hint == QPlatformIntegration::ShowIsFullScreen)
400 return true;
401
402 return QPlatformIntegration::styleHint(hint);
403}
404
405#if QT_CONFIG(evdev) || QT_CONFIG(vxworksevdev)
406void QEglFSIntegration::loadKeymap(const QString &filename)
407{
408 if (m_kbdMgr)
409 m_kbdMgr->loadKeymap(filename);
410 else
411 qWarning("QEglFSIntegration: Cannot load keymap, no keyboard handler found");
412}
413
414void QEglFSIntegration::switchLang()
415{
416 if (m_kbdMgr)
417 m_kbdMgr->switchLang();
418 else
419 qWarning("QEglFSIntegration: Cannot switch language, no keyboard handler found");
420}
421#endif
422
423void QEglFSIntegration::createInputHandlers()
424{
425#if QT_CONFIG(libinput)
426 if (!qEnvironmentVariableIntValue("QT_QPA_EGLFS_NO_LIBINPUT")) {
427 new QLibInputHandler("libinput"_L1, QString());
428 return;
429 }
430#endif
431
432#if QT_CONFIG(tslib)
433 bool useTslib = qEnvironmentVariableIntValue("QT_QPA_EGLFS_TSLIB");
434 if (useTslib)
435 new QTsLibMouseHandler("TsLib"_L1, QString() /* spec */);
436#endif
437
438#if QT_CONFIG(evdev)
439 m_kbdMgr = new QEvdevKeyboardManager("EvdevKeyboard"_L1, QString() /* spec */, this);
440 new QEvdevMouseManager("EvdevMouse"_L1, QString() /* spec */, this);
441#if QT_CONFIG(tslib)
442 if (!useTslib)
443#endif
444 new QEvdevTouchManager("EvdevTouch"_L1, QString() /* spec */, this);
445#elif QT_CONFIG(vxworksevdev)
446 m_kbdMgr = new QVxKeyboardManager("VxKeyboard"_L1, QString() /* spec */, this);
447 new QVxMouseManager("VxMouse"_L1, QString() /* spec */, this);
448 new QVxTouchManager("VxTouch"_L1, QString() /* spec */, this);
449#endif
450
451#if QT_CONFIG(integrityhid)
452 new QIntegrityHIDManager("HID", "", this);
453#endif
454}
455
456EGLNativeDisplayType QEglFSIntegration::nativeDisplay() const
457{
458 return qt_egl_device_integration()->platformDisplay();
459}
460
461QT_END_NAMESPACE
static void initResources()
@ WaylandDisplay
@ NativeDisplay
@ XlibDisplay
static int resourceType(const QByteArray &key)
static void * eglContextForContext(QOpenGLContext *context)