Make log related global variables as local
[platform/core/uifw/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
15 int                 driver_gl_version = 0;
16 static int          api_gl_version = COREGL_GLAPI_2;
17
18 // Symbol definition for real
19 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST)     RET_TYPE(*_sym_##FUNC_NAME) PARAM_LIST;
20 #include "headers/sym.h"
21 #undef _COREGL_SYMBOL
22
23 static unsigned int coregl_log_lvl;
24 static unsigned int coregl_dlog_enable;
25 static unsigned int coregl_log_initialized;
26
27 unsigned int
28 coregl_get_log_lvl()
29 {
30     return coregl_log_lvl;
31 }
32
33 void
34 coregl_set_log_lvl(unsigned int value)
35 {
36      coregl_log_lvl = value;
37 }
38
39 unsigned int
40 coregl_get_dlog_enable()
41 {
42     return coregl_dlog_enable;
43 }
44
45 void
46 coregl_set_dlog_enable(unsigned int value)
47 {
48     coregl_dlog_enable = value;
49 }
50
51 unsigned int
52 coregl_get_log_init()
53 {
54     return coregl_log_initialized;
55 }
56
57 void
58 coregl_set_log_init(unsigned int value)
59 {
60     coregl_log_initialized = value;
61 }
62
63 const char *
64 get_env_setting(const char *name)
65 {
66         char *fp_env = NULL;
67         static char *fp_default = "\0";
68         fp_env = getenv(name);
69         if (fp_env == NULL) fp_env = fp_default;
70         return fp_env;
71 }
72
73 void
74 cleanup_current_thread_state()
75 {
76         GLThreadState *tstate = NULL;
77
78         tstate = get_current_thread_state();
79
80         if (tstate != NULL) {
81                 COREGL_DBG("de-init thread state");
82                 deinit_modules_tstate(tstate);
83                 remove_from_general_trace_list(&thread_trace_list, tstate);
84                 free(tstate);
85                 tstate = NULL;
86         }
87
88         set_current_thread_state(NULL);
89 }
90
91 int
92 init_new_thread_state()
93 {
94         int ret = 0;
95         GLThreadState *tstate = NULL;
96
97         tstate = get_current_thread_state();
98         AST(tstate == NULL);
99
100         tstate = (GLThreadState *)calloc(1, sizeof(GLThreadState));
101
102         if (tstate == NULL) {
103                 ret = 0;
104                 goto finish;
105         }
106
107         tstate->thread_id = get_current_thread();
108         add_to_general_trace_list(&thread_trace_list, tstate);
109
110         init_modules_tstate(tstate);
111         set_current_thread_state(tstate);
112
113         ret = 1;
114         goto finish;
115
116 finish:
117         return ret;
118 }
119
120 static void
121 _sym_missing()
122 {
123         COREGL_ERR("GL symbol missing! Check client version!");
124 }
125
126 #define FINDGLSYM(libhandle, getproc, dst, sym) \
127         if (api_gl_version <= driver_gl_version) { \
128           if (!dst || (void *)dst == (void *)_sym_missing) \
129                   if (getproc) dst = (__typeof__(dst))getproc(sym); \
130           if (!dst || (void *)dst == (void *)_sym_missing) \
131                   dst = (__typeof__(dst))dlsym(libhandle, sym); \
132           if (!dst) dst = (__typeof__(dst))_sym_missing; \
133         }
134
135 #define FINDEGLSYM(libhandle, getproc, dst, sym) { \
136           if (!dst || (void *)dst == (void *)_sym_missing) \
137                   if (getproc) dst = (__typeof__(dst))getproc(sym); \
138           if (!dst || (void *)dst == (void *)_sym_missing) \
139                   dst = (__typeof__(dst))dlsym(libhandle, sym); \
140           if (!dst) dst = (__typeof__(dst))_sym_missing;\
141         }
142
143 static int
144 _glue_sym_init(void)
145 {
146 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST) \
147         FINDEGLSYM(egl_lib_handle, _sym_eglGetProcAddress, _sym_##FUNC_NAME, #FUNC_NAME);
148 #define _COREGL_EXT_SYMBOL_ALIAS(FUNC_NAME, ALIAS_NAME) \
149         FINDEGLSYM(egl_lib_handle, _sym_eglGetProcAddress, _sym_##ALIAS_NAME, #FUNC_NAME);
150
151 #include "headers/sym_egl.h"
152
153 #undef _COREGL_EXT_SYMBOL_ALIAS
154 #undef _COREGL_SYMBOL
155
156         return 1;
157 }
158
159 static int
160 _gl_sym_init(int init_version)
161 {
162
163 #define _COREGL_START_API(version)              api_gl_version = version;
164 #define _COREGL_END_API(version)                api_gl_version = COREGL_GLAPI_2;
165 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST) \
166         FINDGLSYM(gl_lib_handle, _sym_eglGetProcAddress, _sym_##FUNC_NAME, #FUNC_NAME);
167 #define _COREGL_EXT_SYMBOL_ALIAS(FUNC_NAME, ALIAS_NAME) \
168         FINDGLSYM(gl_lib_handle, _sym_eglGetProcAddress, _sym_##ALIAS_NAME, #FUNC_NAME);
169
170         if (init_version == COREGL_GLAPI_1) {
171                 #include "headers/sym_gl1.h"
172                 #include "headers/sym_gl_common.h"
173         } else if (init_version == COREGL_GLAPI_2) {
174                 #include "headers/sym_gl2.h"
175                 #include "headers/sym_gl_common.h"
176         }
177
178 #undef _COREGL_EXT_SYMBOL_ALIAS
179 #undef _COREGL_SYMBOL
180 #undef _COREGL_START_API
181 #undef _COREGL_END_API
182
183         return 1;
184 }
185
186 #undef FINDEGLSYM
187 #undef FINDGLSYM
188
189
190 COREGL_API void coregl_symbol_exported()
191 {
192         COREGL_ERR("Invalid library link! (Check linkage of libCOREGL)");
193 }
194
195 COREGL_API void set_driver_gl_version(int version)
196 {
197         driver_gl_version = version;
198 }
199
200 static int
201 _gl_lib_init(void)
202 {
203         //------------------------------------------------//
204         // Open EGL Library as EGL is separate
205         egl_lib_handle = dlopen(_COREGL_VENDOR_EGL_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
206         if (!egl_lib_handle) {
207                 COREGL_ERR("%s", dlerror());
208                 COREGL_ERR("Invalid library link! (Check linkage of libCOREGL -> %s)",
209                                    _COREGL_VENDOR_EGL_LIB_PATH);
210                 return 0;
211         }
212
213         // test for invalid linking egl
214         if (dlsym(egl_lib_handle, "coregl_symbol_exported")) {
215                 COREGL_ERR("Invalid library link! (Check linkage of libCOREGL -> %s)",
216                                    _COREGL_VENDOR_EGL_LIB_PATH);
217                 return 0;
218         }
219
220         // use gl_lib handle for GL symbols
221         if (driver_gl_version == COREGL_GLAPI_1) {
222                 gl_lib_handle = dlopen(_COREGL_VENDOR_GLV1_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
223                 if (!gl_lib_handle) {
224                         COREGL_ERR("%s", dlerror());
225                         COREGL_ERR("Invalid library link! (Check linkage of libCOREGL -> %s)",
226                                            _COREGL_VENDOR_GLV1_LIB_PATH);
227                 } else {
228                         // test for invalid linking gl
229                         if (dlsym(gl_lib_handle, "coregl_symbol_exported")) {
230                                 COREGL_ERR("Invalid library link! (Check linkage of libCOREGL -> %s)",
231                                                    _COREGL_VENDOR_GLV1_LIB_PATH);
232                                 return 0;
233                         }
234                         COREGL_DBG("Driver GL version 1.1");
235                         _gl_sym_init(COREGL_GLAPI_1);
236                 }
237         } else if (driver_gl_version == COREGL_GLAPI_2) {
238                 // in case of driver library is separated
239                 gl_lib_handle = dlopen(_COREGL_VENDOR_GLV1_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
240                 if (gl_lib_handle)
241                         _gl_sym_init(COREGL_GLAPI_1);
242
243                 gl_lib_handle = dlopen(_COREGL_VENDOR_GLV2_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
244                 if (!gl_lib_handle) {
245                         COREGL_ERR("%s", dlerror());
246                         COREGL_ERR("Invalid library link! (Check linkage of libCOREGL -> %s)",
247                                            _COREGL_VENDOR_GLV2_LIB_PATH);
248                 } else {
249                         // test for invalid linking gl
250                         if (dlsym(gl_lib_handle, "coregl_symbol_exported")) {
251                                 COREGL_ERR("Invalid library link! (Check linkage of libCOREGL -> %s)",
252                                                    _COREGL_VENDOR_GLV2_LIB_PATH);
253                                 return 0;
254                         }
255
256                         // test for a GLES 3.x symbol
257                         if (dlsym(gl_lib_handle, "glBlendBarrier")) {
258                                 COREGL_DBG("Driver GL version 3.2");
259                                 driver_gl_version = COREGL_GLAPI_32;
260                         } else if (dlsym(gl_lib_handle, "glBindProgramPipeline")) {
261                                 COREGL_DBG("Driver GL version 3.1");
262                                 driver_gl_version = COREGL_GLAPI_31;
263                         } else if (dlsym(gl_lib_handle, "glReadBuffer")) {
264                                 COREGL_DBG("Driver GL version 3.0");
265                                 driver_gl_version = COREGL_GLAPI_3;
266                         } else {
267                                 COREGL_DBG("Driver GL version 2.0");
268                         }
269
270                         _gl_sym_init(COREGL_GLAPI_2);
271                 }
272         }
273
274         _glue_sym_init();
275
276         return 1;
277 }
278
279 static int
280 _gl_lib_deinit(void)
281 {
282         if (egl_lib_handle) dlclose(egl_lib_handle);
283         if (gl_lib_handle) dlclose(gl_lib_handle);
284
285         return 1;
286 }
287
288 int
289 coregl_initialize()
290 {
291         COREGL_DBG("(%s) Library initializing...", _COREGL_COMPILE_DATE);
292
293         if (!_gl_lib_init()) return 0;
294
295         init_export(GL_TRUE, GL_TRUE);
296
297         COREGL_DBG(" -> Completed");
298
299         init_modules();
300
301         return 1;
302 }
303
304 __attribute__((destructor))
305 void
306 coregl_terminate()
307 {
308         if (export_initialized != 0) {
309                 deinit_modules();
310
311                 _gl_lib_deinit();
312
313                 export_initialized = 0;
314         }
315 }
316