Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <ctype.h> |
| 18 | #include <string.h> |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #include <sys/ioctl.h> |
| 22 | |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 23 | #include <cutils/log.h> |
| 24 | #include <cutils/properties.h> |
| 25 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 26 | #include "../hooks.h" |
| 27 | #include "../egl_impl.h" |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace android; |
| 30 | |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | // Actual GL entry-points |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | #undef API_ENTRY |
| 36 | #undef CALL_GL_API |
| 37 | #undef CALL_GL_API_RETURN |
| 38 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 39 | #if USE_SLOW_BINDING |
| 40 | |
| 41 | #define API_ENTRY(_api) _api |
| 42 | |
| 43 | #define CALL_GL_API(_api, ...) \ |
| 44 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \ |
| 45 | if (_c) return _c->_api(__VA_ARGS__); |
| 46 | |
| 47 | #elif defined(__arm__) |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 288870e | 2013-02-13 17:30:54 -0800 | [diff] [blame] | 49 | #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n" |
Mathias Agopian | 673d2db | 2009-10-14 02:39:53 -0700 | [diff] [blame] | 50 | |
Mathias Agopian | e0ea89c | 2013-06-14 19:08:36 -0700 | [diff] [blame] | 51 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 52 | |
| 53 | #define CALL_GL_API(_api, ...) \ |
| 54 | asm volatile( \ |
Mathias Agopian | 673d2db | 2009-10-14 02:39:53 -0700 | [diff] [blame] | 55 | GET_TLS(r12) \ |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 56 | "ldr r12, [r12, %[tls]] \n" \ |
| 57 | "cmp r12, #0 \n" \ |
| 58 | "ldrne pc, [r12, %[api]] \n" \ |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 59 | : \ |
| 60 | : [tls] "J"(TLS_SLOT_OPENGL_API*4), \ |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 61 | [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \ |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 62 | : "r12" \ |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 63 | ); |
Mathias Agopian | 673d2db | 2009-10-14 02:39:53 -0700 | [diff] [blame] | 64 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 65 | #elif defined(__aarch64__) |
| 66 | |
| 67 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 68 | |
| 69 | #define CALL_GL_API(_api, ...) \ |
| 70 | asm volatile( \ |
| 71 | "mrs x16, tpidr_el0\n" \ |
| 72 | "ldr x16, [x16, %[tls]]\n" \ |
| 73 | "cbz x16, 1f\n" \ |
| 74 | "ldr x16, [x16, %[api]]\n" \ |
| 75 | "br x16\n" \ |
| 76 | "1:\n" \ |
| 77 | : \ |
| 78 | : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \ |
| 79 | [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 80 | : "x16" \ |
| 81 | ); |
| 82 | |
| 83 | #elif defined(__i386__) |
mwajdecz | c80aafa | 2014-05-26 13:56:37 +0200 | [diff] [blame] | 84 | |
Michal Wajdeczko | 701048c | 2014-08-08 17:26:25 +0200 | [diff] [blame] | 85 | #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api |
mwajdecz | c80aafa | 2014-05-26 13:56:37 +0200 | [diff] [blame] | 86 | |
| 87 | #define CALL_GL_API(_api, ...) \ |
| 88 | register void** fn; \ |
| 89 | __asm__ volatile( \ |
| 90 | "mov %%gs:0, %[fn]\n" \ |
| 91 | "mov %P[tls](%[fn]), %[fn]\n" \ |
| 92 | "test %[fn], %[fn]\n" \ |
| 93 | "je 1f\n" \ |
| 94 | "jmp *%P[api](%[fn])\n" \ |
| 95 | "1:\n" \ |
| 96 | : [fn] "=r" (fn) \ |
| 97 | : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ |
| 98 | [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 99 | : "cc" \ |
| 100 | ); |
| 101 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 102 | #elif defined(__x86_64__) |
mwajdecz | c80aafa | 2014-05-26 13:56:37 +0200 | [diff] [blame] | 103 | |
Michal Wajdeczko | 701048c | 2014-08-08 17:26:25 +0200 | [diff] [blame] | 104 | #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api |
mwajdecz | c80aafa | 2014-05-26 13:56:37 +0200 | [diff] [blame] | 105 | |
| 106 | #define CALL_GL_API(_api, ...) \ |
| 107 | register void** fn; \ |
| 108 | __asm__ volatile( \ |
| 109 | "mov %%fs:0, %[fn]\n" \ |
| 110 | "mov %P[tls](%[fn]), %[fn]\n" \ |
| 111 | "test %[fn], %[fn]\n" \ |
| 112 | "je 1f\n" \ |
| 113 | "jmp *%P[api](%[fn])\n" \ |
| 114 | "1:\n" \ |
| 115 | : [fn] "=r" (fn) \ |
| 116 | : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ |
| 117 | [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 118 | : "cc" \ |
| 119 | ); |
| 120 | |
Duane Sand | 6701fbe | 2014-11-26 13:17:40 -0800 | [diff] [blame] | 121 | #elif defined(__mips64) |
| 122 | |
| 123 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 124 | |
| 125 | #define CALL_GL_API(_api, ...) \ |
Nikola Veljkovic | 5f5f651 | 2014-12-26 17:33:14 +0100 | [diff] [blame] | 126 | register unsigned long _t0 asm("$12"); \ |
| 127 | register unsigned long _fn asm("$25"); \ |
| 128 | register unsigned long _tls asm("$3"); \ |
| 129 | register unsigned long _v0 asm("$2"); \ |
Duane Sand | 6701fbe | 2014-11-26 13:17:40 -0800 | [diff] [blame] | 130 | asm volatile( \ |
| 131 | ".set push\n\t" \ |
| 132 | ".set noreorder\n\t" \ |
| 133 | "rdhwr %[tls], $29\n\t" \ |
| 134 | "ld %[t0], %[OPENGL_API](%[tls])\n\t" \ |
| 135 | "beqz %[t0], 1f\n\t" \ |
| 136 | " move %[fn], $ra\n\t" \ |
| 137 | "ld %[t0], %[API](%[t0])\n\t" \ |
| 138 | "beqz %[t0], 1f\n\t" \ |
| 139 | " nop\n\t" \ |
| 140 | "move %[fn], %[t0]\n\t" \ |
| 141 | "1:\n\t" \ |
| 142 | "jalr $0, %[fn]\n\t" \ |
| 143 | " move %[v0], $0\n\t" \ |
| 144 | ".set pop\n\t" \ |
| 145 | : [fn] "=c"(_fn), \ |
| 146 | [tls] "=&r"(_tls), \ |
| 147 | [t0] "=&r"(_t0), \ |
| 148 | [v0] "=&r"(_v0) \ |
| 149 | : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\ |
| 150 | [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 151 | : \ |
| 152 | ); |
| 153 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 154 | #elif defined(__mips__) |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 155 | |
| 156 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 157 | |
| 158 | #define CALL_GL_API(_api, ...) \ |
Nikola Veljkovic | 5f5f651 | 2014-12-26 17:33:14 +0100 | [diff] [blame] | 159 | register unsigned int _t0 asm("$8"); \ |
| 160 | register unsigned int _fn asm("$25"); \ |
| 161 | register unsigned int _tls asm("$3"); \ |
| 162 | register unsigned int _v0 asm("$2"); \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 163 | asm volatile( \ |
| 164 | ".set push\n\t" \ |
| 165 | ".set noreorder\n\t" \ |
Duane Sand | ecacc3f | 2015-02-02 16:54:15 -0800 | [diff] [blame] | 166 | ".set mips32r2\n\t" \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 167 | "rdhwr %[tls], $29\n\t" \ |
| 168 | "lw %[t0], %[OPENGL_API](%[tls])\n\t" \ |
| 169 | "beqz %[t0], 1f\n\t" \ |
| 170 | " move %[fn],$ra\n\t" \ |
Duane Sand | 6701fbe | 2014-11-26 13:17:40 -0800 | [diff] [blame] | 171 | "lw %[t0], %[API](%[t0])\n\t" \ |
| 172 | "beqz %[t0], 1f\n\t" \ |
| 173 | " nop\n\t" \ |
| 174 | "move %[fn], %[t0]\n\t" \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 175 | "1:\n\t" \ |
Duane Sand | 6701fbe | 2014-11-26 13:17:40 -0800 | [diff] [blame] | 176 | "jalr $0, %[fn]\n\t" \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 177 | " move %[v0], $0\n\t" \ |
| 178 | ".set pop\n\t" \ |
Jesse Hall | 441f694 | 2013-03-30 23:22:19 -0700 | [diff] [blame] | 179 | : [fn] "=c"(_fn), \ |
| 180 | [tls] "=&r"(_tls), \ |
| 181 | [t0] "=&r"(_t0), \ |
| 182 | [v0] "=&r"(_v0) \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 183 | : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \ |
| 184 | [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 185 | : \ |
| 186 | ); |
| 187 | |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 188 | #endif |
| 189 | |
Mathias Agopian | e0ea89c | 2013-06-14 19:08:36 -0700 | [diff] [blame] | 190 | #define CALL_GL_API_RETURN(_api, ...) \ |
| 191 | CALL_GL_API(_api, __VA_ARGS__) \ |
| 192 | return 0; |
| 193 | |
| 194 | |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 195 | |
| 196 | extern "C" { |
Jesse Hall | bbbddb8 | 2014-05-13 21:13:14 -0700 | [diff] [blame] | 197 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Jesse Hall | 4c0596f | 2014-05-13 16:48:35 -0700 | [diff] [blame] | 198 | #include "gl2_api.in" |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 199 | #include "gl2ext_api.in" |
Jesse Hall | bbbddb8 | 2014-05-13 21:13:14 -0700 | [diff] [blame] | 200 | #pragma GCC diagnostic warning "-Wunused-parameter" |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | #undef API_ENTRY |
| 204 | #undef CALL_GL_API |
| 205 | #undef CALL_GL_API_RETURN |
| 206 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 207 | /* |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 208 | * glGetString() and glGetStringi() are special because we expose some |
| 209 | * extensions in the wrapper. Also, wrapping glGetXXX() is required because |
| 210 | * the value returned for GL_NUM_EXTENSIONS may have been altered by the |
| 211 | * injection of the additional extensions. |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 212 | */ |
| 213 | |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 214 | extern "C" { |
| 215 | const GLubyte * __glGetString(GLenum name); |
| 216 | const GLubyte * __glGetStringi(GLenum name, GLuint index); |
| 217 | void __glGetBooleanv(GLenum pname, GLboolean * data); |
| 218 | void __glGetFloatv(GLenum pname, GLfloat * data); |
| 219 | void __glGetIntegerv(GLenum pname, GLint * data); |
| 220 | void __glGetInteger64v(GLenum pname, GLint64 * data); |
| 221 | } |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 222 | |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 223 | const GLubyte * glGetString(GLenum name) { |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 224 | const GLubyte * ret = egl_get_string_for_current_context(name); |
| 225 | if (ret == NULL) { |
Mathias Agopian | e0ea89c | 2013-06-14 19:08:36 -0700 | [diff] [blame] | 226 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
Anshuman Dani | 0627071 | 2013-11-05 21:04:00 +0530 | [diff] [blame] | 227 | if(_c) ret = _c->glGetString(name); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 228 | } |
| 229 | return ret; |
| 230 | } |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 231 | |
| 232 | const GLubyte * glGetStringi(GLenum name, GLuint index) { |
| 233 | const GLubyte * ret = egl_get_string_for_current_context(name, index); |
| 234 | if (ret == NULL) { |
| 235 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
| 236 | if(_c) ret = _c->glGetStringi(name, index); |
| 237 | } |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | void glGetBooleanv(GLenum pname, GLboolean * data) { |
| 242 | if (pname == GL_NUM_EXTENSIONS) { |
| 243 | int num_exts = egl_get_num_extensions_for_current_context(); |
| 244 | if (num_exts >= 0) { |
| 245 | *data = num_exts > 0 ? GL_TRUE : GL_FALSE; |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
| 251 | if (_c) _c->glGetBooleanv(pname, data); |
| 252 | } |
| 253 | |
| 254 | void glGetFloatv(GLenum pname, GLfloat * data) { |
| 255 | if (pname == GL_NUM_EXTENSIONS) { |
| 256 | int num_exts = egl_get_num_extensions_for_current_context(); |
| 257 | if (num_exts >= 0) { |
| 258 | *data = (GLfloat)num_exts; |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
| 264 | if (_c) _c->glGetFloatv(pname, data); |
| 265 | } |
| 266 | |
| 267 | void glGetIntegerv(GLenum pname, GLint * data) { |
| 268 | if (pname == GL_NUM_EXTENSIONS) { |
| 269 | int num_exts = egl_get_num_extensions_for_current_context(); |
| 270 | if (num_exts >= 0) { |
| 271 | *data = (GLint)num_exts; |
| 272 | return; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
| 277 | if (_c) _c->glGetIntegerv(pname, data); |
| 278 | } |
| 279 | |
| 280 | void glGetInteger64v(GLenum pname, GLint64 * data) { |
| 281 | if (pname == GL_NUM_EXTENSIONS) { |
| 282 | int num_exts = egl_get_num_extensions_for_current_context(); |
| 283 | if (num_exts >= 0) { |
| 284 | *data = (GLint64)num_exts; |
| 285 | return; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
| 290 | if (_c) _c->glGetInteger64v(pname, data); |
| 291 | } |