blob: e112fec046f4a896d49d3b2e0ef47336f4e6cd01 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopianb1a39d62009-05-27 20:38:06 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** 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 Agopianb1a39d62009-05-27 20:38:06 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopianb1a39d62009-05-27 20:38:06 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** 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 Agopianb1a39d62009-05-27 20:38:06 -070014 ** 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 Hall47743382013-02-08 11:13:46 -080023#include <GLES3/gl3.h>
24#include <GLES3/gl3ext.h>
Mathias Agopianb1a39d62009-05-27 20:38:06 -070025#include <GLES2/gl2ext.h>
26
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopian39c24a22013-04-04 23:17:56 -070030#include "../hooks.h"
31#include "../egl_impl.h"
Mathias Agopianb1a39d62009-05-27 20:38:06 -070032
33using 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 Hall30a41aa2014-05-30 23:32:12 -070043#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 Sand46b42532013-03-27 10:58:06 -070052
Elliott Hughes288870e2013-02-13 17:30:54 -080053 #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
Mathias Agopian673d2db2009-10-14 02:39:53 -070054
Mathias Agopiane0ea89c2013-06-14 19:08:36 -070055 #define API_ENTRY(_api) __attribute__((noinline)) _api
Mathias Agopianb1a39d62009-05-27 20:38:06 -070056
57 #define CALL_GL_API(_api, ...) \
58 asm volatile( \
Mathias Agopian673d2db2009-10-14 02:39:53 -070059 GET_TLS(r12) \
Mathias Agopianb1a39d62009-05-27 20:38:06 -070060 "ldr r12, [r12, %[tls]] \n" \
61 "cmp r12, #0 \n" \
62 "ldrne pc, [r12, %[api]] \n" \
Mathias Agopianb1a39d62009-05-27 20:38:06 -070063 : \
64 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
Mathias Agopian618fa102009-10-14 02:06:37 -070065 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
Jesse Hall30a41aa2014-05-30 23:32:12 -070066 : "r12" \
Mathias Agopianb1a39d62009-05-27 20:38:06 -070067 );
Mathias Agopian673d2db2009-10-14 02:39:53 -070068
Jesse Hall30a41aa2014-05-30 23:32:12 -070069#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__)
mwajdeczc80aafa2014-05-26 13:56:37 +020088
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 Hall30a41aa2014-05-30 23:32:12 -0700106#elif defined(__x86_64__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200107
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 Hall30a41aa2014-05-30 23:32:12 -0700125#elif defined(__mips__)
Duane Sand46b42532013-03-27 10:58:06 -0700126
127 #define API_ENTRY(_api) __attribute__((noinline)) _api
128
129 #define CALL_GL_API(_api, ...) \
Jesse Hall441f6942013-03-30 23:22:19 -0700130 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 Sand46b42532013-03-27 10:58:06 -0700134 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 Hall441f6942013-03-30 23:22:19 -0700148 : [fn] "=c"(_fn), \
149 [tls] "=&r"(_tls), \
150 [t0] "=&r"(_t0), \
151 [v0] "=&r"(_v0) \
Duane Sand46b42532013-03-27 10:58:06 -0700152 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
153 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
154 : \
155 );
156
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700157#endif
158
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700159#define CALL_GL_API_RETURN(_api, ...) \
160 CALL_GL_API(_api, __VA_ARGS__) \
161 return 0;
162
163
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700164
165extern "C" {
Jesse Hall47743382013-02-08 11:13:46 -0800166#include "gl3_api.in"
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700167#include "gl2ext_api.in"
Jesse Hall47743382013-02-08 11:13:46 -0800168#include "gl3ext_api.in"
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700169}
170
171#undef API_ENTRY
172#undef CALL_GL_API
173#undef CALL_GL_API_RETURN
174
Mathias Agopian48d438d2012-01-28 21:44:00 -0800175/*
176 * glGetString() is special because we expose some extensions in the wrapper
177 */
178
179extern "C" const GLubyte * __glGetString(GLenum name);
180
181const GLubyte * glGetString(GLenum name)
182{
183 const GLubyte * ret = egl_get_string_for_current_context(name);
184 if (ret == NULL) {
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700185 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
186 ret = _c->glGetString(name);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800187 }
188 return ret;
189}