bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #ifndef GrGLUtil_DEFINED |
| 9 | #define GrGLUtil_DEFINED |
| 10 | |
| 11 | #include "gl/GrGLInterface.h" |
bsalomon@google.com | 91bcc94 | 2012-05-07 17:28:41 +0000 | [diff] [blame] | 12 | #include "GrGLDefines.h" |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 14 | class SkMatrix; |
| 15 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 16 | //////////////////////////////////////////////////////////////////////////////// |
| 17 | |
| 18 | typedef uint32_t GrGLVersion; |
| 19 | typedef uint32_t GrGLSLVersion; |
| 20 | |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 21 | /** |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 22 | * The Vendor and Renderer enum values are lazily updated as required. |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 23 | */ |
| 24 | enum GrGLVendor { |
bsalomon@google.com | 96966a5 | 2013-02-21 16:34:21 +0000 | [diff] [blame] | 25 | kARM_GrGLVendor, |
bsalomon@google.com | 3012ded | 2013-02-22 16:44:04 +0000 | [diff] [blame] | 26 | kImagination_GrGLVendor, |
| 27 | kIntel_GrGLVendor, |
commit-bot@chromium.org | 7a434a2 | 2013-08-21 14:01:56 +0000 | [diff] [blame] | 28 | kQualcomm_GrGLVendor, |
bsalomon@google.com | 3012ded | 2013-02-22 16:44:04 +0000 | [diff] [blame] | 29 | |
| 30 | kOther_GrGLVendor |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 33 | enum GrGLRenderer { |
| 34 | kTegra3_GrGLRenderer, |
| 35 | |
| 36 | kOther_GrGLRenderer |
| 37 | }; |
| 38 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 39 | #define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \ |
| 40 | static_cast<int>(minor)) |
| 41 | #define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \ |
| 42 | static_cast<int>(minor)) |
| 43 | |
| 44 | //////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | /** |
| 47 | * Some drivers want the var-int arg to be zero-initialized on input. |
| 48 | */ |
| 49 | #define GR_GL_INIT_ZERO 0 |
| 50 | #define GR_GL_GetIntegerv(gl, e, p) \ |
| 51 | do { \ |
| 52 | *(p) = GR_GL_INIT_ZERO; \ |
| 53 | GR_GL_CALL(gl, GetIntegerv(e, p)); \ |
| 54 | } while (0) |
| 55 | |
| 56 | #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \ |
| 57 | do { \ |
| 58 | *(p) = GR_GL_INIT_ZERO; \ |
| 59 | GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \ |
| 60 | } while (0) |
| 61 | |
| 62 | #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \ |
| 63 | do { \ |
| 64 | *(p) = GR_GL_INIT_ZERO; \ |
| 65 | GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \ |
| 66 | } while (0) |
| 67 | #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \ |
| 68 | do { \ |
| 69 | *(p) = GR_GL_INIT_ZERO; \ |
| 70 | GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \ |
| 71 | } while (0) |
| 72 | |
| 73 | //////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | /** |
| 76 | * Helpers for glGetString() |
| 77 | */ |
| 78 | |
| 79 | // these variants assume caller already has a string from glGetString() |
| 80 | GrGLVersion GrGLGetVersionFromString(const char* versionString); |
| 81 | GrGLBinding GrGLGetBindingInUseFromString(const char* versionString); |
| 82 | GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString); |
commit-bot@chromium.org | 459104c | 2013-06-14 14:42:56 +0000 | [diff] [blame] | 83 | bool GrGLIsMesaFromVersionString(const char* versionString); |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 84 | GrGLVendor GrGLGetVendorFromString(const char* vendorString); |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 85 | GrGLRenderer GrGLGetRendererFromString(const char* rendererString); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 86 | |
| 87 | // these variants call glGetString() |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 88 | GrGLBinding GrGLGetBindingInUse(const GrGLInterface*); |
| 89 | GrGLVersion GrGLGetVersion(const GrGLInterface*); |
| 90 | GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*); |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 91 | GrGLVendor GrGLGetVendor(const GrGLInterface*); |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 92 | GrGLRenderer GrGLGetRenderer(const GrGLInterface*); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Helpers for glGetError() |
| 96 | */ |
| 97 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 98 | void GrGLCheckErr(const GrGLInterface* gl, |
| 99 | const char* location, |
| 100 | const char* call); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 102 | void GrGLClearErr(const GrGLInterface* gl); |
| 103 | |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Helper for converting SkMatrix to a column-major GL float array |
| 106 | */ |
| 107 | template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src); |
| 108 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 109 | //////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | /** |
| 112 | * Macros for using GrGLInterface to make GL calls |
| 113 | */ |
| 114 | |
| 115 | // internal macro to conditionally call glGetError based on compile-time and |
| 116 | // run-time flags. |
| 117 | #if GR_GL_CHECK_ERROR |
| 118 | extern bool gCheckErrorGL; |
| 119 | #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \ |
| 120 | if (gCheckErrorGL) \ |
| 121 | GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X) |
| 122 | #else |
| 123 | #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) |
| 124 | #endif |
| 125 | |
| 126 | // internal macro to conditionally log the gl call using GrPrintf based on |
| 127 | // compile-time and run-time flags. |
| 128 | #if GR_GL_LOG_CALLS |
| 129 | extern bool gLogCallsGL; |
| 130 | #define GR_GL_LOG_CALLS_IMPL(X) \ |
| 131 | if (gLogCallsGL) \ |
| 132 | GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n") |
| 133 | #else |
| 134 | #define GR_GL_LOG_CALLS_IMPL(X) |
| 135 | #endif |
| 136 | |
| 137 | // internal macro that does the per-GL-call callback (if necessary) |
| 138 | #if GR_GL_PER_GL_FUNC_CALLBACK |
| 139 | #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE) |
| 140 | #else |
| 141 | #define GR_GL_CALLBACK_IMPL(IFACE) |
| 142 | #endif |
| 143 | |
| 144 | // makes a GL call on the interface and does any error checking and logging |
| 145 | #define GR_GL_CALL(IFACE, X) \ |
| 146 | do { \ |
| 147 | GR_GL_CALL_NOERRCHECK(IFACE, X); \ |
| 148 | GR_GL_CHECK_ERROR_IMPL(IFACE, X); \ |
| 149 | } while (false) |
| 150 | |
| 151 | // Variant of above that always skips the error check. This is useful when |
| 152 | // the caller wants to do its own glGetError() call and examine the error value. |
| 153 | #define GR_GL_CALL_NOERRCHECK(IFACE, X) \ |
| 154 | do { \ |
| 155 | GR_GL_CALLBACK_IMPL(IFACE); \ |
| 156 | (IFACE)->f##X; \ |
| 157 | GR_GL_LOG_CALLS_IMPL(X); \ |
| 158 | } while (false) |
| 159 | |
| 160 | // same as GR_GL_CALL but stores the return value of the gl call in RET |
| 161 | #define GR_GL_CALL_RET(IFACE, RET, X) \ |
| 162 | do { \ |
| 163 | GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \ |
| 164 | GR_GL_CHECK_ERROR_IMPL(IFACE, X); \ |
| 165 | } while (false) |
| 166 | |
| 167 | // same as GR_GL_CALL_RET but always skips the error check. |
| 168 | #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \ |
| 169 | do { \ |
| 170 | GR_GL_CALLBACK_IMPL(IFACE); \ |
| 171 | (RET) = (IFACE)->f##X; \ |
| 172 | GR_GL_LOG_CALLS_IMPL(X); \ |
| 173 | } while (false) |
| 174 | |
| 175 | // call glGetError without doing a redundant error check or logging. |
| 176 | #define GR_GL_GET_ERROR(IFACE) (IFACE)->fGetError() |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 177 | |
| 178 | #endif |