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

Add dumb allocator #31

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 33 additions & 3 deletions include/aquamarine/allocator/GBM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ namespace Aquamarine {
friend class CGBMAllocator;
};

class CGBMDumbBuffer : public IBuffer {
UjinT34 marked this conversation as resolved.
Show resolved Hide resolved
public:
virtual ~CGBMDumbBuffer();

virtual eBufferCapability caps();
virtual eBufferType type();
virtual void update(const Hyprutils::Math::CRegion& damage);
virtual bool isSynchronous();
virtual bool good();
virtual SDMABUFAttrs dmabuf();
UjinT34 marked this conversation as resolved.
Show resolved Hide resolved
virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags);
virtual void endDataPtr();

private:
CGBMDumbBuffer(const SAllocatorBufferParams& params, Hyprutils::Memory::CWeakPointer<CGBMAllocator> allocator_, Hyprutils::Memory::CSharedPointer<CSwapchain> swapchain);

Hyprutils::Memory::CWeakPointer<CGBMAllocator> allocator;

// dumb stuff
int m_drmFd;
UjinT34 marked this conversation as resolved.
Show resolved Hide resolved
uint32_t m_handle;
void* m_data = nullptr;
size_t m_size = 0;

SDMABUFAttrs attrs{.success = false};

friend class CGBMAllocator;
};

class CGBMAllocator : public IAllocator {
public:
~CGBMAllocator();
Expand All @@ -53,17 +82,18 @@ namespace Aquamarine {
CGBMAllocator(int fd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_);

// a vector purely for tracking (debugging) the buffers and nothing more
std::vector<Hyprutils::Memory::CWeakPointer<CGBMBuffer>> buffers;
std::vector<Hyprutils::Memory::CWeakPointer<IBuffer>> buffers;
UjinT34 marked this conversation as resolved.
Show resolved Hide resolved

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

// gbm stuff
gbm_device* gbmDevice = nullptr;
std::string gbmDeviceBackendName = "";
std::string drmName = "";

friend class CGBMBuffer;
friend class CGBMDumbBuffer;
friend class CDRMRenderer;
};
};
1 change: 1 addition & 0 deletions include/aquamarine/allocator/Swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ namespace Aquamarine {
int lastAcquired = 0;

friend class CGBMBuffer;
friend class CGBMDumbBuffer;
};
};
1 change: 1 addition & 0 deletions include/aquamarine/buffer/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Aquamarine {
BUFFER_TYPE_DMABUF = 0,
BUFFER_TYPE_SHM,
BUFFER_TYPE_MISC,
BUFFER_TYPE_DMABUF_DUMB,
UjinT34 marked this conversation as resolved.
Show resolved Hide resolved
};

class CWLBufferResource;
Expand Down
135 changes: 128 additions & 7 deletions src/allocator/GBM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <aquamarine/allocator/Swapchain.hpp>
#include "FormatUtils.hpp"
#include "Shared.hpp"
#include <cstring>
#include <fcntl.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include <gbm.h>
#include <unistd.h>
Expand Down Expand Up @@ -142,9 +145,9 @@ Aquamarine::CGBMBuffer::CGBMBuffer(const SAllocatorBufferParams& params, Hypruti
bo = gbm_bo_create_with_modifiers2(allocator->gbmDevice, attrs.size.x, attrs.size.y, attrs.format, explicitModifiers.data(), explicitModifiers.size(), flags);

if (!bo && CURSOR) {
// allow non-renderable cursor buffer for nvidia
allocator->backend->log(AQ_LOG_ERROR, "GBM: Allocating with modifiers and flags failed, falling back to modifiers without flags");
bo = gbm_bo_create_with_modifiers(allocator->gbmDevice, attrs.size.x, attrs.size.y, attrs.format, explicitModifiers.data(), explicitModifiers.size());
// use dumb buffers for cursors
allocator->backend->log(AQ_LOG_ERROR, "GBM: Allocating with modifiers and flags failed for cursor plane, falling back to dumb");
return;
}

if (!bo) {
Expand Down Expand Up @@ -199,7 +202,7 @@ Aquamarine::CGBMBuffer::~CGBMBuffer() {
}

eBufferCapability Aquamarine::CGBMBuffer::caps() {
return (Aquamarine::eBufferCapability)0;
return BUFFER_CAPABILITY_DATAPTR;
}

eBufferType Aquamarine::CGBMBuffer::type() {
Expand All @@ -215,7 +218,7 @@ bool Aquamarine::CGBMBuffer::isSynchronous() {
}

bool Aquamarine::CGBMBuffer::good() {
return true;
return attrs.success;
}

SDMABUFAttrs Aquamarine::CGBMBuffer::dmabuf() {
Expand All @@ -240,6 +243,118 @@ void Aquamarine::CGBMBuffer::endDataPtr() {
}
}

Aquamarine::CGBMDumbBuffer::CGBMDumbBuffer(const SAllocatorBufferParams& params, Hyprutils::Memory::CWeakPointer<CGBMAllocator> allocator_,
Hyprutils::Memory::CSharedPointer<CSwapchain> swapchain) :
allocator(allocator_) {
if (!allocator)
return;

drm_mode_create_dumb createArgs{
.height = uint32_t(params.size.x),
.width = uint32_t(params.size.y),
.bpp = 32,
};

TRACE(allocator->backend->log(AQ_LOG_TRACE, std::format("GBM: Allocating a dumb buffer: size {}, format {}", params.size, fourccToName(params.format))));
if (drmIoctl(gbm_device_get_fd(allocator->gbmDevice), DRM_IOCTL_MODE_CREATE_DUMB, &createArgs) != 0) {
allocator->backend->log(AQ_LOG_ERROR, std::format("GBM: DRM_IOCTL_MODE_CREATE_DUMB failed {}", strerror(errno)));
return;
}

int primeFd;
if (drmPrimeHandleToFD(gbm_device_get_fd(allocator->gbmDevice), createArgs.handle, DRM_CLOEXEC, &primeFd) != 0) {
allocator->backend->log(AQ_LOG_ERROR, std::format("GBM: drmPrimeHandleToFD() failed {}", strerror(errno)));
drm_mode_destroy_dumb destroyArgs{
.handle = createArgs.handle,
};
drmIoctl(gbm_device_get_fd(allocator->gbmDevice), DRM_IOCTL_MODE_DESTROY_DUMB, &destroyArgs);
return;
}

m_drmFd = gbm_device_get_fd(allocator->gbmDevice);
m_handle = createArgs.handle;
m_size = createArgs.pitch * params.size.y;

attrs.planes = 1;
attrs.size = params.size;
attrs.format = DRM_FORMAT_ARGB8888;
attrs.modifier = DRM_FORMAT_MOD_LINEAR;
attrs.offsets = {0, 0, 0, 0};
attrs.fds = {primeFd, 0, 0, 0};
attrs.strides = {createArgs.pitch, 0, 0, 0};

attrs.success = true;

allocator->backend->log(AQ_LOG_DEBUG, std::format("GBM: Allocated a new dumb buffer with size {} and format {}", attrs.size, fourccToName(attrs.format)));
}

Aquamarine::CGBMDumbBuffer::~CGBMDumbBuffer() {
events.destroy.emit();

endDataPtr();

if (m_handle) {
drm_mode_destroy_dumb destroyArgs{
.handle = m_handle,
};
drmIoctl(m_drmFd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroyArgs);
}
}

eBufferCapability Aquamarine::CGBMDumbBuffer::caps() {
return BUFFER_CAPABILITY_DATAPTR;
}

eBufferType Aquamarine::CGBMDumbBuffer::type() {
return Aquamarine::eBufferType::BUFFER_TYPE_DMABUF_DUMB;
}

void Aquamarine::CGBMDumbBuffer::update(const Hyprutils::Math::CRegion& damage) {
;
}

bool Aquamarine::CGBMDumbBuffer::isSynchronous() {
return false; // FIXME is it correct?
}

bool Aquamarine::CGBMDumbBuffer::good() {
return true;
}

SDMABUFAttrs Aquamarine::CGBMDumbBuffer::dmabuf() {
return attrs;
}

std::tuple<uint8_t*, uint32_t, size_t> Aquamarine::CGBMDumbBuffer::beginDataPtr(uint32_t flags) {
if (!m_data) {
drm_mode_map_dumb mapArgs{
.handle = m_handle,
};
if (drmIoctl(m_drmFd, DRM_IOCTL_MODE_MAP_DUMB, &mapArgs) != 0) {
allocator->backend->log(AQ_LOG_ERROR, std::format("GBM: DRM_IOCTL_MODE_MAP_DUMB failed {}", strerror(errno)));
return {};
}

void* address = mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_drmFd, mapArgs.offset);
if (address == MAP_FAILED) {
allocator->backend->log(AQ_LOG_ERROR, std::format("GBM: mmap failed {}", strerror(errno)));
return {};
}

m_data = address;
}

// FIXME: assumes a 32-bit pixel format
return {(uint8_t*)m_data, attrs.format, attrs.strides[0]};
}

void Aquamarine::CGBMDumbBuffer::endDataPtr() {
if (m_data) {
munmap(m_data, m_size);
m_data = nullptr;
}
}

CGBMAllocator::~CGBMAllocator() {
if (gbmDevice)
gbm_device_destroy(gbmDevice);
Expand Down Expand Up @@ -285,11 +400,17 @@ SP<IBuffer> Aquamarine::CGBMAllocator::acquire(const SAllocatorBufferParams& par
return nullptr;
}

auto newBuffer = SP<CGBMBuffer>(new CGBMBuffer(params, self, swapchain_));
SP<IBuffer> newBuffer = SP<CGBMBuffer>(new CGBMBuffer(params, self, swapchain_));

if (!newBuffer->good()) {
backend->log(AQ_LOG_ERROR, std::format("Couldn't allocate a gbm buffer with size {} and format {}", params.size, fourccToName(params.format)));
return nullptr;

newBuffer = SP<CGBMDumbBuffer>(new CGBMDumbBuffer(params, self, swapchain_));

if (!newBuffer->good()) {
backend->log(AQ_LOG_ERROR, std::format("Couldn't allocate a dumb gbm buffer with size {} and format {}", params.size, fourccToName(params.format)));
return nullptr;
}
}

buffers.emplace_back(newBuffer);
Expand Down
Loading