dynamic texture path may have a segv issue... right jy? :) this should
[profile/ivi/evas.git] / src / modules / engines / gl_x11 / evas_engine.c
1 #include "evas_common.h" /* Also includes international specific stuff */
2 #include "evas_engine.h"
3
4 #ifdef HAVE_DLSYM
5 # include <dlfcn.h>      /* dlopen,dlclose,etc */
6 #else
7 # error gl_x11 should not get compiled if dlsym is not found on the system!
8 #endif
9
10 #define EVAS_GL_NO_GL_H_CHECK 1
11 #include "Evas_GL.h"
12
13 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
14 // EGL / GLES
15 # if defined(GLES_VARIETY_S3C6410)
16 # elif defined(GLES_VARIETY_SGX)
17 # endif
18 #else
19 // GLX
20 #endif
21
22 typedef struct _Render_Engine               Render_Engine;
23 typedef struct _Render_Engine_GL_Surface    Render_Engine_GL_Surface;
24 typedef struct _Render_Engine_GL_Context    Render_Engine_GL_Context;
25 typedef struct _Render_Engine_GL_Resource   Render_Engine_GL_Resource;
26 typedef struct _Extension_Entry             Extension_Entry;
27
28 struct _Render_Engine
29 {
30    Evas_GL_X11_Window      *win;
31    Evas_Engine_Info_GL_X11 *info;
32    Evas                    *evas;
33    Tilebuf                 *tb;
34    int                      end;
35 /*
36    XrmDatabase   xrdb; // xres - dpi
37    struct { // xres - dpi
38       int        dpi; // xres - dpi
39    } xr; // xres - dpi
40  */
41    int w, h;
42    int vsync;
43
44    // GL Surface Capability
45    struct {
46         int max_rb_size;
47         int msaa_support;
48         int msaa_samples[4];
49
50         //---------//
51         int rgb_888[4];
52         int rgba_8888[4];
53
54         int depth_8[4];
55         int depth_16[4];
56         int depth_24[4];
57         int depth_32[4];
58
59         int stencil_1[4];
60         int stencil_2[4];
61         int stencil_4[4];
62         int stencil_8[4];
63         int stencil_16[4];
64
65         int depth_24_stencil_8[4];
66    } gl_cap;
67
68    int gl_cap_initted;
69 };
70
71 struct _Render_Engine_GL_Surface
72 {
73    int      initialized;
74    int      fbo_attached;
75    int      w, h;
76    int      depth_bits;
77    int      stencil_bits;
78
79    int      direct_fb_opt;
80    int      multisample_bits;
81
82    // Render target Texture/Buffers
83    GLint    rt_msaa_samples;
84
85    GLuint   rt_tex;
86    GLint    rt_internal_fmt;
87    GLenum   rt_fmt;
88    GLuint   rb_depth;
89    GLenum   rb_depth_fmt;
90    GLuint   rb_stencil;
91    GLenum   rb_stencil_fmt;
92    GLuint   rb_depth_stencil;
93    GLenum   rb_depth_stencil_fmt;
94
95 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
96    EGLSurface  direct_sfc;
97 #else
98    Window      direct_sfc;
99 #endif
100
101    Render_Engine_GL_Context   *current_ctx;
102 };
103
104 struct _Render_Engine_GL_Context
105 {
106    int         initialized;
107 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
108    EGLContext  context;
109 #else
110    GLXContext  context;
111 #endif
112    GLuint      context_fbo;
113    GLuint      current_fbo;
114
115    int         scissor_enabled;
116    int         scissor_upated;
117
118    Render_Engine_GL_Surface   *current_sfc;
119 };
120
121 // Resources used per thread
122 struct _Render_Engine_GL_Resource
123 {
124    // Resource context/surface per Thread in TLS for evasgl use
125 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
126    EGLContext context;
127    EGLSurface surface;
128 #else
129    GLXContext context;
130 #endif
131 };
132
133 // Extension Handling
134 struct _Extension_Entry
135 {
136    const char *name;
137    const char *real_name;
138    int supported;
139 };
140
141 static int initted = 0;
142 static int gl_wins = 0;
143 static int gl_direct_override = 0;
144 static int gl_direct_enabled = 0;
145 static Render_Engine_GL_Context *current_evgl_ctx = NULL;
146 static Render_Engine *current_engine = NULL;
147 static Evas_Object *gl_direct_img_obj = NULL;
148
149 static int  _ext_initted = 0;
150 static char *_gl_ext_string = NULL;
151 static char *_evasgl_ext_string = NULL;
152
153 // Resource context/surface per Thread in TLS for evasgl use
154 static Eina_TLS   resource_key;
155 static Eina_List *resource_list;
156 LK(resource_lock);
157
158 typedef void            (*_eng_fn) (void);
159 typedef _eng_fn         (*glsym_func_eng_fn) ();
160 typedef void            (*glsym_func_void) ();
161 typedef void           *(*glsym_func_void_ptr) ();
162 typedef int             (*glsym_func_int) ();
163 typedef unsigned int    (*glsym_func_uint) ();
164 typedef unsigned char   (*glsym_func_uchar) ();
165 typedef unsigned char  *(*glsym_func_uchar_ptr) ();
166 typedef const char     *(*glsym_func_const_char_ptr) ();
167
168 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
169
170 #ifndef EGL_NATIVE_PIXMAP_KHR
171 # define EGL_NATIVE_PIXMAP_KHR 0x30b0
172 #endif
173 _eng_fn  (*glsym_eglGetProcAddress)            (const char *a) = NULL;
174 void     (*glsym_eglBindTexImage)              (EGLDisplay a, EGLSurface b, int c) = NULL;
175 void     (*glsym_eglReleaseTexImage)           (EGLDisplay a, EGLSurface b, int c) = NULL;
176 void    *(*glsym_eglCreateImage)               (EGLDisplay a, EGLContext b, EGLenum c, EGLClientBuffer d, const int *e) = NULL;
177 void     (*glsym_eglDestroyImage)              (EGLDisplay a, void *b) = NULL;
178 void     (*glsym_glEGLImageTargetTexture2DOES) (int a, void *b)  = NULL;
179 void     (*glsym_glEGLImageTargetRenderbufferStorageOES) (int a, void *b)  = NULL;
180 void          *(*glsym_eglMapImageSEC)         (void *a, void *b) = NULL;
181 unsigned int   (*glsym_eglUnmapImageSEC)       (void *a, void *b) = NULL;
182 const char    *(*glsym_eglQueryString)         (EGLDisplay a, int name) = NULL;
183
184 unsigned int   (*glsym_eglLockSurface)          (EGLDisplay a, EGLSurface b, const int *attrib_list) = NULL;
185 unsigned int   (*glsym_eglUnlockSurface)        (EGLDisplay a, EGLSurface b) = NULL;
186
187 #else
188 typedef XID     (*glsym_func_xid) ();
189
190 _eng_fn  (*glsym_glXGetProcAddress)  (const char *a) = NULL;
191 void     (*glsym_glXBindTexImage)    (Display *a, GLXDrawable b, int c, int *d) = NULL;
192 void     (*glsym_glXReleaseTexImage) (Display *a, GLXDrawable b, int c) = NULL;
193 int      (*glsym_glXGetVideoSync)    (unsigned int *a) = NULL;
194 int      (*glsym_glXWaitVideoSync)   (int a, int b, unsigned int *c) = NULL;
195 XID      (*glsym_glXCreatePixmap)    (Display *a, void *b, Pixmap c, const int *d) = NULL;
196 void     (*glsym_glXDestroyPixmap)   (Display *a, XID b) = NULL;
197 void     (*glsym_glXQueryDrawable)   (Display *a, XID b, int c, unsigned int *d) = NULL;
198 int      (*glsym_glXSwapIntervalSGI) (int a) = NULL;
199 void     (*glsym_glXSwapIntervalEXT) (Display *s, GLXDrawable b, int c) = NULL;
200
201 const char *(*glsym_glXQueryExtensionsString) (Display *a, int screen) = NULL;
202 #endif
203
204 // GLES2 Extensions
205 void    (*glsym_glGetProgramBinaryOES) (GLuint program, GLsizei bufSize, GLsizei *length, GLenum *binaryFormat, void *binary) = NULL;
206 void    (*glsym_glProgramBinaryOES) (GLuint program, GLenum binaryFormat, const void *binary, GLint length) = NULL;
207 void*   (*glsym_glMapBufferOES) (GLenum target, GLenum access) = NULL;
208 unsigned char   (*glsym_glUnmapBufferOES) (GLenum target) = NULL;
209 void    (*glsym_glGetBufferPointervOES) (GLenum target, GLenum pname, void** params) = NULL;
210 void    (*glsym_glTexImage3DOES) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void* pixels) = NULL;
211 void    (*glsym_glTexSubImage3DOES) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void* pixels) = NULL;
212 void    (*glsym_glCopyTexSubImage3DOES) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height) = NULL;
213 void    (*glsym_glCompressedTexImage3DOES) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void* data) = NULL;
214 void    (*glsym_glCompressedTexSubImage3DOES) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const void* data) = NULL;
215 void    (*glsym_glFramebufferTexture3DOES) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset) = NULL;
216 void    (*glsym_glGetPerfMonitorGroupsAMD) (GLint* numGroups, GLsizei groupsSize, GLuint* groups) = NULL;
217 void    (*glsym_glGetPerfMonitorCountersAMD) (GLuint group, GLint* numCounters, GLint* maxActiveCounters, GLsizei counterSize, GLuint* counters) = NULL;
218 void    (*glsym_glGetPerfMonitorGroupStringAMD) (GLuint group, GLsizei bufSize, GLsizei* length, char* groupString) = NULL;
219 void    (*glsym_glGetPerfMonitorCounterStringAMD) (GLuint group, GLuint counter, GLsizei bufSize, GLsizei* length, char* counterString) = NULL;
220 void    (*glsym_glGetPerfMonitorCounterInfoAMD) (GLuint group, GLuint counter, GLenum pname, void* data) = NULL;
221 void    (*glsym_glGenPerfMonitorsAMD) (GLsizei n, GLuint* monitors) = NULL;
222 void    (*glsym_glDeletePerfMonitorsAMD) (GLsizei n, GLuint* monitors) = NULL;
223 void    (*glsym_glSelectPerfMonitorCountersAMD) (GLuint monitor, GLboolean enable, GLuint group, GLint numCounters, GLuint* countersList) = NULL;
224 void    (*glsym_glBeginPerfMonitorAMD) (GLuint monitor) = NULL;
225 void    (*glsym_glEndPerfMonitorAMD) (GLuint monitor) = NULL;
226 void    (*glsym_glGetPerfMonitorCounterDataAMD) (GLuint monitor, GLenum pname, GLsizei dataSize, GLuint* data, GLint* bytesWritten) = NULL;
227 void    (*glsym_glDiscardFramebufferEXT) (GLenum target, GLsizei numAttachments, const GLenum* attachments) = NULL;
228 void    (*glsym_glMultiDrawArraysEXT) (GLenum mode, GLint* first, GLsizei* count, GLsizei primcount) = NULL;
229 void    (*glsym_glMultiDrawElementsEXT) (GLenum mode, const GLsizei* count, GLenum type, const GLvoid** indices, GLsizei primcount) = NULL;
230 void    (*glsym_glDeleteFencesNV) (GLsizei n, const GLuint* fences) = NULL;
231 void    (*glsym_glGenFencesNV) (GLsizei n, GLuint* fences) = NULL;
232 unsigned char   (*glsym_glIsFenceNV) (GLuint fence) = NULL;
233 unsigned char   (*glsym_glTestFenceNV) (GLuint fence) = NULL;
234 void    (*glsym_glGetFenceivNV) (GLuint fence, GLenum pname, GLint* params) = NULL;
235 void    (*glsym_glFinishFenceNV) (GLuint fence) = NULL;
236 void    (*glsym_glSetFenceNV) (GLuint, GLenum) = NULL;
237 void    (*glsym_glRenderbufferStorageMultisample) (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height) = NULL;
238 void    (*glsym_glFramebufferTexture2DMultisample) (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLsizei samples) = NULL;
239 void    (*glsym_glGetDriverControlsQCOM) (GLint* num, GLsizei size, GLuint* driverControls) = NULL;
240 void    (*glsym_glGetDriverControlStringQCOM) (GLuint driverControl, GLsizei bufSize, GLsizei* length, char* driverControlString) = NULL;
241 void    (*glsym_glEnableDriverControlQCOM) (GLuint driverControl) = NULL;
242 void    (*glsym_glDisableDriverControlQCOM) (GLuint driverControl) = NULL;
243 void    (*glsym_glExtGetTexturesQCOM) (GLuint* textures, GLint maxTextures, GLint* numTextures) = NULL;
244 void    (*glsym_glExtGetBuffersQCOM) (GLuint* buffers, GLint maxBuffers, GLint* numBuffers) = NULL;
245 void    (*glsym_glExtGetRenderbuffersQCOM) (GLuint* renderbuffers, GLint maxRenderbuffers, GLint* numRenderbuffers) = NULL;
246 void    (*glsym_glExtGetFramebuffersQCOM) (GLuint* framebuffers, GLint maxFramebuffers, GLint* numFramebuffers) = NULL;
247 void    (*glsym_glExtGetTexLevelParameterivQCOM) (GLuint texture, GLenum face, GLint level, GLenum pname, GLint* params) = NULL;
248 void    (*glsym_glExtTexObjectStateOverrideiQCOM) (GLenum target, GLenum pname, GLint param) = NULL;
249 void    (*glsym_glExtGetTexSubImageQCOM) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, void* texels) = NULL;
250 void    (*glsym_glExtGetBufferPointervQCOM) (GLenum target, void** params) = NULL;
251 void    (*glsym_glExtGetShadersQCOM) (GLuint* shaders, GLint maxShaders, GLint* numShaders) = NULL;
252 void    (*glsym_glExtGetProgramsQCOM) (GLuint* programs, GLint maxPrograms, GLint* numPrograms) = NULL;
253 unsigned char   (*glsym_glExtIsProgramBinaryQCOM) (GLuint program) = NULL;
254 void    (*glsym_glExtGetProgramBinarySourceQCOM) (GLuint program, GLenum shadertype, char* source, GLint* length) = NULL;
255
256
257 //------ GLES 2.0 Extensions supported in EvasGL -----//
258 static Extension_Entry _gl_ext_entries[] = {
259 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
260        //--- Function Extensions ---//
261        { "GL_OES_get_program_binary", "get_program_binary", 0 },
262        { "GL_OES_mapbuffer", "mapbuffer", 0 },
263        { "GL_OES_texture_3D", "texture_3D", 0 },
264        { "AMD_performance_monitor", "AMD_performance_monitor", 0 },
265        { "GL_EXT_discard_framebuffer", "discard_framebuffer", 0 },
266        { "GL_EXT_multi_draw_arrays", "multi_draw_arrays", 0 },
267        { "GL_NV_fence", "NV_fence", 0 },
268        { "GL_QCOM_driver_control", "QCOM_driver_control", 0 },
269        { "GL_QCOM_extended_get", "QCOM_extended_get", 0 },
270        { "GL_QCOM_extended_get2", "QCOM_extended_get2", 0 },
271        { "GL_IMG_multlisampled_render_to_texture", "multisampled_render_to_texture", 0 },
272
273        //--- Define Extensions ---//
274        { "GL_OES_compressed_ETC1_RGB8_texture", "compressed_ETC1_RGB8_texture", 0 },
275        { "GL_OES_compressed_paletted_texture", "compressed_paletted_texture", 0 },
276        { "GL_OES_depth24", "depth24", 0 },
277        { "GL_OES_depth32", "depth32", 0 },
278        { "GL_OES_EvasGL_image", "EGL_image", 0 },
279        { "GL_OES_packed_depth_stencil", "packed_depth_stencil", 0 },
280        { "GL_OES_rgb8_rgba8", "rgb8_rgba8", 0 },
281        { "GL_OES_standard_derivatives", "standard_derivatives", 0 },
282        { "GL_OES_stencil1", "stencil1", 0 },
283        { "GL_OES_stencil4", "stencil4", 0 },
284        { "GL_OES_texture_float", "texture_float", 0 },
285        { "GL_OES_texture_half_float", "texture_half_float", 0 },
286        { "GL_OES_texture_half_float_linear", "texture_half_float_linear", 0 },
287        { "GL_OES_texture_npot", "texture_npot", 0 },
288        { "GL_OES_vertex_half_float", "vertex_half_float", 0 },
289        { "GL_OES_vertex_type_10_10_10_2", "vertex_type_10_10_10_2", 0 },
290        { "GL_AMD_compressed_3DC_texture", "compressed_3DC_texture", 0 },
291        { "GL_AMD_compressed_ATC_texture", "compressed_ATC_texture", 0 },
292        { "GL_AMD_program_binary_Z400", "program_binary_Z400", 0 },
293        { "GL_EXT_blend_minmax", "blend_minmax", 0 },
294        { "GL_EXT_read_format_bgra", "read_format_bgra", 0 },
295        { "GL_EXT_texture_filter_anisotropic", "texture_filter_anisotrophic", 0 },
296        { "GL_EXT_texture_format_BGRA8888", "texture_format_BGRA8888", 0 },
297        { "GL_EXT_texture_type_2_10_10_10_REV", "texture_type_2_10_10_10_rev", 0 },
298        { "GL_IMG_program_binary", "IMG_program_binary", 0 },
299        { "GL_IMG_read_format", "IMG_read_format", 0 },
300        { "GL_IMG_shader_binary", "IMG_shader_binary", 0 },
301        { "GL_IMG_texture_compression_pvrtc", "IMG_texture_compression_pvrtc", 0 },
302        { "GL_QCOM_perfmon_global_mode", "QCOM_perfmon_global_mode", 0 },
303        { "GL_QCOM_writeonly_rendering", "QCOM_writeonly_rendering", 0 },
304 #else
305        //--- Function Extensions ---//
306        { "GL_OES_get_program_binary", "get_program_binary", 0 },
307        { "GL_OES_mapbuffer", "mapbuffer", 0 },
308        { "GL_OES_texture_3D", "texture_3D", 0 },
309        { "AMD_performance_monitor", "AMD_performance_monitor", 0 },
310        { "GL_EXT_discard_framebuffer", "discard_framebuffer", 0 },
311        { "GL_EXT_multi_draw_arrays", "multi_draw_arrays", 0 },
312        { "GL_NV_fence", "NV_fence", 0 },
313        { "GL_QCOM_driver_control", "QCOM_driver_control", 0 },
314        { "GL_QCOM_extended_get", "QCOM_extended_get", 0 },
315        { "GL_QCOM_extended_get2", "QCOM_extended_get2", 0 },
316        { "GL_IMG_multlisampled_render_to_texture", "multisampled_render_to_texture", 0 },
317
318        //--- Define Extensions ---//
319        { "GL_OES_compressed_ETC1_RGB8_texture", "compressed_ETC1_RGB8_texture", 0 },
320        { "GL_OES_compressed_paletted_texture", "compressed_paletted_texture", 0 },
321        { "GL_OES_depth24", "depth24", 0 },
322        { "GL_OES_depth32", "depth32", 0 },
323        { "GL_OES_EvasGL_image", "EGL_image", 0 },
324        { "GL_OES_packed_depth_stencil", "packed_depth_stencil", 0 },
325        { "GL_OES_rgb8_rgba8", "rgb8_rgba8", 0 },
326        { "GL_OES_standard_derivatives", "standard_derivatives", 0 },
327        { "GL_OES_stencil1", "stencil1", 0 },
328        { "GL_OES_stencil4", "stencil4", 0 },
329        { "GL_OES_texture_float", "texture_float", 0 },
330        { "GL_OES_texture_half_float", "texture_half_float", 0 },
331        { "GL_OES_texture_half_float_linear", "texture_half_float_linear", 0 },
332        { "GL_OES_texture_npot", "texture_non_power_of_two", 0 },     // Desktop differs
333        { "GL_OES_vertex_half_float", "half_float_vertex", 0 }, // Desktop differs
334        { "GL_OES_vertex_type_10_10_10_2", "vertex_type_10_10_10_2", 0 },
335        { "GL_AMD_compressed_3DC_texture", "compressed_3DC_texture", 0 },
336        { "GL_AMD_compressed_ATC_texture", "compressed_ATC_texture", 0 },
337        { "GL_AMD_program_binary_Z400", "program_binary_Z400", 0 },
338        { "GL_EXT_blend_minmax", "blend_minmax", 0 },
339        { "GL_EXT_read_format_bgra", "bgra", 0 }, // Desktop differs
340        { "GL_EXT_texture_filter_anisotropic", "texture_filter_anisotrophic", 0 },
341        { "GL_EXT_texture_format_BGRA8888", "bgra", 0 }, // Desktop differs
342        { "GL_EXT_texture_type_2_10_10_10_REV", "vertex_type_2_10_10_10_rev", 0 },  // Desktop differs ???
343        { "GL_IMG_program_binary", "IMG_program_binary", 0 },
344        { "GL_IMG_read_format", "IMG_read_format", 0 },
345        { "GL_IMG_shader_binary", "IMG_shader_binary", 0 },
346        { "GL_IMG_texture_compression_pvrtc", "IMG_texture_compression_pvrtc", 0 },
347        { "GL_QCOM_perfmon_global_mode", "QCOM_perfmon_global_mode", 0 },
348        { "GL_QCOM_writeonly_rendering", "QCOM_writeonly_rendering", 0 },
349
350 #endif
351        { NULL, NULL, 0}
352 };
353
354 //------ Extensions supported in EvasGL -----//
355 static Extension_Entry _evasgl_ext_entries[] = {
356 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
357        { "EvasGL_KHR_image", "EGL_KHR_image", 0 },
358        { "EvasGL_KHR_vg_parent_image", "EGL_KHR_vg_parent_image", 0 },
359        { "EvasGL_KHR_gl_texture_2D_image", "EGL_KHR_gl_texture_2D_image", 0 },
360        { "EvasGL_KHR_gl_texture_cubemap_image", "EGL_KHR_gl_texture_cubemap_image", 0 },
361        { "EvasGL_KHR_gl_texture_3D_image", "EGL_KHR_gl_texture_3D_image", 0 },
362        { "EvasGL_KHR_gl_renderbuffer_image", "EGL_KHR_gl_renderbuffer_image", 0 },
363 #else
364 #endif
365        { NULL, NULL, 0 }
366 };
367
368 static void
369 _gl_ext_sym_init(void)
370 {
371    static int done = 0;
372
373    if (done) return;
374
375 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
376 #define FINDSYM(dst, sym, typ) \
377    if ((!dst) && (glsym_eglGetProcAddress)) dst = (typ)glsym_eglGetProcAddress(sym); \
378    if (!dst) dst = (typ)dlsym(RTLD_DEFAULT, sym)
379
380    FINDSYM(glsym_eglGetProcAddress, "eglGetProcAddress", glsym_func_eng_fn);
381    FINDSYM(glsym_eglGetProcAddress, "eglGetProcAddressEXT", glsym_func_eng_fn);
382    FINDSYM(glsym_eglGetProcAddress, "eglGetProcAddressARB", glsym_func_eng_fn);
383    FINDSYM(glsym_eglGetProcAddress, "eglGetProcAddressKHR", glsym_func_eng_fn);
384
385    FINDSYM(glsym_eglBindTexImage, "eglBindTexImage", glsym_func_void);
386    FINDSYM(glsym_eglBindTexImage, "eglBindTexImageEXT", glsym_func_void);
387    FINDSYM(glsym_eglBindTexImage, "eglBindTexImageARB", glsym_func_void);
388    FINDSYM(glsym_eglBindTexImage, "eglBindTexImageKHR", glsym_func_void);
389
390    FINDSYM(glsym_eglReleaseTexImage, "eglReleaseTexImage", glsym_func_void);
391    FINDSYM(glsym_eglReleaseTexImage, "eglReleaseTexImageEXT", glsym_func_void);
392    FINDSYM(glsym_eglReleaseTexImage, "eglReleaseTexImageARB", glsym_func_void);
393    FINDSYM(glsym_eglReleaseTexImage, "eglReleaseTexImageKHR", glsym_func_void);
394
395    FINDSYM(glsym_eglCreateImage, "eglCreateImage", glsym_func_void_ptr);
396    FINDSYM(glsym_eglCreateImage, "eglCreateImageEXT", glsym_func_void_ptr);
397    FINDSYM(glsym_eglCreateImage, "eglCreateImageARB", glsym_func_void_ptr);
398    FINDSYM(glsym_eglCreateImage, "eglCreateImageKHR", glsym_func_void_ptr);
399
400    FINDSYM(glsym_eglDestroyImage, "eglDestroyImage", glsym_func_void);
401    FINDSYM(glsym_eglDestroyImage, "eglDestroyImageEXT", glsym_func_void);
402    FINDSYM(glsym_eglDestroyImage, "eglDestroyImageARB", glsym_func_void);
403    FINDSYM(glsym_eglDestroyImage, "eglDestroyImageKHR", glsym_func_void);
404
405    FINDSYM(glsym_glEGLImageTargetTexture2DOES, "glEGLImageTargetTexture2DOES", glsym_func_void);
406
407    FINDSYM(glsym_glEGLImageTargetRenderbufferStorageOES, "glEGLImageTargetRenderbufferStorageOES", glsym_func_void);
408
409    FINDSYM(glsym_eglMapImageSEC, "eglMapImageSEC", glsym_func_void_ptr);
410    FINDSYM(glsym_eglUnmapImageSEC, "eglUnmapImageSEC", glsym_func_uint);
411
412    FINDSYM(glsym_eglQueryString, "eglQueryString", glsym_func_const_char_ptr);
413
414    FINDSYM(glsym_eglLockSurface, "eglLockSurface", glsym_func_uint);
415    FINDSYM(glsym_eglLockSurface, "eglLockSurfaceEXT", glsym_func_uint);
416    FINDSYM(glsym_eglLockSurface, "eglLockSurfaceARB", glsym_func_uint);
417    FINDSYM(glsym_eglLockSurface, "eglLockSurfaceKHR", glsym_func_uint);
418
419    FINDSYM(glsym_eglUnlockSurface, "eglUnlockSurface", glsym_func_uint);
420    FINDSYM(glsym_eglUnlockSurface, "eglUnlockSurfaceEXT", glsym_func_uint);
421    FINDSYM(glsym_eglUnlockSurface, "eglUnlockSurfaceARB", glsym_func_uint);
422    FINDSYM(glsym_eglUnlockSurface, "eglUnlockSurfaceKHR", glsym_func_uint);
423
424 #else
425 #define FINDSYM(dst, sym, typ) \
426    if ((!dst) && (glsym_glXGetProcAddress)) dst = (typ)glsym_glXGetProcAddress(sym); \
427    if (!dst) dst = (typ)dlsym(RTLD_DEFAULT, sym)
428
429    FINDSYM(glsym_glXGetProcAddress, "glXGetProcAddress", glsym_func_eng_fn);
430    FINDSYM(glsym_glXGetProcAddress, "glXGetProcAddressEXT", glsym_func_eng_fn);
431    FINDSYM(glsym_glXGetProcAddress, "glXGetProcAddressARB", glsym_func_eng_fn);
432
433    FINDSYM(glsym_glXBindTexImage, "glXBindTexImage", glsym_func_void);
434    FINDSYM(glsym_glXBindTexImage, "glXBindTexImageEXT", glsym_func_void);
435    FINDSYM(glsym_glXBindTexImage, "glXBindTexImageARB", glsym_func_void);
436
437    FINDSYM(glsym_glXReleaseTexImage, "glXReleaseTexImage", glsym_func_void);
438    FINDSYM(glsym_glXReleaseTexImage, "glXReleaseTexImageEXT", glsym_func_void);
439    FINDSYM(glsym_glXReleaseTexImage, "glXReleaseTexImageARB", glsym_func_void);
440
441    FINDSYM(glsym_glXGetVideoSync, "glXGetVideoSyncSGI", glsym_func_int);
442
443    FINDSYM(glsym_glXWaitVideoSync, "glXWaitVideoSyncSGI", glsym_func_int);
444
445    FINDSYM(glsym_glXCreatePixmap, "glXCreatePixmap", glsym_func_xid);
446    FINDSYM(glsym_glXCreatePixmap, "glXCreatePixmapEXT", glsym_func_xid);
447    FINDSYM(glsym_glXCreatePixmap, "glXCreatePixmapARB", glsym_func_xid);
448
449    FINDSYM(glsym_glXDestroyPixmap, "glXDestroyPixmap", glsym_func_void);
450    FINDSYM(glsym_glXDestroyPixmap, "glXDestroyPixmapEXT", glsym_func_void);
451    FINDSYM(glsym_glXDestroyPixmap, "glXDestroyPixmapARB", glsym_func_void);
452
453    FINDSYM(glsym_glXQueryDrawable, "glXQueryDrawable", glsym_func_void);
454    FINDSYM(glsym_glXQueryDrawable, "glXQueryDrawableEXT", glsym_func_void);
455    FINDSYM(glsym_glXQueryDrawable, "glXQueryDrawableARB", glsym_func_void);
456
457    FINDSYM(glsym_glXSwapIntervalSGI, "glXSwapIntervalMESA", glsym_func_int);
458    FINDSYM(glsym_glXSwapIntervalSGI, "glXSwapIntervalSGI", glsym_func_int);
459
460    FINDSYM(glsym_glXSwapIntervalEXT, "glXSwapIntervalEXT", glsym_func_void);
461
462    FINDSYM(glsym_glXQueryExtensionsString, "glXQueryExtensionsString", glsym_func_const_char_ptr);
463 #endif
464
465    //----------- GLES 2.0 Extensions ------------//
466    // If the symbol's not found, they get set to NULL
467    // If one of the functions in the extension exists, the extension in supported
468    /* GL_OES_get_program_binary */
469    FINDSYM(glsym_glGetProgramBinaryOES, "glGetProgramBinary", glsym_func_void);
470    FINDSYM(glsym_glGetProgramBinaryOES, "glGetProgramBinaryEXT", glsym_func_void);
471    FINDSYM(glsym_glGetProgramBinaryOES, "glGetProgramBinaryARB", glsym_func_void);
472    FINDSYM(glsym_glGetProgramBinaryOES, "glGetProgramBinaryOES", glsym_func_void);
473
474    FINDSYM(glsym_glProgramBinaryOES, "glProgramBinary", glsym_func_void);
475    FINDSYM(glsym_glProgramBinaryOES, "glProgramBinaryEXT", glsym_func_void);
476    FINDSYM(glsym_glProgramBinaryOES, "glProgramBinaryARB", glsym_func_void);
477    FINDSYM(glsym_glProgramBinaryOES, "glProgramBinaryOES", glsym_func_void);
478
479    // Check the first function to see if the extension is supported...
480    if (glsym_glGetProgramBinaryOES) _gl_ext_entries[0].supported = 1;
481
482
483    /* GL_OES_mapbuffer */
484    FINDSYM(glsym_glMapBufferOES, "glMapBuffer", glsym_func_void_ptr);
485    FINDSYM(glsym_glMapBufferOES, "glMapBufferEXT", glsym_func_void_ptr);
486    FINDSYM(glsym_glMapBufferOES, "glMapBufferARB", glsym_func_void_ptr);
487    FINDSYM(glsym_glMapBufferOES, "glMapBufferOES", glsym_func_void_ptr);
488
489    FINDSYM(glsym_glUnmapBufferOES, "glUnmapBuffer", glsym_func_uchar);
490    FINDSYM(glsym_glUnmapBufferOES, "glUnmapBufferEXT", glsym_func_uchar);
491    FINDSYM(glsym_glUnmapBufferOES, "glUnmapBufferARB", glsym_func_uchar);
492    FINDSYM(glsym_glUnmapBufferOES, "glUnmapBufferOES", glsym_func_uchar);
493
494    FINDSYM(glsym_glGetBufferPointervOES, "glGetBufferPointerv", glsym_func_void);
495    FINDSYM(glsym_glGetBufferPointervOES, "glGetBufferPointervEXT", glsym_func_void);
496    FINDSYM(glsym_glGetBufferPointervOES, "glGetBufferPointervARB", glsym_func_void);
497    FINDSYM(glsym_glGetBufferPointervOES, "glGetBufferPointervOES", glsym_func_void);
498
499    if (glsym_glMapBufferOES) _gl_ext_entries[1].supported = 1;
500
501    /* GL_OES_texture_3D */
502    FINDSYM(glsym_glTexImage3DOES, "glTexImage3D", glsym_func_void);
503    FINDSYM(glsym_glTexImage3DOES, "glTexImage3DEXT", glsym_func_void);
504    FINDSYM(glsym_glTexImage3DOES, "glTexImage3DARB", glsym_func_void);
505    FINDSYM(glsym_glTexImage3DOES, "glTexImage3DOES", glsym_func_void);
506
507    FINDSYM(glsym_glTexSubImage3DOES, "glTexSubImage3D", glsym_func_void);
508    FINDSYM(glsym_glTexSubImage3DOES, "glTexSubImage3DEXT", glsym_func_void);
509    FINDSYM(glsym_glTexSubImage3DOES, "glTexSubImage3DARB", glsym_func_void);
510    FINDSYM(glsym_glTexSubImage3DOES, "glTexSubImage3DOES", glsym_func_void);
511
512    FINDSYM(glsym_glCopyTexSubImage3DOES, "glCopyTexSubImage3D", glsym_func_void);
513    FINDSYM(glsym_glCopyTexSubImage3DOES, "glCopyTexSubImage3DARB", glsym_func_void);
514    FINDSYM(glsym_glCopyTexSubImage3DOES, "glCopyTexSubImage3DEXT", glsym_func_void);
515    FINDSYM(glsym_glCopyTexSubImage3DOES, "glCopyTexSubImage3DOES", glsym_func_void);
516
517    FINDSYM(glsym_glCompressedTexImage3DOES, "glCompressedTexImage3D", glsym_func_void);
518    FINDSYM(glsym_glCompressedTexImage3DOES, "glCompressedTexImage3DARB", glsym_func_void);
519    FINDSYM(glsym_glCompressedTexImage3DOES, "glCompressedTexImage3DEXT", glsym_func_void);
520    FINDSYM(glsym_glCompressedTexImage3DOES, "glCompressedTexImage3DOES", glsym_func_void);
521
522    FINDSYM(glsym_glCompressedTexSubImage3DOES, "glCompressedTexSubImage3D", glsym_func_void);
523    FINDSYM(glsym_glCompressedTexSubImage3DOES, "glCompressedTexSubImage3DARB", glsym_func_void);
524    FINDSYM(glsym_glCompressedTexSubImage3DOES, "glCompressedTexSubImage3DEXT", glsym_func_void);
525    FINDSYM(glsym_glCompressedTexSubImage3DOES, "glCompressedTexSubImage3DOES", glsym_func_void);
526
527    FINDSYM(glsym_glFramebufferTexture3DOES, "glFramebufferTexture3D", glsym_func_void);
528    FINDSYM(glsym_glFramebufferTexture3DOES, "glFramebufferTexture3DARB", glsym_func_void);
529    FINDSYM(glsym_glFramebufferTexture3DOES, "glFramebufferTexture3DEXT", glsym_func_void);
530    FINDSYM(glsym_glFramebufferTexture3DOES, "glFramebufferTexture3DOES", glsym_func_void);
531
532    if (glsym_glTexSubImage3DOES) _gl_ext_entries[2].supported = 1;
533
534    /* AMD_performance_monitor */
535    FINDSYM(glsym_glGetPerfMonitorGroupsAMD, "glGetPerfMonitorGroupsAMD", glsym_func_void);
536    FINDSYM(glsym_glGetPerfMonitorCountersAMD, "glGetPerfMonitorCountersAMD", glsym_func_void);
537    FINDSYM(glsym_glGetPerfMonitorGroupStringAMD, "glGetPerfMonitorGroupStringAMD", glsym_func_void);
538    FINDSYM(glsym_glGetPerfMonitorCounterStringAMD, "glGetPerfMonitorCounterStringAMD", glsym_func_void);
539    FINDSYM(glsym_glGetPerfMonitorCounterInfoAMD, "glGetPerfMonitorCounterInfoAMD", glsym_func_void);
540    FINDSYM(glsym_glGenPerfMonitorsAMD, "glGenPerfMonitorsAMD", glsym_func_void);
541    FINDSYM(glsym_glDeletePerfMonitorsAMD, "glDeletePerfMonitorsAMD", glsym_func_void);
542    FINDSYM(glsym_glSelectPerfMonitorCountersAMD, "glSelectPerfMonitorCountersAMD", glsym_func_void);
543    FINDSYM(glsym_glBeginPerfMonitorAMD, "glBeginPerfMonitorAMD", glsym_func_void);
544    FINDSYM(glsym_glEndPerfMonitorAMD, "glEndPerfMonitorAMD", glsym_func_void);
545    FINDSYM(glsym_glGetPerfMonitorCounterDataAMD, "glGetPerfMonitorCounterDataAMD", glsym_func_void);
546
547    if (glsym_glGetPerfMonitorGroupsAMD) _gl_ext_entries[3].supported = 1;
548
549    /* GL_EXT_discard_framebuffer */
550    FINDSYM(glsym_glDiscardFramebufferEXT, "glDiscardFramebuffer", glsym_func_void);
551    FINDSYM(glsym_glDiscardFramebufferEXT, "glDiscardFramebufferARB", glsym_func_void);
552    FINDSYM(glsym_glDiscardFramebufferEXT, "glDiscardFramebufferEXT", glsym_func_void);
553
554    if (glsym_glDiscardFramebufferEXT) _gl_ext_entries[4].supported = 1;
555
556    /* GL_EXT_multi_draw_arrays */
557    FINDSYM(glsym_glMultiDrawArraysEXT, "glMultiDrawArrays", glsym_func_void);
558    FINDSYM(glsym_glMultiDrawArraysEXT, "glMultiDrawArraysARB", glsym_func_void);
559    FINDSYM(glsym_glMultiDrawArraysEXT, "glMultiDrawArraysEXT", glsym_func_void);
560
561    FINDSYM(glsym_glMultiDrawElementsEXT, "glMultiDrawElements", glsym_func_void);
562    FINDSYM(glsym_glMultiDrawElementsEXT, "glMultiDrawElementsARB", glsym_func_void);
563    FINDSYM(glsym_glMultiDrawElementsEXT, "glMultiDrawElementsEXT", glsym_func_void);
564
565    if (glsym_glMultiDrawArraysEXT) _gl_ext_entries[5].supported = 1;
566
567    /* GL_NV_fence */
568    FINDSYM(glsym_glDeleteFencesNV, "glDeleteFencesNV", glsym_func_void);
569    FINDSYM(glsym_glGenFencesNV, "glGenFencesNV", glsym_func_void);
570    FINDSYM(glsym_glIsFenceNV, "glIsFenceNV", glsym_func_uchar);
571    FINDSYM(glsym_glTestFenceNV, "glTestFenceNV", glsym_func_uchar);
572    FINDSYM(glsym_glGetFenceivNV, "glGetFenceivNV", glsym_func_void);
573    FINDSYM(glsym_glFinishFenceNV, "glFinishFenceNV", glsym_func_void);
574    FINDSYM(glsym_glSetFenceNV, "glSetFenceNV", glsym_func_void);
575
576    if (glsym_glDeleteFencesNV) _gl_ext_entries[6].supported = 1;
577
578    /* GL_QCOM_driver_control */
579    FINDSYM(glsym_glGetDriverControlsQCOM, "glGetDriverControlsQCOM", glsym_func_void);
580    FINDSYM(glsym_glGetDriverControlStringQCOM, "glGetDriverControlStringQCOM", glsym_func_void);
581    FINDSYM(glsym_glEnableDriverControlQCOM, "glEnableDriverControlQCOM", glsym_func_void);
582    FINDSYM(glsym_glDisableDriverControlQCOM, "glDisableDriverControlQCOM", glsym_func_void);
583
584    if (glsym_glGetDriverControlsQCOM) _gl_ext_entries[7].supported = 1;
585
586    /* GL_QCOM_extended_get */
587    FINDSYM(glsym_glExtGetTexturesQCOM, "glExtGetTexturesQCOM", glsym_func_void);
588    FINDSYM(glsym_glExtGetBuffersQCOM, "glExtGetBuffersQCOM", glsym_func_void);
589    FINDSYM(glsym_glExtGetRenderbuffersQCOM, "glExtGetRenderbuffersQCOM", glsym_func_void);
590    FINDSYM(glsym_glExtGetFramebuffersQCOM, "glExtGetFramebuffersQCOM", glsym_func_void);
591    FINDSYM(glsym_glExtGetTexLevelParameterivQCOM, "glExtGetTexLevelParameterivQCOM", glsym_func_void);
592    FINDSYM(glsym_glExtTexObjectStateOverrideiQCOM, "glExtTexObjectStateOverrideiQCOM", glsym_func_void);
593    FINDSYM(glsym_glExtGetTexSubImageQCOM, "glExtGetTexSubImageQCOM", glsym_func_void);
594    FINDSYM(glsym_glExtGetBufferPointervQCOM, "glExtGetBufferPointervQCOM", glsym_func_void);
595
596    if (glsym_glExtGetTexturesQCOM) _gl_ext_entries[8].supported = 1;
597
598    /* GL_QCOM_extended_get2 */
599    FINDSYM(glsym_glExtGetShadersQCOM, "glExtGetShadersQCOM", glsym_func_void);
600    FINDSYM(glsym_glExtGetProgramsQCOM, "glExtGetProgramsQCOM", glsym_func_void);
601    FINDSYM(glsym_glExtIsProgramBinaryQCOM, "glExtIsProgramBinaryQCOM", glsym_func_uchar);
602    FINDSYM(glsym_glExtGetProgramBinarySourceQCOM, "glExtGetProgramBinarySourceQCOM", glsym_func_void);
603
604    if (glsym_glExtGetShadersQCOM) _gl_ext_entries[9].supported = 1;
605
606    /* GL_IMG_multisampled_render_to_texture */
607    FINDSYM(glsym_glRenderbufferStorageMultisample, "glRenderbufferStorageMultisampleIMG", glsym_func_void);
608    FINDSYM(glsym_glRenderbufferStorageMultisample, "glRenderbufferStorageMultisampleEXT", glsym_func_void);
609    FINDSYM(glsym_glFramebufferTexture2DMultisample, "glFramebufferTexture2DMultisampleIMG", glsym_func_void);
610    FINDSYM(glsym_glFramebufferTexture2DMultisample, "glFramebufferTexture2DMultisampleEXT", glsym_func_void);
611
612 }
613
614 static void
615 _gl_ext_init(Render_Engine *re)
616 {
617    int i, ext_len = 0;
618    const char *glexts, *evasglexts;
619
620    // GLES 2.0 Extensions
621    glexts = (const char*)glGetString(GL_EXTENSIONS);
622
623    ext_len = strlen(glexts);
624    if (!ext_len)
625      {
626         DBG("GL Get Extension string NULL: No extensions supported");
627         return;
628      }
629
630    _gl_ext_string = calloc(1, sizeof(char) * ext_len * 2);
631    if (!_gl_ext_string)
632      {
633         ERR("Error allocating _gl_ext_string.");
634         return;
635      }
636
637    DBG("--------GLES 2.0 Extensions--------");
638    for (i = 0; _gl_ext_entries[i].name != NULL; i++)
639      {
640         if ( (strstr(glexts, _gl_ext_entries[i].name) != NULL) ||
641              (strstr(glexts, _gl_ext_entries[i].real_name) != NULL) )
642           {
643              _gl_ext_entries[i].supported = 1;
644              strcat(_gl_ext_string, _gl_ext_entries[i].name);
645              strcat(_gl_ext_string, " ");
646              DBG("\t%s", _gl_ext_entries[i].name);
647           }
648
649      }
650    DBG(" ");
651
652 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
653    // EGL Extensions
654    if (glsym_eglQueryString)
655      {
656         evasglexts = glsym_eglQueryString(re->win->egl_disp, EGL_EXTENSIONS);
657 #else
658    if (glsym_glXQueryExtensionsString)
659      {
660         evasglexts = glXQueryExtensionsString(re->info->info.display,
661                                               re->info->info.screen);
662 #endif
663         ext_len = strlen(evasglexts);
664
665         if (!ext_len)
666           {
667              DBG("GL Get Extension string NULL: No extensions supported");
668              return;
669           }
670
671         _evasgl_ext_string = calloc(1, sizeof(char) * ext_len * 2);
672         if (!_evasgl_ext_string)
673           {
674              ERR("Error allocating _evasgl_ext_string.");
675              return;
676           }
677
678         DBG("--------EvasGL Supported Extensions----------");
679         for (i = 0; _evasgl_ext_entries[i].name != NULL; i++)
680           {
681              if ( (strstr(evasglexts, _evasgl_ext_entries[i].name) != NULL) ||
682                   (strstr(evasglexts, _evasgl_ext_entries[i].real_name) != NULL) )
683                {
684                   _evasgl_ext_entries[i].supported = 1;
685                   strcat(_evasgl_ext_string, _evasgl_ext_entries[i].name);
686                   strcat(_evasgl_ext_string, " ");
687                   DBG("\t%s", _evasgl_ext_entries[i].name);
688                }
689           }
690         DBG(" ");
691      }
692 }
693
694 int _evas_engine_GL_X11_log_dom = -1;
695 /* function tables - filled in later (func and parent func) */
696 static Evas_Func func, pfunc;
697
698 /* Function table for GL APIs */
699 static Evas_GL_API gl_funcs;
700 /*
701 struct xrdb_user
702 {
703    time_t last_stat;
704    time_t last_mtime;
705    XrmDatabase db;
706 };
707 static struct xrdb_user xrdb_user = {0, 0, NULL};
708
709 static Eina_Bool
710 xrdb_user_query(const char *name, const char *cls, char **type, XrmValue *val)
711 {
712    time_t last = xrdb_user.last_stat, now = time(NULL);
713
714    xrdb_user.last_stat = now;
715    if (last != now) // don't stat() more than once every second
716      {
717         struct stat st;
718         const char *home = getenv("HOME");
719         char tmp[PATH_MAX];
720
721         if (!home) goto failed;
722         snprintf(tmp, sizeof(tmp), "%s/.Xdefaults", home);
723         if (stat(tmp, &st) != 0) goto failed;
724         if (xrdb_user.last_mtime != st.st_mtime)
725           {
726              if (xrdb_user.db) XrmDestroyDatabase(xrdb_user.db);
727              xrdb_user.db = XrmGetFileDatabase(tmp);
728              if (!xrdb_user.db) goto failed;
729              xrdb_user.last_mtime = st.st_mtime;
730           }
731      }
732
733    if (!xrdb_user.db) return EINA_FALSE;
734    return XrmGetResource(xrdb_user.db, name, cls, type, val);
735
736  failed:
737    if (xrdb_user.db)
738      {
739         XrmDestroyDatabase(xrdb_user.db);
740         xrdb_user.db = NULL;
741      }
742    xrdb_user.last_mtime = 0;
743    return EINA_FALSE;
744 }
745 */
746
747 static void *
748 eng_info(Evas *e)
749 {
750    Evas_Engine_Info_GL_X11 *info;
751
752    info = calloc(1, sizeof(Evas_Engine_Info_GL_X11));
753    info->magic.magic = rand();
754    info->func.best_visual_get = eng_best_visual_get;
755    info->func.best_colormap_get = eng_best_colormap_get;
756    info->func.best_depth_get = eng_best_depth_get;
757    info->render_mode = EVAS_RENDER_MODE_BLOCKING;
758    return info;
759    e = NULL;
760 }
761
762 static void
763 eng_info_free(Evas *e __UNUSED__, void *info)
764 {
765    Evas_Engine_Info_GL_X11 *in;
766 // dont free! why bother? its not worth it
767 //   eina_log_domain_unregister(_evas_engine_GL_X11_log_dom);
768    in = (Evas_Engine_Info_GL_X11 *)info;
769    free(in);
770 }
771
772 static int
773 _re_wincheck(Render_Engine *re)
774 {
775    if (re->win->surf) return 1;
776    eng_window_resurf(re->win);
777    if (!re->win->surf)
778      {
779         ERR("GL engine can't re-create window surface!");
780      }
781    return 0;
782 }
783
784 static void
785 _re_winfree(Render_Engine *re)
786 {
787    if (!re->win->surf) return;
788    eng_window_unsurf(re->win);
789 }
790
791 static Render_Engine_GL_Resource *
792 _create_internal_glue_resources(void *data)
793 {
794    Render_Engine *re = (Render_Engine *)data;
795    Render_Engine_GL_Resource *rsc;
796
797    rsc = calloc(1, sizeof(Render_Engine_GL_Resource));
798
799    if (!rsc) return NULL;
800
801 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
802    // EGL
803    int context_attrs[3];
804    context_attrs[0] = EGL_CONTEXT_CLIENT_VERSION;
805    context_attrs[1] = 2;
806    context_attrs[2] = EGL_NONE;
807
808    if (eina_main_loop_is())
809      {
810         rsc->surface = re->win->egl_surface[0];
811      }
812    else
813      {
814         // Create resource surface for EGL
815         rsc->surface = eglCreateWindowSurface(re->win->egl_disp,
816                                               re->win->egl_config,
817                                               (EGLNativeWindowType)DefaultRootWindow(re->info->info.display),
818                                               NULL);
819         if (!rsc->surface)
820           {
821              ERR("Creating internal resource surface failed.");
822              free(rsc);
823              return NULL;
824           }
825      }
826
827    // Create a resource context for EGL
828    rsc->context = eglCreateContext(re->win->egl_disp,
829                                    re->win->egl_config,
830                                    re->win->egl_context[0], // Evas' GL Context
831                                    context_attrs);
832    if (!rsc->context)
833      {
834         ERR("Internal Resource Context Creations Failed.");
835         free(rsc);
836         return NULL;
837      }
838
839    // Add to the resource resource list for cleanup
840    LKL(resource_lock);
841    resource_list = eina_list_prepend(resource_list, rsc);
842    LKU(resource_lock);
843
844    // Set the resource in TLS
845    if (eina_tls_set(resource_key, (void*)rsc) == EINA_FALSE)
846      {
847         ERR("Failed setting TLS Resource");
848         free(rsc);
849         return NULL;
850      }
851
852 #else
853    // GLX
854    rsc->context = glXCreateContext(re->info->info.display,
855                                    re->win->visualinfo,
856                                    re->win->context,      // Evas' GL Context
857                                    1);
858    if (!rsc->context)
859      {
860         ERR("Internal Resource Context Creations Failed.");
861         free(rsc);
862         return NULL;
863      }
864
865    // Add to the resource resource list for cleanup
866    LKL(resource_lock);
867    resource_list = eina_list_prepend(resource_list, rsc);
868    LKU(resource_lock);
869
870    // Set the resource in TLS
871    if (eina_tls_set(resource_key, (void*)rsc) == EINA_FALSE)
872      {
873         ERR("Failed setting TLS Resource");
874         free(rsc);
875         return NULL;
876      }
877
878 #endif
879
880
881    return rsc;
882 }
883
884 static int
885 _destroy_internal_glue_resources(void *data)
886 {
887    Render_Engine *re = (Render_Engine *)data;
888    Eina_List *l;
889    Render_Engine_GL_Resource *rsc;
890
891 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
892    // Create internal resource context if it hasn't been created already
893    if ((rsc = eina_tls_get(resource_key)) == NULL)
894      {
895         ERR("Error retrieving the TLS resources.");
896         return 0;
897      }
898
899    if (eina_main_loop_is()) rsc->surface = re->win->egl_surface[0];
900
901    // EGL
902    // Delete the Resources
903    LKL(resource_lock);
904    EINA_LIST_FOREACH(resource_list, l, rsc)
905      {
906         if ((rsc->surface) && (rsc->surface != re->win->egl_surface[0]))
907            eglDestroySurface(re->win->egl_disp, rsc->surface);
908         if (rsc->context)
909            eglDestroyContext(re->win->egl_disp, rsc->context);
910         free(rsc);
911      }
912  #else
913    // GLX
914    // Delete the Resources
915    LKL(resource_lock);
916    EINA_LIST_FOREACH(resource_list, l, rsc)
917      {
918         if (rsc)
919           {
920              glXDestroyContext(re->info->info.display, rsc->context);
921              free(rsc);
922           }
923      }
924 #endif
925    eina_list_free(resource_list);
926    resource_list = NULL;
927    LKU(resource_lock);
928
929    // Destroy TLS
930    eina_tls_free(resource_key);
931
932    // Free the extension strings
933    if (_ext_initted)
934      {
935         if (_gl_ext_string)
936            free(_gl_ext_string);
937         if (_evasgl_ext_string)
938            free(_evasgl_ext_string);
939
940         _gl_ext_string = NULL;
941         _evasgl_ext_string = NULL;
942         _ext_initted = 0;
943      }
944
945    return 1;
946 }
947
948
949 static int
950 _internal_resources_make_current(void *data)
951 {
952    Render_Engine *re = (Render_Engine *)data;
953    Render_Engine_GL_Resource *rsc;
954    int ret = 0;
955
956    // Create internal resource context if it hasn't been created already
957    if ((rsc = eina_tls_get(resource_key)) == NULL)
958      {
959         if ((rsc = _create_internal_glue_resources(re)) == NULL)
960           {
961              ERR("Error creating internal resources.");
962              return 0;
963           }
964      }
965
966    // Use resource surface/context to create surface resrouces
967 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
968    // Update the evas' window surface
969    if (eina_main_loop_is()) rsc->surface = re->win->egl_surface[0];
970
971    ret = eglMakeCurrent(re->win->egl_disp, rsc->surface, rsc->surface, rsc->context);
972
973    if (!ret)
974      {
975         ERR("eglMakeCurrent() failed. Error Code: %#x", eglGetError());
976         return 0;
977      }
978 #else
979    ret = glXMakeCurrent(re->info->info.display, re->win->win, rsc->context);
980    if (!ret)
981      {
982         ERR("glXMakeCurrent()!");
983         return 0;
984      }
985 #endif
986    return 1;
987 }
988
989 static int
990 eng_setup(Evas *e, void *in)
991 {
992    Render_Engine *re;
993    Evas_Engine_Info_GL_X11 *info;
994
995    info = (Evas_Engine_Info_GL_X11 *)in;
996    if (!e->engine.data.output)
997      {
998 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
999 #else
1000         int eb, evb;
1001
1002         if (!glXQueryExtension(info->info.display, &eb, &evb)) return 0;
1003 #endif
1004         re = calloc(1, sizeof(Render_Engine));
1005         if (!re) return 0;
1006         re->info = info;
1007         re->evas = e;
1008         e->engine.data.output = re;
1009         re->w = e->output.w;
1010         re->h = e->output.h;
1011         re->win = eng_window_new(re->info->info.display,
1012                                  re->info->info.drawable,
1013                                  re->info->info.screen,
1014                                  re->info->info.visual,
1015                                  re->info->info.colormap,
1016                                  re->info->info.depth,
1017                                  re->w,
1018                                  re->h,
1019                                  re->info->indirect,
1020                                  re->info->info.destination_alpha,
1021                                  re->info->info.rotation);
1022         if (!re->win)
1023           {
1024              free(re);
1025              e->engine.data.output = NULL;
1026              return 0;
1027           }
1028
1029         gl_wins++;
1030 /*
1031           {
1032              int status;
1033              char *type = NULL;
1034              XrmValue val;
1035
1036              re->xr.dpi = 75000; // dpy * 1000
1037
1038              status = xrdb_user_query("Xft.dpi", "Xft.Dpi", &type, &val);
1039              if ((!status) || (!type))
1040                {
1041                   if (!re->xrdb) re->xrdb = XrmGetDatabase(re->info->info.display);
1042                   if (re->xrdb)
1043                     status = XrmGetResource(re->xrdb,
1044                                             "Xft.dpi", "Xft.Dpi", &type, &val);
1045                }
1046
1047              if ((status) && (type))
1048                {
1049                   if (!strcmp(type, "String"))
1050                     {
1051                        const char *str, *dp;
1052
1053                        str = val.addr;
1054                        dp = strchr(str, '.');
1055                        if (!dp) dp = strchr(str, ',');
1056
1057                        if (dp)
1058                          {
1059                             int subdpi, len, i;
1060                             char *buf;
1061
1062                             buf = alloca(dp - str + 1);
1063                             strncpy(buf, str, dp - str);
1064                             buf[dp - str] = 0;
1065                             len = strlen(dp + 1);
1066                             subdpi = atoi(dp + 1);
1067
1068                             if (len < 3)
1069                               {
1070                                  for (i = len; i < 3; i++) subdpi *= 10;
1071                               }
1072                             else if (len > 3)
1073                               {
1074                                  for (i = len; i > 3; i--) subdpi /= 10;
1075                               }
1076                             re->xr.dpi = atoi(buf) * 1000;
1077                          }
1078                        else
1079                          re->xr.dpi = atoi(str) * 1000;
1080                        evas_common_font_dpi_set(re->xr.dpi / 1000);
1081                     }
1082                }
1083           }
1084  */
1085         if (!initted)
1086           {
1087              evas_common_cpu_init();
1088
1089              evas_common_blend_init();
1090              evas_common_image_init();
1091              evas_common_convert_init();
1092              evas_common_scale_init();
1093              evas_common_rectangle_init();
1094              evas_common_polygon_init();
1095              evas_common_line_init();
1096              evas_common_font_init();
1097              evas_common_draw_init();
1098              evas_common_tilebuf_init();
1099
1100              // Initialize TLS
1101              if (eina_tls_new(&resource_key) == EINA_FALSE)
1102                 ERR("Error creating tls key");
1103              DBG("TLS KEY create... %d", resource_key);
1104
1105              initted = 1;
1106           }
1107      }
1108    else
1109      {
1110         re = e->engine.data.output;
1111         if (_re_wincheck(re))
1112           {
1113              if ((re->info->info.display != re->win->disp) ||
1114                  (re->info->info.drawable != re->win->win) ||
1115                  (re->info->info.screen != re->win->screen) ||
1116                  (re->info->info.visual != re->win->visual) ||
1117                  (re->info->info.colormap != re->win->colormap) ||
1118                  (re->info->info.depth != re->win->depth) ||
1119                  (re->info->info.destination_alpha != re->win->alpha) ||
1120                  (re->info->info.rotation != re->win->rot))
1121                {
1122                   int inc = 0;
1123
1124                   if (re->win)
1125                     {
1126                        re->win->gl_context->references++;
1127                        eng_window_free(re->win);
1128                        inc = 1;
1129                        gl_wins--;
1130                     }
1131                   re->w = e->output.w;
1132                   re->h = e->output.h;
1133                   re->win = eng_window_new(re->info->info.display,
1134                                            re->info->info.drawable,
1135                                            re->info->info.screen,
1136                                            re->info->info.visual,
1137                                            re->info->info.colormap,
1138                                            re->info->info.depth,
1139                                            re->w,
1140                                            re->h,
1141                                            re->info->indirect,
1142                                            re->info->info.destination_alpha,
1143                                            re->info->info.rotation);
1144                   eng_window_use(re->win);
1145                   if (re->win) gl_wins++;
1146                   if ((re->win) && (inc))
1147                      re->win->gl_context->references--;
1148                }
1149              else if ((re->win->w != e->output.w) ||
1150                       (re->win->h != e->output.h))
1151                {
1152                   re->w = e->output.w;
1153                   re->h = e->output.h;
1154                   re->win->w = e->output.w;
1155                   re->win->h = e->output.h;
1156                   eng_window_use(re->win);
1157                   evas_gl_common_context_resize(re->win->gl_context, re->win->w, re->win->h, re->win->rot);
1158                }
1159           }
1160      }
1161    if (!re->win)
1162      {
1163         free(re);
1164         return 0;
1165      }
1166
1167    if (!e->engine.data.output)
1168      {
1169         if (re->win)
1170           {
1171              eng_window_free(re->win);
1172              gl_wins--;
1173           }
1174         free(re);
1175         return 0;
1176      }
1177    re->tb = evas_common_tilebuf_new(re->win->w, re->win->h);
1178    if (!re->tb)
1179      {
1180         if (re->win)
1181           {
1182              eng_window_free(re->win);
1183              gl_wins--;
1184           }
1185         free(re);
1186         return 0;
1187      }
1188    evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
1189
1190    if (!e->engine.data.context)
1191      e->engine.data.context =
1192      e->engine.func->context_new(e->engine.data.output);
1193    eng_window_use(re->win);
1194
1195    re->vsync = 0;
1196
1197    if (!_ext_initted)
1198      {
1199         _gl_ext_sym_init();
1200         _gl_ext_init(re);
1201         _ext_initted = 1 ;
1202      }
1203
1204    return 1;
1205 }
1206
1207 static void
1208 eng_output_free(void *data)
1209 {
1210    Render_Engine *re;
1211
1212    re = (Render_Engine *)data;
1213
1214    if (re)
1215      {
1216 // NOTE: XrmGetDatabase() result is shared per connection, do not free it.
1217 //   if (re->xrdb) XrmDestroyDatabase(re->xrdb);
1218
1219 #if 0
1220 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1221         // Destroy the resource surface
1222         // Only required for EGL case
1223         if (re->surface)
1224            eglDestroySurface(re->win->egl_disp, re->surface);
1225 #endif
1226
1227         // Destroy the resource context
1228         _destroy_internal_context(re, context);
1229 #endif
1230         if (re->win)
1231           {
1232              if ((initted == 1) && (gl_wins == 1))
1233                   _destroy_internal_glue_resources(re);
1234              eng_window_free(re->win);
1235              gl_wins--;
1236           }
1237         evas_common_tilebuf_free(re->tb);
1238         free(re);
1239      }
1240    if ((initted == 1) && (gl_wins == 0))
1241      {
1242         evas_common_image_shutdown();
1243         evas_common_font_shutdown();
1244         initted = 0;
1245      }
1246 }
1247
1248 static void
1249 eng_output_resize(void *data, int w, int h)
1250 {
1251    Render_Engine *re;
1252
1253    re = (Render_Engine *)data;
1254    re->win->w = w;
1255    re->win->h = h;
1256    eng_window_use(re->win);
1257    evas_gl_common_context_resize(re->win->gl_context, w, h, re->win->rot);
1258    evas_common_tilebuf_free(re->tb);
1259    re->tb = evas_common_tilebuf_new(w, h);
1260    if (re->tb)
1261      evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
1262 }
1263
1264 static void
1265 eng_output_tile_size_set(void *data, int w, int h)
1266 {
1267    Render_Engine *re;
1268
1269    re = (Render_Engine *)data;
1270    evas_common_tilebuf_set_tile_size(re->tb, w, h);
1271 }
1272
1273 static void
1274 eng_output_redraws_rect_add(void *data, int x, int y, int w, int h)
1275 {
1276    Render_Engine *re;
1277
1278    re = (Render_Engine *)data;
1279    eng_window_use(re->win);
1280    evas_gl_common_context_resize(re->win->gl_context, re->win->w, re->win->h, re->win->rot);
1281    evas_common_tilebuf_add_redraw(re->tb, x, y, w, h);
1282
1283    RECTS_CLIP_TO_RECT(x, y, w, h, 0, 0, re->win->w, re->win->h);
1284    if ((w <= 0) || (h <= 0)) return;
1285    if (!re->win->draw.redraw)
1286      {
1287 #if 1
1288         re->win->draw.x1 = x;
1289         re->win->draw.y1 = y;
1290         re->win->draw.x2 = x + w - 1;
1291         re->win->draw.y2 = y + h - 1;
1292 #else
1293         re->win->draw.x1 = 0;
1294         re->win->draw.y1 = 0;
1295         re->win->draw.x2 = re->win->w - 1;
1296         re->win->draw.y2 = re->win->h - 1;
1297 #endif
1298      }
1299    else
1300      {
1301         if (x < re->win->draw.x1) re->win->draw.x1 = x;
1302         if (y < re->win->draw.y1) re->win->draw.y1 = y;
1303         if ((x + w - 1) > re->win->draw.x2) re->win->draw.x2 = x + w - 1;
1304         if ((y + h - 1) > re->win->draw.y2) re->win->draw.y2 = y + h - 1;
1305      }
1306    re->win->draw.redraw = 1;
1307 }
1308
1309 static void
1310 eng_output_redraws_rect_del(void *data, int x, int y, int w, int h)
1311 {
1312    Render_Engine *re;
1313
1314    re = (Render_Engine *)data;
1315    evas_common_tilebuf_del_redraw(re->tb, x, y, w, h);
1316 }
1317
1318 static void
1319 eng_output_redraws_clear(void *data)
1320 {
1321    Render_Engine *re;
1322
1323    re = (Render_Engine *)data;
1324    evas_common_tilebuf_clear(re->tb);
1325 /*   re->win->draw.redraw = 0;*/
1326 //   INF("GL: finish update cycle!");
1327 }
1328
1329 /* vsync games - not for now though */
1330 #define VSYNC_TO_SCREEN 1
1331
1332 static void *
1333 eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, int *cx, int *cy, int *cw, int *ch)
1334 {
1335    Render_Engine *re;
1336    Tilebuf_Rect *rects;
1337
1338    re = (Render_Engine *)data;
1339    /* get the upate rect surface - return engine data as dummy */
1340    rects = evas_common_tilebuf_get_render_rects(re->tb);
1341    if (rects)
1342      {
1343 /*        
1344         Tilebuf_Rect *r;
1345         
1346         printf("REAAAAACCTS\n");
1347         EINA_INLIST_FOREACH(EINA_INLIST_GET(rects), r)
1348           {
1349              printf("  %i %i %ix%i\n", r->x, r->y, r->w, r->h);
1350           }
1351  */
1352         evas_common_tilebuf_free_render_rects(rects);
1353         evas_common_tilebuf_clear(re->tb);
1354 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1355         // dont need to for egl - eng_window_use() can check for other ctxt's
1356 #else
1357         eng_window_use(NULL);
1358 #endif
1359         eng_window_use(re->win);
1360         if (!_re_wincheck(re)) return NULL;
1361         evas_gl_common_context_flush(re->win->gl_context);
1362         evas_gl_common_context_newframe(re->win->gl_context);
1363         if (x) *x = 0;
1364         if (y) *y = 0;
1365         if (w) *w = re->win->w;
1366         if (h) *h = re->win->h;
1367         if (cx) *cx = 0;
1368         if (cy) *cy = 0;
1369         if (cw) *cw = re->win->w;
1370         if (ch) *ch = re->win->h;
1371         return re->win->gl_context->def_surface;
1372      }
1373    return NULL;
1374 /*   
1375    if (!re->win->draw.redraw) return NULL;
1376 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1377    // dont need to for egl - eng_window_use() can check for other ctxt's
1378 #else
1379    eng_window_use(NULL);
1380 #endif
1381    eng_window_use(re->win);
1382    if (!_re_wincheck(re)) return NULL;
1383    evas_gl_common_context_flush(re->win->gl_context);
1384    evas_gl_common_context_newframe(re->win->gl_context);
1385    if (x) *x = re->win->draw.x1;
1386    if (y) *y = re->win->draw.y1;
1387    if (w) *w = re->win->draw.x2 - re->win->draw.x1 + 1;
1388    if (h) *h = re->win->draw.y2 - re->win->draw.y1 + 1;
1389    if (cx) *cx = re->win->draw.x1;
1390    if (cy) *cy = re->win->draw.y1;
1391    if (cw) *cw = re->win->draw.x2 - re->win->draw.x1 + 1;
1392    if (ch) *ch = re->win->draw.y2 - re->win->draw.y1 + 1;
1393    return re->win->gl_context->def_surface;
1394  */
1395 }
1396
1397 //#define FRAMECOUNT 1
1398
1399 #ifdef FRAMECOUNT
1400 static double
1401 get_time(void)
1402 {
1403    struct timeval timev;
1404
1405    gettimeofday(&timev, NULL);
1406    return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
1407 }
1408 #endif
1409
1410 static int safe_native = -1;
1411
1412 static void
1413 eng_output_redraws_next_update_push(void *data, void *surface __UNUSED__, int x __UNUSED__, int y __UNUSED__, int w __UNUSED__, int h __UNUSED__)
1414 {
1415    Render_Engine *re;
1416 #ifdef FRAMECOUNT
1417    static double pt = 0.0;
1418    double ta, tb;
1419 #endif
1420
1421    re = (Render_Engine *)data;
1422    /* put back update surface.. in this case just unflag redraw */
1423    if (!_re_wincheck(re)) return;
1424    re->win->draw.redraw = 0;
1425    re->win->draw.drew = 1;
1426    evas_gl_common_context_flush(re->win->gl_context);
1427    if (safe_native == -1)
1428      {
1429         const char *s = getenv("EVAS_GL_SAFE_NATIVE");
1430         safe_native = 0;
1431         if (s) safe_native = atoi(s);
1432         else
1433           {
1434              s = (const char *)glGetString(GL_RENDERER);
1435              if (s)
1436                {
1437                   if (strstr(s, "PowerVR SGX 540") ||
1438                       strstr(s, "Mali-400 MP"))
1439                     safe_native = 1;
1440                }
1441           }
1442      }
1443 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1444    // this is needed to make sure all previous rendering is flushed to
1445    // buffers/surfaces
1446 #ifdef FRAMECOUNT
1447    double t0 = get_time();
1448    ta = t0 - pt;
1449    pt = t0;
1450 #endif
1451    // previous rendering should be done and swapped
1452    if (!safe_native) eglWaitNative(EGL_CORE_NATIVE_ENGINE);
1453 #ifdef FRAMECOUNT
1454    double t1 = get_time();
1455    tb = t1 - t0;
1456    printf("... %1.5f -> %1.5f | ", ta, tb);
1457 #endif
1458 //   if (eglGetError() != EGL_SUCCESS)
1459 //     {
1460 //        printf("Error:  eglWaitNative(EGL_CORE_NATIVE_ENGINE) fail.\n");
1461 //     }
1462 #else
1463    // previous rendering should be done and swapped
1464    if (!safe_native) glXWaitX();
1465 #endif
1466 //x//   printf("frame -> push\n");
1467 }
1468
1469 static void
1470 eng_output_flush(void *data)
1471 {
1472    Render_Engine *re;
1473
1474    re = (Render_Engine *)data;
1475    if (!_re_wincheck(re)) return;
1476    if (!re->win->draw.drew) return;
1477 //x//   printf("frame -> flush\n");
1478    re->win->draw.drew = 0;
1479    eng_window_use(re->win);
1480
1481 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1482 #ifdef FRAMECOUNT
1483    double t0 = get_time();
1484 #endif
1485    if (!re->vsync)
1486      {
1487         if (re->info->vsync) eglSwapInterval(re->win->egl_disp, 1);
1488         else eglSwapInterval(re->win->egl_disp, 0);
1489         re->vsync = 1;
1490      }
1491    if (re->info->callback.pre_swap)
1492      {
1493         re->info->callback.pre_swap(re->info->callback.data, re->evas);
1494      }
1495    eglSwapBuffers(re->win->egl_disp, re->win->egl_surface[0]);
1496    if (!safe_native) eglWaitGL();
1497    if (re->info->callback.post_swap)
1498      {
1499         re->info->callback.post_swap(re->info->callback.data, re->evas);
1500      }
1501 #ifdef FRAMECOUNT
1502    double t1 = get_time();
1503    printf("%1.5f\n", t1 - t0);
1504 #endif
1505 //   if (eglGetError() != EGL_SUCCESS)
1506 //     {
1507 //        printf("Error:  eglSwapBuffers() fail.\n");
1508 //     }
1509 #else
1510 #ifdef VSYNC_TO_SCREEN
1511    if ((re->info->vsync)/* || (1)*/)
1512      {
1513         if (glsym_glXSwapIntervalEXT)
1514           {
1515              if (!re->vsync)
1516                {
1517                   if (re->info->vsync) glsym_glXSwapIntervalEXT(re->win->disp, re->win->win, 1);
1518                   else glsym_glXSwapIntervalEXT(re->win->disp, re->win->win, 0);
1519                   re->vsync = 1;
1520                }
1521           }
1522         if (glsym_glXSwapIntervalSGI)
1523           {
1524              if (!re->vsync)
1525                {
1526                   if (re->info->vsync) glsym_glXSwapIntervalSGI(1);
1527                   else glsym_glXSwapIntervalSGI(0);
1528                   re->vsync = 1;
1529                }
1530           }
1531         else
1532           {
1533              if ((glsym_glXGetVideoSync) && (glsym_glXWaitVideoSync))
1534                {
1535                   unsigned int rc;
1536
1537                   glsym_glXGetVideoSync(&rc);
1538                   glsym_glXWaitVideoSync(1, 0, &rc);
1539                }
1540           }
1541      }
1542 # endif
1543    if (re->info->callback.pre_swap)
1544      {
1545         re->info->callback.pre_swap(re->info->callback.data, re->evas);
1546      }
1547 #if 1
1548    if (1)
1549 #else
1550    if ((re->win->draw.x1 == 0) && (re->win->draw.y1 == 0) && (re->win->draw.x2 == (re->win->w - 1)) && (re->win->draw.y2 == (re->win->h - 1)))
1551 #endif     
1552      {
1553 //        double t, t2 = 0.0;
1554 //        t = get_time();
1555         glXSwapBuffers(re->win->disp, re->win->win);
1556 //        t = get_time() - t;
1557 //        if (!safe_native)
1558 //          {
1559 //             t2 = get_time();
1560 //             glXWaitGL();
1561 //             t2 = get_time() - t2;
1562 //          }
1563 //        printf("swap: %3.5f (%3.5fms), x wait gl: %3.5f (%3.5fms)\n", 
1564 //               t, t * 1000.0, t2, t2 * 1000.0);
1565      }
1566    else
1567      {
1568 // FIXME: this doesn't work.. why oh why?
1569         int sx, sy, sw, sh;
1570
1571         sx = re->win->draw.x1;
1572         sy = re->win->draw.y1;
1573         sw = (re->win->draw.x2 - re->win->draw.x1) + 1;
1574         sh = (re->win->draw.y2 - re->win->draw.y1) + 1;
1575         sy = re->win->h - sy - sh;
1576         
1577         glBitmap(0, 0, 0, 0, sx, re->win->h - sy, NULL);
1578         glEnable(GL_SCISSOR_TEST);
1579         glScissor(sx, sy, sw, sh);
1580         glDrawBuffer(GL_FRONT);
1581         glCopyPixels(sx, sy, sw, sh, GL_COLOR);
1582         glDrawBuffer(GL_BACK);
1583         glDisable(GL_SCISSOR_TEST);
1584         glBitmap(0, 0, 0, 0, 0, 0, NULL);
1585         glFlush();
1586      }
1587    if (re->info->callback.post_swap)
1588      {
1589         re->info->callback.post_swap(re->info->callback.data, re->evas);
1590      }
1591 #endif
1592 }
1593
1594 static void
1595 eng_output_idle_flush(void *data)
1596 {
1597    Render_Engine *re;
1598
1599    re = (Render_Engine *)data;
1600 }
1601
1602 static void
1603 eng_output_dump(void *data)
1604 {
1605    Render_Engine *re;
1606
1607    re = (Render_Engine *)data;
1608    evas_common_image_image_all_unload();
1609    evas_common_font_font_all_unload();
1610    evas_gl_common_image_all_unload(re->win->gl_context);
1611    _re_winfree(re);
1612 }
1613
1614 static void
1615 eng_context_cutout_add(void *data __UNUSED__, void *context, int x, int y, int w, int h)
1616 {
1617 //   Render_Engine *re;
1618 //
1619 //   re = (Render_Engine *)data;
1620 //   re->win->gl_context->dc = context;
1621    evas_common_draw_context_add_cutout(context, x, y, w, h);
1622 }
1623
1624 static void
1625 eng_context_cutout_clear(void *data __UNUSED__, void *context)
1626 {
1627 //   Render_Engine *re;
1628 //
1629 //   re = (Render_Engine *)data;
1630 //   re->win->gl_context->dc = context;
1631    evas_common_draw_context_clear_cutouts(context);
1632 }
1633
1634 static void
1635 eng_rectangle_draw(void *data, void *context, void *surface, int x, int y, int w, int h)
1636 {
1637    Render_Engine *re;
1638
1639    re = (Render_Engine *)data;
1640    eng_window_use(re->win);
1641    evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
1642    re->win->gl_context->dc = context;
1643    evas_gl_common_rect_draw(re->win->gl_context, x, y, w, h);
1644 }
1645
1646 static void
1647 eng_line_draw(void *data, void *context, void *surface, int x1, int y1, int x2, int y2)
1648 {
1649    Render_Engine *re;
1650
1651    re = (Render_Engine *)data;
1652    eng_window_use(re->win);
1653    evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
1654    re->win->gl_context->dc = context;
1655    evas_gl_common_line_draw(re->win->gl_context, x1, y1, x2, y2);
1656 }
1657
1658 static void *
1659 eng_polygon_point_add(void *data, void *context __UNUSED__, void *polygon, int x, int y)
1660 {
1661    Render_Engine *re;
1662
1663    re = (Render_Engine *)data;
1664    return evas_gl_common_poly_point_add(polygon, x, y);
1665 }
1666
1667 static void *
1668 eng_polygon_points_clear(void *data, void *context __UNUSED__, void *polygon)
1669 {
1670    Render_Engine *re;
1671
1672    re = (Render_Engine *)data;
1673    return evas_gl_common_poly_points_clear(polygon);
1674 }
1675
1676 static void
1677 eng_polygon_draw(void *data, void *context, void *surface __UNUSED__, void *polygon, int x, int y)
1678 {
1679    Render_Engine *re;
1680
1681    re = (Render_Engine *)data;
1682    eng_window_use(re->win);
1683    evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
1684    re->win->gl_context->dc = context;
1685    evas_gl_common_poly_draw(re->win->gl_context, polygon, x, y);
1686 }
1687
1688 static int
1689 eng_image_alpha_get(void *data __UNUSED__, void *image)
1690 {
1691 //   Render_Engine *re;
1692    Evas_GL_Image *im;
1693
1694 //   re = (Render_Engine *)data;
1695    if (!image) return 1;
1696    im = image;
1697    return im->alpha;
1698 }
1699
1700 static int
1701 eng_image_colorspace_get(void *data __UNUSED__, void *image)
1702 {
1703 //   Render_Engine *re;
1704    Evas_GL_Image *im;
1705
1706 //   re = (Render_Engine *)data;
1707    if (!image) return EVAS_COLORSPACE_ARGB8888;
1708    im = image;
1709    return im->cs.space;
1710 }
1711
1712 static void
1713 eng_image_mask_create(void *data __UNUSED__, void *image)
1714 {
1715    Evas_GL_Image *im;
1716
1717    if (!image) return;
1718    im = image;
1719    if (!im->im->image.data)
1720       evas_cache_image_load_data(&im->im->cache_entry);
1721    if (!im->tex)
1722       im->tex = evas_gl_common_texture_new(im->gc, im->im);
1723 }
1724
1725
1726 static void *
1727 eng_image_alpha_set(void *data, void *image, int has_alpha)
1728 {
1729    Render_Engine *re;
1730    Evas_GL_Image *im;
1731
1732    re = (Render_Engine *)data;
1733    if (!image) return NULL;
1734    im = image;
1735    if (im->alpha == has_alpha) return image;
1736    if (im->native.data)
1737      {
1738         im->alpha = has_alpha;
1739         return image;
1740      }
1741    eng_window_use(re->win);
1742    if ((im->tex) && (im->tex->pt->dyn.img))
1743      {
1744         im->alpha = has_alpha;
1745         im->tex->alpha = im->alpha;
1746         return image;
1747      }
1748    /* FIXME: can move to gl_common */
1749    if (im->cs.space != EVAS_COLORSPACE_ARGB8888) return im;
1750    if ((has_alpha) && (im->im->cache_entry.flags.alpha)) return image;
1751    else if ((!has_alpha) && (!im->im->cache_entry.flags.alpha)) return image;
1752    if (im->references > 1)
1753      {
1754         Evas_GL_Image *im_new;
1755
1756         im_new = evas_gl_common_image_new_from_copied_data
1757            (im->gc, im->im->cache_entry.w, im->im->cache_entry.h,
1758                im->im->image.data,
1759                eng_image_alpha_get(data, image),
1760                eng_image_colorspace_get(data, image));
1761         if (!im_new) return im;
1762         evas_gl_common_image_free(im);
1763         im = im_new;
1764      }
1765    else
1766      evas_gl_common_image_dirty(im, 0, 0, 0, 0);
1767    return evas_gl_common_image_alpha_set(im, has_alpha ? 1 : 0);
1768 //   im->im->cache_entry.flags.alpha = has_alpha ? 1 : 0;
1769 //   return image;
1770 }
1771
1772 static void *
1773 eng_image_border_set(void *data __UNUSED__, void *image, int l __UNUSED__, int r __UNUSED__, int t __UNUSED__, int b __UNUSED__)
1774 {
1775 //   Render_Engine *re;
1776 //
1777 //   re = (Render_Engine *)data;
1778    return image;
1779 }
1780
1781 static void
1782 eng_image_border_get(void *data __UNUSED__, void *image __UNUSED__, int *l __UNUSED__, int *r __UNUSED__, int *t __UNUSED__, int *b __UNUSED__)
1783 {
1784 //   Render_Engine *re;
1785 //
1786 //   re = (Render_Engine *)data;
1787 }
1788
1789 static char *
1790 eng_image_comment_get(void *data __UNUSED__, void *image, char *key __UNUSED__)
1791 {
1792 //   Render_Engine *re;
1793    Evas_GL_Image *im;
1794
1795 //   re = (Render_Engine *)data;
1796    if (!image) return NULL;
1797    im = image;
1798    if (!im->im) return NULL;
1799    return im->im->info.comment;
1800 }
1801
1802 static char *
1803 eng_image_format_get(void *data __UNUSED__, void *image)
1804 {
1805 //   Render_Engine *re;
1806    Evas_GL_Image *im;
1807
1808 //   re = (Render_Engine *)data;
1809    im = image;
1810    return NULL;
1811 }
1812
1813 static void
1814 eng_image_colorspace_set(void *data, void *image, int cspace)
1815 {
1816    Render_Engine *re;
1817    Evas_GL_Image *im;
1818
1819    re = (Render_Engine *)data;
1820    if (!image) return;
1821    im = image;
1822    if (im->native.data) return;
1823    /* FIXME: can move to gl_common */
1824    if (im->cs.space == cspace) return;
1825    eng_window_use(re->win);
1826    evas_cache_image_colorspace(&im->im->cache_entry, cspace);
1827    switch (cspace)
1828      {
1829       case EVAS_COLORSPACE_ARGB8888:
1830          if (im->cs.data)
1831            {
1832               if (!im->cs.no_free) free(im->cs.data);
1833               im->cs.data = NULL;
1834               im->cs.no_free = 0;
1835            }
1836          break;
1837       case EVAS_COLORSPACE_YCBCR422P601_PL:
1838       case EVAS_COLORSPACE_YCBCR422P709_PL:
1839       case EVAS_COLORSPACE_YCBCR422601_PL:
1840       case EVAS_COLORSPACE_YCBCR420NV12601_PL:
1841       case EVAS_COLORSPACE_YCBCR420TM12601_PL:
1842          if (im->tex) evas_gl_common_texture_free(im->tex);
1843          im->tex = NULL;
1844          if (im->cs.data)
1845            {
1846               if (!im->cs.no_free) free(im->cs.data);
1847            }
1848          if (im->im->cache_entry.h > 0)
1849            im->cs.data =
1850               calloc(1, im->im->cache_entry.h * sizeof(unsigned char *) * 2);
1851          else
1852            im->cs.data = NULL;
1853          im->cs.no_free = 0;
1854          break;
1855       default:
1856          abort();
1857          break;
1858      }
1859    im->cs.space = cspace;
1860 }
1861
1862 /////////////////////////////////////////////////////////////////////////
1863 //
1864 //
1865 typedef struct _Native Native;
1866
1867 struct _Native
1868 {
1869    Evas_Native_Surface ns;
1870    Pixmap     pixmap;
1871    Visual    *visual;
1872
1873 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1874    void      *egl_surface;
1875 #else
1876    void  *fbc;
1877    XID    glx_pixmap;
1878 #endif
1879 };
1880
1881 // FIXME: this is enabled so updates happen - but its SLOOOOOOOOOOOOOOOW
1882 // (i am sure this is the reason)  not to mention seemingly superfluous. but
1883 // i need to enable it for it to work on fglrx at least. havent tried nvidia.
1884 //
1885 // why is this the case? does anyone know? has anyone tried it on other gfx
1886 // drivers?
1887 //
1888 //#define GLX_TEX_PIXMAP_RECREATE 1
1889
1890 static void
1891 _native_bind_cb(void *data, void *image)
1892 {
1893    Evas_GL_Image *im = image;
1894    Native *n = im->native.data;
1895
1896   if (n->ns.type == EVAS_NATIVE_SURFACE_X11)
1897     {
1898 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1899       if (n->egl_surface)
1900         {
1901           if (glsym_glEGLImageTargetTexture2DOES)
1902             {
1903               glsym_glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, n->egl_surface);
1904               if (eglGetError() != EGL_SUCCESS)
1905                 ERR("glEGLImageTargetTexture2DOES() failed.");
1906             }
1907           else
1908             ERR("Try glEGLImageTargetTexture2DOES on EGL with no support");
1909         }
1910 #else
1911 # ifdef GLX_BIND_TO_TEXTURE_TARGETS_EXT
1912       Render_Engine *re = data;
1913
1914       if (glsym_glXBindTexImage)
1915         {
1916           glsym_glXBindTexImage(re->win->disp, n->glx_pixmap,
1917                                 GLX_FRONT_LEFT_EXT, NULL);
1918           GLERR(__FUNCTION__, __FILE__, __LINE__, "");
1919         }
1920       else
1921         ERR("Try glXBindTexImage on GLX with no support");
1922 # endif
1923 #endif
1924     }
1925   else if (n->ns.type == EVAS_NATIVE_SURFACE_OPENGL)
1926     {
1927       glBindTexture(GL_TEXTURE_2D, n->ns.data.opengl.texture_id);
1928       GLERR(__FUNCTION__, __FILE__, __LINE__, "");
1929     }
1930    return;
1931    data = NULL;
1932 }
1933
1934 static void
1935 _native_unbind_cb(void *data, void *image)
1936 {
1937   Evas_GL_Image *im = image;
1938   Native *n = im->native.data;
1939
1940   if (n->ns.type == EVAS_NATIVE_SURFACE_X11)
1941     {
1942 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1943       // nothing
1944 #else
1945 # ifdef GLX_BIND_TO_TEXTURE_TARGETS_EXT
1946       Render_Engine *re = data;
1947
1948       if (glsym_glXReleaseTexImage)
1949         {
1950           glsym_glXReleaseTexImage(re->win->disp, n->glx_pixmap,
1951                                    GLX_FRONT_LEFT_EXT);
1952           GLERR(__FUNCTION__, __FILE__, __LINE__, "");
1953         }
1954       else
1955         ERR("Try glXReleaseTexImage on GLX with no support");
1956 # endif
1957 #endif
1958     }
1959   else if (n->ns.type == EVAS_NATIVE_SURFACE_OPENGL)
1960     {
1961       glBindTexture(GL_TEXTURE_2D, 0);
1962       GLERR(__FUNCTION__, __FILE__, __LINE__, "");
1963     }
1964    return;
1965    data = NULL;
1966 }
1967
1968 static void
1969 _native_free_cb(void *data, void *image)
1970 {
1971   Render_Engine *re = data;
1972   Evas_GL_Image *im = image;
1973   Native *n = im->native.data;
1974   uint32_t pmid, texid;
1975
1976   if (n->ns.type == EVAS_NATIVE_SURFACE_X11)
1977     {
1978       pmid = n->pixmap;
1979       eina_hash_del(re->win->gl_context->shared->native_pm_hash, &pmid, im);
1980 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
1981       if (n->egl_surface)
1982         {
1983           if (glsym_eglDestroyImage)
1984             {
1985               glsym_eglDestroyImage(re->win->egl_disp,
1986                                     n->egl_surface);
1987               if (eglGetError() != EGL_SUCCESS)
1988                 ERR("eglDestroyImage() failed.");
1989             }
1990           else
1991             ERR("Try eglDestroyImage on EGL with no support");
1992         }
1993 #else
1994 # ifdef GLX_BIND_TO_TEXTURE_TARGETS_EXT
1995       if (n->glx_pixmap)
1996         {
1997           if (im->native.loose)
1998             {
1999               if (glsym_glXReleaseTexImage)
2000                 {
2001                   glsym_glXReleaseTexImage(re->win->disp, n->glx_pixmap,
2002                                            GLX_FRONT_LEFT_EXT);
2003                   GLERR(__FUNCTION__, __FILE__, __LINE__, "");
2004                 }
2005               else
2006                 ERR("Try glXReleaseTexImage on GLX with no support");
2007             }
2008           if (glsym_glXDestroyPixmap)
2009             {
2010               glsym_glXDestroyPixmap(re->win->disp, n->glx_pixmap);
2011               GLERR(__FUNCTION__, __FILE__, __LINE__, "");
2012             }
2013           else
2014             ERR("Try glXDestroyPixmap on GLX with no support");
2015           n->glx_pixmap = 0;
2016         }
2017 # endif
2018 #endif
2019     }
2020   else if (n->ns.type == EVAS_NATIVE_SURFACE_OPENGL)
2021     {
2022       texid = n->ns.data.opengl.texture_id;
2023       eina_hash_del(re->win->gl_context->shared->native_tex_hash, &texid, im);
2024     }
2025   im->native.data        = NULL;
2026   im->native.func.data   = NULL;
2027   im->native.func.bind   = NULL;
2028   im->native.func.unbind = NULL;
2029   im->native.func.free   = NULL;
2030   free(n);
2031 }
2032
2033 static void *
2034 eng_image_native_set(void *data, void *image, void *native)
2035 {
2036   Render_Engine *re = (Render_Engine *)data;
2037   Evas_Native_Surface *ns = native;
2038   Evas_GL_Image *im = image, *im2 = NULL;
2039   Visual *vis = NULL;
2040   Pixmap pm = 0;
2041   Native *n = NULL;
2042   uint32_t pmid, texid;
2043   unsigned int tex = 0;
2044   unsigned int fbo = 0;
2045
2046   if (!im)
2047     {
2048        if ((!ns) && (ns->type == EVAS_NATIVE_SURFACE_OPENGL))
2049          {
2050             im = evas_gl_common_image_new_from_data(re->win->gl_context,
2051                                                     ns->data.opengl.w,
2052                                                     ns->data.opengl.h,
2053                                                     NULL, 1,
2054                                                     EVAS_COLORSPACE_ARGB8888);
2055          }
2056        else
2057            return NULL;
2058     }
2059
2060   if (ns)
2061     {
2062       if (ns->type == EVAS_NATIVE_SURFACE_X11)
2063         {
2064           vis = ns->data.x11.visual;
2065           pm = ns->data.x11.pixmap;
2066           if (im->native.data)
2067             {
2068               Evas_Native_Surface *ens = im->native.data;
2069               if ((ens->data.x11.visual == vis) &&
2070                   (ens->data.x11.pixmap == pm))
2071                 return im;
2072             }
2073         }
2074       else if (ns->type == EVAS_NATIVE_SURFACE_OPENGL)
2075         {
2076           tex = ns->data.opengl.texture_id;
2077           fbo = ns->data.opengl.framebuffer_id;
2078           if (im->native.data)
2079             {
2080               Evas_Native_Surface *ens = im->native.data;
2081               if ((ens->data.opengl.texture_id == tex) &&
2082                   (ens->data.opengl.framebuffer_id == fbo))
2083                 return im;
2084             }
2085         }
2086     }
2087   if ((!ns) && (!im->native.data)) return im;
2088
2089   eng_window_use(re->win);
2090
2091   if (im->native.data)
2092     {
2093       if (im->native.func.free)
2094         im->native.func.free(im->native.func.data, im);
2095       evas_gl_common_image_native_disable(im);
2096     }
2097
2098   if (!ns) return im;
2099
2100   if (ns->type == EVAS_NATIVE_SURFACE_X11)
2101     {
2102       pmid = pm;
2103       im2 = eina_hash_find(re->win->gl_context->shared->native_pm_hash, &pmid);
2104       if (im2 == im) return im;
2105       if (im2)
2106         {
2107            n = im2->native.data;
2108            if (n)
2109              {
2110                 evas_gl_common_image_ref(im2);
2111                 evas_gl_common_image_free(im);
2112                 return im2;
2113              }
2114         }
2115     }
2116   else if (ns->type == EVAS_NATIVE_SURFACE_OPENGL)
2117     {
2118        texid = tex;
2119        im2 = eina_hash_find(re->win->gl_context->shared->native_tex_hash, &texid);
2120        if (im2 == im) return im;
2121        if (im2)
2122          {
2123             n = im2->native.data;
2124             if (n)
2125               {
2126                  evas_gl_common_image_ref(im2);
2127                  evas_gl_common_image_free(im);
2128                  return im2;
2129               }
2130          }
2131
2132     }
2133   im2 = evas_gl_common_image_new_from_data(re->win->gl_context,
2134                                            im->w, im->h, NULL, im->alpha,
2135                                            EVAS_COLORSPACE_ARGB8888);
2136   evas_gl_common_image_free(im);
2137   im = im2;
2138   if (ns->type == EVAS_NATIVE_SURFACE_X11)
2139     {
2140 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
2141       if (native)
2142         {
2143           n = calloc(1, sizeof(Native));
2144           if (n)
2145             {
2146               EGLConfig egl_config;
2147               int config_attrs[20];
2148               int num_config, i = 0;
2149
2150               eina_hash_add(re->win->gl_context->shared->native_pm_hash, &pmid, im);
2151
2152               config_attrs[i++] = EGL_RED_SIZE;
2153               config_attrs[i++] = 8;
2154               config_attrs[i++] = EGL_GREEN_SIZE;
2155               config_attrs[i++] = 8;
2156               config_attrs[i++] = EGL_BLUE_SIZE;
2157               config_attrs[i++] = 8;
2158               config_attrs[i++] = EGL_ALPHA_SIZE;
2159               config_attrs[i++] = 8;
2160               config_attrs[i++] = EGL_DEPTH_SIZE;
2161               config_attrs[i++] = 0;
2162               config_attrs[i++] = EGL_STENCIL_SIZE;
2163               config_attrs[i++] = 0;
2164               config_attrs[i++] = EGL_RENDERABLE_TYPE;
2165               config_attrs[i++] = EGL_OPENGL_ES2_BIT;
2166               config_attrs[i++] = EGL_SURFACE_TYPE;
2167               config_attrs[i++] = EGL_PIXMAP_BIT;
2168               config_attrs[i++] = EGL_NONE;
2169
2170               if (!eglChooseConfig(re->win->egl_disp, config_attrs,
2171                                    &egl_config, 1, &num_config))
2172                 ERR("eglChooseConfig() failed for pixmap 0x%x, num_config = %i", (unsigned int)pm, num_config);
2173               memcpy(&(n->ns), ns, sizeof(Evas_Native_Surface));
2174               n->pixmap = pm;
2175               n->visual = vis;
2176               if (glsym_eglCreateImage)
2177                 n->egl_surface = glsym_eglCreateImage(re->win->egl_disp,
2178                                                       EGL_NO_CONTEXT,
2179                                                       EGL_NATIVE_PIXMAP_KHR,
2180                                                       (void *)pm,
2181                                                       NULL);
2182               else
2183                 ERR("Try eglCreateImage on EGL with no support");
2184               if (!n->egl_surface)
2185                 ERR("eglCreatePixmapSurface() for 0x%x failed", (unsigned int)pm);
2186               im->native.yinvert     = 1;
2187               im->native.loose       = 0;
2188               im->native.data        = n;
2189               im->native.func.data   = re;
2190               im->native.func.bind   = _native_bind_cb;
2191               im->native.func.unbind = _native_unbind_cb;
2192               im->native.func.free   = _native_free_cb;
2193               im->native.target      = GL_TEXTURE_2D;
2194               im->native.mipmap      = 0;
2195               evas_gl_common_image_native_enable(im);
2196             }
2197         }
2198 #else
2199 # ifdef GLX_BIND_TO_TEXTURE_TARGETS_EXT
2200       if (native)
2201         {
2202           int dummy;
2203           unsigned int w, h, depth = 32, border;
2204           Window wdummy;
2205
2206           // fixme: round trip :(
2207           XGetGeometry(re->win->disp, pm, &wdummy, &dummy, &dummy,
2208                        &w, &h, &border, &depth);
2209           n = calloc(1, sizeof(Native));
2210           if (n)
2211             {
2212               int pixmap_att[20];
2213               unsigned int target = 0;
2214               unsigned int i = 0;
2215
2216               eina_hash_add(re->win->gl_context->shared->native_pm_hash, &pmid, im);
2217               if ((re->win->depth_cfg[depth].tex_target &
2218                    GLX_TEXTURE_2D_BIT_EXT)
2219                   //                 && (1) // we assume npo2 for now
2220                   // size is pow2 || mnpo2 supported
2221                  )
2222                 target = GLX_TEXTURE_2D_EXT;
2223               else if ((re->win->depth_cfg[depth].tex_target &
2224                         GLX_TEXTURE_RECTANGLE_BIT_EXT))
2225                 {
2226                   ERR("rect!!! (not handled)");
2227                   target = GLX_TEXTURE_RECTANGLE_EXT;
2228                 }
2229               if (!target)
2230                 {
2231                   ERR("broken text-from-pixmap");
2232                   if (!(re->win->depth_cfg[depth].tex_target &
2233                         GLX_TEXTURE_2D_BIT_EXT))
2234                     target = GLX_TEXTURE_RECTANGLE_EXT;
2235                   else if (!(re->win->depth_cfg[depth].tex_target &
2236                              GLX_TEXTURE_RECTANGLE_BIT_EXT))
2237                     target = GLX_TEXTURE_2D_EXT;
2238                 }
2239
2240
2241               pixmap_att[i++] = GLX_TEXTURE_FORMAT_EXT;
2242               pixmap_att[i++] = re->win->depth_cfg[depth].tex_format;
2243               pixmap_att[i++] = GLX_MIPMAP_TEXTURE_EXT;
2244               pixmap_att[i++] = re->win->depth_cfg[depth].mipmap;
2245
2246               if (target)
2247                 {
2248                   pixmap_att[i++] = GLX_TEXTURE_TARGET_EXT;
2249                   pixmap_att[i++] = target;
2250                 }
2251
2252               pixmap_att[i++] = 0;
2253
2254               memcpy(&(n->ns), ns, sizeof(Evas_Native_Surface));
2255               n->pixmap = pm;
2256               n->visual = vis;
2257               n->fbc = re->win->depth_cfg[depth].fbc;
2258               if (glsym_glXCreatePixmap)
2259                 n->glx_pixmap = glsym_glXCreatePixmap(re->win->disp,
2260                                                       n->fbc,
2261                                                       n->pixmap,
2262                                                       pixmap_att);
2263               else
2264                 ERR("Try glXCreatePixmap on GLX with no support");
2265               if (n->glx_pixmap)
2266                 {
2267 //                  printf("%p: new native texture for %x | %4i x %4i @ %2i = %p\n",
2268 //                         n, pm, w, h, depth, n->glx_pixmap);
2269                   if (!target)
2270                     {
2271                       ERR("no target :(");
2272                       if (glsym_glXQueryDrawable)
2273                         glsym_glXQueryDrawable(re->win->disp,
2274                                                n->pixmap,
2275                                                GLX_TEXTURE_TARGET_EXT,
2276                                                &target);
2277                     }
2278                   if (target == GLX_TEXTURE_2D_EXT)
2279                     {
2280                       im->native.target = GL_TEXTURE_2D;
2281                       im->native.mipmap = re->win->depth_cfg[depth].mipmap;
2282                     }
2283 #  ifdef GL_TEXTURE_RECTANGLE_ARB
2284                   else if (target == GLX_TEXTURE_RECTANGLE_EXT)
2285                     {
2286                       im->native.target = GL_TEXTURE_RECTANGLE_ARB;
2287                       im->native.mipmap = 0;
2288                     }
2289 #  endif
2290                   else
2291                     {
2292                       im->native.target = GL_TEXTURE_2D;
2293                       im->native.mipmap = 0;
2294                       ERR("still unknown target");
2295                     }
2296                 }
2297               else
2298                 ERR("GLX Pixmap create fail");
2299               im->native.yinvert     = re->win->depth_cfg[depth].yinvert;
2300               im->native.loose       = re->win->detected.loose_binding;
2301               im->native.data        = n;
2302               im->native.func.data   = re;
2303               im->native.func.bind   = _native_bind_cb;
2304               im->native.func.unbind = _native_unbind_cb;
2305               im->native.func.free   = _native_free_cb;
2306
2307               evas_gl_common_image_native_enable(im);
2308             }
2309         }
2310 # endif
2311 #endif
2312     }
2313   else if (ns->type == EVAS_NATIVE_SURFACE_OPENGL)
2314     {
2315       if (native)
2316         {
2317           n = calloc(1, sizeof(Native));
2318           if (n)
2319             {
2320               memcpy(&(n->ns), ns, sizeof(Evas_Native_Surface));
2321
2322               eina_hash_add(re->win->gl_context->shared->native_tex_hash, &texid, im);
2323
2324               n->pixmap = 0;
2325               n->visual = 0;
2326 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
2327               n->egl_surface = 0;
2328 #else
2329               n->fbc = 0;
2330               n->glx_pixmap = 0;
2331 #endif
2332
2333               im->native.yinvert     = 0;
2334               im->native.loose       = 0;
2335               im->native.data        = n;
2336               im->native.func.data   = re;
2337               im->native.func.bind   = _native_bind_cb;
2338               im->native.func.unbind = _native_unbind_cb;
2339               im->native.func.free   = _native_free_cb;
2340               im->native.target      = GL_TEXTURE_2D;
2341               im->native.mipmap      = 0;
2342
2343               // FIXME: need to implement mapping sub texture regions
2344               // x, y, w, h for possible texture atlasing
2345
2346               evas_gl_common_image_native_enable(im);
2347             }
2348         }
2349
2350     }
2351    return im;
2352 }
2353
2354 static void *
2355 eng_image_native_get(void *data __UNUSED__, void *image)
2356 {
2357    Evas_GL_Image *im = image;
2358    Native *n;
2359    if (!im) return NULL;
2360    n = im->native.data;
2361    if (!n) return NULL;
2362    return &(n->ns);
2363 }
2364
2365 #if 0 // filtering disabled
2366 static void
2367 eng_image_draw_filtered(void *data, void *context, void *surface,
2368                         void *image, Evas_Filter_Info *filter)
2369 {
2370    Render_Engine *re = data;
2371
2372    if (!image) return;
2373    eng_window_use(re->win);
2374    evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
2375    re->win->gl_context->dc = context;
2376
2377    evas_gl_common_filter_draw(re->win->gl_context, image, filter);
2378 }
2379
2380 static Filtered_Image *
2381 eng_image_filtered_get(void *im, uint8_t *key, size_t keylen)
2382 {
2383    return evas_gl_common_image_filtered_get(im, key, keylen);
2384 }
2385
2386 static Filtered_Image *
2387 eng_image_filtered_save(void *im, void *fim, uint8_t *key, size_t keylen)
2388 {
2389    return evas_gl_common_image_filtered_save(im, fim, key, keylen);
2390 }
2391
2392 static void
2393 eng_image_filtered_free(void *im, Filtered_Image *fim)
2394 {
2395    evas_gl_common_image_filtered_free(im, fim);
2396 }
2397 #endif
2398
2399
2400 //
2401 //
2402 /////////////////////////////////////////////////////////////////////////
2403
2404 static void *
2405 eng_image_load(void *data, const char *file, const char *key, int *error, Evas_Image_Load_Opts *lo)
2406 {
2407    Render_Engine *re;
2408
2409    re = (Render_Engine *)data;
2410    *error = EVAS_LOAD_ERROR_NONE;
2411    eng_window_use(re->win);
2412    return evas_gl_common_image_load(re->win->gl_context, file, key, lo, error);
2413 }
2414
2415 static void *
2416 eng_image_new_from_data(void *data, int w, int h, DATA32 *image_data, int alpha, int cspace)
2417 {
2418    Render_Engine *re;
2419
2420    re = (Render_Engine *)data;
2421    eng_window_use(re->win);
2422    return evas_gl_common_image_new_from_data(re->win->gl_context, w, h, image_data, alpha, cspace);
2423 }
2424
2425 static void *
2426 eng_image_new_from_copied_data(void *data, int w, int h, DATA32 *image_data, int alpha, int cspace)
2427 {
2428    Render_Engine *re;
2429
2430    re = (Render_Engine *)data;
2431    eng_window_use(re->win);
2432    return evas_gl_common_image_new_from_copied_data(re->win->gl_context, w, h, image_data, alpha, cspace);
2433 }
2434
2435 static void
2436 eng_image_free(void *data, void *image)
2437 {
2438    Render_Engine *re;
2439
2440    re = (Render_Engine *)data;
2441    if (!image) return;
2442    eng_window_use(re->win);
2443    evas_gl_common_image_free(image);
2444 }
2445
2446 static void
2447 eng_image_size_get(void *data __UNUSED__, void *image, int *w, int *h)
2448 {
2449    if (!image)
2450      {
2451         *w = 0;
2452         *h = 0;
2453         return;
2454      }
2455    if (w) *w = ((Evas_GL_Image *)image)->w;
2456    if (h) *h = ((Evas_GL_Image *)image)->h;
2457 }
2458
2459 static void *
2460 eng_image_size_set(void *data, void *image, int w, int h)
2461 {
2462    Render_Engine *re;
2463    Evas_GL_Image *im = image;
2464    Evas_GL_Image *im_old;
2465
2466    re = (Render_Engine *)data;
2467    if (!im) return NULL;
2468    if (im->native.data)
2469      {
2470         im->w = w;
2471         im->h = h;
2472         return image;
2473      }
2474    eng_window_use(re->win);
2475    if ((im->tex) && (im->tex->pt->dyn.img))
2476      {
2477         evas_gl_common_texture_free(im->tex);
2478         im->tex = NULL;
2479         im->w = w;
2480         im->h = h;
2481         im->tex = evas_gl_common_texture_dynamic_new(im->gc, im);
2482         return image;
2483      }
2484    im_old = image;
2485
2486    switch (eng_image_colorspace_get(data, image))
2487      {
2488       case EVAS_COLORSPACE_YCBCR422P601_PL:
2489       case EVAS_COLORSPACE_YCBCR422P709_PL:
2490       case EVAS_COLORSPACE_YCBCR422601_PL:
2491       case EVAS_COLORSPACE_YCBCR420NV12601_PL:
2492       case EVAS_COLORSPACE_YCBCR420TM12601_PL:
2493          w &= ~0x1;
2494          break;
2495      }
2496
2497    if ((im_old) &&
2498        ((int)im_old->im->cache_entry.w == w) &&
2499        ((int)im_old->im->cache_entry.h == h))
2500      return image;
2501    if (im_old)
2502      {
2503         im = evas_gl_common_image_new(re->win->gl_context, w, h,
2504                                       eng_image_alpha_get(data, image),
2505                                       eng_image_colorspace_get(data, image));
2506         /*
2507         evas_common_load_image_data_from_file(im_old->im);
2508         if (im_old->im->image->data)
2509           {
2510              evas_common_blit_rectangle(im_old->im, im->im, 0, 0, w, h, 0, 0);
2511              evas_common_cpu_end_opt();
2512           }
2513  */
2514         evas_gl_common_image_free(im_old);
2515      }
2516    else
2517      im = evas_gl_common_image_new(re->win->gl_context, w, h, 1, EVAS_COLORSPACE_ARGB8888);
2518    return im;
2519 }
2520
2521 static void *
2522 eng_image_dirty_region(void *data, void *image, int x, int y, int w, int h)
2523 {
2524    Render_Engine *re;
2525    Evas_GL_Image *im = image;
2526
2527    re = (Render_Engine *)data;
2528    if (!image) return NULL;
2529    if (im->native.data) return image;
2530    eng_window_use(re->win);
2531    evas_gl_common_image_dirty(image, x, y, w, h);
2532    return image;
2533 }
2534
2535 static void *
2536 eng_image_data_get(void *data, void *image, int to_write, DATA32 **image_data, int *err)
2537 {
2538    Render_Engine *re;
2539    Evas_GL_Image *im;
2540    int error;
2541
2542    re = (Render_Engine *)data;
2543    if (!image)
2544      {
2545         *image_data = NULL;
2546         if (err) *err = EVAS_LOAD_ERROR_GENERIC;
2547         return NULL;
2548      }
2549    im = image;
2550    if (im->native.data)
2551      {
2552         *image_data = NULL;
2553         if (err) *err = EVAS_LOAD_ERROR_NONE;
2554         return im;
2555      }
2556
2557 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
2558    eng_window_use(re->win);
2559
2560    if ((im->tex) && (im->tex->pt) && (im->tex->pt->dyn.img) && (im->cs.space == EVAS_COLORSPACE_ARGB8888))
2561      {
2562         if (im->tex->pt->dyn.checked_out > 0)
2563           {
2564              im->tex->pt->dyn.checked_out++;
2565              *image_data = im->tex->pt->dyn.data;
2566              if (err) *err = EVAS_LOAD_ERROR_NONE;
2567              return im;
2568           }
2569         *image_data = im->tex->pt->dyn.data = glsym_eglMapImageSEC(re->win->egl_disp, im->tex->pt->dyn.img);
2570
2571         if (!im->tex->pt->dyn.data)
2572           {
2573              if (err) *err = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
2574              GLERR(__FUNCTION__, __FILE__, __LINE__, "");
2575              return im;
2576           }
2577         im->tex->pt->dyn.checked_out++;
2578
2579         if (err) *err = EVAS_LOAD_ERROR_NONE;
2580         return im;
2581      }
2582 #else
2583    if ((im->tex) && (im->tex->pt) && (im->tex->pt->dyn.data))
2584      {
2585         *image_data = im->tex->pt->dyn.data;
2586         if (err) *err = EVAS_LOAD_ERROR_NONE;
2587         return im;
2588      }
2589
2590    eng_window_use(re->win);
2591 #endif
2592
2593    /* Engine can be fail to create texture after cache drop like eng_image_content_hint_set function,
2594         so it is need to add code which check im->im's NULL value*/
2595
2596    if (!im->im)
2597     {
2598        *image_data = NULL;
2599        if (err) *err = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
2600        return NULL;
2601     }
2602
2603    error = evas_cache_image_load_data(&im->im->cache_entry);
2604    switch (im->cs.space)
2605      {
2606       case EVAS_COLORSPACE_ARGB8888:
2607          if (to_write)
2608            {
2609               if (im->references > 1)
2610                 {
2611                    Evas_GL_Image *im_new;
2612
2613                    im_new = evas_gl_common_image_new_from_copied_data
2614                       (im->gc, im->im->cache_entry.w, im->im->cache_entry.h,
2615                        im->im->image.data,
2616                        eng_image_alpha_get(data, image),
2617                        eng_image_colorspace_get(data, image));
2618                    if (!im_new)
2619                      {
2620                         *image_data = NULL;
2621                         if (err) *err = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
2622                         return NULL;
2623                      }
2624                    evas_gl_common_image_free(im);
2625                    im = im_new;
2626                 }
2627               else
2628                 evas_gl_common_image_dirty(im, 0, 0, 0, 0);
2629            }
2630          *image_data = im->im->image.data;
2631          break;
2632       case EVAS_COLORSPACE_YCBCR422P601_PL:
2633       case EVAS_COLORSPACE_YCBCR422P709_PL:
2634       case EVAS_COLORSPACE_YCBCR422601_PL:
2635       case EVAS_COLORSPACE_YCBCR420NV12601_PL:
2636       case EVAS_COLORSPACE_YCBCR420TM12601_PL:
2637          *image_data = im->cs.data;
2638          break;
2639       default:
2640          abort();
2641          break;
2642      }
2643    if (err) *err = error;
2644    return im;
2645 }
2646
2647 static void *
2648 eng_image_data_put(void *data, void *image, DATA32 *image_data)
2649 {
2650    Render_Engine *re;
2651    Evas_GL_Image *im, *im2;
2652
2653    re = (Render_Engine *)data;
2654    if (!image) return NULL;
2655    im = image;
2656    if (im->native.data) return image;
2657    eng_window_use(re->win);
2658    if ((im->tex) && (im->tex->pt)
2659        && (im->tex->pt->dyn.data)
2660        && (im->cs.space == EVAS_COLORSPACE_ARGB8888))
2661      {
2662         if (im->tex->pt->dyn.data == image_data)
2663           {
2664              if (im->tex->pt->dyn.checked_out > 0)
2665                {
2666                  im->tex->pt->dyn.checked_out--;
2667 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
2668                  if (im->tex->pt->dyn.checked_out == 0)
2669                    glsym_eglUnmapImageSEC(re->win->egl_disp, im->tex->pt->dyn.img);
2670 #endif
2671                }
2672
2673              return image;
2674           }
2675         im2 = eng_image_new_from_data(data, im->w, im->h, image_data,
2676                                       eng_image_alpha_get(data, image),
2677                                       eng_image_colorspace_get(data, image));
2678         if (!im2) return im;
2679         evas_gl_common_image_free(im);
2680         im = im2;
2681         evas_gl_common_image_dirty(im, 0, 0, 0, 0);
2682         return im;
2683      }
2684    switch (im->cs.space)
2685      {
2686       case EVAS_COLORSPACE_ARGB8888:
2687          if ((!im->im) || (image_data != im->im->image.data))
2688            {
2689               im2 = eng_image_new_from_data(data, im->w, im->h, image_data,
2690                                             eng_image_alpha_get(data, image),
2691                                             eng_image_colorspace_get(data, image));
2692               if (!im2) return im;
2693               evas_gl_common_image_free(im);
2694               im = im2;
2695            }
2696          break;
2697       case EVAS_COLORSPACE_YCBCR422P601_PL:
2698       case EVAS_COLORSPACE_YCBCR422P709_PL:
2699       case EVAS_COLORSPACE_YCBCR422601_PL:
2700       case EVAS_COLORSPACE_YCBCR420NV12601_PL:
2701       case EVAS_COLORSPACE_YCBCR420TM12601_PL:
2702          if (image_data != im->cs.data)
2703            {
2704               if (im->cs.data)
2705                 {
2706                    if (!im->cs.no_free) free(im->cs.data);
2707                 }
2708               im->cs.data = image_data;
2709            }
2710          evas_gl_common_image_dirty(im, 0, 0, 0, 0);
2711          break;
2712       default:
2713          abort();
2714          break;
2715      }
2716    return im;
2717 }
2718
2719 static void
2720 eng_image_data_preload_request(void *data __UNUSED__, void *image, const void *target)
2721 {
2722    Evas_GL_Image *gim = image;
2723    RGBA_Image *im;
2724
2725    if (!gim) return;
2726    if (gim->native.data) return;
2727    im = (RGBA_Image *)gim->im;
2728    if (!im) return;
2729    evas_cache_image_preload_data(&im->cache_entry, target);
2730 }
2731
2732 static void
2733 eng_image_data_preload_cancel(void *data __UNUSED__, void *image, const void *target)
2734 {
2735    Evas_GL_Image *gim = image;
2736    RGBA_Image *im;
2737
2738    if (!gim) return;
2739    if (gim->native.data) return;
2740    im = (RGBA_Image *)gim->im;
2741    if (!im) return;
2742    evas_cache_image_preload_cancel(&im->cache_entry, target);
2743 }
2744
2745 static void
2746 eng_image_draw(void *data, 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)
2747 {
2748    Render_Engine *re;
2749    re = (Render_Engine *)data;
2750    if (!image) return;
2751
2752    if ((gl_direct_img_obj) && (gl_direct_enabled))
2753      {
2754         DBG("Rendering Directly to the window");
2755         evas_object_image_pixels_dirty_set(gl_direct_img_obj, EINA_TRUE);
2756      }
2757    else
2758      {
2759         eng_window_use(re->win);
2760         evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
2761         re->win->gl_context->dc = context;
2762         evas_gl_common_image_draw(re->win->gl_context, image,
2763                                   src_x, src_y, src_w, src_h,
2764                                   dst_x, dst_y, dst_w, dst_h,
2765                                   smooth);
2766      }
2767 }
2768
2769 static void
2770 eng_image_scale_hint_set(void *data __UNUSED__, void *image, int hint)
2771 {
2772    if (image) evas_gl_common_image_scale_hint_set(image, hint);
2773 }
2774
2775 static int
2776 eng_image_scale_hint_get(void *data __UNUSED__, void *image)
2777 {
2778    Evas_GL_Image *gim = image;
2779    if (!gim) return EVAS_IMAGE_SCALE_HINT_NONE;
2780    return gim->scale_hint;
2781 }
2782
2783 static void
2784 eng_image_map_draw(void *data, void *context, void *surface, void *image, RGBA_Map *m, int smooth, int level)
2785 {
2786    Evas_GL_Image *gim = image;
2787    Render_Engine *re;
2788
2789    re = (Render_Engine *)data;
2790    if (!image) return;
2791    eng_window_use(re->win);
2792    evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
2793    re->win->gl_context->dc = context;
2794    if (m->count != 4)
2795      {
2796         // FIXME: nash - you didn't fix this
2797         abort();
2798      }
2799    if ((m->pts[0].x == m->pts[3].x) &&
2800        (m->pts[1].x == m->pts[2].x) &&
2801        (m->pts[0].y == m->pts[1].y) &&
2802        (m->pts[3].y == m->pts[2].y) &&
2803        (m->pts[0].x <= m->pts[1].x) &&
2804        (m->pts[0].y <= m->pts[2].y) &&
2805        (m->pts[0].u == 0) &&
2806        (m->pts[0].v == 0) &&
2807        (m->pts[1].u == (gim->w << FP)) &&
2808        (m->pts[1].v == 0) &&
2809        (m->pts[2].u == (gim->w << FP)) &&
2810        (m->pts[2].v == (gim->h << FP)) &&
2811        (m->pts[3].u == 0) &&
2812        (m->pts[3].v == (gim->h << FP)) &&
2813        (m->pts[0].col == 0xffffffff) &&
2814        (m->pts[1].col == 0xffffffff) &&
2815        (m->pts[2].col == 0xffffffff) &&
2816        (m->pts[3].col == 0xffffffff))
2817      {
2818         int dx, dy, dw, dh;
2819
2820         dx = m->pts[0].x >> FP;
2821         dy = m->pts[0].y >> FP;
2822         dw = (m->pts[2].x >> FP) - dx;
2823         dh = (m->pts[2].y >> FP) - dy;
2824         eng_image_draw(data, context, surface, image,
2825                        0, 0, gim->w, gim->h, dx, dy, dw, dh, smooth);
2826      }
2827    else
2828      {
2829         evas_gl_common_image_map_draw(re->win->gl_context, image, m->count, &m->pts[0],
2830                                       smooth, level);
2831      }
2832 }
2833
2834 static void
2835 eng_image_map_clean(void *data, RGBA_Map *m)
2836 {
2837 }
2838
2839 static void *
2840 eng_image_map_surface_new(void *data, int w, int h, int alpha)
2841 {
2842    Render_Engine *re;
2843
2844    re = (Render_Engine *)data;
2845    return evas_gl_common_image_surface_new(re->win->gl_context, w, h, alpha);
2846 }
2847
2848 static void
2849 eng_image_map_surface_free(void *data __UNUSED__, void *surface)
2850 {
2851    evas_gl_common_image_free(surface);
2852 }
2853
2854 static void
2855 eng_image_content_hint_set(void *data __UNUSED__, void *image, int hint)
2856 {
2857    if (image) evas_gl_common_image_content_hint_set(image, hint);
2858 }
2859
2860 static int
2861 eng_image_content_hint_get(void *data __UNUSED__, void *image)
2862 {
2863    Evas_GL_Image *gim = image;
2864    if (!gim) return EVAS_IMAGE_CONTENT_HINT_NONE;
2865    return gim->content_hint;
2866 }
2867
2868 static void
2869 eng_image_cache_flush(void *data)
2870 {
2871    Render_Engine *re;
2872    int tmp_size;
2873
2874    re = (Render_Engine *)data;
2875
2876    tmp_size = evas_common_image_get_cache();
2877    evas_common_image_set_cache(0);
2878    evas_common_rgba_image_scalecache_flush();
2879    evas_gl_common_image_cache_flush(re->win->gl_context);
2880    evas_common_image_set_cache(tmp_size);
2881 }
2882
2883 static void
2884 eng_image_cache_set(void *data, int bytes)
2885 {
2886    Render_Engine *re;
2887
2888    re = (Render_Engine *)data;
2889    evas_common_image_set_cache(bytes);
2890    evas_common_rgba_image_scalecache_size_set(bytes);
2891    evas_gl_common_image_cache_flush(re->win->gl_context);
2892 }
2893
2894 static int
2895 eng_image_cache_get(void *data __UNUSED__)
2896 {
2897    return evas_common_image_get_cache();
2898 }
2899
2900 static void
2901 eng_image_stride_get(void *data __UNUSED__, void *image, int *stride)
2902 {
2903    Evas_GL_Image *im = image;
2904
2905    if ((im->tex) && (im->tex->pt->dyn.img))
2906      *stride = im->tex->pt->dyn.stride;
2907    else
2908      *stride = im->w * 4;
2909 }
2910
2911 static void
2912 eng_font_draw(void *data, 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 *intl_props)
2913 {
2914    Render_Engine *re;
2915
2916    re = (Render_Engine *)data;
2917    eng_window_use(re->win);
2918    evas_gl_common_context_target_surface_set(re->win->gl_context, surface);
2919    re->win->gl_context->dc = context;
2920      {
2921         // FIXME: put im into context so we can free it
2922         static RGBA_Image *im = NULL;
2923
2924         if (!im)
2925           im = (RGBA_Image *)evas_cache_image_empty(evas_common_image_cache_get());
2926         im->cache_entry.w = re->win->gl_context->shared->w;
2927         im->cache_entry.h = re->win->gl_context->shared->h;
2928
2929         evas_common_draw_context_font_ext_set(context,
2930                                               re->win->gl_context,
2931                                               evas_gl_font_texture_new,
2932                                               evas_gl_font_texture_free,
2933                                               evas_gl_font_texture_draw);
2934         evas_common_font_draw_prepare(intl_props);
2935         evas_common_font_draw(im, context, x, y, intl_props);
2936         evas_common_draw_context_font_ext_set(context,
2937                                               NULL,
2938                                               NULL,
2939                                               NULL,
2940                                               NULL);
2941      }
2942 }
2943
2944 static Eina_Bool
2945 eng_canvas_alpha_get(void *data, void *info __UNUSED__)
2946 {
2947    Render_Engine *re = (Render_Engine *)data;
2948    return re->win->alpha;
2949 }
2950
2951
2952 // Unfortunately, there is no query function to figure out which surface formats work.
2953 // So, this is one way to test for surface config capability.
2954 static int
2955 _check_gl_surface_format(GLint int_fmt, GLenum fmt, GLenum attachment, GLenum attach_fmt, int mult_samples)
2956 {
2957    GLuint fbo, tex, rb, ds_tex;
2958    int w, h, fb_status;
2959
2960    // Initialize Variables
2961    fbo = tex = rb = ds_tex = 0;
2962
2963    // Width/Heith for test purposes
2964    w = h = 2;
2965
2966    // Gen FBO
2967    glGenFramebuffers(1, &fbo);
2968    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2969
2970    // Render Target Texture
2971    if (int_fmt)
2972      {
2973         glGenTextures(1, &tex);
2974         glBindTexture(GL_TEXTURE_2D, tex );
2975         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2976         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2977         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2978         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2979         glTexImage2D(GL_TEXTURE_2D, 0, int_fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, NULL);
2980         glBindTexture(GL_TEXTURE_2D, 0);
2981
2982         if (mult_samples)
2983            glsym_glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0, mult_samples);
2984         else
2985            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
2986      }
2987
2988    // Render Target Attachment (Stencil or Depth)
2989    if (attachment)
2990      {
2991 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
2992         // This is a little hacky but this is how we'll have to do for now.
2993         if (attach_fmt == GL_DEPTH_STENCIL_OES)
2994           {
2995              glGenTextures(1, &ds_tex);
2996              glBindTexture(GL_TEXTURE_2D, ds_tex);
2997              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2998              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2999              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3000              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3001              glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL_OES, w, h,
3002                           0, GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, NULL);
3003              if (mult_samples)
3004                {
3005                   glsym_glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
3006                                                           GL_TEXTURE_2D, ds_tex, 0, mult_samples);
3007                   glsym_glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
3008                                                           GL_TEXTURE_2D, ds_tex, 0, mult_samples);
3009                }
3010              else
3011                {
3012                   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
3013                                          GL_TEXTURE_2D, ds_tex, 0);
3014                   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
3015                                          GL_TEXTURE_2D, ds_tex, 0);
3016                }
3017              glBindTexture(GL_TEXTURE_2D, 0);
3018           }
3019         else
3020 #endif
3021           {
3022              glGenRenderbuffers(1, &rb);
3023              glBindRenderbuffer(GL_RENDERBUFFER, rb);
3024              if (mult_samples)
3025                 glsym_glRenderbufferStorageMultisample(GL_RENDERBUFFER, mult_samples, attach_fmt, w, h);
3026              else
3027                 glRenderbufferStorage(GL_RENDERBUFFER, attach_fmt, w, h);
3028              glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, rb);
3029              glBindRenderbuffer(GL_RENDERBUFFER, 0);
3030           }
3031
3032      }
3033
3034    // Check FBO for completeness
3035    fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
3036
3037
3038    // Delete Created Resources
3039    glBindFramebuffer(GL_FRAMEBUFFER, 0);
3040    if (fbo) glDeleteFramebuffers(1, &fbo);
3041    if (tex) glDeleteTextures(1, &tex);
3042    if (ds_tex) glDeleteTextures(1, &ds_tex);
3043    if (rb) glDeleteRenderbuffers(1, &rb);
3044
3045    if (fb_status != GL_FRAMEBUFFER_COMPLETE)
3046       return 0;
3047    else
3048      {
3049         if ((attachment) && (!mult_samples))
3050            return attach_fmt;
3051         else
3052            return 1;
3053      }
3054 }
3055
3056 static void
3057 _print_gl_surface_info(Render_Engine_GL_Surface *sfc, int error)
3058 {
3059 #define PRINT_LOG(...) \
3060    if (error) \
3061       ERR(__VA_ARGS__); \
3062    else \
3063       DBG(__VA_ARGS__);
3064
3065    PRINT_LOG("----------Surface Info------------");
3066    PRINT_LOG("     [Surface] %x", (unsigned int)sfc);
3067    PRINT_LOG("         Width:  %d", sfc->w);
3068    PRINT_LOG("         Height: %d", sfc->h);
3069    PRINT_LOG("         Direct Surface: %x", (unsigned int)sfc->direct_sfc);
3070    PRINT_LOG("         Current Context: %x", (unsigned int)sfc->current_ctx);
3071    PRINT_LOG("         [-------Config-------]");
3072    PRINT_LOG("            Depth Bits      : %d", sfc->depth_bits);
3073    PRINT_LOG("            Stencil Bits    : %d", sfc->stencil_bits);
3074    PRINT_LOG("            Direct FB Opt   : %d", sfc->direct_fb_opt);
3075    PRINT_LOG("            Multisample Bits: %d", sfc->multisample_bits);
3076    PRINT_LOG("            MSAA Samples    : %d", sfc->rt_msaa_samples);
3077    PRINT_LOG("         [-------Internal-----]");
3078    PRINT_LOG("            RenderTarget Texture             : %d", sfc->rt_tex);
3079    PRINT_LOG("            RenderTarget Internal Format     : %x", sfc->rt_internal_fmt);
3080    PRINT_LOG("            RenderTaret Format               : %x", sfc->rt_fmt);
3081    PRINT_LOG("            RenderBuffer Depth               : %x", sfc->rb_depth);
3082    PRINT_LOG("            RenderBuffer Depth Format        : %x", sfc->rb_depth_fmt);
3083    PRINT_LOG("            RenderBuffer Stencil             : %d", sfc->rb_stencil);
3084    PRINT_LOG("            RenderBuffer Stencil Format      : %x", sfc->rb_stencil_fmt);
3085    PRINT_LOG("            RenderBuffer Depth Stencil       : %x", sfc->rb_depth_stencil);
3086    PRINT_LOG("            RenderBuffer Depth Stencil Format: %x", sfc->rb_depth_stencil_fmt);
3087    PRINT_LOG("--------------------------------------");
3088
3089 #undef PRINT_LOG
3090 }
3091
3092 static void
3093 _print_gl_surface_cap(Render_Engine *re, int error)
3094 {
3095 #define PRINT_LOG(...) \
3096    if (error) \
3097       ERR(__VA_ARGS__); \
3098    else \
3099       DBG(__VA_ARGS__);
3100
3101    PRINT_LOG("----------------------------------------------------");
3102    PRINT_LOG("           EvasGL Supported Surface Format          ");
3103    PRINT_LOG("                                                    ");
3104    PRINT_LOG(" [Max Renderbuffer Size]  : %d", re->gl_cap.max_rb_size);
3105    PRINT_LOG(" [Multisample Support  ]  : %d", re->gl_cap.msaa_support);
3106    PRINT_LOG("          [Low  Samples]  : %d", re->gl_cap.msaa_samples[1]);
3107    PRINT_LOG("          [Med  Samples]  : %d", re->gl_cap.msaa_samples[2]);
3108    PRINT_LOG("          [High Samples]  : %d", re->gl_cap.msaa_samples[3]);
3109    PRINT_LOG("                                  [--Multisamples--]   ");
3110    PRINT_LOG("                           [Norm] [Low] [Med] [High]");
3111    PRINT_LOG(" [RGB  Format]            : %4x    %d     %d     %d", re->gl_cap.rgb_888[0], re->gl_cap.rgb_888[1], re->gl_cap.rgb_888[2], re->gl_cap.rgb_888[3]);
3112    PRINT_LOG(" [RGBA Format]            : %4x    %d     %d     %d", re->gl_cap.rgba_8888[0], re->gl_cap.rgba_8888[1], re->gl_cap.rgba_8888[2], re->gl_cap.rgba_8888[3]);
3113    PRINT_LOG(" [Depth  8 Bits]          : %4x    %d     %d     %d", re->gl_cap.depth_8[0], re->gl_cap.depth_8[1], re->gl_cap.depth_8[2], re->gl_cap.depth_8[3]);
3114    PRINT_LOG(" [Depth 16 Bits]          : %4x    %d     %d     %d", re->gl_cap.depth_16[0], re->gl_cap.depth_16[1], re->gl_cap.depth_16[2], re->gl_cap.depth_16[3]);
3115    PRINT_LOG(" [Depth 24 Bits]          : %4x    %d     %d     %d", re->gl_cap.depth_24[0], re->gl_cap.depth_24[1], re->gl_cap.depth_24[2], re->gl_cap.depth_24[3]);
3116    PRINT_LOG(" [Depth 32 Bits]          : %4x    %d     %d     %d", re->gl_cap.depth_32[0], re->gl_cap.depth_32[1], re->gl_cap.depth_32[2], re->gl_cap.depth_32[3]);
3117    PRINT_LOG(" [Stencil  1 Bits]        : %4x    %d     %d     %d", re->gl_cap.stencil_1[0], re->gl_cap.stencil_1[1], re->gl_cap.stencil_1[2], re->gl_cap.stencil_1[3]);
3118    PRINT_LOG(" [Stencil  2 Bits]        : %4x    %d     %d     %d", re->gl_cap.stencil_2[0], re->gl_cap.stencil_2[1], re->gl_cap.stencil_2[2], re->gl_cap.stencil_2[3]);
3119    PRINT_LOG(" [Stencil  4 Bits]        : %4x    %d     %d     %d", re->gl_cap.stencil_4[0], re->gl_cap.stencil_4[1], re->gl_cap.stencil_4[2], re->gl_cap.stencil_4[3]);
3120    PRINT_LOG(" [Stencil  8 Bits]        : %4x    %d     %d     %d", re->gl_cap.stencil_8[0], re->gl_cap.stencil_8[1], re->gl_cap.stencil_8[2], re->gl_cap.stencil_8[3]);
3121    PRINT_LOG(" [Stencil 16 Bits]        : %4x    %d     %d     %d", re->gl_cap.stencil_16[0], re->gl_cap.stencil_16[1], re->gl_cap.stencil_16[2], re->gl_cap.stencil_16[3]);
3122    PRINT_LOG(" [Depth 24 Stencil 8 Bits]: %4x    %d     %d     %d", re->gl_cap.depth_24_stencil_8[0], re->gl_cap.depth_24_stencil_8[1], re->gl_cap.depth_24_stencil_8[2], re->gl_cap.depth_24_stencil_8[3]);
3123    PRINT_LOG("----------------------------------------------------");
3124 #undef PRINT_LOG
3125 }
3126
3127 static void
3128 _set_gl_surface_cap(Render_Engine *re)
3129 {
3130    GLuint fbo, tex, depth, stencil;
3131    int w, h;
3132
3133    int i, count;
3134
3135    if (!re) return;
3136    if (re->gl_cap_initted) return;
3137
3138    // Width/Heith for test purposes
3139    w = h = 2;
3140
3141 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3142    int max_samples = 0;
3143
3144    glGetIntegerv(GL_MAX_SAMPLES_IMG, &max_samples);
3145
3146    // Check if msaa_support is supported
3147    if ((max_samples) &&
3148        (glsym_glFramebufferTexture2DMultisample) &&
3149        (glsym_glRenderbufferStorageMultisample))
3150      {
3151         re->gl_cap.msaa_support = 1;
3152
3153         re->gl_cap.msaa_samples[3] = max_samples;
3154         re->gl_cap.msaa_samples[2] = max_samples/2;
3155         re->gl_cap.msaa_samples[1] = max_samples/4;
3156         re->gl_cap.msaa_samples[0] = 0;
3157
3158         if (!re->gl_cap.msaa_samples[2]) re->gl_cap.msaa_samples[3];
3159         if (!re->gl_cap.msaa_samples[1]) re->gl_cap.msaa_samples[2];
3160      }
3161    else
3162      {
3163         re->gl_cap.msaa_support = 0;
3164      }
3165
3166 #endif
3167
3168    glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &re->gl_cap.max_rb_size);
3169
3170 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3171    count = (re->gl_cap.msaa_support) ? 4 : 1;
3172
3173    for (i = 0; i < count; i++)
3174      {
3175         re->gl_cap.rgb_888[i]   = _check_gl_surface_format(GL_RGB, GL_RGB, 0, 0, re->gl_cap.msaa_samples[i]);
3176         re->gl_cap.rgba_8888[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, 0, 0, re->gl_cap.msaa_samples[i]);
3177
3178         re->gl_cap.depth_8[i]   = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, re->gl_cap.msaa_samples[i]);
3179         re->gl_cap.depth_16[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT16, re->gl_cap.msaa_samples[i]);
3180         re->gl_cap.depth_24[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT24_OES, re->gl_cap.msaa_samples[i]);
3181         re->gl_cap.depth_32[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT32_OES, re->gl_cap.msaa_samples[i]);
3182
3183         re->gl_cap.stencil_1[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX1_OES, re->gl_cap.msaa_samples[i]);
3184         re->gl_cap.stencil_4[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX4_OES, re->gl_cap.msaa_samples[i]);
3185         re->gl_cap.stencil_8[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX8, re->gl_cap.msaa_samples[i]);
3186
3187         re->gl_cap.depth_24_stencil_8[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_STENCIL_OES, GL_DEPTH_STENCIL_OES, re->gl_cap.msaa_samples[i]);
3188      }
3189
3190 #else
3191    count = (re->gl_cap.msaa_support) ? 4 : 1;
3192
3193    for (i = 0; i < count; i++)
3194      {
3195         re->gl_cap.rgb_888[i]   = _check_gl_surface_format(GL_RGB, GL_RGB, 0, 0, re->gl_cap.msaa_samples[i]);
3196         re->gl_cap.rgba_8888[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, 0, 0, re->gl_cap.msaa_samples[i]);
3197
3198         re->gl_cap.depth_8[i]   = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, re->gl_cap.msaa_samples[i]);
3199         re->gl_cap.depth_16[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT16, re->gl_cap.msaa_samples[i]);
3200         re->gl_cap.depth_24[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT24, re->gl_cap.msaa_samples[i]);
3201         re->gl_cap.depth_32[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT32, re->gl_cap.msaa_samples[i]);
3202
3203         re->gl_cap.stencil_1[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX1, re->gl_cap.msaa_samples[i]);
3204         re->gl_cap.stencil_4[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX4, re->gl_cap.msaa_samples[i]);
3205         re->gl_cap.stencil_8[i] = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX8, re->gl_cap.msaa_samples[i]);
3206
3207         re->gl_cap.depth_24_stencil_8[i]  = _check_gl_surface_format(GL_RGBA, GL_RGBA, GL_DEPTH_STENCIL_ATTACHMENT, GL_DEPTH24_STENCIL8, re->gl_cap.msaa_samples[i]);
3208      }
3209 #endif
3210
3211    _print_gl_surface_cap(re, 0);
3212
3213    re->gl_cap_initted = 1;
3214 }
3215
3216 static int
3217 _set_internal_config(Render_Engine *re, Render_Engine_GL_Surface *sfc, Evas_GL_Config *cfg)
3218 {
3219    // Check if color formats are supported
3220    switch((int)cfg->color_format)
3221      {
3222       case EVAS_GL_RGB_888:
3223          if (re->gl_cap.rgb_888[0])
3224            {
3225               sfc->rt_fmt          = GL_RGB;
3226               sfc->rt_internal_fmt = GL_RGB;
3227               break;
3228            }
3229       case EVAS_GL_RGBA_8888:
3230          if (re->gl_cap.rgba_8888[0])
3231            {
3232               sfc->rt_fmt          = GL_RGBA;
3233               sfc->rt_internal_fmt = GL_RGBA;
3234               cfg->color_format    = EVAS_GL_RGBA_8888;
3235               break;
3236            }
3237       default:
3238          ERR("Color Format Not Supported: %d", cfg->color_format);
3239          _print_gl_surface_cap(re, 1);
3240          return 0;
3241      }
3242
3243    switch((int)cfg->depth_bits)
3244      {
3245       case EVAS_GL_DEPTH_NONE:
3246          break;
3247       case EVAS_GL_DEPTH_BIT_8:
3248          if (re->gl_cap.depth_8[0])
3249            {
3250               sfc->rb_depth_fmt = re->gl_cap.depth_8[0];
3251               cfg->depth_bits   = EVAS_GL_DEPTH_BIT_8;
3252               break;
3253            }
3254       case EVAS_GL_DEPTH_BIT_16:
3255          if (re->gl_cap.depth_16[0])
3256            {
3257               sfc->rb_depth_fmt = re->gl_cap.depth_16[0];
3258               cfg->depth_bits   = EVAS_GL_DEPTH_BIT_16;
3259               break;
3260            }
3261       case EVAS_GL_DEPTH_BIT_24:
3262          if (re->gl_cap.depth_24[0])
3263            {
3264               sfc->rb_depth_fmt = re->gl_cap.depth_24[0];
3265               cfg->depth_bits   = EVAS_GL_DEPTH_BIT_24;
3266               break;
3267            }
3268          else if (re->gl_cap.depth_24_stencil_8[0])
3269            {
3270               sfc->rb_depth_stencil_fmt = re->gl_cap.depth_24_stencil_8[0];
3271               sfc->rb_depth_fmt         = re->gl_cap.depth_24_stencil_8[0];
3272               cfg->depth_bits           = EVAS_GL_DEPTH_BIT_24;
3273               break;
3274            }
3275       case EVAS_GL_DEPTH_BIT_32:
3276          if (re->gl_cap.depth_32[0])
3277            {
3278               sfc->rb_depth_fmt = re->gl_cap.depth_32[0];
3279               cfg->depth_bits   = EVAS_GL_DEPTH_BIT_32;
3280               break;
3281            }
3282       default:
3283          ERR("Unsupported Depth Bits Format: %d", cfg->depth_bits);
3284          _print_gl_surface_cap(re, 1);
3285          return 0;
3286      }
3287
3288    switch((int)cfg->stencil_bits)
3289      {
3290       case EVAS_GL_STENCIL_NONE:
3291          break;
3292       case EVAS_GL_STENCIL_BIT_1:
3293          if (re->gl_cap.stencil_1[0])
3294            {
3295               sfc->rb_stencil_fmt = re->gl_cap.stencil_1[0];
3296               cfg->stencil_bits   = EVAS_GL_STENCIL_BIT_1;
3297               break;
3298            }
3299       case EVAS_GL_STENCIL_BIT_2:
3300          if (re->gl_cap.stencil_2[0])
3301            {
3302               sfc->rb_stencil_fmt = re->gl_cap.stencil_2[0];
3303               cfg->stencil_bits   = EVAS_GL_STENCIL_BIT_2;
3304               break;
3305            }
3306       case EVAS_GL_STENCIL_BIT_4:
3307          if (re->gl_cap.stencil_4[0])
3308            {
3309               sfc->rb_stencil_fmt = re->gl_cap.stencil_4[0];
3310               cfg->stencil_bits   = EVAS_GL_STENCIL_BIT_4;
3311               break;
3312            }
3313       case EVAS_GL_STENCIL_BIT_8:
3314          if ((sfc->rb_depth_fmt == re->gl_cap.depth_24_stencil_8[0]) ||
3315              (sfc->rb_depth_fmt == re->gl_cap.depth_24[0]) ||
3316              (!(re->gl_cap.stencil_8[0]) && (re->gl_cap.depth_24_stencil_8[0])))
3317            {
3318               sfc->rb_depth_stencil_fmt = re->gl_cap.depth_24_stencil_8[0];
3319               sfc->rb_stencil_fmt       = re->gl_cap.depth_24_stencil_8[0];
3320               cfg->stencil_bits         = EVAS_GL_STENCIL_BIT_8;
3321               break;
3322            }
3323          else if (re->gl_cap.stencil_8[0])
3324            {
3325               sfc->rb_stencil_fmt = re->gl_cap.stencil_8[0];
3326               cfg->stencil_bits   = EVAS_GL_STENCIL_BIT_8;
3327               break;
3328            }
3329       case EVAS_GL_STENCIL_BIT_16:
3330          if (re->gl_cap.stencil_16[0])
3331            {
3332               sfc->rb_stencil_fmt = re->gl_cap.stencil_16[0];
3333               cfg->stencil_bits   = EVAS_GL_STENCIL_BIT_16;
3334               break;
3335            }
3336       default:
3337          ERR("Unsupported Stencil Bits Format: %d", cfg->stencil_bits);
3338          _print_gl_surface_cap(re, 1);
3339          return 0;
3340      }
3341
3342    if (cfg->options_bits)
3343      {
3344         if (cfg->options_bits & EVAS_GL_OPTIONS_DIRECT)
3345           {
3346              sfc->direct_fb_opt       = 1;
3347              DBG("########################################################");
3348              DBG("######### [Evas] Direct option bit is enabled ##########");
3349              DBG("########################################################");
3350           }
3351         // Add other options here...
3352      }
3353
3354    // Multisample bit
3355    if (re->gl_cap.msaa_support)
3356      {
3357         if ( ((int)(cfg->multisample_bits) > (int)EVAS_GL_MULTISAMPLE_HIGH) ||
3358              ((int)(cfg->multisample_bits) < 0) )
3359           {
3360              ERR("Unsupported Multisample Bits Format!");
3361              _print_gl_surface_cap(re, 1);
3362              return 0;
3363           }
3364         else
3365           {
3366              sfc->rt_msaa_samples = re->gl_cap.msaa_samples[(int)cfg->multisample_bits];
3367           }
3368      }
3369
3370    return 1;
3371 }
3372
3373 static int
3374 _attach_fbo_surface(Render_Engine *data __UNUSED__,
3375                     Render_Engine_GL_Surface *sfc,
3376                     int fbo)
3377 {
3378    int fb_status, curr_tex, curr_rb;
3379
3380    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3381
3382    // Detach any previously attached buffers
3383    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
3384    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
3385    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
3386 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3387    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
3388    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
3389 #else
3390    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
3391 #endif
3392
3393
3394    // Render Target Texture
3395    if (sfc->rt_tex)
3396      {
3397         curr_tex = 0;
3398         glGetIntegerv(GL_TEXTURE_BINDING_2D, &curr_tex);
3399         glBindTexture(GL_TEXTURE_2D, sfc->rt_tex );
3400         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3401         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3402         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3403         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3404         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sfc->w, sfc->h, 0,
3405                      GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3406         glBindTexture(GL_TEXTURE_2D, curr_tex);
3407
3408         // Attach texture to FBO
3409         if (sfc->rt_msaa_samples)
3410            glsym_glFramebufferTexture2DMultisample(GL_FRAMEBUFFER,
3411                                                    GL_COLOR_ATTACHMENT0,
3412                                                    GL_TEXTURE_2D, sfc->rt_tex,
3413                                                    0, sfc->rt_msaa_samples);
3414         else
3415            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
3416                                   GL_TEXTURE_2D, sfc->rt_tex, 0);
3417      }
3418
3419
3420    // Depth Stencil RenderBuffer - Attach it to FBO
3421    if (sfc->rb_depth_stencil)
3422      {
3423 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3424         curr_tex = 0;
3425         glGetIntegerv(GL_TEXTURE_BINDING_2D, &curr_tex);
3426         glBindTexture(GL_TEXTURE_2D, sfc->rb_depth_stencil);
3427         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3428         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3429         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3430         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3431         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL_OES, sfc->w, sfc->h,
3432                      0, GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, NULL);
3433        if (sfc->rt_msaa_samples)
3434           {
3435              glsym_glFramebufferTexture2DMultisample(GL_FRAMEBUFFER,
3436                                                      GL_DEPTH_ATTACHMENT,
3437                                                      GL_TEXTURE_2D,
3438                                                      sfc->rb_depth_stencil,
3439                                                      0, sfc->rt_msaa_samples);
3440              glsym_glFramebufferTexture2DMultisample(GL_FRAMEBUFFER,
3441                                                      GL_STENCIL_ATTACHMENT,
3442                                                      GL_TEXTURE_2D,
3443                                                      sfc->rb_depth_stencil,
3444                                                      0, sfc->rt_msaa_samples);
3445           }
3446         else
3447           {
3448              glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
3449                                     GL_TEXTURE_2D, sfc->rb_depth_stencil, 0);
3450              glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
3451                                     GL_TEXTURE_2D, sfc->rb_depth_stencil, 0);
3452           }
3453         glBindTexture(GL_TEXTURE_2D, curr_tex);
3454
3455 #else
3456         curr_rb = 0;
3457         glGetIntegerv(GL_RENDERBUFFER_BINDING, &curr_rb);
3458         glBindRenderbuffer(GL_RENDERBUFFER, sfc->rb_depth_stencil);
3459         glRenderbufferStorage(GL_RENDERBUFFER, sfc->rb_depth_stencil_fmt,
3460                               sfc->w, sfc->h);
3461         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
3462                                   GL_RENDERBUFFER, sfc->rb_depth_stencil);
3463         glBindRenderbuffer(GL_RENDERBUFFER, curr_rb);
3464 #endif
3465      }
3466
3467    // Depth RenderBuffer - Attach it to FBO
3468    if (sfc->rb_depth)
3469      {
3470         curr_rb = 0;
3471         glGetIntegerv(GL_RENDERBUFFER_BINDING, &curr_rb);
3472
3473         glBindRenderbuffer(GL_RENDERBUFFER, sfc->rb_depth);
3474
3475         if (sfc->rt_msaa_samples)
3476            glsym_glRenderbufferStorageMultisample(GL_RENDERBUFFER,
3477                                                   sfc->rt_msaa_samples,
3478                                                   sfc->rb_depth_fmt,
3479                                                   sfc->w, sfc->h);
3480         else
3481            glRenderbufferStorage(GL_RENDERBUFFER, sfc->rb_depth_fmt,
3482                                  sfc->w, sfc->h);
3483         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
3484                                   GL_RENDERBUFFER, sfc->rb_depth);
3485         glBindRenderbuffer(GL_RENDERBUFFER, curr_rb);
3486      }
3487
3488    // Stencil RenderBuffer - Attach it to FBO
3489    if (sfc->rb_stencil)
3490      {
3491         curr_rb = 0;
3492         glGetIntegerv(GL_RENDERBUFFER_BINDING, &curr_rb);
3493
3494         glBindRenderbuffer(GL_RENDERBUFFER, sfc->rb_stencil);
3495
3496         if (sfc->rt_msaa_samples)
3497            glsym_glRenderbufferStorageMultisample(GL_RENDERBUFFER,
3498                                                   sfc->rt_msaa_samples,
3499                                                   sfc->rb_stencil_fmt,
3500                                                   sfc->w, sfc->h);
3501         else
3502            glRenderbufferStorage(GL_RENDERBUFFER, sfc->rb_stencil_fmt,
3503                                  sfc->w, sfc->h);
3504         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
3505                                   GL_RENDERBUFFER, sfc->rb_stencil);
3506         glBindRenderbuffer(GL_RENDERBUFFER, curr_rb);
3507      }
3508
3509    // Check FBO for completeness
3510    fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
3511    if (fb_status != GL_FRAMEBUFFER_COMPLETE)
3512      {
3513         ERR("FBO not complete. Error Code: %x!", fb_status);
3514         _print_gl_surface_info(sfc, 1);
3515         return 0;
3516      }
3517
3518    return 1;
3519 }
3520
3521 static int
3522 _create_rt_buffers(Render_Engine *data __UNUSED__,
3523                    Render_Engine_GL_Surface *sfc)
3524 {
3525    int ret = 0;
3526    GLuint fbo = 0, curr_fbo = 0;
3527
3528    //------------------------------------//
3529    // Render Target texture
3530    if (sfc->rt_fmt)
3531      {
3532         glGenTextures(1, &sfc->rt_tex);
3533      }
3534
3535    // First check if packed buffer is to be used.
3536    if (sfc->rb_depth_stencil_fmt)
3537      {
3538 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3539         glGenTextures(1, &sfc->rb_depth_stencil);
3540 #else
3541         glGenRenderbuffers(1, &sfc->rb_depth_stencil);
3542 #endif
3543      }
3544    else
3545      {
3546         // Depth RenderBuffer - Create storage here...
3547         if (sfc->rb_depth_fmt)
3548            glGenRenderbuffers(1, &sfc->rb_depth);
3549
3550         // Stencil RenderBuffer - Create Storage here...
3551         if (sfc->rb_stencil_fmt)
3552            glGenRenderbuffers(1, &sfc->rb_stencil);
3553      }
3554    //------------------------------------//
3555    // Try attaching the given configuration
3556    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &curr_fbo);
3557    glGenFramebuffers(1 ,&fbo);
3558
3559    ret = _attach_fbo_surface(NULL, sfc, fbo);
3560
3561    if (fbo) glDeleteFramebuffers(1, &fbo);
3562    glBindFramebuffer(GL_FRAMEBUFFER, curr_fbo);
3563
3564    if (!ret)
3565      {
3566         if (sfc->rt_tex) glDeleteTextures(1, &sfc->rt_tex);
3567         if (sfc->rb_depth) glDeleteRenderbuffers(1, &sfc->rb_depth);
3568         if (sfc->rb_stencil) glDeleteRenderbuffers(1, &sfc->rb_stencil);
3569 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3570         if (sfc->rb_depth_stencil) glDeleteTextures(1, &sfc->rb_depth_stencil);
3571 #else
3572         if (sfc->rb_depth_stencil) glDeleteRenderbuffers(1, &sfc->rb_depth_stencil);
3573 #endif
3574         ERR("_attach_fbo_surface() failed.");
3575         return 0;
3576      }
3577    else
3578       return 1;
3579 }
3580
3581
3582 static void *
3583 eng_gl_surface_create(void *data, void *config, int w, int h)
3584 {
3585    Render_Engine *re;
3586    Render_Engine_GL_Surface *sfc;
3587    Render_Engine_GL_Resource *rsc;
3588    Evas_GL_Config *cfg;
3589    void *ret = NULL;
3590    int res;
3591
3592    re  = (Render_Engine *)data;
3593    cfg = (Evas_GL_Config *)config;
3594
3595    // Allocate surface and fill in values
3596    sfc = calloc(1, sizeof(Render_Engine_GL_Surface));
3597    if (!sfc)
3598      {
3599         ERR("Surface allocation failed.");
3600         goto finish;
3601      }
3602
3603    sfc->w            = w;
3604    sfc->h            = h;
3605    sfc->depth_bits   = cfg->depth_bits;
3606    sfc->stencil_bits = cfg->stencil_bits;
3607
3608    // Allow alpha for evas gl direct rendering override
3609    // FIXME!!!: A little out of place but for now...
3610    if (!gl_direct_override)
3611       if (getenv("EVAS_GL_DIRECT_OVERRIDE")) gl_direct_override = 1;
3612
3613    // Set the internal format based on the config
3614    if (cfg->options_bits & EVAS_GL_OPTIONS_DIRECT)
3615      {
3616         DBG("Enabling Direct rendering to the Evas' window.");
3617 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3618         sfc->direct_sfc = re->win->egl_surface[0];
3619 #else
3620         sfc->direct_sfc = re->win->win;
3621 #endif
3622      }
3623
3624    // Use resource surface/context to do a make current
3625    if (!_internal_resources_make_current(re))
3626      {
3627         ERR("Error doing a make current with the internal resources.");
3628         goto finish;
3629      }
3630
3631    // Set the engine surface capability first if it hasn't been set
3632    if (!re->gl_cap_initted) _set_gl_surface_cap(re);
3633
3634    // Check the size of the surface
3635    if ( (w > re->gl_cap.max_rb_size) || (h > re->gl_cap.max_rb_size) )
3636      {
3637         ERR("Surface size greater than the supported size. Max Surface Size: %d", re->gl_cap.max_rb_size);
3638         goto finish;
3639      }
3640
3641    // Set the internal config value
3642    if (!_set_internal_config(re, sfc, cfg))
3643      {
3644         ERR("Unsupported Format!");
3645         goto finish;
3646      }
3647
3648    // Create Render texture
3649    if (!_create_rt_buffers(re, sfc))
3650      {
3651         ERR("Unable Create Specificed Surfaces.  Unsupported format!");
3652         goto finish;
3653      };
3654
3655    ret = sfc;
3656
3657 finish:
3658
3659 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3660    res = eglMakeCurrent(re->win->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
3661 #else
3662    res = glXMakeCurrent(re->info->info.display, None, NULL);
3663 #endif
3664    if (!res)
3665      {
3666         ERR("xxxMakeCurrent() (NULL, NULL) Error!");
3667      }
3668
3669    if (!ret)
3670      {
3671         if (sfc) free(sfc);
3672      }
3673    return ret;
3674 }
3675
3676 static int
3677 eng_gl_surface_destroy(void *data, void *surface)
3678 {
3679    Render_Engine *re;
3680    Render_Engine_GL_Surface *sfc;
3681    Render_Engine_GL_Resource *rsc;
3682    int ret;
3683
3684    re  = (Render_Engine *)data;
3685    sfc = (Render_Engine_GL_Surface*)surface;
3686
3687    if (!sfc) return 0;
3688
3689    // Use resource surface/context to create surface resrouces
3690    if (!_internal_resources_make_current(re))
3691      {
3692         ERR("Error doing a make current with the internal resources.");
3693         return 0;
3694      }
3695
3696    // Reset the Framebuffer binding point
3697    if ((current_evgl_ctx) && (current_evgl_ctx->current_fbo == current_evgl_ctx->context_fbo))
3698      {
3699         //glBindFramebuffer(GL_FRAMEBUFFER, 0);
3700         current_evgl_ctx->current_fbo = 0;
3701         current_evgl_ctx->current_sfc = NULL;
3702      }
3703
3704    // Clear direct rendering flag
3705    gl_direct_enabled = 0;
3706
3707    // Delete FBO/RBO and Texture here
3708    if (sfc->rt_tex)
3709       glDeleteTextures(1, &sfc->rt_tex);
3710
3711    if (sfc->rb_depth)
3712       glDeleteRenderbuffers(1, &sfc->rb_depth);
3713
3714    if (sfc->rb_stencil)
3715       glDeleteRenderbuffers(1, &sfc->rb_stencil);
3716
3717    if (sfc->rb_depth_stencil)
3718      {
3719 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3720         glDeleteTextures(1, &sfc->rb_depth_stencil);
3721 #else
3722         glDeleteRenderbuffers(1, &sfc->rb_depth_stencil);
3723 #endif
3724      }
3725
3726
3727 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3728    ret = eglMakeCurrent(re->win->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
3729 #else
3730    ret = glXMakeCurrent(re->info->info.display, None, NULL);
3731 #endif
3732    if (!ret)
3733      {
3734         ERR("xxxMakeCurrent() failed!");
3735         free(sfc);
3736         return 0;
3737      }
3738
3739    free(sfc);
3740    surface = NULL;
3741
3742    return 1;
3743 }
3744
3745 static void *
3746 eng_gl_context_create(void *data, void *share_context)
3747 {
3748    Render_Engine *re;
3749    Render_Engine_GL_Context *ctx;
3750    Render_Engine_GL_Context *share_ctx;
3751 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3752    int context_attrs[3];
3753 #endif
3754
3755    ctx = calloc(1, sizeof(Render_Engine_GL_Context));
3756
3757    if (!ctx) return NULL;
3758
3759    re = (Render_Engine *)data;
3760    share_ctx = (Render_Engine_GL_Context *)share_context;
3761
3762    // Set the share context to Evas' GL context if share_context is NULL.
3763    // Otherwise set it to the given share_context.
3764 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3765    // EGL
3766    context_attrs[0] = EGL_CONTEXT_CLIENT_VERSION;
3767    context_attrs[1] = 2;
3768    context_attrs[2] = EGL_NONE;
3769
3770    if (share_ctx)
3771      {
3772         ctx->context = eglCreateContext(re->win->egl_disp,
3773                                         re->win->egl_config,
3774                                         share_ctx->context,      // Share Context
3775                                         context_attrs);
3776      }
3777    else
3778      {
3779         ctx->context = eglCreateContext(re->win->egl_disp,
3780                                         re->win->egl_config,
3781                                         re->win->egl_context[0], // Evas' GL Context
3782                                         context_attrs);
3783      }
3784
3785    if (!ctx->context)
3786      {
3787         ERR("eglCreateContext() fail. code=%#x", eglGetError());
3788         return NULL;
3789      }
3790 #else
3791    // GLX
3792    if (share_context)
3793      {
3794         ctx->context = glXCreateContext(re->info->info.display,
3795                                         re->win->visualinfo,
3796                                         share_ctx->context,    // Share Context
3797                                         1);
3798      }
3799    else
3800      {
3801         ctx->context = glXCreateContext(re->info->info.display,
3802                                         re->win->visualinfo,
3803                                         re->win->context,      // Evas' GL Context
3804                                         1);
3805      }
3806
3807    if (!ctx->context)
3808      {
3809         ERR("glXCreateContext() fail.");
3810         return NULL;
3811      }
3812 #endif
3813
3814    ctx->initialized = 0;
3815    ctx->context_fbo = 0;
3816    ctx->current_sfc = NULL;
3817
3818    return ctx;
3819 }
3820
3821 static int
3822 eng_gl_context_destroy(void *data, void *context)
3823 {
3824    Render_Engine *re;
3825    Render_Engine_GL_Context *ctx;
3826    Render_Engine_GL_Resource *rsc;
3827    int ret;
3828
3829    re  = (Render_Engine *)data;
3830    ctx = (Render_Engine_GL_Context*)context;
3831
3832    if (!ctx) return 0;
3833
3834    // Use resource surface/context to create surface resrouces
3835    if (!_internal_resources_make_current(re))
3836      {
3837         ERR("Error doing a make current with the internal resources.");
3838         return 0;
3839      }
3840
3841    // Delete the FBO
3842    if (ctx->context_fbo)
3843       glDeleteFramebuffers(1, &ctx->context_fbo);
3844
3845    // Destroy the Context
3846 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3847    eglDestroyContext(re->win->egl_disp, ctx->context);
3848
3849    ctx->context = EGL_NO_CONTEXT;
3850
3851    ret = eglMakeCurrent(re->win->egl_disp, EGL_NO_SURFACE,
3852                         EGL_NO_SURFACE, EGL_NO_CONTEXT);
3853 #else
3854    glXDestroyContext(re->info->info.display, ctx->context);
3855
3856    ctx->context = 0;
3857
3858    ret = glXMakeCurrent(re->info->info.display, None, NULL);
3859 #endif
3860    if (!ret)
3861      {
3862         ERR("xxxMakeCurrent() failed!");
3863         return 0;
3864      }
3865
3866    free(ctx);
3867    context = NULL;
3868
3869    return 1;
3870 }
3871
3872 static int
3873 eng_gl_make_current(void *data __UNUSED__, void *surface, void *context)
3874 {
3875    Render_Engine *re;
3876    Render_Engine_GL_Surface *sfc;
3877    Render_Engine_GL_Context *ctx;
3878    int ret = 0;
3879 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3880    Render_Engine_GL_Resource *rsc;
3881 #endif
3882
3883    re  = (Render_Engine *)data;
3884    sfc = (Render_Engine_GL_Surface*)surface;
3885    ctx = (Render_Engine_GL_Context*)context;
3886
3887    current_engine = re;
3888
3889    // Unset surface/context
3890    if ((!sfc) || (!ctx))
3891      {
3892 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3893         ret = eglMakeCurrent(re->win->egl_disp, EGL_NO_SURFACE,
3894                              EGL_NO_SURFACE, EGL_NO_CONTEXT);
3895 #else
3896         ret = glXMakeCurrent(re->info->info.display, None, NULL);
3897 #endif
3898         if (!ret)
3899           {
3900              ERR("xxxMakeCurrent() failed!");
3901              return 0;
3902           }
3903
3904         if (ctx) ctx->current_sfc = NULL;
3905         if (sfc) sfc->current_ctx = NULL;
3906         current_evgl_ctx = NULL;
3907         return 1;
3908      }
3909
3910    // Check if direct rendering is possible:
3911    //    It's possible when direct_fb_opt is on and either current image
3912    //    object is valid or gl_direct_override is on.  Override allows
3913    //    rendering outside of pixel getter but it doesn't guarantee
3914    //    correct rendering.
3915    if ((sfc->direct_fb_opt) && (gl_direct_img_obj || gl_direct_override))
3916      {
3917 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3918         sfc->direct_sfc = re->win->egl_surface[0];
3919 #else
3920         sfc->direct_sfc = re->win->win;
3921 #endif
3922         gl_direct_enabled = 1;
3923      }
3924    else
3925       gl_direct_enabled = 0;
3926
3927    if (gl_direct_enabled)
3928      {
3929         int curr_fbo = 0;
3930
3931         // Do a make current only if it's not already current
3932 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3933         if ((eglGetCurrentContext() != ctx->context) ||
3934             (eglGetCurrentSurface(EGL_READ) != sfc->direct_sfc) ||
3935             (eglGetCurrentSurface(EGL_DRAW) != sfc->direct_sfc) )
3936           {
3937              DBG("Rendering Directly to the window\n");
3938
3939              // Flush remainder of what's in Evas' pipeline
3940              eng_window_use(NULL);
3941
3942              // Do a make current
3943              ret = eglMakeCurrent(re->win->egl_disp, sfc->direct_sfc,
3944                                   sfc->direct_sfc, ctx->context);
3945              if (!ret)
3946                {
3947                   ERR("xxxMakeCurrent() failed! code=%#x", eglGetError());
3948                   //ERR("xxxMakeCurrent() failed!");
3949                   return 0;
3950                }
3951           }
3952 #else
3953         if ((glXGetCurrentContext() != ctx->context))
3954           {
3955              // Flush remainder of what's in Evas' pipeline
3956              eng_window_use(NULL);
3957
3958              // Do a make current
3959              ret = glXMakeCurrent(re->info->info.display, sfc->direct_sfc, ctx->context);
3960              if (!ret)
3961                {
3962                   ERR("xxxMakeCurrent() failed!");
3963                   return 0;
3964                }
3965           }
3966 #endif
3967         glGetIntegerv(GL_FRAMEBUFFER_BINDING, &curr_fbo);
3968         if (ctx->context_fbo == curr_fbo)
3969           {
3970              ctx->current_fbo = 0;
3971              glBindFramebuffer(GL_FRAMEBUFFER, 0);
3972           }
3973
3974      }
3975    else
3976      {
3977         // Do a make current only if it's not already current
3978 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
3979         if (eina_main_loop_is())
3980           {
3981              if ((eglGetCurrentContext() != ctx->context) ||
3982                  (eglGetCurrentSurface(EGL_READ) != re->win->egl_surface[0]) ||
3983                  (eglGetCurrentSurface(EGL_DRAW) != re->win->egl_surface[0]) )
3984                {
3985
3986                   // Flush remainder of what's in Evas' pipeline
3987                   eng_window_use(NULL);
3988
3989                   // Do a make current
3990                   ret = eglMakeCurrent(re->win->egl_disp, re->win->egl_surface[0],
3991                                              re->win->egl_surface[0], ctx->context);
3992                   if (!ret)
3993                     {
3994                        ERR("xxxMakeCurrent() failed! code=%#x", eglGetError());
3995                        return 0;
3996                     }
3997                }
3998           }
3999         else
4000           {
4001              if ((rsc = eina_tls_get(resource_key)) == EINA_FALSE) return 0;
4002
4003              if ((eglGetCurrentContext() != ctx->context) ||
4004                  (eglGetCurrentSurface(EGL_READ) != rsc->surface) ||
4005                  (eglGetCurrentSurface(EGL_DRAW) != rsc->surface) )
4006                {
4007                   // Flush remainder of what's in Evas' pipeline
4008                   eng_window_use(NULL);
4009
4010                   // Do a make current
4011                   ret = eglMakeCurrent(re->win->egl_disp, rsc->surface,
4012                                              rsc->surface, ctx->context);
4013                   if (!ret)
4014                     {
4015                        ERR("xxxMakeCurrent() failed!");
4016                        return 0;
4017                     }
4018                }
4019           }
4020 #else
4021         if ((glXGetCurrentContext() != ctx->context) ||
4022             (glXGetCurrentDrawable() != re->win->win) )
4023           {
4024              // Flush remainder of what's in Evas' pipeline
4025              eng_window_use(NULL);
4026
4027              // Do a make current
4028              ret = glXMakeCurrent(re->info->info.display, re->win->win, ctx->context);
4029              if (!ret)
4030                {
4031                   ERR("xxxMakeCurrent() failed!");
4032                   return 0;
4033                }
4034           }
4035 #endif
4036
4037         // Create FBO if not already created
4038         if (!ctx->initialized)
4039           {
4040              glGenFramebuffers(1, &ctx->context_fbo);
4041              ctx->initialized = 1;
4042           }
4043
4044         // Attach FBO if it hasn't been attached or if surface changed
4045         if ((!sfc->fbo_attached) || (ctx->current_sfc != sfc))
4046           {
4047              if (!_attach_fbo_surface(re, sfc, ctx->context_fbo))
4048                {
4049                   ERR("_attach_fbo_surface() failed.");
4050                   _print_gl_surface_info(sfc, 1);
4051                   return 0;
4052                }
4053
4054              if (ctx->current_fbo)
4055                 // Bind to the previously bound buffer
4056                 glBindFramebuffer(GL_FRAMEBUFFER, ctx->current_fbo);
4057              else
4058                 // Bind FBO
4059                 glBindFramebuffer(GL_FRAMEBUFFER, ctx->context_fbo);
4060
4061              sfc->fbo_attached = 1;
4062           }
4063      }
4064
4065    // Set the current surface/context
4066    ctx->current_sfc = sfc;
4067    sfc->current_ctx = ctx;
4068    current_evgl_ctx = ctx;
4069    current_engine = re;
4070
4071    return 1;
4072 }
4073
4074 static void *
4075 eng_gl_string_query(void *data __UNUSED__, int name)
4076 {
4077    switch(name)
4078      {
4079       case EVAS_GL_EXTENSIONS:
4080          return (void*)_evasgl_ext_string;
4081       default:
4082          return NULL;
4083      };
4084 }
4085
4086 static void *
4087 eng_gl_proc_address_get(void *data __UNUSED__, const char *name)
4088 {
4089 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4090    if (glsym_eglGetProcAddress) return glsym_eglGetProcAddress(name);
4091    return dlsym(RTLD_DEFAULT, name);
4092 #else
4093    if (glsym_glXGetProcAddress) return glsym_glXGetProcAddress(name);
4094    return dlsym(RTLD_DEFAULT, name);
4095 #endif
4096 }
4097
4098 static int
4099 eng_gl_native_surface_get(void *data __UNUSED__, void *surface, void *native_surface)
4100 {
4101    Render_Engine_GL_Surface *sfc;
4102    Evas_Native_Surface *ns;
4103
4104    sfc = (Render_Engine_GL_Surface*)surface;
4105    ns  = (Evas_Native_Surface*)native_surface;
4106
4107    if (sfc->direct_fb_opt)
4108      {
4109         ns->type = EVAS_NATIVE_SURFACE_OPENGL;
4110         ns->version = EVAS_NATIVE_SURFACE_VERSION;
4111         ns->data.opengl.texture_id = sfc->rt_tex;
4112         ns->data.opengl.framebuffer_id = 0;
4113         ns->data.opengl.x = 0;
4114         ns->data.opengl.y = 0;
4115         ns->data.opengl.w = sfc->w;
4116         ns->data.opengl.h = sfc->h;
4117      }
4118    else
4119      {
4120         ns->type = EVAS_NATIVE_SURFACE_OPENGL;
4121         ns->version = EVAS_NATIVE_SURFACE_VERSION;
4122         ns->data.opengl.texture_id = sfc->rt_tex;
4123         ns->data.opengl.framebuffer_id = sfc->rt_tex;
4124         ns->data.opengl.x = 0;
4125         ns->data.opengl.y = 0;
4126         ns->data.opengl.w = sfc->w;
4127         ns->data.opengl.h = sfc->h;
4128      }
4129
4130    return 1;
4131 }
4132
4133
4134 static const GLubyte *
4135 evgl_glGetString(GLenum name)
4136 {
4137    if (name == GL_EXTENSIONS)
4138       return (GLubyte *)_gl_ext_string; //glGetString(GL_EXTENSIONS);
4139    else
4140       return glGetString(name);
4141 }
4142
4143 static void
4144 evgl_glBindFramebuffer(GLenum target, GLuint framebuffer)
4145 {
4146    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4147
4148    if (!ctx)
4149      {
4150         ERR("No current context set.");
4151         return;
4152      }
4153
4154    // Take care of BindFramebuffer 0 issue
4155    if (framebuffer==0)
4156      {
4157         if (gl_direct_enabled)
4158            glBindFramebuffer(target, 0);
4159         else
4160            glBindFramebuffer(target, ctx->context_fbo);
4161         ctx->current_fbo = 0;
4162      }
4163    else
4164      {
4165         glBindFramebuffer(target, framebuffer);
4166
4167         // Save this for restore when doing make current
4168         ctx->current_fbo = framebuffer;
4169      }
4170 }
4171
4172 static void
4173 evgl_glBindRenderbuffer(GLenum target, GLuint renderbuffer)
4174 {
4175    // Add logic to take care when renderbuffer=0
4176    // On a second thought we don't need this
4177    glBindRenderbuffer(target, renderbuffer);
4178 }
4179
4180 // Transform from Evas Coordinat to GL Coordinate
4181 // returns: oc[4] original image object dimension in gl coord
4182 // returns: nc[4] tranformed  (x, y, width, heigth) in gl coord
4183 static void
4184 compute_gl_coordinates(Evas_Object *obj, int rot, int clip,
4185                        int x, int y, int width, int height,
4186                        int imgc[4], int objc[4])
4187 {
4188    if (rot == 0)
4189      {
4190         // oringinal image object coordinate in gl coordinate
4191         imgc[0] = obj->cur.geometry.x;
4192         imgc[1] = obj->layer->evas->output.h - obj->cur.geometry.y - obj->cur.geometry.h;
4193         imgc[2] = imgc[0] + obj->cur.geometry.w;
4194         imgc[3] = imgc[1] + obj->cur.geometry.h;
4195
4196         // transformed (x,y,width,height) in gl coordinate
4197         objc[0] = imgc[0] + x;
4198         objc[1] = imgc[1] + y;
4199         objc[2] = objc[0] + width;
4200         objc[3] = objc[1] + height;
4201      }
4202    else if (rot == 180)
4203      {
4204         // oringinal image object coordinate in gl coordinate
4205         imgc[0] = obj->layer->evas->output.w - obj->cur.geometry.x - obj->cur.geometry.w;
4206         imgc[1] = obj->cur.geometry.y;
4207         imgc[2] = imgc[0] + obj->cur.geometry.w;
4208         imgc[3] = imgc[1] + obj->cur.geometry.h;
4209
4210         // transformed (x,y,width,height) in gl coordinate
4211         objc[0] = imgc[0] + obj->cur.geometry.w - x - width;
4212         objc[1] = imgc[1] + obj->cur.geometry.h - y - height;
4213         objc[2] = objc[0] + width;
4214         objc[3] = objc[1] + height;
4215
4216      }
4217    else if (rot == 90)
4218      {
4219         // oringinal image object coordinate in gl coordinate
4220         imgc[0] = obj->cur.geometry.y;
4221         imgc[1] = obj->cur.geometry.x;
4222         imgc[2] = imgc[0] + obj->cur.geometry.h;
4223         imgc[3] = imgc[1] + obj->cur.geometry.w;
4224
4225         // transformed (x,y,width,height) in gl coordinate
4226         objc[0] = imgc[0] + obj->cur.geometry.h - y - height;
4227         objc[1] = imgc[1] + x;
4228         objc[2] = objc[0] + height;
4229         objc[3] = objc[1] + width;
4230      }
4231    else if (rot == 270)
4232      {
4233         // oringinal image object coordinate in gl coordinate
4234         imgc[0] = obj->layer->evas->output.h - obj->cur.geometry.y - obj->cur.geometry.h;
4235         imgc[1] = obj->layer->evas->output.w - obj->cur.geometry.x - obj->cur.geometry.w;
4236         imgc[2] = imgc[0] + obj->cur.geometry.h;
4237         imgc[3] = imgc[1] + obj->cur.geometry.w;
4238
4239         // transformed (x,y,width,height) in gl coordinate
4240         objc[0] = imgc[0] + y;
4241         objc[1] = imgc[1] + obj->cur.geometry.w - x - width;
4242         objc[2] = objc[0] + height;
4243         objc[3] = objc[1] + width;
4244      }
4245    else
4246      {
4247         ERR("Invalid rotation angle %d.", rot);
4248         return;
4249      }
4250
4251    if (clip)
4252      {
4253         // Clip against original image object
4254         if (objc[0] < imgc[0]) objc[0] = imgc[0];
4255         if (objc[0] > imgc[2]) objc[0] = 0;
4256
4257         if (objc[1] < imgc[1]) objc[1] = imgc[1];
4258         if (objc[1] > imgc[3]) objc[1] = 0;
4259
4260         if (objc[2] < imgc[0]) objc[0] = 0;
4261         if (objc[2] > imgc[2]) objc[2] = imgc[2];
4262
4263         if (objc[3] < imgc[1]) objc[1] = 0;
4264         if (objc[3] > imgc[3]) objc[3] = imgc[3];
4265      }
4266
4267    imgc[2] = imgc[2]-imgc[0];     // width
4268    imgc[3] = imgc[3]-imgc[1];     // height
4269
4270    objc[2] = objc[2]-objc[0];     // width
4271    objc[3] = objc[3]-objc[1];     // height
4272 }
4273
4274 static void
4275 evgl_glClear(GLbitfield mask)
4276 {
4277    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4278    int rot = 0;
4279    int oc[4], nc[4];
4280
4281    if ((gl_direct_img_obj) && (gl_direct_enabled) && (ctx) && (!ctx->current_fbo))
4282      {
4283         if ((current_engine) && (current_engine->win) && (current_engine->win->gl_context))
4284            rot = current_engine->win->gl_context->rot;
4285         else
4286            ERR("Unable to retrieve rotation angle: %d", rot);
4287
4288         compute_gl_coordinates(gl_direct_img_obj, rot, 0, 0, 0, 0, 0, oc, nc);
4289         glScissor(oc[0], oc[1], oc[2], oc[3]);
4290         glClear(mask);
4291      }
4292    else
4293       glClear(mask);
4294 }
4295
4296 static void
4297 evgl_glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
4298 {
4299    glClearColor(red, green, blue, alpha);
4300 }
4301
4302 static void
4303 evgl_glEnable(GLenum cap)
4304 {
4305    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4306
4307    if (cap == GL_SCISSOR_TEST)
4308       if (ctx) ctx->scissor_enabled = 1;
4309    glEnable(cap);
4310 }
4311
4312 static void
4313 evgl_glDisable(GLenum cap)
4314 {
4315    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4316
4317    if (cap == GL_SCISSOR_TEST)
4318       if (ctx) ctx->scissor_enabled = 0;
4319    glDisable(cap);
4320 }
4321
4322
4323 static void
4324 evgl_glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
4325 {
4326    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4327    int rot = 0;
4328    int oc[4], nc[4];
4329
4330    if ((gl_direct_img_obj) && (gl_direct_enabled) && (ctx) && (!ctx->current_fbo))
4331      {
4332         if ((current_engine) && (current_engine->win) && (current_engine->win->gl_context))
4333            rot = current_engine->win->gl_context->rot;
4334         else
4335            ERR("Unable to retrieve rotation angle: %d", rot);
4336
4337         compute_gl_coordinates(gl_direct_img_obj, rot, 1, x, y, width, height, oc, nc);
4338         glReadPixels(nc[0], nc[1], nc[2], nc[3], format, type, pixels);
4339      }
4340    else
4341       glReadPixels(x, y, width, height, format, type, pixels);
4342 }
4343
4344 static void
4345 evgl_glScissor(GLint x, GLint y, GLsizei width, GLsizei height)
4346 {
4347    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4348    int rot = 0;
4349    int oc[4], nc[4];
4350
4351    if ((gl_direct_img_obj) && (gl_direct_enabled) && (ctx) && (!ctx->current_fbo))
4352      {
4353         if ((current_engine) && (current_engine->win) && (current_engine->win->gl_context))
4354            rot = current_engine->win->gl_context->rot;
4355         else
4356            ERR("Unable to retrieve rotation angle: %d", rot);
4357
4358         compute_gl_coordinates(gl_direct_img_obj, rot, 1, x, y, width, height, oc, nc);
4359         glScissor(nc[0], nc[1], nc[2], nc[3]);
4360         ctx->scissor_upated = 1;
4361      }
4362    else
4363       glScissor(x, y, width, height);
4364 }
4365
4366 static void
4367 evgl_glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
4368 {
4369    Render_Engine_GL_Context *ctx = current_evgl_ctx;
4370    int rot = 0;
4371    int oc[4], nc[4];
4372
4373    if ((gl_direct_img_obj) && (gl_direct_enabled) && (ctx) && (!ctx->current_fbo))
4374      {
4375         if ((current_engine) && (current_engine->win) && (current_engine->win->gl_context))
4376            rot = current_engine->win->gl_context->rot;
4377         else
4378            ERR("Unable to retrieve rotation angle: %d", rot);
4379
4380         compute_gl_coordinates(gl_direct_img_obj, rot, 0, x, y, width, height, oc, nc);
4381         glEnable(GL_SCISSOR_TEST);
4382         glScissor(oc[0], oc[1], oc[2], oc[3]);
4383         glViewport(nc[0], nc[1], nc[2], nc[3]);
4384      }
4385    else
4386       glViewport(x, y, width, height);
4387
4388 }
4389
4390
4391 //----------------------------------------------//
4392
4393 static void
4394 evgl_glClearDepthf(GLclampf depth)
4395 {
4396 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4397    glClearDepthf(depth);
4398 #else
4399    glClearDepth(depth);
4400 #endif
4401 }
4402
4403 static void
4404 evgl_glDepthRangef(GLclampf zNear, GLclampf zFar)
4405 {
4406 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4407    glDepthRangef(zNear, zFar);
4408 #else
4409    glDepthRange(zNear, zFar);
4410 #endif
4411 }
4412
4413 static void
4414 evgl_glGetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype, GLint* range, GLint* precision)
4415 {
4416 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4417    glGetShaderPrecisionFormat(shadertype, precisiontype, range, precision);
4418 #else
4419    if (range)
4420      {
4421         range[0] = -126; // floor(log2(FLT_MIN))
4422         range[1] = 127; // floor(log2(FLT_MAX))
4423      }
4424    if (precision)
4425      {
4426         precision[0] = 24; // floor(-log2((1.0/16777218.0)));
4427      }
4428    return;
4429 #endif
4430 }
4431
4432 static void
4433 evgl_glReleaseShaderCompiler(void)
4434 {
4435 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4436    glReleaseShaderCompiler();
4437 #else
4438 #endif
4439 }
4440
4441 static void
4442 evgl_glShaderBinary(GLsizei n, const GLuint* shaders, GLenum binaryformat, const void* binary, GLsizei length)
4443 {
4444 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4445    glShaderBinary(n, shaders, binaryformat, binary, length);
4446 #else
4447 // FIXME: need to dlsym/getprocaddress for this
4448    return;
4449    n = binaryformat = length = 0;
4450    shaders = binary = 0;
4451 #endif
4452 }
4453
4454 //--------------------------------//
4455 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4456 // EGL Extensions
4457 static void *
4458 evgl_evasglCreateImage(int target, void* buffer, int *attrib_list)
4459 {
4460    if (current_engine)
4461      {
4462         return glsym_eglCreateImage(current_engine->win->egl_disp,
4463                                     EGL_NO_CONTEXT,
4464                                     target,
4465                                     buffer,
4466                                     attrib_list);
4467      }
4468    else
4469      {
4470         ERR("Invalid Engine... (Can't acccess EGL Display)\n");
4471         return NULL;
4472      }
4473 }
4474
4475 static void
4476 evgl_evasglDestroyImage(EvasGLImage image)
4477 {
4478    if (current_engine)
4479         glsym_eglDestroyImage(current_engine->win->egl_disp, image);
4480    else
4481       ERR("Invalid Engine... (Can't acccess EGL Display)\n");
4482 }
4483
4484 static void
4485 evgl_glEvasGLImageTargetTexture2DOES(GLenum target, EvasGLImage image)
4486 {
4487    glsym_glEGLImageTargetTexture2DOES(target, image);
4488 }
4489
4490 static void
4491 evgl_glEvasGLImageTargetRenderbufferStorageOES(GLenum target, EvasGLImage image)
4492 {
4493    glsym_glEGLImageTargetTexture2DOES(target, image);
4494 }
4495 #else
4496 #endif
4497
4498 //--------------------------------//
4499
4500
4501 static void *
4502 eng_gl_api_get(void *data)
4503 {
4504    Render_Engine *re;
4505
4506    re  = (Render_Engine *)data;
4507
4508    gl_funcs.version = EVAS_GL_API_VERSION;
4509
4510 #define ORD(f) EVAS_API_OVERRIDE(f, &gl_funcs, )
4511    // GLES 2.0
4512    ORD(glActiveTexture);
4513    ORD(glAttachShader);
4514    ORD(glBindAttribLocation);
4515    ORD(glBindBuffer);
4516    ORD(glBindTexture);
4517    ORD(glBlendColor);
4518    ORD(glBlendEquation);
4519    ORD(glBlendEquationSeparate);
4520    ORD(glBlendFunc);
4521    ORD(glBlendFuncSeparate);
4522    ORD(glBufferData);
4523    ORD(glBufferSubData);
4524    ORD(glCheckFramebufferStatus);
4525 //   ORD(glClear);
4526 //   ORD(glClearColor);
4527 //   ORD(glClearDepthf);
4528    ORD(glClearStencil);
4529    ORD(glColorMask);
4530    ORD(glCompileShader);
4531    ORD(glCompressedTexImage2D);
4532    ORD(glCompressedTexSubImage2D);
4533    ORD(glCopyTexImage2D);
4534    ORD(glCopyTexSubImage2D);
4535    ORD(glCreateProgram);
4536    ORD(glCreateShader);
4537    ORD(glCullFace);
4538    ORD(glDeleteBuffers);
4539    ORD(glDeleteFramebuffers);
4540    ORD(glDeleteProgram);
4541    ORD(glDeleteRenderbuffers);
4542    ORD(glDeleteShader);
4543    ORD(glDeleteTextures);
4544    ORD(glDepthFunc);
4545    ORD(glDepthMask);
4546 //   ORD(glDepthRangef);
4547    ORD(glDetachShader);
4548 //   ORD(glDisable);
4549    ORD(glDisableVertexAttribArray);
4550    ORD(glDrawArrays);
4551    ORD(glDrawElements);
4552 //   ORD(glEnable);
4553    ORD(glEnableVertexAttribArray);
4554    ORD(glFinish);
4555    ORD(glFlush);
4556    ORD(glFramebufferRenderbuffer);
4557    ORD(glFramebufferTexture2D);
4558    ORD(glFrontFace);
4559    ORD(glGenBuffers);
4560    ORD(glGenerateMipmap);
4561    ORD(glGenFramebuffers);
4562    ORD(glGenRenderbuffers);
4563    ORD(glGenTextures);
4564    ORD(glGetActiveAttrib);
4565    ORD(glGetActiveUniform);
4566    ORD(glGetAttachedShaders);
4567    ORD(glGetAttribLocation);
4568    ORD(glGetBooleanv);
4569    ORD(glGetBufferParameteriv);
4570    ORD(glGetError);
4571    ORD(glGetFloatv);
4572    ORD(glGetFramebufferAttachmentParameteriv);
4573    ORD(glGetIntegerv);
4574    ORD(glGetProgramiv);
4575    ORD(glGetProgramInfoLog);
4576    ORD(glGetRenderbufferParameteriv);
4577    ORD(glGetShaderiv);
4578    ORD(glGetShaderInfoLog);
4579 //   ORD(glGetShaderPrecisionFormat);
4580    ORD(glGetShaderSource);
4581 //   ORD(glGetString);
4582    ORD(glGetTexParameterfv);
4583    ORD(glGetTexParameteriv);
4584    ORD(glGetUniformfv);
4585    ORD(glGetUniformiv);
4586    ORD(glGetUniformLocation);
4587    ORD(glGetVertexAttribfv);
4588    ORD(glGetVertexAttribiv);
4589    ORD(glGetVertexAttribPointerv);
4590    ORD(glHint);
4591    ORD(glIsBuffer);
4592    ORD(glIsEnabled);
4593    ORD(glIsFramebuffer);
4594    ORD(glIsProgram);
4595    ORD(glIsRenderbuffer);
4596    ORD(glIsShader);
4597    ORD(glIsTexture);
4598    ORD(glLineWidth);
4599    ORD(glLinkProgram);
4600    ORD(glPixelStorei);
4601    ORD(glPolygonOffset);
4602    ORD(glReadPixels);
4603 //   ORD(glReleaseShaderCompiler);
4604    ORD(glRenderbufferStorage);
4605    ORD(glSampleCoverage);
4606 //   ORD(glScissor);
4607 //   ORD(glShaderBinary);
4608    ORD(glShaderSource);
4609    ORD(glStencilFunc);
4610    ORD(glStencilFuncSeparate);
4611    ORD(glStencilMask);
4612    ORD(glStencilMaskSeparate);
4613    ORD(glStencilOp);
4614    ORD(glStencilOpSeparate);
4615    ORD(glTexImage2D);
4616    ORD(glTexParameterf);
4617    ORD(glTexParameterfv);
4618    ORD(glTexParameteri);
4619    ORD(glTexParameteriv);
4620    ORD(glTexSubImage2D);
4621    ORD(glUniform1f);
4622    ORD(glUniform1fv);
4623    ORD(glUniform1i);
4624    ORD(glUniform1iv);
4625    ORD(glUniform2f);
4626    ORD(glUniform2fv);
4627    ORD(glUniform2i);
4628    ORD(glUniform2iv);
4629    ORD(glUniform3f);
4630    ORD(glUniform3fv);
4631    ORD(glUniform3i);
4632    ORD(glUniform3iv);
4633    ORD(glUniform4f);
4634    ORD(glUniform4fv);
4635    ORD(glUniform4i);
4636    ORD(glUniform4iv);
4637    ORD(glUniformMatrix2fv);
4638    ORD(glUniformMatrix3fv);
4639    ORD(glUniformMatrix4fv);
4640    ORD(glUseProgram);
4641    ORD(glValidateProgram);
4642    ORD(glVertexAttrib1f);
4643    ORD(glVertexAttrib1fv);
4644    ORD(glVertexAttrib2f);
4645    ORD(glVertexAttrib2fv);
4646    ORD(glVertexAttrib3f);
4647    ORD(glVertexAttrib3fv);
4648    ORD(glVertexAttrib4f);
4649    ORD(glVertexAttrib4fv);
4650    ORD(glVertexAttribPointer);
4651 //   ORD(glViewport);
4652 #undef ORD
4653
4654 #define ORD(f) EVAS_API_OVERRIDE(f, &gl_funcs, glsym_)
4655    // Extensions
4656    ORD(glGetProgramBinaryOES);
4657    ORD(glProgramBinaryOES);
4658    ORD(glMapBufferOES);
4659    ORD(glUnmapBufferOES);
4660    ORD(glGetBufferPointervOES);
4661    ORD(glTexImage3DOES);
4662    ORD(glTexSubImage3DOES);
4663    ORD(glCopyTexSubImage3DOES);
4664    ORD(glCompressedTexImage3DOES);
4665    ORD(glCompressedTexSubImage3DOES);
4666    ORD(glFramebufferTexture3DOES);
4667    ORD(glGetPerfMonitorGroupsAMD);
4668    ORD(glGetPerfMonitorCountersAMD);
4669    ORD(glGetPerfMonitorGroupStringAMD);
4670    ORD(glGetPerfMonitorCounterStringAMD);
4671    ORD(glGetPerfMonitorCounterInfoAMD);
4672    ORD(glGenPerfMonitorsAMD);
4673    ORD(glDeletePerfMonitorsAMD);
4674    ORD(glSelectPerfMonitorCountersAMD);
4675    ORD(glBeginPerfMonitorAMD);
4676    ORD(glEndPerfMonitorAMD);
4677    ORD(glGetPerfMonitorCounterDataAMD);
4678    ORD(glDiscardFramebufferEXT);
4679    ORD(glMultiDrawArraysEXT);
4680    ORD(glMultiDrawElementsEXT);
4681    ORD(glDeleteFencesNV);
4682    ORD(glGenFencesNV);
4683    ORD(glIsFenceNV);
4684    ORD(glTestFenceNV);
4685    ORD(glGetFenceivNV);
4686    ORD(glFinishFenceNV);
4687    ORD(glSetFenceNV);
4688    ORD(glGetDriverControlsQCOM);
4689    ORD(glGetDriverControlStringQCOM);
4690    ORD(glEnableDriverControlQCOM);
4691    ORD(glDisableDriverControlQCOM);
4692    ORD(glExtGetTexturesQCOM);
4693    ORD(glExtGetBuffersQCOM);
4694    ORD(glExtGetRenderbuffersQCOM);
4695    ORD(glExtGetFramebuffersQCOM);
4696    ORD(glExtGetTexLevelParameterivQCOM);
4697    ORD(glExtTexObjectStateOverrideiQCOM);
4698    ORD(glExtGetTexSubImageQCOM);
4699    ORD(glExtGetBufferPointervQCOM);
4700    ORD(glExtGetShadersQCOM);
4701    ORD(glExtGetProgramsQCOM);
4702    ORD(glExtIsProgramBinaryQCOM);
4703    ORD(glExtGetProgramBinarySourceQCOM);
4704 #undef ORD
4705
4706 // Override functions wrapped by Evas_GL
4707 #define ORD(f) EVAS_API_OVERRIDE(f, &gl_funcs, evgl_)
4708    ORD(glBindFramebuffer);
4709    ORD(glBindRenderbuffer);
4710
4711    ORD(glClear);
4712    ORD(glClearColor);
4713    ORD(glEnable);
4714    ORD(glDisable);
4715    ORD(glReadPixels);
4716    ORD(glScissor);
4717    ORD(glViewport);
4718
4719    // GLES2.0 API compat on top of desktop gl
4720    ORD(glClearDepthf);
4721    ORD(glDepthRangef);
4722    ORD(glGetShaderPrecisionFormat);
4723    ORD(glReleaseShaderCompiler);
4724    ORD(glShaderBinary);
4725
4726    ORD(glGetString);
4727
4728 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
4729    // GLES 2.0 Extensions that needs wrapping
4730    ORD(evasglCreateImage);
4731    ORD(evasglDestroyImage);
4732    ORD(glEvasGLImageTargetTexture2DOES);
4733    ORD(glEvasGLImageTargetRenderbufferStorageOES);
4734 #endif
4735
4736 #undef ORD
4737
4738    return &gl_funcs;
4739 }
4740
4741 static void
4742 eng_gl_img_obj_set(void *data, void *image, int has_alpha)
4743 {
4744    Render_Engine *re = (Render_Engine *)data;
4745
4746    gl_direct_img_obj = NULL;
4747
4748    // Normally direct rendering isn't allowed if alpha is on and
4749    // rotation is not 0.  BUT, if override is on, allow it.
4750    if ((has_alpha) || (re->win->gl_context->rot!=0))
4751      {
4752         if (gl_direct_override)
4753            gl_direct_img_obj = image;
4754      }
4755    else
4756       gl_direct_img_obj = image;
4757 }
4758
4759 static int
4760 eng_image_load_error_get(void *data __UNUSED__, void *image)
4761 {
4762    Evas_GL_Image *im;
4763
4764    if (!image) return EVAS_LOAD_ERROR_NONE;
4765    im = image;
4766    return im->im->cache_entry.load_error;
4767 }
4768
4769 static Eina_Bool
4770 eng_image_animated_get(void *data __UNUSED__, void *image)
4771 {
4772    Evas_GL_Image *gim = image;
4773    Image_Entry *im;
4774
4775    if (!gim) return EINA_FALSE;
4776    im = (Image_Entry *)gim->im;
4777    if (!im) return EINA_FALSE;
4778
4779    return im->flags.animated;
4780 }
4781
4782 static int
4783 eng_image_animated_frame_count_get(void *data __UNUSED__, void *image)
4784 {
4785    Evas_GL_Image *gim = image;
4786    Image_Entry *im;
4787
4788    if (!gim) return -1;
4789    im = (Image_Entry *)gim->im;
4790    if (!im) return -1;
4791
4792    if (!im->flags.animated) return -1;
4793    return im->frame_count;
4794 }
4795
4796 static Evas_Image_Animated_Loop_Hint
4797 eng_image_animated_loop_type_get(void *data __UNUSED__, void *image)
4798 {
4799    Evas_GL_Image *gim = image;
4800    Image_Entry *im;
4801
4802    if (!gim) return EVAS_IMAGE_ANIMATED_HINT_NONE;
4803    im = (Image_Entry *)gim->im;
4804    if (!im) return EVAS_IMAGE_ANIMATED_HINT_NONE;
4805
4806    if (!im->flags.animated) return EVAS_IMAGE_ANIMATED_HINT_NONE;
4807    return im->loop_hint;
4808 }
4809
4810 static int
4811 eng_image_animated_loop_count_get(void *data __UNUSED__, void *image)
4812 {
4813    Evas_GL_Image *gim = image;
4814    Image_Entry *im;
4815
4816    if (!gim) return -1;
4817    im = (Image_Entry *)gim->im;
4818    if (!im) return -1;
4819
4820    if (!im->flags.animated) return -1;
4821    return im->loop_count;
4822 }
4823
4824 static double
4825 eng_image_animated_frame_duration_get(void *data __UNUSED__, void *image, int start_frame, int frame_num)
4826 {
4827    Evas_GL_Image *gim = image;
4828    Image_Entry *im;
4829
4830    if (!gim) return -1;
4831    im = (Image_Entry *)gim->im;
4832    if (!im) return -1;
4833
4834    if (!im->flags.animated) return -1;
4835    return evas_common_load_rgba_image_frame_duration_from_file(im, start_frame, frame_num);
4836 }
4837
4838 static Eina_Bool
4839 eng_image_animated_frame_set(void *data __UNUSED__, void *image, int frame_index)
4840 {
4841    Evas_GL_Image *gim = image;
4842    Image_Entry *im;
4843
4844    if (!gim) return EINA_FALSE;
4845    im = (Image_Entry *)gim->im;
4846    if (!im) return EINA_FALSE;
4847
4848    if (!im->flags.animated) return EINA_FALSE;
4849    if (im->cur_frame == frame_index) return EINA_FALSE;
4850
4851    im->cur_frame = frame_index;
4852    return EINA_TRUE;
4853 }
4854
4855 static Eina_Bool
4856 eng_image_can_region_get(void *data __UNUSED__, void *image)
4857 {
4858    Evas_GL_Image *gim = image;
4859    Image_Entry *im;
4860    if (!gim) return EINA_FALSE;
4861    im = (Image_Entry *)gim->im;
4862    if (!im) return EINA_FALSE;
4863    return ((Evas_Image_Load_Func*) im->info.loader)->do_region;
4864 }
4865
4866
4867 static void
4868 eng_image_max_size_get(void *data, int *maxw, int *maxh)
4869 {
4870    Render_Engine *re = (Render_Engine *)data;
4871    if (maxw) *maxw = re->win->gl_context->shared->info.max_texture_size;
4872    if (maxh) *maxh = re->win->gl_context->shared->info.max_texture_size;
4873 }
4874
4875 static int
4876 module_open(Evas_Module *em)
4877 {
4878    static Eina_Bool xrm_inited = EINA_FALSE;
4879    if (!xrm_inited)
4880      {
4881         xrm_inited = EINA_TRUE;
4882         XrmInitialize();
4883      }
4884
4885    if (!em) return 0;
4886    if (!evas_gl_common_module_open()) return 0;
4887    /* get whatever engine module we inherit from */
4888    if (!_evas_module_engine_inherit(&pfunc, "software_generic")) return 0;
4889    if (_evas_engine_GL_X11_log_dom < 0)
4890      _evas_engine_GL_X11_log_dom = eina_log_domain_register
4891        ("evas-gl_x11", EVAS_DEFAULT_LOG_COLOR);
4892    if (_evas_engine_GL_X11_log_dom < 0)
4893      {
4894         EINA_LOG_ERR("Can not create a module log domain.");
4895         return 0;
4896      }
4897
4898
4899    /* Allow alpha for evas gl direct rendering */
4900    if (getenv("EVAS_GL_DIRECT_OVERRIDE"))
4901      {
4902         gl_direct_override = 1;
4903         DBG("########################################################");
4904         DBG("######### [Evas] Direct overriding is enabled ##########");
4905         DBG("########################################################");
4906      }
4907
4908    /* store it for later use */
4909    func = pfunc;
4910    /* now to override methods */
4911    #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
4912    ORD(info);
4913    ORD(info_free);
4914    ORD(setup);
4915    ORD(canvas_alpha_get);
4916    ORD(output_free);
4917    ORD(output_resize);
4918    ORD(output_tile_size_set);
4919    ORD(output_redraws_rect_add);
4920    ORD(output_redraws_rect_del);
4921    ORD(output_redraws_clear);
4922    ORD(output_redraws_next_update_get);
4923    ORD(output_redraws_next_update_push);
4924    ORD(context_cutout_add);
4925    ORD(context_cutout_clear);
4926    ORD(output_flush);
4927    ORD(output_idle_flush);
4928    ORD(output_dump);
4929    ORD(rectangle_draw);
4930    ORD(line_draw);
4931    ORD(polygon_point_add);
4932    ORD(polygon_points_clear);
4933    ORD(polygon_draw);
4934
4935    ORD(image_load);
4936    ORD(image_new_from_data);
4937    ORD(image_new_from_copied_data);
4938    ORD(image_free);
4939    ORD(image_size_get);
4940    ORD(image_size_set);
4941    ORD(image_dirty_region);
4942    ORD(image_data_get);
4943    ORD(image_data_put);
4944    ORD(image_data_preload_request);
4945    ORD(image_data_preload_cancel);
4946    ORD(image_alpha_set);
4947    ORD(image_alpha_get);
4948    ORD(image_border_set);
4949    ORD(image_border_get);
4950    ORD(image_draw);
4951    ORD(image_comment_get);
4952    ORD(image_format_get);
4953    ORD(image_colorspace_set);
4954    ORD(image_colorspace_get);
4955    ORD(image_can_region_get);
4956    ORD(image_mask_create);
4957    ORD(image_native_set);
4958    ORD(image_native_get);
4959 #if 0 // filtering disabled
4960    ORD(image_draw_filtered);
4961    ORD(image_filtered_get);
4962    ORD(image_filtered_save);
4963    ORD(image_filtered_free);
4964 #endif
4965
4966    ORD(font_draw);
4967
4968    ORD(image_scale_hint_set);
4969    ORD(image_scale_hint_get);
4970    ORD(image_stride_get);
4971
4972    ORD(image_map_draw);
4973    ORD(image_map_surface_new);
4974    ORD(image_map_surface_free);
4975    ORD(image_map_clean);
4976
4977    ORD(image_content_hint_set);
4978    ORD(image_content_hint_get);
4979
4980    ORD(image_cache_flush);
4981    ORD(image_cache_set);
4982    ORD(image_cache_get);
4983
4984    ORD(gl_surface_create);
4985    ORD(gl_surface_destroy);
4986    ORD(gl_context_create);
4987    ORD(gl_context_destroy);
4988    ORD(gl_make_current);
4989    ORD(gl_string_query);
4990    ORD(gl_proc_address_get);
4991    ORD(gl_native_surface_get);
4992    ORD(gl_api_get);
4993    ORD(gl_img_obj_set);
4994
4995    ORD(image_load_error_get);
4996
4997    /* now advertise out own api */
4998    ORD(image_animated_get);
4999    ORD(image_animated_frame_count_get);
5000    ORD(image_animated_loop_type_get);
5001    ORD(image_animated_loop_count_get);
5002    ORD(image_animated_frame_duration_get);
5003    ORD(image_animated_frame_set);
5004
5005    ORD(image_max_size_get);
5006
5007    /* now advertise out own api */
5008    em->functions = (void *)(&func);
5009    return 1;
5010 }
5011
5012 static void
5013 module_close(Evas_Module *em __UNUSED__)
5014 {
5015     eina_log_domain_unregister(_evas_engine_GL_X11_log_dom);
5016 /*
5017     if (xrdb_user.db)
5018       {
5019          XrmDestroyDatabase(xrdb_user.db);
5020          xrdb_user.last_stat = 0;
5021          xrdb_user.last_mtime = 0;
5022          xrdb_user.db = NULL;
5023       }
5024  */
5025     evas_gl_common_module_close();
5026 }
5027
5028 static Evas_Module_Api evas_modapi =
5029 {
5030    EVAS_MODULE_API_VERSION,
5031    "gl_x11",
5032    "none",
5033    {
5034      module_open,
5035      module_close
5036    }
5037 };
5038
5039 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_ENGINE, engine, gl_x11);
5040
5041 #ifndef EVAS_STATIC_BUILD_GL_XLIB
5042 EVAS_EINA_MODULE_DEFINE(engine, gl_x11);
5043 #endif
5044
5045 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/