summaryrefslogtreecommitdiffstats
path: root/opengl/libs/EGL/egl_tls.cpp
blob: 41cfae1b63a1ade1683cfc9f5035b1cf80e8eaa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 ** Copyright 2011, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

#include <stdlib.h>
#include <pthread.h>

#include <cutils/log.h>
#include <cutils/properties.h>

#include <utils/CallStack.h>

#include <EGL/egl.h>

#include "egl_tls.h"


namespace android {

pthread_key_t egl_tls_t::sKey = -1;
pthread_mutex_t egl_tls_t::sLockKey = PTHREAD_MUTEX_INITIALIZER;

egl_tls_t::egl_tls_t()
    : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) {
}

const char *egl_tls_t::egl_strerror(EGLint err) {
    switch (err) {
        case EGL_SUCCESS:               return "EGL_SUCCESS";
        case EGL_NOT_INITIALIZED:       return "EGL_NOT_INITIALIZED";
        case EGL_BAD_ACCESS:            return "EGL_BAD_ACCESS";
        case EGL_BAD_ALLOC:             return "EGL_BAD_ALLOC";
        case EGL_BAD_ATTRIBUTE:         return "EGL_BAD_ATTRIBUTE";
        case EGL_BAD_CONFIG:            return "EGL_BAD_CONFIG";
        case EGL_BAD_CONTEXT:           return "EGL_BAD_CONTEXT";
        case EGL_BAD_CURRENT_SURFACE:   return "EGL_BAD_CURRENT_SURFACE";
        case EGL_BAD_DISPLAY:           return "EGL_BAD_DISPLAY";
        case EGL_BAD_MATCH:             return "EGL_BAD_MATCH";
        case EGL_BAD_NATIVE_PIXMAP:     return "EGL_BAD_NATIVE_PIXMAP";
        case EGL_BAD_NATIVE_WINDOW:     return "EGL_BAD_NATIVE_WINDOW";
        case EGL_BAD_PARAMETER:         return "EGL_BAD_PARAMETER";
        case EGL_BAD_SURFACE:           return "EGL_BAD_SURFACE";
        case EGL_CONTEXT_LOST:          return "EGL_CONTEXT_LOST";
        default: return "UNKNOWN";
    }
}

void egl_tls_t::validateTLSKey()
{
    if (sKey == -1) {
        pthread_mutex_lock(&sLockKey);
        if (sKey == -1)
            pthread_key_create(&sKey, NULL);
        pthread_mutex_unlock(&sLockKey);
    }
}

void egl_tls_t::setErrorEtcImpl(
        const char* caller, int line, EGLint error, bool quiet) {
    validateTLSKey();
    egl_tls_t* tls = getTLS();
    if (tls->error != error) {
        if (!quiet) {
            ALOGE("%s:%d error %x (%s)",
                    caller, line, error, egl_strerror(error));
            char value[PROPERTY_VALUE_MAX];
            property_get("debug.egl.callstack", value, "0");
            if (atoi(value)) {
                CallStack stack;
                stack.update();
                stack.dump();
            }
        }
        tls->error = error;
    }
}

bool egl_tls_t::logNoContextCall() {
    egl_tls_t* tls = getTLS();
    if (tls->logCallWithNoContext == true) {
        tls->logCallWithNoContext = false;
        return true;
    }
    return false;
}

egl_tls_t* egl_tls_t::getTLS() {
    egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
    if (tls == 0) {
        tls = new egl_tls_t;
        pthread_setspecific(sKey, tls);
    }
    return tls;
}

void egl_tls_t::clearTLS() {
    if (sKey != -1) {
        egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
        if (tls) {
            delete tls;
            pthread_setspecific(sKey, 0);
        }
    }
}

void egl_tls_t::clearError() {
    // This must clear the error from all the underlying EGL implementations as
    // well as the EGL wrapper layer.
    eglGetError();
}

EGLint egl_tls_t::getError() {
    if (sKey == -1)
        return EGL_SUCCESS;
    egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
    if (!tls) return EGL_SUCCESS;
    EGLint error = tls->error;
    tls->error = EGL_SUCCESS;
    return error;
}

void egl_tls_t::setContext(EGLContext ctx) {
    validateTLSKey();
    getTLS()->ctx = ctx;
}

EGLContext egl_tls_t::getContext() {
    if (sKey == -1)
        return EGL_NO_CONTEXT;
    egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
    if (!tls) return EGL_NO_CONTEXT;
    return tls->ctx;
}


} // namespace android