Implement listing tests to a JSON file for iOS gtest test launcher
[chromium-blink-merge.git] / gpu / config / gpu_info_collector_linux.cc
blob061fe35929e5acf5019dc2e5e6a0624332fadeed
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/debug/trace_event.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_split.h"
17 #include "base/strings/string_tokenizer.h"
18 #include "base/strings/string_util.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 // This checks if a system supports PCI bus.
35 // We check the existence of /sys/bus/pci or /sys/bug/pci_express.
36 bool IsPciSupported() {
37 const base::FilePath pci_path("/sys/bus/pci/");
38 const base::FilePath pcie_path("/sys/bus/pci_express/");
39 return (base::PathExists(pci_path) ||
40 base::PathExists(pcie_path));
43 // Scan /etc/ati/amdpcsdb.default for "ReleaseVersion".
44 // Return empty string on failing.
45 std::string CollectDriverVersionATI() {
46 const base::FilePath::CharType kATIFileName[] =
47 FILE_PATH_LITERAL("/etc/ati/amdpcsdb.default");
48 base::FilePath ati_file_path(kATIFileName);
49 if (!base::PathExists(ati_file_path))
50 return std::string();
51 std::string contents;
52 if (!base::ReadFileToString(ati_file_path, &contents))
53 return std::string();
54 base::StringTokenizer t(contents, "\r\n");
55 while (t.GetNext()) {
56 std::string line = t.token();
57 if (StartsWithASCII(line, "ReleaseVersion=", true)) {
58 size_t begin = line.find_first_of("0123456789");
59 if (begin != std::string::npos) {
60 size_t end = line.find_first_not_of("0123456789.", begin);
61 if (end == std::string::npos)
62 return line.substr(begin);
63 else
64 return line.substr(begin, end - begin);
68 return std::string();
71 const uint32 kVendorIDIntel = 0x8086;
72 const uint32 kVendorIDNVidia = 0x10de;
73 const uint32 kVendorIDAMD = 0x1002;
75 CollectInfoResult CollectPCIVideoCardInfo(GPUInfo* gpu_info) {
76 DCHECK(gpu_info);
78 #if !defined(USE_LIBPCI)
79 return kCollectInfoNonFatalFailure;
80 #else
82 if (IsPciSupported() == false) {
83 VLOG(1) << "PCI bus scanning is not supported";
84 return kCollectInfoNonFatalFailure;
87 // TODO(zmo): be more flexible about library name.
88 LibPciLoader libpci_loader;
89 if (!libpci_loader.Load("libpci.so.3") &&
90 !libpci_loader.Load("libpci.so")) {
91 VLOG(1) << "Failed to locate libpci";
92 return kCollectInfoNonFatalFailure;
95 pci_access* access = (libpci_loader.pci_alloc)();
96 DCHECK(access != NULL);
97 (libpci_loader.pci_init)(access);
98 (libpci_loader.pci_scan_bus)(access);
99 bool primary_gpu_identified = false;
100 for (pci_dev* device = access->devices;
101 device != NULL; device = device->next) {
102 // Fill the IDs and class fields.
103 (libpci_loader.pci_fill_info)(device, 33);
104 bool is_gpu = false;
105 switch (device->device_class) {
106 case PCI_CLASS_DISPLAY_VGA:
107 case PCI_CLASS_DISPLAY_XGA:
108 case PCI_CLASS_DISPLAY_3D:
109 is_gpu = true;
110 break;
111 case PCI_CLASS_DISPLAY_OTHER:
112 default:
113 break;
115 if (!is_gpu)
116 continue;
117 if (device->vendor_id == 0 || device->device_id == 0)
118 continue;
120 GPUInfo::GPUDevice gpu;
121 gpu.vendor_id = device->vendor_id;
122 gpu.device_id = device->device_id;
124 if (!primary_gpu_identified) {
125 primary_gpu_identified = true;
126 gpu_info->gpu = gpu;
127 } else {
128 // TODO(zmo): if there are multiple GPUs, we assume the non Intel
129 // one is primary. Revisit this logic because we actually don't know
130 // which GPU we are using at this point.
131 if (gpu_info->gpu.vendor_id == kVendorIDIntel &&
132 gpu.vendor_id != kVendorIDIntel) {
133 gpu_info->secondary_gpus.push_back(gpu_info->gpu);
134 gpu_info->gpu = gpu;
135 } else {
136 gpu_info->secondary_gpus.push_back(gpu);
141 // Detect Optimus or AMD Switchable GPU.
142 if (gpu_info->secondary_gpus.size() == 1 &&
143 gpu_info->secondary_gpus[0].vendor_id == kVendorIDIntel) {
144 if (gpu_info->gpu.vendor_id == kVendorIDNVidia)
145 gpu_info->optimus = true;
146 if (gpu_info->gpu.vendor_id == kVendorIDAMD)
147 gpu_info->amd_switchable = true;
150 (libpci_loader.pci_cleanup)(access);
151 if (!primary_gpu_identified)
152 return kCollectInfoNonFatalFailure;
153 return kCollectInfoSuccess;
154 #endif
157 } // namespace anonymous
159 CollectInfoResult CollectContextGraphicsInfo(GPUInfo* gpu_info) {
160 DCHECK(gpu_info);
162 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
164 if (CommandLine::ForCurrentProcess()->HasSwitch(
165 switches::kGpuNoContextLost)) {
166 gpu_info->can_lose_context = false;
167 } else {
168 #if defined(OS_CHROMEOS)
169 gpu_info->can_lose_context = false;
170 #else
171 // TODO(zmo): need to consider the case where we are running on top
172 // of desktop GL and GL_ARB_robustness extension is available.
173 gpu_info->can_lose_context =
174 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2);
175 #endif
178 CollectInfoResult result = CollectGraphicsInfoGL(gpu_info);
179 gpu_info->context_info_state = result;
180 return result;
183 CollectInfoResult CollectGpuID(uint32* vendor_id, uint32* device_id) {
184 DCHECK(vendor_id && device_id);
185 *vendor_id = 0;
186 *device_id = 0;
188 GPUInfo gpu_info;
189 CollectInfoResult result = CollectPCIVideoCardInfo(&gpu_info);
190 if (result == kCollectInfoSuccess) {
191 *vendor_id = gpu_info.gpu.vendor_id;
192 *device_id = gpu_info.gpu.device_id;
194 return result;
197 CollectInfoResult CollectBasicGraphicsInfo(GPUInfo* gpu_info) {
198 DCHECK(gpu_info);
200 CollectInfoResult result = CollectPCIVideoCardInfo(gpu_info);
202 std::string driver_version;
203 switch (gpu_info->gpu.vendor_id) {
204 case kVendorIDAMD:
205 driver_version = CollectDriverVersionATI();
206 if (!driver_version.empty()) {
207 gpu_info->driver_vendor = "ATI / AMD";
208 gpu_info->driver_version = driver_version;
210 break;
211 case kVendorIDNVidia:
212 driver_version = CollectDriverVersionNVidia();
213 if (!driver_version.empty()) {
214 gpu_info->driver_vendor = "NVIDIA";
215 gpu_info->driver_version = driver_version;
217 break;
218 case kVendorIDIntel:
219 // In dual-GPU cases, sometimes PCI scan only gives us the
220 // integrated GPU (i.e., the Intel one).
221 if (gpu_info->secondary_gpus.size() == 0) {
222 driver_version = CollectDriverVersionNVidia();
223 if (!driver_version.empty()) {
224 gpu_info->driver_vendor = "NVIDIA";
225 gpu_info->driver_version = driver_version;
226 gpu_info->optimus = true;
227 // Put Intel to the secondary GPU list.
228 gpu_info->secondary_gpus.push_back(gpu_info->gpu);
229 // Put NVIDIA as the primary GPU.
230 gpu_info->gpu.vendor_id = kVendorIDNVidia;
231 gpu_info->gpu.device_id = 0; // Unknown Device.
234 break;
237 gpu_info->basic_info_state = result;
238 return result;
241 CollectInfoResult CollectDriverInfoGL(GPUInfo* gpu_info) {
242 DCHECK(gpu_info);
244 std::string gl_version = gpu_info->gl_version;
245 if (StartsWithASCII(gl_version, "OpenGL ES", true))
246 gl_version = gl_version.substr(10);
247 std::vector<std::string> pieces;
248 base::SplitStringAlongWhitespace(gl_version, &pieces);
249 // In linux, the gl version string might be in the format of
250 // GLVersion DriverVendor DriverVersion
251 if (pieces.size() < 3)
252 return kCollectInfoNonFatalFailure;
254 std::string driver_version = pieces[2];
255 size_t pos = driver_version.find_first_not_of("0123456789.");
256 if (pos == 0)
257 return kCollectInfoNonFatalFailure;
258 if (pos != std::string::npos)
259 driver_version = driver_version.substr(0, pos);
261 gpu_info->driver_vendor = pieces[1];
262 gpu_info->driver_version = driver_version;
263 return kCollectInfoSuccess;
266 void MergeGPUInfo(GPUInfo* basic_gpu_info,
267 const GPUInfo& context_gpu_info) {
268 MergeGPUInfoGL(basic_gpu_info, context_gpu_info);
271 } // namespace gpu