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
linuxdmabufclientbufferintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include "linuxdmabuf.h"
7
8#include <QtWaylandCompositor/QWaylandCompositor>
9#include <QtWaylandCompositor/private/qwayland-server-wayland.h>
10#include <QtWaylandCompositor/private/qwltextureorphanage_p.h>
11#include <qpa/qplatformnativeinterface.h>
12#include <QtOpenGL/QOpenGLTexture>
13#include <QtCore/QVarLengthArray>
14#include <QtGui/QGuiApplication>
15#include <QtGui/QOpenGLContext>
16
17#include <EGL/egl.h>
18#include <EGL/eglext.h>
19#include <unistd.h>
20#include <drm_fourcc.h>
21
22QT_BEGIN_NAMESPACE
23
24static QWaylandBufferRef::BufferFormatEgl formatFromDrmFormat(EGLint format) {
25 switch (format) {
26 case DRM_FORMAT_RGB332:
27 case DRM_FORMAT_BGR233:
28 case DRM_FORMAT_XRGB4444:
29 case DRM_FORMAT_XBGR4444:
30 case DRM_FORMAT_RGBX4444:
31 case DRM_FORMAT_BGRX4444:
32 case DRM_FORMAT_XRGB1555:
33 case DRM_FORMAT_XBGR1555:
34 case DRM_FORMAT_RGBX5551:
35 case DRM_FORMAT_BGRX5551:
36 case DRM_FORMAT_RGB565:
37 case DRM_FORMAT_BGR565:
38 case DRM_FORMAT_RGB888:
39 case DRM_FORMAT_BGR888:
40 case DRM_FORMAT_XRGB8888:
41 case DRM_FORMAT_XBGR8888:
42 case DRM_FORMAT_RGBX8888:
43 case DRM_FORMAT_BGRX8888:
44 case DRM_FORMAT_XRGB2101010:
45 case DRM_FORMAT_XBGR2101010:
46 case DRM_FORMAT_RGBX1010102:
47 case DRM_FORMAT_BGRX1010102:
48 return QWaylandBufferRef::BufferFormatEgl_RGB;
49 case DRM_FORMAT_ARGB4444:
50 case DRM_FORMAT_ABGR4444:
51 case DRM_FORMAT_RGBA4444:
52 case DRM_FORMAT_BGRA4444:
53 case DRM_FORMAT_ARGB1555:
54 case DRM_FORMAT_ABGR1555:
55 case DRM_FORMAT_RGBA5551:
56 case DRM_FORMAT_BGRA5551:
57 case DRM_FORMAT_ARGB8888:
58 case DRM_FORMAT_ABGR8888:
59 case DRM_FORMAT_RGBA8888:
60 case DRM_FORMAT_BGRA8888:
61 case DRM_FORMAT_ARGB2101010:
62 case DRM_FORMAT_ABGR2101010:
63 case DRM_FORMAT_RGBA1010102:
64 case DRM_FORMAT_BGRA1010102:
65 return QWaylandBufferRef::BufferFormatEgl_RGBA;
66 case DRM_FORMAT_YUYV:
67 return QWaylandBufferRef::BufferFormatEgl_Y_XUXV;
68 default:
69 qCDebug(qLcWaylandCompositorHardwareIntegration) << "Buffer format" << Qt::hex << format << "not supported";
70 return QWaylandBufferRef::BufferFormatEgl_Null;
71 }
72}
73
74static QOpenGLTexture::TextureFormat openGLFormatFromBufferFormat(QWaylandBufferRef::BufferFormatEgl format) {
75 switch (format) {
76 case QWaylandBufferRef::BufferFormatEgl_RGB:
77 return QOpenGLTexture::RGBFormat;
78 case QWaylandBufferRef::BufferFormatEgl_RGBA:
79 return QOpenGLTexture::RGBAFormat;
80 default:
81 return QOpenGLTexture::NoFormat;
82 }
83}
84
85// Initialize the EGLImage for a dmabuf buffer which conceptually consists of a
86// single plane. Note that depending on the modifiers, the buffer may be actually
87// transported as multiple dmabuf planes which must be combined into a single
88// EGLImage. For formats where the buffer needs to be represented as multiple
89// EGLImages (e.g., various YUV formats) a different approach is required.
90bool LinuxDmabufClientBufferIntegration::initSimpleTexture(LinuxDmabufWlBuffer *dmabufBuffer)
91{
92 bool success = true;
93
94 // Resolving GL functions may need a context current, so do it only here.
95 if (!gl_egl_image_target_texture_2d)
96 gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
97
98 if (dmabufBuffer->plane(0).modifiers != DRM_FORMAT_MOD_INVALID && !m_supportsDmabufModifiers) {
99 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Buffer uses dmabuf modifiers, which are not supported.";
100 success = false;
101 }
102
103 // 6 entries for the common attribs plus 10 per possible plane, plus 1 for
104 // the final EGL_NONE sentinel.
105 QVarLengthArray<EGLint, 6 + 10 * 4 + 1> attribs;
106
107 attribs.append(EGL_WIDTH);
108 attribs.append(dmabufBuffer->size().width());
109 attribs.append(EGL_HEIGHT);
110 attribs.append(dmabufBuffer->size().height());
111 attribs.append(EGL_LINUX_DRM_FOURCC_EXT);
112 attribs.append(EGLint(dmabufBuffer->drmFormat()));
113
114#define ADD_PLANE_ATTRIBS(plane_idx) {
115 attribs.append(EGL_DMA_BUF_PLANE ## plane_idx ## _FD_EXT);
116 attribs.append(dmabufBuffer->plane(plane_idx).fd);
117 attribs.append(EGL_DMA_BUF_PLANE ## plane_idx ## _OFFSET_EXT);
118 attribs.append(EGLint(dmabufBuffer->plane(plane_idx).offset));
119 attribs.append(EGL_DMA_BUF_PLANE ## plane_idx ## _PITCH_EXT);
120 attribs.append(EGLint(dmabufBuffer->plane(plane_idx).stride));
121 if (dmabufBuffer->plane(plane_idx).modifiers != DRM_FORMAT_MOD_INVALID) {
122 attribs.append(EGL_DMA_BUF_PLANE ## plane_idx ## _MODIFIER_LO_EXT);
123 attribs.append(EGLint(dmabufBuffer->plane(plane_idx).modifiers & 0xffffffff));
124 attribs.append(EGL_DMA_BUF_PLANE ## plane_idx ## _MODIFIER_HI_EXT);
125 attribs.append(EGLint(dmabufBuffer->plane(plane_idx).modifiers >> 32));
126 } \
127}
128
129 switch (dmabufBuffer->planesNumber()) {
130 case 4:
132 Q_FALLTHROUGH();
133 case 3:
135 Q_FALLTHROUGH();
136 case 2:
138 Q_FALLTHROUGH();
139 case 1:
141 break;
142 default:
143 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Buffer uses invalid number of planes:" << dmabufBuffer->planesNumber();
144 return false;
145 }
146
147 attribs.append(EGL_NONE);
148
149 // note: EGLImageKHR does NOT take ownership of the file descriptors
150 EGLImageKHR image = egl_create_image(m_eglDisplay,
151 EGL_NO_CONTEXT,
152 EGL_LINUX_DMA_BUF_EXT,
153 (EGLClientBuffer) nullptr,
154 attribs.constData());
155
156 if (image == EGL_NO_IMAGE_KHR) {
157 qCWarning(qLcWaylandCompositorHardwareIntegration) << "failed to create EGL image from" <<
158 dmabufBuffer->planesNumber() << "plane(s)";
159 success = false;
160 }
161
162 dmabufBuffer->initImage(0, image);
163
164 return success;
165}
166
167bool LinuxDmabufClientBufferIntegration::initYuvTexture(LinuxDmabufWlBuffer *dmabufBuffer)
168{
169 bool success = true;
170
171 const YuvFormatConversion conversion = m_yuvFormats.value(dmabufBuffer->drmFormat());
172 if (conversion.inputPlanes != dmabufBuffer->planesNumber()) {
173 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Buffer for this format must provide" << conversion.inputPlanes
174 << "planes but only" << dmabufBuffer->planesNumber() << "received";
175 return false;
176 }
177
178 // Resolving GL functions may need a context current, so do it only here.
179 if (!gl_egl_image_target_texture_2d)
180 gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
181
182
183 if (dmabufBuffer->plane(0).modifiers != DRM_FORMAT_MOD_INVALID && !m_supportsDmabufModifiers) {
184 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Buffer uses dmabuf modifiers, which are not supported.";
185 success = false;
186 }
187
188 for (uint32_t i = 0; i < conversion.outputPlanes; ++i) {
189 const YuvPlaneConversion plane = conversion.plane[i];
190
191 QVarLengthArray<EGLint, 17> attribs = {
192 EGL_WIDTH, dmabufBuffer->size().width() / plane.widthDivisor,
193 EGL_HEIGHT, dmabufBuffer->size().height() / plane.heightDivisor,
194 EGL_LINUX_DRM_FOURCC_EXT, plane.format,
195 EGL_DMA_BUF_PLANE0_FD_EXT, dmabufBuffer->plane(plane.planeIndex).fd,
196 EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGLint(dmabufBuffer->plane(plane.planeIndex).offset),
197 EGL_DMA_BUF_PLANE0_PITCH_EXT, EGLint(dmabufBuffer->plane(plane.planeIndex).stride),
198 EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, EGLint(dmabufBuffer->plane(plane.planeIndex).modifiers & 0xffffffff),
199 EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, EGLint(dmabufBuffer->plane(plane.planeIndex).modifiers >> 32),
200 EGL_NONE
201 };
202
203 // note: EGLImageKHR does NOT take ownership of the file descriptors
204 EGLImageKHR image = egl_create_image(m_eglDisplay,
205 EGL_NO_CONTEXT,
206 EGL_LINUX_DMA_BUF_EXT,
207 (EGLClientBuffer) nullptr,
208 attribs.constData());
209
210 if (image == EGL_NO_IMAGE_KHR) {
211 qCWarning(qLcWaylandCompositorHardwareIntegration) << "failed to create EGL image for plane" << i;
212 success = false;
213 }
214
215 dmabufBuffer->initImage(i, image);
216 }
217 return success;
218}
219
222{
223 YuvPlaneConversion firstPlane;
224 firstPlane.format = DRM_FORMAT_GR88;
225 firstPlane.widthDivisor = 1;
226 firstPlane.heightDivisor = 1;
227 firstPlane.planeIndex = 0;
228
229 YuvPlaneConversion secondPlane;
230 secondPlane.format = DRM_FORMAT_ARGB8888;
231 secondPlane.widthDivisor = 2;
232 secondPlane.heightDivisor = 1;
233 secondPlane.planeIndex = 0;
234
235 YuvFormatConversion formatConversion;
236 formatConversion.inputPlanes = 1;
237 formatConversion.outputPlanes = 2;
238 formatConversion.plane[0] = firstPlane;
239 formatConversion.plane[1] = secondPlane;
240
241 m_yuvFormats.insert(DRM_FORMAT_YUYV, formatConversion);
242}
243
245{
246 m_importedBuffers.clear();
247
248 if (m_useLegacyVersion) {
249 if (egl_unbind_wayland_display != nullptr && m_displayBound) {
250 Q_ASSERT(m_wlDisplay != nullptr);
251 if (!egl_unbind_wayland_display(m_eglDisplay, m_wlDisplay))
252 qCWarning(qLcWaylandCompositorHardwareIntegration)
253 << "eglUnbindWaylandDisplayWL failed";
254 }
255 }
256}
257
259{
260 const int version = m_useLegacyVersion ? 3 : 4;
261 m_linuxDmabuf.reset(new LinuxDmabuf(version, display, this));
262
263 // initialize hardware extensions
264 egl_query_dmabuf_modifiers_ext = reinterpret_cast<PFNEGLQUERYDMABUFMODIFIERSEXTPROC>(eglGetProcAddress("eglQueryDmaBufModifiersEXT"));
265 egl_query_dmabuf_formats_ext = reinterpret_cast<PFNEGLQUERYDMABUFFORMATSEXTPROC>(eglGetProcAddress("eglQueryDmaBufFormatsEXT"));
266 if (!egl_query_dmabuf_modifiers_ext || !egl_query_dmabuf_formats_ext) {
267 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize EGL display. Could not find eglQueryDmaBufModifiersEXT and eglQueryDmaBufFormatsEXT.";
268 return;
269 }
270
271 if (m_useLegacyVersion) {
272 const bool ignoreBindDisplay = !qgetenv("QT_WAYLAND_IGNORE_BIND_DISPLAY").isEmpty() && qgetenv("QT_WAYLAND_IGNORE_BIND_DISPLAY").toInt() != 0;
273
274 egl_bind_wayland_display = reinterpret_cast<PFNEGLBINDWAYLANDDISPLAYWL>(eglGetProcAddress("eglBindWaylandDisplayWL"));
275 egl_unbind_wayland_display = reinterpret_cast<PFNEGLUNBINDWAYLANDDISPLAYWL>(eglGetProcAddress("eglUnbindWaylandDisplayWL"));
276 if ((!egl_bind_wayland_display || !egl_unbind_wayland_display) && !ignoreBindDisplay) {
277 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize EGL display. Could not find eglBindWaylandDisplayWL and eglUnbindWaylandDisplayWL.";
278 return;
279 }
280 } else {
281 egl_query_display_attrib = reinterpret_cast<PFNEGLQUERYDISPLAYATTRIBEXTPROC>(eglGetProcAddress("eglQueryDisplayAttribEXT"));
282 egl_query_device_string = reinterpret_cast<PFNEGLQUERYDEVICESTRINGEXTPROC>(eglGetProcAddress("eglQueryDeviceStringEXT"));
283 if (!egl_query_display_attrib || !egl_query_device_string) {
284 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize linux-dmabuf extension. Could not find required function eglQueryDisplayAttribEXT() or eglQueryDeviceStringEXT().";
285 return;
286 }
287 }
288
289 egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
290 egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
291 if (!egl_create_image || !egl_destroy_image) {
292 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize EGL display. Could not find eglCreateImageKHR and eglDestroyImageKHR.";
293 return;
294 }
295
296 // initialize EGL display
297 QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface();
298 if (!nativeInterface) {
299 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize EGL display. No native platform interface available.";
300 return;
301 }
302
303 m_eglDisplay = nativeInterface->nativeResourceForIntegration("EglDisplay");
304 if (!m_eglDisplay) {
305 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize EGL display. Could not get EglDisplay for window.";
306 return;
307 }
308
309 const char *extensionString = eglQueryString(m_eglDisplay, EGL_EXTENSIONS);
310 if (!extensionString || !strstr(extensionString, "EGL_EXT_image_dma_buf_import")) {
311 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to initialize EGL display. There is no EGL_EXT_image_dma_buf_import extension.";
312 return;
313 }
314 if (strstr(extensionString, "EGL_EXT_image_dma_buf_import_modifiers"))
315 m_supportsDmabufModifiers = true;
316
317 if (m_useLegacyVersion && egl_bind_wayland_display && egl_unbind_wayland_display) {
318 m_displayBound = egl_bind_wayland_display(m_eglDisplay, display);
319 if (!m_displayBound)
320 qCDebug(qLcWaylandCompositorHardwareIntegration) << "Wayland display already bound by other client buffer integration.";
321 m_wlDisplay = display;
322 }
323
324 // request and sent formats/modifiers only after egl_display is bound
325 QHash<uint32_t, QList<uint64_t>> modifiers;
326 for (const auto &format : supportedDrmFormats()) {
327 modifiers[format] = supportedDrmModifiers(format);
328 }
329 m_linuxDmabuf->setSupportedModifiers(modifiers);
330 m_linuxDmabuf->setDrmDevice(drmDevice());
331}
332
333const char *LinuxDmabufClientBufferIntegration::drmDevice() const
334{
335 if (!egl_query_device_string || !egl_query_display_attrib)
336 return nullptr;
337
338 EGLAttrib attrib;
339 if (!egl_query_display_attrib(m_eglDisplay, EGL_DEVICE_EXT, &attrib)) {
340 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to query EGL device";
341 return nullptr;
342 }
343
344 EGLDeviceEXT eglDevice = reinterpret_cast<EGLDeviceEXT>(attrib);
345 const char *ret = egl_query_device_string(eglDevice, EGL_DRM_RENDER_NODE_FILE_EXT);
346 if (!ret)
347 ret = egl_query_device_string(eglDevice, EGL_DRM_DEVICE_FILE_EXT);
348 if (!ret)
349 qCWarning(qLcWaylandCompositorHardwareIntegration) << "Failed to query DRM device";
350
351 return ret;
352}
353
354QList<uint32_t> LinuxDmabufClientBufferIntegration::supportedDrmFormats()
355{
356 if (!egl_query_dmabuf_formats_ext)
357 return QList<uint32_t>();
358
359 // request total number of formats
360 EGLint count = 0;
361 EGLBoolean success = egl_query_dmabuf_formats_ext(m_eglDisplay, 0, nullptr, &count);
362
363 if (success && count > 0) {
364 QList<uint32_t> drmFormats(count);
365 if (egl_query_dmabuf_formats_ext(m_eglDisplay, count, (EGLint *) drmFormats.data(), &count))
366 return drmFormats;
367 }
368
369 return QList<uint32_t>();
370}
371
372QList<uint64_t> LinuxDmabufClientBufferIntegration::supportedDrmModifiers(uint32_t format)
373{
374 if (!egl_query_dmabuf_modifiers_ext)
375 return QList<uint64_t>();
376
377 // request total number of formats
378 EGLint count = 0;
379 EGLBoolean success = egl_query_dmabuf_modifiers_ext(m_eglDisplay, format, 0, nullptr, nullptr, &count);
380
381 if (success && count > 0) {
382 QList<uint64_t> modifiers(count);
383 if (egl_query_dmabuf_modifiers_ext(m_eglDisplay, format, count, modifiers.data(), nullptr, &count)) {
384 return modifiers;
385 }
386 }
387
388 return QList<uint64_t>();
389}
390
392{
393 egl_destroy_image(m_eglDisplay, image);
394}
395
397{
398 auto it = m_importedBuffers.find(resource);
399 if (it != m_importedBuffers.end())
400 return new LinuxDmabufClientBuffer(this, resource, it.value());
401
402 return nullptr;
403}
404
405bool LinuxDmabufClientBufferIntegration::importBuffer(wl_resource *resource, LinuxDmabufWlBuffer *linuxDmabufBuffer)
406{
407 if (m_importedBuffers.contains(resource)) {
408 qCWarning(qLcWaylandCompositorHardwareIntegration) << "buffer has already been added";
409 return false;
410 }
411 m_importedBuffers[resource] = linuxDmabufBuffer;
412 if (m_yuvFormats.contains(linuxDmabufBuffer->drmFormat()))
413 return initYuvTexture(linuxDmabufBuffer);
414 else
415 return initSimpleTexture(linuxDmabufBuffer);
416}
417
419{
420 m_importedBuffers.remove(resource);
421}
422
423LinuxDmabufClientBuffer::LinuxDmabufClientBuffer(LinuxDmabufClientBufferIntegration *integration,
424 wl_resource *bufferResource,
425 LinuxDmabufWlBuffer *dmabufBuffer)
426 : ClientBuffer(bufferResource)
427 , m_integration(integration)
428{
429 d = dmabufBuffer;
430}
431
433{
434 // At this point we should have a valid OpenGL context, so it's safe to destroy textures
435 QtWayland::QWaylandTextureOrphanage::instance()->deleteTextures();
436
437 if (!m_buffer)
438 return nullptr;
439
440 QOpenGLTexture *texture = d->texture(plane);
441
442 const auto target = static_cast<QOpenGLTexture::Target>(GL_TEXTURE_2D);
443
444 if (!texture) {
445 texture = new QOpenGLTexture(target);
446 texture->setFormat(openGLFormatFromBufferFormat(formatFromDrmFormat(d->drmFormat())));
447 texture->setSize(d->size().width(), d->size().height());
448 texture->create();
449 d->initTexture(plane, texture);
450 }
451
452 if (m_textureDirty) {
453 m_textureDirty = false;
454 texture->bind();
455 glTexParameterf(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
456 m_integration->gl_egl_image_target_texture_2d(target, d->image(plane));
457 }
458 return texture;
459}
460
462{
463 m_integration->removeBuffer(m_buffer);
464 ClientBuffer::setDestroyed();
465}
466
468{
469 m_buffer = nullptr;
470 delete d;
471}
472
474{
475 return formatFromDrmFormat(d->drmFormat());
476}
477
479{
480 return d->size();
481}
482
484{
485 return (d->flags() & QtWaylandServer::zwp_linux_buffer_params_v1::flags_y_invert) ? QWaylandSurface::OriginBottomLeft : QWaylandSurface::OriginTopLeft;
486}
487
488QT_END_NAMESPACE
QtWayland::ClientBuffer * createBufferFor(wl_resource *resource) override
bool importBuffer(wl_resource *resource, LinuxDmabufWlBuffer *linuxDmabufBuffer)
void initializeHardware(struct ::wl_display *display) override
QOpenGLTexture * toOpenGlTexture(int plane) override
QWaylandBufferRef::BufferFormatEgl bufferFormatEgl() const override
QWaylandSurface::Origin origin() const override
uint32_t drmFormat() const
void initImage(uint32_t plane, EGLImageKHR image)
EGLImageKHR image(uint32_t plane)
uint32_t planesNumber() const
#define EGL_DRM_RENDER_NODE_FILE_EXT
Definition linuxdmabuf.h:42
#define DRM_FORMAT_MOD_INVALID
Definition linuxdmabuf.h:38
#define ADD_PLANE_ATTRIBS(plane_idx)
static QOpenGLTexture::TextureFormat openGLFormatFromBufferFormat(QWaylandBufferRef::BufferFormatEgl format)
#define EGL_DRM_DEVICE_FILE_EXT
struct YuvPlaneConversion plane[LinuxDmabufWlBuffer::MaxDmabufPlanes]