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