OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/gpu/gpu_info_collector.h" | 5 #include "content/gpu/gpu_info_collector.h" |
6 | 6 |
7 #include <X11/Xlib.h> | 7 #include <X11/Xlib.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
19 #include "base/strings/string_tokenizer.h" | 19 #include "base/strings/string_tokenizer.h" |
20 #include "library_loaders/libpci.h" | 20 #include "library_loaders/libpci.h" |
21 #include "third_party/libXNVCtrl/NVCtrl.h" | 21 #include "third_party/libXNVCtrl/NVCtrl.h" |
22 #include "third_party/libXNVCtrl/NVCtrlLib.h" | 22 #include "third_party/libXNVCtrl/NVCtrlLib.h" |
23 #include "ui/gl/gl_bindings.h" | 23 #include "ui/gl/gl_bindings.h" |
24 #include "ui/gl/gl_context.h" | 24 #include "ui/gl/gl_context.h" |
25 #include "ui/gl/gl_implementation.h" | 25 #include "ui/gl/gl_implementation.h" |
26 #include "ui/gl/gl_surface.h" | 26 #include "ui/gl/gl_surface.h" |
27 #include "ui/gl/gl_switches.h" | 27 #include "ui/gl/gl_switches.h" |
28 | 28 |
| 29 namespace gpu { |
| 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 // This checks if a system supports PCI bus. | 33 // This checks if a system supports PCI bus. |
32 // We check the existence of /sys/bus/pci or /sys/bug/pci_express. | 34 // We check the existence of /sys/bus/pci or /sys/bug/pci_express. |
33 bool IsPciSupported() { | 35 bool IsPciSupported() { |
34 const base::FilePath pci_path("/sys/bus/pci/"); | 36 const base::FilePath pci_path("/sys/bus/pci/"); |
35 const base::FilePath pcie_path("/sys/bus/pci_express/"); | 37 const base::FilePath pcie_path("/sys/bus/pci_express/"); |
36 return (file_util::PathExists(pci_path) || | 38 return (file_util::PathExists(pci_path) || |
37 file_util::PathExists(pcie_path)); | 39 file_util::PathExists(pcie_path)); |
38 } | 40 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return driver_version; | 92 return driver_version; |
91 } | 93 } |
92 } | 94 } |
93 return std::string(); | 95 return std::string(); |
94 } | 96 } |
95 | 97 |
96 const uint32 kVendorIDIntel = 0x8086; | 98 const uint32 kVendorIDIntel = 0x8086; |
97 const uint32 kVendorIDNVidia = 0x10de; | 99 const uint32 kVendorIDNVidia = 0x10de; |
98 const uint32 kVendorIDAMD = 0x1002; | 100 const uint32 kVendorIDAMD = 0x1002; |
99 | 101 |
100 bool CollectPCIVideoCardInfo(content::GPUInfo* gpu_info) { | 102 bool CollectPCIVideoCardInfo(GPUInfo* gpu_info) { |
101 DCHECK(gpu_info); | 103 DCHECK(gpu_info); |
102 | 104 |
103 if (IsPciSupported() == false) { | 105 if (IsPciSupported() == false) { |
104 VLOG(1) << "PCI bus scanning is not supported"; | 106 VLOG(1) << "PCI bus scanning is not supported"; |
105 return false; | 107 return false; |
106 } | 108 } |
107 | 109 |
108 // TODO(zmo): be more flexible about library name. | 110 // TODO(zmo): be more flexible about library name. |
109 LibPciLoader libpci_loader; | 111 LibPciLoader libpci_loader; |
110 if (!libpci_loader.Load("libpci.so.3") && | 112 if (!libpci_loader.Load("libpci.so.3") && |
111 !libpci_loader.Load("libpci.so")) { | 113 !libpci_loader.Load("libpci.so")) { |
112 VLOG(1) << "Failed to locate libpci"; | 114 VLOG(1) << "Failed to locate libpci"; |
113 return false; | 115 return false; |
114 } | 116 } |
115 | 117 |
116 pci_access* access = (libpci_loader.pci_alloc)(); | 118 pci_access* access = (libpci_loader.pci_alloc)(); |
117 DCHECK(access != NULL); | 119 DCHECK(access != NULL); |
118 (libpci_loader.pci_init)(access); | 120 (libpci_loader.pci_init)(access); |
119 (libpci_loader.pci_scan_bus)(access); | 121 (libpci_loader.pci_scan_bus)(access); |
120 bool primary_gpu_identified = false; | 122 bool primary_gpu_identified = false; |
121 for (pci_dev* device = access->devices; | 123 for (pci_dev* device = access->devices; |
122 device != NULL; device = device->next) { | 124 device != NULL; device = device->next) { |
123 // Fill the IDs and class fields. | 125 // Fill the IDs and class fields. |
124 (libpci_loader.pci_fill_info)(device, 33); | 126 (libpci_loader.pci_fill_info)(device, 33); |
125 // TODO(zmo): there might be other classes that qualify as display devices. | 127 // TODO(zmo): there might be other classes that qualify as display devices. |
126 if (device->device_class != 0x0300) // Device class is DISPLAY_VGA. | 128 if (device->device_class != 0x0300) // Device class is DISPLAY_VGA. |
127 continue; | 129 continue; |
128 | 130 |
129 content::GPUInfo::GPUDevice gpu; | 131 GPUInfo::GPUDevice gpu; |
130 gpu.vendor_id = device->vendor_id; | 132 gpu.vendor_id = device->vendor_id; |
131 gpu.device_id = device->device_id; | 133 gpu.device_id = device->device_id; |
132 | 134 |
133 if (!primary_gpu_identified) { | 135 if (!primary_gpu_identified) { |
134 primary_gpu_identified = true; | 136 primary_gpu_identified = true; |
135 gpu_info->gpu = gpu; | 137 gpu_info->gpu = gpu; |
136 } else { | 138 } else { |
137 // TODO(zmo): if there are multiple GPUs, we assume the non Intel | 139 // TODO(zmo): if there are multiple GPUs, we assume the non Intel |
138 // one is primary. Revisit this logic because we actually don't know | 140 // one is primary. Revisit this logic because we actually don't know |
139 // which GPU we are using at this point. | 141 // which GPU we are using at this point. |
(...skipping 15 matching lines...) Expand all Loading... |
155 if (gpu_info->gpu.vendor_id == kVendorIDAMD) | 157 if (gpu_info->gpu.vendor_id == kVendorIDAMD) |
156 gpu_info->amd_switchable = true; | 158 gpu_info->amd_switchable = true; |
157 } | 159 } |
158 | 160 |
159 (libpci_loader.pci_cleanup)(access); | 161 (libpci_loader.pci_cleanup)(access); |
160 return (primary_gpu_identified); | 162 return (primary_gpu_identified); |
161 } | 163 } |
162 | 164 |
163 } // namespace anonymous | 165 } // namespace anonymous |
164 | 166 |
165 namespace gpu_info_collector { | 167 bool CollectContextGraphicsInfo(GPUInfo* gpu_info) { |
166 | |
167 bool CollectContextGraphicsInfo(content::GPUInfo* gpu_info) { | |
168 DCHECK(gpu_info); | 168 DCHECK(gpu_info); |
169 | 169 |
170 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo"); | 170 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo"); |
171 | 171 |
172 if (CommandLine::ForCurrentProcess()->HasSwitch( | 172 if (CommandLine::ForCurrentProcess()->HasSwitch( |
173 switches::kGpuNoContextLost)) { | 173 switches::kGpuNoContextLost)) { |
174 gpu_info->can_lose_context = false; | 174 gpu_info->can_lose_context = false; |
175 } else { | 175 } else { |
176 #if defined(OS_CHROMEOS) | 176 #if defined(OS_CHROMEOS) |
177 gpu_info->can_lose_context = false; | 177 gpu_info->can_lose_context = false; |
178 #else | 178 #else |
179 // TODO(zmo): need to consider the case where we are running on top | 179 // TODO(zmo): need to consider the case where we are running on top |
180 // of desktop GL and GL_ARB_robustness extension is available. | 180 // of desktop GL and GL_ARB_robustness extension is available. |
181 gpu_info->can_lose_context = | 181 gpu_info->can_lose_context = |
182 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2); | 182 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2); |
183 #endif | 183 #endif |
184 } | 184 } |
185 | 185 |
186 gpu_info->finalized = true; | 186 gpu_info->finalized = true; |
187 bool rt = CollectGraphicsInfoGL(gpu_info); | 187 bool rt = CollectGraphicsInfoGL(gpu_info); |
188 | 188 |
189 return rt; | 189 return rt; |
190 } | 190 } |
191 | 191 |
192 GpuIDResult CollectGpuID(uint32* vendor_id, uint32* device_id) { | 192 GpuIDResult CollectGpuID(uint32* vendor_id, uint32* device_id) { |
193 DCHECK(vendor_id && device_id); | 193 DCHECK(vendor_id && device_id); |
194 *vendor_id = 0; | 194 *vendor_id = 0; |
195 *device_id = 0; | 195 *device_id = 0; |
196 | 196 |
197 content::GPUInfo gpu_info; | 197 GPUInfo gpu_info; |
198 if (CollectPCIVideoCardInfo(&gpu_info)) { | 198 if (CollectPCIVideoCardInfo(&gpu_info)) { |
199 *vendor_id = gpu_info.gpu.vendor_id; | 199 *vendor_id = gpu_info.gpu.vendor_id; |
200 *device_id = gpu_info.gpu.device_id; | 200 *device_id = gpu_info.gpu.device_id; |
201 return kGpuIDSuccess; | 201 return kGpuIDSuccess; |
202 } | 202 } |
203 return kGpuIDFailure; | 203 return kGpuIDFailure; |
204 } | 204 } |
205 | 205 |
206 bool CollectBasicGraphicsInfo(content::GPUInfo* gpu_info) { | 206 bool CollectBasicGraphicsInfo(GPUInfo* gpu_info) { |
207 DCHECK(gpu_info); | 207 DCHECK(gpu_info); |
208 | 208 |
209 bool rt = CollectPCIVideoCardInfo(gpu_info); | 209 bool rt = CollectPCIVideoCardInfo(gpu_info); |
210 | 210 |
211 std::string driver_version; | 211 std::string driver_version; |
212 switch (gpu_info->gpu.vendor_id) { | 212 switch (gpu_info->gpu.vendor_id) { |
213 case kVendorIDAMD: | 213 case kVendorIDAMD: |
214 driver_version = CollectDriverVersionATI(); | 214 driver_version = CollectDriverVersionATI(); |
215 if (!driver_version.empty()) { | 215 if (!driver_version.empty()) { |
216 gpu_info->driver_vendor = "ATI / AMD"; | 216 gpu_info->driver_vendor = "ATI / AMD"; |
(...skipping 17 matching lines...) Expand all Loading... |
234 // Machines with more than two GPUs are not handled. | 234 // Machines with more than two GPUs are not handled. |
235 if (gpu_info->secondary_gpus.size() <= 1) | 235 if (gpu_info->secondary_gpus.size() <= 1) |
236 gpu_info->optimus = true; | 236 gpu_info->optimus = true; |
237 } | 237 } |
238 break; | 238 break; |
239 } | 239 } |
240 | 240 |
241 return rt; | 241 return rt; |
242 } | 242 } |
243 | 243 |
244 bool CollectDriverInfoGL(content::GPUInfo* gpu_info) { | 244 bool CollectDriverInfoGL(GPUInfo* gpu_info) { |
245 DCHECK(gpu_info); | 245 DCHECK(gpu_info); |
246 | 246 |
247 std::string gl_version_string = gpu_info->gl_version_string; | 247 std::string gl_version_string = gpu_info->gl_version_string; |
248 if (StartsWithASCII(gl_version_string, "OpenGL ES", true)) | 248 if (StartsWithASCII(gl_version_string, "OpenGL ES", true)) |
249 gl_version_string = gl_version_string.substr(10); | 249 gl_version_string = gl_version_string.substr(10); |
250 std::vector<std::string> pieces; | 250 std::vector<std::string> pieces; |
251 base::SplitStringAlongWhitespace(gl_version_string, &pieces); | 251 base::SplitStringAlongWhitespace(gl_version_string, &pieces); |
252 // In linux, the gl version string might be in the format of | 252 // In linux, the gl version string might be in the format of |
253 // GLVersion DriverVendor DriverVersion | 253 // GLVersion DriverVendor DriverVersion |
254 if (pieces.size() < 3) | 254 if (pieces.size() < 3) |
255 return false; | 255 return false; |
256 | 256 |
257 std::string driver_version = pieces[2]; | 257 std::string driver_version = pieces[2]; |
258 size_t pos = driver_version.find_first_not_of("0123456789."); | 258 size_t pos = driver_version.find_first_not_of("0123456789."); |
259 if (pos == 0) | 259 if (pos == 0) |
260 return false; | 260 return false; |
261 if (pos != std::string::npos) | 261 if (pos != std::string::npos) |
262 driver_version = driver_version.substr(0, pos); | 262 driver_version = driver_version.substr(0, pos); |
263 | 263 |
264 gpu_info->driver_vendor = pieces[1]; | 264 gpu_info->driver_vendor = pieces[1]; |
265 gpu_info->driver_version = driver_version; | 265 gpu_info->driver_version = driver_version; |
266 return true; | 266 return true; |
267 } | 267 } |
268 | 268 |
269 void MergeGPUInfo(content::GPUInfo* basic_gpu_info, | 269 void MergeGPUInfo(GPUInfo* basic_gpu_info, |
270 const content::GPUInfo& context_gpu_info) { | 270 const GPUInfo& context_gpu_info) { |
271 MergeGPUInfoGL(basic_gpu_info, context_gpu_info); | 271 MergeGPUInfoGL(basic_gpu_info, context_gpu_info); |
272 } | 272 } |
273 | 273 |
274 } // namespace gpu_info_collector | 274 } // namespace gpu |
OLD | NEW |