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