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