The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame^] | 1 | /* |
| 2 | ** Copyright 2007, 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 | #ifndef ANDROID_GLES_CM_HOOKS_H |
| 18 | #define ANDROID_GLES_CM_HOOKS_H |
| 19 | |
| 20 | #include <ctype.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #include <EGL/egl.h> |
| 25 | #include <GLES/gl.h> |
| 26 | |
| 27 | #define GL_LOGGER 0 |
| 28 | #if !defined(__arm__) |
| 29 | #define USE_SLOW_BINDING 1 |
| 30 | #else |
| 31 | #define USE_SLOW_BINDING 0 |
| 32 | #endif |
| 33 | #undef NELEM |
| 34 | #define NELEM(x) (sizeof(x)/sizeof(*(x))) |
| 35 | #define MAX_NUMBER_OF_GL_EXTENSIONS 32 |
| 36 | |
| 37 | |
| 38 | #if defined(HAVE_ANDROID_OS) && !USE_SLOW_BINDING && !GL_LOGGER && __OPTIMIZE__ |
| 39 | #define USE_FAST_TLS_KEY 1 |
| 40 | #else |
| 41 | #define USE_FAST_TLS_KEY 0 |
| 42 | #endif |
| 43 | |
| 44 | #if USE_FAST_TLS_KEY |
| 45 | # include <bionic_tls.h> /* special private C library header */ |
| 46 | #endif |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | namespace android { |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | |
| 52 | // EGLDisplay are global, not attached to a given thread |
| 53 | const unsigned int NUM_DISPLAYS = 1; |
| 54 | |
| 55 | enum { |
| 56 | IMPL_HARDWARE = 0, |
| 57 | IMPL_SOFTWARE, |
| 58 | IMPL_CONTEXT_LOST, |
| 59 | IMPL_NO_CONTEXT, |
| 60 | |
| 61 | IMPL_NUM_IMPLEMENTATIONS |
| 62 | }; |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | // GL / EGL hooks |
| 67 | |
| 68 | #undef GL_ENTRY |
| 69 | #undef EGL_ENTRY |
| 70 | #define GL_ENTRY(_r, _api, ...) _r (*_api)(__VA_ARGS__); |
| 71 | #define EGL_ENTRY(_r, _api, ...) _r (*_api)(__VA_ARGS__); |
| 72 | |
| 73 | struct gl_hooks_t { |
| 74 | struct gl_t { |
| 75 | #include "gl_entries.in" |
| 76 | } gl; |
| 77 | struct egl_t { |
| 78 | #include "egl_entries.in" |
| 79 | } egl; |
| 80 | struct gl_ext_t { |
| 81 | void (*extensions[MAX_NUMBER_OF_GL_EXTENSIONS])(void); |
| 82 | } ext; |
| 83 | }; |
| 84 | #undef GL_ENTRY |
| 85 | #undef EGL_ENTRY |
| 86 | |
| 87 | |
| 88 | // ---------------------------------------------------------------------------- |
| 89 | |
| 90 | extern gl_hooks_t gHooks[IMPL_NUM_IMPLEMENTATIONS]; |
| 91 | extern pthread_key_t gGLWrapperKey; |
| 92 | |
| 93 | #if USE_FAST_TLS_KEY |
| 94 | |
| 95 | // We have a dedicated TLS slot in bionic |
| 96 | static inline gl_hooks_t const * volatile * get_tls_hooks() { |
| 97 | volatile void *tls_base = __get_tls(); |
| 98 | gl_hooks_t const * volatile * tls_hooks = |
| 99 | reinterpret_cast<gl_hooks_t const * volatile *>(tls_base); |
| 100 | return tls_hooks; |
| 101 | } |
| 102 | |
| 103 | static inline void setGlThreadSpecific(gl_hooks_t const *value) { |
| 104 | gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); |
| 105 | tls_hooks[TLS_SLOT_OPENGL_API] = value; |
| 106 | } |
| 107 | |
| 108 | static gl_hooks_t const* getGlThreadSpecific() { |
| 109 | gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); |
| 110 | gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API]; |
| 111 | if (hooks) return hooks; |
| 112 | return &gHooks[IMPL_NO_CONTEXT]; |
| 113 | } |
| 114 | |
| 115 | #else |
| 116 | |
| 117 | static inline void setGlThreadSpecific(gl_hooks_t const *value) { |
| 118 | pthread_setspecific(gGLWrapperKey, value); |
| 119 | } |
| 120 | |
| 121 | static gl_hooks_t const* getGlThreadSpecific() { |
| 122 | gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey)); |
| 123 | if (hooks) return hooks; |
| 124 | return &gHooks[IMPL_NO_CONTEXT]; |
| 125 | } |
| 126 | |
| 127 | #endif |
| 128 | |
| 129 | |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | }; // namespace android |
| 132 | // ---------------------------------------------------------------------------- |
| 133 | |
| 134 | #endif /* ANDROID_GLES_CM_HOOKS_H */ |