| 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" |
| commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 10 | #include "SkMatrix.h" |
| bsalomon@google.com | f987d1b | 2011-04-04 17:13:52 +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 | |
| bsalomon@google.com | 960d114 | 2013-05-29 13:11:54 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | // Mesa uses a non-standard version string of format: 1.4 Mesa <mesa_major>.<mesa_minor>. |
| 54 | // The mapping of from mesa version to GL version came from here: http://www.mesa3d.org/intro.html |
| 55 | bool get_gl_version_for_mesa(int mesaMajorVersion, int* major, int* minor) { |
| 56 | switch (mesaMajorVersion) { |
| 57 | case 2: |
| 58 | case 3: |
| 59 | case 4: |
| 60 | case 5: |
| 61 | case 6: |
| 62 | *major = 1; |
| 63 | *minor = mesaMajorVersion - 1; |
| 64 | return true; |
| 65 | case 7: |
| 66 | *major = 2; |
| 67 | *minor = 1; |
| 68 | return true; |
| 69 | case 8: |
| 70 | *major = 3; |
| 71 | *minor = 0; |
| 72 | return true; |
| 73 | case 9: |
| 74 | *major = 3; |
| 75 | *minor = 1; |
| 76 | return true; |
| 77 | default: |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| bsalomon@google.com | d5d1049 | 2011-04-28 21:16:31 +0000 | [diff] [blame] | 85 | #if GR_GL_LOG_CALLS |
| 86 | bool gLogCallsGL = !!(GR_GL_LOG_CALLS_START); |
| 87 | #endif |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 88 | |
| bsalomon@google.com | d5d1049 | 2011-04-28 21:16:31 +0000 | [diff] [blame] | 89 | #if GR_GL_CHECK_ERROR |
| 90 | bool gCheckErrorGL = !!(GR_GL_CHECK_ERROR_START); |
| 91 | #endif |
| 92 | |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | GrGLBinding GrGLGetBindingInUseFromString(const char* versionString) { |
| 96 | if (NULL == versionString) { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 97 | SkDEBUGFAIL("NULL GL version string."); |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 98 | return kNone_GrGLBinding; |
| 99 | } |
| 100 | |
| 101 | int major, minor; |
| 102 | |
| 103 | // check for desktop |
| 104 | int n = sscanf(versionString, "%d.%d", &major, &minor); |
| 105 | if (2 == n) { |
| 106 | return kDesktop_GrGLBinding; |
| 107 | } |
| 108 | |
| 109 | // check for ES 1 |
| 110 | char profile[2]; |
| bsalomon@google.com | 960d114 | 2013-05-29 13:11:54 +0000 | [diff] [blame] | 111 | n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, &major, &minor); |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 112 | if (4 == n) { |
| 113 | // we no longer support ES1. |
| 114 | return kNone_GrGLBinding; |
| 115 | } |
| 116 | |
| 117 | // check for ES2 |
| 118 | n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor); |
| 119 | if (2 == n) { |
| bsalomon@google.com | 791816a | 2013-08-15 18:54:39 +0000 | [diff] [blame] | 120 | return kES_GrGLBinding; |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 121 | } |
| 122 | return kNone_GrGLBinding; |
| 123 | } |
| 124 | |
| commit-bot@chromium.org | 459104c | 2013-06-14 14:42:56 +0000 | [diff] [blame] | 125 | bool GrGLIsMesaFromVersionString(const char* versionString) { |
| 126 | int major, minor, mesaMajor, mesaMinor; |
| 127 | int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor); |
| 128 | return 4 == n; |
| 129 | } |
| 130 | |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 131 | GrGLVersion GrGLGetVersionFromString(const char* versionString) { |
| 132 | if (NULL == versionString) { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 133 | SkDEBUGFAIL("NULL GL version string."); |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int major, minor; |
| 138 | |
| bsalomon@google.com | 960d114 | 2013-05-29 13:11:54 +0000 | [diff] [blame] | 139 | // check for mesa |
| 140 | int mesaMajor, mesaMinor; |
| 141 | int n = sscanf(versionString, "%d.%d Mesa %d.%d", &major, &minor, &mesaMajor, &mesaMinor); |
| 142 | if (4 == n) { |
| 143 | if (get_gl_version_for_mesa(mesaMajor, &major, &minor)) { |
| 144 | return GR_GL_VER(major, minor); |
| 145 | } else { |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | n = sscanf(versionString, "%d.%d", &major, &minor); |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 151 | if (2 == n) { |
| 152 | return GR_GL_VER(major, minor); |
| 153 | } |
| 154 | |
| 155 | char profile[2]; |
| 156 | n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1, |
| 157 | &major, &minor); |
| 158 | if (4 == n) { |
| 159 | return GR_GL_VER(major, minor); |
| 160 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 161 | |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 162 | n = sscanf(versionString, "OpenGL ES %d.%d", &major, &minor); |
| 163 | if (2 == n) { |
| 164 | return GR_GL_VER(major, minor); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString) { |
| 171 | if (NULL == versionString) { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 172 | SkDEBUGFAIL("NULL GLSL version string."); |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int major, minor; |
| 177 | |
| 178 | int n = sscanf(versionString, "%d.%d", &major, &minor); |
| 179 | if (2 == n) { |
| 180 | return GR_GLSL_VER(major, minor); |
| 181 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 182 | |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 183 | n = sscanf(versionString, "OpenGL ES GLSL ES %d.%d", &major, &minor); |
| 184 | if (2 == n) { |
| 185 | return GR_GLSL_VER(major, minor); |
| 186 | } |
| 187 | |
| 188 | #ifdef SK_BUILD_FOR_ANDROID |
| 189 | // android hack until the gpu vender updates their drivers |
| 190 | n = sscanf(versionString, "OpenGL ES GLSL %d.%d", &major, &minor); |
| 191 | if (2 == n) { |
| 192 | return GR_GLSL_VER(major, minor); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 199 | GrGLVendor GrGLGetVendorFromString(const char* vendorString) { |
| 200 | if (NULL != vendorString) { |
| bsalomon@google.com | 96966a5 | 2013-02-21 16:34:21 +0000 | [diff] [blame] | 201 | if (0 == strcmp(vendorString, "ARM")) { |
| 202 | return kARM_GrGLVendor; |
| 203 | } |
| bsalomon@google.com | 3012ded | 2013-02-22 16:44:04 +0000 | [diff] [blame] | 204 | if (0 == strcmp(vendorString, "Imagination Technologies")) { |
| 205 | return kImagination_GrGLVendor; |
| 206 | } |
| 207 | if (0 == strcmp(vendorString, "Intel")) { |
| 208 | return kIntel_GrGLVendor; |
| 209 | } |
| commit-bot@chromium.org | 7a434a2 | 2013-08-21 14:01:56 +0000 | [diff] [blame] | 210 | if (0 == strcmp(vendorString, "Qualcomm")) { |
| 211 | return kQualcomm_GrGLVendor; |
| 212 | } |
| bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 213 | } |
| bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 214 | return kOther_GrGLVendor; |
| 215 | } |
| 216 | |
| commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 217 | GrGLRenderer GrGLGetRendererFromString(const char* rendererString) { |
| 218 | if (NULL != rendererString) { |
| 219 | if (0 == strcmp(rendererString, "NVIDIA Tegra 3")) { |
| 220 | return kTegra3_GrGLRenderer; |
| 221 | } |
| 222 | } |
| 223 | return kOther_GrGLRenderer; |
| 224 | } |
| 225 | |
| bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 226 | GrGLBinding GrGLGetBindingInUse(const GrGLInterface* gl) { |
| 227 | const GrGLubyte* v; |
| 228 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION)); |
| 229 | return GrGLGetBindingInUseFromString((const char*) v); |
| 230 | } |
| 231 | |
| 232 | GrGLVersion GrGLGetVersion(const GrGLInterface* gl) { |
| 233 | const GrGLubyte* v; |
| 234 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_VERSION)); |
| 235 | return GrGLGetVersionFromString((const char*) v); |
| 236 | } |
| 237 | |
| 238 | GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface* gl) { |
| 239 | const GrGLubyte* v; |
| 240 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 241 | return GrGLGetGLSLVersionFromString((const char*) v); |
| 242 | } |
| bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 243 | |
| 244 | GrGLVendor GrGLGetVendor(const GrGLInterface* gl) { |
| 245 | const GrGLubyte* v; |
| 246 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_VENDOR)); |
| 247 | return GrGLGetVendorFromString((const char*) v); |
| 248 | } |
| commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 249 | |
| commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 250 | GrGLRenderer GrGLGetRenderer(const GrGLInterface* gl) { |
| 251 | const GrGLubyte* v; |
| 252 | GR_GL_CALL_RET(gl, v, GetString(GR_GL_RENDERER)); |
| 253 | return GrGLGetRendererFromString((const char*) v); |
| 254 | } |
| 255 | |
| commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 256 | template<> void GrGLGetMatrix<3>(GrGLfloat* dest, const SkMatrix& src) { |
| 257 | // Col 0 |
| 258 | dest[0] = SkScalarToFloat(src[SkMatrix::kMScaleX]); |
| 259 | dest[1] = SkScalarToFloat(src[SkMatrix::kMSkewY]); |
| 260 | dest[2] = SkScalarToFloat(src[SkMatrix::kMPersp0]); |
| 261 | |
| 262 | // Col 1 |
| 263 | dest[3] = SkScalarToFloat(src[SkMatrix::kMSkewX]); |
| 264 | dest[4] = SkScalarToFloat(src[SkMatrix::kMScaleY]); |
| 265 | dest[5] = SkScalarToFloat(src[SkMatrix::kMPersp1]); |
| 266 | |
| 267 | // Col 2 |
| 268 | dest[6] = SkScalarToFloat(src[SkMatrix::kMTransX]); |
| 269 | dest[7] = SkScalarToFloat(src[SkMatrix::kMTransY]); |
| 270 | dest[8] = SkScalarToFloat(src[SkMatrix::kMPersp2]); |
| 271 | } |
| 272 | |
| 273 | template<> void GrGLGetMatrix<4>(GrGLfloat* dest, const SkMatrix& src) { |
| 274 | // Col 0 |
| 275 | dest[0] = SkScalarToFloat(src[SkMatrix::kMScaleX]); |
| 276 | dest[1] = SkScalarToFloat(src[SkMatrix::kMSkewY]); |
| 277 | dest[2] = 0; |
| 278 | dest[3] = SkScalarToFloat(src[SkMatrix::kMPersp0]); |
| 279 | |
| 280 | // Col 1 |
| 281 | dest[4] = SkScalarToFloat(src[SkMatrix::kMSkewX]); |
| 282 | dest[5] = SkScalarToFloat(src[SkMatrix::kMScaleY]); |
| 283 | dest[6] = 0; |
| 284 | dest[7] = SkScalarToFloat(src[SkMatrix::kMPersp1]); |
| 285 | |
| 286 | // Col 2 |
| 287 | dest[8] = 0; |
| 288 | dest[9] = 0; |
| 289 | dest[10] = 1; |
| 290 | dest[11] = 0; |
| 291 | |
| 292 | // Col 3 |
| 293 | dest[12] = SkScalarToFloat(src[SkMatrix::kMTransX]); |
| 294 | dest[13] = SkScalarToFloat(src[SkMatrix::kMTransY]); |
| 295 | dest[14] = 0; |
| 296 | dest[15] = SkScalarToFloat(src[SkMatrix::kMPersp2]); |
| 297 | } |