-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#pragma once | ||
|
||
#include "Allocator.hpp" | ||
|
||
namespace Aquamarine { | ||
class CDumbAllocator; | ||
class CBackend; | ||
class CSwapchain; | ||
|
||
class CDumbBuffer : public IBuffer { | ||
public: | ||
virtual ~CDumbBuffer(); | ||
|
||
virtual eBufferCapability caps(); | ||
virtual eBufferType type(); | ||
virtual void update(const Hyprutils::Math::CRegion& damage); | ||
virtual bool isSynchronous(); | ||
virtual bool good(); | ||
virtual SSHMAttrs shm(); | ||
virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags); | ||
virtual void endDataPtr(); | ||
|
||
private: | ||
CDumbBuffer(const SAllocatorBufferParams& params, Hyprutils::Memory::CWeakPointer<CDumbAllocator> allocator_, Hyprutils::Memory::CSharedPointer<CSwapchain> swapchain); | ||
|
||
Hyprutils::Memory::CWeakPointer<CDumbAllocator> allocator; | ||
|
||
// dumb stuff | ||
int drmFd = -1; | ||
uint32_t handle = 0; | ||
void* data = nullptr; | ||
size_t length = 0; | ||
|
||
SSHMAttrs attrs{.success = false}; | ||
|
||
friend class CDumbAllocator; | ||
}; | ||
|
||
class CDumbAllocator : public IAllocator { | ||
public: | ||
~CDumbAllocator(); | ||
static Hyprutils::Memory::CSharedPointer<CDumbAllocator> create(int 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(); | ||
virtual int drmFD(); | ||
|
||
// | ||
Hyprutils::Memory::CWeakPointer<CDumbAllocator> self; | ||
|
||
private: | ||
CDumbAllocator(int drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_); | ||
|
||
// a vector purely for tracking (debugging) the buffers and nothing more | ||
std::vector<Hyprutils::Memory::CWeakPointer<CDumbBuffer>> buffers; | ||
|
||
int fd = -1; | ||
Hyprutils::Memory::CWeakPointer<CBackend> backend; | ||
|
||
friend class CDumbBuffer; | ||
friend class CDRMRenderer; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,6 @@ namespace Aquamarine { | |
int lastAcquired = 0; | ||
|
||
friend class CGBMBuffer; | ||
friend class CDumbBuffer; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#include <aquamarine/allocator/Dumb.hpp> | ||
#include <aquamarine/backend/Backend.hpp> | ||
#include <aquamarine/allocator/Swapchain.hpp> | ||
#include "FormatUtils.hpp" | ||
#include "Shared.hpp" | ||
#include "aquamarine/buffer/Buffer.hpp" | ||
#include <cstring> | ||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
#include <xf86drm.h> | ||
#include <unistd.h> | ||
|
||
using namespace Aquamarine; | ||
using namespace Hyprutils::Memory; | ||
#define SP CSharedPointer | ||
|
||
Aquamarine::CDumbBuffer::CDumbBuffer(const SAllocatorBufferParams& params, Hyprutils::Memory::CWeakPointer<CDumbAllocator> 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("DUMB: Allocating a dumb buffer: size {}, format {}", params.size, fourccToName(params.format)))); | ||
if (drmIoctl(allocator->drmFD(), DRM_IOCTL_MODE_CREATE_DUMB, &createArgs) != 0) { | ||
allocator->backend->log(AQ_LOG_ERROR, std::format("DUMB: DRM_IOCTL_MODE_CREATE_DUMB failed {}", strerror(errno))); | ||
return; | ||
} | ||
|
||
int primeFd; | ||
if (drmPrimeHandleToFD(allocator->drmFD(), createArgs.handle, DRM_CLOEXEC, &primeFd) != 0) { | ||
allocator->backend->log(AQ_LOG_ERROR, std::format("DUMB: drmPrimeHandleToFD() failed {}", strerror(errno))); | ||
drm_mode_destroy_dumb destroyArgs{ | ||
.handle = createArgs.handle, | ||
}; | ||
drmIoctl(allocator->drmFD(), DRM_IOCTL_MODE_DESTROY_DUMB, &destroyArgs); | ||
return; | ||
} | ||
|
||
drmFd = allocator->drmFD(); | ||
handle = createArgs.handle; | ||
length = createArgs.pitch * params.size.y; | ||
|
||
attrs.size = params.size; | ||
attrs.format = DRM_FORMAT_ARGB8888; | ||
attrs.offset = 0; | ||
attrs.fd = primeFd; | ||
attrs.stride = createArgs.pitch; | ||
|
||
attrs.success = true; | ||
|
||
allocator->backend->log(AQ_LOG_DEBUG, std::format("DUMB: Allocated a new dumb buffer with size {} and format {}", attrs.size, fourccToName(attrs.format))); | ||
} | ||
|
||
Aquamarine::CDumbBuffer::~CDumbBuffer() { | ||
events.destroy.emit(); | ||
|
||
endDataPtr(); | ||
|
||
if (handle) { | ||
drm_mode_destroy_dumb destroyArgs{ | ||
.handle = handle, | ||
}; | ||
drmIoctl(drmFd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroyArgs); | ||
} | ||
} | ||
|
||
eBufferCapability Aquamarine::CDumbBuffer::caps() { | ||
return BUFFER_CAPABILITY_DATAPTR; | ||
} | ||
|
||
eBufferType Aquamarine::CDumbBuffer::type() { | ||
return Aquamarine::eBufferType::BUFFER_TYPE_DUMB; | ||
} | ||
|
||
void Aquamarine::CDumbBuffer::update(const Hyprutils::Math::CRegion& damage) { | ||
; | ||
} | ||
|
||
bool Aquamarine::CDumbBuffer::isSynchronous() { | ||
return true; | ||
} | ||
|
||
bool Aquamarine::CDumbBuffer::good() { | ||
return true; | ||
} | ||
|
||
SSHMAttrs Aquamarine::CDumbBuffer::shm() { | ||
return attrs; | ||
} | ||
|
||
std::tuple<uint8_t*, uint32_t, size_t> Aquamarine::CDumbBuffer::beginDataPtr(uint32_t flags) { | ||
if (!data) { | ||
drm_mode_map_dumb mapArgs{ | ||
.handle = handle, | ||
}; | ||
if (drmIoctl(drmFd, DRM_IOCTL_MODE_MAP_DUMB, &mapArgs) != 0) { | ||
allocator->backend->log(AQ_LOG_ERROR, std::format("DUMB: DRM_IOCTL_MODE_MAP_DUMB failed {}", strerror(errno))); | ||
return {}; | ||
} | ||
|
||
void* address = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, drmFd, mapArgs.offset); | ||
if (address == MAP_FAILED) { | ||
allocator->backend->log(AQ_LOG_ERROR, std::format("DUMB: mmap failed {}", strerror(errno))); | ||
return {}; | ||
} | ||
|
||
data = address; | ||
} | ||
|
||
// FIXME: assumes a 32-bit pixel format | ||
return {(uint8_t*)data, attrs.format, attrs.stride}; | ||
} | ||
|
||
void Aquamarine::CDumbBuffer::endDataPtr() { | ||
if (data) { | ||
munmap(data, length); | ||
data = nullptr; | ||
} | ||
} | ||
|
||
CDumbAllocator::~CDumbAllocator() {} | ||
|
||
SP<CDumbAllocator> Aquamarine::CDumbAllocator::create(int drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_) { | ||
auto allocator = SP<CDumbAllocator>(new CDumbAllocator(drmfd_, backend_)); | ||
|
||
if (allocator->drmFD() < 0) { | ||
backend_->log(AQ_LOG_ERROR, "Cannot create a Dumb Allocator: drm fd is required."); | ||
return nullptr; | ||
} | ||
|
||
backend_->log(AQ_LOG_DEBUG, std::format("Created a Dumb Allocator")); | ||
|
||
allocator->self = allocator; | ||
|
||
return allocator; | ||
} | ||
|
||
Aquamarine::CDumbAllocator::CDumbAllocator(int drmfd_, Hyprutils::Memory::CWeakPointer<CBackend> backend_) : fd(drmfd_), backend(backend_) {} | ||
|
||
SP<IBuffer> Aquamarine::CDumbAllocator::acquire(const SAllocatorBufferParams& params, Hyprutils::Memory::CSharedPointer<CSwapchain> swapchain_) { | ||
if (params.size.x < 1 || params.size.y < 1) { | ||
backend->log(AQ_LOG_ERROR, std::format("Couldn't allocate a dumb buffer with invalid size {}", params.size)); | ||
return nullptr; | ||
} | ||
|
||
auto newBuffer = SP<CDumbBuffer>(new CDumbBuffer(params, self, swapchain_)); | ||
|
||
if (!newBuffer->good()) { | ||
backend->log(AQ_LOG_ERROR, std::format("Couldn't allocate a dumb buffer with size {} and format {}", params.size, fourccToName(params.format))); | ||
return nullptr; | ||
} | ||
|
||
buffers.emplace_back(newBuffer); | ||
std::erase_if(buffers, [](const auto& b) { return b.expired(); }); | ||
return newBuffer; | ||
} | ||
|
||
Hyprutils::Memory::CSharedPointer<CBackend> Aquamarine::CDumbAllocator::getBackend() { | ||
return backend.lock(); | ||
} | ||
|
||
int Aquamarine::CDumbAllocator::drmFD() { | ||
return fd; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, no, no. The dumb allocator doesn't really hold any big allocs, so it should be per-drm-backend (can be always present, no big deal)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't really understand what needs to be done here. How HL should get access to dumb allocator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in CDRMBackend
SP<CDumbAllocator>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then it can
->output->backend
, cast that to drm and->dumballocator