From 4c42269ce6c44af40ea63f048d67dc0b898c8213 Mon Sep 17 00:00:00 2001 From: Pppp1116 Date: Fri, 3 Apr 2026 13:26:49 +0100 Subject: monitor: centralize solitary and scanout eligibility checks --- src/helpers/Drm.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/helpers/Drm.cpp') diff --git a/src/helpers/Drm.cpp b/src/helpers/Drm.cpp index 207b5e3d..f91c5692 100644 --- a/src/helpers/Drm.cpp +++ b/src/helpers/Drm.cpp @@ -1,7 +1,50 @@ #include +#include +#include +#include +#include +#include #include "Drm.hpp" +namespace { + using SDRMNodePair = std::array; + + std::optional getDrmNodePair(int fd1, int fd2) { + const auto DEVA = DRM::devIDFromFD(fd1); + const auto DEVB = DRM::devIDFromFD(fd2); + if (!DEVA || !DEVB) + return std::nullopt; + + SDRMNodePair pair = {*DEVA, *DEVB}; + if (pair[0] > pair[1]) + std::swap(pair[0], pair[1]); + + return pair; + } +} + +std::optional DRM::devIDFromFD(int fd) { + struct stat stat = {}; + if (fstat(fd, &stat) != 0 || !S_ISCHR(stat.st_mode)) + return std::nullopt; + + return stat.st_rdev; +} + bool DRM::sameGpu(int fd1, int fd2) { + if (fd1 >= 0 && fd1 == fd2) + return true; + + static std::mutex cacheMutex; + static std::map sameGpuCache; + + const auto NODEPAIR = getDrmNodePair(fd1, fd2); + if (NODEPAIR) { + std::scoped_lock lock(cacheMutex); + if (const auto it = sameGpuCache.find(*NODEPAIR); it != sameGpuCache.end()) + return it->second; + } + drmDevice* devA = nullptr; drmDevice* devB = nullptr; @@ -16,5 +59,11 @@ bool DRM::sameGpu(int fd1, int fd2) { drmFreeDevice(&devA); drmFreeDevice(&devB); + + if (NODEPAIR) { + std::scoped_lock lock(cacheMutex); + sameGpuCache[*NODEPAIR] = same; + } + return same; } -- cgit v1.2.3