tizen 2.3 release
[framework/system/swap-probe.git] / helper / common_probe_init.c
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Vitaliy Cherepanov <v.cherepanov@samsung.com>
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the
12  * Free Software Foundation; either version 2.1 of the License, or (at your option)
13  * any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation, Inc., 51
22  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  * Contributors:
25  * - S-Core Co., Ltd
26  *
27  */
28
29 #include <unistd.h>
30 #include "da_gles20.h"
31 #include "binproto.h"
32 #include "common_probe_init.h"
33 #include "real_functions.h"
34
35 //#define EGL_TEST
36 Evas_GL_API *__gl_api;
37 void (*real_glGetVertexAttribfv)(GLuint index, GLenum pname, GLfloat *params);
38 void (*real_glGetIntegerv)(GLenum pname, GLint * params);
39 void (*real_glGetProgramiv)(GLuint program, GLenum pname, GLint *params);
40 void (*real_glGetShaderiv)(GLuint shader, GLenum pname, GLint *params);
41 void (*real_glGetActiveAttrib)(GLuint program, GLuint index, GLsizei bufSize,
42                                GLsizei *length, GLint *size, GLenum *type,
43                                char *name);
44 void (*real_glGetActiveUniform)(GLuint program, GLuint index, GLsizei bufSize,
45                                 GLsizei *length, GLint *size, GLenum *type,
46                                 char *name);
47 void (*real_glGetShaderSource)(GLuint shader, GLsizei bufSize, GLsizei *length,
48                                char *source);
49 void (*real_glGetBufferParameteriv)(GLenum target, GLenum value, GLint *data);
50
51 void __gl_dummy_function()
52 {
53         PRINTERR("call of dummy gl function!!!");
54 }
55
56 void set_real_func(const char *func_name, void **func_pointer,
57                    ORIGINAL_LIBRARY id)
58 {
59         void *faddr;
60         void *_id;
61
62         if (lib_handle[id] == NULL)
63                 lib_handle[id] = dlopen(lib_string[id], RTLD_LAZY);
64
65         _id = lib_handle[id];
66
67         if (_id == NULL)
68                 probe_terminate_with_err("dlopen failed", func_name, id);
69
70         faddr = dlsym(_id, func_name);
71         if (faddr == NULL || dlerror() != NULL) {
72                 PRINTWRN("[set_real_function] function <%s> not found in lib <%s>; dummy function will be seted",
73                          func_name, lib_string[id]);
74                 faddr = __gl_dummy_function;
75         }
76
77         memcpy(func_pointer, &faddr, sizeof(faddr));
78 }
79
80 #define INIT_REAL_GL(func)\
81         do {                                                                    \
82                  set_real_func(#func, (void **)&real##_##func, LIBGLES20);      \
83         } while (0)
84
85 int __init_gl_functions__(void)
86 {
87         /* init gl API */
88         __init_gl_api__();
89
90         /* init real call functions */
91         INIT_REAL_GL(glGetVertexAttribfv);
92         INIT_REAL_GL(glGetProgramiv);
93         INIT_REAL_GL(glGetShaderiv);
94         INIT_REAL_GL(glGetActiveAttrib);
95         INIT_REAL_GL(glGetActiveUniform);
96         INIT_REAL_GL(glGetShaderSource);
97         INIT_REAL_GL(glGetIntegerv);
98         INIT_REAL_GL(glGetBufferParameteriv);
99
100         return 0;
101 }
102
103 void probe_terminate_with_err(const char *msg, const char *func_name,
104                               ORIGINAL_LIBRARY id)
105 {
106         char error_msg[1024];
107         const char *lib_name = "unknown";
108
109         if (id != LIB_NO && id < NUM_ORIGINAL_LIBRARY)
110                 lib_name = lib_string[id];
111         snprintf(error_msg, sizeof(error_msg), "%s : [%s], %s\n", msg,
112                  func_name, lib_name);
113         perror(error_msg);
114         PRINTERR(error_msg);
115         //wait for flush
116         sleep(1);
117         exit(0);
118 }
119
120 ////////////////////////////////////////////////////////////////////////////
121 //egl init probe function
122 //  params:
123 //    func_name    - original function name for dlsym search
124 //    func_pointer - original function pointer (return)
125 //
126 //  info
127 //    search original function by name
128 //    function have no return becouse on error it terminates main application
129 #ifdef EGL_TEST
130
131 void dummy()
132 {
133         return;
134 }
135
136 void init_probe_egl(__attribute__ ((unused))const char *func_name, void **func_pointer,
137                     __attribute__ ((unused))ORIGINAL_LIBRARY id)
138 {
139         PRINTMSG(func_name);
140         *func_pointer = (void *)dummy;
141 }
142 #else
143 void init_probe_egl(const char *func_name, void **func_pointer,
144                     ORIGINAL_LIBRARY id)
145 {
146         void *faddr = 0;
147
148         (gProbeBlockCount++);
149         if (lib_handle[id] == ((void *)0)) {
150                 lib_handle[id] = dlopen(lib_string[id],
151                                         RTLD_LAZY | RTLD_GLOBAL);
152
153                 if (lib_handle[id] == ((void *)0))
154                         probe_terminate_with_err("dlopen failed", func_name, id);
155         };
156         faddr = dlsym(lib_handle[id], func_name);
157         if (faddr == NULL || dlerror() != NULL)
158                 probe_terminate_with_err("dlsym failed", func_name, id);
159         memcpy(func_pointer, &faddr, sizeof(faddr));
160         (gProbeBlockCount--);
161 }
162 #endif
163
164 void init_probe_gl(const char *func_name, void **func_pointer,
165                    ORIGINAL_LIBRARY id, int blockresult, int32_t vAPI_ID)
166 {
167         void *faddr;
168         probeInfo_t tempProbeInfo;
169
170         probeBlockStart();
171         if (lib_handle[id] == ((void *)0)) {
172                 lib_handle[id] = dlopen(lib_string[id], RTLD_LAZY);
173                 if (lib_handle[id] == ((void *)0))
174                         probe_terminate_with_err("dlopen failed", func_name,
175                                                  id);
176
177                 setProbePoint(&tempProbeInfo);
178                 if (blockresult != 0) {
179                         /* get max value */
180                         char maxValString[64];
181                         GLint maxVal[2];
182                         real_glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVal[0]);
183                         real_glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS,
184                                            &maxVal[1]);
185                         snprintf(maxValString, sizeof(maxValString), "%d,%d", maxVal[0], maxVal[1]);
186                         PREPARE_LOCAL_BUF();
187                         PACK_COMMON_BEGIN(MSG_PROBE_GL, vAPI_ID, "", 0);
188                         PACK_COMMON_END('p', 1, 0, 0);
189                         PACK_GL_ADD(APITYPE_INIT, 0, maxValString);
190                         FLUSH_LOCAL_BUF();
191                 }
192         }
193
194         faddr = dlsym(lib_handle[id], func_name);
195         if (faddr == NULL || dlerror() != NULL)
196                 probe_terminate_with_err("function not found in lib", func_name,
197                                          id);
198
199         memcpy(func_pointer, &faddr, sizeof(faddr));
200         probeBlockEnd();
201 }