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