Add coding style guide.
[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 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 = 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                 COREGL_LOG("[COREGL] de-init thread state \n");
49                 deinit_modules_tstate(tstate);
50                 remove_from_general_trace_list(&thread_trace_list, tstate);
51                 free(tstate);
52                 tstate = NULL;
53         }
54
55         set_current_thread_state(NULL);
56 }
57
58 int
59 init_new_thread_state()
60 {
61         int ret = 0;
62         GLThreadState *tstate = NULL;
63
64         tstate = get_current_thread_state();
65         AST(tstate == NULL);
66
67         tstate = (GLThreadState *)calloc(1, sizeof(GLThreadState));
68         tstate->thread_id = get_current_thread();
69         add_to_general_trace_list(&thread_trace_list, tstate);
70
71         init_modules_tstate(tstate);
72         set_current_thread_state(tstate);
73
74         ret = 1;
75         goto finish;
76
77 finish:
78         return ret;
79 }
80
81 static void
82 _sym_missing()
83 {
84         COREGL_ERR("GL symbol missing! Check client version!\n");
85 }
86
87 #define FINDSYM(libhandle, getproc, dst, sym) \
88    if(api_gl_version <= driver_gl_version) { \
89       if (!dst || (void *)dst == (void *)_sym_missing) \
90                   if (getproc) dst = (__typeof__(dst))getproc(sym); \
91       if (!dst || (void *)dst == (void *)_sym_missing) \
92                   dst = (__typeof__(dst))dlsym(libhandle, sym); \
93           if (!dst) dst = (__typeof__(dst))_sym_missing;\
94    }
95
96 static int
97 _glue_sym_init(void)
98 {
99
100 #define _COREGL_START_API(version)              api_gl_version = version;
101 #define _COREGL_END_API(version)                api_gl_version = COREGL_GLAPI_2;
102 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST) \
103     FINDSYM(egl_lib_handle, _sym_eglGetProcAddress, _sym_##FUNC_NAME, #FUNC_NAME);
104 #define _COREGL_EXT_SYMBOL_ALIAS(FUNC_NAME, ALIAS_NAME) \
105     FINDSYM(egl_lib_handle, _sym_eglGetProcAddress, _sym_##ALIAS_NAME, #FUNC_NAME);
106
107 #include "headers/sym_egl.h"
108
109 #undef _COREGL_EXT_SYMBOL_ALIAS
110 #undef _COREGL_SYMBOL
111 #undef _COREGL_START_API
112 #undef _COREGL_END_API
113
114         return 1;
115 }
116
117 static int
118 _gl_sym_init(void)
119 {
120
121 #define _COREGL_START_API(version)              api_gl_version = version;
122 #define _COREGL_END_API(version)                api_gl_version = COREGL_GLAPI_2;
123 #define _COREGL_SYMBOL(RET_TYPE, FUNC_NAME, PARAM_LIST) \
124     FINDSYM(gl_lib_handle, _sym_eglGetProcAddress, _sym_##FUNC_NAME, #FUNC_NAME);
125 #define _COREGL_EXT_SYMBOL_ALIAS(FUNC_NAME, ALIAS_NAME) \
126     FINDSYM(gl_lib_handle, _sym_eglGetProcAddress, _sym_##ALIAS_NAME, #FUNC_NAME);
127
128 #include "headers/sym_gl.h"
129
130 #undef _COREGL_EXT_SYMBOL_ALIAS
131 #undef _COREGL_SYMBOL
132 #undef _COREGL_START_API
133 #undef _COREGL_END_API
134
135         return 1;
136 }
137
138 #undef FINDSYM
139
140
141 COREGL_API void coregl_symbol_exported()
142 {
143         COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL)\E[0m\n");
144 }
145
146 static int
147 _gl_lib_init(void)
148 {
149         //------------------------------------------------//
150         // Open EGL Library as EGL is separate
151         egl_lib_handle = dlopen(_COREGL_VENDOR_EGL_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
152         if (!egl_lib_handle) {
153                 COREGL_ERR("\E[40;31;1m%s\E[0m\n\n", dlerror());
154                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n",
155                            _COREGL_VENDOR_EGL_LIB_PATH);
156                 return 0;
157         }
158
159         // test for invalid linking egl
160         if (dlsym(egl_lib_handle, "coregl_symbol_exported")) {
161                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n",
162                            _COREGL_VENDOR_EGL_LIB_PATH);
163                 return 0;
164         }
165
166         // use gl_lib handle for GL symbols
167         gl_lib_handle = dlopen(_COREGL_VENDOR_GL_LIB_PATH, RTLD_LAZY | RTLD_LOCAL);
168         if (!gl_lib_handle) {
169                 COREGL_ERR("\E[40;31;1m%s\E[0m\n\n", dlerror());
170                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n",
171                            _COREGL_VENDOR_GL_LIB_PATH);
172                 return 0;
173         }
174
175         // test for invalid linking gl
176         if (dlsym(gl_lib_handle, "coregl_symbol_exported")) {
177                 COREGL_ERR("\E[40;31;1mInvalid library link! (Check linkage of libCOREGL -> %s)\E[0m\n",
178                            _COREGL_VENDOR_GL_LIB_PATH);
179                 return 0;
180         }
181
182         // test for a GLES 3.0 symbol
183         if (dlsym(gl_lib_handle, "glBindProgramPipeline")) {
184                 COREGL_LOG("[CoreGL] Driver GL version 3.1 \n");
185                 driver_gl_version = COREGL_GLAPI_31;
186         } else if (dlsym(gl_lib_handle, "glReadBuffer")) {
187                 COREGL_LOG("[CoreGL] Driver GL version 3.0 \n");
188                 driver_gl_version = COREGL_GLAPI_3;
189         } else {
190                 COREGL_LOG("[CoreGL] Driver GL version 2.0 \n");
191         }
192
193         //------------------------------------------------//
194
195         if (!_glue_sym_init()) return 0;
196         if (!_gl_sym_init()) return 0;
197
198         return 1;
199 }
200
201 static int
202 _gl_lib_deinit(void)
203 {
204         if (egl_lib_handle) dlclose(egl_lib_handle);
205         if (gl_lib_handle) dlclose(gl_lib_handle);
206
207         return 1;
208 }
209
210 int
211 coregl_initialize()
212 {
213         COREGL_LOG("[CoreGL] <%d> (%s) Library initializing...", getpid(),
214                    _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                 deinit_modules();
233
234                 _gl_lib_deinit();
235         }
236 }
237