/opengl/libs/EGL/egl_tls.cpp

http://github.com/CyanogenMod/android_frameworks_base · C++ · 148 lines · 110 code · 21 blank · 17 comment · 22 complexity · 42938ad67b859a729e627079504c3c7a MD5 · raw file

  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 = -1;
  25. pthread_mutex_t egl_tls_t::sLockKey = PTHREAD_MUTEX_INITIALIZER;
  26. egl_tls_t::egl_tls_t()
  27. : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE), dbg(0) {
  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. if (sKey == -1) {
  52. pthread_mutex_lock(&sLockKey);
  53. if (sKey == -1)
  54. pthread_key_create(&sKey, NULL);
  55. pthread_mutex_unlock(&sLockKey);
  56. }
  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. LOGE("%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;
  70. stack.update();
  71. stack.dump();
  72. }
  73. }
  74. tls->error = error;
  75. }
  76. }
  77. bool egl_tls_t::logNoContextCall() {
  78. egl_tls_t* tls = getTLS();
  79. if (tls->logCallWithNoContext == true) {
  80. tls->logCallWithNoContext = false;
  81. return true;
  82. }
  83. return false;
  84. }
  85. egl_tls_t* egl_tls_t::getTLS() {
  86. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  87. if (tls == 0) {
  88. tls = new egl_tls_t;
  89. pthread_setspecific(sKey, tls);
  90. }
  91. return tls;
  92. }
  93. void egl_tls_t::clearTLS() {
  94. if (sKey != -1) {
  95. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  96. if (tls) {
  97. delete tls;
  98. pthread_setspecific(sKey, 0);
  99. }
  100. }
  101. }
  102. void egl_tls_t::clearError() {
  103. // This must clear the error from all the underlying EGL implementations as
  104. // well as the EGL wrapper layer.
  105. eglGetError();
  106. }
  107. EGLint egl_tls_t::getError() {
  108. if (sKey == -1)
  109. return EGL_SUCCESS;
  110. egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
  111. if (!tls) return EGL_SUCCESS;
  112. EGLint error = tls->error;
  113. tls->error = EGL_SUCCESS;
  114. return error;
  115. }
  116. void egl_tls_t::setContext(EGLContext ctx) {
  117. validateTLSKey();
  118. getTLS()->ctx = ctx;
  119. }
  120. EGLContext egl_tls_t::getContext() {
  121. if (sKey == -1)
  122. return EGL_NO_CONTEXT;
  123. egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
  124. if (!tls) return EGL_NO_CONTEXT;
  125. return tls->ctx;
  126. }
  127. } // namespace android