From affa7386f8afaa57bcc50f1b0b8702319d3a4fa0 Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Wed, 22 Jun 2022 11:34:28 +0200 Subject: [PATCH] backensd/drm: manage gpus with std::unique_ptr --- src/backends/drm/drm_backend.cpp | 56 +++++++++++++++----------------- src/backends/drm/drm_backend.h | 5 ++- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/backends/drm/drm_backend.cpp b/src/backends/drm/drm_backend.cpp index 4abf968154..2e475ae2db 100644 --- a/src/backends/drm/drm_backend.cpp +++ b/src/backends/drm/drm_backend.cpp @@ -91,10 +91,7 @@ DrmBackend::DrmBackend(QObject *parent) supportsOutputChanges(); } -DrmBackend::~DrmBackend() -{ - qDeleteAll(m_gpus); -} +DrmBackend::~DrmBackend() = default; bool DrmBackend::isActive() const { @@ -222,7 +219,7 @@ bool DrmBackend::initialize() } } - if (m_gpus.isEmpty()) { + if (m_gpus.empty()) { qCWarning(KWIN_DRM) << "No suitable DRM devices have been found"; return false; } @@ -264,10 +261,7 @@ void DrmBackend::handleUdevEvent() kwinApp()->quit(); return; } else { - qCDebug(KWIN_DRM) << "Removing gpu" << gpu->devNode(); - Q_EMIT gpuRemoved(gpu); - m_gpus.removeOne(gpu); - delete gpu; + removeGpu(gpu); updateOutputs(); } } @@ -308,15 +302,25 @@ DrmGpu *DrmBackend::addGpu(const QString &fileName) return nullptr; } - DrmGpu *gpu = new DrmGpu(this, fileName, fd, buf.st_rdev); - m_gpus.append(gpu); + m_gpus.push_back(std::make_unique(this, fileName, fd, buf.st_rdev)); + auto gpu = m_gpus.back().get(); m_active = true; connect(gpu, &DrmGpu::outputAdded, this, &DrmBackend::addOutput); connect(gpu, &DrmGpu::outputRemoved, this, &DrmBackend::removeOutput); - Q_EMIT gpuAdded(gpu); return gpu; } +void DrmBackend::removeGpu(DrmGpu *gpu) +{ + auto it = std::find_if(m_gpus.begin(), m_gpus.end(), [gpu](const auto &g) { + return g.get() == gpu; + }); + if (it != m_gpus.end()) { + qCDebug(KWIN_DRM) << "Removing gpu" << gpu->devNode(); + m_gpus.erase(it); + } +} + void DrmBackend::addOutput(DrmAbstractOutput *o) { m_outputs.append(o); @@ -335,13 +339,11 @@ void DrmBackend::updateOutputs() { const auto oldOutputs = m_outputs; for (auto it = m_gpus.begin(); it < m_gpus.end();) { - auto gpu = *it; + auto gpu = it->get(); gpu->updateOutputs(); if (gpu->outputs().isEmpty() && gpu != primaryGpu()) { qCDebug(KWIN_DRM) << "removing unused GPU" << gpu->devNode(); it = m_gpus.erase(it); - Q_EMIT gpuRemoved(gpu); - delete gpu; } else { it++; } @@ -589,7 +591,7 @@ QString DrmBackend::supportInformation() const s << "Name: " << "DRM" << Qt::endl; s << "Active: " << m_active << Qt::endl; - for (int g = 0; g < m_gpus.size(); g++) { + for (size_t g = 0; g < m_gpus.size(); g++) { s << "Atomic Mode Setting on GPU " << g << ": " << m_gpus.at(g)->atomicModeSetting() << Qt::endl; } return supportInfo; @@ -659,27 +661,23 @@ std::shared_ptr DrmBackend::createDmaBufTexture(const QSize &size DrmGpu *DrmBackend::primaryGpu() const { - return m_gpus.isEmpty() ? nullptr : m_gpus[0]; + return m_gpus.empty() ? nullptr : m_gpus.front().get(); } DrmGpu *DrmBackend::findGpu(dev_t deviceId) const { - for (DrmGpu *gpu : qAsConst(m_gpus)) { - if (gpu->deviceId() == deviceId) { - return gpu; - } - } - return nullptr; + auto it = std::find_if(m_gpus.begin(), m_gpus.end(), [deviceId](const auto &gpu) { + return gpu->deviceId() == deviceId; + }); + return it == m_gpus.end() ? nullptr : it->get(); } DrmGpu *DrmBackend::findGpuByFd(int fd) const { - for (DrmGpu *gpu : qAsConst(m_gpus)) { - if (gpu->fd() == fd) { - return gpu; - } - } - return nullptr; + auto it = std::find_if(m_gpus.begin(), m_gpus.end(), [fd](const auto &gpu) { + return gpu->fd() == fd; + }); + return it == m_gpus.end() ? nullptr : it->get(); } bool DrmBackend::applyOutputChanges(const OutputConfiguration &config) diff --git a/src/backends/drm/drm_backend.h b/src/backends/drm/drm_backend.h index fc32eede4d..6db337aefa 100644 --- a/src/backends/drm/drm_backend.h +++ b/src/backends/drm/drm_backend.h @@ -84,8 +84,6 @@ public Q_SLOTS: Q_SIGNALS: void activeChanged(); - void gpuRemoved(DrmGpu *gpu); - void gpuAdded(DrmGpu *gpu); protected: bool applyOutputChanges(const OutputConfiguration &config) override; @@ -99,6 +97,7 @@ private: void deactivate(); bool readOutputsConfiguration(const QVector &outputs); void handleUdevEvent(); + void removeGpu(DrmGpu *gpu); DrmGpu *addGpu(const QString &fileName); std::unique_ptr m_udev; @@ -112,7 +111,7 @@ private: bool m_active = false; const QStringList m_explicitGpus; - QVector m_gpus; + std::vector> m_gpus; std::unique_ptr m_dpmsFilter; std::unique_ptr m_placeholderFilter; DrmRenderBackend *m_renderBackend = nullptr;