blob: 8bde4e5f0416cc7f20481c077d6303ab2035ff57 [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
Stephen Hines3e8fce42016-03-22 23:35:27 -070093#undef CALL_GL_API_INTERNAL_CALL
94#undef CALL_GL_API_INTERNAL_SET_RETURN_VALUE
95#undef CALL_GL_API_INTERNAL_DO_RETURN
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096#undef CALL_GL_API_RETURN
97
Jesse Hall30a41aa2014-05-30 23:32:12 -070098#if USE_SLOW_BINDING
99
100 #define API_ENTRY(_api) _api
101
Stephen Hines3e8fce42016-03-22 23:35:27 -0700102 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700103 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
104 if (_c) return _c->_api(__VA_ARGS__);
105
Stephen Hines3e8fce42016-03-22 23:35:27 -0700106 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE return 0;
107
108 // This stays blank, since void functions will implicitly return, and
109 // all of the other functions will return 0 based on the previous macro.
110 #define CALL_GL_API_INTERNAL_DO_RETURN
111
Jesse Hall30a41aa2014-05-30 23:32:12 -0700112#elif defined(__arm__)
Duane Sand46b42532013-03-27 10:58:06 -0700113
Elliott Hughes288870e2013-02-13 17:30:54 -0800114 #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
Mathias Agopian673d2db2009-10-14 02:39:53 -0700115
Stephen Hines3e8fce42016-03-22 23:35:27 -0700116 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Stephen Hines3e8fce42016-03-22 23:35:27 -0700118 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
119 asm volatile( \
120 GET_TLS(r12) \
121 "ldr r12, [r12, %[tls]] \n" \
122 "cmp r12, #0 \n" \
123 "ldrne pc, [r12, %[api]] \n" \
124 : \
125 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
126 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
127 : "r0", "r1", "r2", "r3", "r12" \
128 );
129
130 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
131 asm volatile( \
132 "mov r0, #0 \n" \
133 : \
134 : \
135 : "r0" \
136 );
137
138
139 #define CALL_GL_API_INTERNAL_DO_RETURN \
140 asm volatile( \
141 "bx lr \n" \
142 : \
143 : \
144 : "r0" \
145 );
Mathias Agopian673d2db2009-10-14 02:39:53 -0700146
Jesse Hall30a41aa2014-05-30 23:32:12 -0700147#elif defined(__aarch64__)
148
Stephen Hines3e8fce42016-03-22 23:35:27 -0700149 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
Jesse Hall30a41aa2014-05-30 23:32:12 -0700150
Stephen Hines3e8fce42016-03-22 23:35:27 -0700151 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700152 asm volatile( \
153 "mrs x16, tpidr_el0\n" \
154 "ldr x16, [x16, %[tls]]\n" \
155 "cbz x16, 1f\n" \
156 "ldr x16, [x16, %[api]]\n" \
157 "br x16\n" \
158 "1:\n" \
159 : \
160 : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \
161 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700162 : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x16" \
163 );
164
165 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
166 asm volatile( \
167 "mov w0, wzr \n" \
168 : \
169 : \
170 : "w0" \
171 );
172
173 #define CALL_GL_API_INTERNAL_DO_RETURN \
174 asm volatile( \
175 "ret \n" \
176 : \
177 : \
178 : \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700179 );
180
181#elif defined(__i386__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200182
Stephen Hines3e8fce42016-03-22 23:35:27 -0700183 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200184
Stephen Hines3e8fce42016-03-22 23:35:27 -0700185 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
mwajdeczc80aafa2014-05-26 13:56:37 +0200186 __asm__ volatile( \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700187 "mov %%gs:0, %%eax\n" \
188 "mov %P[tls](%%eax), %%eax\n" \
189 "test %%eax, %%eax\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200190 "je 1f\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700191 "jmp *%P[api](%%eax)\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200192 "1:\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700193 : \
mwajdeczc80aafa2014-05-26 13:56:37 +0200194 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
195 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700196 : "cc", "%eax" \
197 );
198
199 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
200 __asm__ volatile( \
201 "xor %%eax, %%eax\n" \
202 : \
203 : \
204 : "%eax" \
205 );
206
207 #define CALL_GL_API_INTERNAL_DO_RETURN \
208 __asm__ volatile( \
209 "ret\n" \
210 : \
211 : \
212 : \
213 );
mwajdeczc80aafa2014-05-26 13:56:37 +0200214
Jesse Hall30a41aa2014-05-30 23:32:12 -0700215#elif defined(__x86_64__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200216
Stephen Hines3e8fce42016-03-22 23:35:27 -0700217 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200218
Stephen Hines3e8fce42016-03-22 23:35:27 -0700219 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
220 __asm__ volatile( \
221 "mov %%fs:0, %%rax\n" \
222 "mov %P[tls](%%rax), %%rax\n" \
223 "test %%rax, %%rax\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200224 "je 1f\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700225 "jmp *%P[api](%%rax)\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200226 "1:\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700227 : \
mwajdeczc80aafa2014-05-26 13:56:37 +0200228 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
229 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700230 : "cc", "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9", \
231 "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", \
232 "%xmm6", "%xmm7" \
233 );
234
235 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
236 __asm__ volatile( \
237 "xor %%eax, %%eax\n" \
238 : \
239 : \
240 : "%eax" \
241 );
242
243 #define CALL_GL_API_INTERNAL_DO_RETURN \
244 __asm__ volatile( \
245 "retq\n" \
246 : \
247 : \
248 : \
249 );
mwajdeczc80aafa2014-05-26 13:56:37 +0200250
Duane Sand6701fbe2014-11-26 13:17:40 -0800251#elif defined(__mips64)
252
Stephen Hines3e8fce42016-03-22 23:35:27 -0700253 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
Duane Sand6701fbe2014-11-26 13:17:40 -0800254
Stephen Hines3e8fce42016-03-22 23:35:27 -0700255 // t0: $12
256 // fn: $25
257 // tls: $3
258 // v0: $2
259 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
260 asm volatile( \
261 ".set push\n\t" \
262 ".set noreorder\n\t" \
263 "rdhwr $3, $29\n\t" \
264 "ld $12, %[OPENGL_API]($3)\n\t" \
265 "beqz $12, 1f\n\t" \
266 " move $25, $ra\n\t" \
267 "ld $12, %[API]($12)\n\t" \
268 "beqz $12, 1f\n\t" \
269 " nop\n\t" \
270 "move $25, $12\n\t" \
271 "1:\n\t" \
272 "jalr $0, $25\n\t" \
273 " move $2, $0\n\t" \
274 ".set pop\n\t" \
275 : \
276 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\
277 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
278 : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", \
279 "$10", "$11", "$12", "$25" \
Duane Sand6701fbe2014-11-26 13:17:40 -0800280 );
281
Stephen Hines3e8fce42016-03-22 23:35:27 -0700282 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE
283 #define CALL_GL_API_INTERNAL_DO_RETURN
284
Jesse Hall30a41aa2014-05-30 23:32:12 -0700285#elif defined(__mips__)
Duane Sand46b42532013-03-27 10:58:06 -0700286
Stephen Hines3e8fce42016-03-22 23:35:27 -0700287 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
Duane Sand46b42532013-03-27 10:58:06 -0700288
Stephen Hines3e8fce42016-03-22 23:35:27 -0700289 // t0: $8
290 // fn: $25
291 // tls: $3
292 // v0: $2
293 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
Duane Sand46b42532013-03-27 10:58:06 -0700294 asm volatile( \
295 ".set push\n\t" \
296 ".set noreorder\n\t" \
Duane Sandecacc3f2015-02-02 16:54:15 -0800297 ".set mips32r2\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700298 "rdhwr $3, $29\n\t" \
299 "lw $3, %[OPENGL_API]($3)\n\t" \
300 "beqz $3, 1f\n\t" \
301 " move $25,$ra\n\t" \
302 "lw $3, %[API]($3)\n\t" \
303 "beqz $3, 1f\n\t" \
Duane Sand6701fbe2014-11-26 13:17:40 -0800304 " nop\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700305 "move $25, $3\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700306 "1:\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700307 "jalr $0, $25\n\t" \
308 " move $2, $0\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700309 ".set pop\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700310 : \
Duane Sand46b42532013-03-27 10:58:06 -0700311 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
312 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700313 : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$25" \
314 );
315
316 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE
317 #define CALL_GL_API_INTERNAL_DO_RETURN
Duane Sand46b42532013-03-27 10:58:06 -0700318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319#endif
320
Stephen Hines3e8fce42016-03-22 23:35:27 -0700321#define CALL_GL_API(_api, ...) \
322 CALL_GL_API_INTERNAL_CALL(_api, __VA_ARGS__) \
323 CALL_GL_API_INTERNAL_DO_RETURN
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700324
Stephen Hines3e8fce42016-03-22 23:35:27 -0700325#define CALL_GL_API_RETURN(_api, ...) \
326 CALL_GL_API_INTERNAL_CALL(_api, __VA_ARGS__) \
327 CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
328 CALL_GL_API_INTERNAL_DO_RETURN
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330extern "C" {
Jesse Hallbbbddb82014-05-13 21:13:14 -0700331#pragma GCC diagnostic ignored "-Wunused-parameter"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332#include "gl_api.in"
Mathias Agopianb519abb2009-04-23 18:05:44 -0700333#include "glext_api.in"
Jesse Hallbbbddb82014-05-13 21:13:14 -0700334#pragma GCC diagnostic warning "-Wunused-parameter"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335}
336
337#undef API_ENTRY
338#undef CALL_GL_API
Stephen Hines3e8fce42016-03-22 23:35:27 -0700339#undef CALL_GL_API_INTERNAL_CALL
340#undef CALL_GL_API_INTERNAL_SET_RETURN_VALUE
341#undef CALL_GL_API_INTERNAL_DO_RETURN
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342#undef CALL_GL_API_RETURN
343
Mathias Agopian48d438d2012-01-28 21:44:00 -0800344/*
345 * glGetString() is special because we expose some extensions in the wrapper
346 */
347
348extern "C" const GLubyte * __glGetString(GLenum name);
349
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700350const GLubyte * glGetString(GLenum name) {
Mathias Agopian48d438d2012-01-28 21:44:00 -0800351 const GLubyte * ret = egl_get_string_for_current_context(name);
352 if (ret == NULL) {
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700353 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
354 ret = _c->glGetString(name);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800355 }
356 return ret;
357}