1 /* 2 ** Copyright 2009, 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 <stdlib.h> 19 #include <errno.h> 20 21 #include <cutils/log.h> 22 23 #include "egldefs.h" 24 25 // ---------------------------------------------------------------------------- 26 namespace android { 27 // ---------------------------------------------------------------------------- 28 29 #undef API_ENTRY 30 #undef CALL_GL_EXTENSION_API 31 #undef GL_EXTENSION 32 #undef GL_EXTENSION_NAME 33 #undef GL_EXTENSION_ARRAY 34 #undef GL_EXTENSION_LIST 35 #undef GET_TLS 36 37 #if defined(__arm__) 38 39 #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n" 40 41 #define API_ENTRY(_api) __attribute__((naked)) _api 42 43 #define CALL_GL_EXTENSION_API(_api) \ 44 asm volatile( \ 45 GET_TLS(r12) \ 46 "ldr r12, [r12, %[tls]] \n" \ 47 "cmp r12, #0 \n" \ 48 "addne r12, %[api] \n" \ 49 "ldrne r12, [r12, %[ext]] \n" \ 50 "cmpne r12, #0 \n" \ 51 "bxne r12 \n" \ 52 "bx lr \n" \ 53 : \ 54 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \ 55 [ext] "J"(__builtin_offsetof(gl_hooks_t, \ 56 ext.extensions[0])), \ 57 [api] "J"(_api*sizeof(void*)) \ 58 : "r12" \ 59 ); 60 61 #elif defined(__aarch64__) 62 63 #define API_ENTRY(_api) __attribute__((noinline)) _api 64 65 #define CALL_GL_EXTENSION_API(_api) \ 66 asm volatile( \ 67 "mrs x16, tpidr_el0\n" \ 68 "ldr x16, [x16, %[tls]]\n" \ 69 "cbz x16, 1f\n" \ 70 "ldr x16, [x16, %[api]]\n" \ 71 "cbz x16, 1f\n" \ 72 "br x16\n" \ 73 "1:\n" \ 74 : \ 75 : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \ 76 [api] "i" (__builtin_offsetof(gl_hooks_t, \ 77 ext.extensions[_api])) \ 78 : "x16" \ 79 ); 80 81 #elif defined(__i386__) 82 83 #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api 84 85 #define CALL_GL_EXTENSION_API(_api) \ 86 register void** fn; \ 87 __asm__ volatile( \ 88 "mov %%gs:0, %[fn]\n" \ 89 "mov %P[tls](%[fn]), %[fn]\n" \ 90 "test %[fn], %[fn]\n" \ 91 "cmovne %P[api](%[fn]), %[fn]\n" \ 92 "test %[fn], %[fn]\n" \ 93 "je 1f\n" \ 94 "jmp *%[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, \ 99 ext.extensions[_api])) \ 100 : "cc" \ 101 ); 102 103 #elif defined(__x86_64__) 104 105 #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api 106 107 #define CALL_GL_EXTENSION_API(_api) \ 108 register void** fn; \ 109 __asm__ volatile( \ 110 "mov %%fs:0, %[fn]\n" \ 111 "mov %P[tls](%[fn]), %[fn]\n" \ 112 "test %[fn], %[fn]\n" \ 113 "cmovne %P[api](%[fn]), %[fn]\n" \ 114 "test %[fn], %[fn]\n" \ 115 "je 1f\n" \ 116 "jmp *%[fn]\n" \ 117 "1:\n" \ 118 : [fn] "=r" (fn) \ 119 : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \ 120 [api] "i" (__builtin_offsetof(gl_hooks_t, \ 121 ext.extensions[_api])) \ 122 : "cc" \ 123 ); 124 125 #elif defined(__mips64) 126 127 #define API_ENTRY(_api) __attribute__((noinline)) _api 128 129 #define CALL_GL_EXTENSION_API(_api, ...) \ 130 register unsigned int _t0 asm("$12"); \ 131 register unsigned int _fn asm("$25"); \ 132 register unsigned int _tls asm("$3"); \ 133 asm volatile( \ 134 ".set push\n\t" \ 135 ".set noreorder\n\t" \ 136 "rdhwr %[tls], $29\n\t" \ 137 "ld %[t0], %[OPENGL_API](%[tls])\n\t" \ 138 "beqz %[t0], 1f\n\t" \ 139 " move %[fn], $ra\n\t" \ 140 "ld %[t0], %[API](%[t0])\n\t" \ 141 "beqz %[t0], 1f\n\t" \ 142 " nop\n\t" \ 143 "move %[fn], %[t0]\n\t" \ 144 "1:\n\t" \ 145 "jalr $0, %[fn]\n\t" \ 146 " nop\n\t" \ 147 ".set pop\n\t" \ 148 : [fn] "=c"(_fn), \ 149 [tls] "=&r"(_tls), \ 150 [t0] "=&r"(_t0) \ 151 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \ 152 [API] "I"(__builtin_offsetof(gl_hooks_t, \ 153 ext.extensions[_api])) \ 154 : \ 155 ); 156 157 #elif defined(__mips__) 158 159 #define API_ENTRY(_api) __attribute__((noinline)) _api 160 161 #define CALL_GL_EXTENSION_API(_api, ...) \ 162 register unsigned int _t0 asm("$8"); \ 163 register unsigned int _fn asm("$25"); \ 164 register unsigned int _tls asm("$3"); \ 165 asm volatile( \ 166 ".set push\n\t" \ 167 ".set noreorder\n\t" \ 168 ".set mips32r2\n\t" \ 169 "rdhwr %[tls], $29\n\t" \ 170 "lw %[t0], %[OPENGL_API](%[tls])\n\t" \ 171 "beqz %[t0], 1f\n\t" \ 172 " move %[fn], $ra\n\t" \ 173 "lw %[t0], %[API](%[t0])\n\t" \ 174 "beqz %[t0], 1f\n\t" \ 175 " nop\n\t" \ 176 "move %[fn], %[t0]\n\t" \ 177 "1:\n\t" \ 178 "jalr $0, %[fn]\n\t" \ 179 " nop\n\t" \ 180 ".set pop\n\t" \ 181 : [fn] "=c"(_fn), \ 182 [tls] "=&r"(_tls), \ 183 [t0] "=&r"(_t0) \ 184 : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \ 185 [API] "I"(__builtin_offsetof(gl_hooks_t, \ 186 ext.extensions[_api])) \ 187 : \ 188 ); 189 190 #endif 191 192 #if defined(CALL_GL_EXTENSION_API) 193 #define GL_EXTENSION_NAME(_n) __glExtFwd##_n 194 195 #define GL_EXTENSION(_n) \ 196 void API_ENTRY(GL_EXTENSION_NAME(_n))() { \ 197 CALL_GL_EXTENSION_API(_n); \ 198 } 199 #else 200 #define GL_EXTENSION_NAME(_n) NULL 201 202 #define GL_EXTENSION(_n) 203 204 #warning "eglGetProcAddress() partially supported" 205 #endif 206 207 208 #define GL_EXTENSION_LIST(name) \ 209 name(0) name(1) name(2) name(3) name(4) name(5) name(6) name(7) \ 210 name(8) name(9) name(10) name(11) name(12) name(13) name(14) name(15) \ 211 name(16) name(17) name(18) name(19) name(20) name(21) name(22) name(23) \ 212 name(24) name(25) name(26) name(27) name(28) name(29) name(30) name(31) \ 213 name(32) name(33) name(34) name(35) name(36) name(37) name(38) name(39) \ 214 name(40) name(41) name(42) name(43) name(44) name(45) name(46) name(47) \ 215 name(48) name(49) name(50) name(51) name(52) name(53) name(54) name(55) \ 216 name(56) name(57) name(58) name(59) name(60) name(61) name(62) name(63) \ 217 name(64) name(65) name(66) name(67) name(68) name(69) name(70) name(71) \ 218 name(72) name(73) name(74) name(75) name(76) name(77) name(78) name(79) \ 219 name(80) name(81) name(82) name(83) name(84) name(85) name(86) name(87) \ 220 name(88) name(89) name(90) name(91) name(92) name(93) name(94) name(95) \ 221 name(96) name(97) name(98) name(99) \ 222 name(100) name(101) name(102) name(103) name(104) name(105) name(106) name(107) \ 223 name(108) name(109) name(110) name(111) name(112) name(113) name(114) name(115) \ 224 name(116) name(117) name(118) name(119) name(120) name(121) name(122) name(123) \ 225 name(124) name(125) name(126) name(127) name(128) name(129) name(130) name(131) \ 226 name(132) name(133) name(134) name(135) name(136) name(137) name(138) name(139) \ 227 name(140) name(141) name(142) name(143) name(144) name(145) name(146) name(147) \ 228 name(148) name(149) name(150) name(151) name(152) name(153) name(154) name(155) \ 229 name(156) name(157) name(158) name(159) name(160) name(161) name(162) name(163) \ 230 name(164) name(165) name(166) name(167) name(168) name(169) name(170) name(171) \ 231 name(172) name(173) name(174) name(175) name(176) name(177) name(178) name(179) \ 232 name(180) name(181) name(182) name(183) name(184) name(185) name(186) name(187) \ 233 name(188) name(189) name(190) name(191) name(192) name(193) name(194) name(195) \ 234 name(196) name(197) name(198) name(199) \ 235 name(200) name(201) name(202) name(203) name(204) name(205) name(206) name(207) \ 236 name(208) name(209) name(210) name(211) name(212) name(213) name(214) name(215) \ 237 name(216) name(217) name(218) name(219) name(220) name(221) name(222) name(223) \ 238 name(224) name(225) name(226) name(227) name(228) name(229) name(230) name(231) \ 239 name(232) name(233) name(234) name(235) name(236) name(237) name(238) name(239) \ 240 name(240) name(241) name(242) name(243) name(244) name(245) name(246) name(247) \ 241 name(248) name(249) name(250) name(251) name(252) name(253) name(254) name(255) 242 243 244 GL_EXTENSION_LIST( GL_EXTENSION ) 245 246 #define GL_EXTENSION_ARRAY(_n) GL_EXTENSION_NAME(_n), 247 248 extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS] = { 249 GL_EXTENSION_LIST( GL_EXTENSION_ARRAY ) 250 }; 251 252 #undef GET_TLS 253 #undef GL_EXTENSION_LIST 254 #undef GL_EXTENSION_ARRAY 255 #undef GL_EXTENSION_NAME 256 #undef GL_EXTENSION 257 #undef API_ENTRY 258 #undef CALL_GL_EXTENSION_API 259 260 // ---------------------------------------------------------------------------- 261 }; // namespace android 262 // ---------------------------------------------------------------------------- 263 264