PageRenderTime 263ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/opengl/libs/GLES2/gl2.cpp

https://gitlab.com/amardeep434/du_frameworks_native
C++ | 291 lines | 220 code | 47 blank | 24 comment | 23 complexity | b24de8c73f8c8a869af4e83bbf970cd3 MD5 | raw file
  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 <cutils/log.h>
  21. #include <cutils/properties.h>
  22. #include "../hooks.h"
  23. #include "../egl_impl.h"
  24. using namespace android;
  25. // ----------------------------------------------------------------------------
  26. // Actual GL entry-points
  27. // ----------------------------------------------------------------------------
  28. #undef API_ENTRY
  29. #undef CALL_GL_API
  30. #undef CALL_GL_API_RETURN
  31. #if USE_SLOW_BINDING
  32. #define API_ENTRY(_api) _api
  33. #define CALL_GL_API(_api, ...) \
  34. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
  35. if (_c) return _c->_api(__VA_ARGS__);
  36. #elif defined(__arm__)
  37. #define GET_TLS(reg) "mrc p15, 0, " #reg ", c13, c0, 3 \n"
  38. #define API_ENTRY(_api) __attribute__((noinline)) _api
  39. #define CALL_GL_API(_api, ...) \
  40. asm volatile( \
  41. GET_TLS(r12) \
  42. "ldr r12, [r12, %[tls]] \n" \
  43. "cmp r12, #0 \n" \
  44. "ldrne pc, [r12, %[api]] \n" \
  45. : \
  46. : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
  47. [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
  48. : "r12" \
  49. );
  50. #elif defined(__aarch64__)
  51. #define API_ENTRY(_api) __attribute__((noinline)) _api
  52. #define CALL_GL_API(_api, ...) \
  53. asm volatile( \
  54. "mrs x16, tpidr_el0\n" \
  55. "ldr x16, [x16, %[tls]]\n" \
  56. "cbz x16, 1f\n" \
  57. "ldr x16, [x16, %[api]]\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, gl._api)) \
  63. : "x16" \
  64. );
  65. #elif defined(__i386__)
  66. #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
  67. #define CALL_GL_API(_api, ...) \
  68. register void** fn; \
  69. __asm__ volatile( \
  70. "mov %%gs:0, %[fn]\n" \
  71. "mov %P[tls](%[fn]), %[fn]\n" \
  72. "test %[fn], %[fn]\n" \
  73. "je 1f\n" \
  74. "jmp *%P[api](%[fn])\n" \
  75. "1:\n" \
  76. : [fn] "=r" (fn) \
  77. : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
  78. [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
  79. : "cc" \
  80. );
  81. #elif defined(__x86_64__)
  82. #define API_ENTRY(_api) __attribute__((noinline,optimize("omit-frame-pointer"))) _api
  83. #define CALL_GL_API(_api, ...) \
  84. register void** fn; \
  85. __asm__ volatile( \
  86. "mov %%fs:0, %[fn]\n" \
  87. "mov %P[tls](%[fn]), %[fn]\n" \
  88. "test %[fn], %[fn]\n" \
  89. "je 1f\n" \
  90. "jmp *%P[api](%[fn])\n" \
  91. "1:\n" \
  92. : [fn] "=r" (fn) \
  93. : [tls] "i" (TLS_SLOT_OPENGL_API*sizeof(void*)), \
  94. [api] "i" (__builtin_offsetof(gl_hooks_t, gl._api)) \
  95. : "cc" \
  96. );
  97. #elif defined(__mips64)
  98. #define API_ENTRY(_api) __attribute__((noinline)) _api
  99. #define CALL_GL_API(_api, ...) \
  100. register unsigned long _t0 asm("$12"); \
  101. register unsigned long _fn asm("$25"); \
  102. register unsigned long _tls asm("$3"); \
  103. register unsigned long _v0 asm("$2"); \
  104. asm volatile( \
  105. ".set push\n\t" \
  106. ".set noreorder\n\t" \
  107. "rdhwr %[tls], $29\n\t" \
  108. "ld %[t0], %[OPENGL_API](%[tls])\n\t" \
  109. "beqz %[t0], 1f\n\t" \
  110. " move %[fn], $ra\n\t" \
  111. "ld %[t0], %[API](%[t0])\n\t" \
  112. "beqz %[t0], 1f\n\t" \
  113. " nop\n\t" \
  114. "move %[fn], %[t0]\n\t" \
  115. "1:\n\t" \
  116. "jalr $0, %[fn]\n\t" \
  117. " move %[v0], $0\n\t" \
  118. ".set pop\n\t" \
  119. : [fn] "=c"(_fn), \
  120. [tls] "=&r"(_tls), \
  121. [t0] "=&r"(_t0), \
  122. [v0] "=&r"(_v0) \
  123. : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*sizeof(void*)),\
  124. [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
  125. : \
  126. );
  127. #elif defined(__mips__)
  128. #define API_ENTRY(_api) __attribute__((noinline)) _api
  129. #define CALL_GL_API(_api, ...) \
  130. register unsigned int _t0 asm("$8"); \
  131. register unsigned int _fn asm("$25"); \
  132. register unsigned int _tls asm("$3"); \
  133. register unsigned int _v0 asm("$2"); \
  134. asm volatile( \
  135. ".set push\n\t" \
  136. ".set noreorder\n\t" \
  137. ".set mips32r2\n\t" \
  138. "rdhwr %[tls], $29\n\t" \
  139. "lw %[t0], %[OPENGL_API](%[tls])\n\t" \
  140. "beqz %[t0], 1f\n\t" \
  141. " move %[fn],$ra\n\t" \
  142. "lw %[t0], %[API](%[t0])\n\t" \
  143. "beqz %[t0], 1f\n\t" \
  144. " nop\n\t" \
  145. "move %[fn], %[t0]\n\t" \
  146. "1:\n\t" \
  147. "jalr $0, %[fn]\n\t" \
  148. " move %[v0], $0\n\t" \
  149. ".set pop\n\t" \
  150. : [fn] "=c"(_fn), \
  151. [tls] "=&r"(_tls), \
  152. [t0] "=&r"(_t0), \
  153. [v0] "=&r"(_v0) \
  154. : [OPENGL_API] "I"(TLS_SLOT_OPENGL_API*4), \
  155. [API] "I"(__builtin_offsetof(gl_hooks_t, gl._api)) \
  156. : \
  157. );
  158. #endif
  159. #define CALL_GL_API_RETURN(_api, ...) \
  160. CALL_GL_API(_api, __VA_ARGS__) \
  161. return 0;
  162. extern "C" {
  163. #pragma GCC diagnostic ignored "-Wunused-parameter"
  164. #include "gl2_api.in"
  165. #include "gl2ext_api.in"
  166. #pragma GCC diagnostic warning "-Wunused-parameter"
  167. }
  168. #undef API_ENTRY
  169. #undef CALL_GL_API
  170. #undef CALL_GL_API_RETURN
  171. /*
  172. * glGetString() and glGetStringi() are special because we expose some
  173. * extensions in the wrapper. Also, wrapping glGetXXX() is required because
  174. * the value returned for GL_NUM_EXTENSIONS may have been altered by the
  175. * injection of the additional extensions.
  176. */
  177. extern "C" {
  178. const GLubyte * __glGetString(GLenum name);
  179. const GLubyte * __glGetStringi(GLenum name, GLuint index);
  180. void __glGetBooleanv(GLenum pname, GLboolean * data);
  181. void __glGetFloatv(GLenum pname, GLfloat * data);
  182. void __glGetIntegerv(GLenum pname, GLint * data);
  183. void __glGetInteger64v(GLenum pname, GLint64 * data);
  184. }
  185. const GLubyte * glGetString(GLenum name) {
  186. const GLubyte * ret = egl_get_string_for_current_context(name);
  187. if (ret == NULL) {
  188. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  189. if(_c) ret = _c->glGetString(name);
  190. }
  191. return ret;
  192. }
  193. const GLubyte * glGetStringi(GLenum name, GLuint index) {
  194. const GLubyte * ret = egl_get_string_for_current_context(name, index);
  195. if (ret == NULL) {
  196. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  197. if(_c) ret = _c->glGetStringi(name, index);
  198. }
  199. return ret;
  200. }
  201. void glGetBooleanv(GLenum pname, GLboolean * data) {
  202. if (pname == GL_NUM_EXTENSIONS) {
  203. int num_exts = egl_get_num_extensions_for_current_context();
  204. if (num_exts >= 0) {
  205. *data = num_exts > 0 ? GL_TRUE : GL_FALSE;
  206. return;
  207. }
  208. }
  209. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  210. if (_c) _c->glGetBooleanv(pname, data);
  211. }
  212. void glGetFloatv(GLenum pname, GLfloat * data) {
  213. if (pname == GL_NUM_EXTENSIONS) {
  214. int num_exts = egl_get_num_extensions_for_current_context();
  215. if (num_exts >= 0) {
  216. *data = (GLfloat)num_exts;
  217. return;
  218. }
  219. }
  220. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  221. if (_c) _c->glGetFloatv(pname, data);
  222. }
  223. void glGetIntegerv(GLenum pname, GLint * data) {
  224. if (pname == GL_NUM_EXTENSIONS) {
  225. int num_exts = egl_get_num_extensions_for_current_context();
  226. if (num_exts >= 0) {
  227. *data = (GLint)num_exts;
  228. return;
  229. }
  230. }
  231. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  232. if (_c) _c->glGetIntegerv(pname, data);
  233. }
  234. void glGetInteger64v(GLenum pname, GLint64 * data) {
  235. if (pname == GL_NUM_EXTENSIONS) {
  236. int num_exts = egl_get_num_extensions_for_current_context();
  237. if (num_exts >= 0) {
  238. *data = (GLint64)num_exts;
  239. return;
  240. }
  241. }
  242. gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;
  243. if (_c) _c->glGetInteger64v(pname, data);
  244. }