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 
GrGLClearErr(const GrGLInterface * gl)15 void GrGLClearErr(const GrGLInterface* gl) {
16     while (GR_GL_NO_ERROR != gl->fFunctions.fGetError()) {}
17 }
18 
19 namespace {
get_error_string(uint32_t err)20 const char *get_error_string(uint32_t err) {
21     switch (err) {
22     case GR_GL_NO_ERROR:
23         return "";
24     case GR_GL_INVALID_ENUM:
25         return "Invalid Enum";
26     case GR_GL_INVALID_VALUE:
27         return "Invalid Value";
28     case GR_GL_INVALID_OPERATION:
29         return "Invalid Operation";
30     case GR_GL_OUT_OF_MEMORY:
31         return "Out of Memory";
32     case GR_GL_CONTEXT_LOST:
33         return "Context Lost";
34     }
35     return "Unknown";
36 }
37 }
38 
GrGLCheckErr(const GrGLInterface * gl,const char * location,const char * call)39 void GrGLCheckErr(const GrGLInterface* gl,
40                   const char* location,
41                   const char* call) {
42     uint32_t err = GR_GL_GET_ERROR(gl);
43     if (GR_GL_NO_ERROR != err) {
44         SkDebugf("---- glGetError 0x%x(%s)", err, get_error_string(err));
45         if (location) {
46             SkDebugf(" at\n\t%s", location);
47         }
48         if (call) {
49             SkDebugf("\n\t\t%s", call);
50         }
51         SkDebugf("\n");
52     }
53 }
54 
55 ///////////////////////////////////////////////////////////////////////////////
56 
57 #if GR_GL_LOG_CALLS
58     bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START);
59 #endif
60 
61 #if GR_GL_CHECK_ERROR
62     bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START);
63 #endif
64 
65 ///////////////////////////////////////////////////////////////////////////////
66 
GrGLGetStandardInUseFromString(const char * versionString)67 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString) {
68     if (nullptr == versionString) {
69         SkDebugf("nullptr GL version string.");
70         return kNone_GrGLStandard;
71     }
72 
73     int major, minor;
74 
75     // check for desktop
76     int n = sscanf(versionString, "%d.%d", &major, &minor);
77     if (2 == n) {
78         return kGL_GrGLStandard;
79     }
80 
81     // WebGL might look like "OpenGL ES 2.0 (WebGL 1.0 (OpenGL ES 2.0 Chromium))"
82     int esMajor, esMinor;
83     n = sscanf(versionString, "OpenGL ES %d.%d (WebGL %d.%d", &esMajor, &esMinor, &major, &minor);
84     if (4 == n) {
85         return kWebGL_GrGLStandard;
86     }
87 
88     // check for ES 1
89     char profile[2];
90     n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, &major, &minor);
91     if (4 == n) {
92         // we no longer support ES1.
93         return kNone_GrGLStandard;
94     }
95 
96     // check for ES2
97     n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
98     if (2 == n) {
99         return kGLES_GrGLStandard;
100     }
101     return kNone_GrGLStandard;
102 }
103 
GrGLGetDriverInfo(GrGLStandard standard,GrGLVendor vendor,const char * rendererString,const char * versionString,GrGLDriver * outDriver,GrGLDriverVersion * outVersion)104 void GrGLGetDriverInfo(GrGLStandard standard,
105                        GrGLVendor vendor,
106                        const char* rendererString,
107                        const char* versionString,
108                        GrGLDriver* outDriver,
109                        GrGLDriverVersion* outVersion) {
110     int major, minor, rev, driverMajor, driverMinor, driverPoint;
111 
112     *outDriver = kUnknown_GrGLDriver;
113     *outVersion = GR_GL_DRIVER_UNKNOWN_VER;
114     // These null checks are for test GL contexts that return nullptr in their
115     // glGetString implementation.
116     if (!rendererString) {
117         rendererString = "";
118     }
119     if (!versionString) {
120         versionString = "";
121     }
122 
123     static const char kChromium[] = "Chromium";
124     char suffix[SK_ARRAY_COUNT(kChromium)];
125     if (0 == strcmp(rendererString, kChromium) ||
126         (3 == sscanf(versionString, "OpenGL ES %d.%d %8s", &major, &minor, suffix) &&
127          0 == strcmp(kChromium, suffix))) {
128         *outDriver = kChromium_GrGLDriver;
129         return;
130     }
131 
132     if (GR_IS_GR_GL(standard)) {
133         if (kNVIDIA_GrGLVendor == vendor) {
134             *outDriver = kNVIDIA_GrGLDriver;
135             int n = sscanf(versionString, "%d.%d.%d NVIDIA %d.%d",
136                            &major, &minor, &rev, &driverMajor, &driverMinor);
137             // Some older NVIDIA drivers don't report the driver version.
138             if (5 == n) {
139                 *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
140             }
141             return;
142         }
143         int n = sscanf(versionString, "%d.%d Mesa %d.%d",
144                        &major, &minor, &driverMajor, &driverMinor);
145         if (4 != n) {
146             n = sscanf(versionString, "%d.%d (Core Profile) Mesa %d.%d",
147                        &major, &minor, &driverMajor, &driverMinor);
148         }
149         if (4 == n) {
150             *outDriver = kMesa_GrGLDriver;
151             *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
152             return;
153         }
154     } else if (GR_IS_GR_GL_ES(standard)) {
155         if (kNVIDIA_GrGLVendor == vendor) {
156             *outDriver = kNVIDIA_GrGLDriver;
157             int n = sscanf(versionString, "OpenGL ES %d.%d NVIDIA %d.%d",
158                            &major, &minor, &driverMajor, &driverMinor);
159             // Some older NVIDIA drivers don't report the driver version.
160             if (4 == n) {
161                 *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
162             }
163             return;
164         }
165 
166         int n = sscanf(versionString, "OpenGL ES %d.%d Mesa %d.%d",
167                        &major, &minor, &driverMajor, &driverMinor);
168         if (4 == n) {
169             *outDriver = kMesa_GrGLDriver;
170             *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
171             return;
172         }
173         if (0 == strncmp("ANGLE", rendererString, 5)) {
174             *outDriver = kANGLE_GrGLDriver;
175             n = sscanf(versionString, "OpenGL ES %d.%d (ANGLE %d.%d", &major, &minor, &driverMajor,
176                                                                       &driverMinor);
177             if (4 == n) {
178                 *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
179             }
180             return;
181         }
182     }
183 
184     if (kGoogle_GrGLVendor == vendor) {
185         // Swiftshader is the only Google vendor at the moment
186         *outDriver = kSwiftShader_GrGLDriver;
187 
188         // Swiftshader has a strange version string: w.x.y.z  Going to arbitrarily ignore
189         // y and assume w,x and z are major, minor, point.
190         // As of writing, version is 4.0.0.6
191         int n = sscanf(versionString, "OpenGL ES %d.%d SwiftShader %d.%d.0.%d", &major, &minor,
192                        &driverMajor, &driverMinor, &driverPoint);
193         if (5 == n) {
194             *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, driverPoint);
195         }
196         return;
197     }
198 
199     if (kIntel_GrGLVendor == vendor) {
200         // We presume we're on the Intel driver since it hasn't identified itself as Mesa.
201         *outDriver = kIntel_GrGLDriver;
202 
203         //This is how the macOS version strings are structured. This might be different on different
204         // OSes.
205         int n = sscanf(versionString, "%d.%d INTEL-%d.%d.%d", &major, &minor, &driverMajor,
206                        &driverMinor, &driverPoint);
207         if (5 == n) {
208             *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, driverPoint);
209         }
210     }
211 
212     if (kQualcomm_GrGLVendor == vendor) {
213         *outDriver = kQualcomm_GrGLDriver;
214         int n = sscanf(versionString, "OpenGL ES %d.%d V@%d.%d", &major, &minor, &driverMajor,
215                        &driverMinor);
216         if (4 == n) {
217             *outVersion = GR_GL_DRIVER_VER(driverMajor, driverMinor, 0);
218         }
219         return;
220     }
221     static constexpr char kEmulatorPrefix[] = "Android Emulator OpenGL ES Translator";
222     if (0 == strncmp(kEmulatorPrefix, rendererString, strlen(kEmulatorPrefix))) {
223         *outDriver = kAndroidEmulator_GrGLDriver;
224     }
225 }
226 
GrGLGetVersionFromString(const char * versionString)227 GrGLVersion GrGLGetVersionFromString(const char* versionString) {
228     if (nullptr == versionString) {
229         SkDebugf("nullptr GL version string.");
230         return GR_GL_INVALID_VER;
231     }
232 
233     int major, minor;
234 
235     // check for mesa
236     int mesaMajor, mesaMinor;
237     int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor);
238     if (4 == n) {
239         return GR_GL_VER(major, minor);
240     }
241 
242     n = sscanf(versionString, "%d.%d", &major, &minor);
243     if (2 == n) {
244         return GR_GL_VER(major, minor);
245     }
246 
247     // WebGL might look like "OpenGL ES 2.0 (WebGL 1.0 (OpenGL ES 2.0 Chromium))"
248     int esMajor, esMinor;
249     n = sscanf(versionString, "OpenGL ES %d.%d (WebGL %d.%d", &esMajor, &esMinor, &major, &minor);
250     if (4 == n) {
251         return GR_GL_VER(major, minor);
252     }
253 
254     char profile[2];
255     n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
256                &major, &minor);
257     if (4 == n) {
258         return GR_GL_VER(major, minor);
259     }
260 
261     n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor);
262     if (2 == n) {
263         return GR_GL_VER(major, minor);
264     }
265 
266     return GR_GL_INVALID_VER;
267 }
268 
GrGLGetGLSLVersionFromString(const char * versionString)269 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) {
270     if (nullptr == versionString) {
271         SkDebugf("nullptr GLSL version string.");
272         return GR_GLSL_INVALID_VER;
273     }
274 
275     int major, minor;
276 
277     int n = sscanf(versionString, "%d.%d", &major, &minor);
278     if (2 == n) {
279         return GR_GLSL_VER(major, minor);
280     }
281 
282     n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor);
283     if (2 == n) {
284         return GR_GLSL_VER(major, minor);
285     }
286 
287 #ifdef SK_BUILD_FOR_ANDROID
288     // android hack until the gpu vender updates their drivers
289     n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor);
290     if (2 == n) {
291         return GR_GLSL_VER(major, minor);
292     }
293 #endif
294 
295     return GR_GLSL_INVALID_VER;
296 }
297 
GrGLGetVendorFromString(const char * vendorString)298 GrGLVendor GrGLGetVendorFromString(const char* vendorString) {
299     if (vendorString) {
300         if (0 == strcmp(vendorString, "ARM")) {
301             return kARM_GrGLVendor;
302         }
303         if (0 == strcmp(vendorString, "Google Inc.")) {
304             return kGoogle_GrGLVendor;
305         }
306         if (0 == strcmp(vendorString, "Imagination Technologies")) {
307             return kImagination_GrGLVendor;
308         }
309         if (0 == strncmp(vendorString, "Intel ", 6) || 0 == strcmp(vendorString, "Intel")) {
310             return kIntel_GrGLVendor;
311         }
312         if (0 == strcmp(vendorString, "Qualcomm")) {
313             return kQualcomm_GrGLVendor;
314         }
315         if (0 == strcmp(vendorString, "NVIDIA Corporation")) {
316             return kNVIDIA_GrGLVendor;
317         }
318         if (0 == strcmp(vendorString, "ATI Technologies Inc.")) {
319             return kATI_GrGLVendor;
320         }
321     }
322     return kOther_GrGLVendor;
323 }
324 
is_renderer_angle(const char * rendererString)325 static bool is_renderer_angle(const char* rendererString) {
326     static constexpr char kHeader[] = "ANGLE ";
327     static constexpr size_t kHeaderLength = SK_ARRAY_COUNT(kHeader) - 1;
328     return rendererString && 0 == strncmp(rendererString, kHeader, kHeaderLength);
329 }
330 
GrGLGetRendererFromStrings(const char * rendererString,const GrGLExtensions & extensions)331 GrGLRenderer GrGLGetRendererFromStrings(const char* rendererString,
332                                         const GrGLExtensions& extensions) {
333     if (rendererString) {
334         static const char kTegraStr[] = "NVIDIA Tegra";
335         if (0 == strncmp(rendererString, kTegraStr, SK_ARRAY_COUNT(kTegraStr) - 1)) {
336             // Tegra strings are not very descriptive. We distinguish between the modern and legacy
337             // architectures by the presence of NV_path_rendering.
338             return extensions.has("GL_NV_path_rendering") ? kTegra_GrGLRenderer
339                                                           : kTegra_PreK1_GrGLRenderer;
340         }
341         int lastDigit;
342         int n = sscanf(rendererString, "PowerVR SGX 54%d", &lastDigit);
343         if (1 == n && lastDigit >= 0 && lastDigit <= 9) {
344             return kPowerVR54x_GrGLRenderer;
345         }
346         // certain iOS devices also use PowerVR54x GPUs
347         static const char kAppleA4Str[] = "Apple A4";
348         static const char kAppleA5Str[] = "Apple A5";
349         static const char kAppleA6Str[] = "Apple A6";
350         if (0 == strncmp(rendererString, kAppleA4Str,
351                          SK_ARRAY_COUNT(kAppleA4Str)-1) ||
352             0 == strncmp(rendererString, kAppleA5Str,
353                          SK_ARRAY_COUNT(kAppleA5Str)-1) ||
354             0 == strncmp(rendererString, kAppleA6Str,
355                          SK_ARRAY_COUNT(kAppleA6Str)-1)) {
356             return kPowerVR54x_GrGLRenderer;
357         }
358         static const char kPowerVRRogueStr[] = "PowerVR Rogue";
359         static const char kAppleA7Str[] = "Apple A7";
360         static const char kAppleA8Str[] = "Apple A8";
361         if (0 == strncmp(rendererString, kPowerVRRogueStr,
362                          SK_ARRAY_COUNT(kPowerVRRogueStr)-1) ||
363             0 == strncmp(rendererString, kAppleA7Str,
364                          SK_ARRAY_COUNT(kAppleA7Str)-1) ||
365             0 == strncmp(rendererString, kAppleA8Str,
366                          SK_ARRAY_COUNT(kAppleA8Str)-1)) {
367             return kPowerVRRogue_GrGLRenderer;
368         }
369         int adrenoNumber;
370         n = sscanf(rendererString, "Adreno (TM) %d", &adrenoNumber);
371         if (1 == n) {
372             if (adrenoNumber >= 300) {
373                 if (adrenoNumber < 400) {
374                     return kAdreno3xx_GrGLRenderer;
375                 }
376                 if (adrenoNumber < 500) {
377                     return adrenoNumber >= 430
378                             ? kAdreno430_GrGLRenderer : kAdreno4xx_other_GrGLRenderer;
379                 }
380                 if (adrenoNumber < 600) {
381                     return kAdreno5xx_GrGLRenderer;
382                 }
383             }
384         }
385         if (0 == strcmp("Google SwiftShader", rendererString)) {
386             return kGoogleSwiftShader_GrGLRenderer;
387         }
388 
389         if (const char* intelString = strstr(rendererString, "Intel")) {
390             // These generic strings seem to always come from Haswell: Iris 5100 or Iris Pro 5200
391             if (0 == strcmp("Intel Iris OpenGL Engine", intelString) ||
392                 0 == strcmp("Intel Iris Pro OpenGL Engine", intelString)) {
393                 return kIntelHaswell_GrGLRenderer;
394             }
395             if (strstr(intelString, "Sandybridge")) {
396                 return kIntelSandyBridge_GrGLRenderer;
397             }
398             if (strstr(intelString, "Bay Trail")) {
399                 return kIntelValleyView_GrGLRenderer;
400             }
401             // There are many possible intervening strings here:
402             // 'Intel(R)' is a common prefix
403             // 'Iris' may appear, followed by '(R)' or '(TM)'
404             // 'Iris' can then be followed by 'Graphics', 'Pro Graphics', or 'Plus Graphics'
405             // If 'Iris' isn't there, we might have 'HD Graphics' or 'UHD Graphics'
406             //
407             // In all cases, though, we end with 'Graphics ', an optional 'P', and a number,
408             // so just skip to that and handle two cases:
409             if (const char* intelGfxString = strstr(intelString, "Graphics")) {
410                 int intelNumber;
411                 if (sscanf(intelGfxString, "Graphics %d", &intelNumber) ||
412                     sscanf(intelGfxString, "Graphics P%d", &intelNumber)) {
413 
414                     if (intelNumber == 2000 || intelNumber == 3000) {
415                         return kIntelSandyBridge_GrGLRenderer;
416                     }
417                     if (intelNumber == 2500 || intelNumber == 4000) {
418                         return kIntelIvyBridge_GrGLRenderer;
419                     }
420                     if (intelNumber >= 4200 && intelNumber <= 5200) {
421                         return kIntelHaswell_GrGLRenderer;
422                     }
423                     if (intelNumber >= 400 && intelNumber <= 405) {
424                         return kIntelCherryView_GrGLRenderer;
425                     }
426                     if (intelNumber >= 5300 && intelNumber <= 6300) {
427                         return kIntelBroadwell_GrGLRenderer;
428                     }
429                     if (intelNumber >= 500 && intelNumber <= 505) {
430                         return kIntelApolloLake_GrGLRenderer;
431                     }
432                     if (intelNumber >= 510 && intelNumber <= 580) {
433                         return kIntelSkyLake_GrGLRenderer;
434                     }
435                     if (intelNumber >= 600 && intelNumber <= 605) {
436                         return kIntelGeminiLake_GrGLRenderer;
437                     }
438                     // 610 and 630 are reused from KabyLake to CoffeeLake. The CoffeeLake variants
439                     // are "UHD Graphics", while the KabyLake ones are "HD Graphics"
440                     if (intelNumber == 610 || intelNumber == 630) {
441                         return strstr(intelString, "UHD") ? kIntelCoffeeLake_GrGLRenderer
442                                                           : kIntelKabyLake_GrGLRenderer;
443                     }
444                     if (intelNumber >= 610 && intelNumber <= 650) {
445                         return kIntelKabyLake_GrGLRenderer;
446                     }
447                     if (intelNumber == 655) {
448                         return kIntelCoffeeLake_GrGLRenderer;
449                     }
450                     if (intelNumber >= 910 && intelNumber <= 950) {
451                         return kIntelIceLake_GrGLRenderer;
452                     }
453                 }
454             }
455         }
456 
457         // The AMD string can have a somewhat arbitrary preamble (see skbug.com/7195)
458         if (const char* amdString = strstr(rendererString, "Radeon")) {
459             char amdGeneration, amdTier, amdRevision;
460             n = sscanf(amdString, "Radeon (TM) R9 M%c%c%c",
461                                        &amdGeneration, &amdTier, &amdRevision);
462             if (3 == n) {
463                 if ('4' == amdGeneration) {
464                     return kAMDRadeonR9M4xx_GrGLRenderer;
465                 }
466             }
467 
468             char amd0, amd1, amd2;
469             n = sscanf(amdString, "Radeon HD 7%c%c%c Series", &amd0, &amd1, &amd2);
470             if (3 == n) {
471                 return kAMDRadeonHD7xxx_GrGLRenderer;
472             }
473         }
474 
475         if (strstr(rendererString, "llvmpipe")) {
476             return kGalliumLLVM_GrGLRenderer;
477         }
478         static const char kMaliTStr[] = "Mali-T";
479         if (0 == strncmp(rendererString, kMaliTStr, SK_ARRAY_COUNT(kMaliTStr) - 1)) {
480             return kMaliT_GrGLRenderer;
481         }
482         int mali400Num;
483         if (1 == sscanf(rendererString, "Mali-%d", &mali400Num) && mali400Num >= 400 &&
484             mali400Num < 500) {
485             return kMali4xx_GrGLRenderer;
486         }
487         if (is_renderer_angle(rendererString)) {
488             return kANGLE_GrGLRenderer;
489         }
490     }
491     return kOther_GrGLRenderer;
492 }
493 
GrGLGetANGLEInfoFromString(const char * rendererString,GrGLANGLEBackend * backend,GrGLANGLEVendor * vendor,GrGLANGLERenderer * renderer)494 void GrGLGetANGLEInfoFromString(const char* rendererString, GrGLANGLEBackend* backend,
495                                 GrGLANGLEVendor* vendor, GrGLANGLERenderer* renderer) {
496     *backend = GrGLANGLEBackend::kUnknown;
497     *vendor = GrGLANGLEVendor::kUnknown;
498     *renderer = GrGLANGLERenderer::kUnknown;
499     if (!is_renderer_angle(rendererString)) {
500         return;
501     }
502     if (strstr(rendererString, "Intel")) {
503         *vendor = GrGLANGLEVendor::kIntel;
504 
505         const char* modelStr;
506         int modelNumber;
507         if ((modelStr = strstr(rendererString, "HD Graphics")) &&
508             (1 == sscanf(modelStr, "HD Graphics %i", &modelNumber) ||
509              1 == sscanf(modelStr, "HD Graphics P%i", &modelNumber))) {
510             switch (modelNumber) {
511                 case 2000:
512                 case 3000:
513                     *renderer = GrGLANGLERenderer::kSandyBridge;
514                     break;
515                 case 4000:
516                 case 2500:
517                     *renderer = GrGLANGLERenderer::kIvyBridge;
518                     break;
519                 case 510:
520                 case 515:
521                 case 520:
522                 case 530:
523                     *renderer = GrGLANGLERenderer::kSkylake;
524                     break;
525             }
526         } else if ((modelStr = strstr(rendererString, "Iris")) &&
527                    (1 == sscanf(modelStr, "Iris(TM) Graphics %i", &modelNumber) ||
528                     1 == sscanf(modelStr, "Iris(TM) Pro Graphics %i", &modelNumber) ||
529                     1 == sscanf(modelStr, "Iris(TM) Pro Graphics P%i", &modelNumber))) {
530             switch (modelNumber) {
531                 case 540:
532                 case 550:
533                 case 555:
534                 case 580:
535                     *renderer = GrGLANGLERenderer::kSkylake;
536                     break;
537             }
538         }
539     }
540     if (strstr(rendererString, "Direct3D11")) {
541         *backend = GrGLANGLEBackend::kD3D11;
542     } else if (strstr(rendererString, "Direct3D9")) {
543         *backend = GrGLANGLEBackend::kD3D9;
544     } else if (strstr(rendererString, "OpenGL")) {
545         *backend = GrGLANGLEBackend::kOpenGL;
546     }
547 }
548 
GrGLGetVersion(const GrGLInterface * gl)549 GrGLVersion GrGLGetVersion(const GrGLInterface* gl) {
550     const GrGLubyte* v;
551     GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION));
552     return GrGLGetVersionFromString((const char*) v);
553 }
554 
GrGLGetGLSLVersion(const GrGLInterface * gl)555 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) {
556     const GrGLubyte* v;
557     GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
558     return GrGLGetGLSLVersionFromString((const char*) v);
559 }
560 
GrGLGetVendor(const GrGLInterface * gl)561 GrGLVendor GrGLGetVendor(const GrGLInterface* gl) {
562     const GrGLubyte* v;
563     GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR));
564     return GrGLGetVendorFromString((const char*) v);
565 }
566 
GrGLGetRenderer(const GrGLInterface * gl)567 GrGLRenderer GrGLGetRenderer(const GrGLInterface* gl) {
568     const GrGLubyte* rendererString;
569     GR_GL_CALL_RET(gl, rendererString, GetString(GR_GL_RENDERER));
570 
571     return GrGLGetRendererFromStrings((const char*)rendererString, gl->fExtensions);
572 }
573 
GrToGLStencilFunc(GrStencilTest test)574 GrGLenum GrToGLStencilFunc(GrStencilTest test) {
575     static const GrGLenum gTable[kGrStencilTestCount] = {
576         GR_GL_ALWAYS,           // kAlways
577         GR_GL_NEVER,            // kNever
578         GR_GL_GREATER,          // kGreater
579         GR_GL_GEQUAL,           // kGEqual
580         GR_GL_LESS,             // kLess
581         GR_GL_LEQUAL,           // kLEqual
582         GR_GL_EQUAL,            // kEqual
583         GR_GL_NOTEQUAL,         // kNotEqual
584     };
585     GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
586     GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
587     GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
588     GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
589     GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
590     GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
591     GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
592     GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
593     SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
594 
595     return gTable[(int)test];
596 }
597 
GrGLFormatIsCompressed(GrGLFormat format)598 bool GrGLFormatIsCompressed(GrGLFormat format) {
599     switch (format) {
600         case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
601         case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
602             return true;
603 
604         case GrGLFormat::kRGBA8:
605         case GrGLFormat::kR8:
606         case GrGLFormat::kALPHA8:
607         case GrGLFormat::kLUMINANCE8:
608         case GrGLFormat::kBGRA8:
609         case GrGLFormat::kRGB565:
610         case GrGLFormat::kRGBA16F:
611         case GrGLFormat::kR16F:
612         case GrGLFormat::kLUMINANCE16F:
613         case GrGLFormat::kRGB8:
614         case GrGLFormat::kRG8:
615         case GrGLFormat::kRGB10_A2:
616         case GrGLFormat::kRGBA4:
617         case GrGLFormat::kSRGB8_ALPHA8:
618         case GrGLFormat::kR16:
619         case GrGLFormat::kRG16:
620         case GrGLFormat::kRGBA16:
621         case GrGLFormat::kRG16F:
622         case GrGLFormat::kUnknown:
623             return false;
624     }
625     SkUNREACHABLE;
626 }
627 
GrGLFormatToCompressionType(GrGLFormat format,SkImage::CompressionType * compressionType)628 bool GrGLFormatToCompressionType(GrGLFormat format, SkImage::CompressionType* compressionType) {
629     switch (format) {
630         case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
631         case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
632             *compressionType = SkImage::kETC1_CompressionType;
633             return true;
634 
635         case GrGLFormat::kRGBA8:
636         case GrGLFormat::kR8:
637         case GrGLFormat::kALPHA8:
638         case GrGLFormat::kLUMINANCE8:
639         case GrGLFormat::kBGRA8:
640         case GrGLFormat::kRGB565:
641         case GrGLFormat::kRGBA16F:
642         case GrGLFormat::kR16F:
643         case GrGLFormat::kLUMINANCE16F:
644         case GrGLFormat::kRGB8:
645         case GrGLFormat::kRG8:
646         case GrGLFormat::kRGB10_A2:
647         case GrGLFormat::kRGBA4:
648         case GrGLFormat::kSRGB8_ALPHA8:
649         case GrGLFormat::kR16:
650         case GrGLFormat::kRG16:
651         case GrGLFormat::kRGBA16:
652         case GrGLFormat::kRG16F:
653         case GrGLFormat::kUnknown:
654             return false;
655     }
656     SkUNREACHABLE;
657 }
658 
659