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