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
qeglfskmsgbmdevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
2// Copyright (C) 2016 The Qt Company Ltd.
3// Copyright (C) 2016 Pelagicore AG
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
8
9#include <private/qeglfsintegration_p.h>
10#include <private/qeglfskmsintegration_p.h>
11
12#include <QtCore/QLoggingCategory>
13#include <QtCore/qtimer.h>
14#include <QtCore/private/qcore_unix_p.h>
15
16#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
17
19
20QEglFSKmsGbmDevice::QEglFSKmsGbmDevice(QKmsScreenConfig *screenConfig, const QString &path)
21 : QEglFSKmsDevice(screenConfig, path)
22 , m_gbm_device(nullptr)
23 , m_globalCursor(nullptr)
24{
25}
26
27bool QEglFSKmsGbmDevice::open()
28{
29 Q_ASSERT(fd() == -1);
30 Q_ASSERT(m_gbm_device == nullptr);
31
32 int fd = qt_safe_open(devicePath().toLocal8Bit().constData(), O_RDWR | O_CLOEXEC);
33 if (fd == -1) {
34 qErrnoWarning("Could not open DRM device %s", qPrintable(devicePath()));
35 return false;
36 }
37
38 qCDebug(qLcEglfsKmsDebug) << "Creating GBM device for file descriptor" << fd
39 << "obtained from" << devicePath();
40 m_gbm_device = gbm_create_device(fd);
41 if (!m_gbm_device) {
42 qErrnoWarning("Could not create GBM device");
43 qt_safe_close(fd);
44 fd = -1;
45 return false;
46 }
47
48 setFd(fd);
49
50 if (usesEventReader()) {
51 qCDebug(qLcEglfsKmsDebug, "Using dedicated drm event reading thread");
52 m_eventReader.create(this);
53 } else {
54 qCDebug(qLcEglfsKmsDebug, "Not using dedicated drm event reading thread; "
55 "threaded multi-screen setups may experience problems");
56 }
57
58 return true;
59}
60
61void QEglFSKmsGbmDevice::close()
62{
63 // Note: screens are gone at this stage.
64
65 if (usesEventReader())
66 m_eventReader.destroy();
67
68 if (m_gbm_device) {
69 gbm_device_destroy(m_gbm_device);
70 m_gbm_device = nullptr;
71 }
72
73 if (fd() != -1) {
74 qt_safe_close(fd());
75 setFd(-1);
76 }
77}
78
79void *QEglFSKmsGbmDevice::nativeDisplay() const
80{
81 return m_gbm_device;
82}
83
84gbm_device * QEglFSKmsGbmDevice::gbmDevice() const
85{
86 return m_gbm_device;
87}
88
89QPlatformCursor *QEglFSKmsGbmDevice::globalCursor() const
90{
91 return m_globalCursor;
92}
93
94// Cannot do this from close(), it may be too late.
95// Call this from the last screen dtor instead.
96void QEglFSKmsGbmDevice::destroyGlobalCursor()
97{
98 if (m_globalCursor) {
99 qCDebug(qLcEglfsKmsDebug, "Destroying global GBM mouse cursor");
100 delete m_globalCursor;
101 m_globalCursor = nullptr;
102 }
103}
104
105void QEglFSKmsGbmDevice::createGlobalCursor(QEglFSKmsGbmScreen *screen)
106{
107 if (!m_globalCursor && screenConfig()->hwCursor()) {
108 qCDebug(qLcEglfsKmsDebug, "Creating new global GBM mouse cursor");
109 m_globalCursor = new QEglFSKmsGbmCursor(screen);
110 }
111}
112
113QPlatformScreen *QEglFSKmsGbmDevice::createScreen(const QKmsOutput &output)
114{
115 QEglFSKmsGbmScreen *screen = new QEglFSKmsGbmScreen(this, output, false);
116
117
118 // On some platforms (e.g. rpi4), you'll get a kernel warning/error
119 // if the cursor is created 'at the same time' as the screen is created.
120 // (drmModeMoveCursor is the specific call that causes the issue)
121 // When this issue is triggered, the screen's connector is unusable until reboot
122 //
123 // Below is a work-around (without negative implications for other platforms).
124 //
125 // interval of 0 and QMetaObject::invokeMethod (w/o Qt::QueuedConnection)
126 // do no help / will still trigger issue
127 QTimer::singleShot(1, [screen, this](){
128 createGlobalCursor(screen);
129 });
130
131 return screen;
132}
133
134QPlatformScreen *QEglFSKmsGbmDevice::createHeadlessScreen()
135{
136 destroyGlobalCursor();
137
138 return new QEglFSKmsGbmScreen(this, QKmsOutput(), true);
139}
140
141void QEglFSKmsGbmDevice::registerScreenCloning(QPlatformScreen *screen,
142 QPlatformScreen *screenThisScreenClones,
143 const QList<QPlatformScreen *> &screensCloningThisScreen)
144{
145 QEglFSKmsGbmScreen *gbmScreen = static_cast<QEglFSKmsGbmScreen *>(screen);
146 gbmScreen->initCloning(screenThisScreenClones, screensCloningThisScreen);
147}
148
149void QEglFSKmsGbmDevice::registerScreen(QPlatformScreen *screen,
150 bool isPrimary,
151 const QPoint &virtualPos,
152 const QList<QPlatformScreen *> &virtualSiblings)
153{
154 QEglFSKmsDevice::registerScreen(screen, isPrimary, virtualPos, virtualSiblings);
155 if (screenConfig()->hwCursor() && m_globalCursor)
156 m_globalCursor->reevaluateVisibilityForScreens();
157}
158
159void QEglFSKmsGbmDevice::unregisterScreen(QPlatformScreen *screen)
160{
161 // The global cursor holds a pointer to a QEglFSKmsGbmScreen.
162 // If that screen is being unregistered,
163 // this will recreate the global cursor with the first sibling screen.
164 if (m_globalCursor && screen == m_globalCursor->screen()) {
165 qCDebug(qLcEglfsKmsDebug) << "Destroying global GBM mouse cursor due to unregistering"
166 << "it's screen - will probably be recreated right away";
167 delete m_globalCursor;
168 m_globalCursor = nullptr;
169
170 QList<QPlatformScreen *> siblings = screen->virtualSiblings();
171 siblings.removeOne(screen);
172 if (siblings.count() > 0) {
173 QEglFSKmsGbmScreen *kmsScreen = static_cast<QEglFSKmsGbmScreen *>(siblings.first());
174 m_globalCursor = new QEglFSKmsGbmCursor(kmsScreen);
175 qCDebug(qLcEglfsKmsDebug) << "Creating new global GBM mouse cursor on sibling screen";
176 } else {
177 qCWarning(qLcEglfsKmsDebug) << "Couldn't find a sibling to recreate"
178 << "the GBM mouse cursor - it might vanish";
179 }
180 }
181
182 QEglFSKmsDevice::unregisterScreen(screen);
183}
184
185bool QEglFSKmsGbmDevice::usesEventReader() const
186{
187 static const bool eventReaderThreadDisabled = qEnvironmentVariableIntValue("QT_QPA_EGLFS_KMS_NO_EVENT_READER_THREAD");
188 return !eventReaderThreadDisabled;
189}
190
191QT_END_NAMESPACE