This commit is contained in:
Vaxry 2024-07-05 19:21:48 +02:00
parent 9d534ed158
commit 619ecb59af
6 changed files with 449 additions and 4 deletions

View file

@ -315,6 +315,7 @@ protocolNew("unstable/primary-selection" "primary-selection-unstable-v1" false)
protocolNew("staging/xwayland-shell" "xwayland-shell-v1" false) protocolNew("staging/xwayland-shell" "xwayland-shell-v1" false)
protocolNew("stable/viewporter" "viewporter" false) protocolNew("stable/viewporter" "viewporter" false)
protocolNew("stable/linux-dmabuf" "linux-dmabuf-v1" false) protocolNew("stable/linux-dmabuf" "linux-dmabuf-v1" false)
protocolNew("staging/drm-lease" "drm-lease-v1" false)
protocolWayland() protocolWayland()

View file

@ -6,6 +6,7 @@
#include "../devices/ITouch.hpp" #include "../devices/ITouch.hpp"
#include "../protocols/LayerShell.hpp" #include "../protocols/LayerShell.hpp"
#include "../protocols/PresentationTime.hpp" #include "../protocols/PresentationTime.hpp"
#include "../protocols/DRMLease.hpp"
#include "../protocols/core/Output.hpp" #include "../protocols/core/Output.hpp"
#include "../managers/PointerManager.hpp" #include "../managers/PointerManager.hpp"
#include <hyprutils/string/String.hpp> #include <hyprutils/string/String.hpp>
@ -92,10 +93,9 @@ void CMonitor::onConnect(bool noRule) {
if (output->nonDesktop) { if (output->nonDesktop) {
Debug::log(LOG, "Not configuring non-desktop output"); Debug::log(LOG, "Not configuring non-desktop output");
// TODO: if (PROTO::lease)
// if (g_pCompositor->m_sWRLDRMLeaseMgr) { PROTO::lease->offer(self.lock());
// wlr_drm_lease_v1_manager_offer_output(g_pCompositor->m_sWRLDRMLeaseMgr, output);
// }
return; return;
} }

View file

@ -113,6 +113,8 @@ class CMonitor {
bool RATScheduled = false; bool RATScheduled = false;
CTimer lastPresentationTimer; CTimer lastPresentationTimer;
bool isBeingLeased = false;
SMonitorRule activeMonitorRule; SMonitorRule activeMonitorRule;
WP<CMonitor> self; WP<CMonitor> self;

View file

@ -35,6 +35,7 @@
#include "../protocols/Viewporter.hpp" #include "../protocols/Viewporter.hpp"
#include "../protocols/MesaDRM.hpp" #include "../protocols/MesaDRM.hpp"
#include "../protocols/LinuxDMABUF.hpp" #include "../protocols/LinuxDMABUF.hpp"
#include "../protocols/DRMLease.hpp"
#include "../protocols/core/Seat.hpp" #include "../protocols/core/Seat.hpp"
#include "../protocols/core/DataDevice.hpp" #include "../protocols/core/DataDevice.hpp"
@ -130,6 +131,7 @@ CProtocolManager::CProtocolManager() {
PROTO::dataWlr = std::make_unique<CDataDeviceWLRProtocol>(&zwlr_data_control_manager_v1_interface, 2, "DataDeviceWlr"); PROTO::dataWlr = std::make_unique<CDataDeviceWLRProtocol>(&zwlr_data_control_manager_v1_interface, 2, "DataDeviceWlr");
PROTO::primarySelection = std::make_unique<CPrimarySelectionProtocol>(&zwp_primary_selection_device_manager_v1_interface, 1, "PrimarySelection"); PROTO::primarySelection = std::make_unique<CPrimarySelectionProtocol>(&zwp_primary_selection_device_manager_v1_interface, 1, "PrimarySelection");
PROTO::xwaylandShell = std::make_unique<CXWaylandShellProtocol>(&xwayland_shell_v1_interface, 1, "XWaylandShell"); PROTO::xwaylandShell = std::make_unique<CXWaylandShellProtocol>(&xwayland_shell_v1_interface, 1, "XWaylandShell");
PROTO::lease = std::make_unique<CDRMLeaseProtocol>(&wp_drm_lease_device_v1_interface, 1, "DRMLease");
if (g_pHyprOpenGL->getDRMFormats().size() > 0) { if (g_pHyprOpenGL->getDRMFormats().size() > 0) {
PROTO::mesaDRM = std::make_unique<CMesaDRMProtocol>(&wl_drm_interface, 2, "MesaDRM"); PROTO::mesaDRM = std::make_unique<CMesaDRMProtocol>(&wl_drm_interface, 2, "MesaDRM");

303
src/protocols/DRMLease.cpp Normal file
View file

@ -0,0 +1,303 @@
#include "DRMLease.hpp"
#include "../Compositor.hpp"
#include <aquamarine/backend/DRM.hpp>
#include <fcntl.h>
#define LOGM PROTO::lease->protoLog
CDRMLeaseResource::CDRMLeaseResource(SP<CWpDrmLeaseV1> resource_, SP<CDRMLeaseRequestResource> request) : resource(resource_) {
if (!good())
return;
resource->setOnDestroy([this](CWpDrmLeaseV1* r) { PROTO::lease->destroyResource(this); });
resource->setDestroy([this](CWpDrmLeaseV1* r) { PROTO::lease->destroyResource(this); });
parent = request->parent;
requested = request->requested;
for (auto& m : requested) {
if (!m->monitor || m->monitor->isBeingLeased) {
LOGM(ERR, "Rejecting lease: no monitor or monitor is being leased for {}", (m->monitor ? m->monitor->szName : "null"));
resource->sendFinished();
return;
}
}
// grant the lease if it is seemingly valid
LOGM(LOG, "Leasing outputs: {}", [this]() {
std::string roll;
for (auto& o : requested) {
roll += std::format("{} ", o->monitor->szName);
}
return roll;
}());
std::vector<SP<Aquamarine::IOutput>> outputs;
for (auto& m : requested) {
outputs.emplace_back(m->monitor->output);
}
auto aqlease = Aquamarine::CDRMLease::create(outputs);
if (!aqlease) {
LOGM(ERR, "Rejecting lease: backend failed to alloc a lease");
resource->sendFinished();
return;
}
LOGM(LOG, "Granting lease, sending fd {}", aqlease->leaseFD);
resource->sendLeaseFd(aqlease->leaseFD);
lease = aqlease;
for (auto& m : requested) {
m->monitor->isBeingLeased = true;
}
listeners.destroyLease = lease->events.destroy.registerListener([this](std::any d) {
for (auto& m : requested) {
if (m && m->monitor)
m->monitor->isBeingLeased = false;
}
resource->sendFinished();
});
close(lease->leaseFD);
}
bool CDRMLeaseResource::good() {
return resource->resource();
}
CDRMLeaseRequestResource::CDRMLeaseRequestResource(SP<CWpDrmLeaseRequestV1> resource_) : resource(resource_) {
if (!good())
return;
resource->setOnDestroy([this](CWpDrmLeaseRequestV1* r) { PROTO::lease->destroyResource(this); });
resource->setRequestConnector([this](CWpDrmLeaseRequestV1* r, wl_resource* conn) {
if (!conn) {
resource->error(-1, "Null connector");
return;
}
auto CONNECTOR = CDRMLeaseConnectorResource::fromResource(conn);
if (std::find(requested.begin(), requested.end(), CONNECTOR) != requested.end()) {
resource->error(WP_DRM_LEASE_REQUEST_V1_ERROR_DUPLICATE_CONNECTOR, "Connector already requested");
return;
}
// TODO: when (if) we add multi, make sure this is from the correct device.
requested.emplace_back(CONNECTOR);
});
resource->setSubmit([this](CWpDrmLeaseRequestV1* r, uint32_t id) {
if (requested.empty()) {
resource->error(WP_DRM_LEASE_REQUEST_V1_ERROR_EMPTY_LEASE, "No connectors added");
return;
}
auto RESOURCE = makeShared<CDRMLeaseResource>(makeShared<CWpDrmLeaseV1>(resource->client(), resource->version(), -1), self.lock());
if (!RESOURCE) {
resource->noMemory();
return;
}
PROTO::lease->m_vLeases.emplace_back(RESOURCE);
// per protcol, after submit, this is dead.
PROTO::lease->destroyResource(this);
});
}
bool CDRMLeaseRequestResource::good() {
return resource->resource();
}
SP<CDRMLeaseConnectorResource> CDRMLeaseConnectorResource::fromResource(wl_resource* res) {
auto data = (CDRMLeaseConnectorResource*)(((CWpDrmLeaseConnectorV1*)wl_resource_get_user_data(res))->data());
return data ? data->self.lock() : nullptr;
}
CDRMLeaseConnectorResource::CDRMLeaseConnectorResource(SP<CWpDrmLeaseConnectorV1> resource_, SP<CMonitor> monitor_) : monitor(monitor_), resource(resource_) {
if (!good())
return;
resource->setOnDestroy([this](CWpDrmLeaseConnectorV1* r) { PROTO::lease->destroyResource(this); });
resource->setDestroy([this](CWpDrmLeaseConnectorV1* r) { PROTO::lease->destroyResource(this); });
resource->setData(this);
listeners.destroyMonitor = monitor->events.destroy.registerListener([this](std::any d) {
resource->sendWithdrawn();
dead = true;
});
}
bool CDRMLeaseConnectorResource::good() {
return resource->resource();
}
void CDRMLeaseConnectorResource::sendData() {
resource->sendName(monitor->szName.c_str());
resource->sendDescription(monitor->szDescription.c_str());
auto AQDRMOutput = (Aquamarine::CDRMOutput*)monitor->output.get();
resource->sendConnectorId(AQDRMOutput->getConnectorID());
resource->sendDone();
}
CDRMLeaseDeviceResource::CDRMLeaseDeviceResource(SP<CWpDrmLeaseDeviceV1> resource_) : resource(resource_) {
if (!good())
return;
resource->setOnDestroy([this](CWpDrmLeaseDeviceV1* r) { PROTO::lease->destroyResource(this); });
resource->setRelease([this](CWpDrmLeaseDeviceV1* r) { PROTO::lease->destroyResource(this); });
resource->setCreateLeaseRequest([this](CWpDrmLeaseDeviceV1* r, uint32_t id) {
auto RESOURCE = makeShared<CDRMLeaseRequestResource>(makeShared<CWpDrmLeaseRequestV1>(resource->client(), resource->version(), id));
if (!RESOURCE) {
resource->noMemory();
return;
}
RESOURCE->self = RESOURCE;
PROTO::lease->m_vRequests.emplace_back(RESOURCE);
LOGM(LOG, "New lease request {}", id);
RESOURCE->parent = self;
});
int fd = ((Aquamarine::CDRMBackend*)PROTO::lease->primaryDevice->backend.get())->getNonMasterFD();
if (fd < 0) {
LOGM(ERR, "Failed to dup fd in lease");
return;
}
LOGM(LOG, "Sending DRMFD {} to new lease device", fd);
resource->sendDrmFd(fd);
close(fd);
for (auto& m : PROTO::lease->primaryDevice->offeredOutputs) {
sendConnector(m.lock());
}
resource->sendDone();
}
bool CDRMLeaseDeviceResource::good() {
return resource->resource();
}
void CDRMLeaseDeviceResource::sendConnector(SP<CMonitor> monitor) {
if (std::find_if(connectorsSent.begin(), connectorsSent.end(), [monitor](const auto& e) { return e->monitor == monitor; }) != connectorsSent.end())
return;
auto RESOURCE = makeShared<CDRMLeaseConnectorResource>(makeShared<CWpDrmLeaseConnectorV1>(resource->client(), resource->version(), -1), monitor);
if (!RESOURCE) {
resource->noMemory();
return;
}
RESOURCE->parent = self;
RESOURCE->self = RESOURCE;
LOGM(LOG, "Sending new connector {}", monitor->szName);
connectorsSent.emplace_back(RESOURCE);
PROTO::lease->m_vConnectors.emplace_back(RESOURCE);
resource->sendConnector(RESOURCE->resource.get());
RESOURCE->sendData();
}
CDRMLeaseDevice::CDRMLeaseDevice(SP<Aquamarine::CDRMBackend> drmBackend) : backend(drmBackend) {
auto drm = (Aquamarine::CDRMBackend*)drmBackend.get();
auto fd = drm->getNonMasterFD();
if (fd < 0) {
LOGM(ERR, "Failed to dup fd for drm node {}", drm->gpuName);
return;
}
close(fd);
success = true;
name = drm->gpuName;
}
CDRMLeaseProtocol::CDRMLeaseProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
for (auto& b : g_pCompositor->m_pAqBackend->getImplementations()) {
if (b->type() != Aquamarine::AQ_BACKEND_DRM)
continue;
auto drm = ((Aquamarine::CDRMBackend*)b.get())->self.lock();
primaryDevice = makeShared<CDRMLeaseDevice>(drm);
if (primaryDevice->success)
break;
}
if (!primaryDevice || primaryDevice->success) {
LOGM(ERR, "No DRM backend, not creating lease");
PROTO::lease.reset();
return;
}
}
void CDRMLeaseProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(makeShared<CDRMLeaseDeviceResource>(makeShared<CWpDrmLeaseDeviceV1>(client, ver, id)));
if (!RESOURCE->good()) {
wl_client_post_no_memory(client);
m_vManagers.pop_back();
return;
}
RESOURCE->self = RESOURCE;
}
void CDRMLeaseProtocol::destroyResource(CDRMLeaseDeviceResource* resource) {
std::erase_if(m_vManagers, [resource](const auto& e) { return e.get() == resource; });
}
void CDRMLeaseProtocol::destroyResource(CDRMLeaseConnectorResource* resource) {
std::erase_if(m_vConnectors, [resource](const auto& e) { return e.get() == resource; });
}
void CDRMLeaseProtocol::destroyResource(CDRMLeaseRequestResource* resource) {
std::erase_if(m_vRequests, [resource](const auto& e) { return e.get() == resource; });
}
void CDRMLeaseProtocol::destroyResource(CDRMLeaseResource* resource) {
std::erase_if(m_vLeases, [resource](const auto& e) { return e.get() == resource; });
}
void CDRMLeaseProtocol::offer(SP<CMonitor> monitor) {
if (std::find(primaryDevice->offeredOutputs.begin(), primaryDevice->offeredOutputs.end(), monitor) != primaryDevice->offeredOutputs.end())
return;
if (monitor->output->getBackend()->type() != Aquamarine::AQ_BACKEND_DRM)
return;
if (monitor->output->getBackend() != primaryDevice->backend) {
LOGM(ERR, "Monitor {} cannot be leased: primaryDevice lease is for a different device", monitor->szName);
return;
}
primaryDevice->offeredOutputs.emplace_back(monitor);
for (auto& m : m_vManagers) {
m->sendConnector(monitor);
m->resource->sendDone();
}
}

137
src/protocols/DRMLease.hpp Normal file
View file

@ -0,0 +1,137 @@
#pragma once
#include <memory>
#include <vector>
#include <unordered_map>
#include "WaylandProtocol.hpp"
#include "drm-lease-v1.hpp"
#include "../helpers/signal/Signal.hpp"
/*
TODO: this protocol is not made for systems with multiple DRM nodes (e.g. multigpu)
*/
AQUAMARINE_FORWARD(CDRMBackend);
AQUAMARINE_FORWARD(CDRMLease);
class CDRMLeaseDeviceResource;
class CMonitor;
class CDRMLeaseProtocol;
class CDRMLeaseConnectorResource;
class CDRMLeaseRequestResource;
class CDRMLeaseResource {
public:
CDRMLeaseResource(SP<CWpDrmLeaseV1> resource_, SP<CDRMLeaseRequestResource> request);
bool good();
WP<CDRMLeaseDeviceResource> parent;
std::vector<WP<CDRMLeaseConnectorResource>> requested;
WP<Aquamarine::CDRMLease> lease;
int leaseFD = -1;
struct {
CHyprSignalListener destroyLease;
} listeners;
private:
SP<CWpDrmLeaseV1> resource;
};
class CDRMLeaseRequestResource {
public:
CDRMLeaseRequestResource(SP<CWpDrmLeaseRequestV1> resource_);
bool good();
WP<CDRMLeaseDeviceResource> parent;
WP<CDRMLeaseRequestResource> self;
std::vector<WP<CDRMLeaseConnectorResource>> requested;
private:
SP<CWpDrmLeaseRequestV1> resource;
};
class CDRMLeaseConnectorResource {
public:
CDRMLeaseConnectorResource(SP<CWpDrmLeaseConnectorV1> resource_, SP<CMonitor> monitor_);
static SP<CDRMLeaseConnectorResource> fromResource(wl_resource*);
bool good();
void sendData();
WP<CDRMLeaseConnectorResource> self;
WP<CDRMLeaseDeviceResource> parent;
WP<CMonitor> monitor;
bool dead = false;
private:
SP<CWpDrmLeaseConnectorV1> resource;
struct {
CHyprSignalListener destroyMonitor;
} listeners;
friend class CDRMLeaseDeviceResource;
};
class CDRMLeaseDeviceResource {
public:
CDRMLeaseDeviceResource(SP<CWpDrmLeaseDeviceV1> resource_);
bool good();
void sendConnector(SP<CMonitor> monitor);
std::vector<WP<CDRMLeaseConnectorResource>> connectorsSent;
WP<CDRMLeaseDeviceResource> self;
private:
SP<CWpDrmLeaseDeviceV1> resource;
friend class CDRMLeaseProtocol;
};
class CDRMLeaseDevice {
public:
CDRMLeaseDevice(SP<Aquamarine::CDRMBackend> drmBackend);
std::string name = "";
bool success = false;
SP<Aquamarine::CDRMBackend> backend;
std::vector<WP<CMonitor>> offeredOutputs;
};
class CDRMLeaseProtocol : public IWaylandProtocol {
public:
CDRMLeaseProtocol(const wl_interface* iface, const int& ver, const std::string& name);
virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id);
void offer(SP<CMonitor> monitor);
private:
void destroyResource(CDRMLeaseDeviceResource* resource);
void destroyResource(CDRMLeaseConnectorResource* resource);
void destroyResource(CDRMLeaseRequestResource* resource);
void destroyResource(CDRMLeaseResource* resource);
//
std::vector<SP<CDRMLeaseDeviceResource>> m_vManagers;
std::vector<SP<CDRMLeaseConnectorResource>> m_vConnectors;
std::vector<SP<CDRMLeaseRequestResource>> m_vRequests;
std::vector<SP<CDRMLeaseResource>> m_vLeases;
SP<CDRMLeaseDevice> primaryDevice;
friend class CDRMLeaseDeviceResource;
friend class CDRMLeaseConnectorResource;
friend class CDRMLeaseRequestResource;
friend class CDRMLeaseResource;
};
namespace PROTO {
inline UP<CDRMLeaseProtocol> lease;
};