backends/drm: use FileDescriptor class for drm buffer fds

This commit is contained in:
Xaver Hugl 2022-07-27 13:16:32 +02:00
parent 6cd4d69644
commit 1464028934
3 changed files with 14 additions and 25 deletions

View file

@ -34,19 +34,9 @@ DrmGpuBuffer::DrmGpuBuffer(DrmGpu *gpu, QSize size, uint32_t format, uint64_t mo
, m_strides(strides) , m_strides(strides)
, m_offsets(offsets) , m_offsets(offsets)
, m_planeCount(planeCount) , m_planeCount(planeCount)
, m_fds({-1, -1, -1, -1})
{ {
} }
DrmGpuBuffer::~DrmGpuBuffer()
{
for (uint32_t i = 0; i < m_planeCount; i++) {
if (m_fds[i] != -1) {
close(m_fds[i]);
}
}
}
DrmGpu *DrmGpuBuffer::gpu() const DrmGpu *DrmGpuBuffer::gpu() const
{ {
return m_gpu; return m_gpu;
@ -67,9 +57,9 @@ QSize DrmGpuBuffer::size() const
return m_size; return m_size;
} }
std::array<int, 4> DrmGpuBuffer::fds() const std::array<FileDescriptor, 4> &DrmGpuBuffer::fds()
{ {
if (m_fds[0] == -1) { if (!m_fds[0].isValid()) {
createFds(); createFds();
} }
return m_fds; return m_fds;

View file

@ -9,6 +9,8 @@
*/ */
#pragma once #pragma once
#include "utils/filedescriptor.h"
#include <QImage> #include <QImage>
#include <QSize> #include <QSize>
#include <array> #include <array>
@ -25,13 +27,13 @@ class DrmGpuBuffer
{ {
public: public:
DrmGpuBuffer(DrmGpu *gpu, QSize size, uint32_t format, uint64_t modifier, const std::array<uint32_t, 4> &handles, const std::array<uint32_t, 4> &strides, const std::array<uint32_t, 4> &offsets, uint32_t planeCount); DrmGpuBuffer(DrmGpu *gpu, QSize size, uint32_t format, uint64_t modifier, const std::array<uint32_t, 4> &handles, const std::array<uint32_t, 4> &strides, const std::array<uint32_t, 4> &offsets, uint32_t planeCount);
virtual ~DrmGpuBuffer(); virtual ~DrmGpuBuffer() = default;
DrmGpu *gpu() const; DrmGpu *gpu() const;
uint32_t format() const; uint32_t format() const;
uint64_t modifier() const; uint64_t modifier() const;
QSize size() const; QSize size() const;
std::array<int, 4> fds(); const std::array<FileDescriptor, 4> &fds();
std::array<uint32_t, 4> handles() const; std::array<uint32_t, 4> handles() const;
std::array<uint32_t, 4> strides() const; std::array<uint32_t, 4> strides() const;
std::array<uint32_t, 4> offsets() const; std::array<uint32_t, 4> offsets() const;
@ -48,7 +50,7 @@ protected:
const std::array<uint32_t, 4> m_strides; const std::array<uint32_t, 4> m_strides;
const std::array<uint32_t, 4> m_offsets; const std::array<uint32_t, 4> m_offsets;
const uint32_t m_planeCount; const uint32_t m_planeCount;
std::array<int, 4> m_fds; std::array<FileDescriptor, 4> m_fds;
}; };
class DrmFramebuffer class DrmFramebuffer

View file

@ -132,12 +132,9 @@ void GbmBuffer::createFds()
{ {
#if HAVE_GBM_BO_GET_FD_FOR_PLANE #if HAVE_GBM_BO_GET_FD_FOR_PLANE
for (uint32_t i = 0; i < m_planeCount; i++) { for (uint32_t i = 0; i < m_planeCount; i++) {
m_fds[i] = gbm_bo_get_fd_for_plane(m_bo, i); m_fds[i] = FileDescriptor(gbm_bo_get_fd_for_plane(m_bo, i));
if (m_fds[i] == -1) { if (!m_fds[i].isValid()) {
for (uint32_t i2 = 0; i2 < i; i2++) { m_fds = {};
close(m_fds[i2]);
m_fds[i2] = -1;
}
return; return;
} }
} }
@ -146,7 +143,7 @@ void GbmBuffer::createFds()
if (m_planeCount > 1) { if (m_planeCount > 1) {
return; return;
} }
m_fds[0] = gbm_bo_get_fd(m_bo); m_fds[0] = FileDescriptor(gbm_bo_get_fd(m_bo));
#endif #endif
} }
@ -185,8 +182,8 @@ std::shared_ptr<GbmBuffer> GbmBuffer::importBuffer(DrmGpu *gpu, KWaylandServer::
std::shared_ptr<GbmBuffer> GbmBuffer::importBuffer(DrmGpu *gpu, GbmBuffer *buffer, uint32_t flags) std::shared_ptr<GbmBuffer> GbmBuffer::importBuffer(DrmGpu *gpu, GbmBuffer *buffer, uint32_t flags)
{ {
const auto fds = buffer->fds(); const auto &fds = buffer->fds();
if (fds[0] == -1) { if (!fds[0].isValid()) {
return nullptr; return nullptr;
} }
const auto strides = buffer->strides(); const auto strides = buffer->strides();
@ -202,7 +199,7 @@ std::shared_ptr<GbmBuffer> GbmBuffer::importBuffer(DrmGpu *gpu, GbmBuffer *buffe
.modifier = buffer->modifier(), .modifier = buffer->modifier(),
}; };
for (uint32_t i = 0; i < data.num_fds; i++) { for (uint32_t i = 0; i < data.num_fds; i++) {
data.fds[i] = fds[i]; data.fds[i] = fds[i].get();
data.strides[i] = strides[i]; data.strides[i] = strides[i];
data.offsets[i] = offsets[i]; data.offsets[i] = offsets[i];
} }