gl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include <ctype.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <sys/ioctl.h>
  20. #include <GLES/gl.h>
  21. #include <GLES/glext.h>
  22. #include <cutils/log.h>
  23. #include <cutils/properties.h>
  24. #include "../hooks.h"
  25. #include "../egl_impl.h"
  26. using namespace android;
  27. // ----------------------------------------------------------------------------
  28. // extensions for the framework
  29. // ----------------------------------------------------------------------------
  30. extern "C" {
  31. GL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
  32. const GLvoid *ptr, GLsizei count);
  33. GL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride,
  34. const GLvoid *pointer, GLsizei count);
  35. GL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type,
  36. GLsizei stride, const GLvoid *pointer, GLsizei count);
  37. GL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type,
  38. GLsizei stride, const GLvoid *pointer, GLsizei count);
  39. GL_API void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
  40. GLsizei stride, const GLvoid *pointer, GLsizei count);
  41. GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
  42. GLsizei stride, const GLvoid *pointer, GLsizei count);
  43. GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
  44. GLsizei stride, const GLvoid *pointer, GLsizei count);
  45. }
  46. void glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
  47. const GLvoid *ptr, GLsizei /*count*/) {
  48. glColorPointer(size, type, stride, ptr);
  49. }
  50. void glNormalPointerBounds(GLenum type, GLsizei stride,
  51. const GLvoid *pointer, GLsizei /*count*/) {
  52. glNormalPointer(type, stride, pointer);
  53. }
  54. void glTexCoordPointerBounds(GLint size, GLenum type,
  55. GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
  56. glTexCoordPointer(size, type, stride, pointer);
  57. }
  58. void glVertexPointerBounds(GLint size, GLenum type,
  59. GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
  60. glVertexPointer(size, type, stride, pointer);
  61. }
  62. void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
  63. GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
  64. glPointSizePointerOES(type, stride, pointer);
  65. }
  66. GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
  67. GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
  68. glMatrixIndexPointerOES(size, type, stride, pointer);
  69. }
  70. GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
  71. GLsizei stride, const GLvoid *pointer, GLsizei /*count*/) {
  72. glWeightPointerOES(size, type, stride, pointer);
  73. }
  74. // ----------------------------------------------------------------------------
  75. // Actual GL entry-points
  76. // ----------------------------------------------------------------------------
  77. #undef API_ENTRY
  78. #undef CALL_GL_API
  79. #undef CALL_GL_API_RETURN
  80. #if USE_SLOW_BINDING
  81. #define API_ENTRY(_api) _api
  82. #define CALL_GL_API(_api, ...) \
  83. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
  84. if (_c) return _c->_api(__VA_ARGS__);
  85. #elif defined(__arm__)
  86. #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
  87. #define API_ENTRY(_api) __attribute__((noinline)) _api
  88. #define CALL_GL_API(_api, ...) \
  89. asm volatile( \
  90. GET_TLS(r12) \
  91. "ldr r12, [r12, %[tls]] \n" \
  92. "cmp r12, #0 \n" \
  93. "ldrne pc, [r12, %[api]] \n" \
  94. : \
  95. : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
  96. [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
  97. : "r12" \
  98. );
  99. #elif defined(__aarch64__)
  100. #define API_ENTRY(_api) __attribute__((noinline)) _api
  101. #define CALL_GL_API(_api, ...) \
  102. asm volatile( \
  103. "mrs x16, tpidr_el0\n" \
  104. "ldr x16, [x16, %[tls]]\n" \
  105. "cbz x16, 1f\n" \
  106. "ldr x16, [x16, %[api]]\n" \
  107. "br x16\n" \
  108. "1:\n" \
  109. : \
  110. : [tls] "i" (TLS_SLOT_OPENGL_API * sizeof(void*)), \
  111. [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
  112. : "x16" \
  113. );
  114. #elif defined(__i386__)
  115. #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
  116. #define CALL_GL_API(_api, ...) \
  117. register void* fn; \
  118. __asm__ volatile( \
  119. "mov %%gs:0, %[fn]\n" \
  120. "mov %P[tls](%[fn]), %[fn]\n" \
  121. "test %[fn], %[fn]\n" \
  122. "je 1f\n" \
  123. "jmp *%P[api](%[fn])\n" \
  124. "1:\n" \
  125. : [fn] "=r" (fn) \
  126. : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
  127. [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
  128. : "cc" \
  129. );
  130. #elif defined(__x86_64__)
  131. #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
  132. #define CALL_GL_API(_api, ...) \
  133. register void** fn; \
  134. __asm__ volatile( \
  135. "mov %%fs:0, %[fn]\n" \
  136. "mov %P[tls](%[fn]), %[fn]\n" \
  137. "test %[fn], %[fn]\n" \
  138. "je 1f\n" \
  139. "jmp *%P[api](%[fn])\n" \
  140. "1:\n" \
  141. : [fn] "=r" (fn) \
  142. : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
  143. [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
  144. : "cc" \
  145. );
  146. #elif defined(__mips64)
  147. #define API_ENTRY(_api) __attribute__((noinline)) _api
  148. #define CALL_GL_API(_api, ...) \
  149. register unsigned long _t0 asm("$12"); \
  150. register unsigned long _fn asm("$25"); \
  151. register unsigned long _tls asm("$3"); \
  152. register unsigned long _v0 asm("$2"); \
  153. asm volatile( \
  154. ".set push\n\t" \
  155. ".set noreorder\n\t" \
  156. "rdhwr %[tls], $29\n\t" \
  157. "ld %[t0], %[OPENGL_API](%[tls])\n\t" \
  158. "beqz %[t0], 1f\n\t" \
  159. " move %[fn], $ra\n\t" \
  160. "ld %[t0], %[API](%[t0])\n\t" \
  161. "beqz %[t0], 1f\n\t" \
  162. " nop\n\t" \
  163. "move %[fn], %[t0]\n\t" \
  164. "1:\n\t" \
  165. "jalr $0, %[fn]\n\t" \
  166. " move %[v0], $0\n\t" \
  167. ".set pop\n\t" \
  168. : [fn] "=c"(_fn), \
  169. [tls] "=&r"(_tls), \
  170. [t0] "=&r"(_t0), \
  171. [v0] "=&r"(_v0) \
  172. : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\
  173. [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
  174. : \
  175. );
  176. #elif defined(__mips__)
  177. #define API_ENTRY(_api) __attribute__((noinline)) _api
  178. #define CALL_GL_API(_api, ...) \
  179. register unsigned int _t0 asm("$8"); \
  180. register unsigned int _fn asm("$25"); \
  181. register unsigned int _tls asm("$3"); \
  182. register unsigned int _v0 asm("$2"); \
  183. asm volatile( \
  184. ".set push\n\t" \
  185. ".set noreorder\n\t" \
  186. ".set mips32r2\n\t" \
  187. "rdhwr %[tls], $29\n\t" \
  188. "lw %[t0], %[OPENGL_API](%[tls])\n\t" \
  189. "beqz %[t0], 1f\n\t" \
  190. " move %[fn], $ra\n\t" \
  191. "lw %[t0], %[API](%[t0])\n\t" \
  192. "beqz %[t0], 1f\n\t" \
  193. " nop\n\t" \
  194. "move %[fn], %[t0]\n\t" \
  195. "1:\n\t" \
  196. "jalr $0, %[fn]\n\t" \
  197. " move %[v0], $0\n\t" \
  198. ".set pop\n\t" \
  199. : [fn] "=c"(_fn), \
  200. [tls] "=&r"(_tls), \
  201. [t0] "=&r"(_t0), \
  202. [v0] "=&r"(_v0) \
  203. : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
  204. [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
  205. : \
  206. );
  207. #endif
  208. #define CALL_GL_API_RETURN(_api, ...) \
  209. CALL_GL_API(_api, __VA_ARGS__) \
  210. return 0;
  211. extern "C" {
  212. #pragma GCC diagnostic ignored "-Wunused-parameter"
  213. #include "gl_api.in"
  214. #include "glext_api.in"
  215. #pragma GCC diagnostic warning "-Wunused-parameter"
  216. }
  217. #undef API_ENTRY
  218. #undef CALL_GL_API
  219. #undef CALL_GL_API_RETURN
  220. /*
  221. * glGetString() is special because we expose some extensions in the wrapper
  222. */
  223. extern "C" const GLubyte * __glGetString(GLenum name);
  224. const GLubyte * glGetString(GLenum name) {
  225. const GLubyte * ret = egl_get_string_for_current_context(name);
  226. if (ret == NULL) {
  227. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  228. ret = _c->glGetString(name);
  229. }
  230. return ret;
  231. }