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 "egl_tls.h"
18
19 #include <stdlib.h>
20
21 #include <android-base/properties.h>
22 #include <log/log.h>
23 #include "CallStack.h"
24 #include "egl_platform_entries.h"
25
26 namespace android {
27
28 pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
29 pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
30
egl_tls_t()31 egl_tls_t::egl_tls_t()
32 : error(EGL_SUCCESS), ctx(nullptr), logCallWithNoContext(true) {
33 }
34
egl_strerror(EGLint err)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
validateTLSKey()56 void egl_tls_t::validateTLSKey()
57 {
58 struct TlsKeyInitializer {
59 static void create() { pthread_key_create(&sKey, destructTLSData); }
60 };
61 pthread_once(&sOnceKey, TlsKeyInitializer::create);
62 }
63
destructTLSData(void * data)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
setErrorEtcImpl(const char * caller,int line,EGLint error,bool quiet)91 void egl_tls_t::setErrorEtcImpl(
92 const char* caller, int line, EGLint error, bool quiet) {
93 validateTLSKey();
94 egl_tls_t* tls = getTLS();
95 if (tls->error != error) {
96 if (!quiet) {
97 ALOGE("%s:%d error %x (%s)",
98 caller, line, error, egl_strerror(error));
99 if (base::GetBoolProperty("debug.egl.callstack", false)) {
100 CallStack::log(LOG_TAG);
101 }
102 }
103 tls->error = error;
104 }
105 }
106
logNoContextCall()107 bool egl_tls_t::logNoContextCall() {
108 egl_tls_t* tls = getTLS();
109 if (tls->logCallWithNoContext) {
110 tls->logCallWithNoContext = false;
111 return true;
112 }
113 return false;
114
115 }
116
getTLS()117 egl_tls_t* egl_tls_t::getTLS() {
118 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
119 if (tls == nullptr) {
120 tls = new egl_tls_t;
121 pthread_setspecific(sKey, tls);
122 }
123 return tls;
124 }
125
clearTLS()126 void egl_tls_t::clearTLS() {
127 if (sKey != TLS_KEY_NOT_INITIALIZED) {
128 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
129 if (tls) {
130 pthread_setspecific(sKey, nullptr);
131 delete tls;
132 }
133 }
134 }
135
clearError()136 void egl_tls_t::clearError() {
137 // This must clear the error from all the underlying EGL implementations as
138 // well as the EGL wrapper layer.
139 android::eglGetErrorImpl();
140 }
141
getError()142 EGLint egl_tls_t::getError() {
143 if (sKey == TLS_KEY_NOT_INITIALIZED) {
144 return EGL_SUCCESS;
145 }
146 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
147 if (!tls) {
148 return EGL_SUCCESS;
149 }
150 EGLint error = tls->error;
151 tls->error = EGL_SUCCESS;
152 return error;
153 }
154
setContext(EGLContext ctx)155 void egl_tls_t::setContext(EGLContext ctx) {
156 validateTLSKey();
157 getTLS()->ctx = ctx;
158 }
159
getContext()160 EGLContext egl_tls_t::getContext() {
161 if (sKey == TLS_KEY_NOT_INITIALIZED) {
162 return EGL_NO_CONTEXT;
163 }
164 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
165 if (!tls) return EGL_NO_CONTEXT;
166 return tls->ctx;
167 }
168
169
170 } // namespace android
171