1518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian/*
2518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Copyright 2011, The Android Open Source Project
3518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
4518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Licensed under the Apache License, Version 2.0 (the "License");
5518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** you may not use this file except in compliance with the License.
6518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** You may obtain a copy of the License at
7518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
8518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **     http://www.apache.org/licenses/LICENSE-2.0
9518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
10518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Unless required by applicable law or agreed to in writing, software
11518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** distributed under the License is distributed on an "AS IS" BASIS,
12518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** See the License for the specific language governing permissions and
14518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** limitations under the License.
15518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian */
16518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
17518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#ifndef ANDROID_EGL_TLS_H
18518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#define ANDROID_EGL_TLS_H
19518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
20518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/egl.h>
21518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
22311b479d7f50bc9e487cf9b4859843d0f4778382Mathias Agopian#include <pthread.h>
231cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian
241cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian// ----------------------------------------------------------------------------
25518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiannamespace android {
261cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian// ----------------------------------------------------------------------------
27518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
28518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianclass DbgContext;
29518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
30518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianclass egl_tls_t {
314e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian    enum { TLS_KEY_NOT_INITIALIZED = -1 };
32518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static pthread_key_t sKey;
334e620ddce344e946ced992f61a69c367ff92fe24Mathias Agopian    static pthread_once_t sOnceKey;
34518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
35518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLint      error;
36518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    EGLContext  ctx;
37311b479d7f50bc9e487cf9b4859843d0f4778382Mathias Agopian    bool        logCallWithNoContext;
38518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
39518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    egl_tls_t();
40518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static void validateTLSKey();
410e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    static void setErrorEtcImpl(
420e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian            const char* caller, int line, EGLint error, bool quiet);
43518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
44518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianpublic:
45518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static egl_tls_t* getTLS();
46518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static void clearTLS();
47518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static void clearError();
48518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static EGLint getError();
49518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static void setContext(EGLContext ctx);
50518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static EGLContext getContext();
51518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static bool logNoContextCall();
52518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static const char *egl_strerror(EGLint err);
53518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
54518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    template<typename T>
55518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    static T setErrorEtc(const char* caller,
560e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian            int line, EGLint error, T returnValue, bool quiet = false) {
570e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian        setErrorEtcImpl(caller, line, error, quiet);
58518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian        return returnValue;
59518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    }
60518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian};
61518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
620e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian#define setError(_e, _r)        \
630e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    egl_tls_t::setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
640e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian
650e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian#define setErrorQuiet(_e, _r)   \
660e8bbee5775d81c7bbc479b995496cac9238559fMathias Agopian    egl_tls_t::setErrorEtc(__FUNCTION__, __LINE__, _e, _r, true)
67518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
681cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian// ----------------------------------------------------------------------------
69518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}; // namespace android
701cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian// ----------------------------------------------------------------------------
71518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
72518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#endif // ANDROID_EGL_TLS_H
73