| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "sysexplorer.h" |
| 5 | |
| 6 | #include "common/fileutils.h" |
| 7 | #include "common/stringutils.h" |
| 8 | #include <algorithm> |
| 9 | #include <spdlog/spdlog.h> |
| 10 | #include <utility> |
| 11 | |
| 12 | SysExplorer::SysExplorer(std::vector<Vendor> gpuVendors) noexcept |
| 13 | : gpuVendors_(std::move(gpuVendors)) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | std::vector<std::string> SysExplorer::renderers() |
| 18 | { |
| 19 | namespace fs = std::filesystem; |
| 20 | std::vector<std::string> devices; |
| 21 | |
| 22 | fs::path renderBasePath{"/sys/class/drm"}; |
| 23 | for (auto const &drmEntry : fs::directory_iterator(renderBasePath)) { |
| 24 | auto const &renderDPath = drmEntry.path(); |
| 25 | |
| 26 | // use the entries starting with "renderD" |
| 27 | auto const renderDName = renderDPath.filename().string(); |
| 28 | if (renderDName.find("renderD") == 0) { |
| 29 | |
| 30 | auto const sysPath = renderDPath / "device"; |
| 31 | if (Utils::File::isDirectoryPathValid(sysPath)) { |
| 32 | |
| 33 | // skip unsupported devices |
| 34 | if (!checkGPUVendor(sysPath)) |
| 35 | continue; |
| 36 | |
| 37 | devices.emplace_back(std::move(renderDName)); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return devices; |
| 43 | } |
| 44 | |
| 45 | bool SysExplorer::checkGPUVendor(std::filesystem::path sysPath) const |
| 46 | { |
| 47 | auto const vendorPath = sysPath / "vendor"; |
| 48 | auto lines = Utils::File::readFileLines(vendorPath); |
| 49 | if (!lines.empty()) { |
| 50 | int vendor; |
| 51 | if (Utils::String::toNumber<int>(vendor, lines.front(), 16)) { |
| 52 | if (std::find(gpuVendors_.cbegin(), gpuVendors_.cend(), Vendor{vendor}) != |
| 53 | gpuVendors_.cend()) |
| 54 | return true; |
| 55 | } |
| 56 | else |
| 57 | SPDLOG_DEBUG("Cannot parse vendor id from file {}.", vendorPath.c_str()); |
| 58 | } |
| 59 | return false; |
| 60 | } |