[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / gpu / gpu_info_collector_linux.cc
blobe96d251557415422aab83880d0224894da11cc38
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 "content/gpu/gpu_info_collector.h"
7 #include <X11/Xlib.h>
8 #include <vector>
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.h"
16 #include "base/string_piece.h"
17 #include "base/string_split.h"
18 #include "base/string_tokenizer.h"
19 #include "base/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/gl/gl_bindings.h"
24 #include "ui/gl/gl_context.h"
25 #include "ui/gl/gl_implementation.h"
26 #include "ui/gl/gl_surface.h"
27 #include "ui/gl/gl_switches.h"
29 namespace {
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 FilePath pci_path("/sys/bus/pci/");
35 const FilePath pcie_path("/sys/bus/pci_express/");
36 return (file_util::PathExists(pci_path) ||
37 file_util::PathExists(pcie_path));
40 // Scan /etc/ati/amdpcsdb.default for "ReleaseVersion".
41 // Return empty string on failing.
42 std::string CollectDriverVersionATI() {
43 const FilePath::CharType kATIFileName[] =
44 FILE_PATH_LITERAL("/etc/ati/amdpcsdb.default");
45 FilePath ati_file_path(kATIFileName);
46 if (!file_util::PathExists(ati_file_path))
47 return std::string();
48 std::string contents;
49 if (!file_util::ReadFileToString(ati_file_path, &contents))
50 return std::string();
51 StringTokenizer t(contents, "\r\n");
52 while (t.GetNext()) {
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);
60 else
61 return line.substr(begin, end - begin);
65 return std::string();
68 // Use NVCtrl extention to query NV driver version.
69 // Return empty string on failing.
70 std::string CollectDriverVersionNVidia() {
71 Display* display = base::MessagePumpForUI::GetDefaultXDisplay();
72 if (!display) {
73 LOG(ERROR) << "XOpenDisplay failed.";
74 return std::string();
76 int event_base = 0, error_base = 0;
77 if (!XNVCTRLQueryExtension(display, &event_base, &error_base)) {
78 LOG(INFO) << "NVCtrl extension does not exist.";
79 return std::string();
81 int screen_count = ScreenCount(display);
82 for (int screen = 0; screen < screen_count; ++screen) {
83 char* buffer = NULL;
84 if (XNVCTRLIsNvScreen(display, screen) &&
85 XNVCTRLQueryStringAttribute(display, screen, 0,
86 NV_CTRL_STRING_NVIDIA_DRIVER_VERSION,
87 &buffer)) {
88 std::string driver_version(buffer);
89 XFree(buffer);
90 return driver_version;
93 return std::string();
96 const uint32 kVendorIDIntel = 0x8086;
97 const uint32 kVendorIDNVidia = 0x10de;
98 const uint32 kVendorIDAMD = 0x1002;
100 bool CollectPCIVideoCardInfo(content::GPUInfo* gpu_info) {
101 DCHECK(gpu_info);
103 if (IsPciSupported() == false) {
104 VLOG(1) << "PCI bus scanning is not supported";
105 return false;
108 // TODO(zmo): be more flexible about library name.
109 LibPciLoader libpci_loader;
110 if (!libpci_loader.Load("libpci.so.3") &&
111 !libpci_loader.Load("libpci.so")) {
112 VLOG(1) << "Failed to locate libpci";
113 return false;
116 pci_access* access = (libpci_loader.pci_alloc)();
117 DCHECK(access != NULL);
118 (libpci_loader.pci_init)(access);
119 (libpci_loader.pci_scan_bus)(access);
120 bool primary_gpu_identified = false;
121 for (pci_dev* device = access->devices;
122 device != NULL; device = device->next) {
123 // Fill the IDs and class fields.
124 (libpci_loader.pci_fill_info)(device, 33);
125 // TODO(zmo): there might be other classes that qualify as display devices.
126 if (device->device_class != 0x0300) // Device class is DISPLAY_VGA.
127 continue;
129 content::GPUInfo::GPUDevice gpu;
130 gpu.vendor_id = device->vendor_id;
131 gpu.device_id = device->device_id;
133 const int buffer_size = 255;
134 scoped_array<char> buffer(new char[buffer_size]);
135 // The current implementation of pci_lookup_name returns the same pointer
136 // as the passed in upon success, and a different one (NULL or a pointer
137 // to an error message) upon failure.
138 if ((libpci_loader.pci_lookup_name)(access,
139 buffer.get(),
140 buffer_size,
142 device->vendor_id) == buffer.get()) {
143 gpu.vendor_string = buffer.get();
145 if ((libpci_loader.pci_lookup_name)(access,
146 buffer.get(),
147 buffer_size,
149 device->vendor_id,
150 device->device_id) == buffer.get()) {
151 std::string device_string = buffer.get();
152 size_t begin = device_string.find_first_of('[');
153 size_t end = device_string.find_last_of(']');
154 if (begin != std::string::npos && end != std::string::npos &&
155 begin < end) {
156 device_string = device_string.substr(begin + 1, end - begin - 1);
158 gpu.device_string = device_string;
161 if (!primary_gpu_identified) {
162 primary_gpu_identified = true;
163 gpu_info->gpu = gpu;
164 } else {
165 // TODO(zmo): if there are multiple GPUs, we assume the non Intel
166 // one is primary. Revisit this logic because we actually don't know
167 // which GPU we are using at this point.
168 if (gpu_info->gpu.vendor_id == kVendorIDIntel &&
169 gpu.vendor_id != kVendorIDIntel) {
170 gpu_info->secondary_gpus.push_back(gpu_info->gpu);
171 gpu_info->gpu = gpu;
172 } else {
173 gpu_info->secondary_gpus.push_back(gpu);
178 // Detect Optimus or AMD Switchable GPU.
179 if (gpu_info->secondary_gpus.size() == 1 &&
180 gpu_info->secondary_gpus[0].vendor_id == kVendorIDIntel) {
181 if (gpu_info->gpu.vendor_id == kVendorIDNVidia)
182 gpu_info->optimus = true;
183 if (gpu_info->gpu.vendor_id == kVendorIDAMD)
184 gpu_info->amd_switchable = true;
187 (libpci_loader.pci_cleanup)(access);
188 return (primary_gpu_identified);
191 } // namespace anonymous
193 namespace gpu_info_collector {
195 bool CollectContextGraphicsInfo(content::GPUInfo* gpu_info) {
196 DCHECK(gpu_info);
198 TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
200 if (CommandLine::ForCurrentProcess()->HasSwitch(
201 switches::kGpuNoContextLost)) {
202 gpu_info->can_lose_context = false;
203 } else {
204 #if defined(OS_CHROMEOS)
205 gpu_info->can_lose_context = false;
206 #else
207 // TODO(zmo): need to consider the case where we are running on top
208 // of desktop GL and GL_ARB_robustness extension is available.
209 gpu_info->can_lose_context =
210 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2);
211 #endif
214 gpu_info->finalized = true;
215 bool rt = CollectGraphicsInfoGL(gpu_info);
217 return rt;
220 bool CollectBasicGraphicsInfo(content::GPUInfo* gpu_info) {
221 DCHECK(gpu_info);
223 bool rt = CollectPCIVideoCardInfo(gpu_info);
225 std::string driver_version;
226 switch (gpu_info->gpu.vendor_id) {
227 case kVendorIDAMD:
228 driver_version = CollectDriverVersionATI();
229 if (!driver_version.empty()) {
230 gpu_info->driver_vendor = "ATI / AMD";
231 gpu_info->driver_version = driver_version;
233 break;
234 case kVendorIDNVidia:
235 driver_version = CollectDriverVersionNVidia();
236 if (!driver_version.empty()) {
237 gpu_info->driver_vendor = "NVIDIA";
238 gpu_info->driver_version = driver_version;
240 break;
241 case kVendorIDIntel:
242 // In dual-GPU cases, sometimes PCI scan only gives us the
243 // integrated GPU (i.e., the Intel one).
244 driver_version = CollectDriverVersionNVidia();
245 if (!driver_version.empty()) {
246 gpu_info->driver_vendor = "NVIDIA";
247 gpu_info->driver_version = driver_version;
248 // Machines with more than two GPUs are not handled.
249 if (gpu_info->secondary_gpus.size() <= 1)
250 gpu_info->optimus = true;
252 break;
255 return rt;
258 bool CollectDriverInfoGL(content::GPUInfo* gpu_info) {
259 DCHECK(gpu_info);
261 std::string gl_version_string = gpu_info->gl_version_string;
262 if (StartsWithASCII(gl_version_string, "OpenGL ES", true))
263 gl_version_string = gl_version_string.substr(10);
264 std::vector<std::string> pieces;
265 base::SplitStringAlongWhitespace(gl_version_string, &pieces);
266 // In linux, the gl version string might be in the format of
267 // GLVersion DriverVendor DriverVersion
268 if (pieces.size() < 3)
269 return false;
271 std::string driver_version = pieces[2];
272 size_t pos = driver_version.find_first_not_of("0123456789.");
273 if (pos == 0)
274 return false;
275 if (pos != std::string::npos)
276 driver_version = driver_version.substr(0, pos);
278 gpu_info->driver_vendor = pieces[1];
279 gpu_info->driver_version = driver_version;
280 return true;
283 void MergeGPUInfo(content::GPUInfo* basic_gpu_info,
284 const content::GPUInfo& context_gpu_info) {
285 MergeGPUInfoGL(basic_gpu_info, context_gpu_info);
288 } // namespace gpu_info_collector