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