blob: 8d118e07edbc2bae4e522c1a13b8c9e273910b50 [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
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040021#include <android-base/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"
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
31egl_tls_t::egl_tls_t()
Yi Kong48a6cd22018-07-18 10:07:09 -070032 : error(EGL_SUCCESS), ctx(nullptr), logCallWithNoContext(true) {
Mathias Agopian518ec112011-05-13 16:21:08 -070033}
34
35const 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
56void egl_tls_t::validateTLSKey()
57{
Mathias Agopian4e620dd2013-05-30 16:07:36 -070058 struct TlsKeyInitializer {
Jesse Hall6b0aee32018-06-15 16:44:27 -070059 static void create() { pthread_key_create(&sKey, destructTLSData); }
Mathias Agopian4e620dd2013-05-30 16:07:36 -070060 };
61 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070062}
63
Jesse Hall6b0aee32018-06-15 16:44:27 -070064void 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 Agopian0e8bbee2011-10-05 19:15:05 -070091void egl_tls_t::setErrorEtcImpl(
92 const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -070093 validateTLSKey();
94 egl_tls_t* tls = getTLS();
95 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070096 if (!quiet) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000097 ALOGE("%s:%d error %x (%s)",
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070098 caller, line, error, egl_strerror(error));
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040099 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800100 CallStack::log(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700101 }
Mathias Agopianecfe0912011-09-06 17:24:05 -0700102 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -0700103 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -0700104 }
105}
106
107bool egl_tls_t::logNoContextCall() {
108 egl_tls_t* tls = getTLS();
Mathias Agopian311b4792017-02-28 15:00:49 -0800109 if (tls->logCallWithNoContext) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700110 tls->logCallWithNoContext = false;
111 return true;
112 }
113 return false;
Mathias Agopian311b4792017-02-28 15:00:49 -0800114
Mathias Agopian518ec112011-05-13 16:21:08 -0700115}
116
117egl_tls_t* egl_tls_t::getTLS() {
118 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Yi Kong48a6cd22018-07-18 10:07:09 -0700119 if (tls == nullptr) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700120 tls = new egl_tls_t;
121 pthread_setspecific(sKey, tls);
122 }
123 return tls;
124}
125
126void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700127 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700128 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
129 if (tls) {
Yi Kong48a6cd22018-07-18 10:07:09 -0700130 pthread_setspecific(sKey, nullptr);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700131 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700132 }
133 }
134}
135
136void egl_tls_t::clearError() {
137 // This must clear the error from all the underlying EGL implementations as
138 // well as the EGL wrapper layer.
Cody Northrop9a9a1f42018-10-15 18:32:41 -0600139 android::eglGetErrorImpl();
Mathias Agopian518ec112011-05-13 16:21:08 -0700140}
141
142EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700143 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700144 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700145 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700147 if (!tls) {
148 return EGL_SUCCESS;
149 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700150 EGLint error = tls->error;
151 tls->error = EGL_SUCCESS;
152 return error;
153}
154
155void egl_tls_t::setContext(EGLContext ctx) {
156 validateTLSKey();
157 getTLS()->ctx = ctx;
158}
159
160EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700161 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700162 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700163 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700164 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