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(uint32_t buildNumber)70 IntelDriverVersion::IntelDriverVersion(uint32_t buildNumber) : mBuildNumber(buildNumber) {}
71 
operator ==(const IntelDriverVersion & version)72 bool IntelDriverVersion::operator==(const IntelDriverVersion &version)
73 {
74     return mBuildNumber == version.mBuildNumber;
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     return mBuildNumber < version.mBuildNumber;
85 }
86 
operator >=(const IntelDriverVersion & version)87 bool IntelDriverVersion::operator>=(const IntelDriverVersion &version)
88 {
89     return !(*this < version);
90 }
91 
IsSandyBridge(uint32_t DeviceId)92 bool IsSandyBridge(uint32_t DeviceId)
93 {
94     return std::find(std::begin(SandyBridge), std::end(SandyBridge), DeviceId) !=
95            std::end(SandyBridge);
96 }
97 
IsIvyBridge(uint32_t DeviceId)98 bool IsIvyBridge(uint32_t DeviceId)
99 {
100     return std::find(std::begin(IvyBridge), std::end(IvyBridge), DeviceId) != std::end(IvyBridge);
101 }
102 
IsHaswell(uint32_t DeviceId)103 bool IsHaswell(uint32_t DeviceId)
104 {
105     return std::find(std::begin(Haswell), std::end(Haswell), DeviceId) != std::end(Haswell);
106 }
107 
IsBroadwell(uint32_t DeviceId)108 bool IsBroadwell(uint32_t DeviceId)
109 {
110     return std::find(std::begin(Broadwell), std::end(Broadwell), DeviceId) != std::end(Broadwell);
111 }
112 
IsCherryView(uint32_t DeviceId)113 bool IsCherryView(uint32_t DeviceId)
114 {
115     return std::find(std::begin(CherryView), std::end(CherryView), DeviceId) !=
116            std::end(CherryView);
117 }
118 
IsSkylake(uint32_t DeviceId)119 bool IsSkylake(uint32_t DeviceId)
120 {
121     return std::find(std::begin(Skylake), std::end(Skylake), DeviceId) != std::end(Skylake);
122 }
123 
IsBroxton(uint32_t DeviceId)124 bool IsBroxton(uint32_t DeviceId)
125 {
126     return std::find(std::begin(Broxton), std::end(Broxton), DeviceId) != std::end(Broxton);
127 }
128 
IsKabylake(uint32_t DeviceId)129 bool IsKabylake(uint32_t DeviceId)
130 {
131     return std::find(std::begin(Kabylake), std::end(Kabylake), DeviceId) != std::end(Kabylake);
132 }
133 
Is9thGenIntel(uint32_t DeviceId)134 bool Is9thGenIntel(uint32_t DeviceId)
135 {
136     return IsSkylake(DeviceId) || IsBroxton(DeviceId) || IsKabylake(DeviceId);
137 }
138 
GetVendorString(uint32_t vendorId)139 const char *GetVendorString(uint32_t vendorId)
140 {
141     switch (vendorId)
142     {
143         case VENDOR_ID_AMD:
144             return "AMD";
145         case VENDOR_ID_ARM:
146             return "ARM";
147         case VENDOR_ID_APPLE:
148             return "Apple";
149         case VENDOR_ID_BROADCOM:
150             return "Broadcom";
151         case VENDOR_ID_GOOGLE:
152             return "Google";
153         case VENDOR_ID_INTEL:
154             return "Intel";
155         case VENDOR_ID_MESA:
156             return "Mesa";
157         case VENDOR_ID_NVIDIA:
158             return "NVIDIA";
159         case VENDOR_ID_POWERVR:
160             return "Imagination Technologies";
161         case VENDOR_ID_QUALCOMM:
162             return "Qualcomm";
163         case 0xba5eba11:  // Mock vendor ID used for tests.
164             return "Test";
165         case 0:
166             return "NULL";
167         default:
168             // TODO(jmadill): More vendor IDs.
169             UNIMPLEMENTED();
170             return "Unknown";
171     }
172 }
173 
GetAndroidSDKVersion()174 int GetAndroidSDKVersion()
175 {
176 #if defined(ANGLE_PLATFORM_ANDROID)
177     char apiVersion[PROP_VALUE_MAX];
178     int length = __system_property_get("ro.build.version.sdk", apiVersion);
179     if (length == 0)
180     {
181         return 0;
182     }
183     return atoi(apiVersion);
184 #else
185     return 0;
186 #endif
187 }
188 
OSVersion()189 OSVersion::OSVersion() {}
OSVersion(int major,int minor,int patch)190 OSVersion::OSVersion(int major, int minor, int patch)
191     : majorVersion(major), minorVersion(minor), patchVersion(patch)
192 {}
193 
operator ==(const OSVersion & a,const OSVersion & b)194 bool operator==(const OSVersion &a, const OSVersion &b)
195 {
196     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) ==
197            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
198 }
operator !=(const OSVersion & a,const OSVersion & b)199 bool operator!=(const OSVersion &a, const OSVersion &b)
200 {
201     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) !=
202            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
203 }
operator <(const OSVersion & a,const OSVersion & b)204 bool operator<(const OSVersion &a, const OSVersion &b)
205 {
206     return std::tie(a.majorVersion, a.minorVersion, a.patchVersion) <
207            std::tie(b.majorVersion, b.minorVersion, b.patchVersion);
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 }
214 
215 #if !defined(ANGLE_PLATFORM_MACOS)
GetMacOSVersion()216 OSVersion GetMacOSVersion()
217 {
218     // Return a default version
219     return OSVersion(0, 0, 0);
220 }
221 #endif
222 
223 #if !defined(ANGLE_PLATFORM_IOS)
GetiOSVersion()224 OSVersion GetiOSVersion()
225 {
226     // Return a default version
227     return OSVersion(0, 0, 0);
228 }
229 #endif
230 
231 #if defined(ANGLE_PLATFORM_LINUX)
ParseLinuxOSVersion(const char * version,int * major,int * minor,int * patch)232 bool ParseLinuxOSVersion(const char *version, int *major, int *minor, int *patch)
233 {
234     errno = 0;  // reset global error flag.
235     char *next;
236     *major = static_cast<int>(strtol(version, &next, 10));
237     if (next == nullptr || *next != '.' || errno != 0)
238     {
239         return false;
240     }
241 
242     *minor = static_cast<int>(strtol(next + 1, &next, 10));
243     if (next == nullptr || *next != '.' || errno != 0)
244     {
245         return false;
246     }
247 
248     *patch = static_cast<int>(strtol(next + 1, &next, 10));
249     if (errno != 0)
250     {
251         return false;
252     }
253 
254     return true;
255 }
256 #endif
257 
GetLinuxOSVersion()258 OSVersion GetLinuxOSVersion()
259 {
260 #if defined(ANGLE_PLATFORM_LINUX)
261     struct utsname uname_info;
262     if (uname(&uname_info) != 0)
263     {
264         return OSVersion(0, 0, 0);
265     }
266 
267     int majorVersion = 0, minorVersion = 0, patchVersion = 0;
268     if (ParseLinuxOSVersion(uname_info.release, &majorVersion, &minorVersion, &patchVersion))
269     {
270         return OSVersion(majorVersion, minorVersion, patchVersion);
271     }
272 #endif
273 
274     return OSVersion(0, 0, 0);
275 }
276 
277 // There are multiple environment variables that may or may not be set during Wayland
278 // sessions, including WAYLAND_DISPLAY, XDG_SESSION_TYPE, and DESKTOP_SESSION
IsWayland()279 bool IsWayland()
280 {
281     static bool checked   = false;
282     static bool isWayland = false;
283     if (!checked)
284     {
285         if (IsLinux())
286         {
287             if (!angle::GetEnvironmentVar("WAYLAND_DISPLAY").empty())
288             {
289                 isWayland = true;
290             }
291             else if (angle::GetEnvironmentVar("XDG_SESSION_TYPE") == "wayland")
292             {
293                 isWayland = true;
294             }
295             else if (angle::GetEnvironmentVar("DESKTOP_SESSION").find("wayland") !=
296                      std::string::npos)
297             {
298                 isWayland = true;
299             }
300         }
301         checked = true;
302     }
303     return isWayland;
304 }
305 
IsWin10OrGreater()306 bool IsWin10OrGreater()
307 {
308 #if defined(ANGLE_ENABLE_WINDOWS_UWP)
309     return true;
310 #elif defined(ANGLE_PLATFORM_WINDOWS)
311     return IsWindows10OrGreater();
312 #else
313     return false;
314 #endif
315 }
316 
317 }  // namespace rx
318