Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/aquamarine/backend/Backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ namespace Aquamarine {
virtual bool createOutput(const std::string& name = "") = 0; // "" means auto
virtual Hyprutils::Memory::CSharedPointer<IAllocator> preferredAllocator() = 0;
virtual std::vector<SDRMFormat> getRenderableFormats(); // empty = use getRenderFormats
virtual std::vector<Hyprutils::Memory::CSharedPointer<IAllocator>> getAllocators() = 0;
virtual Hyprutils::Memory::CWeakPointer<IBackendImplementation> getPrimary() = 0;
virtual std::vector<Hyprutils::Memory::CSharedPointer<IAllocator>> getAllocators() = 0;
virtual Hyprutils::Memory::CWeakPointer<IBackendImplementation> getPrimary() = 0;
virtual int drmRenderNodeFD() = 0;
};

class CBackend {
Expand Down Expand Up @@ -148,6 +149,9 @@ namespace Aquamarine {
bool ready = false;
Hyprutils::Memory::CSharedPointer<CSession> session;

/* Get the primary DRM RenderNode */
int drmRenderNodeFD();

private:
CBackend();

Expand Down
1 change: 1 addition & 0 deletions include/aquamarine/backend/DRM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ namespace Aquamarine {

std::vector<FIdleCallback> idleCallbacks;
std::string gpuName;
virtual int drmRenderNodeFD();

private:
CDRMBackend(Hyprutils::Memory::CSharedPointer<CBackend> backend);
Expand Down
1 change: 1 addition & 0 deletions include/aquamarine/backend/Headless.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace Aquamarine {
virtual Hyprutils::Memory::CWeakPointer<IBackendImplementation> getPrimary();

Hyprutils::Memory::CWeakPointer<CHeadlessBackend> self;
virtual int drmRenderNodeFD();

private:
CHeadlessBackend(Hyprutils::Memory::CSharedPointer<CBackend> backend_);
Expand Down
3 changes: 3 additions & 0 deletions include/aquamarine/backend/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Aquamarine {
static Hyprutils::Memory::CSharedPointer<CSessionDevice> openIfKMS(Hyprutils::Memory::CSharedPointer<CSession> session_, const std::string& path_);

bool supportsKMS();
void resolveMatchingRenderNode(udev_device* cardDevice);

int fd = -1;
int deviceID = -1;
Expand All @@ -53,6 +54,8 @@ namespace Aquamarine {
Hyprutils::Signal::CSignalT<> remove;
} events;

int renderNodeFd = -1;

private:
Hyprutils::Memory::CWeakPointer<CSession> session;
};
Expand Down
1 change: 1 addition & 0 deletions include/aquamarine/backend/Wayland.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ namespace Aquamarine {
virtual Hyprutils::Memory::CWeakPointer<IBackendImplementation> getPrimary();

Hyprutils::Memory::CWeakPointer<CWaylandBackend> self;
virtual int drmRenderNodeFD();

private:
CWaylandBackend(Hyprutils::Memory::CSharedPointer<CBackend> backend);
Expand Down
11 changes: 11 additions & 0 deletions src/backend/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ int Aquamarine::CBackend::drmFD() {
return -1;
}

int Aquamarine::CBackend::drmRenderNodeFD() {
for (auto const& i : implementations) {
int fd = i->drmRenderNodeFD();
if (fd < 0)
continue;

return fd;
}
return -1;
}

bool Aquamarine::CBackend::hasSession() {
return session;
}
Expand Down
4 changes: 4 additions & 0 deletions src/backend/Headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ int Aquamarine::CHeadlessBackend::drmFD() {
return -1;
}

int Aquamarine::CHeadlessBackend::drmRenderNodeFD() {
return -1;
}

bool Aquamarine::CHeadlessBackend::dispatchEvents() {
return true;
}
Expand Down
82 changes: 82 additions & 0 deletions src/backend/Session.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <aquamarine/backend/Backend.hpp>
#include <fcntl.h>

extern "C" {
#include <libseat.h>
Expand Down Expand Up @@ -141,6 +142,9 @@ Aquamarine::CSessionDevice::~CSessionDevice() {
session->backend->log(AQ_LOG_ERROR, std::format("libseat: Couldn't close device at {}", path));
if (fd >= 0)
close(fd);

if (renderNodeFd)
close(renderNodeFd);
}

bool Aquamarine::CSessionDevice::supportsKMS() {
Expand All @@ -157,6 +161,84 @@ bool Aquamarine::CSessionDevice::supportsKMS() {
return kms;
}

void Aquamarine::CSessionDevice::resolveMatchingRenderNode(udev_device* cardDevice) {
if (!cardDevice)
return;

auto pciParent = udev_device_get_parent_with_subsystem_devtype(cardDevice, "pci", nullptr);
const auto* pciSyspath = pciParent ? udev_device_get_syspath(pciParent) : nullptr;

auto* enumerate = udev_enumerate_new(session->udevHandle);
if (!enumerate)
return;

udev_enumerate_add_match_subsystem(enumerate, "drm");
udev_enumerate_scan_devices(enumerate);

auto* devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry* entry = nullptr;

bool matched = false;

udev_list_entry_foreach(entry, devices) {
const auto* path = udev_list_entry_get_name(entry);
auto dev = udev_device_new_from_syspath(session->udevHandle, path);
if (!dev)
continue;

const auto* devnode = udev_device_get_devnode(dev);
const auto* devtype = udev_device_get_devtype(dev);

if (!devnode || !devtype || strcmp(devtype, "drm_minor") != 0 || !strstr(devnode, "renderD")) {
udev_device_unref(dev);
continue;
}

auto devParent = udev_device_get_parent_with_subsystem_devtype(dev, "pci", nullptr);
if (devParent && pciSyspath && strcmp(udev_device_get_syspath(devParent), pciSyspath) == 0) {
renderNodeFd = open(devnode, O_RDWR | O_CLOEXEC);
if (renderNodeFd < 0)
session->backend->log(AQ_LOG_WARNING, std::format("drm: Failed to open matching render node {}", devnode));
else
matched = true;

udev_device_unref(dev);
break;
}

udev_device_unref(dev);
}

if (!matched) {
// fallback to the first render node
udev_list_entry_foreach(entry, devices) {
const auto* path = udev_list_entry_get_name(entry);
auto dev = udev_device_new_from_syspath(session->udevHandle, path);
if (!dev)
continue;

const auto* devnode = udev_device_get_devnode(dev);
const auto* devtype = udev_device_get_devtype(dev);

if (!devnode || !devtype || strcmp(devtype, "drm_minor") != 0 || !strstr(devnode, "renderD")) {
udev_device_unref(dev);
continue;
}

renderNodeFd = open(devnode, O_RDWR | O_CLOEXEC);
if (renderNodeFd >= 0) {
session->backend->log(AQ_LOG_WARNING, std::format("drm: No matching render node for {}, falling back to {}", path, devnode));
udev_device_unref(dev);
break;
}

udev_device_unref(dev);
}
}

udev_enumerate_unref(enumerate);
}

SP<CSessionDevice> Aquamarine::CSessionDevice::openIfKMS(SP<CSession> session_, const std::string& path_) {
auto dev = makeShared<CSessionDevice>(session_, path_);
if (!dev->supportsKMS())
Expand Down
5 changes: 5 additions & 0 deletions src/backend/Wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ int Aquamarine::CWaylandBackend::drmFD() {
return drmState.fd;
}

int Aquamarine::CWaylandBackend::drmRenderNodeFD() {
// creation already attempts to use the rendernode, so just return same fd as drmFD().
return drmState.fd;
}

bool Aquamarine::CWaylandBackend::createOutput(const std::string& szName) {
auto o = outputs.emplace_back(SP<CWaylandOutput>(new CWaylandOutput(szName.empty() ? std::format("WAYLAND-{}", ++lastOutputID) : szName, self)));
o->self = o;
Expand Down
6 changes: 6 additions & 0 deletions src/backend/drm/DRM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ static std::vector<SP<CSessionDevice>> scanGPUs(SP<CBackend> backend) {
continue;
}

sessionDevice->resolveMatchingRenderNode(device);

udev_device_unref(device);

if (isBootVGA)
Expand Down Expand Up @@ -906,6 +908,10 @@ int Aquamarine::CDRMBackend::drmFD() {
return gpu->fd;
}

int Aquamarine::CDRMBackend::drmRenderNodeFD() {
return gpu->renderNodeFd;
}

static void handlePF(int fd, unsigned seq, unsigned tv_sec, unsigned tv_usec, unsigned crtc_id, void* data) {
auto pageFlip = (SDRMPageFlip*)data;

Expand Down