| 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 |  | 
| 6 | #include "qeglfskmsgbmdevice_p.h" | 
| 7 | #include "qeglfskmsgbmscreen_p.h" | 
| 8 |  | 
| 9 | #include <private/qeglfsintegration_p.h> | 
| 10 |  | 
| 11 | #include <QtCore/QLoggingCategory> | 
| 12 | #include <QtCore/private/qcore_unix_p.h> | 
| 13 |  | 
| 14 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) | 
| 15 |  | 
| 16 | QT_BEGIN_NAMESPACE | 
| 17 |  | 
| 18 | Q_DECLARE_LOGGING_CATEGORY(qLcEglfsKmsDebug) | 
| 19 |  | 
| 20 | QEglFSKmsGbmDevice::QEglFSKmsGbmDevice(QKmsScreenConfig *screenConfig, const QString &path) | 
| 21 |     : QEglFSKmsDevice(screenConfig, path) | 
| 22 |     , m_gbm_device(nullptr) | 
| 23 |     , m_globalCursor(nullptr) | 
| 24 | { | 
| 25 | } | 
| 26 |  | 
| 27 | bool QEglFSKmsGbmDevice::open() | 
| 28 | { | 
| 29 |     Q_ASSERT(fd() == -1); | 
| 30 |     Q_ASSERT(m_gbm_device == nullptr); | 
| 31 |  | 
| 32 |     int fd = qt_safe_open(pathname: devicePath().toLocal8Bit().constData(), O_RDWR | O_CLOEXEC); | 
| 33 |     if (fd == -1) { | 
| 34 |         qErrnoWarning(msg: "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(msg: "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(device: 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 |  | 
| 61 | void 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(gbm: m_gbm_device); | 
| 70 |         m_gbm_device = nullptr; | 
| 71 |     } | 
| 72 |  | 
| 73 |     if (fd() != -1) { | 
| 74 |         qt_safe_close(fd: fd()); | 
| 75 |         setFd(-1); | 
| 76 |     } | 
| 77 | } | 
| 78 |  | 
| 79 | void *QEglFSKmsGbmDevice::nativeDisplay() const | 
| 80 | { | 
| 81 |     return m_gbm_device; | 
| 82 | } | 
| 83 |  | 
| 84 | gbm_device * QEglFSKmsGbmDevice::gbmDevice() const | 
| 85 | { | 
| 86 |     return m_gbm_device; | 
| 87 | } | 
| 88 |  | 
| 89 | QPlatformCursor *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. | 
| 96 | void 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 |  | 
| 105 | void 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 |  | 
| 113 | QPlatformScreen *QEglFSKmsGbmDevice::createScreen(const QKmsOutput &output) | 
| 114 | { | 
| 115 |     QEglFSKmsGbmScreen *screen = new QEglFSKmsGbmScreen(this, output, false); | 
| 116 |  | 
| 117 |     createGlobalCursor(screen); | 
| 118 |  | 
| 119 |     return screen; | 
| 120 | } | 
| 121 |  | 
| 122 | QPlatformScreen *QEglFSKmsGbmDevice::createHeadlessScreen() | 
| 123 | { | 
| 124 |     return new QEglFSKmsGbmScreen(this, QKmsOutput(), true); | 
| 125 | } | 
| 126 |  | 
| 127 | void QEglFSKmsGbmDevice::registerScreenCloning(QPlatformScreen *screen, | 
| 128 |                                                QPlatformScreen *screenThisScreenClones, | 
| 129 |                                                const QList<QPlatformScreen *> &screensCloningThisScreen) | 
| 130 | { | 
| 131 |     if (!screenThisScreenClones && screensCloningThisScreen.isEmpty()) | 
| 132 |         return; | 
| 133 |  | 
| 134 |     QEglFSKmsGbmScreen *gbmScreen = static_cast<QEglFSKmsGbmScreen *>(screen); | 
| 135 |     gbmScreen->initCloning(screenThisScreenClones, screensCloningThisScreen); | 
| 136 | } | 
| 137 |  | 
| 138 | void QEglFSKmsGbmDevice::registerScreen(QPlatformScreen *screen, | 
| 139 |                                         bool isPrimary, | 
| 140 |                                         const QPoint &virtualPos, | 
| 141 |                                         const QList<QPlatformScreen *> &virtualSiblings) | 
| 142 | { | 
| 143 |     QEglFSKmsDevice::registerScreen(screen, isPrimary, virtualPos, virtualSiblings); | 
| 144 |     if (screenConfig()->hwCursor() && m_globalCursor) | 
| 145 |         m_globalCursor->reevaluateVisibilityForScreens(); | 
| 146 | } | 
| 147 |  | 
| 148 | bool QEglFSKmsGbmDevice::usesEventReader() const | 
| 149 | { | 
| 150 |     static const bool eventReaderThreadDisabled = qEnvironmentVariableIntValue(varName: "QT_QPA_EGLFS_KMS_NO_EVENT_READER_THREAD" ); | 
| 151 |     return !eventReaderThreadDisabled; | 
| 152 | } | 
| 153 |  | 
| 154 | QT_END_NAMESPACE | 
| 155 |  |