blob: 297a8a322722cd1838c4f54116a4052d8fd3ec73 [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
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 Agopian311b4792017-02-28 15:00:49 -080017#include "egl_tls.h"
18
Mark Salyzyna5e161b2016-09-29 08:08:05 -070019#include <stdlib.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070020
Mathias Agopianecfe0912011-09-06 17:24:05 -070021#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Mathias Agopian5f1af042017-03-09 18:50:05 -080023#include "CallStack.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070024
Mathias Agopian518ec112011-05-13 16:21:08 -070025namespace android {
26
Mathias Agopian4e620dd2013-05-30 16:07:36 -070027pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
28pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30egl_tls_t::egl_tls_t()
Mathias Agopian311b4792017-02-28 15:00:49 -080031 : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(true) {
Mathias Agopian518ec112011-05-13 16:21:08 -070032}
33
34const char *egl_tls_t::egl_strerror(EGLint err) {
35 switch (err) {
36 case EGL_SUCCESS: return "EGL_SUCCESS";
37 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
38 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
39 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
40 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
41 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
42 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
43 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
44 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
45 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
46 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
47 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
48 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
49 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
50 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
51 default: return "UNKNOWN";
52 }
53}
54
55void egl_tls_t::validateTLSKey()
56{
Mathias Agopian4e620dd2013-05-30 16:07:36 -070057 struct TlsKeyInitializer {
Jesse Hall6b0aee32018-06-15 16:44:27 -070058 static void create() { pthread_key_create(&sKey, destructTLSData); }
Mathias Agopian4e620dd2013-05-30 16:07:36 -070059 };
60 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070061}
62
Jesse Hall6b0aee32018-06-15 16:44:27 -070063void egl_tls_t::destructTLSData(void* data) {
64 egl_tls_t* tls = static_cast<egl_tls_t*>(data);
65 if (!tls) return;
66
67 // Several things in the call tree of eglReleaseThread expect to be able to get the current
68 // thread state directly from TLS. That's a problem because Bionic has already cleared our
69 // TLS pointer before calling this function (pthread_getspecific(sKey) will return nullptr).
70 // Instead the data is passed as our parameter.
71 //
72 // Ideally we'd refactor this so we have thin wrappers that retrieve thread state from TLS and
73 // then pass it as a parameter (or 'this' pointer) to functions that do the real work without
74 // touching TLS. Then from here we could just call those implementation functions with the the
75 // TLS data we just received as a parameter.
76 //
77 // But that's a fairly invasive refactoring, so to do this robustly in the short term we just
78 // put the data *back* in TLS and call the top-level eglReleaseThread. It and it's call tree
79 // will retrieve the value from TLS, and then finally clear the TLS data. Bionic explicitly
80 // tolerates re-setting the value that it's currently trying to destruct (see
81 // pthread_key_clean_all()). Even if we forgot to clear the restored TLS data, bionic would
82 // call the destructor again, but eventually gives up and just leaks the data rather than
83 // enter an infinite loop.
84 pthread_setspecific(sKey, tls);
85 eglReleaseThread();
86 ALOGE_IF(pthread_getspecific(sKey) != nullptr,
87 "EGL TLS data still exists after eglReleaseThread");
88}
89
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070090void egl_tls_t::setErrorEtcImpl(
91 const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -070092 validateTLSKey();
93 egl_tls_t* tls = getTLS();
94 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070095 if (!quiet) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000096 ALOGE("%s:%d error %x (%s)",
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070097 caller, line, error, egl_strerror(error));
98 char value[PROPERTY_VALUE_MAX];
99 property_get("debug.egl.callstack", value, "0");
100 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800101 CallStack::log(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700102 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700103 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700104 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -0700105 }
106}
107
108bool egl_tls_t::logNoContextCall() {
109 egl_tls_t* tls = getTLS();
Mathias Agopian311b4792017-02-28 15:00:49 -0800110 if (tls->logCallWithNoContext) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700111 tls->logCallWithNoContext = false;
112 return true;
113 }
114 return false;
Mathias Agopian311b4792017-02-28 15:00:49 -0800115
Mathias Agopian518ec112011-05-13 16:21:08 -0700116}
117
118egl_tls_t* egl_tls_t::getTLS() {
119 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
120 if (tls == 0) {
121 tls = new egl_tls_t;
122 pthread_setspecific(sKey, tls);
123 }
124 return tls;
125}
126
127void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700128 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700129 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
130 if (tls) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700131 pthread_setspecific(sKey, 0);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700132 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700133 }
134 }
135}
136
137void egl_tls_t::clearError() {
138 // This must clear the error from all the underlying EGL implementations as
139 // well as the EGL wrapper layer.
140 eglGetError();
141}
142
143EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700144 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700145 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700146 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700148 if (!tls) {
149 return EGL_SUCCESS;
150 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700151 EGLint error = tls->error;
152 tls->error = EGL_SUCCESS;
153 return error;
154}
155
156void egl_tls_t::setContext(EGLContext ctx) {
157 validateTLSKey();
158 getTLS()->ctx = ctx;
159}
160
161EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700162 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700163 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700164 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700165 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
166 if (!tls) return EGL_NO_CONTEXT;
167 return tls->ctx;
168}
169
170
171} // namespace android