hooks.h revision 923c661a86c9e0737b3f16ceffd77e71e023ca54
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 <pthread.h> 25 26#include <EGL/egl.h> 27#include <EGL/eglext.h> 28#include <GLES/gl.h> 29#include <GLES/glext.h> 30#include <GLES2/gl2.h> 31#include <GLES2/gl2ext.h> 32 33#if !defined(__arm__) 34#define USE_SLOW_BINDING 1 35#else 36#define USE_SLOW_BINDING 0 37#endif 38#undef NELEM 39#define NELEM(x) (sizeof(x)/sizeof(*(x))) 40#define MAX_NUMBER_OF_GL_EXTENSIONS 32 41 42 43#if defined(HAVE_ANDROID_OS) && !USE_SLOW_BINDING && __OPTIMIZE__ 44#define USE_FAST_TLS_KEY 1 45#else 46#define USE_FAST_TLS_KEY 0 47#endif 48 49#if USE_FAST_TLS_KEY 50# include <bionic_tls.h> /* special private C library header */ 51#endif 52 53// ---------------------------------------------------------------------------- 54namespace android { 55// ---------------------------------------------------------------------------- 56 57// EGLDisplay are global, not attached to a given thread 58const unsigned int NUM_DISPLAYS = 1; 59 60enum { 61 IMPL_HARDWARE = 0, 62 IMPL_SOFTWARE, 63 IMPL_NUM_DRIVERS_IMPLEMENTATIONS, 64 IMPL_NO_CONTEXT = IMPL_NUM_DRIVERS_IMPLEMENTATIONS, 65 IMPL_NUM_IMPLEMENTATIONS 66}; 67 68// ---------------------------------------------------------------------------- 69 70// GL / EGL hooks 71 72#undef GL_ENTRY 73#undef EGL_ENTRY 74#define GL_ENTRY(_r, _api, ...) _r (*_api)(__VA_ARGS__); 75#define EGL_ENTRY(_r, _api, ...) _r (*_api)(__VA_ARGS__); 76 77struct gl_hooks_t { 78 struct gl_t { 79 #include "GLES_CM/gl_entries.in" 80 #include "GLES_CM/glext_entries.in" 81 } gl; 82 struct gl2_t { 83 #include "GLES2/gl2_entries.in" 84 #include "GLES2/gl2ext_entries.in" 85 } gl2; 86 struct egl_t { 87 #include "EGL/egl_entries.in" 88 } egl; 89 struct gl_ext_t { 90 void (*extensions[MAX_NUMBER_OF_GL_EXTENSIONS])(void); 91 } ext; 92}; 93#undef GL_ENTRY 94#undef EGL_ENTRY 95 96 97// ---------------------------------------------------------------------------- 98 99extern gl_hooks_t gHooks[IMPL_NUM_IMPLEMENTATIONS]; 100extern pthread_key_t gGLWrapperKey; 101extern "C" void gl_unimplemented(); 102 103extern char const * const gl_names[]; 104extern char const * const gl2_names[]; 105extern char const * const egl_names[]; 106 107// ---------------------------------------------------------------------------- 108 109#if USE_FAST_TLS_KEY 110 111// We have a dedicated TLS slot in bionic 112static inline gl_hooks_t const * volatile * get_tls_hooks() { 113 volatile void *tls_base = __get_tls(); 114 gl_hooks_t const * volatile * tls_hooks = 115 reinterpret_cast<gl_hooks_t const * volatile *>(tls_base); 116 return tls_hooks; 117} 118 119static inline void setGlThreadSpecific(gl_hooks_t const *value) { 120 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); 121 tls_hooks[TLS_SLOT_OPENGL_API] = value; 122} 123 124static gl_hooks_t const* getGlThreadSpecific() { 125 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); 126 gl_hooks_t const* hooks = tls_hooks[TLS_SLOT_OPENGL_API]; 127 if (hooks) return hooks; 128 return &gHooks[IMPL_NO_CONTEXT]; 129} 130 131#else 132 133static inline void setGlThreadSpecific(gl_hooks_t const *value) { 134 pthread_setspecific(gGLWrapperKey, value); 135} 136 137static gl_hooks_t const* getGlThreadSpecific() { 138 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey)); 139 if (hooks) return hooks; 140 return &gHooks[IMPL_NO_CONTEXT]; 141} 142 143#endif 144 145 146// ---------------------------------------------------------------------------- 147}; // namespace android 148// ---------------------------------------------------------------------------- 149 150#endif /* ANDROID_GLES_CM_HOOKS_H */ 151