| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuinfoopengl.h" |
| 5 | |
| 6 | #include "../infoproviderregistry.h" |
| 7 | #include <QProcess> |
| 8 | #include <QString> |
| 9 | #include <QStringList> |
| 10 | #include <spdlog/spdlog.h> |
| 11 | #include <utility> |
| 12 | |
| 13 | class GPUInfoOpenGLDataSource : public IDataSource<std::string, int const> |
| 14 | { |
| 15 | public: |
| 16 | std::string source() const override |
| 17 | { |
| 18 | return "glxinfo"; |
| 19 | } |
| 20 | |
| 21 | bool read(std::string &data, int const &gpuIndex) override |
| 22 | { |
| 23 | static constexpr std::string_view libGLErrorStr( |
| 24 | "libGL error: failed to load driver"); |
| 25 | |
| 26 | auto env = QProcessEnvironment::systemEnvironment(); |
| 27 | env.insert("LC_ALL", "C"); |
| 28 | env.insert("DRI_PRIME", QString::number(gpuIndex)); |
| 29 | |
| 30 | QProcess cmd; |
| 31 | cmd.setProcessChannelMode(QProcess::MergedChannels); |
| 32 | cmd.setProcessEnvironment(env); |
| 33 | cmd.start(source().c_str(), QStringList("-B")); |
| 34 | |
| 35 | if (cmd.waitForFinished()) { |
| 36 | auto output = cmd.readAllStandardOutput().toStdString(); |
| 37 | |
| 38 | auto libGLErrorPos = output.find(libGLErrorStr); |
| 39 | if (libGLErrorPos == std::string::npos) { |
| 40 | data = output; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | // extract libGL error |
| 45 | auto endLinePos = output.find("\n", libGLErrorPos); |
| 46 | auto libGLError = output.substr( |
| 47 | libGLErrorPos + libGLErrorStr.length(), |
| 48 | endLinePos - libGLErrorPos - libGLErrorStr.length()); |
| 49 | |
| 50 | SPDLOG_WARN("glxinfo command failed for GPU{} with error '{}{}'", |
| 51 | gpuIndex, libGLErrorStr.data(), libGLError); |
| 52 | } |
| 53 | |
| 54 | SPDLOG_WARN("glxinfo command failed"); |
| 55 | return false; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | GPUInfoOpenGL::GPUInfoOpenGL( |
| 60 | std::unique_ptr<IDataSource<std::string, int const>> &&dataSource) noexcept |
| 61 | : dataSource_(std::move(dataSource)) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | std::vector<std::pair<std::string, std::string>> |
| 66 | GPUInfoOpenGL::provideInfo(Vendor, int gpuIndex, IGPUInfo::Path const &, |
| 67 | IHWIDTranslator const &) const |
| 68 | { |
| 69 | std::vector<std::pair<std::string, std::string>> info; |
| 70 | |
| 71 | static constexpr std::string_view queryRendererStr("GLX_MESA_query_renderer"); |
| 72 | static constexpr std::string_view coreVerStr("Max core profile version: "); |
| 73 | static constexpr std::string_view compatVerStr( |
| 74 | "Max compat profile version: "); |
| 75 | |
| 76 | std::string data; |
| 77 | if (dataSource_->read(data, gpuIndex)) { |
| 78 | auto queryRendererPos = data.find(queryRendererStr); |
| 79 | if (queryRendererPos != std::string::npos) { |
| 80 | |
| 81 | auto coreVer = findItem(data, coreVerStr, queryRendererPos); |
| 82 | if (!coreVer.empty()) |
| 83 | info.emplace_back(GPUInfoOpenGL::Keys::coreVersion, std::move(coreVer)); |
| 84 | else |
| 85 | SPDLOG_DEBUG("Cannot find '{}' in glxinfo output", coreVerStr.data()); |
| 86 | |
| 87 | auto compatVer = findItem(data, compatVerStr, queryRendererPos); |
| 88 | if (!compatVer.empty()) |
| 89 | info.emplace_back(GPUInfoOpenGL::Keys::compatVersion, |
| 90 | std::move(compatVer)); |
| 91 | else |
| 92 | SPDLOG_DEBUG("Cannot find '{}' in glxinfo output", compatVerStr.data()); |
| 93 | } |
| 94 | else |
| 95 | SPDLOG_DEBUG("Cannot find '{}' in glxinfo output", queryRendererStr.data()); |
| 96 | } |
| 97 | |
| 98 | return info; |
| 99 | } |
| 100 | |
| 101 | std::vector<std::string> |
| 102 | GPUInfoOpenGL::provideCapabilities(Vendor, int, IGPUInfo::Path const &) const |
| 103 | { |
| 104 | return {}; |
| 105 | } |
| 106 | |
| 107 | std::string GPUInfoOpenGL::findItem(std::string const &src, |
| 108 | std::string_view itemStr, size_t pos) const |
| 109 | { |
| 110 | auto itemPos = src.find(itemStr, pos); |
| 111 | if (itemPos != std::string::npos) { |
| 112 | |
| 113 | auto endLinePos = src.find("\n", itemPos); |
| 114 | auto item = src.substr(itemPos + itemStr.length(), |
| 115 | endLinePos - itemPos - itemStr.length()); |
| 116 | return item; |
| 117 | } |
| 118 | |
| 119 | return std::string{}; |
| 120 | } |
| 121 | |
| 122 | bool GPUInfoOpenGL::registered_ = InfoProviderRegistry::add( |
| 123 | std::make_unique<GPUInfoOpenGL>(std::make_unique<GPUInfoOpenGLDataSource>())); |