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/gfx/gl/gl_bindings.h" | 14 #include "ui/gfx/gl/gl_bindings.h" |
15 #include "ui/gfx/gl/gl_context.h" | 15 #include "ui/gfx/gl/gl_context.h" |
16 #include "ui/gfx/gl/gl_implementation.h" | 16 #include "ui/gfx/gl/gl_implementation.h" |
17 #include "ui/gfx/gl/gl_interface.h" | 17 #include "ui/gfx/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 kVendorIDAMD = 0x1002; |
27 UInt32 device_id; | |
28 | |
29 VideoCardInfo(UInt32 vendor, UInt32 device) { | |
30 vendor_id = vendor; | |
31 device_id = device; | |
32 } | |
33 }; | |
34 | 27 |
35 CFTypeRef SearchPortForProperty(io_registry_entry_t dspPort, | 28 CFTypeRef SearchPortForProperty(io_registry_entry_t dspPort, |
36 CFStringRef propertyName) { | 29 CFStringRef propertyName) { |
37 return IORegistryEntrySearchCFProperty(dspPort, | 30 return IORegistryEntrySearchCFProperty(dspPort, |
38 kIOServicePlane, | 31 kIOServicePlane, |
39 propertyName, | 32 propertyName, |
40 kCFAllocatorDefault, | 33 kCFAllocatorDefault, |
41 kIORegistryIterateRecursively | | 34 kIORegistryIterateRecursively | |
42 kIORegistryIterateParents); | 35 kIORegistryIterateParents); |
43 } | 36 } |
44 | 37 |
45 UInt32 IntValueOfCFData(CFDataRef data_ref) { | 38 UInt32 IntValueOfCFData(CFDataRef data_ref) { |
46 DCHECK(data_ref); | 39 DCHECK(data_ref); |
47 | 40 |
48 UInt32 value = 0; | 41 UInt32 value = 0; |
49 const UInt32* value_pointer = | 42 const UInt32* value_pointer = |
50 reinterpret_cast<const UInt32*>(CFDataGetBytePtr(data_ref)); | 43 reinterpret_cast<const UInt32*>(CFDataGetBytePtr(data_ref)); |
51 if (value_pointer != NULL) | 44 if (value_pointer != NULL) |
52 value = *value_pointer; | 45 value = *value_pointer; |
53 return value; | 46 return value; |
54 } | 47 } |
55 | 48 |
56 // Scan IO registry for PCI video cards. | 49 // 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) { | 50 bool CollectPCIVideoCardInfo(content::GPUInfo* gpu_info) { |
63 DCHECK(gpu_info); | 51 DCHECK(gpu_info); |
64 | 52 |
53 // Collect the active GPU's info. | |
54 io_registry_entry_t dsp_port = CGDisplayIOServicePort(kCGDirectMainDisplay); | |
55 CFTypeRef vendor_id_ref = SearchPortForProperty(dsp_port, CFSTR("vendor-id")); | |
56 if (vendor_id_ref) { | |
57 gpu_info->gpu.vendor_id = IntValueOfCFData((CFDataRef)vendor_id_ref); | |
58 CFRelease(vendor_id_ref); | |
59 } | |
60 CFTypeRef device_id_ref = SearchPortForProperty(dsp_port, CFSTR("device-id")); | |
61 if (device_id_ref) { | |
62 gpu_info->gpu.device_id = IntValueOfCFData((CFDataRef)device_id_ref); | |
63 CFRelease(device_id_ref); | |
64 } | |
65 | |
66 // Collect all GPUs' info. | |
65 // match_dictionary will be consumed by IOServiceGetMatchingServices, no need | 67 // match_dictionary will be consumed by IOServiceGetMatchingServices, no need |
66 // to release it. | 68 // to release it. |
67 CFMutableDictionaryRef match_dictionary = IOServiceMatching("IOPCIDevice"); | 69 CFMutableDictionaryRef match_dictionary = IOServiceMatching("IOPCIDevice"); |
68 io_iterator_t entry_iterator; | 70 io_iterator_t entry_iterator; |
69 if (IOServiceGetMatchingServices(kIOMasterPortDefault, | 71 if (IOServiceGetMatchingServices(kIOMasterPortDefault, |
70 match_dictionary, | 72 match_dictionary, |
71 &entry_iterator) != kIOReturnSuccess) | 73 &entry_iterator) == kIOReturnSuccess) { |
72 return false; | 74 io_registry_entry_t entry; |
75 while ((entry = IOIteratorNext(entry_iterator))) { | |
Ken Russell (switch to Gerrit)
2012/05/11 22:46:15
Because of the asymmetry between GPUInfo::gpu and
Zhenyao Mo
2012/05/14 18:41:11
Done.
| |
76 content::GPUInfo::GPUDevice gpu; | |
77 base::mac::ScopedCFTypeRef<CFDataRef> class_code_ref( | |
78 static_cast<CFDataRef>(SearchPortForProperty(entry, | |
79 CFSTR("class-code")))); | |
Ken Russell (switch to Gerrit)
2012/05/11 22:46:15
There's a lot of boilerplate code duplicated in th
Zhenyao Mo
2012/05/14 18:41:11
Done.
| |
80 if (!class_code_ref) | |
81 continue; | |
82 UInt32 class_code = IntValueOfCFData(class_code_ref); | |
83 if (class_code != 0x30000) // DISPLAY_VGA | |
84 continue; | |
85 base::mac::ScopedCFTypeRef<CFDataRef> vendor_id_ref( | |
86 static_cast<CFDataRef>(SearchPortForProperty(entry, | |
87 CFSTR("vendor-id")))); | |
88 if (!vendor_id_ref) | |
89 continue; | |
90 gpu.vendor_id = IntValueOfCFData(vendor_id_ref); | |
91 base::mac::ScopedCFTypeRef<CFDataRef> device_id_ref( | |
92 static_cast<CFDataRef>(SearchPortForProperty(entry, | |
93 CFSTR("device-id")))); | |
94 if (!device_id_ref) | |
95 continue; | |
96 gpu.device_id = IntValueOfCFData(device_id_ref); | |
97 if (gpu.vendor_id != gpu_info->gpu.vendor_id || | |
98 gpu.device_id != gpu_info->gpu.device_id) | |
99 gpu_info->secondary_gpus.push_back(gpu); | |
100 } | |
101 IOObjectRelease(entry_iterator); | |
102 } | |
73 | 103 |
74 std::vector<VideoCardInfo> video_card_list; | 104 if (gpu_info->secondary_gpus.size() == 1) { |
75 io_registry_entry_t entry; | 105 if ((gpu_info->gpu.vendor_id == kVendorIDAMD && |
76 while ((entry = IOIteratorNext(entry_iterator))) { | 106 gpu_info->secondary_gpus[0].vendor_id == kVendorIDIntel) || |
77 base::mac::ScopedCFTypeRef<CFDataRef> class_code_ref(static_cast<CFDataRef>( | 107 (gpu_info->gpu.vendor_id == kVendorIDIntel && |
78 SearchPortForProperty(entry, CFSTR("class-code")))); | 108 gpu_info->secondary_gpus[0].vendor_id == kVendorIDAMD)) { |
Ken Russell (switch to Gerrit)
2012/05/11 22:46:15
This must be generalized for non-AMD GPUs, or blac
Zhenyao Mo
2012/05/14 18:41:11
Is optimus supported on Mac also? Anyway, I imple
Ken Russell (switch to Gerrit)
2012/05/15 18:08:22
I believe that Apple implements their own GPU-swit
| |
79 if (!class_code_ref) | 109 gpu_info->amd_switchable = true; |
80 continue; | 110 if (gpu_info->gpu.vendor_id == kVendorIDIntel) { |
81 UInt32 class_code = IntValueOfCFData(class_code_ref); | 111 // We need to put AMD as primary for blacklist purpose. |
82 if (class_code != 0x30000) // DISPLAY_VGA | 112 content::GPUInfo::GPUDevice gpu = gpu_info->gpu; |
83 continue; | 113 gpu_info->gpu = gpu_info->secondary_gpus[0]; |
84 base::mac::ScopedCFTypeRef<CFDataRef> vendor_id_ref(static_cast<CFDataRef>( | 114 gpu_info->secondary_gpus[0] = gpu; |
85 SearchPortForProperty(entry, CFSTR("vendor-id")))); | 115 } |
86 if (!vendor_id_ref) | 116 } |
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 } | 117 } |
96 IOObjectRelease(entry_iterator); | 118 return (gpu_info->gpu.vendor_id && gpu_info->gpu.device_id); |
97 | |
98 const UInt32 kIntelVendorId = 0x8086; | |
99 size_t found = video_card_list.size(); | |
100 switch (video_card_list.size()) { | |
101 case 1: | |
102 found = 0; | |
103 break; | |
104 case 2: | |
105 if (video_card_list[0].vendor_id == kIntelVendorId && | |
106 video_card_list[1].vendor_id != kIntelVendorId) | |
107 found = 1; | |
108 else if (video_card_list[0].vendor_id != kIntelVendorId && | |
109 video_card_list[1].vendor_id == kIntelVendorId) | |
110 found = 0; | |
111 break; | |
112 } | |
113 if (found < video_card_list.size()) { | |
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 } | 119 } |
120 | 120 |
121 } // namespace anonymous | 121 } // namespace anonymous |
122 | 122 |
123 namespace gpu_info_collector { | 123 namespace gpu_info_collector { |
124 | 124 |
125 bool CollectGraphicsInfo(content::GPUInfo* gpu_info) { | 125 bool CollectGraphicsInfo(content::GPUInfo* gpu_info) { |
126 DCHECK(gpu_info); | 126 DCHECK(gpu_info); |
127 | 127 |
128 gpu_info->can_lose_context = | 128 gpu_info->can_lose_context = |
129 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2); | 129 (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2); |
130 gpu_info->finalized = true; | 130 gpu_info->finalized = true; |
131 return CollectGraphicsInfoGL(gpu_info); | 131 return CollectGraphicsInfoGL(gpu_info); |
132 } | 132 } |
133 | 133 |
134 bool CollectPreliminaryGraphicsInfo(content::GPUInfo* gpu_info) { | 134 bool CollectPreliminaryGraphicsInfo(content::GPUInfo* gpu_info) { |
135 DCHECK(gpu_info); | 135 DCHECK(gpu_info); |
136 | 136 |
137 bool rt = true; | 137 return CollectPCIVideoCardInfo(gpu_info); |
138 if (!CollectPCIVideoCardInfo(gpu_info) && !CollectVideoCardInfo(gpu_info)) | |
139 rt = false; | |
140 | |
141 return rt; | |
142 } | 138 } |
143 | 139 |
144 bool CollectVideoCardInfo(content::GPUInfo* gpu_info) { | 140 bool CollectVideoCardInfo(content::GPUInfo* gpu_info) { |
145 DCHECK(gpu_info); | 141 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 } | 142 } |
164 | 143 |
165 bool CollectDriverInfoGL(content::GPUInfo* gpu_info) { | 144 bool CollectDriverInfoGL(content::GPUInfo* gpu_info) { |
166 DCHECK(gpu_info); | 145 DCHECK(gpu_info); |
167 | 146 |
168 // Extract the OpenGL driver version string from the GL_VERSION string. | 147 // Extract the OpenGL driver version string from the GL_VERSION string. |
169 // Mac OpenGL drivers have the driver version | 148 // Mac OpenGL drivers have the driver version |
170 // at the end of the gl version string preceded by a dash. | 149 // 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. | 150 // Use some jiggery-pokery to turn that utf8 string into a std::wstring. |
172 std::string gl_version_string = gpu_info->gl_version_string; | 151 std::string gl_version_string = gpu_info->gl_version_string; |
173 size_t pos = gl_version_string.find_last_of('-'); | 152 size_t pos = gl_version_string.find_last_of('-'); |
174 if (pos == std::string::npos) | 153 if (pos == std::string::npos) |
175 return false; | 154 return false; |
176 gpu_info->driver_version = gl_version_string.substr(pos + 1); | 155 gpu_info->driver_version = gl_version_string.substr(pos + 1); |
177 return true; | 156 return true; |
178 } | 157 } |
179 | 158 |
180 } // namespace gpu_info_collector | 159 } // namespace gpu_info_collector |
OLD | NEW |