blob: bacd4b4bc87560cab27b04fc8c0474ecfe3e2eb0 [file] [log] [blame]
Mark Salyzyna5e161b2016-09-29 08:08:05 -07001/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002 ** Copyright 2007, The Android Open Source Project
3 **
Mark Salyzyna5e161b2016-09-29 08:08:05 -07004 ** 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 **
Mark Salyzyna5e161b2016-09-29 08:08:05 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08009 **
Mark Salyzyna5e161b2016-09-29 08:08:05 -070010 ** 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080014 ** limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018#include <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070019#include <string.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <sys/ioctl.h>
21
Mathias Agopian5f1af042017-03-09 18:50:05 -080022#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <cutils/properties.h>
24
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Mathias Agopian39c24a22013-04-04 23:17:56 -070028#include "../hooks.h"
29#include "../egl_impl.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31using namespace android;
32
33// ----------------------------------------------------------------------------
34// extensions for the framework
35// ----------------------------------------------------------------------------
36
Mathias Agopiand373c632009-05-08 15:35:17 -070037extern "C" {
38GL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
39 const GLvoid *ptr, GLsizei count);
40GL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride,
41 const GLvoid *pointer, GLsizei count);
42GL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type,
43 GLsizei stride, const GLvoid *pointer, GLsizei count);
44GL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type,
45 GLsizei stride, const GLvoid *pointer, GLsizei count);
Jack Palevich66089a32009-12-08 15:43:51 +080046GL_API void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
47 GLsizei stride, const GLvoid *pointer, GLsizei count);
48GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
49 GLsizei stride, const GLvoid *pointer, GLsizei count);
50GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
51 GLsizei stride, const GLvoid *pointer, GLsizei count);
Mathias Agopiand373c632009-05-08 15:35:17 -070052}
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054void glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070055 const GLvoid *ptr, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 glColorPointer(size, type, stride, ptr);
57}
58void glNormalPointerBounds(GLenum type, GLsizei stride,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070059 const GLvoid *pointer, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 glNormalPointer(type, stride, pointer);
61}
62void glTexCoordPointerBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070063 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 glTexCoordPointer(size, type, stride, pointer);
65}
66void glVertexPointerBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070067 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 glVertexPointer(size, type, stride, pointer);
69}
70
Jack Palevich66089a32009-12-08 15:43:51 +080071void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070072 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
Jack Palevich66089a32009-12-08 15:43:51 +080073 glPointSizePointerOES(type, stride, pointer);
74}
75
76GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070077 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
Jack Palevich66089a32009-12-08 15:43:51 +080078 glMatrixIndexPointerOES(size, type, stride, pointer);
79}
80
81GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070082 GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
Jack Palevich66089a32009-12-08 15:43:51 +080083 glWeightPointerOES(size, type, stride, pointer);
84}
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086// ----------------------------------------------------------------------------
87// Actual GL entry-points
88// ----------------------------------------------------------------------------
89
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090#undef API_ENTRY
91#undef CALL_GL_API
Stephen Hines3e8fce42016-03-22 23:35:27 -070092#undef CALL_GL_API_INTERNAL_CALL
93#undef CALL_GL_API_INTERNAL_SET_RETURN_VALUE
94#undef CALL_GL_API_INTERNAL_DO_RETURN
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095#undef CALL_GL_API_RETURN
96
Jesse Hall30a41aa2014-05-30 23:32:12 -070097#if USE_SLOW_BINDING
98
99 #define API_ENTRY(_api) _api
100
Stephen Hines3e8fce42016-03-22 23:35:27 -0700101 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700102 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
103 if (_c) return _c->_api(__VA_ARGS__);
104
Stephen Hines3e8fce42016-03-22 23:35:27 -0700105 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE return 0;
106
107 // This stays blank, since void functions will implicitly return, and
108 // all of the other functions will return 0 based on the previous macro.
109 #define CALL_GL_API_INTERNAL_DO_RETURN
110
Jesse Hall30a41aa2014-05-30 23:32:12 -0700111#elif defined(__arm__)
Duane Sand46b42532013-03-27 10:58:06 -0700112
Elliott Hughes288870e2013-02-13 17:30:54 -0800113 #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
Mathias Agopian673d2db2009-10-14 02:39:53 -0700114
Stephen Hines3e8fce42016-03-22 23:35:27 -0700115 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Stephen Hines3e8fce42016-03-22 23:35:27 -0700117 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
118 asm volatile( \
119 GET_TLS(r12) \
120 "ldr r12, [r12, %[tls]] \n" \
121 "cmp r12, #0 \n" \
122 "ldrne pc, [r12, %[api]] \n" \
123 : \
124 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
125 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
126 : "r0", "r1", "r2", "r3", "r12" \
127 );
128
129 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
130 asm volatile( \
131 "mov r0, #0 \n" \
132 : \
133 : \
134 : "r0" \
135 );
136
137
138 #define CALL_GL_API_INTERNAL_DO_RETURN \
139 asm volatile( \
140 "bx lr \n" \
141 : \
142 : \
143 : "r0" \
144 );
Mathias Agopian673d2db2009-10-14 02:39:53 -0700145
Jesse Hall30a41aa2014-05-30 23:32:12 -0700146#elif defined(__aarch64__)
147
Stephen Hines3e8fce42016-03-22 23:35:27 -0700148 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
Jesse Hall30a41aa2014-05-30 23:32:12 -0700149
Stephen Hines3e8fce42016-03-22 23:35:27 -0700150 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700151 asm volatile( \
152 "mrs x16, tpidr_el0\n" \
153 "ldr x16, [x16, %[tls]]\n" \
154 "cbz x16, 1f\n" \
155 "ldr x16, [x16, %[api]]\n" \
156 "br x16\n" \
157 "1:\n" \
158 : \
159 : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \
160 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700161 : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x16" \
162 );
163
164 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
165 asm volatile( \
166 "mov w0, wzr \n" \
167 : \
168 : \
169 : "w0" \
170 );
171
172 #define CALL_GL_API_INTERNAL_DO_RETURN \
173 asm volatile( \
174 "ret \n" \
175 : \
176 : \
177 : \
Jesse Hall30a41aa2014-05-30 23:32:12 -0700178 );
179
180#elif defined(__i386__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200181
Stephen Hines3e8fce42016-03-22 23:35:27 -0700182 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200183
Stephen Hines3e8fce42016-03-22 23:35:27 -0700184 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
mwajdeczc80aafa2014-05-26 13:56:37 +0200185 __asm__ volatile( \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700186 "mov %%gs:0, %%eax\n" \
187 "mov %P[tls](%%eax), %%eax\n" \
188 "test %%eax, %%eax\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200189 "je 1f\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700190 "jmp *%P[api](%%eax)\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200191 "1:\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700192 : \
mwajdeczc80aafa2014-05-26 13:56:37 +0200193 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
194 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700195 : "cc", "%eax" \
196 );
197
198 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
199 __asm__ volatile( \
200 "xor %%eax, %%eax\n" \
201 : \
202 : \
203 : "%eax" \
204 );
205
206 #define CALL_GL_API_INTERNAL_DO_RETURN \
207 __asm__ volatile( \
208 "ret\n" \
209 : \
210 : \
211 : \
212 );
mwajdeczc80aafa2014-05-26 13:56:37 +0200213
Jesse Hall30a41aa2014-05-30 23:32:12 -0700214#elif defined(__x86_64__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200215
Stephen Hines3e8fce42016-03-22 23:35:27 -0700216 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200217
Stephen Hines3e8fce42016-03-22 23:35:27 -0700218 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
219 __asm__ volatile( \
220 "mov %%fs:0, %%rax\n" \
221 "mov %P[tls](%%rax), %%rax\n" \
222 "test %%rax, %%rax\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200223 "je 1f\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700224 "jmp *%P[api](%%rax)\n" \
mwajdeczc80aafa2014-05-26 13:56:37 +0200225 "1:\n" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700226 : \
mwajdeczc80aafa2014-05-26 13:56:37 +0200227 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
228 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700229 : "cc", "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9", \
230 "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", \
231 "%xmm6", "%xmm7" \
232 );
233
234 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
235 __asm__ volatile( \
236 "xor %%eax, %%eax\n" \
237 : \
238 : \
239 : "%eax" \
240 );
241
242 #define CALL_GL_API_INTERNAL_DO_RETURN \
243 __asm__ volatile( \
244 "retq\n" \
245 : \
246 : \
247 : \
248 );
mwajdeczc80aafa2014-05-26 13:56:37 +0200249
Duane Sand6701fbe2014-11-26 13:17:40 -0800250#elif defined(__mips64)
251
Stephen Hines3e8fce42016-03-22 23:35:27 -0700252 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
Duane Sand6701fbe2014-11-26 13:17:40 -0800253
Stephen Hines3e8fce42016-03-22 23:35:27 -0700254 // t0: $12
255 // fn: $25
256 // tls: $3
257 // v0: $2
258 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
259 asm volatile( \
260 ".set push\n\t" \
261 ".set noreorder\n\t" \
262 "rdhwr $3, $29\n\t" \
263 "ld $12, %[OPENGL_API]($3)\n\t" \
264 "beqz $12, 1f\n\t" \
265 " move $25, $ra\n\t" \
266 "ld $12, %[API]($12)\n\t" \
267 "beqz $12, 1f\n\t" \
268 " nop\n\t" \
269 "move $25, $12\n\t" \
270 "1:\n\t" \
271 "jalr $0, $25\n\t" \
272 " move $2, $0\n\t" \
273 ".set pop\n\t" \
274 : \
275 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\
276 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
277 : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", \
278 "$10", "$11", "$12", "$25" \
Duane Sand6701fbe2014-11-26 13:17:40 -0800279 );
280
Stephen Hines3e8fce42016-03-22 23:35:27 -0700281 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE
282 #define CALL_GL_API_INTERNAL_DO_RETURN
283
Jesse Hall30a41aa2014-05-30 23:32:12 -0700284#elif defined(__mips__)
Duane Sand46b42532013-03-27 10:58:06 -0700285
Stephen Hines3e8fce42016-03-22 23:35:27 -0700286 #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
Duane Sand46b42532013-03-27 10:58:06 -0700287
Stephen Hines3e8fce42016-03-22 23:35:27 -0700288 // t0: $8
289 // fn: $25
290 // tls: $3
291 // v0: $2
292 #define CALL_GL_API_INTERNAL_CALL(_api, ...) \
Duane Sand46b42532013-03-27 10:58:06 -0700293 asm volatile( \
294 ".set push\n\t" \
295 ".set noreorder\n\t" \
Duane Sandecacc3f2015-02-02 16:54:15 -0800296 ".set mips32r2\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700297 "rdhwr $3, $29\n\t" \
298 "lw $3, %[OPENGL_API]($3)\n\t" \
299 "beqz $3, 1f\n\t" \
300 " move $25,$ra\n\t" \
301 "lw $3, %[API]($3)\n\t" \
302 "beqz $3, 1f\n\t" \
Duane Sand6701fbe2014-11-26 13:17:40 -0800303 " nop\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700304 "move $25, $3\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700305 "1:\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700306 "jalr $0, $25\n\t" \
307 " move $2, $0\n\t" \
Duane Sand46b42532013-03-27 10:58:06 -0700308 ".set pop\n\t" \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700309 : \
Duane Sand46b42532013-03-27 10:58:06 -0700310 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
311 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
Stephen Hines3e8fce42016-03-22 23:35:27 -0700312 : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$25" \
313 );
314
315 #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE
316 #define CALL_GL_API_INTERNAL_DO_RETURN
Duane Sand46b42532013-03-27 10:58:06 -0700317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318#endif
319
Stephen Hines3e8fce42016-03-22 23:35:27 -0700320#define CALL_GL_API(_api, ...) \
321 CALL_GL_API_INTERNAL_CALL(_api, __VA_ARGS__) \
322 CALL_GL_API_INTERNAL_DO_RETURN
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700323
Stephen Hines3e8fce42016-03-22 23:35:27 -0700324#define CALL_GL_API_RETURN(_api, ...) \
325 CALL_GL_API_INTERNAL_CALL(_api, __VA_ARGS__) \
326 CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
327 CALL_GL_API_INTERNAL_DO_RETURN
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700328
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329extern "C" {
Jesse Hallbbbddb82014-05-13 21:13:14 -0700330#pragma GCC diagnostic ignored "-Wunused-parameter"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331#include "gl_api.in"
Mathias Agopianb519abb2009-04-23 18:05:44 -0700332#include "glext_api.in"
Jesse Hallbbbddb82014-05-13 21:13:14 -0700333#pragma GCC diagnostic warning "-Wunused-parameter"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334}
335
336#undef API_ENTRY
337#undef CALL_GL_API
Stephen Hines3e8fce42016-03-22 23:35:27 -0700338#undef CALL_GL_API_INTERNAL_CALL
339#undef CALL_GL_API_INTERNAL_SET_RETURN_VALUE
340#undef CALL_GL_API_INTERNAL_DO_RETURN
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341#undef CALL_GL_API_RETURN
342
Mathias Agopian48d438d2012-01-28 21:44:00 -0800343/*
344 * glGetString() is special because we expose some extensions in the wrapper
345 */
346
347extern "C" const GLubyte * __glGetString(GLenum name);
348
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700349const GLubyte * glGetString(GLenum name) {
Mathias Agopian48d438d2012-01-28 21:44:00 -0800350 const GLubyte * ret = egl_get_string_for_current_context(name);
351 if (ret == NULL) {
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700352 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
353 ret = _c->glGetString(name);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800354 }
355 return ret;
356}