egl_tls.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ** Copyright 2011, 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 "egl_tls.h"
  17. #include <stdlib.h>
  18. #include <cutils/properties.h>
  19. #include <log/log.h>
  20. #include "CallStack.h"
  21. #include "egl_platform_entries.h"
  22. namespace android {
  23. pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
  24. pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
  25. egl_tls_t::egl_tls_t()
  26. : error(EGL_SUCCESS), ctx(nullptr), logCallWithNoContext(true) {
  27. }
  28. const char *egl_tls_t::egl_strerror(EGLint err) {
  29. switch (err) {
  30. case EGL_SUCCESS: return "EGL_SUCCESS";
  31. case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
  32. case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
  33. case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
  34. case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
  35. case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
  36. case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
  37. case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
  38. case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
  39. case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
  40. case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
  41. case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
  42. case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
  43. case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
  44. case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
  45. default: return "UNKNOWN";
  46. }
  47. }
  48. void egl_tls_t::validateTLSKey()
  49. {
  50. struct TlsKeyInitializer {
  51. static void create() { pthread_key_create(&sKey, destructTLSData); }
  52. };
  53. pthread_once(&sOnceKey, TlsKeyInitializer::create);
  54. }
  55. void egl_tls_t::destructTLSData(void* data) {
  56. egl_tls_t* tls = static_cast<egl_tls_t*>(data);
  57. if (!tls) return;
  58. // Several things in the call tree of eglReleaseThread expect to be able to get the current
  59. // thread state directly from TLS. That's a problem because Bionic has already cleared our
  60. // TLS pointer before calling this function (pthread_getspecific(sKey) will return nullptr).
  61. // Instead the data is passed as our parameter.
  62. //
  63. // Ideally we'd refactor this so we have thin wrappers that retrieve thread state from TLS and
  64. // then pass it as a parameter (or 'this' pointer) to functions that do the real work without
  65. // touching TLS. Then from here we could just call those implementation functions with the the
  66. // TLS data we just received as a parameter.
  67. //
  68. // But that's a fairly invasive refactoring, so to do this robustly in the short term we just
  69. // put the data *back* in TLS and call the top-level eglReleaseThread. It and it's call tree
  70. // will retrieve the value from TLS, and then finally clear the TLS data. Bionic explicitly
  71. // tolerates re-setting the value that it's currently trying to destruct (see
  72. // pthread_key_clean_all()). Even if we forgot to clear the restored TLS data, bionic would
  73. // call the destructor again, but eventually gives up and just leaks the data rather than
  74. // enter an infinite loop.
  75. pthread_setspecific(sKey, tls);
  76. eglReleaseThread();
  77. ALOGE_IF(pthread_getspecific(sKey) != nullptr,
  78. "EGL TLS data still exists after eglReleaseThread");
  79. }
  80. void egl_tls_t::setErrorEtcImpl(
  81. const char* caller, int line, EGLint error, bool quiet) {
  82. validateTLSKey();
  83. egl_tls_t* tls = getTLS();
  84. if (tls->error != error) {
  85. if (!quiet) {
  86. ALOGE("%s:%d error %x (%s)",
  87. caller, line, error, egl_strerror(error));
  88. char value[PROPERTY_VALUE_MAX];
  89. property_get("debug.egl.callstack", value, "0");
  90. if (atoi(value)) {
  91. CallStack::log(LOG_TAG);
  92. }
  93. }
  94. tls->error = error;
  95. }
  96. }
  97. bool egl_tls_t::logNoContextCall() {
  98. egl_tls_t* tls = getTLS();
  99. if (tls->logCallWithNoContext) {
  100. tls->logCallWithNoContext = false;
  101. return true;
  102. }
  103. return false;
  104. }
  105. egl_tls_t* egl_tls_t::getTLS() {
  106. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  107. if (tls == nullptr) {
  108. tls = new egl_tls_t;
  109. pthread_setspecific(sKey, tls);
  110. }
  111. return tls;
  112. }
  113. void egl_tls_t::clearTLS() {
  114. if (sKey != TLS_KEY_NOT_INITIALIZED) {
  115. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  116. if (tls) {
  117. pthread_setspecific(sKey, nullptr);
  118. delete tls;
  119. }
  120. }
  121. }
  122. void egl_tls_t::clearError() {
  123. // This must clear the error from all the underlying EGL implementations as
  124. // well as the EGL wrapper layer.
  125. android::eglGetErrorImpl();
  126. }
  127. EGLint egl_tls_t::getError() {
  128. if (sKey == TLS_KEY_NOT_INITIALIZED) {
  129. return EGL_SUCCESS;
  130. }
  131. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  132. if (!tls) {
  133. return EGL_SUCCESS;
  134. }
  135. EGLint error = tls->error;
  136. tls->error = EGL_SUCCESS;
  137. return error;
  138. }
  139. void egl_tls_t::setContext(EGLContext ctx) {
  140. validateTLSKey();
  141. getTLS()->ctx = ctx;
  142. }
  143. EGLContext egl_tls_t::getContext() {
  144. if (sKey == TLS_KEY_NOT_INITIALIZED) {
  145. return EGL_NO_CONTEXT;
  146. }
  147. egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
  148. if (!tls) return EGL_NO_CONTEXT;
  149. return tls->ctx;
  150. }
  151. } // namespace android