blob: 6de5f27f4f4825b82b5c621f9e20af9618cd74f3 [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
17#include <pthread.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070018#include <stdlib.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070019
Mathias Agopianecfe0912011-09-06 17:24:05 -070020#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070021#include <log/log.h>
Mathias Agopianecfe0912011-09-06 17:24:05 -070022#include <utils/CallStack.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070023
24#include <EGL/egl.h>
25
26#include "egl_tls.h"
27
Mathias Agopian518ec112011-05-13 16:21:08 -070028namespace android {
29
Mathias Agopian4e620dd2013-05-30 16:07:36 -070030pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
31pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
Mathias Agopian518ec112011-05-13 16:21:08 -070032
33egl_tls_t::egl_tls_t()
Siva Velusamy0469dd62011-11-30 15:05:37 -080034 : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -070035}
36
37const char *egl_tls_t::egl_strerror(EGLint err) {
38 switch (err) {
39 case EGL_SUCCESS: return "EGL_SUCCESS";
40 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
41 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
42 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
43 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
44 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
45 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
46 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
47 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
48 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
49 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
50 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
51 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
52 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
53 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
54 default: return "UNKNOWN";
55 }
56}
57
58void egl_tls_t::validateTLSKey()
59{
Mathias Agopian4e620dd2013-05-30 16:07:36 -070060 struct TlsKeyInitializer {
61 static void create() {
62 pthread_key_create(&sKey, (void (*)(void*))&eglReleaseThread);
63 }
64 };
65 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070066}
67
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070068void egl_tls_t::setErrorEtcImpl(
69 const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -070070 validateTLSKey();
71 egl_tls_t* tls = getTLS();
72 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070073 if (!quiet) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000074 ALOGE("%s:%d error %x (%s)",
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070075 caller, line, error, egl_strerror(error));
76 char value[PROPERTY_VALUE_MAX];
77 property_get("debug.egl.callstack", value, "0");
78 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070079 CallStack stack(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070080 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070081 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070082 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -070083 }
84}
85
86bool egl_tls_t::logNoContextCall() {
87 egl_tls_t* tls = getTLS();
88 if (tls->logCallWithNoContext == true) {
89 tls->logCallWithNoContext = false;
90 return true;
91 }
92 return false;
93}
94
95egl_tls_t* egl_tls_t::getTLS() {
96 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
97 if (tls == 0) {
98 tls = new egl_tls_t;
99 pthread_setspecific(sKey, tls);
100 }
101 return tls;
102}
103
104void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700105 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700106 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
107 if (tls) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700108 pthread_setspecific(sKey, 0);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700109 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700110 }
111 }
112}
113
114void egl_tls_t::clearError() {
115 // This must clear the error from all the underlying EGL implementations as
116 // well as the EGL wrapper layer.
117 eglGetError();
118}
119
120EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700121 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700122 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700123 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700124 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700125 if (!tls) {
126 return EGL_SUCCESS;
127 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700128 EGLint error = tls->error;
129 tls->error = EGL_SUCCESS;
130 return error;
131}
132
133void egl_tls_t::setContext(EGLContext ctx) {
134 validateTLSKey();
135 getTLS()->ctx = ctx;
136}
137
138EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700139 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700140 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700141 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700142 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
143 if (!tls) return EGL_NO_CONTEXT;
144 return tls->ctx;
145}
146
147
148} // namespace android