44ad45ed581b796d7392f1baf14fa21c0d8fd1df
[platform/upstream/efl.git] / src / modules / evas / engines / software_x11 / evas_engine.c
1 #include "evas_common_private.h"
2 #include "evas_private.h"
3 #ifdef EVAS_CSERVE2
4 #include "evas_cs2_private.h"
5 #endif
6
7 #include "Evas_Engine_Software_X11.h"
8 #include "evas_engine.h"
9
10 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
11 # include "evas_xlib_outbuf.h"
12 # include "evas_xlib_buffer.h"
13 # include "evas_xlib_swapbuf.h"
14 # include "evas_xlib_color.h"
15 # include "evas_xlib_image.h"
16 # include "evas_xlib_dri_image.h"
17 #endif
18
19 #ifdef BUILD_ENGINE_SOFTWARE_XCB
20 # include "evas_xcb_outbuf.h"
21 # include "evas_xcb_color.h"
22 # include "evas_xcb_xdefaults.h"
23 # include "evas_xcb_image.h"
24 # include "evas_xcb_buffer.h"
25 #endif
26
27 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
28 #include "evas_x_egl.h"
29 #endif
30
31 #include "../software_generic/evas_native_common.h"
32
33 #ifdef HAVE_DLSYM
34 # include <dlfcn.h>
35 #endif
36
37 #include <Ecore.h>
38 #include <Eina.h>
39
40 Evas_Native_Tbm_Surface_Image_Set_Call  glsym__evas_native_tbm_surface_image_set = NULL;
41 Evas_Native_Tbm_Surface_Stride_Get_Call  glsym__evas_native_tbm_surface_stride_get = NULL;
42 int _evas_engine_soft_x11_log_dom = -1;
43
44 /* function tables - filled in later (func and parent func) */
45 static Evas_Func func, pfunc;
46
47 /* engine struct data */
48 typedef struct _Render_Engine Render_Engine;
49
50 struct _Render_Engine
51 {
52    Render_Engine_Software_Generic generic;
53    Eina_Bool (*outbuf_alpha_get)(Outbuf *ob);
54
55    struct {
56       void *disp;
57       void *config;
58       void *surface;
59    } egl;
60 };
61
62 typedef struct _Region_Push_Hook_Ctx {
63 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
64    X_Output_Buffer *changed_pixels;
65 #else
66    Xcb_Output_Buffer *changed_pixels;
67 #endif
68    Outbuf *buf;
69    Eina_Spinlock *lock;
70    struct {
71       void (*cb)(Evas *evas, int x, int y, int w, int h, const void *pixels);
72       Evas *evas;
73    } region_push_hook;
74    int x;
75    int y;
76 } Region_Push_Hook_Ctx;
77
78 /* prototypes we will use here */
79 static void *_best_visual_get(int backend, void *connection, int screen);
80 static unsigned int _best_colormap_get(int backend, void *connection, int screen);
81 static int _best_depth_get(int backend, void *connection, int screen);
82
83 static void *eng_info(Evas *eo_e);
84 static void eng_info_free(Evas *eo_e, void *info);
85 static int eng_setup(Evas *eo_e, void *info);
86 static void eng_output_free(void *data);
87
88 static Eina_List *_outbufs = NULL;
89
90 /* internal engine routines */
91
92
93 static void
94 _evas_software_x11_region_push_hook_call(void *data)
95 {
96    Region_Push_Hook_Ctx *ctx = data;
97
98    if (eina_list_data_find(_outbufs, ctx->buf))
99      {
100 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
101         ctx->region_push_hook.cb(ctx->region_push_hook.evas, ctx->x, ctx->y,
102                                  ctx->changed_pixels->xim->width,
103                                  ctx->changed_pixels->xim->height,
104                                  evas_software_xlib_x_output_buffer_data(ctx->changed_pixels, NULL));
105         eina_spinlock_take(ctx->lock);
106         evas_software_xlib_x_output_buffer_unref(ctx->changed_pixels, 0);
107         eina_spinlock_release(ctx->lock);
108 #else
109         ctx->region_push_hook.cb(ctx->region_push_hook.evas, ctx->x, ctx->y,
110                                  ctx->changed_pixels->xim->width,
111                                  ctx->changed_pixels->xim->height,
112                                  evas_software_xcb_output_buffer_data(ctx->changed_pixels, NULL));
113         eina_spinlock_take(ctx->lock);
114         evas_software_xcb_output_buffer_unref(ctx->changed_pixels, EINA_FALSE);
115         eina_spinlock_release(ctx->lock);
116 #endif
117      }
118    free(ctx);
119 }
120
121 void
122 evas_software_x11_region_push_hook_call(Outbuf *buf, int x, int y, void *out_buf,
123                                         Eina_Spinlock *lock)
124 {
125    Region_Push_Hook_Ctx *ctx;
126
127    if (!buf->region_push_hook.cb)
128      return;
129
130    ctx = malloc(sizeof(Region_Push_Hook_Ctx));
131    EINA_SAFETY_ON_NULL_RETURN(ctx);
132    ctx->x = x;
133    ctx->y = y;
134    ctx->region_push_hook.cb = buf->region_push_hook.cb;
135    ctx->region_push_hook.evas = buf->region_push_hook.evas;
136 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
137    ctx->changed_pixels = evas_software_xlib_x_output_buffer_ref(out_buf);
138 #else
139    ctx->changed_pixels = evas_software_xcb_output_buffer_ref(out_buf);
140 #endif
141    ctx->buf = buf;
142    ctx->lock = lock;
143    ecore_main_loop_thread_safe_call_async(_evas_software_x11_region_push_hook_call,
144                                           ctx);
145 }
146
147 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
148
149 /*
150 static void *
151 _output_egl_setup(int w, int h, int rot, Display *disp, Drawable draw,
152                   Visual *vis, Colormap cmap, int depth, int debug,
153                   int grayscale, int max_colors, Pixmap mask,
154                   int shape_dither, int destination_alpha)
155 {
156    Render_Engine *re;
157    void *ptr;
158    int stride = 0;
159    
160    if (depth != 32) return NULL;
161    if (mask) return NULL;
162    if (!(re = calloc(1, sizeof(Render_Engine)))) return NULL;
163    re->egl.disp = _egl_x_disp_get(disp);
164    if (!re->egl.disp)
165      {
166         free(re);
167         return NULL;
168      }
169    re->egl.config = _egl_x_disp_choose_config(re->egl.disp);
170    if (!re->egl.config)
171      {
172         _egl_x_disp_terminate(re->egl.disp);
173         free(re);
174         return NULL;
175      }
176    re->egl.surface = _egl_x_win_surf_new(re->egl.disp, draw, re->egl.config);
177    if (!re->egl.surface)
178      {
179         _egl_x_disp_terminate(re->egl.disp);
180         free(re);
181         return NULL;
182      }
183    ptr = _egl_x_surf_map(re->egl.disp, re->egl.surface, &stride);
184    if (!ptr)
185      {
186         _egl_x_win_surf_free(re->egl.disp, re->egl.surface);
187         _egl_x_disp_terminate(re->egl.disp);
188         free(re);
189         return NULL;
190      }
191    _egl_x_surf_unmap(re->egl.disp, re->egl.surface);
192    
193    re->ob = 
194      evas_software_egl_outbuf_setup_x(w, h, rot, OUTBUF_DEPTH_INHERIT, disp, 
195                                        draw, vis, cmap, depth, grayscale,
196                                        max_colors, mask, shape_dither,
197                                        destination_alpha);
198    
199    re->tb = evas_common_tilebuf_new(w, h);
200    if (!re->tb)
201      {
202         evas_software_xlib_outbuf_free(re->ob);
203         free(re);
204         return NULL;
205      }
206
207    evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
208    
209    return re;
210    debug = 0;
211 }
212 */
213
214 static void
215 _output_egl_shutdown(Render_Engine *re)
216 {
217    if (!re->egl.disp) return;
218    _egl_x_win_surf_free(re->egl.disp, re->egl.surface);
219    _egl_x_disp_terminate(re->egl.disp);
220 }
221
222 static void *
223 _output_xlib_setup(int w, int h, int rot, Display *disp, Drawable draw,
224                    Visual *vis, Colormap cmap, int depth, int debug,
225                    int grayscale, int max_colors, Pixmap mask,
226                    int shape_dither, int destination_alpha)
227 {
228    Render_Engine *re;
229    Outbuf *ob;
230    Render_Engine_Merge_Mode merge_mode = MERGE_SMART;
231    const char *s;
232
233    if (!(re = calloc(1, sizeof(Render_Engine)))) return NULL;
234
235    evas_software_xlib_x_init();
236    evas_software_xlib_x_color_init();
237    evas_software_xlib_outbuf_init();
238
239    ob =
240      evas_software_xlib_outbuf_setup_x(w, h, rot, OUTBUF_DEPTH_INHERIT, disp,
241                                        draw, vis, cmap, depth, grayscale,
242                                        max_colors, mask, shape_dither,
243                                        destination_alpha);
244    if (!ob) goto on_error;
245
246    /* for updates return 1 big buffer, but only use portions of it, also cache
247     * it and keepit around until an idle_flush */
248
249    /* disable for now - i am hunting down why some expedite tests are slower,
250     * as well as shaped stuff is broken and probable non-32bpp is broken as
251     * convert funcs dont do the right thing
252     *
253     */
254 //   re->ob->onebuf = 1;
255
256    evas_software_xlib_outbuf_debug_set(ob, debug);
257    if (!evas_render_engine_software_generic_init(&re->generic, ob, NULL,
258                                                  evas_software_xlib_outbuf_get_rot,
259                                                  evas_software_xlib_outbuf_reconfigure,
260                                                  NULL,
261                                                  NULL,
262                                                  evas_software_xlib_outbuf_new_region_for_update,
263                                                  evas_software_xlib_outbuf_push_updated_region,
264                                                  evas_software_xlib_outbuf_free_region_for_update,
265                                                  evas_software_xlib_outbuf_idle_flush,
266                                                  evas_software_xlib_outbuf_flush,
267                                                  NULL,
268                                                  evas_software_xlib_outbuf_free,
269                                                  w, h))
270      goto on_error;
271
272    if ((s = getenv("EVAS_SOFTWARE_PARTIAL_MERGE")))
273      {
274         if ((!strcmp(s, "bounding")) ||
275             (!strcmp(s, "b")))
276           merge_mode = MERGE_BOUNDING;
277         else if ((!strcmp(s, "full")) ||
278                  (!strcmp(s, "f")))
279           merge_mode = MERGE_FULL;
280         else if ((!strcmp(s, "smart")) ||
281                  (!strcmp(s, "s")))
282           merge_mode = MERGE_SMART;
283      }
284
285    evas_render_engine_software_generic_merge_mode_set(&re->generic, merge_mode);
286
287    return re;
288
289  on_error:
290    if (ob) evas_software_xlib_outbuf_free(ob);
291    free(re);
292    return NULL;
293 }
294
295 static void *
296 _output_swapbuf_setup(int w, int h, int rot, Display *disp, Drawable draw,
297                       Visual *vis, Colormap cmap, int depth,
298                       int debug EINA_UNUSED,
299                       int grayscale, int max_colors, Pixmap mask,
300                       int shape_dither, int destination_alpha)
301 {
302    Render_Engine *re;
303    Outbuf *ob;
304
305    if (!(re = calloc(1, sizeof(Render_Engine)))) return NULL;
306
307    evas_software_xlib_x_init();
308    evas_software_xlib_x_color_init();
309    evas_software_xlib_swapbuf_init();
310
311    ob =
312      evas_software_xlib_swapbuf_setup_x(w, h, rot, OUTBUF_DEPTH_INHERIT, disp,
313                                         draw, vis, cmap, depth, grayscale,
314                                         max_colors, mask, shape_dither,
315                                         destination_alpha);
316    if (!ob) goto on_error;
317
318    if (!evas_render_engine_software_generic_init(&re->generic, ob,
319                                                  evas_software_xlib_swapbuf_buffer_state_get,
320                                                  evas_software_xlib_swapbuf_get_rot,
321                                                  evas_software_xlib_swapbuf_reconfigure,
322                                                  NULL,
323                                                  NULL,
324                                                  evas_software_xlib_swapbuf_new_region_for_update,
325                                                  evas_software_xlib_swapbuf_push_updated_region,
326                                                  evas_software_xlib_swapbuf_free_region_for_update,
327                                                  evas_software_xlib_swapbuf_idle_flush,
328                                                  evas_software_xlib_swapbuf_flush,
329                                                  NULL,
330                                                  evas_software_xlib_swapbuf_free,
331                                                  w, h))
332      goto on_error;
333    return re;
334
335  on_error:
336    if (ob) evas_software_xlib_swapbuf_free(ob);
337    free(re);
338    return NULL;
339 }
340 #endif
341
342 #ifdef BUILD_ENGINE_SOFTWARE_XCB
343 static void *
344 _output_xcb_setup(int w, int h, int rot, xcb_connection_t *conn, 
345                   xcb_screen_t *screen, xcb_drawable_t draw, 
346                   xcb_visualtype_t *vis, xcb_colormap_t cmap, int depth,
347                   int debug, int grayscale, int max_colors, xcb_drawable_t mask,
348                   int shape_dither, int destination_alpha)
349 {
350    Render_Engine *re;
351    Outbuf *ob;
352
353    if (!(re = calloc(1, sizeof(Render_Engine)))) return NULL;
354
355    evas_software_xcb_init();
356    evas_software_xcb_color_init();
357    evas_software_xcb_outbuf_init();
358
359    ob = 
360      evas_software_xcb_outbuf_setup(w, h, rot, OUTBUF_DEPTH_INHERIT, conn,
361                                     screen, draw, vis, cmap, depth,
362                                     grayscale, max_colors, mask,
363                                     shape_dither, destination_alpha);
364    if (!ob) goto on_error;
365
366    /* for updates return 1 big buffer, but only use portions of it, also cache
367     * it and keepit around until an idle_flush */
368
369    /* disable for now - i am hunting down why some expedite tests are slower,
370     * as well as shaped stuff is broken and probable non-32bpp is broken as
371     * convert funcs dont do the right thing
372     *
373     */
374 //   re->ob->onebuf = 1;
375
376    evas_software_xcb_outbuf_debug_set(ob, debug);
377
378    if (!evas_render_engine_software_generic_init(&re->generic, ob, NULL,
379                                                  evas_software_xcb_outbuf_rotation_get,
380                                                  evas_software_xcb_outbuf_reconfigure,
381                                                  NULL,
382                                                  NULL,
383                                                  evas_software_xcb_outbuf_new_region_for_update,
384                                                  evas_software_xcb_outbuf_push_updated_region,
385                                                  evas_software_xcb_outbuf_free_region_for_update,
386                                                  evas_software_xcb_outbuf_idle_flush,
387                                                  evas_software_xcb_outbuf_flush,
388                                                  NULL,
389                                                  evas_software_xcb_outbuf_free,
390                                                  w, h))
391      goto on_error;
392    return re;
393
394  on_error:
395    if (ob) evas_software_xcb_outbuf_free(ob);
396    free(re);
397    return NULL;
398 }
399 #endif
400
401 static void *
402 _best_visual_get(int backend, void *connection, int screen)
403 {
404    if (!connection) return NULL;
405
406 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
407    if (backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB)
408      return DefaultVisual((Display *)connection, screen);
409 #endif
410
411 #ifdef BUILD_ENGINE_SOFTWARE_XCB
412    if (backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XCB)
413      {
414         xcb_screen_iterator_t iter_screen;
415         xcb_depth_iterator_t iter_depth;
416         xcb_screen_t *s = NULL;
417
418         iter_screen = 
419           xcb_setup_roots_iterator(xcb_get_setup((xcb_connection_t *)connection));
420         for (; iter_screen.rem; --screen, xcb_screen_next(&iter_screen))
421           if (screen == 0)
422             {
423                s = iter_screen.data;
424                break;
425             }
426
427         iter_depth = xcb_screen_allowed_depths_iterator(s);
428         for (; iter_depth.rem; xcb_depth_next(&iter_depth))
429           {
430              xcb_visualtype_iterator_t iter_vis;
431
432              iter_vis = xcb_depth_visuals_iterator(iter_depth.data);
433              for (; iter_vis.rem; xcb_visualtype_next(&iter_vis))
434                {
435                   if (s->root_visual == iter_vis.data->visual_id)
436                     return iter_vis.data;
437                }
438           }
439      }
440 #endif
441
442    return NULL;
443 }
444
445 static unsigned int
446 _best_colormap_get(int backend, void *connection, int screen)
447 {
448    if (!connection) return 0;
449
450 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
451    if (backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB)
452      return DefaultColormap((Display *)connection, screen);
453 #endif
454
455 #ifdef BUILD_ENGINE_SOFTWARE_XCB
456    if (backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XCB)
457      {
458         xcb_screen_iterator_t iter_screen;
459         xcb_screen_t *s = NULL;
460
461         iter_screen = 
462           xcb_setup_roots_iterator(xcb_get_setup((xcb_connection_t *)connection));
463         for (; iter_screen.rem; --screen, xcb_screen_next(&iter_screen))
464           if (screen == 0)
465             {
466                s = iter_screen.data;
467                break;
468             }
469
470         if (s) return s->default_colormap;
471      }
472 #endif
473
474    return 0;
475 }
476
477 static int
478 _best_depth_get(int backend, void *connection, int screen)
479 {
480    if (!connection) return 0;
481
482 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
483    if (backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB)
484      return DefaultDepth((Display *)connection, screen);
485 #endif
486
487 #ifdef BUILD_ENGINE_SOFTWARE_XCB
488    if (backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XCB)
489      {
490         xcb_screen_iterator_t iter_screen;
491         xcb_screen_t *s = NULL;
492
493         iter_screen = 
494           xcb_setup_roots_iterator(xcb_get_setup((xcb_connection_t *)connection));
495         for (; iter_screen.rem; --screen, xcb_screen_next(&iter_screen))
496           if (screen == 0)
497             {
498                s = iter_screen.data;
499                break;
500             }
501
502         return s->root_depth;
503      }
504 #endif
505
506    return 0;
507 }
508
509 static void
510 _symbols(void)
511 {
512    static int done = 0;
513
514    if (done) return;
515
516 #define LINK2GENERIC(sym) \
517    glsym_##sym = dlsym(RTLD_DEFAULT, #sym);
518
519    // Get function pointer to native_common that is now provided through the link of SW_Generic.
520    LINK2GENERIC(_evas_native_tbm_surface_image_set);
521    LINK2GENERIC(_evas_native_tbm_surface_stride_get);
522
523    done = 1;
524 }
525
526 /* engine api this module provides */
527 static void *
528 eng_info(Evas *eo_e EINA_UNUSED)
529 {
530    Evas_Engine_Info_Software_X11 *info;
531
532    if (!(info = calloc(1, sizeof(Evas_Engine_Info_Software_X11))))
533      return NULL;
534
535    info->magic.magic = rand();
536    info->info.debug = 0;
537    info->info.alloc_grayscale = 0;
538    info->info.alloc_colors_max = 216;
539    info->func.best_visual_get = _best_visual_get;
540    info->func.best_colormap_get = _best_colormap_get;
541    info->func.best_depth_get = _best_depth_get;
542    info->render_mode = EVAS_RENDER_MODE_BLOCKING;
543    return info;
544 }
545
546 static void
547 eng_info_free(Evas *eo_e EINA_UNUSED, void *info)
548 {
549    Evas_Engine_Info_Software_X11 *in;
550
551    in = (Evas_Engine_Info_Software_X11 *)info;
552    free(in);
553 }
554
555 static int
556 eng_setup(Evas *eo_e, void *in)
557 {
558    Evas_Public_Data *e = efl_data_scope_get(eo_e, EVAS_CANVAS_CLASS);
559    Evas_Engine_Info_Software_X11 *info;
560    Render_Engine *re = NULL;
561
562    info = (Evas_Engine_Info_Software_X11 *)in;
563    if (!e->engine.data.output)
564      {
565         /* if we haven't initialized - init (automatic abort if already done) */
566         evas_common_init();
567
568 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
569         if (info->info.backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB)
570           {
571              static int try_swapbuf = -1;
572              char* s;
573
574              if (try_swapbuf == -1)
575                {
576                   if ((s = getenv("EVAS_NO_DRI_SWAPBUF")) != NULL)
577                     {
578                        if (atoi(s) == 1) try_swapbuf = 0;
579                        else try_swapbuf = 1;
580                     }
581                   else try_swapbuf = 1;
582                }
583              if (try_swapbuf)
584                re = _output_swapbuf_setup(e->output.w, e->output.h,
585                                           info->info.rotation, info->info.connection,
586                                           info->info.drawable, info->info.visual,
587                                           info->info.colormap,
588                                           info->info.depth, info->info.debug,
589                                           info->info.alloc_grayscale,
590                                           info->info.alloc_colors_max,
591                                           info->info.mask, info->info.shape_dither,
592                                           info->info.destination_alpha);
593              if (re) re->outbuf_alpha_get = evas_software_xlib_swapbuf_alpha_get;
594              else
595                {
596                   re = _output_xlib_setup(e->output.w, e->output.h,
597                                           info->info.rotation, info->info.connection,
598                                           info->info.drawable, info->info.visual,
599                                           info->info.colormap,
600                                           info->info.depth, info->info.debug,
601                                           info->info.alloc_grayscale,
602                                           info->info.alloc_colors_max,
603                                           info->info.mask, info->info.shape_dither,
604                                           info->info.destination_alpha);
605                   re->outbuf_alpha_get = evas_software_xlib_outbuf_alpha_get;
606                }
607              re->generic.ob->region_push_hook.cb = info->func.region_push_hook;
608              re->generic.ob->region_push_hook.evas = eo_e;
609           }
610 #endif
611
612 #ifdef BUILD_ENGINE_SOFTWARE_XCB
613         if (info->info.backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XCB)
614           {
615              re = _output_xcb_setup(e->output.w, e->output.h,
616                                     info->info.rotation, info->info.connection,
617                                     info->info.screen, info->info.drawable,
618                                     info->info.visual, info->info.colormap,
619                                     info->info.depth, info->info.debug,
620                                     info->info.alloc_grayscale,
621                                     info->info.alloc_colors_max,
622                                     info->info.mask, info->info.shape_dither,
623                                     info->info.destination_alpha);
624              re->outbuf_alpha_get = evas_software_xcb_outbuf_alpha_get;
625              re->generic.ob->region_push_hook.cb = info->func.region_push_hook;
626              re->generic.ob->region_push_hook.evas = eo_e;
627           }
628 #endif
629
630         e->engine.data.output = re;
631      }
632    else
633      {
634         Outbuf *ob = NULL;
635         /* int ponebuf = 0; */
636
637         re = e->engine.data.output;
638         /* if ((re) && (re->ob)) ponebuf = re->ob->onebuf; */
639
640         _outbufs = eina_list_remove(_outbufs, re->generic.ob);
641
642 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
643         if (info->info.backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB)
644           {
645              if (re->generic.outbuf_free == evas_software_xlib_swapbuf_free)
646                {
647                   ob =
648                     evas_software_xlib_swapbuf_setup_x(e->output.w, e->output.h,
649                                                        info->info.rotation,
650                                                        OUTBUF_DEPTH_INHERIT,
651                                                        info->info.connection,
652                                                        info->info.drawable,
653                                                        info->info.visual,
654                                                        info->info.colormap,
655                                                        info->info.depth,
656                                                        info->info.alloc_grayscale,
657                                                        info->info.alloc_colors_max,
658                                                        info->info.mask,
659                                                        info->info.shape_dither,
660                                                        info->info.destination_alpha);
661                }
662              else
663                {
664                   ob =
665                     evas_software_xlib_outbuf_setup_x(e->output.w, e->output.h,
666                                                       info->info.rotation,
667                                                       OUTBUF_DEPTH_INHERIT,
668                                                       info->info.connection,
669                                                       info->info.drawable,
670                                                       info->info.visual,
671                                                       info->info.colormap,
672                                                       info->info.depth,
673                                                       info->info.alloc_grayscale,
674                                                       info->info.alloc_colors_max,
675                                                       info->info.mask,
676                                                       info->info.shape_dither,
677                                                       info->info.destination_alpha);
678                   evas_software_xlib_outbuf_debug_set(ob, info->info.debug);
679                }
680           }
681 #endif
682
683 #ifdef BUILD_ENGINE_SOFTWARE_XCB
684         if (info->info.backend == EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XCB)
685           {
686              ob =
687                evas_software_xcb_outbuf_setup(e->output.w, e->output.h,
688                                               info->info.rotation,
689                                               OUTBUF_DEPTH_INHERIT,
690                                               info->info.connection,
691                                               info->info.screen,
692                                               info->info.drawable,
693                                               info->info.visual,
694                                               info->info.colormap,
695                                               info->info.depth,
696                                               info->info.alloc_grayscale,
697                                               info->info.alloc_colors_max,
698                                               info->info.mask,
699                                               info->info.shape_dither,
700                                               info->info.destination_alpha);
701              evas_software_xcb_outbuf_debug_set(ob, info->info.debug);
702           }
703 #endif
704
705         if (ob)
706           {
707              evas_render_engine_software_generic_update(&re->generic, ob, e->output.w, e->output.h);
708              ob->region_push_hook.cb = info->func.region_push_hook;
709              ob->region_push_hook.evas = eo_e;
710           }
711
712         /* if ((re) && (re->ob)) re->ob->onebuf = ponebuf; */
713      }
714    if (!e->engine.data.output) return 0;
715    if (!e->engine.data.context)
716      {
717         e->engine.data.context =
718           e->engine.func->context_new(e->engine.data.output);
719      }
720
721    re = e->engine.data.output;
722    _outbufs =  eina_list_append(_outbufs, re->generic.ob);
723
724    return 1;
725 }
726
727 static void
728 eng_output_free(void *data)
729 {
730    Render_Engine *re;
731
732    if ((re = (Render_Engine *)data))
733      {
734         _outbufs = eina_list_remove(_outbufs, re->generic.ob);
735         evas_render_engine_software_generic_clean(&re->generic);
736 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
737         _output_egl_shutdown(re);
738 #endif
739         free(re);
740      }
741
742    evas_common_shutdown();
743 }
744
745 static Eina_Bool
746 eng_canvas_alpha_get(void *data, void *context EINA_UNUSED)
747 {
748    Render_Engine *re;
749
750    re = (Render_Engine *)data;
751    return (re->generic.ob->priv.destination_alpha) ||
752      (re->outbuf_alpha_get(re->generic.ob));
753 }
754
755 static void
756 _native_evasgl_free(void *image)
757 {
758    RGBA_Image *im = image;
759    Native *n = im->native.data;
760
761    im->native.data        = NULL;
762    im->native.func.bind   = NULL;
763    im->native.func.unbind = NULL;
764    im->native.func.free   = NULL;
765    //im->image.data         = NULL;
766    free(n);
767 }
768
769 static int
770 eng_image_native_init(void *data EINA_UNUSED, Evas_Native_Surface_Type type)
771 {
772    switch (type)
773      {
774 #ifdef GL_GLES
775       case EVAS_NATIVE_SURFACE_TBM:
776         return _evas_native_tbm_init();
777 #endif
778       case EVAS_NATIVE_SURFACE_X11:
779       case EVAS_NATIVE_SURFACE_EVASGL:
780         return 1;
781       default:
782         ERR("Native surface type %d not supported!", type);
783         return 0;
784      }
785 }
786
787 static void
788 eng_image_native_shutdown(void *data EINA_UNUSED, Evas_Native_Surface_Type type)
789 {
790    switch (type)
791      {
792 #ifdef GL_GLES
793       case EVAS_NATIVE_SURFACE_TBM:
794         _evas_native_tbm_shutdown();
795         return;
796 #endif
797       case EVAS_NATIVE_SURFACE_X11:
798       case EVAS_NATIVE_SURFACE_OPENGL:
799         return;
800       default:
801         ERR("Native surface type %d not supported!", type);
802         return;
803      }
804 }
805
806 static void *
807 eng_image_native_set(void *data EINA_UNUSED, void *image, void *native)
808 {
809    Render_Engine *re = (Render_Engine *)data;
810    Evas_Native_Surface *ns = native;
811    Image_Entry *ie = image, *ie2 = NULL;
812    RGBA_Image *im = image;
813    int stride;
814
815    if (!im) return NULL;
816    if (!ns)
817      {
818         if (im->native.data && im->native.func.free)
819           im->native.func.free(im);
820         return NULL;
821      }
822
823    if (ns->type == EVAS_NATIVE_SURFACE_X11)
824      {
825         if (im->native.data)
826           {
827              //image have native surface already
828              Evas_Native_Surface *ens = im->native.data;
829
830              if ((ens->type == ns->type) &&
831                  (ens->data.x11.visual == ns->data.x11.visual) &&
832                  (ens->data.x11.pixmap == ns->data.x11.pixmap))
833                return im;
834           }
835      }
836    else if (ns->type == EVAS_NATIVE_SURFACE_TBM)
837      {
838         if (im->native.data)
839           {
840              //image have native surface already
841              Evas_Native_Surface *ens = im->native.data;
842
843              if ((ens->type == ns->type) &&
844                  (ens->data.tbm.buffer == ns->data.tbm.buffer))
845                return im;
846           }
847      }
848
849    // Code from software_generic
850    if ((ns->type == EVAS_NATIVE_SURFACE_EVASGL) &&
851        (ns->version == EVAS_NATIVE_SURFACE_VERSION))
852      ie2 = evas_cache_image_data(evas_common_image_cache_get(),
853                                  ie->w, ie->h, ns->data.evasgl.surface, 1,
854                                  EVAS_COLORSPACE_ARGB8888);
855    else if (ns->type == EVAS_NATIVE_SURFACE_TBM)
856      {
857         stride = glsym__evas_native_tbm_surface_stride_get(re->generic.ob, ns);
858         ie2 = evas_cache_image_copied_data(evas_common_image_cache_get(),
859                                    stride, ie->h, NULL, ie->flags.alpha,
860                                    EVAS_COLORSPACE_ARGB8888);
861      }
862    else
863      ie2 = evas_cache_image_data(evas_common_image_cache_get(),
864                                  ie->w, ie->h, NULL, ie->flags.alpha,
865                                  EVAS_COLORSPACE_ARGB8888);
866
867    if (im->native.data)
868      {
869         if (im->native.func.free)
870           im->native.func.free(im);
871      }
872
873 #ifdef EVAS_CSERVE2
874    if (evas_cserve2_use_get() && evas_cache2_image_cached(ie))
875      evas_cache2_image_close(ie);
876    else
877 #endif
878      evas_cache_image_drop(ie);
879    ie = ie2;
880
881    if (ns->type == EVAS_NATIVE_SURFACE_X11)
882      {
883 #ifdef BUILD_ENGINE_SOFTWARE_XLIB
884         RGBA_Image *ret_im = NULL;
885         ret_im = evas_xlib_image_dri_native_set(re->generic.ob, ie, ns);
886         if (!ret_im) 
887            ret_im = evas_xlib_image_native_set(re->generic.ob, ie, ns);
888         return ret_im;
889 #endif
890 #ifdef BUILD_ENGINE_SOFTWARE_XCB
891         return evas_xcb_image_native_set(re->generic.ob, ie, ns);
892 #endif
893      }
894    else if (ns->type == EVAS_NATIVE_SURFACE_TBM)
895      {
896         return glsym__evas_native_tbm_surface_image_set(re->generic.ob, ie, ns);
897      }
898    else if (ns->type == EVAS_NATIVE_SURFACE_EVASGL)
899      {
900         /* Native contains Evas_Native_Surface. What a mess. */
901         Native *n = calloc(1, sizeof(Native));
902         if (n)
903           {
904              n->ns_data.evasgl.surface = ns->data.evasgl.surface;
905              im = (RGBA_Image *) ie;
906              n->ns.type = EVAS_NATIVE_SURFACE_EVASGL;
907              n->ns.version = EVAS_NATIVE_SURFACE_VERSION;
908              n->ns.data.evasgl.surface = ns->data.evasgl.surface;
909              im->native.data = n;
910              im->native.func.free = _native_evasgl_free;
911              im->native.func.bind = NULL;
912              im->native.func.unbind = NULL;
913           }
914      }
915
916    return ie;
917 }
918
919 static void *
920 eng_image_native_get(void *data EINA_UNUSED, void *image)
921 {
922    RGBA_Image *im = image;
923    Native *n;
924    if (!im) return NULL;
925    n = im->native.data;
926    if (!n) return NULL;
927    return &(n->ns);
928 }
929
930
931 /* module advertising code */
932 static int
933 module_open(Evas_Module *em)
934 {
935    if (!em) return 0;
936
937    /* get whatever engine module we inherit from */
938    if (!_evas_module_engine_inherit(&pfunc, "software_generic")) return 0;
939
940    _evas_engine_soft_x11_log_dom = 
941      eina_log_domain_register("evas-software_x11", EVAS_DEFAULT_LOG_COLOR);
942
943    if (_evas_engine_soft_x11_log_dom < 0)
944      {
945         EINA_LOG_ERR("Can not create a module log domain.");
946         return 0;
947      }
948
949    /* store it for later use */
950    func = pfunc;
951
952    /* now to override methods */
953 #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
954    ORD(info);
955    ORD(info_free);
956    ORD(setup);
957    ORD(canvas_alpha_get);
958    ORD(output_free);
959    ORD(image_native_init);
960    ORD(image_native_shutdown);
961    ORD(image_native_set);
962    ORD(image_native_get);
963
964    _symbols();
965    /* now advertise out own api */
966    em->functions = (void *)(&func);
967    return 1;
968 }
969
970 static void
971 module_close(Evas_Module *em EINA_UNUSED)
972 {
973   eina_log_domain_unregister(_evas_engine_soft_x11_log_dom);
974 }
975
976 static Evas_Module_Api evas_modapi =
977 {
978    EVAS_MODULE_API_VERSION, "software_x11", "none",
979    {
980      module_open,
981      module_close
982    }
983 };
984
985 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_ENGINE, engine, software_x11);
986
987 #ifndef EVAS_STATIC_BUILD_SOFTWARE_X11
988 EVAS_EINA_MODULE_DEFINE(engine, software_x11);
989 #endif