gl2.cpp revision 6cb7d3dc9f1ce37547646979e0625992264ea4d0
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 <cutils/log.h>
24#include <cutils/properties.h>
25
26#include "../hooks.h"
27#include "../egl_impl.h"
28
29using namespace android;
30
31// ----------------------------------------------------------------------------
32// Actual GL entry-points
33// ----------------------------------------------------------------------------
34
35#undef API_ENTRY
36#undef CALL_GL_API
37#undef CALL_GL_API_RETURN
38
39#if defined(__arm__) && !USE_SLOW_BINDING
40
41    #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
42
43    #define API_ENTRY(_api) __attribute__((noinline)) _api
44
45    #define CALL_GL_API(_api, ...)                              \
46         asm volatile(                                          \
47            GET_TLS(r12)                                        \
48            "ldr   r12, [r12, %[tls]] \n"                       \
49            "cmp   r12, #0            \n"                       \
50            "ldrne pc,  [r12, %[api]] \n"                       \
51            :                                                   \
52            : [tls] "J"(TLS_SLOT_OPENGL_API*4),                 \
53              [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api))    \
54            :                                                   \
55            );
56
57#elif defined(__mips__) && !USE_SLOW_BINDING
58
59    #define API_ENTRY(_api) __attribute__((noinline)) _api
60
61    #define CALL_GL_API(_api, ...)                               \
62        register unsigned int _t0 asm("t0");                     \
63        register unsigned int _fn asm("t1");                     \
64        register unsigned int _tls asm("v1");                    \
65        register unsigned int _v0 asm("v0");                     \
66        asm volatile(                                            \
67            ".set  push\n\t"                                     \
68            ".set  noreorder\n\t"                                \
69            ".set mips32r2\n\t"                                  \
70            "rdhwr %[tls], $29\n\t"                              \
71            "lw    %[t0], %[OPENGL_API](%[tls])\n\t"             \
72            "beqz  %[t0], 1f\n\t"                                \
73            " move %[fn],$ra\n\t"                                \
74            "lw    %[fn], %[API](%[t0])\n\t"                     \
75            "movz  %[fn], $ra, %[fn]\n\t"                        \
76            "1:\n\t"                                             \
77            "j     %[fn]\n\t"                                    \
78            " move %[v0], $0\n\t"                                \
79            ".set  pop\n\t"                                      \
80            : [fn] "=c"(_fn),                                    \
81              [tls] "=&r"(_tls),                                 \
82              [t0] "=&r"(_t0),                                   \
83              [v0] "=&r"(_v0)                                    \
84            : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4),           \
85              [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
86            :                                                    \
87            );
88
89#else
90
91    #define API_ENTRY(_api) _api
92
93    #define CALL_GL_API(_api, ...)                                       \
94        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;  \
95        if (_c) return _c->_api(__VA_ARGS__);
96
97#endif
98
99#define CALL_GL_API_RETURN(_api, ...) \
100    CALL_GL_API(_api, __VA_ARGS__) \
101    return 0;
102
103
104
105extern "C" {
106#pragma GCC diagnostic ignored "-Wunused-parameter"
107#include "gl2_api.in"
108#include "gl2ext_api.in"
109#pragma GCC diagnostic warning "-Wunused-parameter"
110}
111
112#undef API_ENTRY
113#undef CALL_GL_API
114#undef CALL_GL_API_RETURN
115
116/*
117 * glGetString() is special because we expose some extensions in the wrapper
118 */
119
120extern "C" const GLubyte * __glGetString(GLenum name);
121
122const GLubyte * glGetString(GLenum name)
123{
124    const GLubyte * ret = egl_get_string_for_current_context(name);
125    if (ret == NULL) {
126        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
127        ret = _c->glGetString(name);
128    }
129    return ret;
130}
131