OSDN Git Service

a56f12d7d207d95e210c972d750ad159a3a3f66c
[android-x86/frameworks-native.git] / opengl / libs / GLES2 / gl2.cpp
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
33 using 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 defined(__arm__) && !USE_SLOW_BINDING
44
45     #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
46
47     #define API_ENTRY(_api) __attribute__((noinline)) _api
48
49     #define CALL_GL_API(_api, ...)                              \
50          asm volatile(                                          \
51             GET_TLS(r12)                                        \
52             "ldr   r12, [r12, %[tls]] \n"                       \
53             "cmp   r12, #0            \n"                       \
54             "ldrne pc,  [r12, %[api]] \n"                       \
55             :                                                   \
56             : [tls] "J"(TLS_SLOT_OPENGL_API*4),                 \
57               [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api))    \
58             :                                                   \
59             );
60
61 #elif defined(__i386__) && !USE_SLOW_BINDING
62
63     /* Tell gcc 4.4+ to always omit the frame pointer to have working code
64      * independ of compiler flag
65      */
66     #define API_ENTRY(_api) __attribute__((noinline))            \
67             __attribute__((optimize("omit-frame-pointer")))      \
68             _api
69
70     /* pop %%ebp only because x86 doesn't support naked functions */
71     /* 4.4+ supports __attribute__((optimize)) that disable function
72      * prologues
73      */
74     #if __GNUC__ < 4 || \
75     (__GNUC__ == 4 && __GNUC_MINOR__ < 4 )
76          #define PROLOGUE  "pop %%ebp   \n"
77     #else
78          #define PROLOGUE  ""
79     #endif
80
81     #define CALL_GL_API(_api, ...)                              \
82         asm volatile(                                           \
83             PROLOGUE                                            \
84             "movl %%gs:0, %%ecx         \n"                     \
85             "movl %c[tls](%%ecx), %%eax \n"                     \
86             "test %%eax, %%eax          \n"                     \
87             "je out" #_api "            \n"                     \
88             "movl %c[api](%%eax), %%ecx \n"                     \
89             "jmp *%%ecx                 \n"                     \
90             "out" #_api ": ret          \n"                     \
91             :                                                   \
92             : [tls] "i"(TLS_SLOT_OPENGL_API*4),                 \
93               [api] "i"(__builtin_offsetof(gl_hooks_t, gl._api))    \
94             :                                                   \
95             );
96
97 #elif defined(__mips__) && !USE_SLOW_BINDING
98
99     #define API_ENTRY(_api) __attribute__((noinline)) _api
100
101     #define CALL_GL_API(_api, ...)                               \
102         register unsigned int _t0 asm("t0");                     \
103         register unsigned int _fn asm("t1");                     \
104         register unsigned int _tls asm("v1");                    \
105         register unsigned int _v0 asm("v0");                     \
106         asm volatile(                                            \
107             ".set  push\n\t"                                     \
108             ".set  noreorder\n\t"                                \
109             ".set mips32r2\n\t"                                  \
110             "rdhwr %[tls], $29\n\t"                              \
111             "lw    %[t0], %[OPENGL_API](%[tls])\n\t"             \
112             "beqz  %[t0], 1f\n\t"                                \
113             " move %[fn],$ra\n\t"                                \
114             "lw    %[fn], %[API](%[t0])\n\t"                     \
115             "movz  %[fn], $ra, %[fn]\n\t"                        \
116             "1:\n\t"                                             \
117             "j     %[fn]\n\t"                                    \
118             " move %[v0], $0\n\t"                                \
119             ".set  pop\n\t"                                      \
120             : [fn] "=c"(_fn),                                    \
121               [tls] "=&r"(_tls),                                 \
122               [t0] "=&r"(_t0),                                   \
123               [v0] "=&r"(_v0)                                    \
124             : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4),           \
125               [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
126             :                                                    \
127             );
128
129 #else
130
131     #define API_ENTRY(_api) _api
132
133     #define CALL_GL_API(_api, ...)                                       \
134         gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;  \
135         if (_c) return _c->_api(__VA_ARGS__);
136
137 #endif
138
139 #define CALL_GL_API_RETURN(_api, ...) \
140     CALL_GL_API(_api, __VA_ARGS__) \
141     return 0;
142
143
144
145 extern "C" {
146 #include "gl3_api.in"
147 #include "gl2ext_api.in"
148 #include "gl3ext_api.in"
149 }
150
151 #undef API_ENTRY
152 #undef CALL_GL_API
153 #undef CALL_GL_API_RETURN
154
155 /*
156  * glGetString() is special because we expose some extensions in the wrapper
157  */
158
159 extern "C" const GLubyte * __glGetString(GLenum name);
160
161 const GLubyte * glGetString(GLenum name)
162 {
163     const GLubyte * ret = egl_get_string_for_current_context(name);
164     if (ret == NULL) {
165         gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
166         ret = _c->glGetString(name);
167     }
168     return ret;
169 }