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