egl_tls.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <stdlib.h>
  17. #include <pthread.h>
  18. #include <cutils/log.h>
  19. #include <cutils/properties.h>
  20. #include <utils/CallStack.h>
  21. #include <EGL/egl.h>
  22. #include "egl_tls.h"
  23. namespace android {
  24. pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
  25. pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
  26. egl_tls_t::egl_tls_t()
  27. : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) {
  28. }
  29. const char *egl_tls_t::egl_strerror(EGLint err) {
  30. switch (err) {
  31. case EGL_SUCCESS: return "EGL_SUCCESS";
  32. case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
  33. case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
  34. case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
  35. case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
  36. case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
  37. case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
  38. case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
  39. case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
  40. case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
  41. case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
  42. case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
  43. case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
  44. case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
  45. case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
  46. default: return "UNKNOWN";
  47. }
  48. }
  49. void egl_tls_t::validateTLSKey()
  50. {
  51. struct TlsKeyInitializer {
  52. static void create() {
  53. pthread_key_create(&sKey, (void (*)(void*))&eglReleaseThread);
  54. }
  55. };
  56. pthread_once(&sOnceKey, TlsKeyInitializer::create);
  57. }
  58. void egl_tls_t::setErrorEtcImpl(
  59. const char* caller, int line, EGLint error, bool quiet) {
  60. validateTLSKey();
  61. egl_tls_t* tls = getTLS();
  62. if (tls->error != error) {
  63. if (!quiet) {
  64. ALOGE("%s:%d error %x (%s)",
  65. caller, line, error, egl_strerror(error));
  66. char value[PROPERTY_VALUE_MAX];
  67. property_get("debug.egl.callstack", value, "0");
  68. if (atoi(value)) {
  69. CallStack stack(LOG_TAG);
  70. }
  71. }
  72. tls->error = error;
  73. }
  74. }
  75. bool egl_tls_t::logNoContextCall() {
  76. egl_tls_t* tls = getTLS();
  77. if (tls->logCallWithNoContext == true) {
  78. tls->logCallWithNoContext = false;
  79. return true;
  80. }
  81. return false;
  82. }
  83. egl_tls_t* egl_tls_t::getTLS() {
  84. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  85. if (tls == 0) {
  86. tls = new egl_tls_t;
  87. pthread_setspecific(sKey, tls);
  88. }
  89. return tls;
  90. }
  91. void egl_tls_t::clearTLS() {
  92. if (sKey != TLS_KEY_NOT_INITIALIZED) {
  93. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  94. if (tls) {
  95. pthread_setspecific(sKey, 0);
  96. delete tls;
  97. }
  98. }
  99. }
  100. void egl_tls_t::clearError() {
  101. // This must clear the error from all the underlying EGL implementations as
  102. // well as the EGL wrapper layer.
  103. eglGetError();
  104. }
  105. EGLint egl_tls_t::getError() {
  106. if (sKey == TLS_KEY_NOT_INITIALIZED) {
  107. return EGL_SUCCESS;
  108. }
  109. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  110. if (!tls) {
  111. return EGL_SUCCESS;
  112. }
  113. EGLint error = tls->error;
  114. tls->error = EGL_SUCCESS;
  115. return error;
  116. }
  117. void egl_tls_t::setContext(EGLContext ctx) {
  118. validateTLSKey();
  119. getTLS()->ctx = ctx;
  120. }
  121. EGLContext egl_tls_t::getContext() {
  122. if (sKey == TLS_KEY_NOT_INITIALIZED) {
  123. return EGL_NO_CONTEXT;
  124. }
  125. egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
  126. if (!tls) return EGL_NO_CONTEXT;
  127. return tls->ctx;
  128. }
  129. } // namespace android