7#include <QtCore/QStandardPaths>
9#include <QtWaylandCompositor/QWaylandCompositor>
10#include <QtWaylandCompositor/private/qwltextureorphanage_p.h>
12#include <drm_fourcc.h>
21# include <sys/syscall.h>
24# define MFD_CLOEXEC 0x0001U
26# ifndef MFD_ALLOW_SEALING
27# define MFD_ALLOW_SEALING 0x0002U
31# define F_ADD_SEALS 1033
34# define F_SEAL_SEAL 0x0001
37# define F_SEAL_SHRINK 0x0002
43LinuxDmabuf::LinuxDmabuf(
int version,
45 LinuxDmabufClientBufferIntegration *clientBufferIntegration)
46 : zwp_linux_dmabuf_v1(display, version)
47 , m_clientBufferIntegration(clientBufferIntegration)
53 m_drmDevice = drmDevice;
58 Q_ASSERT(resourceMap().isEmpty());
59 m_modifiers = modifiers;
64 if (resource->version() >= ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION)
67 for (
auto it = m_modifiers.constBegin(); it != m_modifiers.constEnd(); ++it) {
68 auto format = it.key();
69 auto modifiers = it.value();
71 if (modifiers.isEmpty())
73 for (
const auto &modifier : std::as_const(modifiers)) {
74 if (resource->version() >= ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) {
75 const uint32_t modifier_lo = modifier & 0xFFFFFFFF;
76 const uint32_t modifier_hi = modifier >> 32;
77 send_modifier(resource->handle, format, modifier_hi, modifier_lo);
79 send_format(resource->handle, format);
87 wl_resource *r = wl_resource_create(resource->client(), &zwp_linux_buffer_params_v1_interface,
88 wl_resource_get_version(resource->handle), params_id);
94 if (resource->version() < ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION)
97 wl_resource *r = wl_resource_create(resource->client(),
98 &zwp_linux_dmabuf_feedback_v1_interface,
99 wl_resource_get_version(resource->handle),
102 new LinuxDmabufFeedback(m_modifiers, m_drmDevice, r);
107 if (resource->version() < ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION)
111 wl_resource *r = wl_resource_create(resource->client(),
112 &zwp_linux_dmabuf_feedback_v1_interface,
113 wl_resource_get_version(resource->handle),
116 new LinuxDmabufFeedback(m_modifiers, m_drmDevice, r);
119LinuxDmabufFeedback::LinuxDmabufFeedback(QHash<uint32_t, QList<uint64_t>> modifiers,
120 const char *drmDevice, wl_resource *res)
121 : zwp_linux_dmabuf_feedback_v1(res)
122 , m_modifiers(modifiers)
123 , m_drmDevice(drmDevice)
125 sendFeedback(resource());
131 munmap(m_data, m_size);
136 wl_resource_destroy(resource->handle);
147 sendFeedback(resource);
152 QList<std::pair<uint32_t, uint64_t> > formatModifierPairs;
153 for (
auto it = m_modifiers.constBegin(); it != m_modifiers.constEnd(); ++it) {
154 uint32_t format = it.key();
155 QList<uint64_t> modifiers = it.value();
157 if (!modifiers.isEmpty()) {
158 for (uint64_t modifier : modifiers)
159 formatModifierPairs.append(std::make_pair(format, modifier));
165 if (formatModifierPairs.isEmpty()) {
166 qCWarning(qLcWaylandCompositorHardwareIntegration) <<
"LinuxDmabufFeedback: No formats";
171#ifdef SYS_memfd_create
172 fd = syscall(SYS_memfd_create,
"wayland-dmabuf", MFD_CLOEXEC | MFD_ALLOW_SEALING);
174 fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
177 std::unique_ptr<QFile> filePointer;
180 std::make_unique<QTemporaryFile>(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
181 QLatin1String(
"/wayland-dmabuf-XXXXXX"));
183 filePointer = std::move(tmpFile);
185 auto file = std::make_unique<QFile>();
186 if (file->open(fd, QIODevice::ReadWrite | QIODevice::Unbuffered, QFile::AutoCloseHandle))
187 filePointer = std::move(file);
190 m_size = formatModifierPairs.size() * 16;
191 if (!filePointer || !filePointer->resize(m_size)) {
192 qCWarning(qLcWaylandCompositorHardwareIntegration)
193 <<
"LinuxDmabufFeedback: failed: " << filePointer->errorString();
197 fd = filePointer->handle();
200 munmap(m_data, m_size);
201 m_data = (uchar *) mmap(
nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
203 QByteArray indicesArray;
204 indicesArray.resize(formatModifierPairs.size() * 2);
205 char *iptr = indicesArray.data();
208 for (quint16 i = 0; i < quint16(formatModifierPairs.size()); ++i) {
212 const auto &formatModifierPair = formatModifierPairs.at(i);
213 uint32_t format = formatModifierPair.first;
214 uint64_t modifiers = formatModifierPair.second;
215 memcpy(m_data, &format, 4);
220 memcpy(m_data, &modifiers, 8);
224 send_format_table(fd, m_size);
235 QByteArray indices = sendFormatTable(resource);
239 struct stat drmDeviceStat;
240 if (stat(m_drmDevice, &drmDeviceStat)) {
241 qCWarning(qLcWaylandCompositorHardwareIntegration)
242 <<
"Failed to access DRM device in linux-dmabuf-feedback";
246 dev_t mainDevice = drmDeviceStat.st_rdev;
247 QByteArray mainDeviceArray;
248 mainDeviceArray.setRawData(
reinterpret_cast<
const char *>(&mainDevice),
sizeof(dev_t));
249 send_main_device(mainDeviceArray);
252 send_tranche_target_device(mainDeviceArray);
253 send_tranche_flags(0);
254 send_tranche_formats(indices);
262 , m_clientBufferIntegration(clientBufferIntegration)
268 for (
auto it = m_planes.begin(); it != m_planes.end(); ++it) {
269 if (it.value().fd != -1)
270 close(it.value().fd);
275bool LinuxDmabufParams::handleCreateParams(Resource *resource,
int width,
int height, uint format, uint flags)
278 wl_resource_post_error(resource->handle,
279 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED,
280 "Params already used");
284 if (width <= 0 || height <= 0) {
285 wl_resource_post_error(resource->handle,
286 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_DIMENSIONS,
287 "Invalid dimensions in create request");
291 if (m_planes.isEmpty()) {
292 wl_resource_post_error(resource->handle,
293 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
294 "Cannot create a buffer with no planes");
299 auto planeIds = m_planes.keys();
300 std::sort(planeIds.begin(), planeIds.end());
301 for (
int i = 0; i < planeIds.size(); ++i) {
302 if (uint(i) != planeIds[i]) {
303 wl_resource_post_error(resource->handle,
304 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
305 "No dmabuf parameters provided for plane %i", i);
311 for (
auto it = m_planes.constBegin(); it != m_planes.constEnd(); ++it) {
312 const auto planeId = it.key();
313 const auto plane = it.value();
314 if (
static_cast<int64_t>(plane.offset) + plane.stride > UINT32_MAX) {
315 wl_resource_post_error(resource->handle,
316 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
317 "Size overflow for plane %i",
321 if (planeId == 0 &&
static_cast<int64_t>(plane.offset) + plane.stride *
static_cast<int64_t>(height) > UINT32_MAX) {
322 wl_resource_post_error(resource->handle,
323 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
324 "Size overflow for plane %i",
330 off_t size = lseek(plane.fd, 0, SEEK_END);
332 qCDebug(qLcWaylandCompositorHardwareIntegration) <<
"Seeking is not supported";
336 if (
static_cast<int64_t>(plane.offset) >= size) {
337 wl_resource_post_error(resource->handle,
338 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
339 "Invalid offset %i for plane %i",
340 plane.offset, planeId);
344 if (
static_cast<int64_t>(plane.offset) +
static_cast<int64_t>(plane.stride) > size) {
345 wl_resource_post_error(resource->handle,
346 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
347 "Invalid stride %i for plane %i",
348 plane.stride, planeId);
353 if (planeId == 0 && plane.offset +
static_cast<int64_t>(plane.stride) * height > size) {
354 wl_resource_post_error(resource->handle,
355 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
356 "Invalid buffer stride or height for plane %i", planeId);
361 m_size = QSize(width, height);
362 m_drmFormat = format;
371 wl_resource_destroy(resource->handle);
382 const uint64_t modifiers = (
static_cast<uint64_t>(modifier_hi) << 32) | modifier_lo;
384 wl_resource_post_error(resource->handle,
385 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_IDX,
386 "Plane index %i is out of bounds", plane_idx);
389 if (m_planes.contains(plane_idx)) {
390 wl_resource_post_error(resource->handle,
391 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_SET,
392 "Plane already set");
400 m_planes.insert(plane_idx, plane);
405 if (!handleCreateParams(resource, width, height, format, flags))
409 buffer->m_size = m_size;
410 buffer->m_flags = m_flags;
411 buffer->m_drmFormat = m_drmFormat;
412 buffer->m_planesNumber = m_planes.size();
413 for (
auto it = m_planes.begin(); it != m_planes.end(); ++it) {
414 buffer->m_planes[it.key()] = it.value();
418 if (!m_clientBufferIntegration
->importBuffer(buffer->resource()->handle
, buffer
)) {
419 send_failed(resource->handle);
421 send_created(resource->handle, buffer->resource()->handle);
427 if (!handleCreateParams(resource, width, height, format, flags))
430 auto *buffer =
new LinuxDmabufWlBuffer(resource->client(), m_clientBufferIntegration, buffer_id);
431 buffer->m_size = m_size;
432 buffer->m_flags = m_flags;
433 buffer->m_drmFormat = m_drmFormat;
434 buffer->m_planesNumber = m_planes.size();
435 for (
auto it = m_planes.begin(); it != m_planes.end(); ++it) {
436 buffer->m_planes[it.key()] = it.value();
440 if (!m_clientBufferIntegration
->importBuffer(buffer->resource()->handle
, buffer
)) {
444 wl_resource_post_error(resource->handle,
445 ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_WL_BUFFER,
446 "Import of the provided DMA buffer failed");
453 , m_clientBufferIntegration(clientBufferIntegration)
467 wl_resource_destroy(resource->handle);
472 QMutexLocker locker(&m_texturesLock);
474 for (uint32_t i = 0; i < m_planesNumber; ++i) {
475 if (m_textures[i] !=
nullptr) {
476 QtWayland::QWaylandTextureOrphanage::instance()->admitTexture(m_textures[i],
477 m_texturesContext[i]);
478 m_textures[i] =
nullptr;
479 m_texturesContext[i] =
nullptr;
480 QObject::disconnect(m_texturesAboutToBeDestroyedConnection[i]);
481 m_texturesAboutToBeDestroyedConnection[i] = QMetaObject::Connection();
483 if (m_eglImages[i] != EGL_NO_IMAGE_KHR) {
485 m_eglImages[i] = EGL_NO_IMAGE_KHR;
487 if (m_planes[i].fd != -1)
488 close(m_planes[i].fd);
496 Q_ASSERT(plane < m_planesNumber);
497 Q_ASSERT(m_eglImages.at(plane) == EGL_NO_IMAGE_KHR);
498 m_eglImages[plane] = image;
503 QMutexLocker locker(&m_texturesLock);
505 Q_ASSERT(plane < m_planesNumber);
506 Q_ASSERT(m_textures.at(plane) ==
nullptr);
507 Q_ASSERT(QOpenGLContext::currentContext());
508 m_textures[plane] = texture;
509 m_texturesContext[plane] = QOpenGLContext::currentContext();
511 m_texturesAboutToBeDestroyedConnection[plane] =
512 QObject::connect(m_texturesContext[plane], &QOpenGLContext::aboutToBeDestroyed,
513 m_texturesContext[plane], [
this, plane]() {
515 QMutexLocker locker(&
this->m_texturesLock);
519 if (
this->m_textures[plane] ==
nullptr)
522 delete this->m_textures[plane];
524 qCDebug(qLcWaylandCompositorHardwareIntegration)
526 <<
"texture deleted due to QOpenGLContext::aboutToBeDestroyed!"
527 <<
"Pointer (now dead) was:" << (
void*)(
this->m_textures[plane])
528 <<
" Associated context (about to die too) is: " << (
void*)(
this->m_texturesContext[plane]);
530 this->m_textures[plane] =
nullptr;
531 this->m_texturesContext[plane] =
nullptr;
533 QObject::disconnect(
this->m_texturesAboutToBeDestroyedConnection[plane]);
534 this->m_texturesAboutToBeDestroyedConnection[plane] = QMetaObject::Connection();
536 }, 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