tizen 2.4 release
[framework/graphics/coregl.git] / src / coregl.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <dlfcn.h>
4 #include <string.h>
5
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include "coregl_internal.h"
10 #include "coregl_export.h"
11
12 void               *egl_lib_handle;
13 void               *gl_lib_handle;
14 int                 driver_gl_version=COREGL_GLAPI_2;
15 static int          api_gl_version=COREGL_GLAPI_2;
16
17 #ifndef _COREGL_VENDOR_EGL_LIB_PATH
18 #define _COREGL_VENDOR_EGL_LIB_PATH "/usr/lib/driver/libEGL.so" /* DEFAULT EGL PATH */
19 #endif
20
21 #ifndef _COREGL_VENDOR_GL_LIB_PATH
22 #define _COREGL_VENDOR_GL_LIB_PATH "/usr/lib/driver/libGLESv2.so" /* DEFAULT GL PATH */
23 #endif
24
25 // Symbol definition for real
26 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST)     RET_TYPE (*_sym_##FUNC_NAME) PARAM_LIST;
27 #include "headers/sym.h"
28 #undef _COREGL_SYMBOL
29
30 const char *
31 get_env_setting(const char *name)
32 {
33         char *fp_env = NULL;
34         static char *fp_default = "\0";
35         fp_env = secure_getenv(name);
36         if (fp_env == NULL) fp_env = fp_default;
37         return fp_env;
38 }
39
40 void
41 cleanup_current_thread_state()
42 {
43         GLThreadState *tstate = NULL;
44
45         tstate = get_current_thread_state();
46
47         if (tstate != NULL)
48         {
49                 COREGL_LOG("[COREGL] de-init thread state \n");
50                 deinit_modules_tstate(tstate);
51                 remove_from_general_trace_list(&thread_trace_list, tstate);
52                 free(tstate);
53                 tstate = NULL;
54         }
55
56         set_current_thread_state(NULL);
57 }
58
59 int
60 init_new_thread_state()
61 {
62         int ret = 0;
63         GLThreadState *tstate = NULL;
64
65         tstate = get_current_thread_state();
66         AST(tstate == NULL);
67
68         tstate = (GLThreadState *)calloc(1, sizeof(GLThreadState));
69         /* Prevent CID : 395855 */
70         if (tstate == NULL) goto finish;
71         tstate->thread_id = get_current_thread();
72         add_to_general_trace_list(&thread_trace_list, tstate);
73
74         init_modules_tstate(tstate);
75         set_current_thread_state(tstate);
76
77         ret = 1;
78         goto finish;
79
80 finish:
81         return ret;
82 }
83
84 static void
85 _sym_missing()
86 {
87         COREGL_ERR("GL symbol missing! Check client version!\n");
88 }
89
90 #define FINDSYM(libhandle, getproc, dst, sym) \
91    if(api_gl_version <= driver_gl_version) { \
92       if (!dst || (void *)dst == (void *)_sym_missing) \
93                   if (getproc) dst = (__typeof__(dst))getproc(sym); \
94       if (!dst || (void *)dst == (void *)_sym_missing) \
95                   dst = (__typeof__(dst))dlsym(libhandle, sym); \
96           if (!dst) dst = (__typeof__(dst))_sym_missing;\
97    }
98
99 static int
100 _glue_sym_init(void)
101 {
102
103 #define _COREGL_START_API(version)              api_gl_version = version;
104 #define _COREGL_END_API(version)                api_gl_version = COREGL_GLAPI_2;
105 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST) \
106     FINDSYM(egl_lib_handle, _sym_eglGetProcAddress, _sym_##FUNC_NAME, #FUNC_NAME);
107 #define _COREGL_EXT_SYMBOL_ALIAS(FUNC_NAME, ALIAS_NAME) \
108     FINDSYM(egl_lib_handle, _sym_eglGetProcAddress, _sym_##ALIAS_NAME, #FUNC_NAME);
109
110 #include "headers/sym_egl.h"
111
112 #undef _COREGL_EXT_SYMBOL_ALIAS
113 #undef _COREGL_SYMBOL
114 #undef _COREGL_START_API
115 #undef _COREGL_END_API
116
117         return 1;
118 }
119
120 static int
121 _gl_sym_init(void)
122 {
123
124 #define _COREGL_START_API(version)              api_gl_version = version;
125 #define _COREGL_END_API(version)                api_gl_version = COREGL_GLAPI_2;
126 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST) \
127     FINDSYM(gl_lib_handle, _sym_eglGetProcAddress, _sym_##FUNC_NAME, #FUNC_NAME);
128 #define _COREGL_EXT_SYMBOL_ALIAS(FUNC_NAME, ALIAS_NAME) \
129     FINDSYM(gl_lib_handle, _sym_eglGetProcAddress, _sym_##ALIAS_NAME, #FUNC_NAME);
130
131 #include "headers/sym_gl.h"
132
133 #undef _COREGL_EXT_SYMBOL_ALIAS
134 #undef _COREGL_SYMBOL
135 #undef _COREGL_START_API
136 #undef _COREGL_END_API
137
138         return 1;
139 }
140
141 #undef FINDSYM
142
143
144 COREGL_API void coregl_symbol_exported()
145 {
146         COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL)\E[0m\n");
147 }
148
149 static int
150 _gl_lib_init(void)
151 {
152         //------------------------------------------------//
153         // Open EGL Library as EGL is separate
154         egl_lib_handle = dlopen(_COREGL_VENDOR_EGL_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
155         if (!egl_lib_handle)
156         {
157                 COREGL_ERR("\E[40;31;1m%s\E[0m\n\n", dlerror());
158                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n", _COREGL_VENDOR_EGL_LIB_PATH);
159                 return 0;
160         }
161
162         // test for invalid linking egl
163         if (dlsym(egl_lib_handle, "coregl_symbol_exported"))
164         {
165                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n", _COREGL_VENDOR_EGL_LIB_PATH);
166                 return 0;
167         }
168
169         // use gl_lib handle for GL symbols
170         gl_lib_handle = dlopen(_COREGL_VENDOR_GL_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
171         if (!gl_lib_handle)
172         {
173                 COREGL_ERR("\E[40;31;1m%s\E[0m\n\n", dlerror());
174                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n", _COREGL_VENDOR_GL_LIB_PATH);
175                 return 0;
176         }
177
178         // test for invalid linking gl
179         if (dlsym(gl_lib_handle, "coregl_symbol_exported"))
180         {
181                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n", _COREGL_VENDOR_GL_LIB_PATH);
182                 return 0;
183         }
184
185         // test for a GLES 3.0 symbol
186         if (dlsym(gl_lib_handle, "glReadBuffer"))
187         {
188                 COREGL_LOG("[CoreGL] Driver GL version 3.0 \n");
189                 driver_gl_version = COREGL_GLAPI_3;
190         }else {
191                 COREGL_LOG("[CoreGL] Driver GL version 2.0 \n");
192         }
193
194         //------------------------------------------------//
195
196         if (!_glue_sym_init()) return 0;
197         if (!_gl_sym_init()) return 0;
198
199         return 1;
200 }
201
202 static int
203 _gl_lib_deinit(void)
204 {
205         if (egl_lib_handle) dlclose(egl_lib_handle);
206         if (gl_lib_handle) dlclose(gl_lib_handle);
207
208         return 1;
209 }
210
211 int
212 coregl_initialize()
213 {
214         COREGL_LOG("[CoreGL] <%d> (%s) Library initializing...", getpid(), _COREGL_COMPILE_DATE);
215
216         if (!_gl_lib_init()) return 0;
217
218         init_export();
219
220         COREGL_LOG(" -> Completed\n");
221
222         init_modules();
223
224         return 1;
225 }
226
227 __attribute__((destructor))
228 void
229 coregl_terminate()
230 {
231         if (export_initialized != 0)
232         {
233                 deinit_modules();
234
235                 _gl_lib_deinit();
236         }
237 }
238