gl2.cpp revision 30a41aa1ccc47de41642308a243fa5df2bfeec06
1/*
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
17#include <ctype.h>
18#include <string.h>
19#include <errno.h>
20
21#include <sys/ioctl.h>
22
23#include <GLES3/gl3.h>
24#include <GLES3/gl3ext.h>
25#include <GLES2/gl2ext.h>
26
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
30#include "../hooks.h"
31#include "../egl_impl.h"
32
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
43#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__)
52
53    #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
54
55    #define API_ENTRY(_api) __attribute__((noinline)) _api
56
57    #define CALL_GL_API(_api, ...)                              \
58         asm volatile(                                          \
59            GET_TLS(r12)                                        \
60            "ldr   r12, [r12, %[tls]] \n"                       \
61            "cmp   r12, #0            \n"                       \
62            "ldrne pc,  [r12, %[api]] \n"                       \
63            :                                                   \
64            : [tls] "J"(TLS_SLOT_OPENGL_API*4),                 \
65              [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api))    \
66            : "r12"                                             \
67            );
68
69#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__)
88
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
106#elif defined(__x86_64__)
107
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
125#elif defined(__mips__)
126
127    #define API_ENTRY(_api) __attribute__((noinline)) _api
128
129    #define CALL_GL_API(_api, ...)                               \
130        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");                     \
134        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"                                      \
148            : [fn] "=c"(_fn),                                    \
149              [tls] "=&r"(_tls),                                 \
150              [t0] "=&r"(_t0),                                   \
151              [v0] "=&r"(_v0)                                    \
152            : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4),           \
153              [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
154            :                                                    \
155            );
156
157#endif
158
159#define CALL_GL_API_RETURN(_api, ...) \
160    CALL_GL_API(_api, __VA_ARGS__) \
161    return 0;
162
163
164
165extern "C" {
166#include "gl3_api.in"
167#include "gl2ext_api.in"
168#include "gl3ext_api.in"
169}
170
171#undef API_ENTRY
172#undef CALL_GL_API
173#undef CALL_GL_API_RETURN
174
175/*
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) {
185        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
186        ret = _c->glGetString(name);
187    }
188    return ret;
189}
190