1a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project ** Copyright 2007, The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project **
4a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** Licensed under the Apache License, Version 2.0 (the "License");
5a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** you may not use this file except in compliance with the License.
6a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project **
8a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn **     http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project **
10a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** Unless required by applicable law or agreed to in writing, software
11a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** distributed under the License is distributed on an "AS IS" BASIS,
12a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn ** See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project ** limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <ctype.h>
18edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <errno.h>
19a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn#include <string.h>
20edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <sys/ioctl.h>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
225f1af0457a8a6c2922a0d5e55cb6c248ec539f7fMathias Agopian#include <log/log.h>
23a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn#include <cutils/properties.h>
24a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn
25edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <GLES/gl.h>
26edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <GLES/glext.h>
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
2839c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian#include "../hooks.h"
2939c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian#include "../egl_impl.h"
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
31edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectusing namespace android;
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
33edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// extensions for the framework
35edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
36edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
37d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopianextern "C" {
38d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias AgopianGL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
39d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopian        const GLvoid *ptr, GLsizei count);
40d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias AgopianGL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride,
41d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopian        const GLvoid *pointer, GLsizei count);
42d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias AgopianGL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type,
43d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopian        GLsizei stride, const GLvoid *pointer, GLsizei count);
44d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias AgopianGL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type,
45d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopian        GLsizei stride, const GLvoid *pointer, GLsizei count);
4666089a33ea4efda711a3a5180f0b001bdac48dcfJack PalevichGL_API void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
4766089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich        GLsizei stride, const GLvoid *pointer, GLsizei count);
4866089a33ea4efda711a3a5180f0b001bdac48dcfJack PalevichGL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
4966089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich        GLsizei stride, const GLvoid *pointer, GLsizei count);
5066089a33ea4efda711a3a5180f0b001bdac48dcfJack PalevichGL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
5166089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich        GLsizei stride, const GLvoid *pointer, GLsizei count);
52d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopian}
53d373c6348a91595dd4c0b83ad256dbf30870fa5dMathias Agopian
54edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
5592dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        const GLvoid *ptr, GLsizei /*count*/) {
56edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glColorPointer(size, type, stride, ptr);
57edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
58edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid glNormalPointerBounds(GLenum type, GLsizei stride,
5992dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        const GLvoid *pointer, GLsizei /*count*/) {
60edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glNormalPointer(type, stride, pointer);
61edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
62edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid glTexCoordPointerBounds(GLint size, GLenum type,
6392dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glTexCoordPointer(size, type, stride, pointer);
65edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
66edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid glVertexPointerBounds(GLint size, GLenum type,
6792dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
68edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    glVertexPointer(size, type, stride, pointer);
69edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
70edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
7166089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevichvoid GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
7292dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
7366089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich    glPointSizePointerOES(type, stride, pointer);
7466089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich}
7566089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich
7666089a33ea4efda711a3a5180f0b001bdac48dcfJack PalevichGL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
7792dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
7866089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich    glMatrixIndexPointerOES(size, type, stride, pointer);
7966089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich}
8066089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich
8166089a33ea4efda711a3a5180f0b001bdac48dcfJack PalevichGL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
8292dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
8366089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich    glWeightPointerOES(size, type, stride, pointer);
8466089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich}
8566089a33ea4efda711a3a5180f0b001bdac48dcfJack Palevich
86edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
87edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// Actual GL entry-points
88edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
89edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
90edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef API_ENTRY
91edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef CALL_GL_API
923e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#undef CALL_GL_API_INTERNAL_CALL
933e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#undef CALL_GL_API_INTERNAL_SET_RETURN_VALUE
943e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#undef CALL_GL_API_INTERNAL_DO_RETURN
95edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef CALL_GL_API_RETURN
96edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
9730a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall#if USE_SLOW_BINDING
9830a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall
9930a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall    #define API_ENTRY(_api) _api
10030a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall
1013e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                         \
10230a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;  \
10330a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall        if (_c) return _c->_api(__VA_ARGS__);
10430a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall
1053e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE return 0;
1063e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1073e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // This stays blank, since void functions will implicitly return, and
1083e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // all of the other functions will return 0 based on the previous macro.
1093e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN
1103e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
11130a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall#elif defined(__arm__)
11246b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand
113288870ebc3da8121b7a237a53280bd8b931b7a2fElliott Hughes    #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
114673d2dbaae647abc58b08de873fd364f0b2fa3f5Mathias Agopian
1153e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
116edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1173e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                 \
1183e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        asm volatile(                                            \
1193e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            GET_TLS(r12)                                         \
1203e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "ldr   r12, [r12, %[tls]] \n"                        \
1213e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "cmp   r12, #0            \n"                        \
1223e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "ldrne pc,  [r12, %[api]] \n"                        \
1233e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                                    \
1243e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : [tls] "J"(TLS_SLOT_OPENGL_API*4),                  \
1253e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines              [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
1263e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "r0", "r1", "r2", "r3", "r12"                      \
1273e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
1283e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1293e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
1303e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        asm volatile(                             \
1313e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "mov r0, #0 \n"                       \
1323e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
1333e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
1343e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "r0"                                \
1353e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
1363e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1373e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1383e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN \
1393e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        asm volatile(                      \
1403e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "bx lr \n"                     \
1413e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
1423e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
1433e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "r0"                         \
1443e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
145673d2dbaae647abc58b08de873fd364f0b2fa3f5Mathias Agopian
14630a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall#elif defined(__aarch64__)
14730a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall
1483e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
14930a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall
1503e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                    \
15130a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall        asm volatile(                                               \
15230a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            "mrs x16, tpidr_el0\n"                                  \
15330a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            "ldr x16, [x16, %[tls]]\n"                              \
15430a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            "cbz x16, 1f\n"                                         \
15530a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            "ldr x16, [x16, %[api]]\n"                              \
15630a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            "br  x16\n"                                             \
15730a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            "1:\n"                                                  \
15830a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            :                                                       \
15930a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall            : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)),      \
16030a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall              [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api))   \
1613e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x16" \
1623e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
1633e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1643e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
1653e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        asm volatile(                             \
1663e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "mov w0, wzr \n"                      \
1673e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
1683e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
1693e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "w0"                                \
1703e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
1713e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1723e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN \
1733e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        asm volatile(                      \
1743e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "ret \n"                       \
1753e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
1763e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
1773e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
17830a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall        );
17930a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall
18030a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall#elif defined(__i386__)
181c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz
1823e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
183c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz
1843e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                    \
185c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz        __asm__ volatile(                                           \
1863e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "mov %%gs:0, %%eax\n"                                   \
1873e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "mov %P[tls](%%eax), %%eax\n"                           \
1883e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "test %%eax, %%eax\n"                                   \
189c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz            "je 1f\n"                                               \
1903e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "jmp *%P[api](%%eax)\n"                                 \
191c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz            "1:\n"                                                  \
1923e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                                       \
193c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz            : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)),        \
194c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz              [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api))   \
1953e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "cc", "%eax"                                          \
1963e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
1973e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
1983e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
1993e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        __asm__ volatile(                         \
2003e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "xor %%eax, %%eax\n"                  \
2013e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
2023e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
2033e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "%eax"                              \
2043e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
2053e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
2063e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN \
2073e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        __asm__ volatile(                  \
2083e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "ret\n"                        \
2093e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
2103e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
2113e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
2123e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
213c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz
21430a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall#elif defined(__x86_64__)
215c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz
2163e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
217c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz
2183e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                    \
2193e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        __asm__ volatile(                                           \
2203e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "mov %%fs:0, %%rax\n"                                   \
2213e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "mov %P[tls](%%rax), %%rax\n"                           \
2223e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "test %%rax, %%rax\n"                                   \
223c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz            "je 1f\n"                                               \
2243e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "jmp *%P[api](%%rax)\n"                                 \
225c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz            "1:\n"                                                  \
2263e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                                       \
227c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz            : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)),        \
228c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz              [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api))   \
2293e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "cc", "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9",   \
2303e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines              "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", \
2313e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines              "%xmm6", "%xmm7"                                      \
2323e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
2333e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
2343e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
2353e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        __asm__ volatile(                         \
2363e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "xor %%eax, %%eax\n"                  \
2373e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
2383e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                     \
2393e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "%eax"                              \
2403e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
2413e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
2423e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN \
2433e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        __asm__ volatile(                  \
2443e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "retq\n"                       \
2453e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
2463e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
2473e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                              \
2483e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
249c80aafa61d619087a879dc1095ddf1ca2a5e69fbmwajdecz
2506701fbe5f0d799c5245b0cdf3a69ecdcbfd9fb08Duane Sand#elif defined(__mips64)
2516701fbe5f0d799c5245b0cdf3a69ecdcbfd9fb08Duane Sand
2523e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
2533e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
2543e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // t0:  $12
2553e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // fn:  $25
2563e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // tls: $3
2573e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // v0:  $2
2583e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                  \
2593e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        asm volatile(                                             \
2603e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            ".set  push\n\t"                                      \
2613e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            ".set  noreorder\n\t"                                 \
2623e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "rdhwr $3, $29\n\t"                                   \
2633e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "ld    $12, %[OPENGL_API]($3)\n\t"                    \
2643e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "beqz  $12, 1f\n\t"                                   \
2653e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            " move $25, $ra\n\t"                                  \
2663e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "ld    $12, %[API]($12)\n\t"                          \
2673e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "beqz  $12, 1f\n\t"                                   \
2683e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            " nop\n\t"                                            \
2693e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "move  $25, $12\n\t"                                  \
2703e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "1:\n\t"                                              \
2713e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "jalr  $0, $25\n\t"                                   \
2723e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            " move $2, $0\n\t"                                    \
2733e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            ".set  pop\n\t"                                       \
2743e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                                     \
2753e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\
2763e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines              [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api))  \
2773e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9",     \
2783e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines              "$10", "$11", "$12", "$25"                          \
2796701fbe5f0d799c5245b0cdf3a69ecdcbfd9fb08Duane Sand        );
2806701fbe5f0d799c5245b0cdf3a69ecdcbfd9fb08Duane Sand
2813e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE
2823e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN
2833e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
28430a41aa1ccc47de41642308a243fa5df2bfeec06Jesse Hall#elif defined(__mips__)
28546b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand
2863e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define API_ENTRY(_api) __attribute__((naked,noinline)) _api
28746b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand
2883e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // t0:  $8
2893e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // fn:  $25
2903e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // tls: $3
2913e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    // v0:  $2
2923e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_CALL(_api, ...)                 \
29346b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand        asm volatile(                                            \
29446b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand            ".set  push\n\t"                                     \
29546b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand            ".set  noreorder\n\t"                                \
296ecacc3f164f2bf2c8c4ff5a20496511249d2cd4bDuane Sand            ".set  mips32r2\n\t"                                 \
2973e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "rdhwr $3, $29\n\t"                                  \
2983e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "lw    $3, %[OPENGL_API]($3)\n\t"                    \
2993e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "beqz  $3, 1f\n\t"                                   \
3003e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            " move $25,$ra\n\t"                                  \
3013e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "lw    $3, %[API]($3)\n\t"                           \
3023e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "beqz  $3, 1f\n\t"                                   \
3036701fbe5f0d799c5245b0cdf3a69ecdcbfd9fb08Duane Sand            " nop\n\t"                                           \
3043e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "move  $25, $3\n\t"                                  \
30546b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand            "1:\n\t"                                             \
3063e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            "jalr  $0, $25\n\t"                                  \
3073e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            " move $2, $0\n\t"                                   \
30846b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand            ".set  pop\n\t"                                      \
3093e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            :                                                    \
31046b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand            : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4),           \
31146b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand              [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
3123e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines            : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$25"    \
3133e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines        );
3143e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines
3153e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_SET_RETURN_VALUE
3163e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    #define CALL_GL_API_INTERNAL_DO_RETURN
31746b4253b760916ea8f64c8c32a51ae85ec3b050eDuane Sand
318edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
319edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
3203e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#define CALL_GL_API(_api, ...) \
3213e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    CALL_GL_API_INTERNAL_CALL(_api, __VA_ARGS__) \
3223e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    CALL_GL_API_INTERNAL_DO_RETURN
323e0ea89ceef3b0fc5f3efc5d709a8156f0628c6c8Mathias Agopian
3243e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#define CALL_GL_API_RETURN(_api, ...) \
3253e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    CALL_GL_API_INTERNAL_CALL(_api, __VA_ARGS__) \
3263e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    CALL_GL_API_INTERNAL_SET_RETURN_VALUE \
3273e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines    CALL_GL_API_INTERNAL_DO_RETURN
328076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
329edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectextern "C" {
330bbbddb83bbd3ca2032294b55c91b1023acc927acJesse Hall#pragma GCC diagnostic ignored "-Wunused-parameter"
331edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "gl_api.in"
332b519abb606c819c446a408f422530268b21a100bMathias Agopian#include "glext_api.in"
333bbbddb83bbd3ca2032294b55c91b1023acc927acJesse Hall#pragma GCC diagnostic warning "-Wunused-parameter"
334edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
335edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
336edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef API_ENTRY
337edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef CALL_GL_API
3383e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#undef CALL_GL_API_INTERNAL_CALL
3393e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#undef CALL_GL_API_INTERNAL_SET_RETURN_VALUE
3403e8fce48a6c768036c11290b93116665e8e8bddaStephen Hines#undef CALL_GL_API_INTERNAL_DO_RETURN
341edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef CALL_GL_API_RETURN
342edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
34348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian/*
34448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian * glGetString() is special because we expose some extensions in the wrapper
34548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian */
34648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
34748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopianextern "C" const GLubyte * __glGetString(GLenum name);
34848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
349e0ea89ceef3b0fc5f3efc5d709a8156f0628c6c8Mathias Agopianconst GLubyte * glGetString(GLenum name) {
35048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    const GLubyte * ret = egl_get_string_for_current_context(name);
35148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    if (ret == NULL) {
352e0ea89ceef3b0fc5f3efc5d709a8156f0628c6c8Mathias Agopian        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
353e0ea89ceef3b0fc5f3efc5d709a8156f0628c6c8Mathias Agopian        ret = _c->glGetString(name);
35448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    }
35548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    return ret;
35648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian}
357