gl2.cpp revision c80aafa61d619087a879dc1095ddf1ca2a5e69fb
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 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 #define API_ENTRY(_api) __attribute__((noinline)) _api 64 65 #define CALL_GL_API(_api, ...) \ 66 register void** fn; \ 67 __asm__ volatile( \ 68 "mov %%gs:0, %[fn]\n" \ 69 "mov %P[tls](%[fn]), %[fn]\n" \ 70 "test %[fn], %[fn]\n" \ 71 "je 1f\n" \ 72 "jmp *%P[api](%[fn])\n" \ 73 "1:\n" \ 74 : [fn] "=r" (fn) \ 75 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ 76 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ 77 : "cc" \ 78 ); 79 80#elif defined(__x86_64__) && !USE_SLOW_BINDING 81 82 #define API_ENTRY(_api) __attribute__((noinline)) _api 83 84 #define CALL_GL_API(_api, ...) \ 85 register void** fn; \ 86 __asm__ volatile( \ 87 "mov %%fs:0, %[fn]\n" \ 88 "mov %P[tls](%[fn]), %[fn]\n" \ 89 "test %[fn], %[fn]\n" \ 90 "je 1f\n" \ 91 "jmp *%P[api](%[fn])\n" \ 92 "1:\n" \ 93 : [fn] "=r" (fn) \ 94 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ 95 [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \ 96 : "cc" \ 97 ); 98 99#elif defined(__mips__) && !USE_SLOW_BINDING 100 101 #define API_ENTRY(_api) __attribute__((noinline)) _api 102 103 #define CALL_GL_API(_api, ...) \ 104 register unsigned int _t0 asm("t0"); \ 105 register unsigned int _fn asm("t1"); \ 106 register unsigned int _tls asm("v1"); \ 107 register unsigned int _v0 asm("v0"); \ 108 asm volatile( \ 109 ".set push\n\t" \ 110 ".set noreorder\n\t" \ 111 ".set mips32r2\n\t" \ 112 "rdhwr %[tls], $29\n\t" \ 113 "lw %[t0], %[OPENGL_API](%[tls])\n\t" \ 114 "beqz %[t0], 1f\n\t" \ 115 " move %[fn],$ra\n\t" \ 116 "lw %[fn], %[API](%[t0])\n\t" \ 117 "movz %[fn], $ra, %[fn]\n\t" \ 118 "1:\n\t" \ 119 "j %[fn]\n\t" \ 120 " move %[v0], $0\n\t" \ 121 ".set pop\n\t" \ 122 : [fn] "=c"(_fn), \ 123 [tls] "=&r"(_tls), \ 124 [t0] "=&r"(_t0), \ 125 [v0] "=&r"(_v0) \ 126 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \ 127 [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \ 128 : \ 129 ); 130 131#else 132 133 #define API_ENTRY(_api) _api 134 135 #define CALL_GL_API(_api, ...) \ 136 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \ 137 if (_c) return _c->_api(__VA_ARGS__); 138 139#endif 140 141#define CALL_GL_API_RETURN(_api, ...) \ 142 CALL_GL_API(_api, __VA_ARGS__) \ 143 return 0; 144 145 146 147extern "C" { 148#include "gl3_api.in" 149#include "gl2ext_api.in" 150#include "gl3ext_api.in" 151} 152 153#undef API_ENTRY 154#undef CALL_GL_API 155#undef CALL_GL_API_RETURN 156 157/* 158 * glGetString() is special because we expose some extensions in the wrapper 159 */ 160 161extern "C" const GLubyte * __glGetString(GLenum name); 162 163const GLubyte * glGetString(GLenum name) 164{ 165 const GLubyte * ret = egl_get_string_for_current_context(name); 166 if (ret == NULL) { 167 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; 168 ret = _c->glGetString(name); 169 } 170 return ret; 171} 172