wayland/drmlease: use FileDescriptor class

This commit is contained in:
Xaver Hugl 2022-07-27 13:19:10 +02:00
parent 1464028934
commit 1b1e0ab95f
4 changed files with 24 additions and 25 deletions

View file

@ -88,15 +88,14 @@ DrmGpu::DrmGpu(DrmBackend *backend, const QString &devNode, int fd, dev_t device
m_leaseDevice = new KWaylandServer::DrmLeaseDeviceV1Interface(waylandServer()->display(), [this] {
char *path = drmGetDeviceNameFromFd2(m_fd);
int fd = open(path, O_RDWR | O_CLOEXEC);
if (fd < 0) {
FileDescriptor fd{open(path, O_RDWR | O_CLOEXEC)};
if (!fd.isValid()) {
qCWarning(KWIN_DRM) << "Could not open DRM fd for leasing!" << strerror(errno);
} else {
if (drmIsMaster(fd)) {
if (drmDropMaster(fd) != 0) {
close(fd);
if (drmIsMaster(fd.get())) {
if (drmDropMaster(fd.get()) != 0) {
qCWarning(KWIN_DRM) << "Could not create a non-master DRM fd for leasing!" << strerror(errno);
return -1;
return FileDescriptor{};
}
}
}
@ -625,8 +624,8 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques
}
uint32_t lesseeId;
int fd = drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId);
if (fd < 0) {
FileDescriptor fd{drmModeCreateLease(m_fd, objects.constData(), objects.count(), 0, &lesseeId)};
if (!fd.isValid()) {
qCWarning(KWIN_DRM) << "Could not create DRM lease!" << strerror(errno);
qCWarning(KWIN_DRM, "Tried to lease the following %d resources:", objects.count());
for (const auto &res : qAsConst(objects)) {
@ -634,11 +633,11 @@ void DrmGpu::handleLeaseRequest(KWaylandServer::DrmLeaseV1Interface *leaseReques
}
leaseRequest->deny();
} else {
qCDebug(KWIN_DRM, "Created lease with leaseFd %d and lesseeId %d for %d resources:", fd, lesseeId, objects.count());
qCDebug(KWIN_DRM, "Created lease for %d resources:", objects.count());
for (const auto &res : qAsConst(objects)) {
qCDebug(KWIN_DRM) << res;
}
leaseRequest->grant(fd, lesseeId);
leaseRequest->grant(std::move(fd), lesseeId);
for (const auto &output : qAsConst(outputs)) {
output->leased(leaseRequest);
}

View file

@ -18,7 +18,7 @@ namespace KWaylandServer
static const quint32 s_version = 1;
DrmLeaseDeviceV1Interface::DrmLeaseDeviceV1Interface(Display *display, std::function<int()> createNonMasterFd)
DrmLeaseDeviceV1Interface::DrmLeaseDeviceV1Interface(Display *display, std::function<KWin::FileDescriptor()> createNonMasterFd)
: d(new DrmLeaseDeviceV1InterfacePrivate(display, this, createNonMasterFd))
{
}
@ -36,9 +36,8 @@ void DrmLeaseDeviceV1Interface::setDrmMaster(bool hasDrmMaster)
if (hasDrmMaster) {
// send pending drm fds
while (!d->pendingFds.isEmpty()) {
int fd = d->createNonMasterFd();
d->send_drm_fd(d->pendingFds.dequeue(), fd);
close(fd);
KWin::FileDescriptor fd = d->createNonMasterFd();
d->send_drm_fd(d->pendingFds.dequeue(), fd.get());
}
// offer all connectors again
for (const auto &connector : qAsConst(d->connectors)) {
@ -72,7 +71,7 @@ void DrmLeaseDeviceV1Interface::done()
}
}
DrmLeaseDeviceV1InterfacePrivate::DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<int()> createNonMasterFd)
DrmLeaseDeviceV1InterfacePrivate::DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<KWin::FileDescriptor()> createNonMasterFd)
: QtWaylandServer::wp_drm_lease_device_v1(*display, s_version)
, q(device)
, createNonMasterFd(createNonMasterFd)
@ -159,9 +158,8 @@ void DrmLeaseDeviceV1InterfacePrivate::wp_drm_lease_device_v1_bind_resource(Reso
pendingFds << resource->handle;
return;
}
int fd = createNonMasterFd();
send_drm_fd(resource->handle, fd);
close(fd);
KWin::FileDescriptor fd = createNonMasterFd();
send_drm_fd(resource->handle, fd.get());
for (const auto &connector : qAsConst(connectors)) {
auto connectorPrivate = DrmLeaseConnectorV1InterfacePrivate::get(connector);
if (!connectorPrivate->withdrawn) {
@ -326,10 +324,10 @@ DrmLeaseV1Interface::~DrmLeaseV1Interface()
d->device->leases.removeOne(this);
}
void DrmLeaseV1Interface::grant(int leaseFd, uint32_t lesseeId)
void DrmLeaseV1Interface::grant(KWin::FileDescriptor &&leaseFd, uint32_t lesseeId)
{
d->send_lease_fd(leaseFd);
close(leaseFd);
KWin::FileDescriptor tmp = std::move(leaseFd);
d->send_lease_fd(tmp.get());
d->lesseeId = lesseeId;
for (const auto &connector : qAsConst(d->connectors)) {
DrmLeaseConnectorV1InterfacePrivate::get(connector)->withdraw();

View file

@ -7,6 +7,7 @@
#pragma once
#include "kwin_export.h"
#include "utils/filedescriptor.h"
#include <QObject>
#include <QPointer>
@ -35,7 +36,7 @@ public:
* @param createNonMasterFd a function that creates non-master drm file descriptors for
* this device that clients can use to enumerate connectors and their properties
*/
explicit DrmLeaseDeviceV1Interface(Display *display, std::function<int()> createNonMasterFd);
explicit DrmLeaseDeviceV1Interface(Display *display, std::function<KWin::FileDescriptor()> createNonMasterFd);
~DrmLeaseDeviceV1Interface() override;
/**
@ -98,7 +99,7 @@ public:
* drive the outputs corresponding to the requested connectors.
* Must only be called once in response to DrmLeaseDeviceV1Interface::leaseRequested
*/
void grant(int leaseFd, uint32_t lesseeId);
void grant(KWin::FileDescriptor &&leaseFd, uint32_t lesseeId);
/**
* Deny the lease request. The compositor may call this in response to

View file

@ -6,6 +6,7 @@
#pragma once
#include "utils/filedescriptor.h"
#include <qwayland-server-drm-lease-v1.h>
#include <QObject>
@ -23,7 +24,7 @@ class DrmLeaseV1Interface;
class DrmLeaseDeviceV1InterfacePrivate : public QtWaylandServer::wp_drm_lease_device_v1
{
public:
DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<int()> createNonMasterFd);
DrmLeaseDeviceV1InterfacePrivate(Display *display, DrmLeaseDeviceV1Interface *device, std::function<KWin::FileDescriptor()> createNonMasterFd);
~DrmLeaseDeviceV1InterfacePrivate();
void remove();
@ -37,7 +38,7 @@ public:
QVector<DrmLeaseRequestV1Interface *> leaseRequests;
QVector<DrmLeaseV1Interface *> leases;
QQueue<wl_resource *> pendingFds;
std::function<int()> createNonMasterFd;
std::function<KWin::FileDescriptor()> createNonMasterFd;
bool hasDrmMaster = true;
bool removed = false;