diff --git a/src/backends/drm/drm_buffer.cpp b/src/backends/drm/drm_buffer.cpp index aafb857466..e5e0141e50 100644 --- a/src/backends/drm/drm_buffer.cpp +++ b/src/backends/drm/drm_buffer.cpp @@ -34,19 +34,9 @@ DrmGpuBuffer::DrmGpuBuffer(DrmGpu *gpu, QSize size, uint32_t format, uint64_t mo , m_strides(strides) , m_offsets(offsets) , 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 { return m_gpu; @@ -67,9 +57,9 @@ QSize DrmGpuBuffer::size() const return m_size; } -std::array DrmGpuBuffer::fds() +const std::array &DrmGpuBuffer::fds() { - if (m_fds[0] == -1) { + if (!m_fds[0].isValid()) { createFds(); } return m_fds; diff --git a/src/backends/drm/drm_buffer.h b/src/backends/drm/drm_buffer.h index b48caeae9e..78c6b3a853 100644 --- a/src/backends/drm/drm_buffer.h +++ b/src/backends/drm/drm_buffer.h @@ -9,6 +9,8 @@ */ #pragma once +#include "utils/filedescriptor.h" + #include #include #include @@ -25,13 +27,13 @@ class DrmGpuBuffer { public: DrmGpuBuffer(DrmGpu *gpu, QSize size, uint32_t format, uint64_t modifier, const std::array &handles, const std::array &strides, const std::array &offsets, uint32_t planeCount); - virtual ~DrmGpuBuffer(); + virtual ~DrmGpuBuffer() = default; DrmGpu *gpu() const; uint32_t format() const; uint64_t modifier() const; QSize size() const; - std::array fds(); + const std::array &fds(); std::array handles() const; std::array strides() const; std::array offsets() const; @@ -48,7 +50,7 @@ protected: const std::array m_strides; const std::array m_offsets; const uint32_t m_planeCount; - std::array m_fds; + std::array m_fds; }; class DrmFramebuffer diff --git a/src/backends/drm/drm_buffer_gbm.cpp b/src/backends/drm/drm_buffer_gbm.cpp index d4e73f86ae..47d67a2423 100644 --- a/src/backends/drm/drm_buffer_gbm.cpp +++ b/src/backends/drm/drm_buffer_gbm.cpp @@ -132,12 +132,9 @@ void GbmBuffer::createFds() { #if HAVE_GBM_BO_GET_FD_FOR_PLANE for (uint32_t i = 0; i < m_planeCount; i++) { - m_fds[i] = gbm_bo_get_fd_for_plane(m_bo, i); - if (m_fds[i] == -1) { - for (uint32_t i2 = 0; i2 < i; i2++) { - close(m_fds[i2]); - m_fds[i2] = -1; - } + m_fds[i] = FileDescriptor(gbm_bo_get_fd_for_plane(m_bo, i)); + if (!m_fds[i].isValid()) { + m_fds = {}; return; } } @@ -146,7 +143,7 @@ void GbmBuffer::createFds() if (m_planeCount > 1) { return; } - m_fds[0] = gbm_bo_get_fd(m_bo); + m_fds[0] = FileDescriptor(gbm_bo_get_fd(m_bo)); #endif } @@ -185,8 +182,8 @@ std::shared_ptr GbmBuffer::importBuffer(DrmGpu *gpu, KWaylandServer:: std::shared_ptr GbmBuffer::importBuffer(DrmGpu *gpu, GbmBuffer *buffer, uint32_t flags) { - const auto fds = buffer->fds(); - if (fds[0] == -1) { + const auto &fds = buffer->fds(); + if (!fds[0].isValid()) { return nullptr; } const auto strides = buffer->strides(); @@ -202,7 +199,7 @@ std::shared_ptr GbmBuffer::importBuffer(DrmGpu *gpu, GbmBuffer *buffe .modifier = buffer->modifier(), }; 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.offsets[i] = offsets[i]; }