1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Pelagicore AG
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qeglfskmsegldeviceintegration.h"
6#include "qeglfskmsegldevice.h"
7#include "qeglfskmsegldevicescreen.h"
8#include <QtGui/private/qeglconvenience_p.h>
9#include "private/qeglfswindow_p.h"
10#include "private/qeglfscursor_p.h"
11#include <QLoggingCategory>
12#include <private/qmath_p.h>
13
14QT_BEGIN_NAMESPACE
15
16QEglFSKmsEglDeviceIntegration::QEglFSKmsEglDeviceIntegration()
17 : m_egl_device(EGL_NO_DEVICE_EXT)
18 , m_funcs(nullptr)
19{
20 qCDebug(qLcEglfsKmsDebug, "New DRM/KMS on EGLDevice integration created");
21}
22
23QSurfaceFormat QEglFSKmsEglDeviceIntegration::surfaceFormatFor(const QSurfaceFormat &inputFormat) const
24{
25 QSurfaceFormat format = QEglFSKmsIntegration::surfaceFormatFor(inputFormat);
26 format.setAlphaBufferSize(8);
27 return format;
28}
29
30EGLint QEglFSKmsEglDeviceIntegration::surfaceType() const
31{
32 return EGL_STREAM_BIT_KHR;
33}
34
35EGLDisplay QEglFSKmsEglDeviceIntegration::createDisplay(EGLNativeDisplayType nativeDisplay)
36{
37 qCDebug(qLcEglfsKmsDebug, "Creating display");
38
39 EGLDisplay display;
40
41 if (m_funcs->has_egl_platform_device) {
42 display = m_funcs->get_platform_display(EGL_PLATFORM_DEVICE_EXT, nativeDisplay, nullptr);
43 } else {
44 qWarning(msg: "EGL_EXT_platform_device not available, falling back to legacy path!");
45 display = eglGetDisplay(display_id: nativeDisplay);
46 }
47
48 if (Q_UNLIKELY(display == EGL_NO_DISPLAY))
49 qFatal(msg: "Could not get EGL display");
50
51 EGLint major, minor;
52 if (Q_UNLIKELY(!eglInitialize(display, &major, &minor)))
53 qFatal(msg: "Could not initialize egl display");
54
55 if (Q_UNLIKELY(!eglBindAPI(EGL_OPENGL_ES_API)))
56 qFatal(msg: "Failed to bind EGL_OPENGL_ES_API\n");
57
58 return display;
59}
60
61bool QEglFSKmsEglDeviceIntegration::supportsSurfacelessContexts() const
62{
63 // Returning false disables the usage of EGL_KHR_surfaceless_context even when the
64 // extension is available. This is just what we need since, at least with NVIDIA
65 // 352.00 making a null surface current with a context breaks.
66 return false;
67}
68
69bool QEglFSKmsEglDeviceIntegration::supportsPBuffers() const
70{
71 return true;
72}
73
74class QEglFSKmsEglDeviceWindow : public QEglFSWindow
75{
76public:
77 QEglFSKmsEglDeviceWindow(QWindow *w, const QEglFSKmsEglDeviceIntegration *integration)
78 : QEglFSWindow(w)
79 , m_integration(integration)
80 , m_egl_stream(EGL_NO_STREAM_KHR)
81 { }
82
83 ~QEglFSKmsEglDeviceWindow() { destroy(); }
84
85 void invalidateSurface() override;
86 void resetSurface() override;
87
88 const QEglFSKmsEglDeviceIntegration *m_integration;
89 EGLStreamKHR m_egl_stream;
90 EGLint m_latency;
91};
92
93void QEglFSKmsEglDeviceWindow::invalidateSurface()
94{
95 QEglFSWindow::invalidateSurface();
96 m_integration->m_funcs->destroy_stream(screen()->display(), m_egl_stream);
97}
98
99void QEglFSKmsEglDeviceWindow::resetSurface()
100{
101 qCDebug(qLcEglfsKmsDebug, "Creating stream");
102
103 EGLDisplay display = screen()->display();
104 EGLint streamAttribs[3];
105 int streamAttribCount = 0;
106 int fifoLength = qEnvironmentVariableIntValue(varName: "QT_QPA_EGLFS_STREAM_FIFO_LENGTH");
107 if (fifoLength > 0) {
108 streamAttribs[streamAttribCount++] = EGL_STREAM_FIFO_LENGTH_KHR;
109 streamAttribs[streamAttribCount++] = fifoLength;
110 }
111 streamAttribs[streamAttribCount++] = EGL_NONE;
112
113 m_egl_stream = m_integration->m_funcs->create_stream(display, streamAttribs);
114 if (m_egl_stream == EGL_NO_STREAM_KHR) {
115 qWarning(msg: "resetSurface: Couldn't create EGLStream for native window");
116 return;
117 }
118
119 qCDebug(qLcEglfsKmsDebug, "Created stream %p on display %p", m_egl_stream, display);
120
121 EGLint count;
122 if (m_integration->m_funcs->query_stream(display, m_egl_stream, EGL_STREAM_FIFO_LENGTH_KHR, &count)) {
123 if (count > 0)
124 qCDebug(qLcEglfsKmsDebug, "Using EGLStream FIFO mode with %d frames", count);
125 else
126 qCDebug(qLcEglfsKmsDebug, "Using EGLStream mailbox mode");
127 } else {
128 qCDebug(qLcEglfsKmsDebug, "Could not query number of EGLStream FIFO frames");
129 }
130
131 if (!m_integration->m_funcs->get_output_layers(display, nullptr, nullptr, 0, &count) || count == 0) {
132 qWarning(msg: "No output layers found");
133 return;
134 }
135
136 qCDebug(qLcEglfsKmsDebug, "Output has %d layers", count);
137
138 QList<EGLOutputLayerEXT> layers;
139 layers.resize(size: count);
140 EGLint actualCount;
141 if (!m_integration->m_funcs->get_output_layers(display, nullptr, layers.data(), count, &actualCount)) {
142 qWarning(msg: "Failed to get layers");
143 return;
144 }
145
146 QEglFSKmsEglDeviceScreen *cur_screen = static_cast<QEglFSKmsEglDeviceScreen *>(screen());
147 Q_ASSERT(cur_screen);
148 QKmsOutput &output(cur_screen->output());
149 const uint32_t wantedId = !output.wants_forced_plane ? output.crtc_id : output.forced_plane_id;
150 qCDebug(qLcEglfsKmsDebug, "Searching for id: %d", wantedId);
151
152 EGLOutputLayerEXT layer = EGL_NO_OUTPUT_LAYER_EXT;
153 for (int i = 0; i < actualCount; ++i) {
154 EGLAttrib id;
155 if (m_integration->m_funcs->query_output_layer_attrib(display, layers[i], EGL_DRM_CRTC_EXT, &id)) {
156 qCDebug(qLcEglfsKmsDebug, " [%d] layer %p - crtc %d", i, layers[i], (int) id);
157 if (id == EGLAttrib(wantedId))
158 layer = layers[i];
159 } else if (m_integration->m_funcs->query_output_layer_attrib(display, layers[i], EGL_DRM_PLANE_EXT, &id)) {
160 qCDebug(qLcEglfsKmsDebug, " [%d] layer %p - plane %d", i, layers[i], (int) id);
161 if (id == EGLAttrib(wantedId))
162 layer = layers[i];
163 } else {
164 qCDebug(qLcEglfsKmsDebug, " [%d] layer %p - unknown", i, layers[i]);
165 }
166 }
167
168 QByteArray reqLayerIndex = qgetenv(varName: "QT_QPA_EGLFS_LAYER_INDEX");
169 if (!reqLayerIndex.isEmpty()) {
170 int idx = reqLayerIndex.toInt();
171 if (idx >= 0 && idx < layers.size()) {
172 qCDebug(qLcEglfsKmsDebug, "EGLOutput layer index override = %d", idx);
173 layer = layers[idx];
174 }
175 }
176
177 if (layer == EGL_NO_OUTPUT_LAYER_EXT) {
178 qWarning(msg: "resetSurface: Couldn't get EGLOutputLayer for native window");
179 return;
180 }
181
182 qCDebug(qLcEglfsKmsDebug, "Using layer %p", layer);
183
184 if (!m_integration->m_funcs->stream_consumer_output(display, m_egl_stream, layer))
185 qWarning(msg: "resetSurface: Unable to connect stream");
186
187 m_config = QEglFSDeviceIntegration::chooseConfig(display, format: m_integration->surfaceFormatFor(inputFormat: window()->requestedFormat()));
188 m_format = q_glFormatFromConfig(display, config: m_config);
189 qCDebug(qLcEglfsKmsDebug) << "Stream producer format is" << m_format;
190
191 const int w = cur_screen->rawGeometry().width();
192 const int h = cur_screen->rawGeometry().height();
193 qCDebug(qLcEglfsKmsDebug, "Creating stream producer surface of size %dx%d", w, h);
194
195 const EGLint stream_producer_attribs[] = {
196 EGL_WIDTH, w,
197 EGL_HEIGHT, h,
198 EGL_NONE
199 };
200
201 m_surface = m_integration->m_funcs->create_stream_producer_surface(display, m_config, m_egl_stream, stream_producer_attribs);
202 if (m_surface == EGL_NO_SURFACE)
203 return;
204
205 qCDebug(qLcEglfsKmsDebug, "Created stream producer surface %p", m_surface);
206}
207
208QEglFSWindow *QEglFSKmsEglDeviceIntegration::createWindow(QWindow *window) const
209{
210 QEglFSKmsEglDeviceWindow *eglWindow = new QEglFSKmsEglDeviceWindow(window, this);
211
212 m_funcs->initialize(dpy: eglWindow->screen()->display());
213 if (Q_UNLIKELY(!(m_funcs->has_egl_output_base && m_funcs->has_egl_output_drm && m_funcs->has_egl_stream &&
214 m_funcs->has_egl_stream_producer_eglsurface && m_funcs->has_egl_stream_consumer_egloutput)))
215 qFatal(msg: "Required extensions missing!");
216
217 return eglWindow;
218}
219
220QKmsDevice *QEglFSKmsEglDeviceIntegration::createDevice()
221{
222 if (Q_UNLIKELY(!query_egl_device()))
223 qFatal(msg: "Could not set up EGL device!");
224
225 const char *deviceName = m_funcs->query_device_string(m_egl_device, EGL_DRM_DEVICE_FILE_EXT);
226 if (Q_UNLIKELY(!deviceName))
227 qFatal(msg: "Failed to query device name from EGLDevice");
228
229 return new QEglFSKmsEglDevice(this, screenConfig(), QLatin1StringView(deviceName));
230}
231
232bool QEglFSKmsEglDeviceIntegration::query_egl_device()
233{
234 m_funcs = new QEGLStreamConvenience;
235 if (Q_UNLIKELY(!m_funcs->has_egl_device_base))
236 qFatal(msg: "EGL_EXT_device_base missing");
237
238 EGLint num_devices = 0;
239 if (m_funcs->query_devices(1, &m_egl_device, &num_devices) != EGL_TRUE) {
240 qWarning(msg: "eglQueryDevicesEXT failed: eglError: %x", eglGetError());
241 return false;
242 }
243
244 qCDebug(qLcEglfsKmsDebug, "Found %d EGL devices", num_devices);
245
246 if (num_devices < 1 || m_egl_device == EGL_NO_DEVICE_EXT) {
247 qWarning(msg: "eglQueryDevicesEXT could not find any EGL devices");
248 return false;
249 }
250
251 return true;
252}
253
254QPlatformCursor *QEglFSKmsEglDeviceIntegration::createCursor(QPlatformScreen *screen) const
255{
256#if QT_CONFIG(opengl)
257 if (screenConfig()->separateScreens())
258 return new QEglFSCursor(screen);
259#else
260 Q_UNUSED(screen);
261#endif
262 return nullptr;
263}
264
265QT_END_NAMESPACE
266

source code of qtbase/src/plugins/platforms/eglfs/deviceintegration/eglfs_kms_egldevice/qeglfskmsegldeviceintegration.cpp