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