blob: dd1dcc275ff8b5ed883811ffaa3911591b3ac53c [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
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040019#include <android-base/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <log/log.h>
Yiwei Zhang8af03062020-08-12 21:28:15 -070021#include <stdlib.h>
22
Mathias Agopian5f1af042017-03-09 18:50:05 -080023#include "CallStack.h"
Cody Northrop9a9a1f42018-10-15 18:32:41 -060024#include "egl_platform_entries.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070025
Mathias Agopian518ec112011-05-13 16:21:08 -070026namespace android {
27
Mathias Agopian4e620dd2013-05-30 16:07:36 -070028pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
29pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
Mathias Agopian518ec112011-05-13 16:21:08 -070030
Yiwei Zhang8af03062020-08-12 21:28:15 -070031egl_tls_t::egl_tls_t() : error(EGL_SUCCESS), ctx(nullptr), logCallWithNoContext(true) {}
Mathias Agopian518ec112011-05-13 16:21:08 -070032
Yiwei Zhang8af03062020-08-12 21:28:15 -070033const char* egl_tls_t::egl_strerror(EGLint err) {
Mathias Agopian518ec112011-05-13 16:21:08 -070034 switch (err) {
Yiwei Zhang8af03062020-08-12 21:28:15 -070035 case EGL_SUCCESS:
36 return "EGL_SUCCESS";
37 case EGL_NOT_INITIALIZED:
38 return "EGL_NOT_INITIALIZED";
39 case EGL_BAD_ACCESS:
40 return "EGL_BAD_ACCESS";
41 case EGL_BAD_ALLOC:
42 return "EGL_BAD_ALLOC";
43 case EGL_BAD_ATTRIBUTE:
44 return "EGL_BAD_ATTRIBUTE";
45 case EGL_BAD_CONFIG:
46 return "EGL_BAD_CONFIG";
47 case EGL_BAD_CONTEXT:
48 return "EGL_BAD_CONTEXT";
49 case EGL_BAD_CURRENT_SURFACE:
50 return "EGL_BAD_CURRENT_SURFACE";
51 case EGL_BAD_DISPLAY:
52 return "EGL_BAD_DISPLAY";
53 case EGL_BAD_MATCH:
54 return "EGL_BAD_MATCH";
55 case EGL_BAD_NATIVE_PIXMAP:
56 return "EGL_BAD_NATIVE_PIXMAP";
57 case EGL_BAD_NATIVE_WINDOW:
58 return "EGL_BAD_NATIVE_WINDOW";
59 case EGL_BAD_PARAMETER:
60 return "EGL_BAD_PARAMETER";
61 case EGL_BAD_SURFACE:
62 return "EGL_BAD_SURFACE";
63 case EGL_CONTEXT_LOST:
64 return "EGL_CONTEXT_LOST";
65 default:
66 return "UNKNOWN";
Mathias Agopian518ec112011-05-13 16:21:08 -070067 }
68}
69
Yiwei Zhang8af03062020-08-12 21:28:15 -070070void egl_tls_t::validateTLSKey() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -070071 struct TlsKeyInitializer {
Jesse Hall6b0aee32018-06-15 16:44:27 -070072 static void create() { pthread_key_create(&sKey, destructTLSData); }
Mathias Agopian4e620dd2013-05-30 16:07:36 -070073 };
74 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070075}
76
Jesse Hall6b0aee32018-06-15 16:44:27 -070077void egl_tls_t::destructTLSData(void* data) {
78 egl_tls_t* tls = static_cast<egl_tls_t*>(data);
79 if (!tls) return;
80
81 // Several things in the call tree of eglReleaseThread expect to be able to get the current
82 // thread state directly from TLS. That's a problem because Bionic has already cleared our
83 // TLS pointer before calling this function (pthread_getspecific(sKey) will return nullptr).
84 // Instead the data is passed as our parameter.
85 //
86 // Ideally we'd refactor this so we have thin wrappers that retrieve thread state from TLS and
87 // then pass it as a parameter (or 'this' pointer) to functions that do the real work without
88 // touching TLS. Then from here we could just call those implementation functions with the the
89 // TLS data we just received as a parameter.
90 //
91 // But that's a fairly invasive refactoring, so to do this robustly in the short term we just
92 // put the data *back* in TLS and call the top-level eglReleaseThread. It and it's call tree
93 // will retrieve the value from TLS, and then finally clear the TLS data. Bionic explicitly
94 // tolerates re-setting the value that it's currently trying to destruct (see
95 // pthread_key_clean_all()). Even if we forgot to clear the restored TLS data, bionic would
96 // call the destructor again, but eventually gives up and just leaks the data rather than
97 // enter an infinite loop.
98 pthread_setspecific(sKey, tls);
99 eglReleaseThread();
100 ALOGE_IF(pthread_getspecific(sKey) != nullptr,
101 "EGL TLS data still exists after eglReleaseThread");
102}
103
Yiwei Zhang8af03062020-08-12 21:28:15 -0700104void egl_tls_t::setErrorEtcImpl(const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700105 validateTLSKey();
106 egl_tls_t* tls = getTLS();
107 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700108 if (!quiet) {
Yiwei Zhang8af03062020-08-12 21:28:15 -0700109 ALOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400110 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800111 CallStack::log(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700112 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700113 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700114 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -0700115 }
116}
117
118bool egl_tls_t::logNoContextCall() {
119 egl_tls_t* tls = getTLS();
Mathias Agopian311b4792017-02-28 15:00:49 -0800120 if (tls->logCallWithNoContext) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700121 tls->logCallWithNoContext = false;
122 return true;
123 }
124 return false;
125}
126
127egl_tls_t* egl_tls_t::getTLS() {
128 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Yi Kong48a6cd22018-07-18 10:07:09 -0700129 if (tls == nullptr) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700130 tls = new egl_tls_t;
131 pthread_setspecific(sKey, tls);
132 }
133 return tls;
134}
135
136void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700137 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700138 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
139 if (tls) {
Yi Kong48a6cd22018-07-18 10:07:09 -0700140 pthread_setspecific(sKey, nullptr);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700141 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700142 }
143 }
144}
145
146void egl_tls_t::clearError() {
147 // This must clear the error from all the underlying EGL implementations as
148 // well as the EGL wrapper layer.
Cody Northrop9a9a1f42018-10-15 18:32:41 -0600149 android::eglGetErrorImpl();
Mathias Agopian518ec112011-05-13 16:21:08 -0700150}
151
152EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700153 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700155 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700156 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700157 if (!tls) {
158 return EGL_SUCCESS;
159 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700160 EGLint error = tls->error;
161 tls->error = EGL_SUCCESS;
162 return error;
163}
164
165void egl_tls_t::setContext(EGLContext ctx) {
166 validateTLSKey();
167 getTLS()->ctx = ctx;
168}
169
170EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700171 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700172 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700173 }
Yiwei Zhang8af03062020-08-12 21:28:15 -0700174 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian518ec112011-05-13 16:21:08 -0700175 if (!tls) return EGL_NO_CONTEXT;
176 return tls->ctx;
177}
178
Mathias Agopian518ec112011-05-13 16:21:08 -0700179} // namespace android