| 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 |  | 
| Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 17 | #include "egl_tls.h" | 
|  | 18 |  | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 19 | #include <stdlib.h> | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 20 |  | 
| Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 22 | #include <log/log.h> | 
| Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 23 | #include "CallStack.h" | 
| Cody Northrop | 9a9a1f4 | 2018-10-15 18:32:41 -0600 | [diff] [blame] | 24 | #include "egl_platform_entries.h" | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 25 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 26 | namespace android { | 
|  | 27 |  | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 28 | pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED; | 
|  | 29 | pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | egl_tls_t::egl_tls_t() | 
| Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 32 | : error(EGL_SUCCESS), ctx(nullptr), logCallWithNoContext(true) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 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 | { | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 58 | struct TlsKeyInitializer { | 
| Jesse Hall | 6b0aee3 | 2018-06-15 16:44:27 -0700 | [diff] [blame] | 59 | static void create() { pthread_key_create(&sKey, destructTLSData); } | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 60 | }; | 
|  | 61 | pthread_once(&sOnceKey, TlsKeyInitializer::create); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Jesse Hall | 6b0aee3 | 2018-06-15 16:44:27 -0700 | [diff] [blame] | 64 | void egl_tls_t::destructTLSData(void* data) { | 
|  | 65 | egl_tls_t* tls = static_cast<egl_tls_t*>(data); | 
|  | 66 | if (!tls) return; | 
|  | 67 |  | 
|  | 68 | // Several things in the call tree of eglReleaseThread expect to be able to get the current | 
|  | 69 | // thread state directly from TLS. That's a problem because Bionic has already cleared our | 
|  | 70 | // TLS pointer before calling this function (pthread_getspecific(sKey) will return nullptr). | 
|  | 71 | // Instead the data is passed as our parameter. | 
|  | 72 | // | 
|  | 73 | // Ideally we'd refactor this so we have thin wrappers that retrieve thread state from TLS and | 
|  | 74 | // then pass it as a parameter (or 'this' pointer) to functions that do the real work without | 
|  | 75 | // touching TLS. Then from here we could just call those implementation functions with the the | 
|  | 76 | // TLS data we just received as a parameter. | 
|  | 77 | // | 
|  | 78 | // But that's a fairly invasive refactoring, so to do this robustly in the short term we just | 
|  | 79 | // put the data *back* in TLS and call the top-level eglReleaseThread. It and it's call tree | 
|  | 80 | // will retrieve the value from TLS, and then finally clear the TLS data. Bionic explicitly | 
|  | 81 | // tolerates re-setting the value that it's currently trying to destruct (see | 
|  | 82 | // pthread_key_clean_all()). Even if we forgot to clear the restored TLS data, bionic would | 
|  | 83 | // call the destructor again, but eventually gives up and just leaks the data rather than | 
|  | 84 | // enter an infinite loop. | 
|  | 85 | pthread_setspecific(sKey, tls); | 
|  | 86 | eglReleaseThread(); | 
|  | 87 | ALOGE_IF(pthread_getspecific(sKey) != nullptr, | 
|  | 88 | "EGL TLS data still exists after eglReleaseThread"); | 
|  | 89 | } | 
|  | 90 |  | 
| Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 91 | void egl_tls_t::setErrorEtcImpl( | 
|  | 92 | const char* caller, int line, EGLint error, bool quiet) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 93 | validateTLSKey(); | 
|  | 94 | egl_tls_t* tls = getTLS(); | 
|  | 95 | if (tls->error != error) { | 
| Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 96 | if (!quiet) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 97 | ALOGE("%s:%d error %x (%s)", | 
| Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 98 | caller, line, error, egl_strerror(error)); | 
|  | 99 | char value[PROPERTY_VALUE_MAX]; | 
|  | 100 | property_get("debug.egl.callstack", value, "0"); | 
|  | 101 | if (atoi(value)) { | 
| Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 102 | CallStack::log(LOG_TAG); | 
| Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 103 | } | 
| Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 104 | } | 
| Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 105 | tls->error = error; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 106 | } | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | bool egl_tls_t::logNoContextCall() { | 
|  | 110 | egl_tls_t* tls = getTLS(); | 
| Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 111 | if (tls->logCallWithNoContext) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 112 | tls->logCallWithNoContext = false; | 
|  | 113 | return true; | 
|  | 114 | } | 
|  | 115 | return false; | 
| Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 116 |  | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | egl_tls_t* egl_tls_t::getTLS() { | 
|  | 120 | egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey); | 
| Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 121 | if (tls == nullptr) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 122 | tls = new egl_tls_t; | 
|  | 123 | pthread_setspecific(sKey, tls); | 
|  | 124 | } | 
|  | 125 | return tls; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | void egl_tls_t::clearTLS() { | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 129 | if (sKey != TLS_KEY_NOT_INITIALIZED) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 130 | egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey); | 
|  | 131 | if (tls) { | 
| Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 132 | pthread_setspecific(sKey, nullptr); | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 133 | delete tls; | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 134 | } | 
|  | 135 | } | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | void egl_tls_t::clearError() { | 
|  | 139 | // This must clear the error from all the underlying EGL implementations as | 
|  | 140 | // well as the EGL wrapper layer. | 
| Cody Northrop | 9a9a1f4 | 2018-10-15 18:32:41 -0600 | [diff] [blame] | 141 | android::eglGetErrorImpl(); | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
|  | 144 | EGLint egl_tls_t::getError() { | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 145 | if (sKey == TLS_KEY_NOT_INITIALIZED) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 146 | return EGL_SUCCESS; | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 147 | } | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 148 | egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey); | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 149 | if (!tls) { | 
|  | 150 | return EGL_SUCCESS; | 
|  | 151 | } | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 152 | EGLint error = tls->error; | 
|  | 153 | tls->error = EGL_SUCCESS; | 
|  | 154 | return error; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | void egl_tls_t::setContext(EGLContext ctx) { | 
|  | 158 | validateTLSKey(); | 
|  | 159 | getTLS()->ctx = ctx; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | EGLContext egl_tls_t::getContext() { | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 163 | if (sKey == TLS_KEY_NOT_INITIALIZED) { | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 164 | return EGL_NO_CONTEXT; | 
| Mathias Agopian | 4e620dd | 2013-05-30 16:07:36 -0700 | [diff] [blame] | 165 | } | 
| Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 166 | egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey); | 
|  | 167 | if (!tls) return EGL_NO_CONTEXT; | 
|  | 168 | return tls->ctx; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 |  | 
|  | 172 | } // namespace android |