Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / gpu / config / gpu_info_collector_x11.cc
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.
4
5 #include "gpu/config/gpu_info_collector.h"
6
7 #include <X11/Xlib.h>
8 #include <vector>
9
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"
29
30 namespace gpu {
31
32 namespace {
33
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));
41 }
42
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))
50     return std::string();
51   std::string contents;
52   if (!base::ReadFileToString(ati_file_path, &contents))
53     return std::string();
54   base::StringTokenizer t(contents, "\r\n");
55   while (t.GetNext()) {
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);
63         else
64           return line.substr(begin, end - begin);
65       }
66     }
67   }
68   return std::string();
69 }
70
71 // Use NVCtrl extention to query NV driver version.
72 // Return empty string on failing.
73 std::string CollectDriverVersionNVidia() {
74   Display* display = gfx::GetXDisplay();
75   if (!display) {
76     LOG(ERROR) << "XOpenDisplay failed.";
77     return std::string();
78   }
79   int event_base = 0, error_base = 0;
80   if (!XNVCTRLQueryExtension(display, &event_base, &error_base)) {
81     VLOG(1) << "NVCtrl extension does not exist.";
82     return std::string();
83   }
84   int screen_count = ScreenCount(display);
85   for (int screen = 0; screen < screen_count; ++screen) {
86     char* buffer = NULL;
87     if (XNVCTRLIsNvScreen(display, screen) &&
88         XNVCTRLQueryStringAttribute(display, screen, 0,
89                                     NV_CTRL_STRING_NVIDIA_DRIVER_VERSION,
90                                     &buffer)) {
91       std::string driver_version(buffer);
92       XFree(buffer);
93       return driver_version;
94     }
95   }
96   return std::string();
97 }
98
99 const uint32 kVendorIDIntel = 0x8086;
100 const uint32 kVendorIDNVidia = 0x10de;
101 const uint32 kVendorIDAMD = 0x1002;
102
103 bool CollectPCIVideoCardInfo(GPUInfo* gpu_info) {
104   DCHECK(gpu_info);
105
106   if (IsPciSupported() == false) {
107     VLOG(1) << "PCI bus scanning is not supported";
108     return false;
109   }
110
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";
116     return false;
117   }
118
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);
128     bool is_gpu = false;
129     switch (device->device_class) {
130       case PCI_CLASS_DISPLAY_VGA:
131       case PCI_CLASS_DISPLAY_XGA:
132       case PCI_CLASS_DISPLAY_3D:
133         is_gpu = true;
134         break;
135       case PCI_CLASS_DISPLAY_OTHER:
136       default:
137         break;
138     }
139     if (!is_gpu)
140       continue;
141
142     GPUInfo::GPUDevice gpu;
143     gpu.vendor_id = device->vendor_id;
144     gpu.device_id = device->device_id;
145
146     if (!primary_gpu_identified) {
147       primary_gpu_identified = true;
148       gpu_info->gpu = gpu;
149     } else {
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);
156         gpu_info->gpu = gpu;
157       } else {
158         gpu_info->secondary_gpus.push_back(gpu);
159       }
160     }
161   }
162
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;
170   }
171
172   (libpci_loader.pci_cleanup)(access);
173   return (primary_gpu_identified);
174 }
175
176 }  // namespace anonymous
177
178 CollectInfoResult CollectContextGraphicsInfo(GPUInfo* gpu_info) {
179   DCHECK(gpu_info);
180
181   TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
182
183   if (CommandLine::ForCurrentProcess()->HasSwitch(
184           switches::kGpuNoContextLost)) {
185     gpu_info->can_lose_context = false;
186   } else {
187 #if defined(OS_CHROMEOS)
188     gpu_info->can_lose_context = false;
189 #else
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);
194 #endif
195   }
196
197   CollectInfoResult result = CollectGraphicsInfoGL(gpu_info);
198   gpu_info->finalized = true;
199   return result;
200 }
201
202 GpuIDResult CollectGpuID(uint32* vendor_id, uint32* device_id) {
203   DCHECK(vendor_id && device_id);
204   *vendor_id = 0;
205   *device_id = 0;
206
207   GPUInfo gpu_info;
208   if (CollectPCIVideoCardInfo(&gpu_info)) {
209     *vendor_id = gpu_info.gpu.vendor_id;
210     *device_id = gpu_info.gpu.device_id;
211     return kGpuIDSuccess;
212   }
213   return kGpuIDFailure;
214 }
215
216 CollectInfoResult CollectBasicGraphicsInfo(GPUInfo* gpu_info) {
217   DCHECK(gpu_info);
218
219   bool rt = CollectPCIVideoCardInfo(gpu_info);
220
221   std::string driver_version;
222   switch (gpu_info->gpu.vendor_id) {
223     case kVendorIDAMD:
224       driver_version = CollectDriverVersionATI();
225       if (!driver_version.empty()) {
226         gpu_info->driver_vendor = "ATI / AMD";
227         gpu_info->driver_version = driver_version;
228       }
229       break;
230     case kVendorIDNVidia:
231       driver_version = CollectDriverVersionNVidia();
232       if (!driver_version.empty()) {
233         gpu_info->driver_vendor = "NVIDIA";
234         gpu_info->driver_version = driver_version;
235       }
236       break;
237     case kVendorIDIntel:
238       // In dual-GPU cases, sometimes PCI scan only gives us the
239       // integrated GPU (i.e., the Intel one).
240       driver_version = CollectDriverVersionNVidia();
241       if (!driver_version.empty()) {
242         gpu_info->driver_vendor = "NVIDIA";
243         gpu_info->driver_version = driver_version;
244         // Machines with more than two GPUs are not handled.
245         if (gpu_info->secondary_gpus.size() <= 1)
246           gpu_info->optimus = true;
247       }
248       break;
249   }
250
251   return rt ? kCollectInfoSuccess : kCollectInfoNonFatalFailure;
252 }
253
254 CollectInfoResult CollectDriverInfoGL(GPUInfo* gpu_info) {
255   DCHECK(gpu_info);
256
257   std::string gl_version = gpu_info->gl_version;
258   if (StartsWithASCII(gl_version, "OpenGL ES", true))
259     gl_version = gl_version.substr(10);
260   std::vector<std::string> pieces;
261   base::SplitStringAlongWhitespace(gl_version, &pieces);
262   // In linux, the gl version string might be in the format of
263   //   GLVersion DriverVendor DriverVersion
264   if (pieces.size() < 3)
265     return kCollectInfoNonFatalFailure;
266
267   std::string driver_version = pieces[2];
268   size_t pos = driver_version.find_first_not_of("0123456789.");
269   if (pos == 0)
270     return kCollectInfoNonFatalFailure;
271   if (pos != std::string::npos)
272     driver_version = driver_version.substr(0, pos);
273
274   gpu_info->driver_vendor = pieces[1];
275   gpu_info->driver_version = driver_version;
276   return kCollectInfoSuccess;
277 }
278
279 void MergeGPUInfo(GPUInfo* basic_gpu_info,
280                   const GPUInfo& context_gpu_info) {
281   MergeGPUInfoGL(basic_gpu_info, context_gpu_info);
282 }
283
284 }  // namespace gpu