Skip to content

Commit

Permalink
gbm: Expose bo buffer mapping (#12)
Browse files Browse the repository at this point in the history
* expose bo buffer mapping

* update comment

* implement endDataPtr to unmap gbm buffer
  • Loading branch information
UjinT34 authored Jul 12, 2024
1 parent 47d95b8 commit 815df06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
18 changes: 11 additions & 7 deletions include/aquamarine/allocator/GBM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ namespace Aquamarine {
public:
virtual ~CGBMBuffer();

virtual eBufferCapability caps();
virtual eBufferType type();
virtual void update(const Hyprutils::Math::CRegion& damage);
virtual bool isSynchronous();
virtual bool good();
virtual SDMABUFAttrs dmabuf();
virtual eBufferCapability caps();
virtual eBufferType type();
virtual void update(const Hyprutils::Math::CRegion& damage);
virtual bool isSynchronous();
virtual bool good();
virtual SDMABUFAttrs dmabuf();
virtual std::tuple<uint8_t*, uint32_t, size_t> beginDataPtr(uint32_t flags);
virtual void endDataPtr();

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

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

// gbm stuff
gbm_bo* bo = nullptr;
gbm_bo* bo = nullptr;
void* boBuffer = nullptr;
void* gboMapping = nullptr;
SDMABUFAttrs attrs{.success = false};

friend class CGBMAllocator;
Expand Down
26 changes: 24 additions & 2 deletions src/allocator/GBM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ static SDRMFormat guessFormatFrom(std::vector<SDRMFormat> formats, bool cursor)
}

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

Expand Down Expand Up @@ -158,8 +159,11 @@ Aquamarine::CGBMBuffer::CGBMBuffer(const SAllocatorBufferParams& params, Hypruti

Aquamarine::CGBMBuffer::~CGBMBuffer() {
events.destroy.emit();
if (bo)
if (bo) {
if (gboMapping)
gbm_bo_unmap(bo, gboMapping); // FIXME: is it needed before destroy?
gbm_bo_destroy(bo);
}
for (size_t i = 0; i < (size_t)attrs.planes; i++)
close(attrs.fds.at(i));
}
Expand Down Expand Up @@ -188,6 +192,24 @@ SDMABUFAttrs Aquamarine::CGBMBuffer::dmabuf() {
return attrs;
}

std::tuple<uint8_t*, uint32_t, size_t> Aquamarine::CGBMBuffer::beginDataPtr(uint32_t flags) {
uint32_t dst_stride = 0;
if (boBuffer)
allocator->backend->log(AQ_LOG_ERROR, "beginDataPtr is called a second time without calling endDataPtr first. Returning old mapping");
else
boBuffer = gbm_bo_map(bo, 0, 0, attrs.size.x, attrs.size.y, flags, &dst_stride, &gboMapping);
// FIXME: assumes a 32-bit pixel format
return {(uint8_t*)boBuffer, attrs.format, attrs.size.x * attrs.size.y * 4};
}

void Aquamarine::CGBMBuffer::endDataPtr() {
if (gboMapping) {
gbm_bo_unmap(bo, gboMapping);
gboMapping = nullptr;
boBuffer = nullptr;
}
}

CGBMAllocator::~CGBMAllocator() {
if (gbmDevice)
gbm_device_destroy(gbmDevice);
Expand Down

0 comments on commit 815df06

Please sign in to comment.