aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/opengl/host/libs/Translator/EGL/ClientAPIExts.cpp
blob: 42d5764b372ca9ea4123b424be78769ab6b2bd2b (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
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (C) 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 "ClientAPIExts.h"
#include "EglGlobalInfo.h"
#include "GLcommon/GLutils.h"
#include "GLcommon/TranslatorIfaces.h"
#include "ThreadInfo.h"
#include <GLES/gl.h>
#include <GLES/glext.h>

namespace ClientAPIExts
{

//
// define function pointer type for each extention function
// typename has the form __egl_{funcname}_t
//
#define FUNC_TYPE(fname) __egl_ ## fname ## _t
#define API_ENTRY(fname,params,args) \
    typedef void (GL_APIENTRY *FUNC_TYPE(fname)) params;

#define API_ENTRY_RET(rtype,fname,params,args) \
    typedef rtype (GL_APIENTRY *FUNC_TYPE(fname)) params;

#include "ClientAPIExts.in"
#undef API_ENTRY
#undef API_ENTRY_RET

/////
// Define static table to store the function value for each
// client API. functions pointers will get initialized through
// ClientAPIExts::initClientFuncs function after each client API has been
// loaded.
/////
#define API_ENTRY(fname,params,args) \
    FUNC_TYPE(fname) fname;

#define API_ENTRY_RET(rtype,fname,params,args) \
    API_ENTRY(fname,params,args)

static struct _ext_table
{
#include "ClientAPIExts.in"
} s_client_extensions[MAX_GLES_VERSION-1];

#undef API_ENTRY
#undef API_ENTRY_RET

//
// This function initialized each entry in the s_client_extensions
// struct at the givven index using the givven client interface
//
void initClientFuncs(GLESiface *iface, int idx)
{
#define API_ENTRY(fname,params,args) \
    s_client_extensions[idx].fname = \
          (FUNC_TYPE(fname))iface->getProcAddress(#fname);

#define API_ENTRY_RET(rtype,fname,params,args) \
    API_ENTRY(fname,params,args)

    //
    // reset all func pointers to NULL
    //
    memset(&s_client_extensions[idx], 0, sizeof(struct _ext_table));

    //
    // And now query the GLES library for each proc address
    //
#include "ClientAPIExts.in"
#undef API_ENTRY
#undef API_ENTRY_RET
}

//
// Define implementation for each extension function which checks
// the current context version and calls to the correct client API
// function.
//
#define API_ENTRY(fname,params,args) \
    static void _egl_ ## fname params \
    { \
        ThreadInfo* thread  = getThreadInfo(); \
        if (!thread->eglContext.Ptr()) { \
            return; \
        } \
        int idx = (int)thread->eglContext->version() - 1; \
        if (!s_client_extensions[idx].fname) { \
            return; \
        } \
        (*s_client_extensions[idx].fname) args; \
    }

#define API_ENTRY_RET(rtype,fname,params,args) \
    static rtype _egl_ ## fname params \
    { \
        ThreadInfo* thread  = getThreadInfo(); \
        if (!thread->eglContext.Ptr()) { \
            return (rtype)0; \
        } \
        int idx = (int)thread->eglContext->version() - 1; \
        if (!s_client_extensions[idx].fname) { \
            return (rtype)0; \
        } \
        return (*s_client_extensions[idx].fname) args; \
    }

#include "ClientAPIExts.in"
#undef API_ENTRY
#undef API_ENTRY_RET

//
// Define a table to map function names to the local _egl_ version of
// the extension function, to be used in eglGetProcAddress.
//
#define API_ENTRY(fname,params,args) \
    { #fname, (__translatorMustCastToProperFunctionPointerType)_egl_ ## fname},
#define API_ENTRY_RET(rtype,fname,params,args) \
    API_ENTRY(fname,params,args)

static struct _client_ext_funcs {
    const char *fname;
    __translatorMustCastToProperFunctionPointerType proc;
} s_client_ext_funcs[] = {
#include "ClientAPIExts.in"
};
static const int numExtFuncs = sizeof(s_client_ext_funcs) / 
                               sizeof(s_client_ext_funcs[0]);

#undef API_ENTRY
#undef API_ENTRY_RET

//
// returns the __egl_ version of the givven extension function name.
//
__translatorMustCastToProperFunctionPointerType getProcAddress(const char *fname)
{
    for (int i=0; i<numExtFuncs; i++) {
        if (!strcmp(fname, s_client_ext_funcs[i].fname)) {
            return s_client_ext_funcs[i].proc;
        }
    }
    return NULL;
}

} // of namespace ClientAPIExts