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/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"
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
))
54 if (!base::ReadFileToString(ati_file_path
, &contents
))
56 base::StringTokenizer
t(contents
, "\r\n");
58 std::string line
= t
.token();
59 if (StartsWithASCII(line
, "ReleaseVersion=", true)) {
60 size_t begin
= line
.find_first_of("0123456789");
61 if (begin
!= std::string::npos
) {
62 size_t end
= line
.find_first_not_of("0123456789.", begin
);
63 if (end
== std::string::npos
)
64 return line
.substr(begin
);
66 return line
.substr(begin
, end
- begin
);
73 const uint32 kVendorIDIntel
= 0x8086;
74 const uint32 kVendorIDNVidia
= 0x10de;
75 const uint32 kVendorIDAMD
= 0x1002;
77 CollectInfoResult
CollectPCIVideoCardInfo(GPUInfo
* gpu_info
) {
80 #if !defined(USE_LIBPCI)
81 return kCollectInfoNonFatalFailure
;
84 if (IsPciSupported() == false) {
85 VLOG(1) << "PCI bus scanning is not supported";
86 return kCollectInfoNonFatalFailure
;
89 // TODO(zmo): be more flexible about library name.
90 LibPciLoader libpci_loader
;
91 if (!libpci_loader
.Load("libpci.so.3") &&
92 !libpci_loader
.Load("libpci.so")) {
93 VLOG(1) << "Failed to locate libpci";
94 return kCollectInfoNonFatalFailure
;
97 pci_access
* access
= (libpci_loader
.pci_alloc
)();
98 DCHECK(access
!= NULL
);
99 (libpci_loader
.pci_init
)(access
);
100 (libpci_loader
.pci_scan_bus
)(access
);
101 bool primary_gpu_identified
= false;
102 for (pci_dev
* device
= access
->devices
;
103 device
!= NULL
; device
= device
->next
) {
104 // Fill the IDs and class fields.
105 (libpci_loader
.pci_fill_info
)(device
, 33);
107 switch (device
->device_class
) {
108 case PCI_CLASS_DISPLAY_VGA
:
109 case PCI_CLASS_DISPLAY_XGA
:
110 case PCI_CLASS_DISPLAY_3D
:
113 case PCI_CLASS_DISPLAY_OTHER
:
119 if (device
->vendor_id
== 0 || device
->device_id
== 0)
122 GPUInfo::GPUDevice gpu
;
123 gpu
.vendor_id
= device
->vendor_id
;
124 gpu
.device_id
= device
->device_id
;
126 if (!primary_gpu_identified
) {
127 primary_gpu_identified
= true;
130 // TODO(zmo): if there are multiple GPUs, we assume the non Intel
131 // one is primary. Revisit this logic because we actually don't know
132 // which GPU we are using at this point.
133 if (gpu_info
->gpu
.vendor_id
== kVendorIDIntel
&&
134 gpu
.vendor_id
!= kVendorIDIntel
) {
135 gpu_info
->secondary_gpus
.push_back(gpu_info
->gpu
);
138 gpu_info
->secondary_gpus
.push_back(gpu
);
143 // Detect Optimus or AMD Switchable GPU.
144 if (gpu_info
->secondary_gpus
.size() == 1 &&
145 gpu_info
->secondary_gpus
[0].vendor_id
== kVendorIDIntel
) {
146 if (gpu_info
->gpu
.vendor_id
== kVendorIDNVidia
)
147 gpu_info
->optimus
= true;
148 if (gpu_info
->gpu
.vendor_id
== kVendorIDAMD
)
149 gpu_info
->amd_switchable
= true;
152 (libpci_loader
.pci_cleanup
)(access
);
153 if (!primary_gpu_identified
)
154 return kCollectInfoNonFatalFailure
;
155 return kCollectInfoSuccess
;
159 } // namespace anonymous
161 CollectInfoResult
CollectContextGraphicsInfo(GPUInfo
* gpu_info
) {
164 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
166 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
167 switches::kGpuNoContextLost
)) {
168 gpu_info
->can_lose_context
= false;
170 #if defined(OS_CHROMEOS)
171 gpu_info
->can_lose_context
= false;
173 // TODO(zmo): need to consider the case where we are running on top
174 // of desktop GL and GL_ARB_robustness extension is available.
175 gpu_info
->can_lose_context
=
176 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2
);
180 CollectInfoResult result
= CollectGraphicsInfoGL(gpu_info
);
181 gpu_info
->context_info_state
= result
;
185 CollectInfoResult
CollectGpuID(uint32
* vendor_id
, uint32
* device_id
) {
186 DCHECK(vendor_id
&& device_id
);
191 CollectInfoResult result
= CollectPCIVideoCardInfo(&gpu_info
);
192 if (result
== kCollectInfoSuccess
) {
193 *vendor_id
= gpu_info
.gpu
.vendor_id
;
194 *device_id
= gpu_info
.gpu
.device_id
;
199 CollectInfoResult
CollectBasicGraphicsInfo(GPUInfo
* gpu_info
) {
202 CollectInfoResult result
= CollectPCIVideoCardInfo(gpu_info
);
204 std::string driver_version
;
205 switch (gpu_info
->gpu
.vendor_id
) {
207 driver_version
= CollectDriverVersionATI();
208 if (!driver_version
.empty()) {
209 gpu_info
->driver_vendor
= "ATI / AMD";
210 gpu_info
->driver_version
= driver_version
;
213 case kVendorIDNVidia
:
214 driver_version
= CollectDriverVersionNVidia();
215 if (!driver_version
.empty()) {
216 gpu_info
->driver_vendor
= "NVIDIA";
217 gpu_info
->driver_version
= driver_version
;
221 // In dual-GPU cases, sometimes PCI scan only gives us the
222 // integrated GPU (i.e., the Intel one).
223 if (gpu_info
->secondary_gpus
.size() == 0) {
224 driver_version
= CollectDriverVersionNVidia();
225 if (!driver_version
.empty()) {
226 gpu_info
->driver_vendor
= "NVIDIA";
227 gpu_info
->driver_version
= driver_version
;
228 gpu_info
->optimus
= true;
229 // Put Intel to the secondary GPU list.
230 gpu_info
->secondary_gpus
.push_back(gpu_info
->gpu
);
231 // Put NVIDIA as the primary GPU.
232 gpu_info
->gpu
.vendor_id
= kVendorIDNVidia
;
233 gpu_info
->gpu
.device_id
= 0; // Unknown Device.
239 gpu_info
->basic_info_state
= result
;
243 CollectInfoResult
CollectDriverInfoGL(GPUInfo
* gpu_info
) {
246 std::string gl_version
= gpu_info
->gl_version
;
247 if (StartsWithASCII(gl_version
, "OpenGL ES", true))
248 gl_version
= gl_version
.substr(10);
249 std::vector
<std::string
> pieces
;
250 base::SplitStringAlongWhitespace(gl_version
, &pieces
);
251 // In linux, the gl version string might be in the format of
252 // GLVersion DriverVendor DriverVersion
253 if (pieces
.size() < 3)
254 return kCollectInfoNonFatalFailure
;
256 std::string driver_version
= pieces
[2];
257 size_t pos
= driver_version
.find_first_not_of("0123456789.");
259 return kCollectInfoNonFatalFailure
;
260 if (pos
!= std::string::npos
)
261 driver_version
= driver_version
.substr(0, pos
);
263 gpu_info
->driver_vendor
= pieces
[1];
264 gpu_info
->driver_version
= driver_version
;
265 return kCollectInfoSuccess
;
268 void MergeGPUInfo(GPUInfo
* basic_gpu_info
,
269 const GPUInfo
& context_gpu_info
) {
270 MergeGPUInfoGL(basic_gpu_info
, context_gpu_info
);