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