Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make use of CFileDescriptors #142

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion include/aquamarine/allocator/DRMDumb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Aquamarine {
virtual void update(const Hyprutils::Math::CRegion& damage);
virtual bool isSynchronous();
virtual bool good();
virtual SDMABUFAttrs dmabuf();
virtual const SDMABUFAttrs& dmabuf() const;
virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags);
virtual void endDataPtr();

Expand Down
9 changes: 5 additions & 4 deletions include/aquamarine/allocator/GBM.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <hyprutils/os/FileDescriptor.hpp>
#include "Allocator.hpp"

struct gbm_device;
Expand All @@ -19,7 +20,7 @@ namespace Aquamarine {
virtual void update(const Hyprutils::Math::CRegion& damage);
virtual bool isSynchronous();
virtual bool good();
virtual SDMABUFAttrs dmabuf();
virtual const SDMABUFAttrs& dmabuf() const;
virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags);
virtual void endDataPtr();

Expand All @@ -40,7 +41,7 @@ namespace Aquamarine {
class CGBMAllocator : public IAllocator {
public:
~CGBMAllocator();
static Hyprutils::Memory::CSharedPointer<CGBMAllocator> create(int drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_);
static Hyprutils::Memory::CSharedPointer<CGBMAllocator> create(Hyprutils::OS::CFileDescriptor&& drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_);

virtual Hyprutils::Memory::CSharedPointer<IBuffer> acquire(const SAllocatorBufferParams& params, Hyprutils::Memory::CSharedPointer<CSwapchain> swapchain_);
virtual Hyprutils::Memory::CSharedPointer<CBackend> getBackend();
Expand All @@ -52,12 +53,12 @@ namespace Aquamarine {
Hyprutils::Memory::CWeakPointer<CGBMAllocator> self;

private:
CGBMAllocator(int fd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_);
CGBMAllocator(Hyprutils::OS::CFileDescriptor&& fd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_);

// a vector purely for tracking (debugging) the buffers and nothing more
std::vector<Hyprutils::Memory::CWeakPointer<CGBMBuffer>> buffers;

int fd = -1;
Hyprutils::OS::CFileDescriptor fd;
Hyprutils::Memory::CWeakPointer<CBackend> backend;

// gbm stuff
Expand Down
3 changes: 2 additions & 1 deletion include/aquamarine/backend/Backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <hyprutils/memory/SharedPtr.hpp>
#include <hyprutils/signal/Signal.hpp>
#include <hyprutils/os/FileDescriptor.hpp>
#include <vector>
#include <functional>
#include <mutex>
Expand Down Expand Up @@ -115,7 +116,7 @@ namespace Aquamarine {
void removeIdleEvent(Hyprutils::Memory::CSharedPointer<std::function<void(void)>> pfn);

// utils
int reopenDRMNode(int drmFD, bool allowRenderNode = true);
Hyprutils::OS::CFileDescriptor reopenDRMNode(int drmFD, bool allowRenderNode = true);

// called when a new DRM card is hotplugged
void onNewGpu(std::string path);
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 @@ -5,6 +5,7 @@
#include "../output/Output.hpp"
#include "../input/Input.hpp"
#include <hyprutils/memory/WeakPtr.hpp>
#include <hyprutils/os/FileDescriptor.hpp>
#include <wayland-client.h>
#include <xf86drmMode.h>
#include <optional>
Expand Down
22 changes: 12 additions & 10 deletions include/aquamarine/buffer/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <tuple>
#include <hyprutils/signal/Signal.hpp>
#include <hyprutils/math/Region.hpp>
#include <hyprutils/os/FileDescriptor.hpp>
#include "../misc/Attachment.hpp"

namespace Aquamarine {
Expand All @@ -19,15 +20,15 @@ namespace Aquamarine {
};

struct SDMABUFAttrs {
bool success = false;
Hyprutils::Math::Vector2D size;
uint32_t format = 0; // fourcc
uint64_t modifier = 0;
bool success = false;
Hyprutils::Math::Vector2D size;
uint32_t format = 0; // fourcc
uint64_t modifier = 0;

int planes = 1;
std::array<uint32_t, 4> offsets = {0};
std::array<uint32_t, 4> strides = {0};
std::array<int, 4> fds = {-1, -1, -1, -1};
int planes = 1;
std::array<uint32_t, 4> offsets = {0};
std::array<uint32_t, 4> strides = {0};
std::array<Hyprutils::OS::CFileDescriptor, 4> fds;
};

struct SSHMAttrs {
Expand All @@ -50,7 +51,7 @@ namespace Aquamarine {
virtual void update(const Hyprutils::Math::CRegion& damage) = 0;
virtual bool isSynchronous() = 0; // whether the updates to this buffer are synchronous, aka happen over cpu
virtual bool good() = 0;
virtual SDMABUFAttrs dmabuf();
virtual const SDMABUFAttrs& dmabuf() const;
virtual SSHMAttrs shm();
virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags);
virtual void endDataPtr();
Expand All @@ -71,7 +72,8 @@ namespace Aquamarine {
} events;

private:
int locks = 0;
SDMABUFAttrs m_attrs{};
int locks = 0;
};

};
5 changes: 3 additions & 2 deletions include/aquamarine/output/Output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <hyprutils/memory/SharedPtr.hpp>
#include <hyprutils/math/Region.hpp>
#include <hyprutils/math/Mat3x3.hpp>
#include <hyprutils/os/FileDescriptor.hpp>
#include <drm_fourcc.h>
#include <xf86drmMode.h>
#include "../allocator/Swapchain.hpp"
Expand Down Expand Up @@ -74,7 +75,7 @@ namespace Aquamarine {
Hyprutils::Memory::CSharedPointer<SOutputMode> customMode;
uint32_t drmFormat = DRM_FORMAT_INVALID;
Hyprutils::Memory::CSharedPointer<IBuffer> buffer;
int32_t explicitInFence = -1, explicitOutFence = -1;
Hyprutils::OS::CFileDescriptor explicitInFence, explicitOutFence;
Hyprutils::Math::Mat3x3 ctm;
bool wideColorGamut = false;
hdr_output_metadata hdrMetadata;
Expand All @@ -94,7 +95,7 @@ namespace Aquamarine {
void setCustomMode(Hyprutils::Memory::CSharedPointer<SOutputMode> mode);
void setFormat(uint32_t drmFormat);
void setBuffer(Hyprutils::Memory::CSharedPointer<IBuffer> buffer);
void setExplicitInFence(int32_t fenceFD); // -1 removes
void setExplicitInFence(Hyprutils::OS::CFileDescriptor&& fenceFD);
void enableExplicitOutFenceForNextCommit();
void resetExplicitFences();
void setCTM(const Hyprutils::Math::Mat3x3& ctm);
Expand Down
5 changes: 3 additions & 2 deletions src/allocator/DRMDumb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using namespace Aquamarine;
using namespace Hyprutils::Memory;
using namespace Hyprutils::OS;
#define SP CSharedPointer
#define WP CWeakPointer

Expand Down Expand Up @@ -53,7 +54,7 @@ Aquamarine::CDRMDumbBuffer::CDRMDumbBuffer(const SAllocatorBufferParams& params,
return;
}

attrs.fds.at(0) = primeFD;
attrs.fds.at(0) = CFileDescriptor{primeFD};

attrs.success = true;

Expand Down Expand Up @@ -94,7 +95,7 @@ bool Aquamarine::CDRMDumbBuffer::good() {
return attrs.success && data;
}

SDMABUFAttrs Aquamarine::CDRMDumbBuffer::dmabuf() {
const SDMABUFAttrs& Aquamarine::CDRMDumbBuffer::dmabuf() const {
return attrs;
}

Expand Down
41 changes: 17 additions & 24 deletions src/allocator/GBM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using namespace Aquamarine;
using namespace Hyprutils::Memory;
using namespace Hyprutils::OS;
#define SP CSharedPointer

static SDRMFormat guessFormatFrom(std::vector<SDRMFormat> formats, bool cursor, bool scanout) {
Expand Down Expand Up @@ -195,13 +196,13 @@ Aquamarine::CGBMBuffer::CGBMBuffer(const SAllocatorBufferParams& params, Hypruti
for (size_t i = 0; i < (size_t)attrs.planes; ++i) {
attrs.strides.at(i) = gbm_bo_get_stride_for_plane(bo, i);
attrs.offsets.at(i) = gbm_bo_get_offset(bo, i);
attrs.fds.at(i) = gbm_bo_get_fd_for_plane(bo, i);
attrs.fds.at(i) = CFileDescriptor{gbm_bo_get_fd_for_plane(bo, i)};

if (attrs.fds.at(i) < 0) {
if (!attrs.fds.at(i).isValid()) {
allocator->backend->log(AQ_LOG_ERROR, std::format("GBM: Failed to query fd for plane {}", i));
for (size_t j = 0; j < i; ++j) {
close(attrs.fds.at(j));
}
for (size_t j = 0; j < i; ++j)
attrs.fds.at(j).reset();

attrs.planes = 0;
return;
}
Expand All @@ -226,10 +227,6 @@ Aquamarine::CGBMBuffer::CGBMBuffer(const SAllocatorBufferParams& params, Hypruti
}

Aquamarine::CGBMBuffer::~CGBMBuffer() {
for (size_t i = 0; i < (size_t)attrs.planes; i++) {
close(attrs.fds.at(i));
}

events.destroy.emit();
if (bo) {
if (gboMapping)
Expand Down Expand Up @@ -258,7 +255,7 @@ bool Aquamarine::CGBMBuffer::good() {
return bo;
}

SDMABUFAttrs Aquamarine::CGBMBuffer::dmabuf() {
const SDMABUFAttrs& Aquamarine::CGBMBuffer::dmabuf() const {
return attrs;
}

Expand Down Expand Up @@ -290,44 +287,40 @@ CGBMAllocator::~CGBMAllocator() {
if (!gbmDevice)
return;

int fd = gbm_device_get_fd(gbmDevice);
CFileDescriptor fd{gbm_device_get_fd(gbmDevice)};
gbm_device_destroy(gbmDevice);

if (fd < 0)
return;

close(fd);
}

SP<CGBMAllocator> Aquamarine::CGBMAllocator::create(int drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_) {
SP<CGBMAllocator> Aquamarine::CGBMAllocator::create(CFileDescriptor&& drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_) {
uint64_t capabilities = 0;
if (drmGetCap(drmfd_, DRM_CAP_PRIME, &capabilities) || !(capabilities & DRM_PRIME_CAP_EXPORT)) {
if (drmGetCap(drmfd_.get(), DRM_CAP_PRIME, &capabilities) || !(capabilities & DRM_PRIME_CAP_EXPORT)) {
backend_->log(AQ_LOG_ERROR, "Cannot create a GBM Allocator: PRIME export is not supported by the gpu.");
return nullptr;
}

auto allocator = SP<CGBMAllocator>(new CGBMAllocator(drmfd_, backend_));
auto allocator = SP<CGBMAllocator>(new CGBMAllocator(std::move(drmfd_), backend_));

if (!allocator->gbmDevice) {
backend_->log(AQ_LOG_ERROR, "Cannot create a GBM Allocator: gbm failed to create a device.");
return nullptr;
}

backend_->log(AQ_LOG_DEBUG, std::format("Created a GBM allocator with drm fd {}", drmfd_));
backend_->log(AQ_LOG_DEBUG, std::format("Created a GBM allocator with drm fd {}", allocator->fd.get()));

allocator->self = allocator;

return allocator;
}

Aquamarine::CGBMAllocator::CGBMAllocator(int fd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_) : fd(fd_), backend(backend_), gbmDevice(gbm_create_device(fd_)) {
Aquamarine::CGBMAllocator::CGBMAllocator(CFileDescriptor&& fd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_) :
fd(std::move(fd_)), backend(backend_), gbmDevice(gbm_create_device(fd.get())) {
if (!gbmDevice) {
backend->log(AQ_LOG_ERROR, std::format("Couldn't open a GBM device at fd {}", fd_));
backend->log(AQ_LOG_ERROR, std::format("Couldn't open a GBM device at fd {}", fd.get()));
return;
}

gbmDeviceBackendName = gbm_device_get_backend_name(gbmDevice);
auto drmName_ = drmGetDeviceNameFromFd2(fd_);
auto drmName_ = drmGetDeviceNameFromFd2(fd.get());
drmName = drmName_;
free(drmName_);
}
Expand Down Expand Up @@ -355,7 +348,7 @@ Hyprutils::Memory::CSharedPointer<CBackend> Aquamarine::CGBMAllocator::getBacken
}

int Aquamarine::CGBMAllocator::drmFD() {
return fd;
return fd.get();
}

eAllocatorType Aquamarine::CGBMAllocator::type() {
Expand Down
35 changes: 17 additions & 18 deletions src/backend/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <unistd.h>

using namespace Hyprutils::Memory;
using namespace Hyprutils::OS;
using namespace Aquamarine;
#define SP CSharedPointer

Expand Down Expand Up @@ -145,12 +146,12 @@ bool Aquamarine::CBackend::start() {
for (auto const& b : implementations) {
if (b->drmFD() >= 0) {
auto fd = reopenDRMNode(b->drmFD());
if (fd < 0) {
if (!fd.isValid()) {
// this is critical, we cannot create an allocator properly
log(AQ_LOG_CRITICAL, "Failed to create an allocator (reopenDRMNode failed)");
return false;
}
primaryAllocator = CGBMAllocator::create(fd, self);
primaryAllocator = CGBMAllocator::create(std::move(fd), self);
break;
}
}
Expand Down Expand Up @@ -291,17 +292,17 @@ void Aquamarine::CBackend::onNewGpu(std::string path) {

// Yoinked from wlroots, render/allocator/allocator.c
// Ref-counting reasons, see https://gitlab.freedesktop.org/mesa/drm/-/merge_requests/110
int Aquamarine::CBackend::reopenDRMNode(int drmFD, bool allowRenderNode) {
CFileDescriptor Aquamarine::CBackend::reopenDRMNode(int drmFD, bool allowRenderNode) {

if (drmIsMaster(drmFD)) {
// Only recent kernels support empty leases
uint32_t lesseeID = 0;
int leaseFD = drmModeCreateLease(drmFD, nullptr, 0, O_CLOEXEC, &lesseeID);
if (leaseFD >= 0) {
uint32_t lesseeID = 0;
CFileDescriptor leaseFD{drmModeCreateLease(drmFD, nullptr, 0, O_CLOEXEC, &lesseeID)};
if (leaseFD.isValid()) {
return leaseFD;
} else if (leaseFD != -EINVAL && leaseFD != -EOPNOTSUPP) {
} else if (leaseFD.get() != -EINVAL && leaseFD.get() != -EOPNOTSUPP) {
log(AQ_LOG_ERROR, "reopenDRMNode: drmModeCreateLease failed");
return -1;
return {};
}
log(AQ_LOG_DEBUG, "reopenDRMNode: drmModeCreateLease failed, falling back to open");
}
Expand All @@ -316,34 +317,32 @@ int Aquamarine::CBackend::reopenDRMNode(int drmFD, bool allowRenderNode) {

if (!name) {
log(AQ_LOG_ERROR, "reopenDRMNode: drmGetDeviceNameFromFd2 failed");
return -1;
return {};
}
}

log(AQ_LOG_DEBUG, std::format("reopenDRMNode: opening node {}", name));

int newFD = open(name, O_RDWR | O_CLOEXEC);
if (newFD < 0) {
CFileDescriptor newFD{open(name, O_RDWR | O_CLOEXEC)};
if (!newFD.isValid()) {
log(AQ_LOG_ERROR, std::format("reopenDRMNode: failed to open node {}", name));
free(name);
return -1;
return {};
}

free(name);

// We need to authenticate if we are using a DRM primary node and are the master
if (drmIsMaster(drmFD) && drmGetNodeTypeFromFd(newFD) == DRM_NODE_PRIMARY) {
if (drmIsMaster(drmFD) && drmGetNodeTypeFromFd(newFD.get()) == DRM_NODE_PRIMARY) {
drm_magic_t magic;
if (int ret = drmGetMagic(newFD, &magic); ret < 0) {
if (int ret = drmGetMagic(newFD.get(), &magic); ret < 0) {
log(AQ_LOG_ERROR, std::format("reopenDRMNode: drmGetMagic failed: {}", strerror(-ret)));
close(newFD);
return -1;
return {};
}

if (int ret = drmAuthMagic(drmFD, magic); ret < 0) {
log(AQ_LOG_ERROR, std::format("reopenDRMNode: drmAuthMagic failed: {}", strerror(-ret)));
close(newFD);
return -1;
return {};
}
}

Expand Down
Loading