blob: f3c8d2c78337cf14896ea8809c16ab2b3e68b060 [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 Agopianecfe0912011-09-06 17:24:05 -070017#include <stdlib.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070018#include <pthread.h>
19
20#include <cutils/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
29
30namespace android {
31
32pthread_key_t egl_tls_t::sKey = -1;
33pthread_mutex_t egl_tls_t::sLockKey = PTHREAD_MUTEX_INITIALIZER;
34
35egl_tls_t::egl_tls_t()
36 : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE), dbg(0) {
37}
38
39const char *egl_tls_t::egl_strerror(EGLint err) {
40 switch (err) {
41 case EGL_SUCCESS: return "EGL_SUCCESS";
42 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
43 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
44 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
45 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
46 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
47 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
48 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
49 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
50 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
51 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
52 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
53 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
54 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
55 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
56 default: return "UNKNOWN";
57 }
58}
59
60void egl_tls_t::validateTLSKey()
61{
62 if (sKey == -1) {
63 pthread_mutex_lock(&sLockKey);
64 if (sKey == -1)
65 pthread_key_create(&sKey, NULL);
66 pthread_mutex_unlock(&sLockKey);
67 }
68}
69
70void egl_tls_t::setErrorEtcImpl(const char* caller, int line, EGLint error) {
71 validateTLSKey();
72 egl_tls_t* tls = getTLS();
73 if (tls->error != error) {
74 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
75 tls->error = error;
Mathias Agopianecfe0912011-09-06 17:24:05 -070076 char value[PROPERTY_VALUE_MAX];
77 property_get("debug.egl.callstack", value, "0");
78 if (atoi(value)) {
79 CallStack stack;
80 stack.update();
81 stack.dump();
82 }
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() {
105 if (sKey != -1) {
106 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
107 if (tls) {
108 delete tls;
109 pthread_setspecific(sKey, 0);
110 }
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() {
121 if (sKey == -1)
122 return EGL_SUCCESS;
123 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
124 if (!tls) return EGL_SUCCESS;
125 EGLint error = tls->error;
126 tls->error = EGL_SUCCESS;
127 return error;
128}
129
130void egl_tls_t::setContext(EGLContext ctx) {
131 validateTLSKey();
132 getTLS()->ctx = ctx;
133}
134
135EGLContext egl_tls_t::getContext() {
136 if (sKey == -1)
137 return EGL_NO_CONTEXT;
138 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
139 if (!tls) return EGL_NO_CONTEXT;
140 return tls->ctx;
141}
142
143
144} // namespace android