Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame^] | 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 | |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | #include <cutils/log.h> |
| 20 | |
| 21 | #include <EGL/egl.h> |
| 22 | |
| 23 | #include "egl_tls.h" |
| 24 | |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | pthread_key_t egl_tls_t::sKey = -1; |
| 29 | pthread_mutex_t egl_tls_t::sLockKey = PTHREAD_MUTEX_INITIALIZER; |
| 30 | |
| 31 | egl_tls_t::egl_tls_t() |
| 32 | : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE), dbg(0) { |
| 33 | } |
| 34 | |
| 35 | const char *egl_tls_t::egl_strerror(EGLint err) { |
| 36 | switch (err) { |
| 37 | case EGL_SUCCESS: return "EGL_SUCCESS"; |
| 38 | case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; |
| 39 | case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; |
| 40 | case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; |
| 41 | case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; |
| 42 | case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; |
| 43 | case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; |
| 44 | case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; |
| 45 | case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; |
| 46 | case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; |
| 47 | case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; |
| 48 | case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; |
| 49 | case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; |
| 50 | case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; |
| 51 | case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; |
| 52 | default: return "UNKNOWN"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void egl_tls_t::validateTLSKey() |
| 57 | { |
| 58 | if (sKey == -1) { |
| 59 | pthread_mutex_lock(&sLockKey); |
| 60 | if (sKey == -1) |
| 61 | pthread_key_create(&sKey, NULL); |
| 62 | pthread_mutex_unlock(&sLockKey); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void egl_tls_t::setErrorEtcImpl(const char* caller, int line, EGLint error) { |
| 67 | validateTLSKey(); |
| 68 | egl_tls_t* tls = getTLS(); |
| 69 | if (tls->error != error) { |
| 70 | LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error)); |
| 71 | tls->error = error; |
| 72 | } |
| 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 | |
| 84 | egl_tls_t* egl_tls_t::getTLS() { |
| 85 | egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey); |
| 86 | if (tls == 0) { |
| 87 | tls = new egl_tls_t; |
| 88 | pthread_setspecific(sKey, tls); |
| 89 | } |
| 90 | return tls; |
| 91 | } |
| 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 | |
| 103 | void egl_tls_t::clearError() { |
| 104 | // This must clear the error from all the underlying EGL implementations as |
| 105 | // well as the EGL wrapper layer. |
| 106 | eglGetError(); |
| 107 | } |
| 108 | |
| 109 | EGLint egl_tls_t::getError() { |
| 110 | if (sKey == -1) |
| 111 | return EGL_SUCCESS; |
| 112 | egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey); |
| 113 | if (!tls) return EGL_SUCCESS; |
| 114 | EGLint error = tls->error; |
| 115 | tls->error = EGL_SUCCESS; |
| 116 | return error; |
| 117 | } |
| 118 | |
| 119 | void egl_tls_t::setContext(EGLContext ctx) { |
| 120 | validateTLSKey(); |
| 121 | getTLS()->ctx = ctx; |
| 122 | } |
| 123 | |
| 124 | EGLContext egl_tls_t::getContext() { |
| 125 | if (sKey == -1) |
| 126 | return EGL_NO_CONTEXT; |
| 127 | egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey); |
| 128 | if (!tls) return EGL_NO_CONTEXT; |
| 129 | return tls->ctx; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | } // namespace android |