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"
9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h"
11 #include "base/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 "library_loaders/libpci.h"
21 #include "ui/gl/gl_bindings.h"
22 #include "ui/gl/gl_context.h"
23 #include "ui/gl/gl_implementation.h"
24 #include "ui/gl/gl_surface.h"
25 #include "ui/gl/gl_switches.h"
31 // This checks if a system supports PCI bus.
32 // We check the existence of /sys/bus/pci or /sys/bug/pci_express.
33 bool IsPciSupported() {
34 const base::FilePath
pci_path("/sys/bus/pci/");
35 const base::FilePath
pcie_path("/sys/bus/pci_express/");
36 return (base::PathExists(pci_path
) ||
37 base::PathExists(pcie_path
));
40 // Scan /etc/ati/amdpcsdb.default for "ReleaseVersion".
41 // Return empty string on failing.
42 std::string
CollectDriverVersionATI() {
43 const base::FilePath::CharType kATIFileName
[] =
44 FILE_PATH_LITERAL("/etc/ati/amdpcsdb.default");
45 base::FilePath
ati_file_path(kATIFileName
);
46 if (!base::PathExists(ati_file_path
))
49 if (!base::ReadFileToString(ati_file_path
, &contents
))
51 base::StringTokenizer
t(contents
, "\r\n");
53 std::string line
= t
.token();
54 if (StartsWithASCII(line
, "ReleaseVersion=", true)) {
55 size_t begin
= line
.find_first_of("0123456789");
56 if (begin
!= std::string::npos
) {
57 size_t end
= line
.find_first_not_of("0123456789.", begin
);
58 if (end
== std::string::npos
)
59 return line
.substr(begin
);
61 return line
.substr(begin
, end
- begin
);
68 const uint32 kVendorIDIntel
= 0x8086;
69 const uint32 kVendorIDNVidia
= 0x10de;
70 const uint32 kVendorIDAMD
= 0x1002;
72 bool CollectPCIVideoCardInfo(GPUInfo
* gpu_info
) {
75 if (IsPciSupported() == false) {
76 VLOG(1) << "PCI bus scanning is not supported";
80 // TODO(zmo): be more flexible about library name.
81 LibPciLoader libpci_loader
;
82 if (!libpci_loader
.Load("libpci.so.3") &&
83 !libpci_loader
.Load("libpci.so")) {
84 VLOG(1) << "Failed to locate libpci";
88 pci_access
* access
= (libpci_loader
.pci_alloc
)();
89 DCHECK(access
!= NULL
);
90 (libpci_loader
.pci_init
)(access
);
91 (libpci_loader
.pci_scan_bus
)(access
);
92 bool primary_gpu_identified
= false;
93 for (pci_dev
* device
= access
->devices
;
94 device
!= NULL
; device
= device
->next
) {
95 // Fill the IDs and class fields.
96 (libpci_loader
.pci_fill_info
)(device
, 33);
98 switch (device
->device_class
) {
99 case PCI_CLASS_DISPLAY_VGA
:
100 case PCI_CLASS_DISPLAY_XGA
:
101 case PCI_CLASS_DISPLAY_3D
:
104 case PCI_CLASS_DISPLAY_OTHER
:
111 GPUInfo::GPUDevice gpu
;
112 gpu
.vendor_id
= device
->vendor_id
;
113 gpu
.device_id
= device
->device_id
;
115 if (!primary_gpu_identified
) {
116 primary_gpu_identified
= true;
119 // TODO(zmo): if there are multiple GPUs, we assume the non Intel
120 // one is primary. Revisit this logic because we actually don't know
121 // which GPU we are using at this point.
122 if (gpu_info
->gpu
.vendor_id
== kVendorIDIntel
&&
123 gpu
.vendor_id
!= kVendorIDIntel
) {
124 gpu_info
->secondary_gpus
.push_back(gpu_info
->gpu
);
127 gpu_info
->secondary_gpus
.push_back(gpu
);
132 // Detect Optimus or AMD Switchable GPU.
133 if (gpu_info
->secondary_gpus
.size() == 1 &&
134 gpu_info
->secondary_gpus
[0].vendor_id
== kVendorIDIntel
) {
135 if (gpu_info
->gpu
.vendor_id
== kVendorIDNVidia
)
136 gpu_info
->optimus
= true;
137 if (gpu_info
->gpu
.vendor_id
== kVendorIDAMD
)
138 gpu_info
->amd_switchable
= true;
141 (libpci_loader
.pci_cleanup
)(access
);
142 return (primary_gpu_identified
);
145 } // namespace anonymous
147 CollectInfoResult
CollectContextGraphicsInfo(GPUInfo
* gpu_info
) {
150 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
152 if (CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kGpuNoContextLost
)) {
154 gpu_info
->can_lose_context
= false;
156 #if defined(OS_CHROMEOS)
157 gpu_info
->can_lose_context
= false;
159 // TODO(zmo): need to consider the case where we are running on top
160 // of desktop GL and GL_ARB_robustness extension is available.
161 gpu_info
->can_lose_context
=
162 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2
);
166 CollectInfoResult result
= CollectGraphicsInfoGL(gpu_info
);
167 gpu_info
->finalized
= true;
171 GpuIDResult
CollectGpuID(uint32
* vendor_id
, uint32
* device_id
) {
172 DCHECK(vendor_id
&& device_id
);
177 if (CollectPCIVideoCardInfo(&gpu_info
)) {
178 *vendor_id
= gpu_info
.gpu
.vendor_id
;
179 *device_id
= gpu_info
.gpu
.device_id
;
180 if (*vendor_id
!= 0 && *device_id
!= 0)
181 return kGpuIDSuccess
;
183 return kGpuIDFailure
;
186 CollectInfoResult
CollectBasicGraphicsInfo(GPUInfo
* gpu_info
) {
189 bool rt
= CollectPCIVideoCardInfo(gpu_info
);
191 std::string driver_version
;
192 switch (gpu_info
->gpu
.vendor_id
) {
194 driver_version
= CollectDriverVersionATI();
195 if (!driver_version
.empty()) {
196 gpu_info
->driver_vendor
= "ATI / AMD";
197 gpu_info
->driver_version
= driver_version
;
200 case kVendorIDNVidia
:
201 driver_version
= CollectDriverVersionNVidia();
202 if (!driver_version
.empty()) {
203 gpu_info
->driver_vendor
= "NVIDIA";
204 gpu_info
->driver_version
= driver_version
;
208 // In dual-GPU cases, sometimes PCI scan only gives us the
209 // integrated GPU (i.e., the Intel one).
210 if (gpu_info
->secondary_gpus
.size() == 0) {
211 driver_version
= CollectDriverVersionNVidia();
212 if (!driver_version
.empty()) {
213 gpu_info
->driver_vendor
= "NVIDIA";
214 gpu_info
->driver_version
= driver_version
;
215 gpu_info
->optimus
= true;
216 // Put Intel to the secondary GPU list.
217 gpu_info
->secondary_gpus
.push_back(gpu_info
->gpu
);
218 // Put NVIDIA as the primary GPU.
219 gpu_info
->gpu
.vendor_id
= kVendorIDNVidia
;
220 gpu_info
->gpu
.device_id
= 0; // Unknown Device.
226 return rt
? kCollectInfoSuccess
: kCollectInfoNonFatalFailure
;
229 CollectInfoResult
CollectDriverInfoGL(GPUInfo
* gpu_info
) {
232 std::string gl_version
= gpu_info
->gl_version
;
233 if (StartsWithASCII(gl_version
, "OpenGL ES", true))
234 gl_version
= gl_version
.substr(10);
235 std::vector
<std::string
> pieces
;
236 base::SplitStringAlongWhitespace(gl_version
, &pieces
);
237 // In linux, the gl version string might be in the format of
238 // GLVersion DriverVendor DriverVersion
239 if (pieces
.size() < 3)
240 return kCollectInfoNonFatalFailure
;
242 std::string driver_version
= pieces
[2];
243 size_t pos
= driver_version
.find_first_not_of("0123456789.");
245 return kCollectInfoNonFatalFailure
;
246 if (pos
!= std::string::npos
)
247 driver_version
= driver_version
.substr(0, pos
);
249 gpu_info
->driver_vendor
= pieces
[1];
250 gpu_info
->driver_version
= driver_version
;
251 return kCollectInfoSuccess
;
254 void MergeGPUInfo(GPUInfo
* basic_gpu_info
,
255 const GPUInfo
& context_gpu_info
) {
256 MergeGPUInfoGL(basic_gpu_info
, context_gpu_info
);