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