1 // Copyright (c) 2012 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.h"
10 #include "base/command_line.h"
11 #include "base/debug/trace_event.h"
12 #include "base/file_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_piece.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_tokenizer.h"
19 #include "base/strings/string_util.h"
20 #include "library_loaders/libpci.h"
21 #include "third_party/libXNVCtrl/NVCtrl.h"
22 #include "third_party/libXNVCtrl/NVCtrlLib.h"
23 #include "ui/gfx/x/x11_types.h"
24 #include "ui/gl/gl_bindings.h"
25 #include "ui/gl/gl_context.h"
26 #include "ui/gl/gl_implementation.h"
27 #include "ui/gl/gl_surface.h"
28 #include "ui/gl/gl_switches.h"
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))
52 if (!base::ReadFileToString(ati_file_path, &contents))
54 base::StringTokenizer t(contents, "\r\n");
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);
64 return line.substr(begin, end - begin);
71 // Use NVCtrl extention to query NV driver version.
72 // Return empty string on failing.
73 std::string CollectDriverVersionNVidia() {
74 Display* display = gfx::GetXDisplay();
76 LOG(ERROR) << "XOpenDisplay failed.";
79 int event_base = 0, error_base = 0;
80 if (!XNVCTRLQueryExtension(display, &event_base, &error_base)) {
81 VLOG(1) << "NVCtrl extension does not exist.";
84 int screen_count = ScreenCount(display);
85 for (int screen = 0; screen < screen_count; ++screen) {
87 if (XNVCTRLIsNvScreen(display, screen) &&
88 XNVCTRLQueryStringAttribute(display, screen, 0,
89 NV_CTRL_STRING_NVIDIA_DRIVER_VERSION,
91 std::string driver_version(buffer);
93 return driver_version;
99 const uint32 kVendorIDIntel = 0x8086;
100 const uint32 kVendorIDNVidia = 0x10de;
101 const uint32 kVendorIDAMD = 0x1002;
103 bool CollectPCIVideoCardInfo(GPUInfo* gpu_info) {
106 if (IsPciSupported() == false) {
107 VLOG(1) << "PCI bus scanning is not supported";
111 // TODO(zmo): be more flexible about library name.
112 LibPciLoader libpci_loader;
113 if (!libpci_loader.Load("libpci.so.3") &&
114 !libpci_loader.Load("libpci.so")) {
115 VLOG(1) << "Failed to locate libpci";
119 pci_access* access = (libpci_loader.pci_alloc)();
120 DCHECK(access != NULL);
121 (libpci_loader.pci_init)(access);
122 (libpci_loader.pci_scan_bus)(access);
123 bool primary_gpu_identified = false;
124 for (pci_dev* device = access->devices;
125 device != NULL; device = device->next) {
126 // Fill the IDs and class fields.
127 (libpci_loader.pci_fill_info)(device, 33);
129 switch (device->device_class) {
130 case PCI_CLASS_DISPLAY_VGA:
131 case PCI_CLASS_DISPLAY_XGA:
132 case PCI_CLASS_DISPLAY_3D:
135 case PCI_CLASS_DISPLAY_OTHER:
142 GPUInfo::GPUDevice gpu;
143 gpu.vendor_id = device->vendor_id;
144 gpu.device_id = device->device_id;
146 if (!primary_gpu_identified) {
147 primary_gpu_identified = true;
150 // TODO(zmo): if there are multiple GPUs, we assume the non Intel
151 // one is primary. Revisit this logic because we actually don't know
152 // which GPU we are using at this point.
153 if (gpu_info->gpu.vendor_id == kVendorIDIntel &&
154 gpu.vendor_id != kVendorIDIntel) {
155 gpu_info->secondary_gpus.push_back(gpu_info->gpu);
158 gpu_info->secondary_gpus.push_back(gpu);
163 // Detect Optimus or AMD Switchable GPU.
164 if (gpu_info->secondary_gpus.size() == 1 &&
165 gpu_info->secondary_gpus[0].vendor_id == kVendorIDIntel) {
166 if (gpu_info->gpu.vendor_id == kVendorIDNVidia)
167 gpu_info->optimus = true;
168 if (gpu_info->gpu.vendor_id == kVendorIDAMD)
169 gpu_info->amd_switchable = true;
172 (libpci_loader.pci_cleanup)(access);
173 return (primary_gpu_identified);
176 } // namespace anonymous
178 CollectInfoResult CollectContextGraphicsInfo(GPUInfo* gpu_info) {
181 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
183 if (CommandLine::ForCurrentProcess()->HasSwitch(
184 switches::kGpuNoContextLost)) {
185 gpu_info->can_lose_context = false;
187 #if defined(OS_CHROMEOS)
188 gpu_info->can_lose_context = false;
190 // TODO(zmo): need to consider the case where we are running on top
191 // of desktop GL and GL_ARB_robustness extension is available.
192 gpu_info->can_lose_context =
193 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2);
197 CollectInfoResult result = CollectGraphicsInfoGL(gpu_info);
198 gpu_info->finalized = true;
202 GpuIDResult CollectGpuID(uint32* vendor_id, uint32* device_id) {
203 DCHECK(vendor_id && device_id);
208 if (CollectPCIVideoCardInfo(&gpu_info)) {
209 *vendor_id = gpu_info.gpu.vendor_id;
210 *device_id = gpu_info.gpu.device_id;
211 return kGpuIDSuccess;
213 return kGpuIDFailure;
216 CollectInfoResult CollectBasicGraphicsInfo(GPUInfo* gpu_info) {
219 bool rt = CollectPCIVideoCardInfo(gpu_info);
221 std::string driver_version;
222 switch (gpu_info->gpu.vendor_id) {
224 driver_version = CollectDriverVersionATI();
225 if (!driver_version.empty()) {
226 gpu_info->driver_vendor = "ATI / AMD";
227 gpu_info->driver_version = driver_version;
230 case kVendorIDNVidia:
231 driver_version = CollectDriverVersionNVidia();
232 if (!driver_version.empty()) {
233 gpu_info->driver_vendor = "NVIDIA";
234 gpu_info->driver_version = driver_version;
238 // In dual-GPU cases, sometimes PCI scan only gives us the
239 // integrated GPU (i.e., the Intel one).
240 driver_version = CollectDriverVersionNVidia();
241 if (!driver_version.empty()) {
242 gpu_info->driver_vendor = "NVIDIA";
243 gpu_info->driver_version = driver_version;
244 // Machines with more than two GPUs are not handled.
245 if (gpu_info->secondary_gpus.size() <= 1)
246 gpu_info->optimus = true;
251 return rt ? kCollectInfoSuccess : kCollectInfoNonFatalFailure;
254 CollectInfoResult CollectDriverInfoGL(GPUInfo* gpu_info) {
257 std::string gl_version = gpu_info->gl_version;
258 if (StartsWithASCII(gl_version, "OpenGL ES", true))
259 gl_version = gl_version.substr(10);
260 std::vector<std::string> pieces;
261 base::SplitStringAlongWhitespace(gl_version, &pieces);
262 // In linux, the gl version string might be in the format of
263 // GLVersion DriverVendor DriverVersion
264 if (pieces.size() < 3)
265 return kCollectInfoNonFatalFailure;
267 std::string driver_version = pieces[2];
268 size_t pos = driver_version.find_first_not_of("0123456789.");
270 return kCollectInfoNonFatalFailure;
271 if (pos != std::string::npos)
272 driver_version = driver_version.substr(0, pos);
274 gpu_info->driver_vendor = pieces[1];
275 gpu_info->driver_version = driver_version;
276 return kCollectInfoSuccess;
279 void MergeGPUInfo(GPUInfo* basic_gpu_info,
280 const GPUInfo& context_gpu_info) {
281 MergeGPUInfoGL(basic_gpu_info, context_gpu_info);