blob: 125700405f7e2825db1a2a35929e5e520427c35b [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
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <android/log.h>
Mathias Agopianecfe0912011-09-06 17:24:05 -070021#include <cutils/properties.h>
22
23#include <utils/CallStack.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070024
25#include <EGL/egl.h>
26
27#include "egl_tls.h"
28
Mathias Agopian518ec112011-05-13 16:21:08 -070029namespace android {
30
Mathias Agopian4e620dd2013-05-30 16:07:36 -070031pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
32pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
Mathias Agopian518ec112011-05-13 16:21:08 -070033
34egl_tls_t::egl_tls_t()
Siva Velusamy0469dd62011-11-30 15:05:37 -080035 : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -070036}
37
38const char *egl_tls_t::egl_strerror(EGLint err) {
39 switch (err) {
40 case EGL_SUCCESS: return "EGL_SUCCESS";
41 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
42 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
43 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
44 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
45 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
46 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
47 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
48 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
49 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
50 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
51 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
52 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
53 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
54 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
55 default: return "UNKNOWN";
56 }
57}
58
59void egl_tls_t::validateTLSKey()
60{
Mathias Agopian4e620dd2013-05-30 16:07:36 -070061 struct TlsKeyInitializer {
62 static void create() {
63 pthread_key_create(&sKey, (void (*)(void*))&eglReleaseThread);
64 }
65 };
66 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070067}
68
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070069void egl_tls_t::setErrorEtcImpl(
70 const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -070071 validateTLSKey();
72 egl_tls_t* tls = getTLS();
73 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070074 if (!quiet) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000075 ALOGE("%s:%d error %x (%s)",
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070076 caller, line, error, egl_strerror(error));
77 char value[PROPERTY_VALUE_MAX];
78 property_get("debug.egl.callstack", value, "0");
79 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070080 CallStack stack(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070081 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070082 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070083 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -070084 }
85}
86
87bool egl_tls_t::logNoContextCall() {
88 egl_tls_t* tls = getTLS();
89 if (tls->logCallWithNoContext == true) {
90 tls->logCallWithNoContext = false;
91 return true;
92 }
93 return false;
94}
95
96egl_tls_t* egl_tls_t::getTLS() {
97 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
98 if (tls == 0) {
99 tls = new egl_tls_t;
100 pthread_setspecific(sKey, tls);
101 }
102 return tls;
103}
104
105void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700106 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700107 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
108 if (tls) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700109 pthread_setspecific(sKey, 0);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700110 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700111 }
112 }
113}
114
115void egl_tls_t::clearError() {
116 // This must clear the error from all the underlying EGL implementations as
117 // well as the EGL wrapper layer.
118 eglGetError();
119}
120
121EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700122 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700123 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700124 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700125 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700126 if (!tls) {
127 return EGL_SUCCESS;
128 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700129 EGLint error = tls->error;
130 tls->error = EGL_SUCCESS;
131 return error;
132}
133
134void egl_tls_t::setContext(EGLContext ctx) {
135 validateTLSKey();
136 getTLS()->ctx = ctx;
137}
138
139EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700140 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700141 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700142 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700143 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
144 if (!tls) return EGL_NO_CONTEXT;
145 return tls->ctx;
146}
147
148
149} // namespace android