bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 9 | #include "GrGLUtil.h" |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 11 | void GrGLClearErr(const GrGLInterface* gl) { |
| 12 | while (GR_GL_NO_ERROR != gl->fGetError()) {} |
bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +0000 | [diff] [blame] | 13 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | 845eafd | 2012-06-18 12:27:29 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | const char *get_error_string(uint32_t err) { |
| 17 | switch (err) { |
| 18 | case GR_GL_NO_ERROR: |
| 19 | return ""; |
| 20 | case GR_GL_INVALID_ENUM: |
| 21 | return "Invalid Enum"; |
| 22 | case GR_GL_INVALID_VALUE: |
| 23 | return "Invalid Value"; |
| 24 | case GR_GL_INVALID_OPERATION: |
| 25 | return "Invalid Operation"; |
| 26 | case GR_GL_OUT_OF_MEMORY: |
| 27 | return "Out of Memory"; |
| 28 | case GR_GL_CONTEXT_LOST: |
| 29 | return "Context Lost"; |
| 30 | } |
| 31 | return "Unknown"; |
| 32 | } |
| 33 | } |
| 34 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 35 | void GrGLCheckErr(const GrGLInterface* gl, |
| 36 | const char* location, |
| 37 | const char* call) { |
| 38 | uint32_t err = GR_GL_GET_ERROR(gl); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 39 | if (GR_GL_NO_ERROR != err) { |
bsalomon@google.com | 845eafd | 2012-06-18 12:27:29 +0000 | [diff] [blame] | 40 | GrPrintf("---- glGetError 0x%x(%s)", err, get_error_string(err)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 41 | if (NULL != location) { |
| 42 | GrPrintf(" at\n\t%s", location); |
| 43 | } |
| 44 | if (NULL != call) { |
| 45 | GrPrintf("\n\t\t%s", call); |
| 46 | } |
| 47 | GrPrintf("\n"); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
bsalomon@google.com | d5d1049 | 2011-04-28 21:16:31 +0000 | [diff] [blame] | 53 | #if GR_GL_LOG_CALLS |
| 54 | bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START); |
| 55 | #endif |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | d5d1049 | 2011-04-28 21:16:31 +0000 | [diff] [blame] | 57 | #if GR_GL_CHECK_ERROR |
| 58 | bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START); |
| 59 | #endif |
| 60 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 | |
| 63 | GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) { |
| 64 | if (NULL == versionString) { |
| 65 | GrAssert(!"NULL GL version string."); |
| 66 | return kNone_GrGLBinding; |
| 67 | } |
| 68 | |
| 69 | int major, minor; |
| 70 | |
| 71 | // check for desktop |
| 72 | int n = sscanf(versionString, "%d.%d", &major, &minor); |
| 73 | if (2 == n) { |
| 74 | return kDesktop_GrGLBinding; |
| 75 | } |
| 76 | |
| 77 | // check for ES 1 |
| 78 | char profile[2]; |
| 79 | n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, |
| 80 | &major, &minor); |
| 81 | if (4 == n) { |
| 82 | // we no longer support ES1. |
| 83 | return kNone_GrGLBinding; |
| 84 | } |
| 85 | |
| 86 | // check for ES2 |
| 87 | n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor); |
| 88 | if (2 == n) { |
| 89 | return kES2_GrGLBinding; |
| 90 | } |
| 91 | return kNone_GrGLBinding; |
| 92 | } |
| 93 | |
| 94 | GrGLVersion GrGLGetVersionFromString(const char* versionString) { |
| 95 | if (NULL == versionString) { |
| 96 | GrAssert(!"NULL GL version string."); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int major, minor; |
| 101 | |
| 102 | int n = sscanf(versionString, "%d.%d", &major, &minor); |
| 103 | if (2 == n) { |
| 104 | return GR_GL_VER(major, minor); |
| 105 | } |
| 106 | |
| 107 | char profile[2]; |
| 108 | n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, |
| 109 | &major, &minor); |
| 110 | if (4 == n) { |
| 111 | return GR_GL_VER(major, minor); |
| 112 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 113 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 114 | n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor); |
| 115 | if (2 == n) { |
| 116 | return GR_GL_VER(major, minor); |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) { |
| 123 | if (NULL == versionString) { |
| 124 | GrAssert(!"NULL GLSL version string."); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int major, minor; |
| 129 | |
| 130 | int n = sscanf(versionString, "%d.%d", &major, &minor); |
| 131 | if (2 == n) { |
| 132 | return GR_GLSL_VER(major, minor); |
| 133 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 134 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 135 | n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor); |
| 136 | if (2 == n) { |
| 137 | return GR_GLSL_VER(major, minor); |
| 138 | } |
| 139 | |
| 140 | #ifdef SK_BUILD_FOR_ANDROID |
| 141 | // android hack until the gpu vender updates their drivers |
| 142 | n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor); |
| 143 | if (2 == n) { |
| 144 | return GR_GLSL_VER(major, minor); |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 151 | GrGLVendor GrGLGetVendorFromString(const char* vendorString) { |
| 152 | if (NULL != vendorString) { |
bsalomon@google.com | 96966a5 | 2013-02-21 16:34:21 +0000 | [diff] [blame] | 153 | if (0 == strcmp(vendorString, "ARM")) { |
| 154 | return kARM_GrGLVendor; |
| 155 | } |
bsalomon@google.com | 3012ded | 2013-02-22 16:44:04 +0000 | [diff] [blame] | 156 | if (0 == strcmp(vendorString, "Imagination Technologies")) { |
| 157 | return kImagination_GrGLVendor; |
| 158 | } |
| 159 | if (0 == strcmp(vendorString, "Intel")) { |
| 160 | return kIntel_GrGLVendor; |
| 161 | } |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 162 | } |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 163 | return kOther_GrGLVendor; |
| 164 | } |
| 165 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 166 | GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) { |
| 167 | const GrGLubyte* v; |
| 168 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION)); |
| 169 | return GrGLGetBindingInUseFromString((const char*) v); |
| 170 | } |
| 171 | |
| 172 | GrGLVersion GrGLGetVersion(const GrGLInterface* gl) { |
| 173 | const GrGLubyte* v; |
| 174 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION)); |
| 175 | return GrGLGetVersionFromString((const char*) v); |
| 176 | } |
| 177 | |
| 178 | GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) { |
| 179 | const GrGLubyte* v; |
| 180 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 181 | return GrGLGetGLSLVersionFromString((const char*) v); |
| 182 | } |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 183 | |
| 184 | GrGLVendor GrGLGetVendor(const GrGLInterface* gl) { |
| 185 | const GrGLubyte* v; |
| 186 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR)); |
| 187 | return GrGLGetVendorFromString((const char*) v); |
| 188 | } |