8#include <QtCore/QStandardPaths>
10#include <QtWaylandCompositor/QWaylandCompositor>
11#include <QtWaylandCompositor/private/qwltextureorphanage_p.h>
13#include <drm_fourcc.h>
22# include <sys/syscall.h>
25# define MFD_CLOEXEC 0x0001U
27# ifndef MFD_ALLOW_SEALING
28# define MFD_ALLOW_SEALING 0x0002U
32# define F_ADD_SEALS 1033
35# define F_SEAL_SEAL 0x0001
38# define F_SEAL_SHRINK 0x0002
44LinuxDmabuf::LinuxDmabuf(
int version,
46 LinuxDmabufClientBufferIntegration *clientBufferIntegration)
47 : zwp_linux_dmabuf_v1(display, version)
48 , m_clientBufferIntegration(clientBufferIntegration)
54 m_drmDevice = drmDevice;
59 Q_ASSERT(resourceMap().isEmpty());
60 m_modifiers = modifiers;
65 if (resource->version() >= ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION)
68 for (
auto it = m_modifiers.constBegin(); it != m_modifiers.constEnd(); ++it) {
69 auto format = it.key();
70 auto modifiers = it.value();
72 if (modifiers.isEmpty())
74 for (
const auto &modifier : std::as_const(modifiers)) {
75 if (resource->version() >= ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) {
76 const uint32_t modifier_lo = modifier & 0xFFFFFFFF;
77 const uint32_t modifier_hi = modifier >> 32;
78 send_modifier(resource->handle, format, modifier_hi, modifier_lo);
80 send_format(resource->handle, format);
88 wl_resource *r = wl_resource_create(resource->client(), &zwp_linux_buffer_params_v1_interface,
89 wl_resource_get_version(resource->handle), params_id);
95 if (resource->version() < ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION)
98 wl_resource *r = wl_resource_create(resource->client(),
99 &zwp_linux_dmabuf_feedback_v1_interface,
100 wl_resource_get_version(resource->handle),
103 new LinuxDmabufFeedback(m_modifiers, m_drmDevice, r);
108 if (resource->version() < ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION)
112 wl_resource *r = wl_resource_create(resource->client(),
113 &zwp_linux_dmabuf_feedback_v1_interface,
114 wl_resource_get_version(resource->handle),
117 new LinuxDmabufFeedback(m_modifiers, m_drmDevice, r);
120LinuxDmabufFeedback::LinuxDmabufFeedback(QHash<uint32_t, QList<uint64_t>> modifiers,
121 const char *drmDevice, wl_resource *res)
122 : zwp_linux_dmabuf_feedback_v1(res)
123 , m_modifiers(modifiers)
124 , m_drmDevice(drmDevice)
126 sendFeedback(resource());
132 munmap(m_data, m_size);
137 wl_resource_destroy(resource->handle);
148 sendFeedback(resource);
153 QList<std::pair<uint32_t, uint64_t> > formatModifierPairs;
154 for (
auto it = m_modifiers.constBegin(); it != m_modifiers.constEnd(); ++it) {
155 uint32_t format = it.key();
156 QList<uint64_t> modifiers = it.value();
158 if (!modifiers.isEmpty()) {
159 for (uint64_t modifier : modifiers)
160 formatModifierPairs.append(std::make_pair(format, modifier));
166 if (formatModifierPairs.isEmpty()) {
167 qCWarning(qLcWaylandCompositorHardwareIntegration) <<
"LinuxDmabufFeedback: No formats";
172#ifdef SYS_memfd_create
173 fd = syscall(SYS_memfd_create,
"wayland-dmabuf", MFD_CLOEXEC | MFD_ALLOW_SEALING);
175 fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
178 std::unique_ptr<QFile> filePointer;
181 std::make_unique<QTemporaryFile>(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
182 QLatin1String(
"/wayland-dmabuf-XXXXXX"));
184 filePointer = std::move(tmpFile);
186 auto file = std::make_unique<QFile>();
187 if (file->open(fd, QIODevice::ReadWrite | QIODevice::Unbuffered, QFile::AutoCloseHandle))
188 filePointer = std::move(file);
191 m_size = formatModifierPairs.size() * 16;
192 if (!filePointer || !filePointer->resize(m_size)) {
193 qCWarning(qLcWaylandCompositorHardwareIntegration)
194 <<
"LinuxDmabufFeedback: failed: " << filePointer->errorString();
198 fd = filePointer->handle();
201 munmap(m_data, m_size);
202 m_data = (uchar *) mmap(
nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
204 QByteArray indicesArray;
205 indicesArray.resize(formatModifierPairs.size() * 2);
206 char *iptr = indicesArray.data();
209 for (quint16 i = 0; i < quint16(formatModifierPairs.size()); ++i) {
213 const auto &formatModifierPair = formatModifierPairs.at(i);
214 uint32_t format = formatModifierPair.first;
215 uint64_t modifiers = formatModifierPair.second;
216 memcpy(m_data, &format, 4);
221 memcpy(m_data, &modifiers, 8);
225 send_format_table(fd, m_size);
236 QByteArray indices = sendFormatTable(resource);
240 struct stat drmDeviceStat;
241 if (stat(m_drmDevice, &drmDeviceStat)) {
242 qCWarning(qLcWaylandCompositorHardwareIntegration)
243 <<
"Failed to access DRM device in linux-dmabuf-feedback";
247 dev_t mainDevice = drmDeviceStat.st_rdev;
248 QByteArray mainDeviceArray;
249 mainDeviceArray.setRawData(
reinterpret_cast<
const char *>(&mainDevice),
sizeof(dev_t));
250 send_main_device(mainDeviceArray);
253 send_tranche_target_device(mainDeviceArray);
254 send_tranche_flags(0);
255 send_tranche_formats(indices);
263 , m_clientBufferIntegration(clientBufferIntegration)
269 for (
auto it = m_planes.begin(); it != m_planes.end(); ++it) {
270 if (it.value().fd != -1)
271 close(it.value().fd);
276bool LinuxDmabufParams::handleCreateParams(Resource *resource,
int width,
int height, uint format, uint flags)
279 wl_resource_post_error(resource->handle,
280 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED,
281 "Params already used");
285 if (width <= 0 || height <= 0) {
286 wl_resource_post_error(resource->handle,
287 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_DIMENSIONS,
288 "Invalid dimensions in create request");
292 if (m_planes.isEmpty()) {
293 wl_resource_post_error(resource->handle,
294 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
295 "Cannot create a buffer with no planes");
300 auto planeIds = m_planes.keys();
301 std::sort(planeIds.begin(), planeIds.end());
302 for (
int i = 0; i < planeIds.size(); ++i) {
303 if (uint(i) != planeIds[i]) {
304 wl_resource_post_error(resource->handle,
305 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
306 "No dmabuf parameters provided for plane %i", i);
312 for (
auto it = m_planes.constBegin(); it != m_planes.constEnd(); ++it) {
313 const auto planeId = it.key();
314 const auto plane = it.value();
315 if (
static_cast<int64_t>(plane.offset) + plane.stride > UINT32_MAX) {
316 wl_resource_post_error(resource->handle,
317 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
318 "Size overflow for plane %i",
322 if (planeId == 0 &&
static_cast<int64_t>(plane.offset) + plane.stride *
static_cast<int64_t>(height) > UINT32_MAX) {
323 wl_resource_post_error(resource->handle,
324 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
325 "Size overflow for plane %i",
331 off_t size = lseek(plane.fd, 0, SEEK_END);
333 qCDebug(qLcWaylandCompositorHardwareIntegration) <<
"Seeking is not supported";
337 if (
static_cast<int64_t>(plane.offset) >= size) {
338 wl_resource_post_error(resource->handle,
339 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
340 "Invalid offset %i for plane %i",
341 plane.offset, planeId);
345 if (
static_cast<int64_t>(plane.offset) +
static_cast<int64_t>(plane.stride) > size) {
346 wl_resource_post_error(resource->handle,
347 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
348 "Invalid stride %i for plane %i",
349 plane.stride, planeId);
354 if (planeId == 0 && plane.offset +
static_cast<int64_t>(plane.stride) * height > size) {
355 wl_resource_post_error(resource->handle,
356 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
357 "Invalid buffer stride or height for plane %i", planeId);
362 m_size = QSize(width, height);
363 m_drmFormat = format;
372 wl_resource_destroy(resource->handle);
383 const uint64_t modifiers = (
static_cast<uint64_t>(modifier_hi) << 32) | modifier_lo;
385 wl_resource_post_error(resource->handle,
386 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_IDX,
387 "Plane index %i is out of bounds", plane_idx);
390 if (m_planes.contains(plane_idx)) {
391 wl_resource_post_error(resource->handle,
392 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_SET,
393 "Plane already set");
401 m_planes.insert(plane_idx, plane);
406 if (!handleCreateParams(resource, width, height, format, flags))
410 buffer->m_size = m_size;
411 buffer->m_flags = m_flags;
412 buffer->m_drmFormat = m_drmFormat;
413 buffer->m_planesNumber = m_planes.size();
414 for (
auto it = m_planes.begin(); it != m_planes.end(); ++it) {
415 buffer->m_planes[it.key()] = it.value();
419 if (!m_clientBufferIntegration
->importBuffer(buffer->resource()->handle
, buffer
)) {
420 send_failed(resource->handle);
422 send_created(resource->handle, buffer->resource()->handle);
428 if (!handleCreateParams(resource, width, height, format, flags))
431 auto *buffer =
new LinuxDmabufWlBuffer(resource->client(), m_clientBufferIntegration, buffer_id);
432 buffer->m_size = m_size;
433 buffer->m_flags = m_flags;
434 buffer->m_drmFormat = m_drmFormat;
435 buffer->m_planesNumber = m_planes.size();
436 for (
auto it = m_planes.begin(); it != m_planes.end(); ++it) {
437 buffer->m_planes[it.key()] = it.value();
441 if (!m_clientBufferIntegration
->importBuffer(buffer->resource()->handle
, buffer
)) {
445 wl_resource_post_error(resource->handle,
446 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_WL_BUFFER,
447 "Import of the provided DMA buffer failed");
454 , m_clientBufferIntegration(clientBufferIntegration)
468 wl_resource_destroy(resource->handle);
473 QMutexLocker locker(&m_texturesLock);
475 for (uint32_t i = 0; i < m_planesNumber; ++i) {
476 if (m_textures[i] !=
nullptr) {
477 QtWayland::QWaylandTextureOrphanage::instance()->admitTexture(m_textures[i],
478 m_texturesContext[i]);
479 m_textures[i] =
nullptr;
480 m_texturesContext[i] =
nullptr;
481 QObject::disconnect(m_texturesAboutToBeDestroyedConnection[i]);
482 m_texturesAboutToBeDestroyedConnection[i] = QMetaObject::Connection();
484 if (m_eglImages[i] != EGL_NO_IMAGE_KHR) {
486 m_eglImages[i] = EGL_NO_IMAGE_KHR;
488 if (m_planes[i].fd != -1)
489 close(m_planes[i].fd);
497 Q_ASSERT(plane < m_planesNumber);
498 Q_ASSERT(m_eglImages.at(plane) == EGL_NO_IMAGE_KHR);
499 m_eglImages[plane] = image;
504 QMutexLocker locker(&m_texturesLock);
506 Q_ASSERT(plane < m_planesNumber);
507 Q_ASSERT(m_textures.at(plane) ==
nullptr);
508 Q_ASSERT(QOpenGLContext::currentContext());
509 m_textures[plane] = texture;
510 m_texturesContext[plane] = QOpenGLContext::currentContext();
512 m_texturesAboutToBeDestroyedConnection[plane] =
513 QObject::connect(m_texturesContext[plane], &QOpenGLContext::aboutToBeDestroyed,
514 m_texturesContext[plane], [
this, plane]() {
516 QMutexLocker locker(&
this->m_texturesLock);
520 if (
this->m_textures[plane] ==
nullptr)
523 delete this->m_textures[plane];
525 qCDebug(qLcWaylandCompositorHardwareIntegration)
527 <<
"texture deleted due to QOpenGLContext::aboutToBeDestroyed!"
528 <<
"Pointer (now dead) was:" << (
void*)(
this->m_textures[plane])
529 <<
" Associated context (about to die too) is: " << (
void*)(
this->m_texturesContext[plane]);
531 this->m_textures[plane] =
nullptr;
532 this->m_texturesContext[plane] =
nullptr;
534 QObject::disconnect(
this->m_texturesAboutToBeDestroyedConnection[plane]);
535 this->m_texturesAboutToBeDestroyedConnection[plane] = QMetaObject::Connection();
537 }, Qt::DirectConnection);
bool importBuffer(wl_resource *resource, LinuxDmabufWlBuffer *linuxDmabufBuffer)
void deleteImage(EGLImageKHR image)
void removeBuffer(wl_resource *resource)
void zwp_linux_dmabuf_feedback_v1_bind_resource(Resource *resource) override
void zwp_linux_dmabuf_feedback_v1_destroy_resource(Resource *resource) override
~LinuxDmabufFeedback() override
void zwp_linux_dmabuf_feedback_v1_destroy(Resource *resource) override
void zwp_linux_buffer_params_v1_create_immed(Resource *resource, uint32_t buffer_id, int32_t width, int32_t height, uint32_t format, uint32_t flags) override
~LinuxDmabufParams() override
LinuxDmabufParams(LinuxDmabufClientBufferIntegration *clientBufferIntegration, wl_resource *resource)
void zwp_linux_buffer_params_v1_create(Resource *resource, int32_t width, int32_t height, uint32_t format, uint32_t flags) override
void zwp_linux_buffer_params_v1_destroy(Resource *resource) override
void zwp_linux_buffer_params_v1_add(Resource *resource, int32_t fd, uint32_t plane_idx, uint32_t offset, uint32_t stride, uint32_t modifier_hi, uint32_t modifier_lo) override
void zwp_linux_buffer_params_v1_destroy_resource(Resource *resource) override
void initTexture(uint32_t plane, QOpenGLTexture *texture)
void initImage(uint32_t plane, EGLImageKHR image)
void buffer_destroy(Resource *resource) override
~LinuxDmabufWlBuffer() override
LinuxDmabufWlBuffer(::wl_client *client, LinuxDmabufClientBufferIntegration *clientBufferIntegration, uint id=0)
static const uint32_t MaxDmabufPlanes
void buffer_destroy_resource(Resource *resource) override
void zwp_linux_dmabuf_v1_get_surface_feedback(Resource *resource, uint32_t id, struct ::wl_resource *surface) override
void setDrmDevice(const char *drmDevice)
void zwp_linux_dmabuf_v1_bind_resource(Resource *resource) override
void setSupportedModifiers(const QHash< uint32_t, QList< uint64_t > > &modifiers)
void zwp_linux_dmabuf_v1_create_params(Resource *resource, uint32_t params_id) override
void zwp_linux_dmabuf_v1_get_default_feedback(Resource *resource, uint32_t id) override
#define DRM_FORMAT_MOD_LINEAR
#define DRM_FORMAT_MOD_INVALID