From fe0039091c21fb3fd99979a611da6519edbbe794 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Wed, 28 Apr 2021 21:09:38 +0300 Subject: [PATCH] platforms/drm: Don't search for gpus on add udev event If a gpu is added, don't search for it pointlessly in m_gpus. --- src/plugins/platforms/drm/drm_backend.cpp | 42 ++++++++++++++--------- src/plugins/platforms/drm/drm_backend.h | 1 + 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/plugins/platforms/drm/drm_backend.cpp b/src/plugins/platforms/drm/drm_backend.cpp index 6c3b94ea18..ce580636eb 100644 --- a/src/plugins/platforms/drm/drm_backend.cpp +++ b/src/plugins/platforms/drm/drm_backend.cpp @@ -236,13 +236,7 @@ void DrmBackend::handleUdevEvent() if (!session()->isActive()) { continue; } - DrmGpu *drmGpu = nullptr; - for (const auto &gpu : qAsConst(m_gpus)) { - if (gpu->drmId() == device->sysNum()) { - drmGpu = gpu; - break; - } - } + if (device->action() == QStringLiteral("add")) { if (m_gpus.isEmpty() || !primaryGpu()->useEglStreams()) { if (const auto &gpu = addGpu(std::move(device))) { @@ -251,24 +245,28 @@ void DrmBackend::handleUdevEvent() emit gpuAdded(gpu); } } - } else if (drmGpu) { - if (device->action() == QStringLiteral("change")) { - qCDebug(KWIN_DRM) << "Received hot plug event for monitored drm device"; - updateOutputs(); - updateCursor(); - } else if (device->action() == QStringLiteral("remove")) { - if (primaryGpu() == drmGpu) { + } else if (device->action() == QStringLiteral("remove")) { + DrmGpu *gpu = findGpu(device->sysNum()); + if (gpu) { + if (primaryGpu() == gpu) { qCCritical(KWIN_DRM) << "Primary gpu has been removed! Quitting..."; kwinApp()->quit(); return; } else { - emit gpuRemoved(drmGpu); - m_gpus.removeOne(drmGpu); - delete drmGpu; + emit gpuRemoved(gpu); + m_gpus.removeOne(gpu); + delete gpu; updateOutputs(); updateCursor(); } } + } else if (device->action() == QStringLiteral("change")) { + DrmGpu *gpu = findGpu(device->sysNum()); + if (gpu) { + qCDebug(KWIN_DRM) << "Received hot plug event for monitored drm device"; + updateOutputs(); + updateCursor(); + } } } } @@ -675,4 +673,14 @@ DrmGpu *DrmBackend::primaryGpu() const return m_gpus.isEmpty() ? nullptr : m_gpus[0]; } +DrmGpu *DrmBackend::findGpu(int sysNum) const +{ + for (DrmGpu *gpu : qAsConst(m_gpus)) { + if (gpu->drmId() == sysNum) { + return gpu; + } + } + return nullptr; +} + } diff --git a/src/plugins/platforms/drm/drm_backend.h b/src/plugins/platforms/drm/drm_backend.h index 6fd6d68a96..d9223e31ec 100644 --- a/src/plugins/platforms/drm/drm_backend.h +++ b/src/plugins/platforms/drm/drm_backend.h @@ -64,6 +64,7 @@ public: QString supportInformation() const override; DrmGpu *primaryGpu() const; + DrmGpu *findGpu(int sysNum) const; public Q_SLOTS: void turnOutputsOn();