blob: 0fb8965f894097b333496373d0f9449e257aebdf [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopianb1a39d62009-05-27 20:38:06 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** 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
Mathias Agopianb1a39d62009-05-27 20:38:06 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopianb1a39d62009-05-27 20:38:06 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** 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
Mathias Agopianb1a39d62009-05-27 20:38:06 -070014 ** 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
Mathias Agopianb1a39d62009-05-27 20:38:06 -070023#include <cutils/log.h>
24#include <cutils/properties.h>
25
Mathias Agopian39c24a22013-04-04 23:17:56 -070026#include "../hooks.h"
27#include "../egl_impl.h"
Mathias Agopianb1a39d62009-05-27 20:38:06 -070028
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
Jesse Hall30a41aa2014-05-30 23:32:12 -070039#if USE_SLOW_BINDING
40
41 #define API_ENTRY(_api) _api
42
43 #define CALL_GL_API(_api, ...) \
44 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
45 if (_c) return _c->_api(__VA_ARGS__);
46
47#elif defined(__arm__)
Duane Sand46b42532013-03-27 10:58:06 -070048
Elliott Hughes288870e2013-02-13 17:30:54 -080049 #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
Mathias Agopian673d2db2009-10-14 02:39:53 -070050
Mathias Agopiane0ea89c2013-06-14 19:08:36 -070051 #define API_ENTRY(_api) __attribute__((noinline)) _api
Mathias Agopianb1a39d62009-05-27 20:38:06 -070052
53 #define CALL_GL_API(_api, ...) \
54 asm volatile( \
Mathias Agopian673d2db2009-10-14 02:39:53 -070055 GET_TLS(r12) \
Mathias Agopianb1a39d62009-05-27 20:38:06 -070056 "ldr r12, [r12, %[tls]] \n" \
57 "cmp r12, #0 \n" \
58 "ldrne pc, [r12, %[api]] \n" \
Mathias Agopianb1a39d62009-05-27 20:38:06 -070059 : \
60 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
Mathias Agopian618fa102009-10-14 02:06:37 -070061 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
Jesse Hall30a41aa2014-05-30 23:32:12 -070062 : "r12" \
Mathias Agopianb1a39d62009-05-27 20:38:06 -070063 );
Mathias Agopian673d2db2009-10-14 02:39:53 -070064
Jesse Hall30a41aa2014-05-30 23:32:12 -070065#elif defined(__aarch64__)
66
67 #define API_ENTRY(_api) __attribute__((noinline)) _api
68
69 #define CALL_GL_API(_api, ...) \
70 asm volatile( \
71 "mrs x16, tpidr_el0\n" \
72 "ldr x16, [x16, %[tls]]\n" \
73 "cbz x16, 1f\n" \
74 "ldr x16, [x16, %[api]]\n" \
75 "br x16\n" \
76 "1:\n" \
77 : \
78 : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \
79 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
80 : "x16" \
81 );
82
83#elif defined(__i386__)
mwajdeczc80aafa2014-05-26 13:56:37 +020084
Michal Wajdeczko701048c2014-08-08 17:26:25 +020085 #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
mwajdeczc80aafa2014-05-26 13:56:37 +020086
87 #define CALL_GL_API(_api, ...) \
88 register void** fn; \
89 __asm__ volatile( \
90 "mov %%gs:0, %[fn]\n" \
91 "mov %P[tls](%[fn]), %[fn]\n" \
92 "test %[fn], %[fn]\n" \
93 "je 1f\n" \
94 "jmp *%P[api](%[fn])\n" \
95 "1:\n" \
96 : [fn] "=r" (fn) \
97 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
98 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
99 : "cc" \
100 );
101
Jesse Hall30a41aa2014-05-30 23:32:12 -0700102#elif defined(__x86_64__)
mwajdeczc80aafa2014-05-26 13:56:37 +0200103
Michal Wajdeczko701048c2014-08-08 17:26:25 +0200104 #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
mwajdeczc80aafa2014-05-26 13:56:37 +0200105
106 #define CALL_GL_API(_api, ...) \
107 register void** fn; \
108 __asm__ volatile( \
109 "mov %%fs:0, %[fn]\n" \
110 "mov %P[tls](%[fn]), %[fn]\n" \
111 "test %[fn], %[fn]\n" \
112 "je 1f\n" \
113 "jmp *%P[api](%[fn])\n" \
114 "1:\n" \
115 : [fn] "=r" (fn) \
116 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
117 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
118 : "cc" \
119 );
120
Jesse Hall30a41aa2014-05-30 23:32:12 -0700121#elif defined(__mips__)
Duane Sand46b42532013-03-27 10:58:06 -0700122
123 #define API_ENTRY(_api) __attribute__((noinline)) _api
124
125 #define CALL_GL_API(_api, ...) \
Jesse Hall441f6942013-03-30 23:22:19 -0700126 register unsigned int _t0 asm("t0"); \
127 register unsigned int _fn asm("t1"); \
128 register unsigned int _tls asm("v1"); \
129 register unsigned int _v0 asm("v0"); \
Duane Sand46b42532013-03-27 10:58:06 -0700130 asm volatile( \
131 ".set push\n\t" \
132 ".set noreorder\n\t" \
133 ".set mips32r2\n\t" \
134 "rdhwr %[tls], $29\n\t" \
135 "lw %[t0], %[OPENGL_API](%[tls])\n\t" \
136 "beqz %[t0], 1f\n\t" \
137 " move %[fn],$ra\n\t" \
138 "lw %[fn], %[API](%[t0])\n\t" \
139 "movz %[fn], $ra, %[fn]\n\t" \
140 "1:\n\t" \
141 "j %[fn]\n\t" \
142 " move %[v0], $0\n\t" \
143 ".set pop\n\t" \
Jesse Hall441f6942013-03-30 23:22:19 -0700144 : [fn] "=c"(_fn), \
145 [tls] "=&r"(_tls), \
146 [t0] "=&r"(_t0), \
147 [v0] "=&r"(_v0) \
Duane Sand46b42532013-03-27 10:58:06 -0700148 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
149 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
150 : \
151 );
152
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700153#endif
154
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700155#define CALL_GL_API_RETURN(_api, ...) \
156 CALL_GL_API(_api, __VA_ARGS__) \
157 return 0;
158
159
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700160
161extern "C" {
Jesse Hallbbbddb82014-05-13 21:13:14 -0700162#pragma GCC diagnostic ignored "-Wunused-parameter"
Jesse Hall4c0596f2014-05-13 16:48:35 -0700163#include "gl2_api.in"
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700164#include "gl2ext_api.in"
Jesse Hallbbbddb82014-05-13 21:13:14 -0700165#pragma GCC diagnostic warning "-Wunused-parameter"
Mathias Agopianb1a39d62009-05-27 20:38:06 -0700166}
167
168#undef API_ENTRY
169#undef CALL_GL_API
170#undef CALL_GL_API_RETURN
171
Mathias Agopian48d438d2012-01-28 21:44:00 -0800172/*
173 * glGetString() is special because we expose some extensions in the wrapper
174 */
175
176extern "C" const GLubyte * __glGetString(GLenum name);
177
178const GLubyte * glGetString(GLenum name)
179{
180 const GLubyte * ret = egl_get_string_for_current_context(name);
181 if (ret == NULL) {
Mathias Agopiane0ea89c2013-06-14 19:08:36 -0700182 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
Anshuman Dani06270712013-11-05 21:04:00 +0530183 if(_c) ret = _c->glGetString(name);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800184 }
185 return ret;
186}