diff options
| author | Pppp1116 <pcaiadoguerreiro@gmail.com> | 2026-04-03 13:26:49 +0100 |
|---|---|---|
| committer | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2026-04-04 08:26:24 -0400 |
| commit | 4c42269ce6c44af40ea63f048d67dc0b898c8213 (patch) | |
| tree | 469602f310004ec1c41bc99432abae9828b2012a /src/helpers/Drm.cpp | |
| parent | d5b00bbc1759d2e8f6486152c0e622e57fc54b72 (diff) | |
monitor: centralize solitary and scanout eligibility checks
Diffstat (limited to 'src/helpers/Drm.cpp')
| -rw-r--r-- | src/helpers/Drm.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
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 <xf86drm.h> +#include <array> +#include <map> +#include <mutex> +#include <optional> +#include <sys/stat.h> #include "Drm.hpp" +namespace { + using SDRMNodePair = std::array<dev_t, 2>; + + std::optional<SDRMNodePair> 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<dev_t> 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<SDRMNodePair, bool> 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; } |
