Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / config / gpu_info_collector_linux.cc
blobe93a79c42ebdf7289e1729f0255dab90b77d88b8
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gpu/config/gpu_info_collector_linux.h"
7 #include <vector>
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_tokenizer.h"
17 #include "base/strings/string_util.h"
18 #include "base/trace_event/trace_event.h"
19 #include "gpu/config/gpu_info_collector.h"
20 #include "ui/gl/gl_bindings.h"
21 #include "ui/gl/gl_context.h"
22 #include "ui/gl/gl_implementation.h"
23 #include "ui/gl/gl_surface.h"
24 #include "ui/gl/gl_switches.h"
26 #if defined(USE_LIBPCI)
27 #include "library_loaders/libpci.h"
28 #endif
30 namespace gpu {
32 namespace {
34 #if defined(USE_LIBPCI)
35 // This checks if a system supports PCI bus.
36 // We check the existence of /sys/bus/pci or /sys/bug/pci_express.
37 bool IsPciSupported() {
38 const base::FilePath pci_path("/sys/bus/pci/");
39 const base::FilePath pcie_path("/sys/bus/pci_express/");
40 return (base::PathExists(pci_path) ||
41 base::PathExists(pcie_path));
43 #endif // defined(USE_LIBPCI)
45 // Scan /etc/ati/amdpcsdb.default for "ReleaseVersion".
46 // Return empty string on failing.
47 std::string CollectDriverVersionATI() {
48 const base::FilePath::CharType kATIFileName[] =
49 FILE_PATH_LITERAL("/etc/ati/amdpcsdb.default");
50 base::FilePath ati_file_path(kATIFileName);
51 if (!base::PathExists(ati_file_path))
52 return std::string();
53 std::string contents;
54 if (!base::ReadFileToString(ati_file_path, &contents))
55 return std::string();
56 base::StringTokenizer t(contents, "\r\n");
57 while (t.GetNext()) {
58 std::string line = t.token();
59 if (base::StartsWith(line, "ReleaseVersion=",
60 base::CompareCase::SENSITIVE)) {
61 size_t begin = line.find_first_of("0123456789");
62 if (begin != std::string::npos) {
63 size_t end = line.find_first_not_of("0123456789.", begin);
64 if (end == std::string::npos)
65 return line.substr(begin);
66 else
67 return line.substr(begin, end - begin);
71 return std::string();
74 const uint32 kVendorIDIntel = 0x8086;
75 const uint32 kVendorIDNVidia = 0x10de;
76 const uint32 kVendorIDAMD = 0x1002;
78 CollectInfoResult CollectPCIVideoCardInfo(GPUInfo* gpu_info) {
79 DCHECK(gpu_info);
81 #if !defined(USE_LIBPCI)
82 return kCollectInfoNonFatalFailure;
83 #else
85 if (IsPciSupported() == false) {
86 VLOG(1) << "PCI bus scanning is not supported";
87 return kCollectInfoNonFatalFailure;
90 // TODO(zmo): be more flexible about library name.
91 LibPciLoader libpci_loader;
92 if (!libpci_loader.Load("libpci.so.3") &&
93 !libpci_loader.Load("libpci.so")) {
94 VLOG(1) << "Failed to locate libpci";
95 return kCollectInfoNonFatalFailure;
98 pci_access* access = (libpci_loader.pci_alloc)();
99 DCHECK(access != NULL);
100 (libpci_loader.pci_init)(access);
101 (libpci_loader.pci_scan_bus)(access);
102 bool primary_gpu_identified = false;
103 for (pci_dev* device = access->devices;
104 device != NULL; device = device->next) {
105 // Fill the IDs and class fields.
106 (libpci_loader.pci_fill_info)(device, 33);
107 bool is_gpu = false;
108 switch (device->device_class) {
109 case PCI_CLASS_DISPLAY_VGA:
110 case PCI_CLASS_DISPLAY_XGA:
111 case PCI_CLASS_DISPLAY_3D:
112 is_gpu = true;
113 break;
114 case PCI_CLASS_DISPLAY_OTHER:
115 default:
116 break;
118 if (!is_gpu)
119 continue;
120 if (device->vendor_id == 0 || device->device_id == 0)
121 continue;
123 GPUInfo::GPUDevice gpu;
124 gpu.vendor_id = device->vendor_id;
125 gpu.device_id = device->device_id;
127 if (!primary_gpu_identified) {
128 primary_gpu_identified = true;
129 gpu_info->gpu = gpu;
130 } else {
131 // TODO(zmo): if there are multiple GPUs, we assume the non Intel
132 // one is primary. Revisit this logic because we actually don't know
133 // which GPU we are using at this point.
134 if (gpu_info->gpu.vendor_id == kVendorIDIntel &&
135 gpu.vendor_id != kVendorIDIntel) {
136 gpu_info->secondary_gpus.push_back(gpu_info->gpu);
137 gpu_info->gpu = gpu;
138 } else {
139 gpu_info->secondary_gpus.push_back(gpu);
144 // Detect Optimus or AMD Switchable GPU.
145 if (gpu_info->secondary_gpus.size() == 1 &&
146 gpu_info->secondary_gpus[0].vendor_id == kVendorIDIntel) {
147 if (gpu_info->gpu.vendor_id == kVendorIDNVidia)
148 gpu_info->optimus = true;
149 if (gpu_info->gpu.vendor_id == kVendorIDAMD)
150 gpu_info->amd_switchable = true;
153 (libpci_loader.pci_cleanup)(access);
154 if (!primary_gpu_identified)
155 return kCollectInfoNonFatalFailure;
156 return kCollectInfoSuccess;
157 #endif
160 } // namespace anonymous
162 CollectInfoResult CollectContextGraphicsInfo(GPUInfo* gpu_info) {
163 DCHECK(gpu_info);
165 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
167 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
168 switches::kGpuNoContextLost)) {
169 gpu_info->can_lose_context = false;
170 } else {
171 #if defined(OS_CHROMEOS)
172 gpu_info->can_lose_context = false;
173 #else
174 // TODO(zmo): need to consider the case where we are running on top
175 // of desktop GL and GL_ARB_robustness extension is available.
176 gpu_info->can_lose_context =
177 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2);
178 #endif
181 CollectInfoResult result = CollectGraphicsInfoGL(gpu_info);
182 gpu_info->context_info_state = result;
183 return result;
186 CollectInfoResult CollectGpuID(uint32* vendor_id, uint32* device_id) {
187 DCHECK(vendor_id && device_id);
188 *vendor_id = 0;
189 *device_id = 0;
191 GPUInfo gpu_info;
192 CollectInfoResult result = CollectPCIVideoCardInfo(&gpu_info);
193 if (result == kCollectInfoSuccess) {
194 *vendor_id = gpu_info.gpu.vendor_id;
195 *device_id = gpu_info.gpu.device_id;
197 return result;
200 CollectInfoResult CollectBasicGraphicsInfo(GPUInfo* gpu_info) {
201 DCHECK(gpu_info);
203 CollectInfoResult result = CollectPCIVideoCardInfo(gpu_info);
205 std::string driver_version;
206 switch (gpu_info->gpu.vendor_id) {
207 case kVendorIDAMD:
208 driver_version = CollectDriverVersionATI();
209 if (!driver_version.empty()) {
210 gpu_info->driver_vendor = "ATI / AMD";
211 gpu_info->driver_version = driver_version;
213 break;
214 case kVendorIDNVidia:
215 driver_version = CollectDriverVersionNVidia();
216 if (!driver_version.empty()) {
217 gpu_info->driver_vendor = "NVIDIA";
218 gpu_info->driver_version = driver_version;
220 break;
221 case kVendorIDIntel:
222 // In dual-GPU cases, sometimes PCI scan only gives us the
223 // integrated GPU (i.e., the Intel one).
224 if (gpu_info->secondary_gpus.size() == 0) {
225 driver_version = CollectDriverVersionNVidia();
226 if (!driver_version.empty()) {
227 gpu_info->driver_vendor = "NVIDIA";
228 gpu_info->driver_version = driver_version;
229 gpu_info->optimus = true;
230 // Put Intel to the secondary GPU list.
231 gpu_info->secondary_gpus.push_back(gpu_info->gpu);
232 // Put NVIDIA as the primary GPU.
233 gpu_info->gpu.vendor_id = kVendorIDNVidia;
234 gpu_info->gpu.device_id = 0; // Unknown Device.
237 break;
240 gpu_info->basic_info_state = result;
241 return result;
244 CollectInfoResult CollectDriverInfoGL(GPUInfo* gpu_info) {
245 DCHECK(gpu_info);
247 std::string gl_version = gpu_info->gl_version;
248 if (base::StartsWith(gl_version, "OpenGL ES", base::CompareCase::SENSITIVE))
249 gl_version = gl_version.substr(10);
250 std::vector<std::string> pieces = base::SplitString(
251 gl_version, base::kWhitespaceASCII, base::KEEP_WHITESPACE,
252 base::SPLIT_WANT_NONEMPTY);
253 // In linux, the gl version string might be in the format of
254 // GLVersion DriverVendor DriverVersion
255 if (pieces.size() < 3)
256 return kCollectInfoNonFatalFailure;
258 std::string driver_version = pieces[2];
259 size_t pos = driver_version.find_first_not_of("0123456789.");
260 if (pos == 0)
261 return kCollectInfoNonFatalFailure;
262 if (pos != std::string::npos)
263 driver_version = driver_version.substr(0, pos);
265 gpu_info->driver_vendor = pieces[1];
266 gpu_info->driver_version = driver_version;
267 return kCollectInfoSuccess;
270 void MergeGPUInfo(GPUInfo* basic_gpu_info,
271 const GPUInfo& context_gpu_info) {
272 MergeGPUInfoGL(basic_gpu_info, context_gpu_info);
275 } // namespace gpu