egl.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <stdlib.h>
  17. #include <hardware/gralloc.h>
  18. #include <EGL/egl.h>
  19. #include <cutils/properties.h>
  20. #include <log/log.h>
  21. #include "../egl_impl.h"
  22. #include "egldefs.h"
  23. #include "egl_tls.h"
  24. #include "egl_display.h"
  25. #include "egl_object.h"
  26. #include "egl_layers.h"
  27. #include "CallStack.h"
  28. #include "Loader.h"
  29. // ----------------------------------------------------------------------------
  30. namespace android {
  31. // ----------------------------------------------------------------------------
  32. egl_connection_t gEGLImpl;
  33. gl_hooks_t gHooks[2];
  34. gl_hooks_t gHooksNoContext;
  35. pthread_key_t gGLWrapperKey = -1;
  36. // ----------------------------------------------------------------------------
  37. void setGLHooksThreadSpecific(gl_hooks_t const *value) {
  38. setGlThreadSpecific(value);
  39. }
  40. /*****************************************************************************/
  41. static int gl_no_context() {
  42. if (egl_tls_t::logNoContextCall()) {
  43. char const* const error = "call to OpenGL ES API with "
  44. "no current context (logged once per thread)";
  45. if (LOG_NDEBUG) {
  46. ALOGE(error);
  47. } else {
  48. LOG_ALWAYS_FATAL(error);
  49. }
  50. char value[PROPERTY_VALUE_MAX];
  51. property_get("debug.egl.callstack", value, "0");
  52. if (atoi(value)) {
  53. CallStack::log(LOG_TAG);
  54. }
  55. }
  56. return 0;
  57. }
  58. static void early_egl_init(void)
  59. {
  60. int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
  61. EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
  62. for (int hook = 0; hook < numHooks; ++hook) {
  63. *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
  64. }
  65. setGLHooksThreadSpecific(&gHooksNoContext);
  66. }
  67. static pthread_once_t once_control = PTHREAD_ONCE_INIT;
  68. static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
  69. // ----------------------------------------------------------------------------
  70. egl_display_ptr validate_display(EGLDisplay dpy) {
  71. egl_display_ptr dp = get_display(dpy);
  72. if (!dp)
  73. return setError(EGL_BAD_DISPLAY, egl_display_ptr(nullptr));
  74. if (!dp->isReady())
  75. return setError(EGL_NOT_INITIALIZED, egl_display_ptr(nullptr));
  76. return dp;
  77. }
  78. egl_display_ptr validate_display_connection(EGLDisplay dpy,
  79. egl_connection_t*& cnx) {
  80. cnx = nullptr;
  81. egl_display_ptr dp = validate_display(dpy);
  82. if (!dp)
  83. return dp;
  84. cnx = &gEGLImpl;
  85. if (cnx->dso == nullptr) {
  86. return setError(EGL_BAD_CONFIG, egl_display_ptr(nullptr));
  87. }
  88. return dp;
  89. }
  90. // ----------------------------------------------------------------------------
  91. const GLubyte * egl_get_string_for_current_context(GLenum name) {
  92. // NOTE: returning NULL here will fall-back to the default
  93. // implementation.
  94. EGLContext context = egl_tls_t::getContext();
  95. if (context == EGL_NO_CONTEXT)
  96. return nullptr;
  97. egl_context_t const * const c = get_context(context);
  98. if (c == nullptr) // this should never happen, by construction
  99. return nullptr;
  100. if (name != GL_EXTENSIONS)
  101. return nullptr;
  102. return (const GLubyte *)c->gl_extensions.c_str();
  103. }
  104. const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
  105. // NOTE: returning NULL here will fall-back to the default
  106. // implementation.
  107. EGLContext context = egl_tls_t::getContext();
  108. if (context == EGL_NO_CONTEXT)
  109. return nullptr;
  110. egl_context_t const * const c = get_context(context);
  111. if (c == nullptr) // this should never happen, by construction
  112. return nullptr;
  113. if (name != GL_EXTENSIONS)
  114. return nullptr;
  115. // if index is out of bounds, assume it will be in the default
  116. // implementation too, so we don't have to generate a GL error here
  117. if (index >= c->tokenized_gl_extensions.size())
  118. return nullptr;
  119. return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
  120. }
  121. GLint egl_get_num_extensions_for_current_context() {
  122. // NOTE: returning -1 here will fall-back to the default
  123. // implementation.
  124. EGLContext context = egl_tls_t::getContext();
  125. if (context == EGL_NO_CONTEXT)
  126. return -1;
  127. egl_context_t const * const c = get_context(context);
  128. if (c == nullptr) // this should never happen, by construction
  129. return -1;
  130. return (GLint)c->tokenized_gl_extensions.size();
  131. }
  132. egl_connection_t* egl_get_connection() {
  133. return &gEGLImpl;
  134. }
  135. // ----------------------------------------------------------------------------
  136. // this mutex protects:
  137. // d->disp[]
  138. // egl_init_drivers_locked()
  139. //
  140. static EGLBoolean egl_init_drivers_locked() {
  141. if (sEarlyInitState) {
  142. // initialized by static ctor. should be set here.
  143. return EGL_FALSE;
  144. }
  145. // get our driver loader
  146. Loader& loader(Loader::getInstance());
  147. // dynamically load our EGL implementation
  148. egl_connection_t* cnx = &gEGLImpl;
  149. cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
  150. cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
  151. cnx->dso = loader.open(cnx);
  152. // Check to see if any layers are enabled and route functions through them
  153. if (cnx->dso) {
  154. // Layers can be enabled long after the drivers have been loaded.
  155. // They will only be initialized once.
  156. LayerLoader& layer_loader(LayerLoader::getInstance());
  157. layer_loader.InitLayers(cnx);
  158. }
  159. return cnx->dso ? EGL_TRUE : EGL_FALSE;
  160. }
  161. static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
  162. EGLBoolean egl_init_drivers() {
  163. EGLBoolean res;
  164. pthread_mutex_lock(&sInitDriverMutex);
  165. res = egl_init_drivers_locked();
  166. pthread_mutex_unlock(&sInitDriverMutex);
  167. return res;
  168. }
  169. static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
  170. static std::chrono::steady_clock::time_point sLogPrintTime;
  171. static constexpr std::chrono::seconds DURATION(1);
  172. void gl_unimplemented() {
  173. bool printLog = false;
  174. auto now = std::chrono::steady_clock::now();
  175. pthread_mutex_lock(&sLogPrintMutex);
  176. if ((now - sLogPrintTime) > DURATION) {
  177. sLogPrintTime = now;
  178. printLog = true;
  179. }
  180. pthread_mutex_unlock(&sLogPrintMutex);
  181. if (printLog) {
  182. ALOGE("called unimplemented OpenGL ES API");
  183. char value[PROPERTY_VALUE_MAX];
  184. property_get("debug.egl.callstack", value, "0");
  185. if (atoi(value)) {
  186. CallStack::log(LOG_TAG);
  187. }
  188. }
  189. }
  190. void gl_noop() {
  191. }
  192. // ----------------------------------------------------------------------------
  193. void setGlThreadSpecific(gl_hooks_t const *value) {
  194. gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
  195. tls_hooks[TLS_SLOT_OPENGL_API] = value;
  196. }
  197. // ----------------------------------------------------------------------------
  198. // GL / EGL hooks
  199. // ----------------------------------------------------------------------------
  200. #undef GL_ENTRY
  201. #undef EGL_ENTRY
  202. #define GL_ENTRY(_r, _api, ...) #_api,
  203. #define EGL_ENTRY(_r, _api, ...) #_api,
  204. char const * const gl_names[] = {
  205. #include "../entries.in"
  206. nullptr
  207. };
  208. char const * const gl_names_1[] = {
  209. #include "../entries_gles1.in"
  210. nullptr
  211. };
  212. char const * const egl_names[] = {
  213. #include "egl_entries.in"
  214. nullptr
  215. };
  216. char const * const platform_names[] = {
  217. #include "platform_entries.in"
  218. nullptr
  219. };
  220. #undef GL_ENTRY
  221. #undef EGL_ENTRY
  222. // ----------------------------------------------------------------------------
  223. }; // namespace android
  224. // ----------------------------------------------------------------------------