Skip to content

Commit

Permalink
core: make SDMABUFAttrs use CFileDescriptor
Browse files Browse the repository at this point in the history
change SDMABUFAttrs use CFileDescriptor for its fd, change to use const
references to the attrs, and its related usage. requires compositor
patch.
  • Loading branch information
gulafaran committed Feb 2, 2025
1 parent 343178b commit ecf26a1
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 48 deletions.
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
3 changes: 2 additions & 1 deletion 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 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 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
17 changes: 7 additions & 10 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
8 changes: 4 additions & 4 deletions src/backend/Wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,11 @@ bool Aquamarine::CWaylandOutput::setCursor(Hyprutils::Memory::CSharedPointer<IBu
pool.reset();

close(fd);
} else if (auto attrs = buffer->dmabuf(); attrs.success) {
} else if (const auto& attrs = buffer->dmabuf(); attrs.success) {
auto params = makeShared<CCZwpLinuxBufferParamsV1>(backend->waylandState.dmabuf->sendCreateParams());

for (int i = 0; i < attrs.planes; ++i) {
params->sendAdd(attrs.fds.at(i), i, attrs.offsets.at(i), attrs.strides.at(i), attrs.modifier >> 32, attrs.modifier & 0xFFFFFFFF);
params->sendAdd(attrs.fds.at(i).get(), i, attrs.offsets.at(i), attrs.strides.at(i), attrs.modifier >> 32, attrs.modifier & 0xFFFFFFFF);
}

cursorState.cursorWlBuffer = makeShared<CCWlBuffer>(params->sendCreateImmed(attrs.size.x, attrs.size.y, attrs.format, (zwpLinuxBufferParamsV1Flags)0));
Expand Down Expand Up @@ -781,10 +781,10 @@ Aquamarine::CWaylandBuffer::CWaylandBuffer(SP<IBuffer> buffer_, Hyprutils::Memor
return;
}

auto attrs = buffer->dmabuf();
const auto& attrs = buffer->dmabuf();

for (int i = 0; i < attrs.planes; ++i) {
params->sendAdd(attrs.fds.at(i), i, attrs.offsets.at(i), attrs.strides.at(i), attrs.modifier >> 32, attrs.modifier & 0xFFFFFFFF);
params->sendAdd(attrs.fds.at(i).get(), i, attrs.offsets.at(i), attrs.strides.at(i), attrs.modifier >> 32, attrs.modifier & 0xFFFFFFFF);
}

waylandState.buffer = makeShared<CCWlBuffer>(params->sendCreateImmed(attrs.size.x, attrs.size.y, attrs.format, (zwpLinuxBufferParamsV1Flags)0));
Expand Down
16 changes: 8 additions & 8 deletions src/backend/drm/DRM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,9 +1650,9 @@ bool Aquamarine::CDRMOutput::commitState(bool onlyTest) {
mgpu.swapchain = CSwapchain::create(backend->rendererState.allocator, backend.lock());
}

auto OPTIONS = swapchain->currentOptions();
auto bufDma = STATE.buffer->dmabuf();
OPTIONS.size = STATE.buffer->size;
auto OPTIONS = swapchain->currentOptions();
const auto& bufDma = STATE.buffer->dmabuf();
OPTIONS.size = STATE.buffer->size;
if (OPTIONS.format == DRM_FORMAT_INVALID)
OPTIONS.format = bufDma.format;
OPTIONS.multigpu = false; // this is not a shared swapchain, and additionally, don't make it linear, nvidia would be mad
Expand Down Expand Up @@ -1699,7 +1699,7 @@ bool Aquamarine::CDRMOutput::commitState(bool onlyTest) {
// sometimes, our consumer could f up the swapchain format and change it without the state changing
bool formatMismatch = false;
if (data.mainFB) {
if (const auto params = data.mainFB->buffer->dmabuf(); params.success && params.format != STATE.drmFormat) {
if (const auto& params = data.mainFB->buffer->dmabuf(); params.success && params.format != STATE.drmFormat) {
// formats mismatch. Update the state format and roll with it
backend->backend->log(AQ_LOG_WARNING,
std::format("drm: Formats mismatch in commit, buffer is {} but output is set to {}. Modesetting to {}", fourccToName(params.format),
Expand Down Expand Up @@ -1988,7 +1988,7 @@ Aquamarine::CDRMFB::CDRMFB(SP<IBuffer> buffer_, Hyprutils::Memory::CWeakPointer<
}

void Aquamarine::CDRMFB::import() {
auto attrs = buffer->dmabuf();
const auto& attrs = buffer->dmabuf();
if (!attrs.success) {
backend->backend->log(AQ_LOG_ERROR, "drm: Buffer submitted has no dmabuf or a drm handle");
return;
Expand All @@ -2001,14 +2001,14 @@ void Aquamarine::CDRMFB::import() {

// TODO: check format
for (int i = 0; i < attrs.planes; ++i) {
int ret = drmPrimeFDToHandle(backend->gpu->fd, attrs.fds.at(i), &boHandles[i]);
int ret = drmPrimeFDToHandle(backend->gpu->fd, attrs.fds.at(i).get(), &boHandles[i]);
if (ret) {
backend->backend->log(AQ_LOG_ERROR, "drm: drmPrimeFDToHandle failed");
drop();
return;
}

TRACE(backend->backend->log(AQ_LOG_TRACE, std::format("drm: CDRMFB: plane {} has fd {}, got handle {}", i, attrs.fds.at(i), boHandles.at(i))));
TRACE(backend->backend->log(AQ_LOG_TRACE, std::format("drm: CDRMFB: plane {} has fd {}, got handle {}", i, attrs.fds.at(i).get(), boHandles.at(i))));
}

id = submitBuffer();
Expand Down Expand Up @@ -2101,7 +2101,7 @@ uint32_t Aquamarine::CDRMFB::submitBuffer() {
if (!buffer->dmabuf().success)
return 0;

auto attrs = buffer->dmabuf();
const auto& attrs = buffer->dmabuf();
std::array<uint64_t, 4> mods = {0, 0, 0, 0};
for (int i = 0; i < attrs.planes; ++i) {
mods[i] = attrs.modifier;
Expand Down
12 changes: 6 additions & 6 deletions src/backend/drm/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ EGLImageKHR CDRMRenderer::createEGLImage(const SDMABUFAttrs& attrs) {

for (int i = 0; i < attrs.planes; i++) {
attribs.push_back(attrNames[i].fd);
attribs.push_back(attrs.fds[i]);
attribs.push_back(attrs.fds[i].get());
attribs.push_back(attrNames[i].offset);
attribs.push_back(attrs.offsets[i]);
attribs.push_back(attrNames[i].pitch);
Expand Down Expand Up @@ -665,9 +665,9 @@ EGLImageKHR CDRMRenderer::createEGLImage(const SDMABUFAttrs& attrs) {
}

SGLTex CDRMRenderer::glTex(Hyprutils::Memory::CSharedPointer<IBuffer> buffa) {
SGLTex tex;
SGLTex tex;

const auto dma = buffa->dmabuf();
const auto& dma = buffa->dmabuf();

tex.image = createEGLImage(dma);
if (tex.image == EGL_NO_IMAGE_KHR) {
Expand Down Expand Up @@ -785,8 +785,8 @@ int CDRMRenderer::recreateBlitSync() {
void CDRMRenderer::clearBuffer(IBuffer* buf) {
setEGL();

auto dmabuf = buf->dmabuf();
GLuint rboID = 0, fboID = 0;
const auto& dmabuf = buf->dmabuf();
GLuint rboID = 0, fboID = 0;

if (!dmabuf.success) {
backend->log(AQ_LOG_ERROR, "EGL (clear): cannot clear a non-dmabuf");
Expand Down Expand Up @@ -874,7 +874,7 @@ CDRMRenderer::SBlitResult CDRMRenderer::blit(SP<IBuffer> from, SP<IBuffer> to, i

EGLImageKHR rboImage = nullptr;
GLuint rboID = 0, fboID = 0;
auto toDma = to->dmabuf();
const auto& toDma = to->dmabuf();

if (!verifyDestinationDMABUF(toDma)) {
backend->log(AQ_LOG_ERROR, "EGL (blit): failed to blit: destination dmabuf unsupported");
Expand Down
8 changes: 4 additions & 4 deletions src/backend/drm/impl/Legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ bool Aquamarine::CDRMLegacyImpl::commitInternal(Hyprutils::Memory::CSharedPointe

if (data.cursorFB && connector->crtc->cursor && connector->output->cursorVisible && enable &&
(STATE.committed & COutputState::AQ_OUTPUT_STATE_CURSOR_SHAPE || STATE.committed & COutputState::AQ_OUTPUT_STATE_CURSOR_POS)) {
uint32_t boHandle = 0;
auto attrs = data.cursorFB->buffer->dmabuf();
uint32_t boHandle = 0;
const auto& attrs = data.cursorFB->buffer->dmabuf();

if (int ret = drmPrimeFDToHandle(connector->backend->gpu->fd, attrs.fds.at(0), &boHandle); ret) {
if (int ret = drmPrimeFDToHandle(connector->backend->gpu->fd, attrs.fds.at(0).get(), &boHandle); ret) {
connector->backend->backend->log(AQ_LOG_ERROR, std::format("legacy drm: drmPrimeFDToHandle failed: {}", strerror(-ret)));
return false;
}

connector->backend->backend->log(AQ_LOG_DEBUG,
std::format("legacy drm: cursor fb: {} with bo handle {} from fd {}, size {}", connector->backend->gpu->fd, boHandle,
data.cursorFB->buffer->dmabuf().fds.at(0), data.cursorFB->buffer->size));
data.cursorFB->buffer->dmabuf().fds.at(0).get(), data.cursorFB->buffer->size));

Vector2D cursorPos = connector->output->cursorPos;

Expand Down
4 changes: 2 additions & 2 deletions src/buffer/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using namespace Aquamarine;

SDMABUFAttrs Aquamarine::IBuffer::dmabuf() {
return SDMABUFAttrs{};
const SDMABUFAttrs& Aquamarine::IBuffer::dmabuf() const {
return m_attrs;
}

SSHMAttrs Aquamarine::IBuffer::shm() {
Expand Down

0 comments on commit ecf26a1

Please sign in to comment.