evas: remove EVAS_FRAME_QUEUING.
[framework/uifw/evas.git] / src / modules / engines / software_generic / evas_engine.c
1 #include "evas_common.h" /* Also includes international specific stuff */
2 #include "evas_private.h"
3
4 #ifdef HAVE_DLSYM
5 # include <dlfcn.h>      /* dlopen,dlclose,etc */
6
7 # define EVAS_GL 1
8 # define EVAS_GL_NO_GL_H_CHECK 1
9 # include "Evas_GL.h"
10
11 #else
12 # warning software_generic will not be able to have Evas_GL API.
13 #endif
14
15 #ifdef EVAS_GL
16 //----------------------------------//
17 // OSMesa...
18
19 #define OSMESA_MAJOR_VERSION 6
20 #define OSMESA_MINOR_VERSION 5
21 #define OSMESA_PATCH_VERSION 0
22
23 /*
24  * Values for the format parameter of OSMesaCreateContext()
25  * New in version 2.0.
26  */
27 #define OSMESA_COLOR_INDEX      GL_COLOR_INDEX
28 #define OSMESA_RGBA             GL_RGBA
29 #define OSMESA_BGRA             0x1
30 #define OSMESA_ARGB             0x2
31 #define OSMESA_RGB              GL_RGB
32 #define OSMESA_BGR              0x4
33 #define OSMESA_RGB_565          0x5
34
35
36 /*
37  * OSMesaPixelStore() parameters:
38  * New in version 2.0.
39  */
40 #define OSMESA_ROW_LENGTH       0x10
41 #define OSMESA_Y_UP             0x11
42
43
44 /*
45  * Accepted by OSMesaGetIntegerv:
46  */
47 #define OSMESA_WIDTH            0x20
48 #define OSMESA_HEIGHT           0x21
49 #define OSMESA_FORMAT           0x22
50 #define OSMESA_TYPE             0x23
51 #define OSMESA_MAX_WIDTH        0x24  /* new in 4.0 */
52 #define OSMESA_MAX_HEIGHT       0x25  /* new in 4.0 */
53
54
55 typedef void (*OSMESAproc)();
56 typedef struct osmesa_context *OSMesaContext;
57 #endif
58
59 typedef struct _Render_Engine_GL_Surface    Render_Engine_GL_Surface;
60 typedef struct _Render_Engine_GL_Context    Render_Engine_GL_Context;
61
62 struct _Render_Engine_GL_Surface
63 {
64    int     initialized;
65    int     w, h;
66
67 #ifdef EVAS_GL
68    GLenum  internal_fmt;
69 #endif
70    int     internal_cpp;   // Component per pixel.  ie. RGB = 3
71
72    int     depth_bits;
73    int     stencil_bits;
74
75    // Data 
76    void   *buffer;
77
78    Render_Engine_GL_Context   *current_ctx;
79 };
80
81 #ifdef EVAS_GL
82 struct _Render_Engine_GL_Context
83 {
84    int            initialized;
85
86    OSMesaContext  context;
87
88    Render_Engine_GL_Context   *share_ctx;
89
90    Render_Engine_GL_Surface   *current_sfc;
91 };
92
93 //------------------------------------------------------//
94 typedef void                   (*_eng_fn) (void );
95 typedef _eng_fn                (*glsym_func_eng_fn) ();
96 typedef void                   (*glsym_func_void) ();
97 typedef unsigned int           (*glsym_func_uint) ();
98 typedef int                    (*glsym_func_int) ();
99 typedef unsigned char          (*glsym_func_uchar) ();
100 typedef unsigned char         *(*glsym_func_uchar_ptr) ();
101 typedef const unsigned char   *(*glsym_func_const_uchar_ptr) ();
102 typedef char const            *(*glsym_func_char_const_ptr) ();
103 typedef GLboolean              (*glsym_func_bool) ();
104 typedef OSMesaContext          (*glsym_func_osm_ctx) ();
105 //------------------------------------------------------//
106
107 /* Function table for GL APIs */
108 static Evas_GL_API gl_funcs;
109 static void *gl_lib_handle;
110 static int gl_lib_is_gles = 0;
111 static Evas_GL_API gl_funcs;
112
113 //------------------------------------------------------//
114 // OSMesa APIS...
115 static OSMesaContext (*_sym_OSMesaCreateContextExt)             (GLenum format, GLint depthBits, GLint stencilBits, GLint accumBits, OSMesaContext sharelist) = NULL;
116 static void          (*_sym_OSMesaDestroyContext)               (OSMesaContext ctx) = NULL;
117 static GLboolean     (*_sym_OSMesaMakeCurrent)                  (OSMesaContext ctx, void *buffer, GLenum type, GLsizei width, GLsizei height) = NULL;
118 static void          (*_sym_OSMesaPixelStore)                   (GLint pname, GLint value) = NULL;
119 static OSMESAproc    (*_sym_OSMesaGetProcAddress)               (const char *funcName);
120
121
122 //------------------------------------------------------//
123 // GLES 2.0 APIs...
124 static void       (*_sym_glActiveTexture)                       (GLenum texture) = NULL;
125 static void       (*_sym_glAttachShader)                        (GLuint program, GLuint shader) = NULL;
126 static void       (*_sym_glBindAttribLocation)                  (GLuint program, GLuint index, const char* name) = NULL;
127 static void       (*_sym_glBindBuffer)                          (GLenum target, GLuint buffer) = NULL;
128 static void       (*_sym_glBindFramebuffer)                     (GLenum target, GLuint framebuffer) = NULL;
129 static void       (*_sym_glBindRenderbuffer)                    (GLenum target, GLuint renderbuffer) = NULL;
130 static void       (*_sym_glBindTexture)                         (GLenum target, GLuint texture) = NULL;
131 static void       (*_sym_glBlendColor)                          (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) = NULL;
132 static void       (*_sym_glBlendEquation)                       (GLenum mode) = NULL;
133 static void       (*_sym_glBlendEquationSeparate)               (GLenum modeRGB, GLenum modeAlpha) = NULL;
134 static void       (*_sym_glBlendFunc)                           (GLenum sfactor, GLenum dfactor) = NULL;
135 static void       (*_sym_glBlendFuncSeparate)                   (GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha) = NULL;
136 static void       (*_sym_glBufferData)                          (GLenum target, GLsizeiptr size, const void* data, GLenum usage) = NULL;
137 static void       (*_sym_glBufferSubData)                       (GLenum target, GLintptr offset, GLsizeiptr size, const void* data) = NULL;
138 static GLenum     (*_sym_glCheckFramebufferStatus)              (GLenum target) = NULL;
139 static void       (*_sym_glClear)                               (GLbitfield mask) = NULL;
140 static void       (*_sym_glClearColor)                          (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) = NULL;
141 static void       (*_sym_glClearDepthf)                         (GLclampf depth) = NULL;
142 static void       (*_sym_glClearStencil)                        (GLint s) = NULL;
143 static void       (*_sym_glColorMask)                           (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) = NULL;
144 static void       (*_sym_glCompileShader)                       (GLuint shader) = NULL;
145 static void       (*_sym_glCompressedTexImage2D)                (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void* data) = NULL;
146 static void       (*_sym_glCompressedTexSubImage2D)             (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void* data) = NULL;
147 static void       (*_sym_glCopyTexImage2D)                      (GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) = NULL;
148 static void       (*_sym_glCopyTexSubImage2D)                   (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) = NULL;
149 static GLuint     (*_sym_glCreateProgram)                       (void) = NULL;
150 static GLuint     (*_sym_glCreateShader)                        (GLenum type) = NULL;
151 static void       (*_sym_glCullFace)                            (GLenum mode) = NULL;
152 static void       (*_sym_glDeleteBuffers)                       (GLsizei n, const GLuint* buffers) = NULL;
153 static void       (*_sym_glDeleteFramebuffers)                  (GLsizei n, const GLuint* framebuffers) = NULL;
154 static void       (*_sym_glDeleteProgram)                       (GLuint program) = NULL;
155 static void       (*_sym_glDeleteRenderbuffers)                 (GLsizei n, const GLuint* renderbuffers) = NULL;
156 static void       (*_sym_glDeleteShader)                        (GLuint shader) = NULL;
157 static void       (*_sym_glDeleteTextures)                      (GLsizei n, const GLuint* textures) = NULL;
158 static void       (*_sym_glDepthFunc)                           (GLenum func) = NULL;
159 static void       (*_sym_glDepthMask)                           (GLboolean flag) = NULL;
160 static void       (*_sym_glDepthRangef)                         (GLclampf zNear, GLclampf zFar) = NULL;
161 static void       (*_sym_glDetachShader)                        (GLuint program, GLuint shader) = NULL;
162 static void       (*_sym_glDisable)                             (GLenum cap) = NULL;
163 static void       (*_sym_glDisableVertexAttribArray)            (GLuint index) = NULL;
164 static void       (*_sym_glDrawArrays)                          (GLenum mode, GLint first, GLsizei count) = NULL;
165 static void       (*_sym_glDrawElements)                        (GLenum mode, GLsizei count, GLenum type, const void* indices) = NULL;
166 static void       (*_sym_glEnable)                              (GLenum cap) = NULL;
167 static void       (*_sym_glEnableVertexAttribArray)             (GLuint index) = NULL;
168 static void       (*_sym_glFinish)                              (void) = NULL;
169 static void       (*_sym_glFlush)                               (void) = NULL;
170 static void       (*_sym_glFramebufferRenderbuffer)             (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer) = NULL;
171 static void       (*_sym_glFramebufferTexture2D)                (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) = NULL;
172 static void       (*_sym_glFrontFace)                           (GLenum mode) = NULL;
173 static void       (*_sym_glGenBuffers)                          (GLsizei n, GLuint* buffers) = NULL;
174 static void       (*_sym_glGenerateMipmap)                      (GLenum target) = NULL;
175 static void       (*_sym_glGenFramebuffers)                     (GLsizei n, GLuint* framebuffers) = NULL;
176 static void       (*_sym_glGenRenderbuffers)                    (GLsizei n, GLuint* renderbuffers) = NULL;
177 static void       (*_sym_glGenTextures)                         (GLsizei n, GLuint* textures) = NULL;
178 static void       (*_sym_glGetActiveAttrib)                     (GLuint program, GLuint index, GLsizei bufsize, GLsizei* length, GLint* size, GLenum* type, char* name) = NULL;
179 static void       (*_sym_glGetActiveUniform)                    (GLuint program, GLuint index, GLsizei bufsize, GLsizei* length, GLint* size, GLenum* type, char* name) = NULL;
180 static void       (*_sym_glGetAttachedShaders)                  (GLuint program, GLsizei maxcount, GLsizei* count, GLuint* shaders) = NULL;
181 static int        (*_sym_glGetAttribLocation)                   (GLuint program, const char* name) = NULL;
182 static void       (*_sym_glGetBooleanv)                         (GLenum pname, GLboolean* params) = NULL;
183 static void       (*_sym_glGetBufferParameteriv)                (GLenum target, GLenum pname, GLint* params) = NULL;
184 static GLenum     (*_sym_glGetError)                            (void) = NULL;
185 static void       (*_sym_glGetFloatv)                           (GLenum pname, GLfloat* params) = NULL;
186 static void       (*_sym_glGetFramebufferAttachmentParameteriv) (GLenum target, GLenum attachment, GLenum pname, GLint* params) = NULL;
187 static void       (*_sym_glGetIntegerv)                         (GLenum pname, GLint* params) = NULL;
188 static void       (*_sym_glGetProgramiv)                        (GLuint program, GLenum pname, GLint* params) = NULL;
189 static void       (*_sym_glGetProgramInfoLog)                   (GLuint program, GLsizei bufsize, GLsizei* length, char* infolog) = NULL;
190 static void       (*_sym_glGetRenderbufferParameteriv)          (GLenum target, GLenum pname, GLint* params) = NULL;
191 static void       (*_sym_glGetShaderiv)                         (GLuint shader, GLenum pname, GLint* params) = NULL;
192 static void       (*_sym_glGetShaderInfoLog)                    (GLuint shader, GLsizei bufsize, GLsizei* length, char* infolog) = NULL;
193 static void       (*_sym_glGetShaderPrecisionFormat)            (GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision) = NULL;
194 static void       (*_sym_glGetShaderSource)                     (GLuint shader, GLsizei bufsize, GLsizei* length, char* source) = NULL;
195 static const GLubyte *(*_sym_glGetString)                           (GLenum name) = NULL;
196 static void       (*_sym_glGetTexParameterfv)                   (GLenum target, GLenum pname, GLfloat* params) = NULL;
197 static void       (*_sym_glGetTexParameteriv)                   (GLenum target, GLenum pname, GLint* params) = NULL;
198 static void       (*_sym_glGetUniformfv)                        (GLuint program, GLint location, GLfloat* params) = NULL;
199 static void       (*_sym_glGetUniformiv)                        (GLuint program, GLint location, GLint* params) = NULL;
200 static int        (*_sym_glGetUniformLocation)                  (GLuint program, const char* name) = NULL;
201 static void       (*_sym_glGetVertexAttribfv)                   (GLuint index, GLenum pname, GLfloat* params) = NULL;
202 static void       (*_sym_glGetVertexAttribiv)                   (GLuint index, GLenum pname, GLint* params) = NULL;
203 static void       (*_sym_glGetVertexAttribPointerv)             (GLuint index, GLenum pname, void** pointer) = NULL;
204 static void       (*_sym_glHint)                                (GLenum target, GLenum mode) = NULL;
205 static GLboolean  (*_sym_glIsBuffer)                            (GLuint buffer) = NULL;
206 static GLboolean  (*_sym_glIsEnabled)                           (GLenum cap) = NULL;
207 static GLboolean  (*_sym_glIsFramebuffer)                       (GLuint framebuffer) = NULL;
208 static GLboolean  (*_sym_glIsProgram)                           (GLuint program) = NULL;
209 static GLboolean  (*_sym_glIsRenderbuffer)                      (GLuint renderbuffer) = NULL;
210 static GLboolean  (*_sym_glIsShader)                            (GLuint shader) = NULL;
211 static GLboolean  (*_sym_glIsTexture)                           (GLuint texture) = NULL;
212 static void       (*_sym_glLineWidth)                           (GLfloat width) = NULL;
213 static void       (*_sym_glLinkProgram)                         (GLuint program) = NULL;
214 static void       (*_sym_glPixelStorei)                         (GLenum pname, GLint param) = NULL;
215 static void       (*_sym_glPolygonOffset)                       (GLfloat factor, GLfloat units) = NULL;
216 static void       (*_sym_glReadPixels)                          (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels) = NULL;
217 static void       (*_sym_glReleaseShaderCompiler)               (void) = NULL;
218 static void       (*_sym_glRenderbufferStorage)                 (GLenum target, GLenum internalformat, GLsizei width, GLsizei height) = NULL;
219 static void       (*_sym_glSampleCoverage)                      (GLclampf value, GLboolean invert) = NULL;
220 static void       (*_sym_glScissor)                             (GLint x, GLint y, GLsizei width, GLsizei height) = NULL;
221 static void       (*_sym_glShaderBinary)                        (GLsizei n, const GLuint* shaders, GLenum binaryformat, const void* binary, GLsizei length) = NULL;
222 static void       (*_sym_glShaderSource)                        (GLuint shader, GLsizei count, const char** string, const GLint* length) = NULL;
223 static void       (*_sym_glStencilFunc)                         (GLenum func, GLint ref, GLuint mask) = NULL;
224 static void       (*_sym_glStencilFuncSeparate)                 (GLenum face, GLenum func, GLint ref, GLuint mask) = NULL;
225 static void       (*_sym_glStencilMask)                         (GLuint mask) = NULL;
226 static void       (*_sym_glStencilMaskSeparate)                 (GLenum face, GLuint mask) = NULL;
227 static void       (*_sym_glStencilOp)                           (GLenum fail, GLenum zfail, GLenum zpass) = NULL;
228 static void       (*_sym_glStencilOpSeparate)                   (GLenum face, GLenum fail, GLenum zfail, GLenum zpass) = NULL;
229 static void       (*_sym_glTexImage2D)                          (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* pixels) = NULL;
230 static void       (*_sym_glTexParameterf)                       (GLenum target, GLenum pname, GLfloat param) = NULL;
231 static void       (*_sym_glTexParameterfv)                      (GLenum target, GLenum pname, const GLfloat* params) = NULL;
232 static void       (*_sym_glTexParameteri)                       (GLenum target, GLenum pname, GLint param) = NULL;
233 static void       (*_sym_glTexParameteriv)                      (GLenum target, GLenum pname, const GLint* params) = NULL;
234 static void       (*_sym_glTexSubImage2D)                       (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* pixels) = NULL;
235 static void       (*_sym_glUniform1f)                           (GLint location, GLfloat x) = NULL;
236 static void       (*_sym_glUniform1fv)                          (GLint location, GLsizei count, const GLfloat* v) = NULL;
237 static void       (*_sym_glUniform1i)                           (GLint location, GLint x) = NULL;
238 static void       (*_sym_glUniform1iv)                          (GLint location, GLsizei count, const GLint* v) = NULL;
239 static void       (*_sym_glUniform2f)                           (GLint location, GLfloat x, GLfloat y) = NULL;
240 static void       (*_sym_glUniform2fv)                          (GLint location, GLsizei count, const GLfloat* v) = NULL;
241 static void       (*_sym_glUniform2i)                           (GLint location, GLint x, GLint y) = NULL;
242 static void       (*_sym_glUniform2iv)                          (GLint location, GLsizei count, const GLint* v) = NULL;
243 static void       (*_sym_glUniform3f)                           (GLint location, GLfloat x, GLfloat y, GLfloat z) = NULL;
244 static void       (*_sym_glUniform3fv)                          (GLint location, GLsizei count, const GLfloat* v) = NULL;
245 static void       (*_sym_glUniform3i)                           (GLint location, GLint x, GLint y, GLint z) = NULL;
246 static void       (*_sym_glUniform3iv)                          (GLint location, GLsizei count, const GLint* v) = NULL;
247 static void       (*_sym_glUniform4f)                           (GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w) = NULL;
248 static void       (*_sym_glUniform4fv)                          (GLint location, GLsizei count, const GLfloat* v) = NULL;
249 static void       (*_sym_glUniform4i)                           (GLint location, GLint x, GLint y, GLint z, GLint w) = NULL;
250 static void       (*_sym_glUniform4iv)                          (GLint location, GLsizei count, const GLint* v) = NULL;
251 static void       (*_sym_glUniformMatrix2fv)                    (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
252 static void       (*_sym_glUniformMatrix3fv)                    (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
253 static void       (*_sym_glUniformMatrix4fv)                    (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
254 static void       (*_sym_glUseProgram)                          (GLuint program) = NULL;
255 static void       (*_sym_glValidateProgram)                     (GLuint program) = NULL;
256 static void       (*_sym_glVertexAttrib1f)                      (GLuint indx, GLfloat x) = NULL;
257 static void       (*_sym_glVertexAttrib1fv)                     (GLuint indx, const GLfloat* values) = NULL;
258 static void       (*_sym_glVertexAttrib2f)                      (GLuint indx, GLfloat x, GLfloat y) = NULL;
259 static void       (*_sym_glVertexAttrib2fv)                     (GLuint indx, const GLfloat* values) = NULL;
260 static void       (*_sym_glVertexAttrib3f)                      (GLuint indx, GLfloat x, GLfloat y, GLfloat z) = NULL;
261 static void       (*_sym_glVertexAttrib3fv)                     (GLuint indx, const GLfloat* values) = NULL;
262 static void       (*_sym_glVertexAttrib4f)                      (GLuint indx, GLfloat x, GLfloat y, GLfloat z, GLfloat w) = NULL;
263 static void       (*_sym_glVertexAttrib4fv)                     (GLuint indx, const GLfloat* values) = NULL;
264 static void       (*_sym_glVertexAttribPointer)                 (GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void* ptr) = NULL;
265 static void       (*_sym_glViewport)                            (GLint x, GLint y, GLsizei width, GLsizei height) = NULL;
266
267 // GLES Extensions...
268 /* static void       (*_sym_glGetProgramBinary)                    (GLuint a, GLsizei b, GLsizei* c, GLenum* d, void* e) = NULL; */
269 /* static void       (*_sym_glProgramBinary)                       (GLuint a, GLenum b, const void* c, GLint d) = NULL; */
270 /* static void       (*_sym_glProgramParameteri)                   (GLuint a, GLuint b, GLint d) = NULL; */
271 #endif
272
273 /*
274  *****
275  **
276  ** ENGINE ROUTINES
277  **
278  *****
279  */
280 static int cpunum = 0;
281 static int _evas_soft_gen_log_dom = -1;
282
283 static void
284 eng_output_dump(void *data __UNUSED__)
285 {
286    evas_common_image_image_all_unload();
287    evas_common_font_font_all_unload();
288 }
289
290 static void *
291 eng_context_new(void *data __UNUSED__)
292 {
293    return evas_common_draw_context_new();
294 }
295
296 static void
297 eng_context_free(void *data __UNUSED__, void *context)
298 {
299    evas_common_draw_context_free(context);
300 }
301
302 static void
303 eng_context_clip_set(void *data __UNUSED__, void *context, int x, int y, int w, int h)
304 {
305    evas_common_draw_context_set_clip(context, x, y, w, h);
306 }
307
308 static void
309 eng_context_clip_clip(void *data __UNUSED__, void *context, int x, int y, int w, int h)
310 {
311    evas_common_draw_context_clip_clip(context, x, y, w, h);
312 }
313
314 static void
315 eng_context_clip_unset(void *data __UNUSED__, void *context)
316 {
317    evas_common_draw_context_unset_clip(context);
318 }
319
320 static int
321 eng_context_clip_get(void *data __UNUSED__, void *context, int *x, int *y, int *w, int *h)
322 {
323    *x = ((RGBA_Draw_Context *)context)->clip.x;
324    *y = ((RGBA_Draw_Context *)context)->clip.y;
325    *w = ((RGBA_Draw_Context *)context)->clip.w;
326    *h = ((RGBA_Draw_Context *)context)->clip.h;
327    return ((RGBA_Draw_Context *)context)->clip.use;
328 }
329
330 static void
331 eng_context_color_set(void *data __UNUSED__, void *context, int r, int g, int b, int a)
332 {
333    evas_common_draw_context_set_color(context, r, g, b, a);
334 }
335
336 static int
337 eng_context_color_get(void *data __UNUSED__, void *context, int *r, int *g, int *b, int *a)
338 {
339    *r = (int)(R_VAL(&((RGBA_Draw_Context *)context)->col.col));
340    *g = (int)(G_VAL(&((RGBA_Draw_Context *)context)->col.col));
341    *b = (int)(B_VAL(&((RGBA_Draw_Context *)context)->col.col));
342    *a = (int)(A_VAL(&((RGBA_Draw_Context *)context)->col.col));
343    return 1;
344 }
345
346 static void
347 eng_context_multiplier_set(void *data __UNUSED__, void *context, int r, int g, int b, int a)
348 {
349    evas_common_draw_context_set_multiplier(context, r, g, b, a);
350 }
351
352 static void
353 eng_context_multiplier_unset(void *data __UNUSED__, void *context)
354 {
355    evas_common_draw_context_unset_multiplier(context);
356 }
357
358 static int
359 eng_context_multiplier_get(void *data __UNUSED__, void *context, int *r, int *g, int *b, int *a)
360 {
361    *r = (int)(R_VAL(&((RGBA_Draw_Context *)context)->mul.col));
362    *g = (int)(G_VAL(&((RGBA_Draw_Context *)context)->mul.col));
363    *b = (int)(B_VAL(&((RGBA_Draw_Context *)context)->mul.col));
364    *a = (int)(A_VAL(&((RGBA_Draw_Context *)context)->mul.col));
365    return ((RGBA_Draw_Context *)context)->mul.use;
366 }
367
368 static void
369 eng_context_mask_set(void *data __UNUSED__, void *context, void *mask, int x, int y, int w, int h)
370 {
371    evas_common_draw_context_set_mask(context, mask, x, y, w, h);
372 }
373
374 static void
375 eng_context_mask_unset(void *data __UNUSED__, void *context)
376 {
377    evas_common_draw_context_unset_mask(context);
378 }
379 /*
380 static void *
381 eng_context_mask_get(void *data __UNUSED__, void *context)
382 {
383    return ((RGBA_Draw_Context *)context)->mask.mask;
384 }
385 */
386
387 static void
388 eng_context_cutout_add(void *data __UNUSED__, void *context, int x, int y, int w, int h)
389 {
390    evas_common_draw_context_add_cutout(context, x, y, w, h);
391 }
392
393 static void
394 eng_context_cutout_clear(void *data __UNUSED__, void *context)
395 {
396    evas_common_draw_context_clear_cutouts(context);
397 }
398
399 static void
400 eng_context_anti_alias_set(void *data __UNUSED__, void *context, unsigned char aa)
401 {
402    evas_common_draw_context_set_anti_alias(context, aa);
403 }
404
405 static unsigned char
406 eng_context_anti_alias_get(void *data __UNUSED__, void *context)
407 {
408    return ((RGBA_Draw_Context *)context)->anti_alias;
409 }
410
411 static void
412 eng_context_color_interpolation_set(void *data __UNUSED__, void *context, int color_space)
413 {
414    evas_common_draw_context_set_color_interpolation(context, color_space);
415 }
416
417 static int
418 eng_context_color_interpolation_get(void *data __UNUSED__, void *context)
419 {
420    return ((RGBA_Draw_Context *)context)->interpolation.color_space;
421 }
422
423 static void
424 eng_context_render_op_set(void *data __UNUSED__, void *context, int op)
425 {
426    evas_common_draw_context_set_render_op(context, op);
427 }
428
429 static int
430 eng_context_render_op_get(void *data __UNUSED__, void *context)
431 {
432    return ((RGBA_Draw_Context *)context)->render_op;
433 }
434
435
436
437 static void
438 eng_rectangle_draw(void *data __UNUSED__, void *context, void *surface, int x, int y, int w, int h)
439 {
440 #ifdef BUILD_PIPE_RENDER
441    if ((cpunum > 1))
442      evas_common_pipe_rectangle_draw(surface, context, x, y, w, h);
443    else
444 #endif
445      {
446         evas_common_rectangle_draw(surface, context, x, y, w, h);
447         evas_common_cpu_end_opt();
448      }
449 }
450
451 static void
452 eng_line_draw(void *data __UNUSED__, void *context, void *surface, int x1, int y1, int x2, int y2)
453 {
454 #ifdef BUILD_PIPE_RENDER
455    if ((cpunum > 1))
456     evas_common_pipe_line_draw(surface, context, x1, y1, x2, y2);
457    else
458 #endif   
459      {
460         evas_common_line_draw(surface, context, x1, y1, x2, y2);
461         evas_common_cpu_end_opt();
462      }
463 }
464
465 static void *
466 eng_polygon_point_add(void *data __UNUSED__, void *context __UNUSED__, void *polygon, int x, int y)
467 {
468    return evas_common_polygon_point_add(polygon, x, y);
469 }
470
471 static void *
472 eng_polygon_points_clear(void *data __UNUSED__, void *context __UNUSED__, void *polygon)
473 {
474    return evas_common_polygon_points_clear(polygon);
475 }
476
477 static void
478 eng_polygon_draw(void *data __UNUSED__, void *context, void *surface, void *polygon, int x, int y)
479 {
480 #ifdef BUILD_PIPE_RENDER
481    if ((cpunum > 1))
482      evas_common_pipe_poly_draw(surface, context, polygon, x, y);
483    else
484 #endif
485      {
486         evas_common_polygon_draw(surface, context, polygon, x, y);
487         evas_common_cpu_end_opt();
488      }
489 }
490
491 static int
492 eng_image_alpha_get(void *data __UNUSED__, void *image)
493 {
494    Image_Entry *im;
495
496    if (!image) return 1;
497    im = image;
498    switch (im->space)
499      {
500       case EVAS_COLORSPACE_ARGB8888:
501         if (im->flags.alpha) return 1;
502       default:
503         break;
504      }
505    return 0;
506 }
507
508 static int
509 eng_image_colorspace_get(void *data __UNUSED__, void *image)
510 {
511    Image_Entry *im;
512
513    if (!image) return EVAS_COLORSPACE_ARGB8888;
514    im = image;
515    return im->space;
516 }
517
518 static Eina_Bool
519 eng_image_can_region_get(void *data __UNUSED__, void *image)
520 {
521    Image_Entry *im;
522    if (!image) return EINA_FALSE;
523    im = image;
524    return ((Evas_Image_Load_Func*) im->info.loader)->do_region;
525 }
526
527 static void
528 eng_image_mask_create(void *data __UNUSED__, void *image)
529 {
530    RGBA_Image *im;
531    int sz;
532    uint8_t *dst,*end;
533    uint32_t *src;
534
535    if (!image) return;
536    im = image;
537    if (im->mask.mask && !im->mask.dirty) return;
538
539    if (im->mask.mask) free(im->mask.mask);
540    sz = im->cache_entry.w * im->cache_entry.h;
541    im->mask.mask = malloc(sz);
542    dst = im->mask.mask;
543    if (!im->image.data)
544       evas_cache_image_load_data(&im->cache_entry);
545    src = (void*) im->image.data;
546    if (!src) return;
547    for (end = dst + sz ; dst < end ; dst ++, src ++)
548       *dst = *src >> 24;
549    im->mask.dirty = 0;
550 }
551
552
553 static void *
554 eng_image_alpha_set(void *data __UNUSED__, void *image, int has_alpha)
555 {
556    RGBA_Image *im;
557
558    if (!image) return NULL;
559    im = image;
560    if (im->cache_entry.space != EVAS_COLORSPACE_ARGB8888)
561      {
562         im->cache_entry.flags.alpha = 0;
563         return im;
564      }
565    im = (RGBA_Image *) evas_cache_image_alone(&im->cache_entry);
566    evas_common_image_colorspace_dirty(im);
567
568    im->cache_entry.flags.alpha = has_alpha ? 1 : 0;
569    return im;
570 }
571
572 static void *
573 eng_image_border_set(void *data __UNUSED__, void *image, int l __UNUSED__, int r __UNUSED__, int t __UNUSED__, int b __UNUSED__)
574 {
575    RGBA_Image *im;
576
577    im = image;
578    return im;
579 }
580
581 static void
582 eng_image_border_get(void *data __UNUSED__, void *image __UNUSED__, int *l __UNUSED__, int *r __UNUSED__, int *t __UNUSED__, int *b __UNUSED__)
583 {
584 }
585
586 static char *
587 eng_image_comment_get(void *data __UNUSED__, void *image, char *key __UNUSED__)
588 {
589    RGBA_Image *im;
590
591    if (!image) return NULL;
592    im = image;
593    return im->info.comment;
594 }
595
596 static char *
597 eng_image_format_get(void *data __UNUSED__, void *image __UNUSED__)
598 {
599    return NULL;
600 }
601
602 static void
603 eng_image_colorspace_set(void *data __UNUSED__, void *image, int cspace)
604 {
605    Image_Entry *im;
606
607    if (!image) return;
608    im = image;
609    evas_cache_image_colorspace(im, cspace);
610 }
611
612 static void *
613 eng_image_native_set(void *data __UNUSED__, void *image, void *native __UNUSED__)
614 {
615    //return image;
616    Evas_Native_Surface *ns = native;
617    Image_Entry *im = image, *im2 = NULL;
618
619    if (!im)
620      {
621         if ((!ns) && (ns->data.x11.visual))
622           {
623              im = evas_cache_image_data(evas_common_image_cache_get(),
624                                         im->w, im->h,
625                                         ns->data.x11.visual, 1,
626                                         EVAS_COLORSPACE_ARGB8888);
627              return im;
628           }
629         else
630            return NULL;
631      }
632
633    if ((!ns) && (!im)) return im;
634
635    if (!ns) return im;
636
637    im2 = evas_cache_image_data(evas_common_image_cache_get(), 
638                                im->w, im->h, 
639                                ns->data.x11.visual, 1,
640                                EVAS_COLORSPACE_ARGB8888);
641    evas_cache_image_drop(im);
642    im = im2;
643
644    return im;
645
646 }
647
648 static void *
649 eng_image_native_get(void *data __UNUSED__, void *image __UNUSED__)
650 {
651    return NULL;
652 }
653
654 static void *
655 eng_image_load(void *data __UNUSED__, const char *file, const char *key, int *error, Evas_Image_Load_Opts *lo)
656 {
657    *error = EVAS_LOAD_ERROR_NONE;
658    return evas_common_load_image_from_file(file, key, lo, error);
659 }
660
661 static void *
662 eng_image_new_from_data(void *data __UNUSED__, int w, int h, DATA32 *image_data, int alpha, int cspace)
663 {
664    return evas_cache_image_data(evas_common_image_cache_get(), w, h, image_data, alpha, cspace);
665 }
666
667 static void *
668 eng_image_new_from_copied_data(void *data __UNUSED__, int w, int h, DATA32 *image_data, int alpha, int cspace)
669 {
670    return evas_cache_image_copied_data(evas_common_image_cache_get(), w, h, image_data, alpha, cspace);
671 }
672
673 static void
674 eng_image_free(void *data __UNUSED__, void *image)
675 {
676    evas_cache_image_drop(image);
677 }
678
679 static void
680 eng_image_size_get(void *data __UNUSED__, void *image, int *w, int *h)
681 {
682    Image_Entry *im;
683
684    im = image;
685    if (w) *w = im->w;
686    if (h) *h = im->h;
687 }
688
689 static void *
690 eng_image_size_set(void *data __UNUSED__, void *image, int w, int h)
691 {
692    Image_Entry *im = image;
693    if (!im) return NULL;
694    return evas_cache_image_size_set(im, w, h);
695 }
696
697 static void *
698 eng_image_dirty_region(void *data __UNUSED__, void *image, int x, int y, int w, int h)
699 {
700    Image_Entry *im = image;
701    if (!im) return NULL;
702    return evas_cache_image_dirty(im, x, y, w, h);
703 }
704
705 static void *
706 eng_image_data_get(void *data __UNUSED__, void *image, int to_write, DATA32 **image_data, int *err)
707 {
708    RGBA_Image *im;
709    int error;
710
711    if (!image)
712      {
713         *image_data = NULL;
714         return NULL;
715      }
716    im = image;
717    error = evas_cache_image_load_data(&im->cache_entry);
718    switch (im->cache_entry.space)
719      {
720       case EVAS_COLORSPACE_ARGB8888:
721         if (to_write)
722           im = (RGBA_Image *)evas_cache_image_alone(&im->cache_entry);
723         *image_data = im->image.data;
724         break;
725       case EVAS_COLORSPACE_YCBCR422P601_PL:
726       case EVAS_COLORSPACE_YCBCR422P709_PL:
727       case EVAS_COLORSPACE_YCBCR422601_PL:
728       case EVAS_COLORSPACE_YCBCR420NV12601_PL:
729       case EVAS_COLORSPACE_YCBCR420TM12601_PL:
730         *image_data = im->cs.data;
731         break;
732       default:
733         abort();
734         break;
735      }
736    if (err) *err = error;
737    return im;
738 }
739
740 static void *
741 eng_image_data_put(void *data, void *image, DATA32 *image_data)
742 {
743    RGBA_Image *im, *im2;
744
745    if (!image) return NULL;
746    im = image;
747    switch (im->cache_entry.space)
748      {
749       case EVAS_COLORSPACE_ARGB8888:
750         if (image_data != im->image.data)
751           {
752              int w, h;
753
754              w = im->cache_entry.w;
755              h = im->cache_entry.h;
756              im2 = eng_image_new_from_data(data, w, h, image_data,
757                                            eng_image_alpha_get(data, image),
758                                            eng_image_colorspace_get(data, image));
759              evas_cache_image_drop(&im->cache_entry);
760              im = im2;
761           }
762         break;
763       case EVAS_COLORSPACE_YCBCR422P601_PL:
764       case EVAS_COLORSPACE_YCBCR422P709_PL:
765       case EVAS_COLORSPACE_YCBCR422601_PL:
766       case EVAS_COLORSPACE_YCBCR420NV12601_PL:
767       case EVAS_COLORSPACE_YCBCR420TM12601_PL:
768         if (image_data != im->cs.data)
769           {
770              if (im->cs.data)
771                {
772                   if (!im->cs.no_free) free(im->cs.data);
773                }
774              im->cs.data = image_data;
775           }
776         evas_common_image_colorspace_dirty(im);
777         break;
778       default:
779         abort();
780         break;
781      }
782    return im;
783 }
784
785 static void
786 eng_image_data_preload_request(void *data __UNUSED__, void *image, const void *target)
787 {
788    RGBA_Image *im = image;
789
790    if (!im) return ;
791    evas_cache_image_preload_data(&im->cache_entry, target);
792 }
793
794 static void
795 eng_image_data_preload_cancel(void *data __UNUSED__, void *image, const void *target)
796 {
797    RGBA_Image *im = image;
798
799    if (!im) return ;
800    evas_cache_image_preload_cancel(&im->cache_entry, target);
801 }
802
803 static void
804 eng_image_draw(void *data __UNUSED__, void *context, void *surface, void *image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int dst_w, int dst_h, int smooth)
805 {
806    RGBA_Image *im;
807
808    if (!image) return;
809    im = image;
810 #ifdef BUILD_PIPE_RENDER
811    if ((cpunum > 1))
812      {
813         evas_common_rgba_image_scalecache_prepare((Image_Entry *)(im), 
814                                                   surface, context, smooth,
815                                                   src_x, src_y, src_w, src_h,
816                                                   dst_x, dst_y, dst_w, dst_h);
817         
818         evas_common_pipe_image_draw(im, surface, context, smooth,
819                                     src_x, src_y, src_w, src_h,
820                                     dst_x, dst_y, dst_w, dst_h);
821      }
822    else
823 #endif
824      {
825 //        if (im->cache_entry.space == EVAS_COLORSPACE_ARGB8888)
826 //          evas_cache_image_load_data(&im->cache_entry);
827 //        evas_common_image_colorspace_normalize(im);
828         evas_common_rgba_image_scalecache_prepare(&im->cache_entry, surface, context, smooth,
829                                                   src_x, src_y, src_w, src_h,
830                                                   dst_x, dst_y, dst_w, dst_h);
831         evas_common_rgba_image_scalecache_do(&im->cache_entry, surface, context, smooth,
832                                              src_x, src_y, src_w, src_h,
833                                              dst_x, dst_y, dst_w, dst_h);
834 /*        
835         if (smooth)
836           evas_common_scale_rgba_in_to_out_clip_smooth(im, surface, context,
837                                                        src_x, src_y, src_w, src_h,
838                                                        dst_x, dst_y, dst_w, dst_h);
839         else
840           evas_common_scale_rgba_in_to_out_clip_sample(im, surface, context,
841                                                        src_x, src_y, src_w, src_h,
842                                                        dst_x, dst_y, dst_w, dst_h);
843  */
844         evas_common_cpu_end_opt();
845      }
846 }
847
848 static void
849 eng_image_map_draw(void *data __UNUSED__, void *context, void *surface, void *image, int npoints, RGBA_Map_Point *p, int smooth, int level)
850 {
851    RGBA_Image *im;
852
853    if (!image) return;
854    if (npoints < 3) return;
855    im = image;
856
857    if ((p[0].x == p[3].x) &&
858        (p[1].x == p[2].x) &&
859        (p[0].y == p[1].y) &&
860        (p[3].y == p[2].y) &&
861        (p[0].x <= p[1].x) &&
862        (p[0].y <= p[2].y) &&
863        (p[0].u == 0) &&
864        (p[0].v == 0) &&
865        (p[1].u == (int)(im->cache_entry.w << FP)) &&
866        (p[1].v == 0) &&
867        (p[2].u == (int)(im->cache_entry.w << FP)) &&
868        (p[2].v == (int)(im->cache_entry.h << FP)) &&
869        (p[3].u == 0) &&
870        (p[3].v == (int)(im->cache_entry.h << FP)) &&
871        (p[0].col == 0xffffffff) &&
872        (p[1].col == 0xffffffff) &&
873        (p[2].col == 0xffffffff) &&
874        (p[3].col == 0xffffffff))
875      {
876         int dx, dy, dw, dh;
877
878         dx = p[0].x >> FP;
879         dy = p[0].y >> FP;
880         dw = (p[2].x >> FP) - dx;
881         dh = (p[2].y >> FP) - dy;
882         eng_image_draw
883           (data, context, surface, image,
884            0, 0, im->cache_entry.w, im->cache_entry.h,
885            dx, dy, dw, dh, smooth);
886      }
887    else
888      {
889 #ifdef BUILD_PIPE_RENDER
890         if ((cpunum > 1))
891           evas_common_pipe_map_draw(im, surface, context, npoints, p, smooth, level);
892         else
893 #endif
894           evas_common_map_rgba(im, surface, context, npoints, p, smooth, level);
895      }
896    evas_common_cpu_end_opt();
897
898    if (npoints > 4)
899      {
900         eng_image_map_draw(data, context, surface, image, npoints - 2, p + 2,
901                            smooth, level);
902      }
903 }
904
905 static void *
906 eng_image_map_surface_new(void *data __UNUSED__, int w, int h, int alpha)
907 {
908    void *surface;
909    surface = evas_cache_image_copied_data(evas_common_image_cache_get(), 
910                                           w, h, NULL, alpha, 
911                                           EVAS_COLORSPACE_ARGB8888);
912    evas_cache_image_pixels(surface);
913    return surface;
914 }
915
916 static void
917 eng_image_map_surface_free(void *data __UNUSED__, void *surface)
918 {
919    evas_cache_image_drop(surface);
920 }
921
922 static void
923 eng_image_scale_hint_set(void *data __UNUSED__, void *image, int hint)
924 {
925    Image_Entry *im;
926
927    if (!image) return;
928    im = image;
929    im->scale_hint = hint;
930 }
931
932 static int
933 eng_image_scale_hint_get(void *data __UNUSED__, void *image)
934 {
935    Image_Entry *im;
936
937    if (!image) return EVAS_IMAGE_SCALE_HINT_NONE;
938    im = image;
939    return im->scale_hint;
940 }
941
942 static Eina_Bool
943 eng_image_animated_get(void *data __UNUSED__, void *image)
944 {
945    Image_Entry *im;
946
947    if (!image) return EINA_FALSE;
948    im = image;
949    return im->flags.animated;
950 }
951
952 static int
953 eng_image_animated_frame_count_get(void *data __UNUSED__, void *image)
954 {
955    Image_Entry *im;
956
957    if (!image) return -1;
958    im = image;
959    if (!im->flags.animated) return -1;
960    return im->frame_count;
961 }
962
963 static Evas_Image_Animated_Loop_Hint
964 eng_image_animated_loop_type_get(void *data __UNUSED__, void *image)
965 {
966    Image_Entry *im;
967
968    if (!image) return EVAS_IMAGE_ANIMATED_HINT_NONE;
969    im = image;
970    if (!im->flags.animated) return EVAS_IMAGE_ANIMATED_HINT_NONE;
971    return im->loop_hint;
972 }
973
974 static int
975 eng_image_animated_loop_count_get(void *data __UNUSED__, void *image)
976 {
977    Image_Entry *im;
978
979    if (!image) return -1;
980    im = image;
981    if (!im->flags.animated) return -1;
982    return im->loop_count;
983 }
984
985 static double
986 eng_image_animated_frame_duration_get(void *data __UNUSED__, void *image, int start_frame, int frame_num)
987 {
988    Image_Entry *im;
989
990    if (!image) return -1;
991    im = image;
992    if (!im->flags.animated) return -1;
993    return evas_common_load_rgba_image_frame_duration_from_file(im, start_frame, frame_num);
994 }
995
996 static Eina_Bool
997 eng_image_animated_frame_set(void *data __UNUSED__, void *image, int frame_index)
998 {
999    Image_Entry *im;
1000
1001    if (!image) return EINA_FALSE;
1002    im = image;
1003    if (!im->flags.animated) return EINA_FALSE;
1004    if (im->cur_frame == frame_index) return EINA_FALSE;
1005    im->cur_frame = frame_index;
1006    return EINA_TRUE;
1007 }
1008
1009 static void
1010 eng_image_cache_flush(void *data __UNUSED__)
1011 {
1012    int tmp_size;
1013
1014    tmp_size = evas_common_image_get_cache();
1015    evas_common_image_set_cache(0);
1016    evas_common_rgba_image_scalecache_flush();
1017    evas_common_image_set_cache(tmp_size);
1018 }
1019
1020 static void
1021 eng_image_cache_set(void *data __UNUSED__, int bytes)
1022 {
1023    evas_common_image_set_cache(bytes);
1024    evas_common_rgba_image_scalecache_size_set(bytes);
1025 }
1026
1027 static int
1028 eng_image_cache_get(void *data __UNUSED__)
1029 {
1030    return evas_common_image_get_cache();
1031 }
1032
1033 static Evas_Font_Set *
1034 eng_font_load(void *data __UNUSED__, const char *name, int size,
1035       Font_Rend_Flags wanted_rend)
1036 {
1037    return (Evas_Font_Set *) evas_common_font_load(name, size, wanted_rend);
1038 }
1039
1040 static Evas_Font_Set *
1041 eng_font_memory_load(void *data __UNUSED__, char *name, int size, const void *fdata, int fdata_size, Font_Rend_Flags wanted_rend)
1042 {
1043    return (Evas_Font_Set *) evas_common_font_memory_load(name, size, fdata,
1044          fdata_size, wanted_rend);
1045 }
1046
1047 static Evas_Font_Set *
1048 eng_font_add(void *data __UNUSED__, Evas_Font_Set *font, const char *name, int size, Font_Rend_Flags wanted_rend)
1049 {
1050    return (Evas_Font_Set *) evas_common_font_add((RGBA_Font *) font, name,
1051          size, wanted_rend);
1052 }
1053
1054 static Evas_Font_Set *
1055 eng_font_memory_add(void *data __UNUSED__, Evas_Font_Set *font, char *name, int size, const void *fdata, int fdata_size, Font_Rend_Flags wanted_rend)
1056 {
1057    return (Evas_Font_Set *) evas_common_font_memory_add((RGBA_Font *) font,
1058          name, size, fdata, fdata_size, wanted_rend);
1059 }
1060
1061 static void
1062 eng_font_free(void *data __UNUSED__, Evas_Font_Set *font)
1063 {
1064    evas_common_font_free((RGBA_Font *) font);
1065 }
1066
1067 static int
1068 eng_font_ascent_get(void *data __UNUSED__, Evas_Font_Set *font)
1069 {
1070    return evas_common_font_ascent_get((RGBA_Font *) font);
1071 }
1072
1073 static int
1074 eng_font_descent_get(void *data __UNUSED__, Evas_Font_Set *font)
1075 {
1076    return evas_common_font_descent_get((RGBA_Font *) font);
1077 }
1078
1079 static int
1080 eng_font_max_ascent_get(void *data __UNUSED__, Evas_Font_Set *font)
1081 {
1082    return evas_common_font_max_ascent_get((RGBA_Font *) font);
1083 }
1084
1085 static int
1086 eng_font_max_descent_get(void *data __UNUSED__, Evas_Font_Set *font)
1087 {
1088    return evas_common_font_max_descent_get((RGBA_Font *) font);
1089 }
1090
1091 static void
1092 eng_font_string_size_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props, int *w, int *h)
1093 {
1094    evas_common_font_query_size((RGBA_Font *) font, text_props, w, h);
1095 }
1096
1097 static int
1098 eng_font_inset_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props)
1099 {
1100    return evas_common_font_query_inset((RGBA_Font *) font, text_props);
1101 }
1102
1103 static int
1104 eng_font_right_inset_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props)
1105 {
1106    return evas_common_font_query_right_inset((RGBA_Font *) font, text_props);
1107 }
1108
1109 static int
1110 eng_font_h_advance_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props)
1111 {
1112    int h, v;
1113
1114    evas_common_font_query_advance((RGBA_Font *) font, text_props, &h, &v);
1115    return h;
1116 }
1117
1118 static int
1119 eng_font_v_advance_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props)
1120 {
1121    int h, v;
1122
1123    evas_common_font_query_advance((RGBA_Font *) font, text_props, &h, &v);
1124    return v;
1125 }
1126
1127 static int
1128 eng_font_pen_coords_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props, int pos, int *cpen_x, int *cy, int *cadv, int *ch)
1129 {
1130    return evas_common_font_query_pen_coords((RGBA_Font *) font, text_props, pos, cpen_x, cy, cadv, ch);
1131 }
1132
1133 static Eina_Bool
1134 eng_font_text_props_info_create(void *data __UNUSED__, Evas_Font_Instance *fi, const Eina_Unicode *text, Evas_Text_Props *text_props, const Evas_BiDi_Paragraph_Props *par_props, size_t par_pos, size_t len)
1135 {
1136    return evas_common_text_props_content_create((RGBA_Font_Int *) fi, text,
1137          text_props, par_props, par_pos, len);
1138 }
1139
1140 static int
1141 eng_font_char_coords_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props, int pos, int *cx, int *cy, int *cw, int *ch)
1142 {
1143    return evas_common_font_query_char_coords((RGBA_Font *) font, text_props, pos, cx, cy, cw, ch);
1144 }
1145
1146 static int
1147 eng_font_char_at_coords_get(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props, int x, int y, int *cx, int *cy, int *cw, int *ch)
1148 {
1149    return evas_common_font_query_char_at_coords((RGBA_Font *) font, text_props, x, y, cx, cy, cw, ch);
1150 }
1151
1152 static int
1153 eng_font_last_up_to_pos(void *data __UNUSED__, Evas_Font_Set *font, const Evas_Text_Props *text_props, int x, int y)
1154 {
1155    return evas_common_font_query_last_up_to_pos((RGBA_Font *) font, text_props, x, y);
1156 }
1157
1158 static int
1159 eng_font_run_font_end_get(void *data __UNUSED__, Evas_Font_Set *font, Evas_Font_Instance **script_fi, Evas_Font_Instance **cur_fi, Evas_Script_Type script, const Eina_Unicode *text, int run_len)
1160 {
1161    return evas_common_font_query_run_font_end_get((RGBA_Font *) font,
1162          (RGBA_Font_Int **) script_fi, (RGBA_Font_Int **) cur_fi,
1163          script, text, run_len);
1164 }
1165
1166 static void
1167 eng_font_draw(void *data __UNUSED__, void *context, void *surface, Evas_Font_Set *font, int x, int y, int w __UNUSED__, int h __UNUSED__, int ow __UNUSED__, int oh __UNUSED__, const Evas_Text_Props *text_props)
1168 {
1169 #ifdef BUILD_PIPE_RENDER
1170    if ((cpunum > 1))
1171      evas_common_pipe_text_draw(surface, context, (RGBA_Font *) font, x, y,
1172            text_props);
1173    else
1174 #endif   
1175      {
1176         evas_common_font_draw(surface, context, (RGBA_Font *) font, x, y,
1177               text_props);
1178         evas_common_cpu_end_opt();
1179      }
1180 }
1181
1182 static void
1183 eng_font_cache_flush(void *data __UNUSED__)
1184 {
1185    int tmp_size;
1186
1187    tmp_size = evas_common_font_cache_get();
1188    evas_common_font_cache_set(0);
1189    evas_common_font_flush();
1190    evas_common_font_cache_set(tmp_size);
1191 }
1192
1193 static void
1194 eng_font_cache_set(void *data __UNUSED__, int bytes)
1195 {
1196    evas_common_font_cache_set(bytes);
1197 }
1198
1199 static int
1200 eng_font_cache_get(void *data __UNUSED__)
1201 {
1202    return evas_common_font_cache_get();
1203 }
1204
1205 static void
1206 eng_font_hinting_set(void *data __UNUSED__, Evas_Font_Set *font, int hinting)
1207 {
1208    evas_common_font_hinting_set((RGBA_Font *) font, hinting);
1209 }
1210
1211 static int
1212 eng_font_hinting_can_hint(void *data __UNUSED__, int hinting)
1213 {
1214    return evas_common_hinting_available(hinting);
1215 }
1216
1217 static Eina_Bool
1218 eng_canvas_alpha_get(void *data __UNUSED__, void *info __UNUSED__)
1219 {
1220    return EINA_TRUE;
1221 }
1222
1223
1224 /* Filter API */
1225 #if 0 // filtering disabled
1226 static void
1227 eng_image_draw_filtered(void *data __UNUSED__, void *context __UNUSED__,
1228                         void *surface, void *image, Evas_Filter_Info *filter)
1229 {
1230    Evas_Software_Filter_Fn fn;
1231    RGBA_Image *im = image;
1232
1233    fn = evas_filter_software_get(filter);
1234    if (!fn) return;
1235    if (im->cache_entry.cache) evas_cache_image_load_data(&im->cache_entry);
1236    fn(filter, image, surface);
1237    return;
1238 }
1239
1240 static Filtered_Image *
1241 eng_image_filtered_get(void *image, uint8_t *key, size_t keylen)
1242 {
1243    RGBA_Image *im = image;
1244    Filtered_Image *fi;
1245    Eina_List *l;
1246
1247    for (l = im->filtered ; l ; l = l->next)
1248      {
1249          fi = l->data;
1250          if (fi->keylen != keylen) continue;
1251          if (memcmp(key, fi->key, keylen) != 0) continue;
1252          fi->ref ++;
1253          return fi;
1254      }
1255
1256    return NULL;
1257 }
1258
1259 static Filtered_Image *
1260 eng_image_filtered_save(void *image, void *fimage, uint8_t *key, size_t keylen)
1261 {
1262    RGBA_Image *im = image;
1263    Filtered_Image *fi;
1264    Eina_List *l;
1265
1266    for (l = im->filtered ; l ; l = l->next)
1267      {
1268         fi = l->data;
1269         if (fi->keylen != keylen) continue;
1270         if (memcmp(key, fi->key, keylen) == 0) continue;
1271         evas_cache_image_drop((void *)fi->image);
1272         fi->image = fimage;
1273         return fi;
1274      }
1275
1276    fi = calloc(1,sizeof(Filtered_Image));
1277    if (!fi) return NULL;
1278
1279    fi->keylen = keylen;
1280    fi->key = malloc(keylen);
1281    memcpy(fi->key, key, keylen);
1282    fi->image = fimage;
1283    fi->ref = 1;
1284
1285    im->filtered = eina_list_prepend(im->filtered, fi);
1286
1287    return fi;
1288 }
1289
1290 static void
1291 eng_image_filtered_free(void *image, Filtered_Image *fi)
1292 {
1293    RGBA_Image *im = image;
1294
1295    fi->ref --;
1296    if (fi->ref) return;
1297
1298    free(fi->key);
1299    evas_cache_image_drop(&fi->image->cache_entry);
1300    fi->image = NULL;
1301
1302    im->filtered = eina_list_remove(im->filtered, fi);
1303 }
1304 #endif
1305
1306 static int
1307 eng_image_load_error_get(void *data __UNUSED__, void *image)
1308 {
1309    RGBA_Image *im;
1310    
1311    if (!image) return EVAS_LOAD_ERROR_NONE;
1312    im = image;
1313    return im->cache_entry.load_error;
1314 }
1315
1316 //------------ Evas GL engine code ---------------//
1317 static void *
1318 eng_gl_surface_create(void *data __UNUSED__, void *config, int w, int h)
1319 {
1320 #ifdef EVAS_GL
1321    Render_Engine_GL_Surface *sfc;
1322    Evas_GL_Config *cfg;
1323
1324    sfc = calloc(1, sizeof(Render_Engine_GL_Surface));
1325    if (!sfc) return NULL;
1326
1327    cfg = (Evas_GL_Config *)config;
1328
1329    sfc->initialized  = 0;
1330    sfc->w            = w;
1331    sfc->h            = h;
1332
1333    // Color Format
1334    switch (cfg->color_format)
1335      {
1336
1337       case EVAS_GL_RGB_888:
1338          sfc->internal_fmt = OSMESA_RGB;
1339          sfc->internal_cpp = 3;
1340          break;
1341       case EVAS_GL_RGBA_8888:
1342          sfc->internal_fmt = OSMESA_BGRA;
1343          sfc->internal_cpp = 4;
1344          break;
1345       default:
1346          sfc->internal_fmt = OSMESA_RGBA;
1347          sfc->internal_cpp = 4;
1348          break;
1349      }
1350
1351    // Depth Bits
1352    switch (cfg->depth_bits)
1353      {
1354       case EVAS_GL_DEPTH_BIT_8:
1355          sfc->depth_bits = 8;
1356          break;
1357       case EVAS_GL_DEPTH_BIT_16:
1358          sfc->depth_bits = 16;
1359          break;
1360       case EVAS_GL_DEPTH_BIT_24:
1361          sfc->depth_bits = 24;
1362          break;
1363       case EVAS_GL_DEPTH_BIT_32:
1364          sfc->depth_bits = 32;
1365          break;
1366       case EVAS_GL_DEPTH_NONE:
1367       default:
1368          sfc->depth_bits = 0;
1369          break;
1370      }
1371    
1372    // Stencil Bits
1373    switch (cfg->stencil_bits)
1374      {
1375       case EVAS_GL_STENCIL_BIT_1:
1376          sfc->stencil_bits = 1;
1377          break;
1378       case EVAS_GL_STENCIL_BIT_2:
1379          sfc->stencil_bits = 2;
1380          break;
1381       case EVAS_GL_STENCIL_BIT_4:
1382          sfc->stencil_bits = 4;
1383          break;
1384       case EVAS_GL_STENCIL_BIT_8:
1385          sfc->stencil_bits = 8;
1386          break;
1387       case EVAS_GL_STENCIL_BIT_16:
1388          sfc->stencil_bits = 16;
1389          break;
1390       case EVAS_GL_STENCIL_NONE:
1391       default:
1392          sfc->stencil_bits = 0;
1393          break;
1394      }
1395
1396    sfc->buffer = malloc(sizeof(unsigned char)*sfc->internal_cpp*w*h);
1397
1398    if (!sfc->buffer)
1399      {
1400         free(sfc);
1401         return NULL;
1402      }
1403
1404    return sfc;
1405 #else
1406    (void) config;
1407    (void) w;
1408    (void) h;
1409    return NULL;
1410 #endif
1411 }
1412
1413 static int
1414 eng_gl_surface_destroy(void *data __UNUSED__, void *surface)
1415 {
1416 #ifdef EVAS_GL
1417    Render_Engine_GL_Surface *sfc;
1418
1419    sfc = (Render_Engine_GL_Surface*)surface;
1420
1421    if (!sfc) return 0;
1422
1423    if (sfc->buffer) free(sfc->buffer);
1424
1425    free(sfc);
1426
1427    surface = NULL;
1428
1429    return 1;
1430 #else
1431    (void) surface;
1432    return 1;
1433 #endif
1434 }
1435
1436 static void *
1437 eng_gl_context_create(void *data __UNUSED__, void *share_context)
1438 {
1439 #ifdef EVAS_GL
1440    Render_Engine_GL_Context *ctx;
1441    Render_Engine_GL_Context *share_ctx;
1442
1443    ctx = calloc(1, sizeof(Render_Engine_GL_Context));
1444
1445    if (!ctx) return NULL;
1446
1447    share_ctx = (Render_Engine_GL_Context *)share_context;
1448
1449    ctx->share_ctx = share_ctx;
1450
1451    /*
1452    if (share_ctx)
1453       ctx->context = OSMesaCreateContextExt( OSMESA_RGBA, 8, 0, 0, share_ctx->context );
1454    else
1455       ctx->context = OSMesaCreateContextExt( OSMESA_RGBA, 8, 0, 0, NULL );
1456
1457
1458    if (!ctx->context)
1459      {
1460         ERR("Error creating OSMesa Context.");
1461         free(ctx);
1462         return NULL;
1463      }
1464      */
1465
1466    ctx->initialized = 0;
1467
1468    return ctx;
1469 #else
1470    (void) share_context;
1471    return NULL;
1472 #endif
1473 }
1474
1475 static int
1476 eng_gl_context_destroy(void *data __UNUSED__, void *context)
1477 {
1478 #ifdef EVAS_GL
1479    Render_Engine_GL_Context *ctx;
1480
1481    ctx = (Render_Engine_GL_Context*)context;
1482
1483    if (!ctx) return 0;
1484
1485    _sym_OSMesaDestroyContext(ctx->context);
1486
1487    free(ctx);
1488    context = NULL;
1489
1490    return 1;
1491 #else
1492    (void) context;
1493    return 0;
1494 #endif
1495 }
1496
1497 static int
1498 eng_gl_make_current(void *data __UNUSED__, void *surface, void *context)
1499 {
1500 #ifdef EVAS_GL
1501    Render_Engine_GL_Surface *sfc;
1502    Render_Engine_GL_Context *ctx;
1503    OSMesaContext share_ctx;
1504    GLboolean ret;
1505
1506    sfc = (Render_Engine_GL_Surface*)surface;
1507    ctx = (Render_Engine_GL_Context*)context;
1508
1509    // Unset surface/context
1510    if ((!sfc) || (!ctx))
1511      {
1512         if (ctx) ctx->current_sfc = NULL;
1513         if (sfc) sfc->current_ctx = NULL;
1514         return 1;
1515      }
1516
1517    // Initialize Context if it hasn't been.
1518    if (!ctx->initialized)
1519      {
1520         if (ctx->share_ctx)
1521            share_ctx = ctx->share_ctx->context;
1522         else
1523            share_ctx = NULL;
1524
1525         ctx->context =  _sym_OSMesaCreateContextExt(sfc->internal_fmt, 
1526                                                sfc->depth_bits,
1527                                                sfc->stencil_bits, 
1528                                                0,
1529                                                share_ctx);
1530         if (!ctx->context)
1531           {
1532              ERR("Error initializing context.");
1533              return 0;
1534           }
1535
1536         ctx->initialized = 1;
1537      }
1538
1539
1540    // Call MakeCurrent
1541    ret = _sym_OSMesaMakeCurrent(ctx->context, sfc->buffer, GL_UNSIGNED_BYTE, 
1542                            sfc->w, sfc->h);
1543
1544    if (ret == GL_FALSE)
1545      {
1546         ERR("Error doing MakeCurrent.");
1547         return 0;
1548      }
1549
1550    _sym_OSMesaPixelStore(OSMESA_Y_UP, 0);
1551
1552    // Set the current surface/context
1553    ctx->current_sfc = sfc;
1554    sfc->current_ctx = ctx;
1555
1556    return 1;
1557 #else
1558    (void) surface;
1559    (void) context;
1560    return 1;
1561 #endif
1562 }
1563
1564 // FIXME!!! Implement later
1565 static void *
1566 eng_gl_string_query(void *data __UNUSED__, int name __UNUSED__)
1567 {
1568    return NULL;
1569 }
1570
1571 static void *
1572 eng_gl_proc_address_get(void *data __UNUSED__, const char *name)
1573 {
1574 #ifdef EVAS_GL
1575    if (_sym_OSMesaGetProcAddress) return _sym_OSMesaGetProcAddress(name);
1576    return dlsym(RTLD_DEFAULT, name);
1577 #else
1578    (void) name;
1579    return NULL;
1580 #endif
1581 }
1582
1583 static int
1584 eng_gl_native_surface_get(void *data __UNUSED__, void *surface, void *native_surface)
1585 {
1586 #ifdef EVAS_GL
1587    Render_Engine_GL_Surface *sfc;
1588    Evas_Native_Surface *ns;
1589
1590    sfc = (Render_Engine_GL_Surface*)surface;
1591    ns  = (Evas_Native_Surface*)native_surface;
1592
1593    if (!sfc) return 0;
1594
1595    ns->type = EVAS_NATIVE_SURFACE_OPENGL;
1596    ns->version = EVAS_NATIVE_SURFACE_VERSION;
1597    ns->data.x11.visual = sfc->buffer;
1598
1599    return 1;
1600 #else
1601    (void) surface;
1602    (void) native_surface;
1603    return 1;
1604 #endif
1605 }
1606
1607
1608 static void *
1609 eng_gl_api_get(void *data __UNUSED__)
1610 {
1611 #ifdef EVAS_GL
1612    return &gl_funcs;
1613 #else
1614    return NULL;
1615 #endif
1616 }
1617
1618 //------------------------------------------------//
1619
1620
1621 /*
1622  *****
1623  **
1624  ** ENGINE API
1625  **
1626  *****
1627  */
1628
1629 static Evas_Func func =
1630 {
1631    NULL,
1632      NULL,
1633      NULL,
1634      NULL,
1635      NULL,
1636      NULL,
1637      NULL,
1638      NULL,
1639      NULL,
1640      NULL,
1641      NULL,
1642      NULL,
1643      NULL,
1644      eng_output_dump,
1645      /* draw context virtual methods */
1646      eng_context_new,
1647      eng_canvas_alpha_get,
1648      eng_context_free,
1649      eng_context_clip_set,
1650      eng_context_clip_clip,
1651      eng_context_clip_unset,
1652      eng_context_clip_get,
1653      eng_context_mask_set,
1654      eng_context_mask_unset,
1655      eng_context_color_set,
1656      eng_context_color_get,
1657      eng_context_multiplier_set,
1658      eng_context_multiplier_unset,
1659      eng_context_multiplier_get,
1660      eng_context_cutout_add,
1661      eng_context_cutout_clear,
1662      eng_context_anti_alias_set,
1663      eng_context_anti_alias_get,
1664      eng_context_color_interpolation_set,
1665      eng_context_color_interpolation_get,
1666      eng_context_render_op_set,
1667      eng_context_render_op_get,
1668      /* rect draw funcs */
1669      eng_rectangle_draw,
1670      /* line draw funcs */
1671      eng_line_draw,
1672      /* polygon draw funcs */
1673      eng_polygon_point_add,
1674      eng_polygon_points_clear,
1675      eng_polygon_draw,
1676      /* image draw funcs */
1677      eng_image_load,
1678      eng_image_new_from_data,
1679      eng_image_new_from_copied_data,
1680      eng_image_free,
1681      eng_image_size_get,
1682      eng_image_size_set,
1683      NULL,
1684      eng_image_dirty_region,
1685      eng_image_data_get,
1686      eng_image_data_put,
1687      eng_image_data_preload_request,
1688      eng_image_data_preload_cancel,
1689      eng_image_alpha_set,
1690      eng_image_alpha_get,
1691      eng_image_border_set,
1692      eng_image_border_get,
1693      eng_image_draw,
1694      eng_image_comment_get,
1695      eng_image_format_get,
1696      eng_image_colorspace_set,
1697      eng_image_colorspace_get,
1698      eng_image_can_region_get,
1699      eng_image_mask_create,
1700      eng_image_native_set,
1701      eng_image_native_get,
1702      /* image cache funcs */
1703      eng_image_cache_flush,
1704      eng_image_cache_set,
1705      eng_image_cache_get,
1706      /* font draw functions */
1707      eng_font_load,
1708      eng_font_memory_load,
1709      eng_font_add,
1710      eng_font_memory_add,
1711      eng_font_free,
1712      eng_font_ascent_get,
1713      eng_font_descent_get,
1714      eng_font_max_ascent_get,
1715      eng_font_max_descent_get,
1716      eng_font_string_size_get,
1717      eng_font_inset_get,
1718      eng_font_h_advance_get,
1719      eng_font_v_advance_get,
1720      eng_font_char_coords_get,
1721      eng_font_char_at_coords_get,
1722      eng_font_draw,
1723      /* font cache functions */
1724      eng_font_cache_flush,
1725      eng_font_cache_set,
1726      eng_font_cache_get,
1727      /* font hinting functions */
1728      eng_font_hinting_set,
1729      eng_font_hinting_can_hint,
1730      eng_image_scale_hint_set,
1731      eng_image_scale_hint_get,
1732      /* more font draw functions */
1733      eng_font_last_up_to_pos,
1734      eng_image_map_draw,
1735      eng_image_map_surface_new,
1736      eng_image_map_surface_free,
1737      NULL, // eng_image_content_hint_set - software doesn't use it
1738      NULL, // eng_image_content_hint_get - software doesn't use it
1739      eng_font_pen_coords_get,
1740      eng_font_text_props_info_create,
1741      eng_font_right_inset_get,
1742 #if 0 // filtering disabled
1743      eng_image_draw_filtered,
1744      eng_image_filtered_get,
1745      eng_image_filtered_save,
1746      eng_image_filtered_free,
1747 #endif   
1748      NULL, // need software mesa for gl rendering <- gl_surface_create
1749      NULL, // need software mesa for gl rendering <- gl_surface_destroy
1750      NULL, // need software mesa for gl rendering <- gl_context_create
1751      NULL, // need software mesa for gl rendering <- gl_context_destroy
1752      NULL, // need software mesa for gl rendering <- gl_make_current
1753      NULL, // need software mesa for gl rendering <- gl_string_query
1754      NULL, // need software mesa for gl rendering <- gl_proc_address_get
1755      NULL, // need software mesa for gl rendering <- gl_native_surface_get
1756      NULL, // need software mesa for gl rendering <- gl_api_get
1757      NULL, // need software mesa for gl rendering <- gl_img_obj_set
1758      eng_image_load_error_get,
1759      eng_font_run_font_end_get,
1760      eng_image_animated_get,
1761      eng_image_animated_frame_count_get,
1762      eng_image_animated_loop_type_get,
1763      eng_image_animated_loop_count_get,
1764      eng_image_animated_frame_duration_get,
1765      eng_image_animated_frame_set,
1766      NULL
1767    /* FUTURE software generic calls go here */
1768 };
1769
1770
1771 //----------------------------------------------------------------//
1772 //                                                                //
1773 //                      Load Symbols                              //
1774 //                                                                //
1775 //----------------------------------------------------------------//
1776 #ifdef EVAS_GL
1777 static void
1778 sym_missing(void)
1779 {
1780    ERR("GL symbols missing!\n");
1781 }
1782
1783 static int
1784 glue_sym_init(void)
1785 {
1786    //------------------------------------------------//
1787    // Use eglGetProcAddress
1788 #define FINDSYM(dst, sym, typ) \
1789    if (!dst) dst = (typeof(dst))dlsym(gl_lib_handle, sym); \
1790    if (!dst)  \
1791      { \
1792         ERR("Symbol not found %s\n", sym); \
1793         return 0; \
1794      }
1795 #define FALLBAK(dst, typ) if (!dst) dst = (typeof(dst))sym_missing;
1796
1797     //------------------------------------------------------//
1798    // OSMesa APIs...
1799    FINDSYM(_sym_OSMesaCreateContextExt, "OSMesaCreateContextExt", glsym_func_osm_ctx);
1800    FALLBAK(_sym_OSMesaCreateContextExt, glsym_func_void);
1801
1802    FINDSYM(_sym_OSMesaDestroyContext, "OSMesaDestroyContext", glsym_func_void);
1803    FALLBAK(_sym_OSMesaDestroyContext, glsym_func_void);
1804
1805    FINDSYM(_sym_OSMesaMakeCurrent, "OSMesaMakeCurrent", glsym_func_bool);
1806    FALLBAK(_sym_OSMesaMakeCurrent, glsym_func_void);
1807
1808    FINDSYM(_sym_OSMesaPixelStore, "OSMesaPixelStore", glsym_func_void);
1809    FALLBAK(_sym_OSMesaPixelStore, glsym_func_void);
1810
1811    FINDSYM(_sym_OSMesaGetProcAddress, "OSMesaGetProcAddress", glsym_func_eng_fn);
1812    FALLBAK(_sym_OSMesaGetProcAddress, glsym_func_void);
1813
1814 #undef FINDSYM
1815 #undef FALLBAK
1816
1817    return 1;
1818 }
1819
1820 static int
1821 gl_sym_init(void)
1822 {
1823    //------------------------------------------------//
1824 #define FINDSYM(dst, sym, typ) \
1825    if (!dst) dst = (typeof(dst))dlsym(gl_lib_handle, sym); \
1826    if (!dst) DBG("Symbol not found %s\n", sym);
1827 #define FALLBAK(dst, typ) if (!dst) dst = (typeof(dst))sym_missing;
1828
1829
1830    //------------------------------------------------------//
1831    // GLES 2.0 APIs...
1832    FINDSYM(_sym_glActiveTexture, "glActiveTexture", glsym_func_void);
1833    FALLBAK(_sym_glActiveTexture, glsym_func_void);
1834
1835    FINDSYM(_sym_glAttachShader, "glAttachShader", glsym_func_void);
1836    FALLBAK(_sym_glAttachShader, glsym_func_void);
1837
1838    FINDSYM(_sym_glBindAttribLocation, "glBindAttribLocation", glsym_func_void);
1839    FALLBAK(_sym_glBindAttribLocation, glsym_func_void);
1840
1841    FINDSYM(_sym_glBindBuffer, "glBindBuffer", glsym_func_void);
1842    FALLBAK(_sym_glBindBuffer, glsym_func_void);
1843
1844    FINDSYM(_sym_glBindFramebuffer, "glBindFramebuffer", glsym_func_void);
1845    FALLBAK(_sym_glBindFramebuffer, glsym_func_void);
1846
1847    FINDSYM(_sym_glBindRenderbuffer, "glBindRenderbuffer", glsym_func_void);
1848    FALLBAK(_sym_glBindRenderbuffer, glsym_func_void);
1849
1850    FINDSYM(_sym_glBindTexture, "glBindTexture", glsym_func_void);
1851    FALLBAK(_sym_glBindTexture, glsym_func_void);
1852
1853    FINDSYM(_sym_glBlendColor, "glBlendColor", glsym_func_void);
1854    FALLBAK(_sym_glBlendColor, glsym_func_void);
1855
1856    FINDSYM(_sym_glBlendEquation, "glBlendEquation", glsym_func_void);
1857    FALLBAK(_sym_glBlendEquation, glsym_func_void);
1858
1859    FINDSYM(_sym_glBlendEquationSeparate, "glBlendEquationSeparate", glsym_func_void);
1860    FALLBAK(_sym_glBlendEquationSeparate, glsym_func_void);
1861
1862    FINDSYM(_sym_glBlendFunc, "glBlendFunc", glsym_func_void);
1863    FALLBAK(_sym_glBlendFunc, glsym_func_void);
1864
1865    FINDSYM(_sym_glBlendFuncSeparate, "glBlendFuncSeparate", glsym_func_void);
1866    FALLBAK(_sym_glBlendFuncSeparate, glsym_func_void);
1867
1868    FINDSYM(_sym_glBufferData, "glBufferData", glsym_func_void);
1869    FALLBAK(_sym_glBufferData, glsym_func_void);
1870
1871    FINDSYM(_sym_glBufferSubData, "glBufferSubData", glsym_func_void);
1872    FALLBAK(_sym_glBufferSubData, glsym_func_void);
1873
1874    FINDSYM(_sym_glCheckFramebufferStatus, "glCheckFramebufferStatus", glsym_func_uint);
1875    FALLBAK(_sym_glCheckFramebufferStatus, glsym_func_uint);
1876
1877    FINDSYM(_sym_glClear, "glClear", glsym_func_void);
1878    FALLBAK(_sym_glClear, glsym_func_void);
1879
1880    FINDSYM(_sym_glClearColor, "glClearColor", glsym_func_void);
1881    FALLBAK(_sym_glClearColor, glsym_func_void);
1882
1883    FINDSYM(_sym_glClearDepthf, "glClearDepthf", glsym_func_void);
1884    FINDSYM(_sym_glClearDepthf, "glClearDepth", glsym_func_void);
1885    FALLBAK(_sym_glClearDepthf, glsym_func_void);
1886
1887    FINDSYM(_sym_glClearStencil, "glClearStencil", glsym_func_void);
1888    FALLBAK(_sym_glClearStencil, glsym_func_void);
1889
1890    FINDSYM(_sym_glColorMask, "glColorMask", glsym_func_void);
1891    FALLBAK(_sym_glColorMask, glsym_func_void);
1892
1893    FINDSYM(_sym_glCompileShader, "glCompileShader", glsym_func_void);
1894    FALLBAK(_sym_glCompileShader, glsym_func_void);
1895
1896    FINDSYM(_sym_glCompressedTexImage2D, "glCompressedTexImage2D", glsym_func_void);
1897    FALLBAK(_sym_glCompressedTexImage2D, glsym_func_void);
1898
1899    FINDSYM(_sym_glCompressedTexSubImage2D, "glCompressedTexSubImage2D", glsym_func_void);
1900    FALLBAK(_sym_glCompressedTexSubImage2D, glsym_func_void);
1901
1902    FINDSYM(_sym_glCopyTexImage2D, "glCopyTexImage2D", glsym_func_void);
1903    FALLBAK(_sym_glCopyTexImage2D, glsym_func_void);
1904
1905    FINDSYM(_sym_glCopyTexSubImage2D, "glCopyTexSubImage2D", glsym_func_void);
1906    FALLBAK(_sym_glCopyTexSubImage2D, glsym_func_void);
1907
1908    FINDSYM(_sym_glCreateProgram, "glCreateProgram", glsym_func_uint);
1909    FALLBAK(_sym_glCreateProgram, glsym_func_uint);
1910
1911    FINDSYM(_sym_glCreateShader, "glCreateShader", glsym_func_uint);
1912    FALLBAK(_sym_glCreateShader, glsym_func_uint);
1913
1914    FINDSYM(_sym_glCullFace, "glCullFace", glsym_func_void);
1915    FALLBAK(_sym_glCullFace, glsym_func_void);
1916
1917    FINDSYM(_sym_glDeleteBuffers, "glDeleteBuffers", glsym_func_void);
1918    FALLBAK(_sym_glDeleteBuffers, glsym_func_void);
1919
1920    FINDSYM(_sym_glDeleteFramebuffers, "glDeleteFramebuffers", glsym_func_void);
1921    FALLBAK(_sym_glDeleteFramebuffers, glsym_func_void);
1922
1923    FINDSYM(_sym_glDeleteProgram, "glDeleteProgram", glsym_func_void);
1924    FALLBAK(_sym_glDeleteProgram, glsym_func_void);
1925
1926    FINDSYM(_sym_glDeleteRenderbuffers, "glDeleteRenderbuffers", glsym_func_void);
1927    FALLBAK(_sym_glDeleteRenderbuffers, glsym_func_void);
1928
1929    FINDSYM(_sym_glDeleteShader, "glDeleteShader", glsym_func_void);
1930    FALLBAK(_sym_glDeleteShader, glsym_func_void);
1931
1932    FINDSYM(_sym_glDeleteTextures, "glDeleteTextures", glsym_func_void);
1933    FALLBAK(_sym_glDeleteTextures, glsym_func_void);
1934
1935    FINDSYM(_sym_glDepthFunc, "glDepthFunc", glsym_func_void);
1936    FALLBAK(_sym_glDepthFunc, glsym_func_void);
1937
1938    FINDSYM(_sym_glDepthMask, "glDepthMask", glsym_func_void);
1939    FALLBAK(_sym_glDepthMask, glsym_func_void);
1940
1941    FINDSYM(_sym_glDepthRangef, "glDepthRangef", glsym_func_void);
1942    FINDSYM(_sym_glDepthRangef, "glDepthRange", glsym_func_void);
1943    FALLBAK(_sym_glDepthRangef, glsym_func_void);
1944
1945    FINDSYM(_sym_glDetachShader, "glDetachShader", glsym_func_void);
1946    FALLBAK(_sym_glDetachShader, glsym_func_void);
1947
1948    FINDSYM(_sym_glDisable, "glDisable", glsym_func_void);
1949    FALLBAK(_sym_glDisable, glsym_func_void);
1950
1951    FINDSYM(_sym_glDisableVertexAttribArray, "glDisableVertexAttribArray", glsym_func_void);
1952    FALLBAK(_sym_glDisableVertexAttribArray, glsym_func_void);
1953
1954    FINDSYM(_sym_glDrawArrays, "glDrawArrays", glsym_func_void);
1955    FALLBAK(_sym_glDrawArrays, glsym_func_void);
1956
1957    FINDSYM(_sym_glDrawElements, "glDrawElements", glsym_func_void);
1958    FALLBAK(_sym_glDrawElements, glsym_func_void);
1959
1960    FINDSYM(_sym_glEnable, "glEnable", glsym_func_void);
1961    FALLBAK(_sym_glEnable, glsym_func_void);
1962
1963    FINDSYM(_sym_glEnableVertexAttribArray, "glEnableVertexAttribArray", glsym_func_void);
1964    FALLBAK(_sym_glEnableVertexAttribArray, glsym_func_void);
1965
1966    FINDSYM(_sym_glFinish, "glFinish", glsym_func_void);
1967    FALLBAK(_sym_glFinish, glsym_func_void);
1968
1969    FINDSYM(_sym_glFlush, "glFlush", glsym_func_void);
1970    FALLBAK(_sym_glFlush, glsym_func_void);
1971
1972    FINDSYM(_sym_glFramebufferRenderbuffer, "glFramebufferRenderbuffer", glsym_func_void);
1973    FALLBAK(_sym_glFramebufferRenderbuffer, glsym_func_void);
1974
1975    FINDSYM(_sym_glFramebufferTexture2D, "glFramebufferTexture2D", glsym_func_void);
1976    FALLBAK(_sym_glFramebufferTexture2D, glsym_func_void);
1977
1978    FINDSYM(_sym_glFrontFace, "glFrontFace", glsym_func_void);
1979    FALLBAK(_sym_glFrontFace, glsym_func_void);
1980
1981    FINDSYM(_sym_glGenBuffers, "glGenBuffers", glsym_func_void);
1982    FALLBAK(_sym_glGenBuffers, glsym_func_void);
1983
1984    FINDSYM(_sym_glGenerateMipmap, "glGenerateMipmap", glsym_func_void);
1985    FALLBAK(_sym_glGenerateMipmap, glsym_func_void);
1986
1987    FINDSYM(_sym_glGenFramebuffers, "glGenFramebuffers", glsym_func_void);
1988    FALLBAK(_sym_glGenFramebuffers, glsym_func_void);
1989
1990    FINDSYM(_sym_glGenRenderbuffers, "glGenRenderbuffers", glsym_func_void);
1991    FALLBAK(_sym_glGenRenderbuffers, glsym_func_void);
1992
1993    FINDSYM(_sym_glGenTextures, "glGenTextures", glsym_func_void);
1994    FALLBAK(_sym_glGenTextures, glsym_func_void);
1995
1996    FINDSYM(_sym_glGetActiveAttrib, "glGetActiveAttrib", glsym_func_void);
1997    FALLBAK(_sym_glGetActiveAttrib, glsym_func_void);
1998
1999    FINDSYM(_sym_glGetActiveUniform, "glGetActiveUniform", glsym_func_void);
2000    FALLBAK(_sym_glGetActiveUniform, glsym_func_void);
2001
2002    FINDSYM(_sym_glGetAttachedShaders, "glGetAttachedShaders", glsym_func_void);
2003    FALLBAK(_sym_glGetAttachedShaders, glsym_func_void);
2004
2005    FINDSYM(_sym_glGetAttribLocation, "glGetAttribLocation", glsym_func_int);
2006    FALLBAK(_sym_glGetAttribLocation, glsym_func_int);
2007
2008    FINDSYM(_sym_glGetBooleanv, "glGetBooleanv", glsym_func_void);
2009    FALLBAK(_sym_glGetBooleanv, glsym_func_void);
2010
2011    FINDSYM(_sym_glGetBufferParameteriv, "glGetBufferParameteriv", glsym_func_void);
2012    FALLBAK(_sym_glGetBufferParameteriv, glsym_func_void);
2013
2014    FINDSYM(_sym_glGetError, "glGetError", glsym_func_uint);
2015    FALLBAK(_sym_glGetError, glsym_func_uint);
2016
2017    FINDSYM(_sym_glGetFloatv, "glGetFloatv", glsym_func_void);
2018    FALLBAK(_sym_glGetFloatv, glsym_func_void);
2019
2020    FINDSYM(_sym_glGetFramebufferAttachmentParameteriv, "glGetFramebufferAttachmentParameteriv", glsym_func_void);
2021    FALLBAK(_sym_glGetFramebufferAttachmentParameteriv, glsym_func_void);
2022
2023    FINDSYM(_sym_glGetIntegerv, "glGetIntegerv", glsym_func_void);
2024    FALLBAK(_sym_glGetIntegerv, glsym_func_void);
2025
2026    FINDSYM(_sym_glGetProgramiv, "glGetProgramiv", glsym_func_void);
2027    FALLBAK(_sym_glGetProgramiv, glsym_func_void);
2028
2029    FINDSYM(_sym_glGetProgramInfoLog, "glGetProgramInfoLog", glsym_func_void);
2030    FALLBAK(_sym_glGetProgramInfoLog, glsym_func_void);
2031
2032    FINDSYM(_sym_glGetRenderbufferParameteriv, "glGetRenderbufferParameteriv", glsym_func_void);
2033    FALLBAK(_sym_glGetRenderbufferParameteriv, glsym_func_void);
2034
2035    FINDSYM(_sym_glGetShaderiv, "glGetShaderiv", glsym_func_void);
2036    FALLBAK(_sym_glGetShaderiv, glsym_func_void);
2037
2038    FINDSYM(_sym_glGetShaderInfoLog, "glGetShaderInfoLog", glsym_func_void);
2039    FALLBAK(_sym_glGetShaderInfoLog, glsym_func_void);
2040
2041    FINDSYM(_sym_glGetShaderPrecisionFormat, "glGetShaderPrecisionFormat", glsym_func_void);
2042    FALLBAK(_sym_glGetShaderPrecisionFormat, glsym_func_void);
2043
2044    FINDSYM(_sym_glGetShaderSource, "glGetShaderSource", glsym_func_void);
2045    FALLBAK(_sym_glGetShaderSource, glsym_func_void);
2046
2047    FINDSYM(_sym_glGetString, "glGetString", glsym_func_uchar_ptr);
2048    FALLBAK(_sym_glGetString, glsym_func_const_uchar_ptr);
2049
2050    FINDSYM(_sym_glGetTexParameterfv, "glGetTexParameterfv", glsym_func_void);
2051    FALLBAK(_sym_glGetTexParameterfv, glsym_func_void);
2052
2053    FINDSYM(_sym_glGetTexParameteriv, "glGetTexParameteriv", glsym_func_void);
2054    FALLBAK(_sym_glGetTexParameteriv, glsym_func_void);
2055
2056    FINDSYM(_sym_glGetUniformfv, "glGetUniformfv", glsym_func_void);
2057    FALLBAK(_sym_glGetUniformfv, glsym_func_void);
2058
2059    FINDSYM(_sym_glGetUniformiv, "glGetUniformiv", glsym_func_void);
2060    FALLBAK(_sym_glGetUniformiv, glsym_func_void);
2061
2062    FINDSYM(_sym_glGetUniformLocation, "glGetUniformLocation", glsym_func_int);
2063    FALLBAK(_sym_glGetUniformLocation, glsym_func_int);
2064
2065    FINDSYM(_sym_glGetVertexAttribfv, "glGetVertexAttribfv", glsym_func_void);
2066    FALLBAK(_sym_glGetVertexAttribfv, glsym_func_void);
2067
2068    FINDSYM(_sym_glGetVertexAttribiv, "glGetVertexAttribiv", glsym_func_void);
2069    FALLBAK(_sym_glGetVertexAttribiv, glsym_func_void);
2070
2071    FINDSYM(_sym_glGetVertexAttribPointerv, "glGetVertexAttribPointerv", glsym_func_void);
2072    FALLBAK(_sym_glGetVertexAttribPointerv, glsym_func_void);
2073
2074    FINDSYM(_sym_glHint, "glHint", glsym_func_void);
2075    FALLBAK(_sym_glHint, glsym_func_void);
2076
2077    FINDSYM(_sym_glIsBuffer, "glIsBuffer", glsym_func_uchar);
2078    FALLBAK(_sym_glIsBuffer, glsym_func_uchar);
2079
2080    FINDSYM(_sym_glIsEnabled, "glIsEnabled", glsym_func_uchar);
2081    FALLBAK(_sym_glIsEnabled, glsym_func_uchar);
2082
2083    FINDSYM(_sym_glIsFramebuffer, "glIsFramebuffer", glsym_func_uchar);
2084    FALLBAK(_sym_glIsFramebuffer, glsym_func_uchar);
2085
2086    FINDSYM(_sym_glIsProgram, "glIsProgram", glsym_func_uchar);
2087    FALLBAK(_sym_glIsProgram, glsym_func_uchar);
2088
2089    FINDSYM(_sym_glIsRenderbuffer, "glIsRenderbuffer", glsym_func_uchar);
2090    FALLBAK(_sym_glIsRenderbuffer, glsym_func_uchar);
2091
2092    FINDSYM(_sym_glIsShader, "glIsShader", glsym_func_uchar);
2093    FALLBAK(_sym_glIsShader, glsym_func_uchar);
2094
2095    FINDSYM(_sym_glIsTexture, "glIsTexture", glsym_func_uchar);
2096    FALLBAK(_sym_glIsTexture, glsym_func_uchar);
2097
2098    FINDSYM(_sym_glLineWidth, "glLineWidth", glsym_func_void);
2099    FALLBAK(_sym_glLineWidth, glsym_func_void);
2100
2101    FINDSYM(_sym_glLinkProgram, "glLinkProgram", glsym_func_void);
2102    FALLBAK(_sym_glLinkProgram, glsym_func_void);
2103
2104    FINDSYM(_sym_glPixelStorei, "glPixelStorei", glsym_func_void);
2105    FALLBAK(_sym_glPixelStorei, glsym_func_void);
2106
2107    FINDSYM(_sym_glPolygonOffset, "glPolygonOffset", glsym_func_void);
2108    FALLBAK(_sym_glPolygonOffset, glsym_func_void);
2109
2110    FINDSYM(_sym_glReadPixels, "glReadPixels", glsym_func_void);
2111    FALLBAK(_sym_glReadPixels, glsym_func_void);
2112
2113    FINDSYM(_sym_glReleaseShaderCompiler, "glReleaseShaderCompiler", glsym_func_void);
2114    FALLBAK(_sym_glReleaseShaderCompiler, glsym_func_void);
2115
2116    FINDSYM(_sym_glRenderbufferStorage, "glRenderbufferStorage", glsym_func_void);
2117    FALLBAK(_sym_glRenderbufferStorage, glsym_func_void);
2118
2119    FINDSYM(_sym_glSampleCoverage, "glSampleCoverage", glsym_func_void);
2120    FALLBAK(_sym_glSampleCoverage, glsym_func_void);
2121
2122    FINDSYM(_sym_glScissor, "glScissor", glsym_func_void);
2123    FALLBAK(_sym_glScissor, glsym_func_void);
2124
2125    FINDSYM(_sym_glShaderBinary, "glShaderBinary", glsym_func_void);
2126    FALLBAK(_sym_glShaderBinary, glsym_func_void);
2127
2128    FINDSYM(_sym_glShaderSource, "glShaderSource", glsym_func_void);
2129    FALLBAK(_sym_glShaderSource, glsym_func_void);
2130
2131    FINDSYM(_sym_glStencilFunc, "glStencilFunc", glsym_func_void);
2132    FALLBAK(_sym_glStencilFunc, glsym_func_void);
2133
2134    FINDSYM(_sym_glStencilFuncSeparate, "glStencilFuncSeparate", glsym_func_void);
2135    FALLBAK(_sym_glStencilFuncSeparate, glsym_func_void);
2136
2137    FINDSYM(_sym_glStencilMask, "glStencilMask", glsym_func_void);
2138    FALLBAK(_sym_glStencilMask, glsym_func_void);
2139
2140    FINDSYM(_sym_glStencilMaskSeparate, "glStencilMaskSeparate", glsym_func_void);
2141    FALLBAK(_sym_glStencilMaskSeparate, glsym_func_void);
2142
2143    FINDSYM(_sym_glStencilOp, "glStencilOp", glsym_func_void);
2144    FALLBAK(_sym_glStencilOp, glsym_func_void);
2145
2146    FINDSYM(_sym_glStencilOpSeparate, "glStencilOpSeparate", glsym_func_void);
2147    FALLBAK(_sym_glStencilOpSeparate, glsym_func_void);
2148
2149    FINDSYM(_sym_glTexImage2D, "glTexImage2D", glsym_func_void);
2150    FALLBAK(_sym_glTexImage2D, glsym_func_void);
2151
2152    FINDSYM(_sym_glTexParameterf, "glTexParameterf", glsym_func_void);
2153    FALLBAK(_sym_glTexParameterf, glsym_func_void);
2154
2155    FINDSYM(_sym_glTexParameterfv, "glTexParameterfv", glsym_func_void);
2156    FALLBAK(_sym_glTexParameterfv, glsym_func_void);
2157
2158    FINDSYM(_sym_glTexParameteri, "glTexParameteri", glsym_func_void);
2159    FALLBAK(_sym_glTexParameteri, glsym_func_void);
2160
2161    FINDSYM(_sym_glTexParameteriv, "glTexParameteriv", glsym_func_void);
2162    FALLBAK(_sym_glTexParameteriv, glsym_func_void);
2163
2164    FINDSYM(_sym_glTexSubImage2D, "glTexSubImage2D", glsym_func_void);
2165    FALLBAK(_sym_glTexSubImage2D, glsym_func_void);
2166
2167    FINDSYM(_sym_glUniform1f, "glUniform1f", glsym_func_void);
2168    FALLBAK(_sym_glUniform1f, glsym_func_void);
2169
2170    FINDSYM(_sym_glUniform1fv, "glUniform1fv", glsym_func_void);
2171    FALLBAK(_sym_glUniform1fv, glsym_func_void);
2172
2173    FINDSYM(_sym_glUniform1i, "glUniform1i", glsym_func_void);
2174    FALLBAK(_sym_glUniform1i, glsym_func_void);
2175
2176    FINDSYM(_sym_glUniform1iv, "glUniform1iv", glsym_func_void);
2177    FALLBAK(_sym_glUniform1iv, glsym_func_void);
2178
2179    FINDSYM(_sym_glUniform2f, "glUniform2f", glsym_func_void);
2180    FALLBAK(_sym_glUniform2f, glsym_func_void);
2181
2182    FINDSYM(_sym_glUniform2fv, "glUniform2fv", glsym_func_void);
2183    FALLBAK(_sym_glUniform2fv, glsym_func_void);
2184
2185    FINDSYM(_sym_glUniform2i, "glUniform2i", glsym_func_void);
2186    FALLBAK(_sym_glUniform2i, glsym_func_void);
2187
2188    FINDSYM(_sym_glUniform2iv, "glUniform2iv", glsym_func_void);
2189    FALLBAK(_sym_glUniform2iv, glsym_func_void);
2190
2191    FINDSYM(_sym_glUniform3f, "glUniform3f", glsym_func_void);
2192    FALLBAK(_sym_glUniform3f, glsym_func_void);
2193
2194    FINDSYM(_sym_glUniform3fv, "glUniform3fv", glsym_func_void);
2195    FALLBAK(_sym_glUniform3fv, glsym_func_void);
2196
2197    FINDSYM(_sym_glUniform3i, "glUniform3i", glsym_func_void);
2198    FALLBAK(_sym_glUniform3i, glsym_func_void);
2199
2200    FINDSYM(_sym_glUniform3iv, "glUniform3iv", glsym_func_void);
2201    FALLBAK(_sym_glUniform3iv, glsym_func_void);
2202
2203    FINDSYM(_sym_glUniform4f, "glUniform4f", glsym_func_void);
2204    FALLBAK(_sym_glUniform4f, glsym_func_void);
2205
2206    FINDSYM(_sym_glUniform4fv, "glUniform4fv", glsym_func_void);
2207    FALLBAK(_sym_glUniform4fv, glsym_func_void);
2208
2209    FINDSYM(_sym_glUniform4i, "glUniform4i", glsym_func_void);
2210    FALLBAK(_sym_glUniform4i, glsym_func_void);
2211
2212    FINDSYM(_sym_glUniform4iv, "glUniform4iv", glsym_func_void);
2213    FALLBAK(_sym_glUniform4iv, glsym_func_void);
2214
2215    FINDSYM(_sym_glUniformMatrix2fv, "glUniformMatrix2fv", glsym_func_void);
2216    FALLBAK(_sym_glUniformMatrix2fv, glsym_func_void);
2217
2218    FINDSYM(_sym_glUniformMatrix3fv, "glUniformMatrix3fv", glsym_func_void);
2219    FALLBAK(_sym_glUniformMatrix3fv, glsym_func_void);
2220
2221    FINDSYM(_sym_glUniformMatrix4fv, "glUniformMatrix4fv", glsym_func_void);
2222    FALLBAK(_sym_glUniformMatrix4fv, glsym_func_void);
2223
2224    FINDSYM(_sym_glUseProgram, "glUseProgram", glsym_func_void);
2225    FALLBAK(_sym_glUseProgram, glsym_func_void);
2226
2227    FINDSYM(_sym_glValidateProgram, "glValidateProgram", glsym_func_void);
2228    FALLBAK(_sym_glValidateProgram, glsym_func_void);
2229
2230    FINDSYM(_sym_glVertexAttrib1f, "glVertexAttrib1f", glsym_func_void);
2231    FALLBAK(_sym_glVertexAttrib1f, glsym_func_void);
2232
2233    FINDSYM(_sym_glVertexAttrib1fv, "glVertexAttrib1fv", glsym_func_void);
2234    FALLBAK(_sym_glVertexAttrib1fv, glsym_func_void);
2235
2236    FINDSYM(_sym_glVertexAttrib2f, "glVertexAttrib2f", glsym_func_void);
2237    FALLBAK(_sym_glVertexAttrib2f, glsym_func_void);
2238
2239    FINDSYM(_sym_glVertexAttrib2fv, "glVertexAttrib2fv", glsym_func_void);
2240    FALLBAK(_sym_glVertexAttrib2fv, glsym_func_void);
2241
2242    FINDSYM(_sym_glVertexAttrib3f, "glVertexAttrib3f", glsym_func_void);
2243    FALLBAK(_sym_glVertexAttrib3f, glsym_func_void);
2244
2245    FINDSYM(_sym_glVertexAttrib3fv, "glVertexAttrib3fv", glsym_func_void);
2246    FALLBAK(_sym_glVertexAttrib3fv, glsym_func_void);
2247
2248    FINDSYM(_sym_glVertexAttrib4f, "glVertexAttrib4f", glsym_func_void);
2249    FALLBAK(_sym_glVertexAttrib4f, glsym_func_void);
2250
2251    FINDSYM(_sym_glVertexAttrib4fv, "glVertexAttrib4fv", glsym_func_void);
2252    FALLBAK(_sym_glVertexAttrib4fv, glsym_func_void);
2253
2254    FINDSYM(_sym_glVertexAttribPointer, "glVertexAttribPointer", glsym_func_void);
2255    FALLBAK(_sym_glVertexAttribPointer, glsym_func_void);
2256
2257    FINDSYM(_sym_glViewport, "glViewport", glsym_func_void);
2258    FALLBAK(_sym_glViewport, glsym_func_void);
2259
2260 #undef FINDSYM
2261 #undef FALLBAK
2262
2263    // Checking to see if this function exists is a poor but reasonable way to 
2264    // check if it's gles but it works for now
2265    if (_sym_glGetShaderPrecisionFormat != (typeof(_sym_glGetShaderPrecisionFormat))sym_missing ) 
2266      {
2267         DBG("GL Library is GLES.");
2268         gl_lib_is_gles = 1;
2269      }
2270
2271    return 1;
2272 }
2273
2274 //--------------------------------------------------------------//
2275 // Wrapped GL APIs to handle desktop compatibility
2276
2277 // Stripping precision code from GLES shader for desktop compatibility
2278 // Code adopted from Meego GL code. Temporary Fix.  
2279 static const char *
2280 opengl_strtok(const char *s, int *n, char **saveptr, char *prevbuf)
2281 {
2282    char *start;
2283    char *ret;
2284    char *p;
2285    int retlen;
2286    static const char *delim = " \t\n\r/";
2287
2288    if (prevbuf) free(prevbuf);
2289
2290    if (s) 
2291       *saveptr = (char *)s;
2292    else 
2293      {
2294         if (!(*saveptr) || !(*n))
2295            return NULL;
2296         s = *saveptr;
2297      }
2298
2299    for (; *n && strchr(delim, *s); s++, (*n)--) 
2300      {
2301         if (*s == '/' && *n > 1) 
2302           {
2303              if (s[1] == '/') 
2304                {
2305                   do 
2306                     {
2307                        s++, (*n)--;
2308                     } 
2309                   while (*n > 1 && s[1] != '\n' && s[1] != '\r');
2310                } 
2311              else if (s[1] == '*') 
2312                {
2313                   do 
2314                     {
2315                        s++, (*n)--;
2316                     } 
2317                   while (*n > 2 && (s[1] != '*' || s[2] != '/'));
2318                   s++, (*n)--;
2319                }
2320           }
2321      }
2322
2323    start = (char *)s;
2324    for (; *n && *s && !strchr(delim, *s); s++, (*n)--);
2325    if (*n > 0) s++, (*n)--;
2326
2327    *saveptr = (char *)s;
2328
2329    retlen = s - start;
2330    ret = malloc(retlen + 1);
2331    p = ret;
2332
2333    while (retlen > 0) 
2334      {
2335         if (*start == '/' && retlen > 1) 
2336           {
2337              if (start[1] == '/') 
2338                {
2339                   do 
2340                     {
2341                        start++, retlen--;
2342                     } 
2343                   while (retlen > 1 && start[1] != '\n' && start[1] != '\r');
2344                   start++, retlen--;
2345                   continue;
2346                } 
2347              else if (start[1] == '*') 
2348                {
2349                   do 
2350                     {
2351                        start++, retlen--;
2352                     } 
2353                   while (retlen > 2 && (start[1] != '*' || start[2] != '/'));
2354                   start += 3, retlen -= 3;
2355                   continue;
2356                }
2357           }
2358         *(p++) = *(start++), retlen--;
2359      }
2360
2361    *p = 0;
2362    return ret;
2363 }       
2364
2365 static char *
2366 patch_gles_shader(const char *source, int length, int *patched_len)
2367 {
2368    char *saveptr = NULL;
2369    char *sp;
2370    char *p = NULL;
2371
2372    if (!length) length = strlen(source);
2373
2374    *patched_len = 0;
2375    int patched_size = length;
2376    char *patched = malloc(patched_size + 1);
2377
2378    if (!patched) return NULL;
2379
2380    p = (char *)opengl_strtok(source, &length, &saveptr, NULL);
2381    for (; p; p = (char *)opengl_strtok(0, &length, &saveptr, p)) 
2382      {
2383         if (!strncmp(p, "lowp", 4) || !strncmp(p, "mediump", 7) || !strncmp(p, "highp", 5)) 
2384           {
2385              continue;
2386           } 
2387         else if (!strncmp(p, "precision", 9)) 
2388           {
2389              while ((p = (char *)opengl_strtok(0, &length, &saveptr, p)) && !strchr(p, ';'));
2390           } 
2391         else 
2392           {
2393              if (!strncmp(p, "gl_MaxVertexUniformVectors", 26)) 
2394                {
2395                   p = "(gl_MaxVertexUniformComponents / 4)";
2396                } 
2397              else if (!strncmp(p, "gl_MaxFragmentUniformVectors", 28)) 
2398                {
2399                   p = "(gl_MaxFragmentUniformComponents / 4)";
2400                } 
2401              else if (!strncmp(p, "gl_MaxVaryingVectors", 20)) 
2402                {
2403                   p = "(gl_MaxVaryingFloats / 4)";
2404                }
2405
2406              int new_len = strlen(p);
2407              if (*patched_len + new_len > patched_size) 
2408                {
2409                   patched_size *= 2;
2410                   patched = realloc(patched, patched_size + 1);
2411
2412                   if (!patched) 
2413                      return NULL;
2414                }
2415
2416              memcpy(patched + *patched_len, p, new_len);
2417              *patched_len += new_len;
2418           }     
2419      }
2420
2421    patched[*patched_len] = 0;
2422    /* check that we don't leave dummy preprocessor lines */
2423    for (sp = patched; *sp;) 
2424      {
2425         for (; *sp == ' ' || *sp == '\t'; sp++);
2426         if (!strncmp(sp, "#define", 7)) 
2427           {
2428              for (p = sp + 7; *p == ' ' || *p == '\t'; p++);
2429              if (*p == '\n' || *p == '\r' || *p == '/') 
2430                {
2431                   memset(sp, 0x20, 7);
2432                }
2433           }
2434         for (; *sp && *sp != '\n' && *sp != '\r'; sp++);
2435         for (; *sp == '\n' || *sp == '\r'; sp++);
2436      }
2437    return patched;
2438 }
2439
2440 static void
2441 evgl_glShaderSource(GLuint shader, GLsizei count, const char** string, const GLint* length)
2442 {
2443    int i = 0, len = 0;
2444
2445    char **s = malloc(count * sizeof(char*));
2446    GLint *l = malloc(count * sizeof(GLint));
2447
2448    memset(s, 0, count * sizeof(char*));
2449    memset(l, 0, count * sizeof(GLint));
2450
2451    for (i = 0; i < count; ++i) 
2452      {
2453         if (length) 
2454           {
2455              len = length[i];
2456              if (len < 0) 
2457                 len = string[i] ? strlen(string[i]) : 0;
2458           }
2459         else
2460            len = string[i] ? strlen(string[i]) : 0;
2461
2462         if (string[i]) 
2463           {
2464              s[i] = patch_gles_shader(string[i], len, &l[i]);
2465              if (!s[i]) 
2466                {
2467                   while(i)
2468                      free(s[--i]);
2469                   free(l);
2470                   free(s);
2471
2472                   DBG("Patching Shader Failed.");
2473                   return;
2474                }
2475           } 
2476         else 
2477           {
2478              s[i] = NULL;
2479              l[i] = 0;
2480           }
2481      }
2482
2483    _sym_glShaderSource(shader, count, (const char **)s, l);
2484
2485    while(i)
2486       free(s[--i]);
2487    free(l);
2488    free(s);
2489 }
2490
2491
2492 static void
2493 evgl_glGetShaderPrecisionFormat(GLenum shadertype __UNUSED__, GLenum precisiontype __UNUSED__, GLint* range, GLint* precision)
2494 {
2495    if (range)
2496      {
2497         range[0] = -126; // floor(log2(FLT_MIN))
2498         range[1] = 127; // floor(log2(FLT_MAX))
2499      }
2500    if (precision)
2501      {
2502         precision[0] = 24; // floor(-log2((1.0/16777218.0)));
2503      }
2504    return;
2505 }
2506
2507 static void
2508 evgl_glReleaseShaderCompiler(void)
2509 {
2510    DBG("Not supported in Desktop GL");
2511    return;
2512 }
2513
2514 static void
2515 evgl_glShaderBinary(GLsizei n __UNUSED__, const GLuint* shaders __UNUSED__, GLenum binaryformat __UNUSED__, const void* binary __UNUSED__, GLsizei length __UNUSED__)
2516 {
2517    // FIXME: need to dlsym/getprocaddress for this
2518    DBG("Not supported in Desktop GL");
2519    return;
2520    //n = binaryformat = length = 0;
2521    //shaders = binary = 0;
2522 }
2523 #endif
2524 //--------------------------------------------------------------//
2525
2526
2527 #ifdef EVAS_GL
2528 static void
2529 override_gl_apis(Evas_GL_API *api)
2530 {
2531
2532    api->version = EVAS_GL_API_VERSION;
2533
2534 #define ORD(f) EVAS_API_OVERRIDE(f, api, _sym_)
2535    // GLES 2.0
2536    ORD(glActiveTexture);
2537    ORD(glAttachShader);
2538    ORD(glBindAttribLocation);
2539    ORD(glBindBuffer);
2540    ORD(glBindFramebuffer);
2541    ORD(glBindRenderbuffer);
2542    ORD(glBindTexture);
2543    ORD(glBlendColor);
2544    ORD(glBlendEquation);
2545    ORD(glBlendEquationSeparate);
2546    ORD(glBlendFunc);
2547    ORD(glBlendFuncSeparate);
2548    ORD(glBufferData);
2549    ORD(glBufferSubData);
2550    ORD(glCheckFramebufferStatus);
2551    ORD(glClear);
2552    ORD(glClearColor);
2553    ORD(glClearDepthf);     
2554    ORD(glClearStencil);
2555    ORD(glColorMask);
2556    ORD(glCompileShader);
2557    ORD(glCompressedTexImage2D);
2558    ORD(glCompressedTexSubImage2D);
2559    ORD(glCopyTexImage2D);
2560    ORD(glCopyTexSubImage2D);
2561    ORD(glCreateProgram);
2562    ORD(glCreateShader);
2563    ORD(glCullFace);
2564    ORD(glDeleteBuffers);
2565    ORD(glDeleteFramebuffers);
2566    ORD(glDeleteProgram);
2567    ORD(glDeleteRenderbuffers);
2568    ORD(glDeleteShader);
2569    ORD(glDeleteTextures);
2570    ORD(glDepthFunc);
2571    ORD(glDepthMask);
2572    ORD(glDepthRangef);     
2573    ORD(glDetachShader);
2574    ORD(glDisable);
2575    ORD(glDisableVertexAttribArray);
2576    ORD(glDrawArrays);
2577    ORD(glDrawElements);
2578    ORD(glEnable);
2579    ORD(glEnableVertexAttribArray);
2580    ORD(glFinish);
2581    ORD(glFlush);
2582    ORD(glFramebufferRenderbuffer);
2583    ORD(glFramebufferTexture2D);
2584    ORD(glFrontFace);
2585    ORD(glGenBuffers);
2586    ORD(glGenerateMipmap);
2587    ORD(glGenFramebuffers);
2588    ORD(glGenRenderbuffers);
2589    ORD(glGenTextures);
2590    ORD(glGetActiveAttrib);
2591    ORD(glGetActiveUniform);
2592    ORD(glGetAttachedShaders);
2593    ORD(glGetAttribLocation);
2594    ORD(glGetBooleanv);
2595    ORD(glGetBufferParameteriv);
2596    ORD(glGetError);
2597    ORD(glGetFloatv);
2598    ORD(glGetFramebufferAttachmentParameteriv);
2599    ORD(glGetIntegerv);
2600    ORD(glGetProgramiv);
2601    ORD(glGetProgramInfoLog);
2602    ORD(glGetRenderbufferParameteriv);
2603    ORD(glGetShaderiv);
2604    ORD(glGetShaderInfoLog);
2605    ORD(glGetShaderPrecisionFormat);  
2606    ORD(glGetShaderSource);
2607    ORD(glGetString);             // FIXME
2608    ORD(glGetTexParameterfv);
2609    ORD(glGetTexParameteriv);
2610    ORD(glGetUniformfv);
2611    ORD(glGetUniformiv);
2612    ORD(glGetUniformLocation);
2613    ORD(glGetVertexAttribfv);
2614    ORD(glGetVertexAttribiv);
2615    ORD(glGetVertexAttribPointerv);
2616    ORD(glHint);
2617    ORD(glIsBuffer);
2618    ORD(glIsEnabled);
2619    ORD(glIsFramebuffer);
2620    ORD(glIsProgram);
2621    ORD(glIsRenderbuffer);
2622    ORD(glIsShader);
2623    ORD(glIsTexture);
2624    ORD(glLineWidth);
2625    ORD(glLinkProgram);
2626    ORD(glPixelStorei);
2627    ORD(glPolygonOffset);
2628    ORD(glReadPixels);
2629    ORD(glReleaseShaderCompiler); 
2630    ORD(glRenderbufferStorage);
2631    ORD(glSampleCoverage);
2632    ORD(glScissor);
2633    ORD(glShaderBinary); 
2634    ORD(glShaderSource);
2635    ORD(glStencilFunc);
2636    ORD(glStencilFuncSeparate);
2637    ORD(glStencilMask);
2638    ORD(glStencilMaskSeparate);
2639    ORD(glStencilOp);
2640    ORD(glStencilOpSeparate);
2641    ORD(glTexImage2D);
2642    ORD(glTexParameterf);
2643    ORD(glTexParameterfv);
2644    ORD(glTexParameteri);
2645    ORD(glTexParameteriv);
2646    ORD(glTexSubImage2D);
2647    ORD(glUniform1f);
2648    ORD(glUniform1fv);
2649    ORD(glUniform1i);
2650    ORD(glUniform1iv);
2651    ORD(glUniform2f);
2652    ORD(glUniform2fv);
2653    ORD(glUniform2i);
2654    ORD(glUniform2iv);
2655    ORD(glUniform3f);
2656    ORD(glUniform3fv);
2657    ORD(glUniform3i);
2658    ORD(glUniform3iv);
2659    ORD(glUniform4f);
2660    ORD(glUniform4fv);
2661    ORD(glUniform4i);
2662    ORD(glUniform4iv);
2663    ORD(glUniformMatrix2fv);
2664    ORD(glUniformMatrix3fv);
2665    ORD(glUniformMatrix4fv);
2666    ORD(glUseProgram);
2667    ORD(glValidateProgram);
2668    ORD(glVertexAttrib1f);
2669    ORD(glVertexAttrib1fv);
2670    ORD(glVertexAttrib2f);
2671    ORD(glVertexAttrib2fv);
2672    ORD(glVertexAttrib3f);
2673    ORD(glVertexAttrib3fv);
2674    ORD(glVertexAttrib4f);
2675    ORD(glVertexAttrib4fv);
2676    ORD(glVertexAttribPointer);
2677    ORD(glViewport);
2678 #undef ORD
2679
2680 #define ORD(f) EVAS_API_OVERRIDE(f, &gl_funcs, evgl_)
2681    if (!gl_lib_is_gles)
2682      {
2683         // Override functions wrapped by Evas_GL
2684         // GLES2.0 API compat on top of desktop gl
2685         ORD(glGetShaderPrecisionFormat);
2686         ORD(glReleaseShaderCompiler);
2687         ORD(glShaderBinary);
2688      }
2689
2690    ORD(glShaderSource);    // Do precision stripping in both cases
2691 #undef ORD
2692 }
2693 #endif
2694
2695 //-------------------------------------------//
2696 static int
2697 gl_lib_init(void)
2698 {
2699 #ifdef EVAS_GL
2700    // dlopen OSMesa 
2701    gl_lib_handle = dlopen("libOSMesa.so.1", RTLD_NOW);
2702    if (!gl_lib_handle) gl_lib_handle = dlopen("libOSMesa.so", RTLD_NOW);
2703    if (!gl_lib_handle)
2704      {
2705         DBG("Unable to open libOSMesa:  %s", dlerror());
2706         return 0;
2707      }
2708
2709    //------------------------------------------------//
2710    if (!glue_sym_init()) return 0;
2711    if (!gl_sym_init()) return 0;
2712
2713    override_gl_apis(&gl_funcs);
2714
2715    return 1;
2716 #else
2717    return 0;
2718 #endif
2719 }
2720
2721
2722 static void
2723 init_gl(void)
2724 {
2725    DBG("Initializing Software OpenGL APIs...\n");
2726
2727    if (!gl_lib_init())
2728       DBG("Unable to support EvasGL in this engine module. Install OSMesa to get it running");
2729    else
2730      {
2731 #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
2732         ORD(gl_surface_create);
2733         ORD(gl_surface_destroy);
2734         ORD(gl_context_create);
2735         ORD(gl_context_destroy);
2736         ORD(gl_make_current);
2737         ORD(gl_string_query);           // FIXME: Need to implement
2738         ORD(gl_proc_address_get);       // FIXME: Need to implement
2739         ORD(gl_native_surface_get);
2740         ORD(gl_api_get);
2741 #undef ORD
2742      }
2743 }
2744
2745
2746 /*
2747  *****
2748  **
2749  ** MODULE ACCESSIBLE API API
2750  **
2751  *****
2752  */
2753
2754 static int
2755 module_open(Evas_Module *em)
2756 {
2757    if (!em) return 0;
2758    _evas_soft_gen_log_dom = eina_log_domain_register
2759      ("evas-software_generic", EVAS_DEFAULT_LOG_COLOR);
2760    if(_evas_soft_gen_log_dom<0)
2761      {
2762         EINA_LOG_ERR("Can not create a module log domain.");
2763         return 0;
2764      }
2765
2766    init_gl();
2767
2768    em->functions = (void *)(&func);
2769    cpunum = eina_cpu_count();
2770    return 1;
2771 }
2772
2773 static void
2774 module_close(Evas_Module *em __UNUSED__)
2775 {
2776   eina_log_domain_unregister(_evas_soft_gen_log_dom);
2777 }
2778
2779 static Evas_Module_Api evas_modapi =
2780 {
2781    EVAS_MODULE_API_VERSION,
2782    "software_generic",
2783    "none",
2784    {
2785      module_open,
2786      module_close
2787    }
2788 };
2789
2790 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_ENGINE, engine, software_generic);
2791
2792 #ifndef EVAS_STATIC_BUILD_SOFTWARE_GENERIC
2793 EVAS_EINA_MODULE_DEFINE(engine, software_generic);
2794 #endif