1 //
2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // driver_utils.h : provides more information about current driver.
8 
9 #include <algorithm>
10 
11 #include "libANGLE/renderer/driver_utils.h"
12 
13 #include "common/platform.h"
14 #include "common/system_utils.h"
15 
16 #if defined(ANGLE_PLATFORM_ANDROID)
17 #    include <sys/system_properties.h>
18 #endif
19 
20 #if defined(ANGLE_PLATFORM_LINUX)
21 #    include <sys/utsname.h>
22 #endif
23 
24 #if defined(ANGLE_PLATFORM_WINDOWS)
25 #    include <versionhelpers.h>
26 #endif
27 
28 namespace rx
29 {
30 // Intel
31 // Referenced from https://cgit.freedesktop.org/vaapi/intel-driver/tree/src/i965_pciids.h
32 namespace
33 {
34 // gen6
35 const uint32_t SandyBridge[] = {0x0102, 0x0106, 0x010A, 0x0112, 0x0122, 0x0116, 0x0126};
36 
37 // gen7
38 const uint32_t IvyBridge[] = {0x0152, 0x0156, 0x015A, 0x0162, 0x0166, 0x016A};
39 
40 const uint32_t Haswell[] = {
41     0x0402, 0x0406, 0x040A, 0x040B, 0x040E, 0x0C02, 0x0C06, 0x0C0A, 0x0C0B, 0x0C0E,
42     0x0A02, 0x0A06, 0x0A0A, 0x0A0B, 0x0A0E, 0x0D02, 0x0D06, 0x0D0A, 0x0D0B, 0x0D0E,  // hsw_gt1
43     0x0412, 0x0416, 0x041A, 0x041B, 0x041E, 0x0C12, 0x0C16, 0x0C1A, 0x0C1B, 0x0C1E,
44     0x0A12, 0x0A16, 0x0A1A, 0x0A1B, 0x0A1E, 0x0D12, 0x0D16, 0x0D1A, 0x0D1B, 0x0D1E,  // hsw_gt2
45     0x0422, 0x0426, 0x042A, 0x042B, 0x042E, 0x0C22, 0x0C26, 0x0C2A, 0x0C2B, 0x0C2E,
46     0x0A22, 0x0A26, 0x0A2A, 0x0A2B, 0x0A2E, 0x0D22, 0x0D26, 0x0D2A, 0x0D2B, 0x0D2E  // hsw_gt3
47 };
48 
49 // gen8
50 const uint32_t Broadwell[] = {0x1602, 0x1606, 0x160A, 0x160B, 0x160D, 0x160E,
51                               0x1612, 0x1616, 0x161A, 0x161B, 0x161D, 0x161E,
52                               0x1622, 0x1626, 0x162A, 0x162B, 0x162D, 0x162E};
53 
54 const uint32_t CherryView[] = {0x22B0, 0x22B1, 0x22B2, 0x22B3};
55 
56 // gen9
57 const uint32_t Skylake[] = {0x1902, 0x1906, 0x190A, 0x190B, 0x190E, 0x1912, 0x1913, 0x1915, 0x1916,
58                             0x1917, 0x191A, 0x191B, 0x191D, 0x191E, 0x1921, 0x1923, 0x1926, 0x1927,
59                             0x192A, 0x192B, 0x192D, 0x1932, 0x193A, 0x193B, 0x193D};
60 
61 const uint32_t Broxton[] = {0x0A84, 0x1A84, 0x1A85, 0x5A84, 0x5A85};
62 
63 // gen9p5
64 const uint32_t Kabylake[] = {0x5916, 0x5913, 0x5906, 0x5926, 0x5921, 0x5915, 0x590E,
65                              0x591E, 0x5912, 0x5917, 0x5902, 0x591B, 0x593B, 0x590B,
66                              0x591A, 0x590A, 0x591D, 0x5908, 0x5923, 0x5927};
67 
68 }  // anonymous namespace
69 
IntelDriverVersion(uint16_t lastPart)70 IntelDriverVersion::IntelDriverVersion(uint16_t lastPart) : mVersionPart(lastPart) {}
71 
operator ==(const IntelDriverVersion & version)72 bool IntelDriverVersion::operator==(const IntelDriverVersion &version)
73 {
74     return mVersionPart == version.mVersionPart;
75 }
76 
operator !=(const IntelDriverVersion & version)77 bool IntelDriverVersion::operator!=(const IntelDriverVersion &version)
78 {
79     return !(*this == version);
80 }
81 
operator <(const IntelDriverVersion & version)82 bool IntelDriverVersion::operator<(const IntelDriverVersion &version)
83 {
84     // See http://www.intel.com/content/www/us/en/support/graphics-drivers/000005654.html to
85     // understand the Intel graphics driver version number on Windows.
86     // mVersionPart1 changes with OS version. mVersionPart2 changes with DirectX version.
87     // mVersionPart3 stands for release year. mVersionPart4 is driver specific unique version
88     // number.
89     // For example: Intel driver version '20.19.15.4539'
90     //              20   -> windows 10 driver
91     //              19   -> DirectX 12 first version(12.0) supported
92     //              15   -> Driver released in 2015
93     //              4539 -> Driver specific unique version number
94     // For linux, Intel graphics driver version is the mesa version. The version number has three
95     // parts: major revision, minor revision, release number. So, for linux, we need to compare
96     // three parts.
97     // Currently, it's only used in windows. So, checking the last part is enough. Once it's needed
98     // in other platforms, it's easy to be extended.
99     return mVersionPart < version.mVersionPart;
100 }
101 
operator >=(const IntelDriverVersion & version)102 bool IntelDriverVersion::operator>=(const IntelDriverVersion &version)
103 {
104     return !(*this < version);
105 }
106 
IsSandyBridge(uint32_t DeviceId)107 bool IsSandyBridge(uint32_t DeviceId)
108 {
109     return std::find(std::begin(SandyBridge), std::end(SandyBridge), DeviceId) !=
110            std::end(SandyBridge);
111 }
112 
IsIvyBridge(uint32_t DeviceId)113 bool IsIvyBridge(uint32_t DeviceId)
114 {
115     return std::find(std::begin(IvyBridge), std::end(IvyBridge), DeviceId) != std::end(IvyBridge);
116 }
117 
IsHaswell(uint32_t DeviceId)118 bool IsHaswell(uint32_t DeviceId)
119 {
120     return std::find(std::begin(Haswell), std::end(Haswell), DeviceId) != std::end(Haswell);
121 }
122 
IsBroadwell(uint32_t DeviceId)123 bool IsBroadwell(uint32_t DeviceId)
124 {
125     return std::find(std::begin(Broadwell), std::end(Broadwell), DeviceId) != std::end(Broadwell);
126 }
127 
IsCherryView(uint32_t DeviceId)128 bool IsCherryView(uint32_t DeviceId)
129 {
130     return std::find(std::begin(CherryView), std::end(CherryView), DeviceId) !=
131            std::end(CherryView);
132 }
133 
IsSkylake(uint32_t DeviceId)134 bool IsSkylake(uint32_t DeviceId)
135 {
136     return std::find(std::begin(Skylake), std::end(Skylake), DeviceId) != std::end(Skylake);
137 }
138 
IsBroxton(uint32_t DeviceId)139 bool IsBroxton(uint32_t DeviceId)
140 {
141     return std::find(std::begin(Broxton), std::end(Broxton), DeviceId) != std::end(Broxton);
142 }
143 
IsKabylake(uint32_t DeviceId)144 bool IsKabylake(uint32_t DeviceId)
145 {
146     return std::find(std::begin(Kabylake), std::end(Kabylake), DeviceId) != std::end(Kabylake);
147 }
148 
Is9thGenIntel(uint32_t DeviceId)149 bool Is9thGenIntel(uint32_t DeviceId)
150 {
151     return IsSkylake(DeviceId) || IsBroxton(DeviceId) || IsKabylake(DeviceId);
152 }
153 
GetVendorString(uint32_t vendorId)154 const char *GetVendorString(uint32_t vendorId)
155 {
156     switch (vendorId)
157     {
158         case VENDOR_ID_AMD:
159             return "AMD";
160         case VENDOR_ID_ARM:
161             return "ARM";
162         case VENDOR_ID_APPLE:
163             return "Apple";
164         case VENDOR_ID_BROADCOM:
165             return "Broadcom";
166         case VENDOR_ID_GOOGLE:
167             return "Google";
168         case VENDOR_ID_INTEL:
169             return "Intel";
170         case VENDOR_ID_MESA:
171             return "Mesa";
172         case VENDOR_ID_NVIDIA:
173             return "NVIDIA";
174         case VENDOR_ID_POWERVR:
175             return "Imagination Technologies";
176         case VENDOR_ID_QUALCOMM:
177             return "Qualcomm";
178         case 0xba5eba11:  // Mock vendor ID used for tests.
179             return "Test";
180         case 0:
181             return "NULL";
182         default:
183             // TODO(jmadill): More vendor IDs.
184             UNIMPLEMENTED();
185             return "Unknown";
186     }
187 }
188 
GetAndroidSDKVersion()189 int GetAndroidSDKVersion()
190 {
191 #if defined(ANGLE_PLATFORM_ANDROID)
192     char apiVersion[PROP_VALUE_MAX];
193     int length = __system_property_get("ro.build.version.sdk", apiVersion);
194     if (length == 0)
195     {
196         return 0;
197     }
198     return atoi(apiVersion);
199 #else
200     return 0;
201 #endif
202 }
203 
OSVersion()204 OSVersion::OSVersion() {}
OSVersion(int major,int minor,int patch)205 OSVersion::OSVersion(int major, int minor, int patch)
206     : majorVersion(major), minorVersion(minor), patchVersion(patch)
207 {}
208 
operator ==(const OSVersion & a,const OSVersion & b)209 bool operator==(const OSVersion &a, const OSVersion &b)
210 {
211     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) ==
212            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
213 }
operator !=(const OSVersion & a,const OSVersion & b)214 bool operator!=(const OSVersion &a, const OSVersion &b)
215 {
216     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) !=
217            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
218 }
operator <(const OSVersion & a,const OSVersion & b)219 bool operator<(const OSVersion &a, const OSVersion &b)
220 {
221     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) <
222            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
223 }
operator >=(const OSVersion & a,const OSVersion & b)224 bool operator>=(const OSVersion &a, const OSVersion &b)
225 {
226     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) >=
227            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
228 }
229 
230 #if !defined(ANGLE_PLATFORM_APPLE)
GetMacOSVersion()231 OSVersion GetMacOSVersion()
232 {
233     // Return a default version
234     return OSVersion(0, 0, 0);
235 }
236 #endif
237 
238 #if defined(ANGLE_PLATFORM_LINUX)
ParseLinuxOSVersion(const char * version,int * major,int * minor,int * patch)239 bool ParseLinuxOSVersion(const char *version, int *major, int *minor, int *patch)
240 {
241     errno = 0;  // reset global error flag.
242     char *next;
243     *major = static_cast<int>(strtol(version, &next, 10));
244     if (next == nullptr || *next != '.' || errno != 0)
245     {
246         return false;
247     }
248 
249     *minor = static_cast<int>(strtol(next + 1, &next, 10));
250     if (next == nullptr || *next != '.' || errno != 0)
251     {
252         return false;
253     }
254 
255     *patch = static_cast<int>(strtol(next + 1, &next, 10));
256     if (errno != 0)
257     {
258         return false;
259     }
260 
261     return true;
262 }
263 #endif
264 
GetLinuxOSVersion()265 OSVersion GetLinuxOSVersion()
266 {
267 #if defined(ANGLE_PLATFORM_LINUX)
268     struct utsname uname_info;
269     if (uname(&uname_info) != 0)
270     {
271         return OSVersion(0, 0, 0);
272     }
273 
274     int majorVersion = 0, minorVersion = 0, patchVersion = 0;
275     if (ParseLinuxOSVersion(uname_info.release, &majorVersion, &minorVersion, &patchVersion))
276     {
277         return OSVersion(majorVersion, minorVersion, patchVersion);
278     }
279 #endif
280 
281     return OSVersion(0, 0, 0);
282 }
283 
284 // There are multiple environment variables that may or may not be set during Wayland
285 // sessions, including WAYLAND_DISPLAY, XDG_SESSION_TYPE, and DESKTOP_SESSION
IsWayland()286 bool IsWayland()
287 {
288     static bool checked   = false;
289     static bool isWayland = false;
290     if (!checked)
291     {
292         if (IsLinux())
293         {
294             if (!angle::GetEnvironmentVar("WAYLAND_DISPLAY").empty())
295             {
296                 isWayland = true;
297             }
298             else if (angle::GetEnvironmentVar("XDG_SESSION_TYPE") == "wayland")
299             {
300                 isWayland = true;
301             }
302             else if (angle::GetEnvironmentVar("DESKTOP_SESSION").find("wayland") !=
303                      std::string::npos)
304             {
305                 isWayland = true;
306             }
307         }
308         checked = true;
309     }
310     return isWayland;
311 }
312 
IsWin10OrGreater()313 bool IsWin10OrGreater()
314 {
315 #if defined(ANGLE_ENABLE_WINDOWS_UWP)
316     return true;
317 #elif defined(ANGLE_PLATFORM_WINDOWS)
318     return IsWindows10OrGreater();
319 #else
320     return false;
321 #endif
322 }
323 
324 }  // namespace rx
325