1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9 #include "include/core/SkMatrix.h"
10 #include "include/private/GrTypesPriv.h"
11 #include "src/gpu/GrDataUtils.h"
12 #include "src/gpu/gl/GrGLUtil.h"
13 #include <stdio.h>
14
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #if GR_GL_LOG_CALLS
18 bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
19 #endif
20
21 #if GR_GL_CHECK_ERROR
22 bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
23 #endif
24
25 ///////////////////////////////////////////////////////////////////////////////
26
GrGLGetStandardInUseFromString(const char * versionString)27 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString) {
28 if (!versionString) {
29 SkDebugf("nullptr GL version string.");
30 return kNone_GrGLStandard;
31 }
32
33 int major, minor;
34
35 // check for desktop
36 int n = sscanf(versionString, "%d.%d", &major, &minor);
37 if (2 == n) {
38 return kGL_GrGLStandard;
39 }
40
41 // WebGL might look like "OpenGL ES 2.0 (WebGL 1.0 (OpenGL ES 2.0 Chromium))"
42 int esMajor, esMinor;
43 n = sscanf(versionString, "OpenGL ES %d.%d (WebGL %d.%d", &esMajor, &esMinor, &major, &minor);
44 if (4 == n) {
45 return kWebGL_GrGLStandard;
46 }
47
48 // check for ES 1
49 char profile[2];
50 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, &major, &minor);
51 if (4 == n) {
52 // we no longer support ES1.
53 return kNone_GrGLStandard;
54 }
55
56 // check for ES2
57 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
58 if (2 == n) {
59 return kGLES_GrGLStandard;
60 }
61 return kNone_GrGLStandard;
62 }
63
GrGLGetVersionFromString(const char * versionString)64 GrGLVersion GrGLGetVersionFromString(const char* versionString) {
65 if (!versionString) {
66 SkDebugf("nullptr GL version string.");
67 return GR_GL_INVALID_VER;
68 }
69
70 int major, minor;
71
72 // check for mesa
73 int mesaMajor, mesaMinor;
74 int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
75 if (4 == n) {
76 return GR_GL_VER(major, minor);
77 }
78
79 n = sscanf(versionString, "%d.%d", &major, &minor);
80 if (2 == n) {
81 return GR_GL_VER(major, minor);
82 }
83
84 // WebGL might look like "OpenGL ES 2.0 (WebGL 1.0 (OpenGL ES 2.0 Chromium))"
85 int esMajor, esMinor;
86 n = sscanf(versionString, "OpenGL ES %d.%d (WebGL %d.%d", &esMajor, &esMinor, &major, &minor);
87 if (4 == n) {
88 return GR_GL_VER(major, minor);
89 }
90
91 char profile[2];
92 n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile + 1, &major, &minor);
93 if (4 == n) {
94 return GR_GL_VER(major, minor);
95 }
96
97 n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
98 if (2 == n) {
99 return GR_GL_VER(major, minor);
100 }
101
102 return GR_GL_INVALID_VER;
103 }
104
GrGLGetVersion(const GrGLInterface * gl)105 GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
106 SkASSERT(gl);
107 const GrGLubyte* v;
108 GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
109 return GrGLGetVersionFromString((const char*)v);
110 }
111
get_glsl_version(const char * versionString)112 static GrGLSLVersion get_glsl_version(const char* versionString) {
113 SkASSERT(versionString);
114 int major, minor;
115
116 int n = sscanf(versionString, "%d.%d", &major, &minor);
117 if (2 == n) {
118 return GR_GLSL_VER(major, minor);
119 }
120
121 n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
122 if (2 == n) {
123 return GR_GLSL_VER(major, minor);
124 }
125
126 #ifdef SK_BUILD_FOR_ANDROID
127 // android hack until the gpu vender updates their drivers
128 n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
129 if (2 == n) {
130 return GR_GLSL_VER(major, minor);
131 }
132 #endif
133
134 return GR_GLSL_INVALID_VER;
135 }
136
get_vendor(const char * vendorString)137 static GrGLVendor get_vendor(const char* vendorString) {
138 SkASSERT(vendorString);
139 if (0 == strcmp(vendorString, "ARM")) {
140 return GrGLVendor::kARM;
141 }
142 if (0 == strcmp(vendorString, "Google Inc.")) {
143 return GrGLVendor::kGoogle;
144 }
145 if (0 == strcmp(vendorString, "Imagination Technologies")) {
146 return GrGLVendor::kImagination;
147 }
148 if (0 == strncmp(vendorString, "Intel ", 6) || 0 == strcmp(vendorString, "Intel")) {
149 return GrGLVendor::kIntel;
150 }
151 if (0 == strcmp(vendorString, "Qualcomm") || 0 == strcmp(vendorString, "freedreno")) {
152 return GrGLVendor::kQualcomm;
153 }
154 if (0 == strcmp(vendorString, "NVIDIA Corporation")) {
155 return GrGLVendor::kNVIDIA;
156 }
157 if (0 == strcmp(vendorString, "ATI Technologies Inc.")) {
158 return GrGLVendor::kATI;
159 }
160 return GrGLVendor::kOther;
161 }
162
get_renderer(const char * rendererString,const GrGLExtensions & extensions)163 static GrGLRenderer get_renderer(const char* rendererString, const GrGLExtensions& extensions) {
164 SkASSERT(rendererString);
165 static const char kTegraStr[] = "NVIDIA Tegra";
166 if (0 == strncmp(rendererString, kTegraStr, SK_ARRAY_COUNT(kTegraStr) - 1)) {
167 // Tegra strings are not very descriptive. We distinguish between the modern and legacy
168 // architectures by the presence of NV_path_rendering.
169 return extensions.has("GL_NV_path_rendering") ? GrGLRenderer::kTegra
170 : GrGLRenderer::kTegra_PreK1;
171 }
172 int lastDigit;
173 int n = sscanf(rendererString, "PowerVR SGX 54%d", &lastDigit);
174 if (1 == n && lastDigit >= 0 && lastDigit <= 9) {
175 return GrGLRenderer::kPowerVR54x;
176 }
177 // certain iOS devices also use PowerVR54x GPUs
178 static const char kAppleA4Str[] = "Apple A4";
179 static const char kAppleA5Str[] = "Apple A5";
180 static const char kAppleA6Str[] = "Apple A6";
181 if (0 == strncmp(rendererString, kAppleA4Str, SK_ARRAY_COUNT(kAppleA4Str) - 1) ||
182 0 == strncmp(rendererString, kAppleA5Str, SK_ARRAY_COUNT(kAppleA5Str) - 1) ||
183 0 == strncmp(rendererString, kAppleA6Str, SK_ARRAY_COUNT(kAppleA6Str) - 1)) {
184 return GrGLRenderer::kPowerVR54x;
185 }
186 static const char kPowerVRRogueStr[] = "PowerVR Rogue";
187 static const char kAppleA7Str[] = "Apple A7";
188 static const char kAppleA8Str[] = "Apple A8";
189 if (0 == strncmp(rendererString, kPowerVRRogueStr, SK_ARRAY_COUNT(kPowerVRRogueStr) - 1) ||
190 0 == strncmp(rendererString, kAppleA7Str, SK_ARRAY_COUNT(kAppleA7Str) - 1) ||
191 0 == strncmp(rendererString, kAppleA8Str, SK_ARRAY_COUNT(kAppleA8Str) - 1)) {
192 return GrGLRenderer::kPowerVRRogue;
193 }
194 int adrenoNumber;
195 n = sscanf(rendererString, "Adreno (TM) %d", &adrenoNumber);
196 if (n < 1) {
197 // retry with freedreno driver
198 n = sscanf(rendererString, "FD%d", &adrenoNumber);
199 }
200 if (1 == n) {
201 if (adrenoNumber >= 300) {
202 if (adrenoNumber < 400) {
203 return GrGLRenderer::kAdreno3xx;
204 }
205 if (adrenoNumber < 500) {
206 return adrenoNumber >= 430 ? GrGLRenderer::kAdreno430
207 : GrGLRenderer::kAdreno4xx_other;
208 }
209 if (adrenoNumber < 600) {
210 return adrenoNumber == 530 ? GrGLRenderer::kAdreno530
211 : GrGLRenderer::kAdreno5xx_other;
212 }
213 if (adrenoNumber < 700) {
214 if (adrenoNumber == 615) {
215 return GrGLRenderer::kAdreno615;
216 }
217 if (adrenoNumber == 620) {
218 return GrGLRenderer::kAdreno620;
219 }
220 if (adrenoNumber == 630) {
221 return GrGLRenderer::kAdreno630;
222 }
223 if (adrenoNumber == 640) {
224 return GrGLRenderer::kAdreno640;
225 }
226 return GrGLRenderer::kAdreno6xx_other;
227 }
228 }
229 }
230 if (0 == strcmp("Google SwiftShader", rendererString)) {
231 return GrGLRenderer::kGoogleSwiftShader;
232 }
233
234 if (const char* intelString = strstr(rendererString, "Intel")) {
235 // These generic strings seem to always come from Haswell: Iris 5100 or Iris Pro 5200
236 if (0 == strcmp("Intel Iris OpenGL Engine", intelString) ||
237 0 == strcmp("Intel Iris Pro OpenGL Engine", intelString)) {
238 return GrGLRenderer::kIntelHaswell;
239 }
240 if (strstr(intelString, "Sandybridge")) {
241 return GrGLRenderer::kIntelSandyBridge;
242 }
243 if (strstr(intelString, "Bay Trail")) {
244 return GrGLRenderer::kIntelValleyView;
245 }
246 // There are many possible intervening strings here:
247 // 'Intel(R)' is a common prefix
248 // 'Iris' may appear, followed by '(R)' or '(TM)'
249 // 'Iris' can then be followed by 'Graphics', 'Pro Graphics', or 'Plus Graphics'
250 // If 'Iris' isn't there, we might have 'HD Graphics' or 'UHD Graphics'
251 //
252 // In all cases, though, we end with 'Graphics ', an optional 'P', and a number,
253 // so just skip to that and handle two cases:
254 if (const char* intelGfxString = strstr(intelString, "Graphics")) {
255 int intelNumber;
256 if (sscanf(intelGfxString, "Graphics %d", &intelNumber) ||
257 sscanf(intelGfxString, "Graphics P%d", &intelNumber)) {
258 if (intelNumber == 2000 || intelNumber == 3000) {
259 return GrGLRenderer::kIntelSandyBridge;
260 }
261 if (intelNumber == 2500 || intelNumber == 4000) {
262 return GrGLRenderer::kIntelIvyBridge;
263 }
264 if (intelNumber >= 4200 && intelNumber <= 5200) {
265 return GrGLRenderer::kIntelHaswell;
266 }
267 if (intelNumber >= 400 && intelNumber <= 405) {
268 return GrGLRenderer::kIntelCherryView;
269 }
270 if (intelNumber >= 5300 && intelNumber <= 6300) {
271 return GrGLRenderer::kIntelBroadwell;
272 }
273 if (intelNumber >= 500 && intelNumber <= 505) {
274 return GrGLRenderer::kIntelApolloLake;
275 }
276 if (intelNumber >= 510 && intelNumber <= 580) {
277 return GrGLRenderer::kIntelSkyLake;
278 }
279 if (intelNumber >= 600 && intelNumber <= 605) {
280 return GrGLRenderer::kIntelGeminiLake;
281 }
282 // 610 and 630 are reused from KabyLake to CoffeeLake. The CoffeeLake variants
283 // are "UHD Graphics", while the KabyLake ones are "HD Graphics"
284 if (intelNumber == 610 || intelNumber == 630) {
285 return strstr(intelString, "UHD") ? GrGLRenderer::kIntelCoffeeLake
286 : GrGLRenderer::kIntelKabyLake;
287 }
288 if (intelNumber >= 610 && intelNumber <= 650) {
289 return GrGLRenderer::kIntelKabyLake;
290 }
291 if (intelNumber == 655) {
292 return GrGLRenderer::kIntelCoffeeLake;
293 }
294 if (intelNumber >= 910 && intelNumber <= 950) {
295 return GrGLRenderer::kIntelIceLake;
296 }
297 }
298 }
299 }
300
301 // The AMD string can have a somewhat arbitrary preamble (see skbug.com/7195)
302 static constexpr char kRadeonStr[] = "Radeon ";
303 if (const char* amdString = strstr(rendererString, kRadeonStr)) {
304 amdString += strlen(kRadeonStr);
305 // Sometimes there is a (TM) and sometimes not.
306 static constexpr char kTMStr[] = "(TM) ";
307 if (!strncmp(amdString, kTMStr, strlen(kTMStr))) {
308 amdString += strlen(kTMStr);
309 }
310
311 char amd0, amd1, amd2;
312 int amdModel;
313 n = sscanf(amdString, "R9 M3%c%c", &amd0, &amd1);
314 if (2 == n && isdigit(amd0) && isdigit(amd1)) {
315 return GrGLRenderer::kAMDRadeonR9M3xx;
316 }
317
318 n = sscanf(amdString, "R9 M4%c%c", &amd0, &amd1);
319 if (2 == n && isdigit(amd0) && isdigit(amd1)) {
320 return GrGLRenderer::kAMDRadeonR9M4xx;
321 }
322
323 n = sscanf(amdString, "HD 7%c%c%c Series", &amd0, &amd1, &amd2);
324 if (3 == n && isdigit(amd0) && isdigit(amd1) && isdigit(amd2)) {
325 return GrGLRenderer::kAMDRadeonHD7xxx;
326 }
327
328 n = sscanf(amdString, "Pro 5%c%c%c", &amd0, &amd1, &amd2);
329 if (3 == n && isdigit(amd0) && isdigit(amd1) && isdigit(amd2)) {
330 return GrGLRenderer::kAMDRadeonPro5xxx;
331 }
332
333 n = sscanf(amdString, "Pro Vega %i", &amdModel);
334 if (1 == n) {
335 return GrGLRenderer::kAMDRadeonProVegaxx;
336 }
337 }
338
339 if (strstr(rendererString, "llvmpipe")) {
340 return GrGLRenderer::kGalliumLLVM;
341 }
342 if (strstr(rendererString, "virgl")) {
343 return GrGLRenderer::kVirgl;
344 }
345 static const char kMaliGStr[] = "Mali-G";
346 if (0 == strncmp(rendererString, kMaliGStr, SK_ARRAY_COUNT(kMaliGStr) - 1)) {
347 return GrGLRenderer::kMaliG;
348 }
349 static const char kMaliTStr[] = "Mali-T";
350 if (0 == strncmp(rendererString, kMaliTStr, SK_ARRAY_COUNT(kMaliTStr) - 1)) {
351 return GrGLRenderer::kMaliT;
352 }
353 int mali400Num;
354 if (1 == sscanf(rendererString, "Mali-%d", &mali400Num) && mali400Num >= 400 &&
355 mali400Num < 500) {
356 return GrGLRenderer::kMali4xx;
357 }
358 return GrGLRenderer::kOther;
359 }
360
is_commamd_buffer(const char * rendererString,const char * versionString)361 static bool is_commamd_buffer(const char* rendererString, const char* versionString) {
362 SkASSERT(rendererString);
363 SkASSERT(versionString);
364
365 int major, minor;
366 static const char kChromium[] = "Chromium";
367 char suffix[SK_ARRAY_COUNT(kChromium)] = {0};
368 return (0 == strcmp(rendererString, kChromium) ||
369 (3 == sscanf(versionString, "OpenGL ES %d.%d %8s", &major, &minor, suffix) &&
370 0 == strcmp(kChromium, suffix)));
371 }
372
get_driver_and_version(GrGLStandard standard,GrGLVendor vendor,const char * vendorString,const char * rendererString,const char * versionString)373 static std::tuple<GrGLDriver, GrGLDriverVersion> get_driver_and_version(GrGLStandard standard,
374 GrGLVendor vendor,
375 const char* vendorString,
376 const char* rendererString,
377 const char* versionString) {
378 SkASSERT(rendererString);
379 SkASSERT(versionString);
380
381 GrGLDriver driver = GrGLDriver::kUnknown;
382 GrGLDriverVersion driverVersion = GR_GL_DRIVER_UNKNOWN_VER;
383
384 int major, minor, rev, driverMajor, driverMinor, driverPoint;
385 // This is the same on ES and regular GL.
386 if (!strcmp(vendorString, "freedreno")) {
387 driver = GrGLDriver::kFreedreno;
388 } else if (GR_IS_GR_GL(standard)) {
389 if (vendor == GrGLVendor::kNVIDIA) {
390 driver = GrGLDriver::kNVIDIA;
391 int n = sscanf(versionString,
392 "%d.%d.%d NVIDIA %d.%d",
393 &major,
394 &minor,
395 &rev,
396 &driverMajor,
397 &driverMinor);
398 // Some older NVIDIA drivers don't report the driver version.
399 if (n == 5) {
400 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
401 }
402 } else {
403 int n = sscanf(versionString,
404 "%d.%d Mesa %d.%d",
405 &major,
406 &minor,
407 &driverMajor,
408 &driverMinor);
409 if (n != 4) {
410 n = sscanf(versionString,
411 "%d.%d (Core Profile) Mesa %d.%d",
412 &major,
413 &minor,
414 &driverMajor,
415 &driverMinor);
416 }
417 if (n == 4) {
418 driver = GrGLDriver::kMesa;
419 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
420 }
421 }
422 } else if (standard == kGLES_GrGLStandard) {
423 if (vendor == GrGLVendor::kNVIDIA) {
424 driver = GrGLDriver::kNVIDIA;
425 int n = sscanf(versionString,
426 "OpenGL ES %d.%d NVIDIA %d.%d",
427 &major,
428 &minor,
429 &driverMajor,
430 &driverMinor);
431 // Some older NVIDIA drivers don't report the driver version.
432 if (n == 4) {
433 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
434 }
435 } else if (vendor == GrGLVendor::kImagination) {
436 int revision;
437 int n = sscanf(versionString,
438 "OpenGL ES %d.%d build %d.%d@%d",
439 &major,
440 &minor,
441 &driverMajor,
442 &driverMinor,
443 &revision);
444 if (n == 5) {
445 driver = GrGLDriver::kImagination;
446 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
447 }
448 } else {
449 int n = sscanf(versionString,
450 "OpenGL ES %d.%d Mesa %d.%d",
451 &major,
452 &minor,
453 &driverMajor,
454 &driverMinor);
455 if (n == 4) {
456 driver = GrGLDriver::kMesa;
457 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
458 }
459 }
460 }
461
462 if (driver == GrGLDriver::kUnknown) {
463 if (vendor == GrGLVendor::kGoogle) {
464 // Swiftshader is the only Google vendor at the moment
465 driver = GrGLDriver::kSwiftShader;
466
467 // Swiftshader has a strange version string: w.x.y.z Going to arbitrarily ignore
468 // y and assume w,x and z are major, minor, point.
469 // As of writing, version is 4.0.0.6
470 int n = sscanf(versionString,
471 "OpenGL ES %d.%d SwiftShader %d.%d.0.%d",
472 &major,
473 &minor,
474 &driverMajor,
475 &driverMinor,
476 &driverPoint);
477 if (n == 5) {
478 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, driverPoint);
479 }
480 } else if (vendor == GrGLVendor::kIntel) {
481 // We presume we're on the Intel driver since it hasn't identified itself as Mesa.
482 driver = GrGLDriver::kIntel;
483
484 // This is how the macOS version strings are structured. This might be different on
485 // different
486 // OSes.
487 int n = sscanf(versionString,
488 "%d.%d INTEL-%d.%d.%d",
489 &major,
490 &minor,
491 &driverMajor,
492 &driverMinor,
493 &driverPoint);
494 if (n == 5) {
495 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, driverPoint);
496 }
497 } else if (vendor == GrGLVendor::kQualcomm) {
498 driver = GrGLDriver::kQualcomm;
499 int n = sscanf(versionString,
500 "OpenGL ES %d.%d V@%d.%d",
501 &major,
502 &minor,
503 &driverMajor,
504 &driverMinor);
505 if (n == 4) {
506 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
507 }
508 } else if (vendor == GrGLVendor::kImagination) {
509 int revision;
510 int n = sscanf(versionString,
511 "OpenGL ES %d.%d build %d.%d@%d",
512 &major,
513 &minor,
514 &driverMajor,
515 &driverMinor,
516 &revision);
517 if (n == 5) {
518 // Revision is a large number (looks like a source control revision number) that
519 // doesn't fit into the 'patch' bits, so omit it until we need it.
520 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
521 }
522 } else if (vendor == GrGLVendor::kARM) {
523 // Example:
524 // OpenGL ES 3.2 v1.r26p0-01rel0.217d2597f6bd19b169343737782e56e3
525 // It's unclear how to interpret what comes between "p" and "rel". Every string we've
526 // seen so far has "0-01" there. We ignore it for now.
527 int ignored0;
528 int ignored1;
529 int n = sscanf(versionString,
530 "OpenGL ES %d.%d v%d.r%dp%d-%drel",
531 &major,
532 &minor,
533 &driverMajor,
534 &driverMinor,
535 &ignored0,
536 &ignored1);
537 if (n == 6) {
538 driver = GrGLDriver::kARM;
539 driverVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
540 }
541 } else {
542 static constexpr char kEmulatorPrefix[] = "Android Emulator OpenGL ES Translator";
543 if (0 == strncmp(kEmulatorPrefix, rendererString, strlen(kEmulatorPrefix))) {
544 driver = GrGLDriver::kAndroidEmulator;
545 }
546 }
547 }
548 return {driver, driverVersion};
549 }
550
551 // If this is detected as ANGLE then the ANGLE backend is returned along with rendererString
552 // stripped of "ANGLE(" and ")" at the start and end, respectively.
get_angle_backend(const char * rendererString)553 static std::tuple<GrGLANGLEBackend, SkString> get_angle_backend(const char* rendererString) {
554 // crbug.com/1203705 ANGLE renderer will be "ANGLE (<gl-vendor>, <gl-renderer>, <gl-version>)"
555 // on ANGLE's GL backend with related substitutions for the inner strings on other backends.
556 static constexpr char kHeader[] = "ANGLE (";
557 static constexpr size_t kHeaderLength = SK_ARRAY_COUNT(kHeader) - 1;
558 int rendererLength = strlen(rendererString);
559 if (!strncmp(rendererString, kHeader, kHeaderLength) &&
560 rendererString[rendererLength - 1] == ')') {
561 SkString innerString;
562 innerString.set(rendererString + kHeaderLength, rendererLength - kHeaderLength - 1);
563 if (strstr(rendererString, "Direct3D11")) {
564 return {GrGLANGLEBackend::kD3D11, std::move(innerString)};
565 } else if (strstr(rendererString, "Direct3D9")) {
566 return {GrGLANGLEBackend::kD3D9, std::move(innerString)};
567 } else if (strstr(rendererString, "OpenGL")) {
568 return {GrGLANGLEBackend::kOpenGL, std::move(innerString)};
569 }
570 }
571 return {GrGLANGLEBackend::kUnknown, {}};
572 }
573
574 static std::tuple<GrGLVendor, GrGLRenderer, GrGLDriver, GrGLDriverVersion>
get_angle_gl_vendor_and_renderer(const char * innerString,const GrGLExtensions & extensions)575 get_angle_gl_vendor_and_renderer(
576 const char* innerString,
577 const GrGLExtensions& extensions) {
578 SkTArray<SkString> parts;
579 SkStrSplit(innerString, ",", &parts);
580 // This would need some fixing if we have substrings that contain commas.
581 if (parts.size() != 3) {
582 return {GrGLVendor::kOther,
583 GrGLRenderer::kOther,
584 GrGLDriver::kUnknown,
585 GR_GL_DRIVER_UNKNOWN_VER};
586 }
587
588 const char* angleVendorString = parts[0].c_str();
589 const char* angleRendererString = parts[1].c_str() + 1; // skip initial space
590 const char* angleVersionString = parts[2].c_str() + 1; // skip initial space
591
592 GrGLVendor angleVendor = get_vendor(angleVendorString);
593
594 auto [angleDriver, angleDriverVersion] = get_driver_and_version(kGLES_GrGLStandard,
595 angleVendor,
596 angleVendorString,
597 angleRendererString,
598 angleVersionString);
599
600 auto angleRenderer = get_renderer(angleRendererString, extensions);
601
602 return {angleVendor, angleRenderer, angleDriver, angleDriverVersion};
603 }
604
605 static std::tuple<GrGLVendor, GrGLRenderer, GrGLDriver, GrGLDriverVersion>
get_angle_d3d_vendor_and_renderer(const char * innerString)606 get_angle_d3d_vendor_and_renderer(const char* innerString) {
607 auto vendor = GrGLVendor::kOther;
608 auto renderer = GrGLRenderer::kOther;
609
610 if (strstr(innerString, "Intel")) {
611 vendor = GrGLVendor::kIntel;
612
613 const char* modelStr;
614 int modelNumber;
615 if ((modelStr = strstr(innerString, "HD Graphics")) &&
616 (1 == sscanf(modelStr, "HD Graphics %i", &modelNumber) ||
617 1 == sscanf(modelStr, "HD Graphics P%i", &modelNumber))) {
618 switch (modelNumber) {
619 case 2000:
620 case 3000:
621 renderer = GrGLRenderer::kIntelSandyBridge;
622 break;
623 case 4000:
624 case 2500:
625 renderer = GrGLRenderer::kIntelSandyBridge;
626 break;
627 case 510:
628 case 515:
629 case 520:
630 case 530:
631 renderer = GrGLRenderer::kIntelSkyLake;
632 break;
633 }
634 } else if ((modelStr = strstr(innerString, "Iris")) &&
635 (1 == sscanf(modelStr, "Iris(TM) Graphics %i", &modelNumber) ||
636 1 == sscanf(modelStr, "Iris(TM) Pro Graphics %i", &modelNumber) ||
637 1 == sscanf(modelStr, "Iris(TM) Pro Graphics P%i", &modelNumber))) {
638 switch (modelNumber) {
639 case 540:
640 case 550:
641 case 555:
642 case 580:
643 renderer = GrGLRenderer::kIntelSkyLake;
644 break;
645 }
646 }
647 } else if (strstr(innerString, "NVIDIA")) {
648 vendor = GrGLVendor::kNVIDIA;
649 } else if (strstr(innerString, "Radeon")) {
650 vendor = GrGLVendor::kATI;
651 }
652 // We haven't had a need yet to parse the D3D driver string.
653 return {vendor, renderer, GrGLDriver::kUnknown, GR_GL_DRIVER_UNKNOWN_VER};
654 }
655
GrGLGetDriverInfo(const GrGLInterface * interface)656 GrGLDriverInfo GrGLGetDriverInfo(const GrGLInterface* interface) {
657 if (!interface) {
658 return {};
659 }
660 SkASSERT(interface->fStandard != kNone_GrGLStandard);
661 GrGLDriverInfo info;
662 info.fStandard = interface->fStandard;
663
664 auto getString = [&](GrGLenum s) {
665 const GrGLubyte* bytes = interface->fFunctions.fGetString(s);
666 if (!bytes) {
667 return "";
668 }
669 return reinterpret_cast<const char*>(bytes);
670 };
671
672 const char* const version = getString(GR_GL_VERSION);
673 const char* const slversion = getString(GR_GL_SHADING_LANGUAGE_VERSION);
674 const char* const renderer = getString(GR_GL_RENDERER);
675 const char* const vendor = getString(GR_GL_VENDOR);
676
677 info.fVersion = GrGLGetVersionFromString(version);
678 info.fGLSLVersion = get_glsl_version(slversion);
679 info.fVendor = get_vendor(vendor);
680 info.fRenderer = get_renderer(renderer, interface->fExtensions);
681
682 std::tie(info.fDriver, info.fDriverVersion) = get_driver_and_version(interface->fStandard,
683 info.fVendor,
684 vendor,
685 renderer,
686 version);
687
688 SkString innerAngleRendererString;
689 std::tie(info.fANGLEBackend, innerAngleRendererString) = get_angle_backend(renderer);
690
691 if (info.fANGLEBackend == GrGLANGLEBackend::kD3D9 ||
692 info.fANGLEBackend == GrGLANGLEBackend::kD3D11) {
693 std::tie(info.fANGLEVendor,
694 info.fANGLERenderer,
695 info.fANGLEDriver,
696 info.fANGLEDriverVersion) =
697 get_angle_d3d_vendor_and_renderer(innerAngleRendererString.c_str());
698 } else if (info.fANGLEBackend == GrGLANGLEBackend::kOpenGL) {
699 std::tie(info.fANGLEVendor,
700 info.fANGLERenderer,
701 info.fANGLEDriver,
702 info.fANGLEDriverVersion) =
703 get_angle_gl_vendor_and_renderer(innerAngleRendererString.c_str(),
704 interface->fExtensions);
705 }
706
707 info.fIsOverCommandBuffer = is_commamd_buffer(renderer, version);
708
709 return info;
710 }
711
GrToGLStencilFunc(GrStencilTest test)712 GrGLenum GrToGLStencilFunc(GrStencilTest test) {
713 static const GrGLenum gTable[kGrStencilTestCount] = {
714 GR_GL_ALWAYS, // kAlways
715 GR_GL_NEVER, // kNever
716 GR_GL_GREATER, // kGreater
717 GR_GL_GEQUAL, // kGEqual
718 GR_GL_LESS, // kLess
719 GR_GL_LEQUAL, // kLEqual
720 GR_GL_EQUAL, // kEqual
721 GR_GL_NOTEQUAL, // kNotEqual
722 };
723 static_assert(0 == (int)GrStencilTest::kAlways);
724 static_assert(1 == (int)GrStencilTest::kNever);
725 static_assert(2 == (int)GrStencilTest::kGreater);
726 static_assert(3 == (int)GrStencilTest::kGEqual);
727 static_assert(4 == (int)GrStencilTest::kLess);
728 static_assert(5 == (int)GrStencilTest::kLEqual);
729 static_assert(6 == (int)GrStencilTest::kEqual);
730 static_assert(7 == (int)GrStencilTest::kNotEqual);
731 SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
732
733 return gTable[(int)test];
734 }
735
GrGLFormatIsCompressed(GrGLFormat format)736 bool GrGLFormatIsCompressed(GrGLFormat format) {
737 switch (format) {
738 case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
739 case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
740 case GrGLFormat::kCOMPRESSED_RGB8_BC1:
741 case GrGLFormat::kCOMPRESSED_RGBA8_BC1:
742 return true;
743
744 case GrGLFormat::kRGBA8:
745 case GrGLFormat::kR8:
746 case GrGLFormat::kALPHA8:
747 case GrGLFormat::kLUMINANCE8:
748 case GrGLFormat::kLUMINANCE8_ALPHA8:
749 case GrGLFormat::kBGRA8:
750 case GrGLFormat::kRGB565:
751 case GrGLFormat::kRGBA16F:
752 case GrGLFormat::kR16F:
753 case GrGLFormat::kLUMINANCE16F:
754 case GrGLFormat::kRGB8:
755 case GrGLFormat::kRG8:
756 case GrGLFormat::kRGB10_A2:
757 case GrGLFormat::kRGBA4:
758 case GrGLFormat::kSRGB8_ALPHA8:
759 case GrGLFormat::kR16:
760 case GrGLFormat::kRG16:
761 case GrGLFormat::kRGBA16:
762 case GrGLFormat::kRG16F:
763 case GrGLFormat::kSTENCIL_INDEX8:
764 case GrGLFormat::kSTENCIL_INDEX16:
765 case GrGLFormat::kDEPTH24_STENCIL8:
766 case GrGLFormat::kUnknown:
767 return false;
768 }
769 SkUNREACHABLE;
770 }
771
772