blob: b1b31f82944cf67f0e016b1ea2111bdd51bd983e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 ** Copyright 2007, The Android Open Source Project
3 **
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
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
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
14 ** limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
18#include <string.h>
19#include <errno.h>
20
21#include <sys/ioctl.h>
22
23#include <GLES/gl.h>
24#include <GLES/glext.h>
25
26#include <cutils/log.h>
27#include <cutils/properties.h>
28
Mathias Agopian39c24a22013-04-04 23:17:56 -070029#include "../hooks.h"
30#include "../egl_impl.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32using namespace android;
33
34// ----------------------------------------------------------------------------
35// extensions for the framework
36// ----------------------------------------------------------------------------
37
Mathias Agopiand373c632009-05-08 15:35:17 -070038extern "C" {
39GL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
40 const GLvoid *ptr, GLsizei count);
41GL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride,
42 const GLvoid *pointer, GLsizei count);
43GL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type,
44 GLsizei stride, const GLvoid *pointer, GLsizei count);
45GL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type,
46 GLsizei stride, const GLvoid *pointer, GLsizei count);
Jack Palevich66089a32009-12-08 15:43:51 +080047GL_API void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
48 GLsizei stride, const GLvoid *pointer, GLsizei count);
49GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
50 GLsizei stride, const GLvoid *pointer, GLsizei count);
51GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
52 GLsizei stride, const GLvoid *pointer, GLsizei count);
Mathias Agopiand373c632009-05-08 15:35:17 -070053}
54
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055void glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070056 const GLvoid *ptr, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 glColorPointer(size, type, stride, ptr);
58}
59void glNormalPointerBounds(GLenum type, GLsizei stride,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070060 const GLvoid *pointer, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 glNormalPointer(type, stride, pointer);
62}
63void glTexCoordPointerBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070064 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 glTexCoordPointer(size, type, stride, pointer);
66}
67void glVertexPointerBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070068 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 glVertexPointer(size, type, stride, pointer);
70}
71
Jack Palevich66089a32009-12-08 15:43:51 +080072void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070073 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
Jack Palevich66089a32009-12-08 15:43:51 +080074 glPointSizePointerOES(type, stride, pointer);
75}
76
77GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070078 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
Jack Palevich66089a32009-12-08 15:43:51 +080079 glMatrixIndexPointerOES(size, type, stride, pointer);
80}
81
82GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070083 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
Jack Palevich66089a32009-12-08 15:43:51 +080084 glWeightPointerOES(size, type, stride, pointer);
85}
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087// ----------------------------------------------------------------------------
88// Actual GL entry-points
89// ----------------------------------------------------------------------------
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091#undef API_ENTRY
92#undef CALL_GL_API
93#undef CALL_GL_API_RETURN
94
Jesse Hall30a41aa2014-05-30 23:32:12 -070095#if USE_SLOW_BINDING
96
97 #define API_ENTRY(_api) _api
98
99 #define CALL_GL_API(_api, ...) \
100 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
101 if (_c) return _c->_api(__VA_ARGS__);
102
103#elif defined(__arm__)
Duane Sand46b42532013-03-27 10:58:06 -0700104
Elliott Hughes288870e2013-02-13 17:30:54 -0800105 #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
Mathias Agopian673d2db2009-10-14 02:39:53 -0700106
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700107 #define API_ENTRY(_api) __attribute__((noinline)) _api
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 #define CALL_GL_API(_api, ...) \
110 asm volatile( \
Mathias Agopian673d2db2009-10-14 02:39:53 -0700111 GET_TLS(r12) \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 "ldr r12, [r12, %[tls]] \n" \
113 "cmp r12, #0 \n" \
114 "ldrne pc, [r12, %[api]] \n" \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 : \
116 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
117 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700118 : "r12" \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 );
Mathias Agopian673d2db2009-10-14 02:39:53 -0700120
Jesse Hall30a41aa2014-05-30 23:32:12 -0700121#elif defined(__aarch64__)
122
123 #define API_ENTRY(_api) __attribute__((noinline)) _api
124
125 #define CALL_GL_API(_api, ...) \
126 asm volatile( \
127 "mrs x16, tpidr_el0\n" \
128 "ldr x16, [x16, %[tls]]\n" \
129 "cbz x16, 1f\n" \
130 "ldr x16, [x16, %[api]]\n" \
131 "br x16\n" \
132 "1:\n" \
133 : \
134 : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \
135 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
136 : "x16" \
137 );
138
139#elif defined(__i386__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200140
Michal Wajdeczko701048c2014-08-08 17:26:25 +0200141 #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200142
143 #define CALL_GL_API(_api, ...) \
144 register void* fn; \
145 __asm__ volatile( \
146 "mov %%gs:0, %[fn]\n" \
147 "mov %P[tls](%[fn]), %[fn]\n" \
148 "test %[fn], %[fn]\n" \
149 "je 1f\n" \
150 "jmp *%P[api](%[fn])\n" \
151 "1:\n" \
152 : [fn] "=r" (fn) \
153 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
154 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
155 : "cc" \
156 );
157
Jesse Hall30a41aa2014-05-30 23:32:12 -0700158#elif defined(__x86_64__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200159
Michal Wajdeczko701048c2014-08-08 17:26:25 +0200160 #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200161
162 #define CALL_GL_API(_api, ...) \
163 register void** fn; \
164 __asm__ volatile( \
165 "mov %%fs:0, %[fn]\n" \
166 "mov %P[tls](%[fn]), %[fn]\n" \
167 "test %[fn], %[fn]\n" \
168 "je 1f\n" \
169 "jmp *%P[api](%[fn])\n" \
170 "1:\n" \
171 : [fn] "=r" (fn) \
172 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
173 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
174 : "cc" \
175 );
176
Duane Sand6701fbe2014-11-26 13:17:40 -0800177#elif defined(__mips64)
178
179 #define API_ENTRY(_api) __attribute__((noinline)) _api
180
181 #define CALL_GL_API(_api, ...) \
Nikola Veljkovic5f5f6512014-12-26 17:33:14 +0100182 register unsigned long _t0 asm("$12"); \
183 register unsigned long _fn asm("$25"); \
184 register unsigned long _tls asm("$3"); \
185 register unsigned long _v0 asm("$2"); \
Duane Sand6701fbe2014-11-26 13:17:40 -0800186 asm volatile( \
187 ".set push\n\t" \
188 ".set noreorder\n\t" \
189 "rdhwr %[tls], $29\n\t" \
190 "ld %[t0], %[OPENGL_API](%[tls])\n\t" \
191 "beqz %[t0], 1f\n\t" \
192 " move %[fn], $ra\n\t" \
193 "ld %[t0], %[API](%[t0])\n\t" \
194 "beqz %[t0], 1f\n\t" \
195 " nop\n\t" \
196 "move %[fn], %[t0]\n\t" \
197 "1:\n\t" \
198 "jalr $0, %[fn]\n\t" \
199 " move %[v0], $0\n\t" \
200 ".set pop\n\t" \
201 : [fn] "=c"(_fn), \
202 [tls] "=&r"(_tls), \
203 [t0] "=&r"(_t0), \
204 [v0] "=&r"(_v0) \
205 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\
206 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
207 : \
208 );
209
Jesse Hall30a41aa2014-05-30 23:32:12 -0700210#elif defined(__mips__)
Duane Sand46b42532013-03-27 10:58:06 -0700211
212 #define API_ENTRY(_api) __attribute__((noinline)) _api
213
214 #define CALL_GL_API(_api, ...) \
Nikola Veljkovic5f5f6512014-12-26 17:33:14 +0100215 register unsigned int _t0 asm("$8"); \
216 register unsigned int _fn asm("$25"); \
217 register unsigned int _tls asm("$3"); \
218 register unsigned int _v0 asm("$2"); \
Duane Sand46b42532013-03-27 10:58:06 -0700219 asm volatile( \
220 ".set push\n\t" \
221 ".set noreorder\n\t" \
Duane Sandecacc3f2015-02-02 16:54:15 -0800222 ".set mips32r2\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700223 "rdhwr %[tls], $29\n\t" \
224 "lw %[t0], %[OPENGL_API](%[tls])\n\t" \
225 "beqz %[t0], 1f\n\t" \
226 " move %[fn], $ra\n\t" \
Duane Sand6701fbe2014-11-26 13:17:40 -0800227 "lw %[t0], %[API](%[t0])\n\t" \
228 "beqz %[t0], 1f\n\t" \
229 " nop\n\t" \
230 "move %[fn], %[t0]\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700231 "1:\n\t" \
Duane Sand6701fbe2014-11-26 13:17:40 -0800232 "jalr $0, %[fn]\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700233 " move %[v0], $0\n\t" \
234 ".set pop\n\t" \
Jesse Hall441f6942013-03-30 23:22:19 -0700235 : [fn] "=c"(_fn), \
236 [tls] "=&r"(_tls), \
237 [t0] "=&r"(_t0), \
238 [v0] "=&r"(_v0) \
Duane Sand46b42532013-03-27 10:58:06 -0700239 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
240 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
241 : \
242 );
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244#endif
245
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700246#define CALL_GL_API_RETURN(_api, ...) \
247 CALL_GL_API(_api, __VA_ARGS__) \
248 return 0;
249
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251extern "C" {
Jesse Hallbbbddb82014-05-13 21:13:14 -0700252#pragma GCC diagnostic ignored "-Wunused-parameter"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253#include "gl_api.in"
Mathias Agopianb519abb2009-04-23 18:05:44 -0700254#include "glext_api.in"
Jesse Hallbbbddb82014-05-13 21:13:14 -0700255#pragma GCC diagnostic warning "-Wunused-parameter"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256}
257
258#undef API_ENTRY
259#undef CALL_GL_API
260#undef CALL_GL_API_RETURN
261
Mathias Agopian48d438d2012-01-28 21:44:00 -0800262/*
263 * glGetString() is special because we expose some extensions in the wrapper
264 */
265
266extern "C" const GLubyte * __glGetString(GLenum name);
267
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700268const GLubyte * glGetString(GLenum name) {
Mathias Agopian48d438d2012-01-28 21:44:00 -0800269 const GLubyte * ret = egl_get_string_for_current_context(name);
270 if (ret == NULL) {
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700271 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
272 ret = _c->glGetString(name);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800273 }
274 return ret;
275}