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 | |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 23 | #include <GLES3/gl3.h> |
| 24 | #include <GLES3/gl3ext.h> |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 25 | #include <GLES2/gl2ext.h> |
| 26 | |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/properties.h> |
| 29 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 30 | #include "../hooks.h" |
| 31 | #include "../egl_impl.h" |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 32 | |
| 33 | using namespace android; |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | // Actual GL entry-points |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | #undef API_ENTRY |
| 40 | #undef CALL_GL_API |
| 41 | #undef CALL_GL_API_RETURN |
| 42 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 43 | #if USE_SLOW_BINDING |
| 44 | |
| 45 | #define API_ENTRY(_api) _api |
| 46 | |
| 47 | #define CALL_GL_API(_api, ...) \ |
| 48 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \ |
| 49 | if (_c) return _c->_api(__VA_ARGS__); |
| 50 | |
| 51 | #elif defined(__arm__) |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 288870e | 2013-02-13 17:30:54 -0800 | [diff] [blame] | 53 | #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n" |
Mathias Agopian | 673d2db | 2009-10-14 02:39:53 -0700 | [diff] [blame] | 54 | |
Mathias Agopian | e0ea89c | 2013-06-14 19:08:36 -0700 | [diff] [blame] | 55 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 56 | |
| 57 | #define CALL_GL_API(_api, ...) \ |
| 58 | asm volatile( \ |
Mathias Agopian | 673d2db | 2009-10-14 02:39:53 -0700 | [diff] [blame] | 59 | GET_TLS(r12) \ |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 60 | "ldr r12, [r12, %[tls]] \n" \ |
| 61 | "cmp r12, #0 \n" \ |
| 62 | "ldrne pc, [r12, %[api]] \n" \ |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 63 | : \ |
| 64 | : [tls] "J"(TLS_SLOT_OPENGL_API*4), \ |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 65 | [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \ |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 66 | : "r12" \ |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 67 | ); |
Mathias Agopian | 673d2db | 2009-10-14 02:39:53 -0700 | [diff] [blame] | 68 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 69 | #elif defined(__aarch64__) |
| 70 | |
| 71 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 72 | |
| 73 | #define CALL_GL_API(_api, ...) \ |
| 74 | asm volatile( \ |
| 75 | "mrs x16, tpidr_el0\n" \ |
| 76 | "ldr x16, [x16, %[tls]]\n" \ |
| 77 | "cbz x16, 1f\n" \ |
| 78 | "ldr x16, [x16, %[api]]\n" \ |
| 79 | "br x16\n" \ |
| 80 | "1:\n" \ |
| 81 | : \ |
| 82 | : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \ |
| 83 | [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 84 | : "x16" \ |
| 85 | ); |
| 86 | |
| 87 | #elif defined(__i386__) |
mwajdecz | c80aafa | 2014-05-26 13:56:37 +0200 | [diff] [blame] | 88 | |
| 89 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 90 | |
| 91 | #define CALL_GL_API(_api, ...) \ |
| 92 | register void** fn; \ |
| 93 | __asm__ volatile( \ |
| 94 | "mov %%gs:0, %[fn]\n" \ |
| 95 | "mov %P[tls](%[fn]), %[fn]\n" \ |
| 96 | "test %[fn], %[fn]\n" \ |
| 97 | "je 1f\n" \ |
| 98 | "jmp *%P[api](%[fn])\n" \ |
| 99 | "1:\n" \ |
| 100 | : [fn] "=r" (fn) \ |
| 101 | : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ |
| 102 | [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 103 | : "cc" \ |
| 104 | ); |
| 105 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 106 | #elif defined(__x86_64__) |
mwajdecz | c80aafa | 2014-05-26 13:56:37 +0200 | [diff] [blame] | 107 | |
| 108 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 109 | |
| 110 | #define CALL_GL_API(_api, ...) \ |
| 111 | register void** fn; \ |
| 112 | __asm__ volatile( \ |
| 113 | "mov %%fs:0, %[fn]\n" \ |
| 114 | "mov %P[tls](%[fn]), %[fn]\n" \ |
| 115 | "test %[fn], %[fn]\n" \ |
| 116 | "je 1f\n" \ |
| 117 | "jmp *%P[api](%[fn])\n" \ |
| 118 | "1:\n" \ |
| 119 | : [fn] "=r" (fn) \ |
| 120 | : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ |
| 121 | [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 122 | : "cc" \ |
| 123 | ); |
| 124 | |
Jesse Hall | 30a41aa | 2014-05-30 23:32:12 -0700 | [diff] [blame] | 125 | #elif defined(__mips__) |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 126 | |
| 127 | #define API_ENTRY(_api) __attribute__((noinline)) _api |
| 128 | |
| 129 | #define CALL_GL_API(_api, ...) \ |
Jesse Hall | 441f694 | 2013-03-30 23:22:19 -0700 | [diff] [blame] | 130 | register unsigned int _t0 asm("t0"); \ |
| 131 | register unsigned int _fn asm("t1"); \ |
| 132 | register unsigned int _tls asm("v1"); \ |
| 133 | register unsigned int _v0 asm("v0"); \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 134 | asm volatile( \ |
| 135 | ".set push\n\t" \ |
| 136 | ".set noreorder\n\t" \ |
| 137 | ".set mips32r2\n\t" \ |
| 138 | "rdhwr %[tls], $29\n\t" \ |
| 139 | "lw %[t0], %[OPENGL_API](%[tls])\n\t" \ |
| 140 | "beqz %[t0], 1f\n\t" \ |
| 141 | " move %[fn],$ra\n\t" \ |
| 142 | "lw %[fn], %[API](%[t0])\n\t" \ |
| 143 | "movz %[fn], $ra, %[fn]\n\t" \ |
| 144 | "1:\n\t" \ |
| 145 | "j %[fn]\n\t" \ |
| 146 | " move %[v0], $0\n\t" \ |
| 147 | ".set pop\n\t" \ |
Jesse Hall | 441f694 | 2013-03-30 23:22:19 -0700 | [diff] [blame] | 148 | : [fn] "=c"(_fn), \ |
| 149 | [tls] "=&r"(_tls), \ |
| 150 | [t0] "=&r"(_t0), \ |
| 151 | [v0] "=&r"(_v0) \ |
Duane Sand | 46b4253 | 2013-03-27 10:58:06 -0700 | [diff] [blame] | 152 | : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \ |
| 153 | [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \ |
| 154 | : \ |
| 155 | ); |
| 156 | |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 157 | #endif |
| 158 | |
Mathias Agopian | e0ea89c | 2013-06-14 19:08:36 -0700 | [diff] [blame] | 159 | #define CALL_GL_API_RETURN(_api, ...) \ |
| 160 | CALL_GL_API(_api, __VA_ARGS__) \ |
| 161 | return 0; |
| 162 | |
| 163 | |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 164 | |
| 165 | extern "C" { |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 166 | #include "gl3_api.in" |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 167 | #include "gl2ext_api.in" |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 168 | #include "gl3ext_api.in" |
Mathias Agopian | b1a39d6 | 2009-05-27 20:38:06 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #undef API_ENTRY |
| 172 | #undef CALL_GL_API |
| 173 | #undef CALL_GL_API_RETURN |
| 174 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 175 | /* |
| 176 | * glGetString() is special because we expose some extensions in the wrapper |
| 177 | */ |
| 178 | |
| 179 | extern "C" const GLubyte * __glGetString(GLenum name); |
| 180 | |
| 181 | const GLubyte * glGetString(GLenum name) |
| 182 | { |
| 183 | const GLubyte * ret = egl_get_string_for_current_context(name); |
| 184 | if (ret == NULL) { |
Mathias Agopian | e0ea89c | 2013-06-14 19:08:36 -0700 | [diff] [blame] | 185 | gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; |
| 186 | ret = _c->glGetString(name); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 187 | } |
| 188 | return ret; |
| 189 | } |