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 if (*vendor_id
!= 0 && *device_id
!= 0)
212 return kGpuIDSuccess
;
214 return kGpuIDFailure
;
217 CollectInfoResult
CollectBasicGraphicsInfo(GPUInfo
* gpu_info
) {
220 bool rt
= CollectPCIVideoCardInfo(gpu_info
);
222 std::string driver_version
;
223 switch (gpu_info
->gpu
.vendor_id
) {
225 driver_version
= CollectDriverVersionATI();
226 if (!driver_version
.empty()) {
227 gpu_info
->driver_vendor
= "ATI / AMD";
228 gpu_info
->driver_version
= driver_version
;
231 case kVendorIDNVidia
:
232 driver_version
= CollectDriverVersionNVidia();
233 if (!driver_version
.empty()) {
234 gpu_info
->driver_vendor
= "NVIDIA";
235 gpu_info
->driver_version
= driver_version
;
239 // In dual-GPU cases, sometimes PCI scan only gives us the
240 // integrated GPU (i.e., the Intel one).
241 if (gpu_info
->secondary_gpus
.size() == 0) {
242 driver_version
= CollectDriverVersionNVidia();
243 if (!driver_version
.empty()) {
244 gpu_info
->driver_vendor
= "NVIDIA";
245 gpu_info
->driver_version
= driver_version
;
246 gpu_info
->optimus
= true;
247 // Put Intel to the secondary GPU list.
248 gpu_info
->secondary_gpus
.push_back(gpu_info
->gpu
);
249 // Put NVIDIA as the primary GPU.
250 gpu_info
->gpu
.vendor_id
= kVendorIDNVidia
;
251 gpu_info
->gpu
.device_id
= 0; // Unknown Device.
257 return rt
? kCollectInfoSuccess
: kCollectInfoNonFatalFailure
;
260 CollectInfoResult
CollectDriverInfoGL(GPUInfo
* gpu_info
) {
263 std::string gl_version
= gpu_info
->gl_version
;
264 if (StartsWithASCII(gl_version
, "OpenGL ES", true))
265 gl_version
= gl_version
.substr(10);
266 std::vector
<std::string
> pieces
;
267 base::SplitStringAlongWhitespace(gl_version
, &pieces
);
268 // In linux, the gl version string might be in the format of
269 // GLVersion DriverVendor DriverVersion
270 if (pieces
.size() < 3)
271 return kCollectInfoNonFatalFailure
;
273 std::string driver_version
= pieces
[2];
274 size_t pos
= driver_version
.find_first_not_of("0123456789.");
276 return kCollectInfoNonFatalFailure
;
277 if (pos
!= std::string::npos
)
278 driver_version
= driver_version
.substr(0, pos
);
280 gpu_info
->driver_vendor
= pieces
[1];
281 gpu_info
->driver_version
= driver_version
;
282 return kCollectInfoSuccess
;
285 void MergeGPUInfo(GPUInfo
* basic_gpu_info
,
286 const GPUInfo
& context_gpu_info
) {
287 MergeGPUInfoGL(basic_gpu_info
, context_gpu_info
);