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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
14 #include "ui/gl/gl_bindings.h" | 14 #include "ui/gl/gl_bindings.h" |
15 #include "ui/gl/gl_context.h" | 15 #include "ui/gl/gl_context.h" |
16 #include "ui/gl/gl_implementation.h" | 16 #include "ui/gl/gl_implementation.h" |
17 #include "ui/gl/gl_interface.h" | 17 #include "ui/gl/gl_interface.h" |
18 | 18 |
19 #import <Cocoa/Cocoa.h> | 19 #import <Cocoa/Cocoa.h> |
20 #import <Foundation/Foundation.h> | 20 #import <Foundation/Foundation.h> |
21 #import <IOKit/IOKitLib.h> | 21 #import <IOKit/IOKitLib.h> |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 struct VideoCardInfo { | 25 const UInt32 kVendorIDIntel = 0x8086; |
26 UInt32 vendor_id; | 26 const UInt32 kVendorIDNVidia = 0x10de; |
27 UInt32 device_id; | 27 const UInt32 kVendorIDAMD = 0x1002; |
28 | 28 |
29 VideoCardInfo(UInt32 vendor, UInt32 device) { | 29 // Return 0 if we couldn't find the property. |
30 vendor_id = vendor; | 30 // The property values we use should not be 0, so it's OK to use 0 as failure. |
31 device_id = device; | 31 UInt32 GetEntryProperty(io_registry_entry_t entry, CFStringRef property_name) { |
32 } | 32 base::mac::ScopedCFTypeRef<CFDataRef> data_ref(static_cast<CFDataRef>( |
33 }; | 33 IORegistryEntrySearchCFProperty(entry, |
34 | 34 kIOServicePlane, |
35 CFTypeRef SearchPortForProperty(io_registry_entry_t dspPort, | 35 property_name, |
36 CFStringRef propertyName) { | 36 kCFAllocatorDefault, |
37 return IORegistryEntrySearchCFProperty(dspPort, | 37 kIORegistryIterateRecursively | |
38 kIOServicePlane, | 38 kIORegistryIterateParents))); |
39 propertyName, | 39 if (!data_ref) |
40 kCFAllocatorDefault, | 40 return 0; |
41 kIORegistryIterateRecursively | | |
42 kIORegistryIterateParents); | |
43 } | |
44 | |
45 UInt32 IntValueOfCFData(CFDataRef data_ref) { | |
46 DCHECK(data_ref); | |
47 | 41 |
48 UInt32 value = 0; | 42 UInt32 value = 0; |
49 const UInt32* value_pointer = | 43 const UInt32* value_pointer = |
50 reinterpret_cast<const UInt32*>(CFDataGetBytePtr(data_ref)); | 44 reinterpret_cast<const UInt32*>(CFDataGetBytePtr(data_ref)); |
51 if (value_pointer != NULL) | 45 if (value_pointer != NULL) |
52 value = *value_pointer; | 46 value = *value_pointer; |
53 return value; | 47 return value; |
54 } | 48 } |
55 | 49 |
| 50 // Find the info of the current GPU. |
| 51 content::GPUInfo::GPUDevice GetActiveGPU() { |
| 52 content::GPUInfo::GPUDevice gpu; |
| 53 io_registry_entry_t dsp_port = CGDisplayIOServicePort(kCGDirectMainDisplay); |
| 54 gpu.vendor_id = GetEntryProperty(dsp_port, CFSTR("vendor-id")); |
| 55 gpu.device_id = GetEntryProperty(dsp_port, CFSTR("device-id")); |
| 56 return gpu; |
| 57 } |
| 58 |
56 // Scan IO registry for PCI video cards. | 59 // Scan IO registry for PCI video cards. |
57 // If two cards are located, assume the non-Intel card is the high-end | |
58 // one that's going to be used by Chromium GPU process. | |
59 // If more than two cards are located, return false. In such rare situation, | |
60 // video card information should be collected through identifying the currently | |
61 // in-use card as in CollectVideoCardInfo(). | |
62 bool CollectPCIVideoCardInfo(content::GPUInfo* gpu_info) { | 60 bool CollectPCIVideoCardInfo(content::GPUInfo* gpu_info) { |
63 DCHECK(gpu_info); | 61 DCHECK(gpu_info); |
64 | 62 |
| 63 // Collect all GPUs' info. |
65 // match_dictionary will be consumed by IOServiceGetMatchingServices, no need | 64 // match_dictionary will be consumed by IOServiceGetMatchingServices, no need |
66 // to release it. | 65 // to release it. |
67 CFMutableDictionaryRef match_dictionary = IOServiceMatching("IOPCIDevice"); | 66 CFMutableDictionaryRef match_dictionary = IOServiceMatching("IOPCIDevice"); |
68 io_iterator_t entry_iterator; | 67 io_iterator_t entry_iterator; |
| 68 std::vector<content::GPUInfo::GPUDevice> gpu_list; |
69 if (IOServiceGetMatchingServices(kIOMasterPortDefault, | 69 if (IOServiceGetMatchingServices(kIOMasterPortDefault, |
70 match_dictionary, | 70 match_dictionary, |
71 &entry_iterator) != kIOReturnSuccess) | 71 &entry_iterator) == kIOReturnSuccess) { |
72 return false; | 72 io_registry_entry_t entry; |
| 73 while ((entry = IOIteratorNext(entry_iterator))) { |
| 74 content::GPUInfo::GPUDevice gpu; |
| 75 if (GetEntryProperty(entry, CFSTR("class-code")) != 0x30000) { |
| 76 // 0x30000 : DISPLAY_VGA |
| 77 continue; |
| 78 } |
| 79 gpu.vendor_id = GetEntryProperty(entry, CFSTR("vendor-id")); |
| 80 gpu.device_id = GetEntryProperty(entry, CFSTR("device-id")); |
| 81 if (gpu.vendor_id && gpu.device_id) |
| 82 gpu_list.push_back(gpu); |
| 83 } |
| 84 IOObjectRelease(entry_iterator); |
| 85 } |
73 | 86 |
74 std::vector<VideoCardInfo> video_card_list; | 87 switch (gpu_list.size()) { |
75 io_registry_entry_t entry; | 88 case 0: |
76 while ((entry = IOIteratorNext(entry_iterator))) { | 89 return false; |
77 base::mac::ScopedCFTypeRef<CFDataRef> class_code_ref(static_cast<CFDataRef>( | |
78 SearchPortForProperty(entry, CFSTR("class-code")))); | |
79 if (!class_code_ref) | |
80 continue; | |
81 UInt32 class_code = IntValueOfCFData(class_code_ref); | |
82 if (class_code != 0x30000) // DISPLAY_VGA | |
83 continue; | |
84 base::mac::ScopedCFTypeRef<CFDataRef> vendor_id_ref(static_cast<CFDataRef>( | |
85 SearchPortForProperty(entry, CFSTR("vendor-id")))); | |
86 if (!vendor_id_ref) | |
87 continue; | |
88 UInt32 vendor_id = IntValueOfCFData(vendor_id_ref); | |
89 base::mac::ScopedCFTypeRef<CFDataRef> device_id_ref(static_cast<CFDataRef>( | |
90 SearchPortForProperty(entry, CFSTR("device-id")))); | |
91 if (!device_id_ref) | |
92 continue; | |
93 UInt32 device_id = IntValueOfCFData(device_id_ref); | |
94 video_card_list.push_back(VideoCardInfo(vendor_id, device_id)); | |
95 } | |
96 IOObjectRelease(entry_iterator); | |
97 | |
98 const UInt32 kIntelVendorId = 0x8086; | |
99 size_t found = video_card_list.size(); | |
100 switch (video_card_list.size()) { | |
101 case 1: | 90 case 1: |
102 found = 0; | 91 gpu_info->gpu = gpu_list[0]; |
103 break; | 92 break; |
104 case 2: | 93 case 2: |
105 if (video_card_list[0].vendor_id == kIntelVendorId && | 94 { |
106 video_card_list[1].vendor_id != kIntelVendorId) | 95 int integrated = -1; |
107 found = 1; | 96 int discrete = -1; |
108 else if (video_card_list[0].vendor_id != kIntelVendorId && | 97 if (gpu_list[0].vendor_id == kVendorIDIntel) |
109 video_card_list[1].vendor_id == kIntelVendorId) | 98 integrated = 0; |
110 found = 0; | 99 else if (gpu_list[1].vendor_id == kVendorIDIntel) |
| 100 integrated = 1; |
| 101 if (integrated >= 0) { |
| 102 switch (gpu_list[1 - integrated].vendor_id) { |
| 103 case kVendorIDAMD: |
| 104 gpu_info->amd_switchable = true; |
| 105 discrete = 1 - integrated; |
| 106 break; |
| 107 case kVendorIDNVidia: |
| 108 gpu_info->optimus = true; |
| 109 discrete = 1 - integrated; |
| 110 break; |
| 111 default: |
| 112 break; |
| 113 } |
| 114 } |
| 115 if (integrated >= 0 && discrete >= 0) { |
| 116 // We always put discrete GPU as primary for blacklisting purpose. |
| 117 gpu_info->gpu = gpu_list[discrete]; |
| 118 gpu_info->secondary_gpus.push_back(gpu_list[integrated]); |
| 119 break; |
| 120 } |
| 121 // If it's not optimus or amd_switchable, we put the current GPU as |
| 122 // primary. Fall through to default. |
| 123 } |
| 124 default: |
| 125 { |
| 126 content::GPUInfo::GPUDevice active_gpu = GetActiveGPU(); |
| 127 size_t current = gpu_list.size(); |
| 128 if (active_gpu.vendor_id && active_gpu.device_id) { |
| 129 for (size_t i = 0; i < gpu_list.size(); ++i) { |
| 130 if (gpu_list[i].vendor_id == active_gpu.vendor_id && |
| 131 gpu_list[i].device_id == active_gpu.device_id) { |
| 132 current = i; |
| 133 break; |
| 134 } |
| 135 } |
| 136 } |
| 137 if (current == gpu_list.size()) { |
| 138 // If we fail to identify the current GPU, select any one as primary. |
| 139 current = 0; |
| 140 } |
| 141 for (size_t i = 0; i < gpu_list.size(); ++i) { |
| 142 if (i == current) |
| 143 gpu_info->gpu = gpu_list[i]; |
| 144 else |
| 145 gpu_info->secondary_gpus.push_back(gpu_list[i]); |
| 146 } |
| 147 } |
111 break; | 148 break; |
112 } | 149 } |
113 if (found < video_card_list.size()) { | 150 return (gpu_info->gpu.vendor_id && gpu_info->gpu.device_id); |
114 gpu_info->gpu.vendor_id = video_card_list[found].vendor_id; | |
115 gpu_info->gpu.device_id = video_card_list[found].device_id; | |
116 return true; | |
117 } | |
118 return false; | |
119 } | 151 } |
120 | 152 |
121 } // namespace anonymous | 153 } // namespace anonymous |
122 | 154 |
123 namespace gpu_info_collector { | 155 namespace gpu_info_collector { |
124 | 156 |
125 bool CollectGraphicsInfo(content::GPUInfo* gpu_info) { | 157 bool CollectGraphicsInfo(content::GPUInfo* gpu_info) { |
126 DCHECK(gpu_info); | 158 DCHECK(gpu_info); |
127 | 159 |
128 gpu_info->can_lose_context = | 160 gpu_info->can_lose_context = |
129 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2); | 161 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2); |
130 gpu_info->finalized = true; | 162 gpu_info->finalized = true; |
131 return CollectGraphicsInfoGL(gpu_info); | 163 return CollectGraphicsInfoGL(gpu_info); |
132 } | 164 } |
133 | 165 |
134 bool CollectPreliminaryGraphicsInfo(content::GPUInfo* gpu_info) { | 166 bool CollectPreliminaryGraphicsInfo(content::GPUInfo* gpu_info) { |
135 DCHECK(gpu_info); | 167 DCHECK(gpu_info); |
136 | 168 |
137 bool rt = true; | 169 return CollectPCIVideoCardInfo(gpu_info); |
138 if (!CollectPCIVideoCardInfo(gpu_info) && !CollectVideoCardInfo(gpu_info)) | |
139 rt = false; | |
140 | |
141 return rt; | |
142 } | 170 } |
143 | 171 |
144 bool CollectVideoCardInfo(content::GPUInfo* gpu_info) { | 172 bool CollectVideoCardInfo(content::GPUInfo* gpu_info) { |
145 DCHECK(gpu_info); | 173 return CollectPreliminaryGraphicsInfo(gpu_info); |
146 | |
147 UInt32 vendor_id = 0, device_id = 0; | |
148 io_registry_entry_t dsp_port = CGDisplayIOServicePort(kCGDirectMainDisplay); | |
149 CFTypeRef vendor_id_ref = SearchPortForProperty(dsp_port, CFSTR("vendor-id")); | |
150 if (vendor_id_ref) { | |
151 vendor_id = IntValueOfCFData((CFDataRef)vendor_id_ref); | |
152 CFRelease(vendor_id_ref); | |
153 } | |
154 CFTypeRef device_id_ref = SearchPortForProperty(dsp_port, CFSTR("device-id")); | |
155 if (device_id_ref) { | |
156 device_id = IntValueOfCFData((CFDataRef)device_id_ref); | |
157 CFRelease(device_id_ref); | |
158 } | |
159 | |
160 gpu_info->gpu.vendor_id = vendor_id; | |
161 gpu_info->gpu.device_id = device_id; | |
162 return true; | |
163 } | 174 } |
164 | 175 |
165 bool CollectDriverInfoGL(content::GPUInfo* gpu_info) { | 176 bool CollectDriverInfoGL(content::GPUInfo* gpu_info) { |
166 DCHECK(gpu_info); | 177 DCHECK(gpu_info); |
167 | 178 |
168 // Extract the OpenGL driver version string from the GL_VERSION string. | 179 // Extract the OpenGL driver version string from the GL_VERSION string. |
169 // Mac OpenGL drivers have the driver version | 180 // Mac OpenGL drivers have the driver version |
170 // at the end of the gl version string preceded by a dash. | 181 // at the end of the gl version string preceded by a dash. |
171 // Use some jiggery-pokery to turn that utf8 string into a std::wstring. | 182 // Use some jiggery-pokery to turn that utf8 string into a std::wstring. |
172 std::string gl_version_string = gpu_info->gl_version_string; | 183 std::string gl_version_string = gpu_info->gl_version_string; |
173 size_t pos = gl_version_string.find_last_of('-'); | 184 size_t pos = gl_version_string.find_last_of('-'); |
174 if (pos == std::string::npos) | 185 if (pos == std::string::npos) |
175 return false; | 186 return false; |
176 gpu_info->driver_version = gl_version_string.substr(pos + 1); | 187 gpu_info->driver_version = gl_version_string.substr(pos + 1); |
177 return true; | 188 return true; |
178 } | 189 } |
179 | 190 |
180 } // namespace gpu_info_collector | 191 } // namespace gpu_info_collector |
OLD | NEW |