547f6a87170978ab3a17ed46a1f95273f7e96ce9
[platform/framework/web/crosswalk.git] / src / gpu / config / gpu_info_collector_mac.mm
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 <vector>
8
9 #include "base/debug/trace_event.h"
10 #include "base/logging.h"
11 #include "base/mac/mac_util.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "ui/gl/gl_bindings.h"
19 #include "ui/gl/gl_context.h"
20 #include "ui/gl/gl_implementation.h"
21
22 #import <Cocoa/Cocoa.h>
23 #import <Foundation/Foundation.h>
24 #import <IOKit/IOKitLib.h>
25
26 namespace gpu {
27
28 namespace {
29
30 const UInt32 kVendorIDIntel = 0x8086;
31 const UInt32 kVendorIDNVidia = 0x10de;
32 const UInt32 kVendorIDAMD = 0x1002;
33
34 // Return 0 if we couldn't find the property.
35 // The property values we use should not be 0, so it's OK to use 0 as failure.
36 UInt32 GetEntryProperty(io_registry_entry_t entry, CFStringRef property_name) {
37   base::ScopedCFTypeRef<CFDataRef> data_ref(
38       static_cast<CFDataRef>(IORegistryEntrySearchCFProperty(
39           entry,
40           kIOServicePlane,
41           property_name,
42           kCFAllocatorDefault,
43           kIORegistryIterateRecursively | kIORegistryIterateParents)));
44   if (!data_ref)
45     return 0;
46
47   UInt32 value = 0;
48   const UInt32* value_pointer =
49       reinterpret_cast<const UInt32*>(CFDataGetBytePtr(data_ref));
50   if (value_pointer != NULL)
51     value = *value_pointer;
52   return value;
53 }
54
55 // Find the info of the current GPU.
56 GPUInfo::GPUDevice GetActiveGPU() {
57   GPUInfo::GPUDevice gpu;
58   io_registry_entry_t dsp_port = CGDisplayIOServicePort(kCGDirectMainDisplay);
59   gpu.vendor_id = GetEntryProperty(dsp_port, CFSTR("vendor-id"));
60   gpu.device_id = GetEntryProperty(dsp_port, CFSTR("device-id"));
61   return gpu;
62 }
63
64 // Scan IO registry for PCI video cards.
65 bool CollectPCIVideoCardInfo(GPUInfo* gpu_info) {
66   DCHECK(gpu_info);
67
68   // Collect all GPUs' info.
69   // match_dictionary will be consumed by IOServiceGetMatchingServices, no need
70   // to release it.
71   CFMutableDictionaryRef match_dictionary = IOServiceMatching("IOPCIDevice");
72   io_iterator_t entry_iterator;
73   std::vector<GPUInfo::GPUDevice> gpu_list;
74   if (IOServiceGetMatchingServices(kIOMasterPortDefault,
75                                    match_dictionary,
76                                    &entry_iterator) == kIOReturnSuccess) {
77     io_registry_entry_t entry;
78     while ((entry = IOIteratorNext(entry_iterator))) {
79       GPUInfo::GPUDevice gpu;
80       if (GetEntryProperty(entry, CFSTR("class-code")) != 0x30000) {
81         // 0x30000 : DISPLAY_VGA
82         continue;
83       }
84       gpu.vendor_id = GetEntryProperty(entry, CFSTR("vendor-id"));
85       gpu.device_id = GetEntryProperty(entry, CFSTR("device-id"));
86       if (gpu.vendor_id && gpu.device_id)
87         gpu_list.push_back(gpu);
88     }
89     IOObjectRelease(entry_iterator);
90   }
91
92   switch (gpu_list.size()) {
93     case 0:
94       return false;
95     case 1:
96       gpu_info->gpu = gpu_list[0];
97       break;
98     case 2:
99       {
100         int integrated = -1;
101         int discrete = -1;
102         if (gpu_list[0].vendor_id == kVendorIDIntel)
103           integrated = 0;
104         else if (gpu_list[1].vendor_id == kVendorIDIntel)
105           integrated = 1;
106         if (integrated >= 0) {
107           switch (gpu_list[1 - integrated].vendor_id) {
108             case kVendorIDAMD:
109               gpu_info->amd_switchable = true;
110               discrete = 1 - integrated;
111               break;
112             case kVendorIDNVidia:
113               gpu_info->optimus = true;
114               discrete = 1 - integrated;
115               break;
116             default:
117               break;
118           }
119         }
120         if (integrated >= 0 && discrete >= 0) {
121           // We always put discrete GPU as primary for blacklisting purpose.
122           gpu_info->gpu = gpu_list[discrete];
123           gpu_info->secondary_gpus.push_back(gpu_list[integrated]);
124           break;
125         }
126         // If it's not optimus or amd_switchable, we put the current GPU as
127         // primary.  Fall through to default.
128       }
129     default:
130       {
131         GPUInfo::GPUDevice active_gpu = GetActiveGPU();
132         size_t current = gpu_list.size();
133         if (active_gpu.vendor_id && active_gpu.device_id) {
134           for (size_t i = 0; i < gpu_list.size(); ++i) {
135             if (gpu_list[i].vendor_id == active_gpu.vendor_id &&
136                 gpu_list[i].device_id == active_gpu.device_id) {
137               current = i;
138               break;
139             }
140           }
141         }
142         if (current == gpu_list.size()) {
143           // If we fail to identify the current GPU, select any one as primary.
144           current = 0;
145         }
146         for (size_t i = 0; i < gpu_list.size(); ++i) {
147           if (i == current)
148             gpu_info->gpu = gpu_list[i];
149           else
150             gpu_info->secondary_gpus.push_back(gpu_list[i]);
151         }
152       }
153       break;
154   }
155   return (gpu_info->gpu.vendor_id && gpu_info->gpu.device_id);
156 }
157
158 }  // namespace anonymous
159
160 CollectInfoResult CollectContextGraphicsInfo(GPUInfo* gpu_info) {
161   DCHECK(gpu_info);
162
163   TRACE_EVENT0("gpu", "gpu_info_collector::CollectGraphicsInfo");
164
165   gpu_info->can_lose_context =
166       (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2);
167   gpu_info->finalized = true;
168   return CollectGraphicsInfoGL(gpu_info);
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     return kGpuIDSuccess;
181   }
182   return kGpuIDFailure;
183 }
184
185 CollectInfoResult CollectBasicGraphicsInfo(GPUInfo* gpu_info) {
186   DCHECK(gpu_info);
187
188   std::string model_name;
189   int32 model_major = 0, model_minor = 0;
190   base::mac::ParseModelIdentifier(base::mac::GetModelIdentifier(),
191                                   &model_name, &model_major, &model_minor);
192   base::ReplaceChars(model_name, " ", "_", &gpu_info->machine_model);
193   gpu_info->machine_model += " " + base::IntToString(model_major) +
194                              "." + base::IntToString(model_minor);
195
196   bool result = CollectPCIVideoCardInfo(gpu_info);
197   return result ? kCollectInfoSuccess : kCollectInfoNonFatalFailure;
198 }
199
200 CollectInfoResult CollectDriverInfoGL(GPUInfo* gpu_info) {
201   DCHECK(gpu_info);
202
203   // Extract the OpenGL driver version string from the GL_VERSION string.
204   // Mac OpenGL drivers have the driver version
205   // at the end of the gl version string preceded by a dash.
206   // Use some jiggery-pokery to turn that utf8 string into a std::wstring.
207   std::string gl_version_string = gpu_info->gl_version_string;
208   size_t pos = gl_version_string.find_last_of('-');
209   if (pos == std::string::npos)
210     return kCollectInfoNonFatalFailure;
211   gpu_info->driver_version = gl_version_string.substr(pos + 1);
212   return kCollectInfoSuccess;
213 }
214
215 void MergeGPUInfo(GPUInfo* basic_gpu_info,
216                   const GPUInfo& context_gpu_info) {
217   MergeGPUInfoGL(basic_gpu_info, context_gpu_info);
218 }
219
220 }  // namespace gpu