blob: ca8684e3dd253302869e97638825240bf74df075 [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 Agopianecfe0912011-09-06 17:24:05 -070023#include <utils/CallStack.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070024
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()
Mathias Agopian311b4792017-02-28 15:00:49 -080032 : error(EGL_SUCCESS), ctx(0), 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 {
59 static void create() {
60 pthread_key_create(&sKey, (void (*)(void*))&eglReleaseThread);
61 }
62 };
63 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070064}
65
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070066void egl_tls_t::setErrorEtcImpl(
67 const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -070068 validateTLSKey();
69 egl_tls_t* tls = getTLS();
70 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070071 if (!quiet) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000072 ALOGE("%s:%d error %x (%s)",
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070073 caller, line, error, egl_strerror(error));
74 char value[PROPERTY_VALUE_MAX];
75 property_get("debug.egl.callstack", value, "0");
76 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070077 CallStack stack(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070078 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070079 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070080 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -070081 }
82}
83
84bool egl_tls_t::logNoContextCall() {
85 egl_tls_t* tls = getTLS();
Mathias Agopian311b4792017-02-28 15:00:49 -080086 if (tls->logCallWithNoContext) {
Mathias Agopian518ec112011-05-13 16:21:08 -070087 tls->logCallWithNoContext = false;
88 return true;
89 }
90 return false;
Mathias Agopian311b4792017-02-28 15:00:49 -080091
Mathias Agopian518ec112011-05-13 16:21:08 -070092}
93
94egl_tls_t* egl_tls_t::getTLS() {
95 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
96 if (tls == 0) {
97 tls = new egl_tls_t;
98 pthread_setspecific(sKey, tls);
99 }
100 return tls;
101}
102
103void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700104 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700105 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
106 if (tls) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700107 pthread_setspecific(sKey, 0);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700108 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700109 }
110 }
111}
112
113void egl_tls_t::clearError() {
114 // This must clear the error from all the underlying EGL implementations as
115 // well as the EGL wrapper layer.
116 eglGetError();
117}
118
119EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700120 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700121 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700122 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700123 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700124 if (!tls) {
125 return EGL_SUCCESS;
126 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700127 EGLint error = tls->error;
128 tls->error = EGL_SUCCESS;
129 return error;
130}
131
132void egl_tls_t::setContext(EGLContext ctx) {
133 validateTLSKey();
134 getTLS()->ctx = ctx;
135}
136
137EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700138 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700139 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700140 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700141 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
142 if (!tls) return EGL_NO_CONTEXT;
143 return tls->ctx;
144}
145
146
147} // namespace android