getProcAddress.cpp 13 KB

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