Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / gpu / config / gpu_info_collector_linux.cc
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.
4
5 #include "gpu/config/gpu_info_collector_linux.h"
6
7 #include <vector>
8
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"
26
27 namespace gpu {
28
29 namespace {
30
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));
38 }
39
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))
47     return std::string();
48   std::string contents;
49   if (!base::ReadFileToString(ati_file_path, &contents))
50     return std::string();
51   base::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);
62       }
63     }
64   }
65   return std::string();
66 }
67
68 const uint32 kVendorIDIntel = 0x8086;
69 const uint32 kVendorIDNVidia = 0x10de;
70 const uint32 kVendorIDAMD = 0x1002;
71
72 bool CollectPCIVideoCardInfo(GPUInfo* gpu_info) {
73   DCHECK(gpu_info);
74
75   if (IsPciSupported() == false) {
76     VLOG(1) << "PCI bus scanning is not supported";
77     return false;
78   }
79
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";
85     return false;
86   }
87
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);
97     bool is_gpu = false;
98     switch (device->device_class) {
99       case PCI_CLASS_DISPLAY_VGA:
100       case PCI_CLASS_DISPLAY_XGA:
101       case PCI_CLASS_DISPLAY_3D:
102         is_gpu = true;
103         break;
104       case PCI_CLASS_DISPLAY_OTHER:
105       default:
106         break;
107     }
108     if (!is_gpu)
109       continue;
110
111     GPUInfo::GPUDevice gpu;
112     gpu.vendor_id = device->vendor_id;
113     gpu.device_id = device->device_id;
114
115     if (!primary_gpu_identified) {
116       primary_gpu_identified = true;
117       gpu_info->gpu = gpu;
118     } else {
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);
125         gpu_info->gpu = gpu;
126       } else {
127         gpu_info->secondary_gpus.push_back(gpu);
128       }
129     }
130   }
131
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;
139   }
140
141   (libpci_loader.pci_cleanup)(access);
142   return (primary_gpu_identified);
143 }
144
145 }  // namespace anonymous
146
147 CollectInfoResult CollectContextGraphicsInfo(GPUInfo* gpu_info) {
148   DCHECK(gpu_info);
149
150   TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
151
152   if (CommandLine::ForCurrentProcess()->HasSwitch(
153           switches::kGpuNoContextLost)) {
154     gpu_info->can_lose_context = false;
155   } else {
156 #if defined(OS_CHROMEOS)
157     gpu_info->can_lose_context = false;
158 #else
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);
163 #endif
164   }
165
166   CollectInfoResult result = CollectGraphicsInfoGL(gpu_info);
167   gpu_info->finalized = true;
168   return result;
169 }
170
171 GpuIDResult CollectGpuID(uint32* vendor_id, uint32* device_id) {
172   DCHECK(vendor_id && device_id);
173   *vendor_id = 0;
174   *device_id = 0;
175
176   GPUInfo gpu_info;
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;
182   }
183   return kGpuIDFailure;
184 }
185
186 CollectInfoResult CollectBasicGraphicsInfo(GPUInfo* gpu_info) {
187   DCHECK(gpu_info);
188
189   bool rt = CollectPCIVideoCardInfo(gpu_info);
190
191   std::string driver_version;
192   switch (gpu_info->gpu.vendor_id) {
193     case kVendorIDAMD:
194       driver_version = CollectDriverVersionATI();
195       if (!driver_version.empty()) {
196         gpu_info->driver_vendor = "ATI / AMD";
197         gpu_info->driver_version = driver_version;
198       }
199       break;
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;
205       }
206       break;
207     case kVendorIDIntel:
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.
221         }
222       }
223       break;
224   }
225
226   return rt ? kCollectInfoSuccess : kCollectInfoNonFatalFailure;
227 }
228
229 CollectInfoResult CollectDriverInfoGL(GPUInfo* gpu_info) {
230   DCHECK(gpu_info);
231
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;
241
242   std::string driver_version = pieces[2];
243   size_t pos = driver_version.find_first_not_of("0123456789.");
244   if (pos == 0)
245     return kCollectInfoNonFatalFailure;
246   if (pos != std::string::npos)
247     driver_version = driver_version.substr(0, pos);
248
249   gpu_info->driver_vendor = pieces[1];
250   gpu_info->driver_version = driver_version;
251   return kCollectInfoSuccess;
252 }
253
254 void MergeGPUInfo(GPUInfo* basic_gpu_info,
255                   const GPUInfo& context_gpu_info) {
256   MergeGPUInfoGL(basic_gpu_info, context_gpu_info);
257 }
258
259 }  // namespace gpu