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