move around - flatter.
[profile/ivi/evas.git] / src / modules / engines / software_xcb / evas_engine.c
1 #include <xcb/xcb.h>
2 #include "evas_common.h"
3 #include "evas_private.h"
4 #include "evas_engine.h"
5 #include "Evas_Engine_Software_Xcb.h"
6
7 /* function tables - filled in later (func and parent func) */
8 static Evas_Func func, pfunc;
9
10 /* engine struct data */
11 typedef struct _Render_Engine Render_Engine;
12
13 struct _Render_Engine
14 {
15    Tilebuf          *tb;
16    Outbuf           *ob;
17    Tilebuf_Rect     *rects;
18    Evas_Object_List *cur_rect;
19    int               end : 1;
20 };
21
22 /* prototypes we will use here */
23 static void *_output_setup(int w, int h, int rot, xcb_connection_t *conn, xcb_screen_t     *screen, xcb_drawable_t draw, xcb_visualtype_t *vis, xcb_colormap_t cmap, int depth, int debug, int grayscale, int max_colors, xcb_drawable_t mask, int shape_dither, int destination_alpha);
24 static xcb_visualtype_t *_best_visual_get(xcb_screen_t *screen);
25 static xcb_colormap_t _best_colormap_get(xcb_screen_t *screen);
26 static int _best_depth_get(xcb_screen_t *screen);
27
28 static void *eng_info(Evas *e);
29 static void eng_info_free(Evas *e, void *info);
30 static void eng_setup(Evas *e, void *info);
31 static void eng_output_free(void *data);
32 static void eng_output_resize(void *data, int w, int h);
33 static void eng_output_tile_size_set(void *data, int w, int h);
34 static void eng_output_redraws_rect_add(void *data, int x, int y, int w, int h);
35 static void eng_output_redraws_rect_del(void *data, int x, int y, int w, int h);
36 static void eng_output_redraws_clear(void *data);
37 static void *eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, int *cx, int *cy, int *cw, int *ch);
38 static void eng_output_redraws_next_update_push(void *data, void *surface, int x, int y, int w, int h);
39 static void eng_output_flush(void *data);
40 static void eng_output_idle_flush(void *data);
41
42 static void *
43 _output_setup(int               w,
44               int               h,
45               int               rot,
46               xcb_connection_t *conn,
47               xcb_screen_t     *screen,
48               xcb_drawable_t    draw,
49               xcb_visualtype_t *vis,
50               xcb_colormap_t    cmap,
51               int               depth,
52               int               debug,
53               int               grayscale,
54               int               max_colors,
55               xcb_drawable_t    mask,
56               int               shape_dither,
57               int               destination_alpha)
58 {
59    Render_Engine *re;
60
61    re = calloc(1, sizeof(Render_Engine));
62    /* if we haven't initialized - init (automatic abort if already done) */
63    evas_common_cpu_init();
64
65    evas_common_blend_init();
66    evas_common_image_init();
67    evas_common_convert_init();
68    evas_common_scale_init();
69    evas_common_rectangle_init();
70    evas_common_gradient_init();
71    evas_common_polygon_init();
72    evas_common_line_init();
73    evas_common_font_init();
74    evas_common_draw_init();
75    evas_common_tilebuf_init();
76
77    evas_software_xcb_x_init();
78    evas_software_xcb_x_color_init();
79    evas_software_xcb_outbuf_init();
80
81    re->ob = evas_software_xcb_outbuf_setup_x(w, h, rot,
82                                              OUTBUF_DEPTH_INHERIT,
83                                              conn,
84                                              screen,
85                                              draw,
86                                              vis,
87                                              cmap,
88                                              depth,
89                                              grayscale,
90                                              max_colors,
91                                              mask,
92                                              shape_dither,
93                                              destination_alpha);
94    if (!re->ob)
95      {
96         free(re);
97         return NULL;
98      }
99
100    /* for updates return 1 big buffer, but only use portions of it, also cache
101     it and keepit around until an idle_flush */
102    /* disable for now - i am hunting down why some expedite tests are slower,
103     * as well as shaped stuff is broken and probable non-32bpp is broken as
104     * convert funcs dont do the right thing
105     *
106    re->ob->onebuf = 1;
107     */
108
109    evas_software_xcb_outbuf_debug_set(re->ob, debug);
110    re->tb = evas_common_tilebuf_new(w, h);
111    if (!re->tb)
112      {
113         evas_software_xcb_outbuf_free(re->ob);
114         free(re);
115         return NULL;
116      }
117    /* in preliminary tests 16x16 gave highest framerates */
118    evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
119    return re;
120 }
121
122 static xcb_visualtype_t *
123 _best_visual_get(xcb_screen_t *screen)
124 {
125    xcb_depth_iterator_t  iter_depth;
126
127    if (!screen) return NULL;
128
129    iter_depth = xcb_screen_allowed_depths_iterator(screen);
130    for (; iter_depth.rem; xcb_depth_next (&iter_depth))
131      {
132         xcb_visualtype_iterator_t iter_vis;
133
134         iter_vis = xcb_depth_visuals_iterator(iter_depth.data);
135         for (; iter_vis.rem; xcb_visualtype_next (&iter_vis))
136           {
137             if (screen->root_visual == iter_vis.data->visual_id)
138               return iter_vis.data;
139           }
140      }
141
142    return NULL;
143 }
144
145 static xcb_colormap_t
146 _best_colormap_get(xcb_screen_t *screen)
147 {
148    if (!screen)
149      return 0;
150
151    return screen->default_colormap;
152 }
153
154 static int
155 _best_depth_get(xcb_screen_t *screen)
156 {
157    if (!screen)
158      return 0;
159
160    return screen->root_depth;
161 }
162
163 /* engine api this module provides */
164 static void *
165 eng_info(Evas *e)
166 {
167    Evas_Engine_Info_Software_Xcb *info;
168
169    info = calloc(1, sizeof(Evas_Engine_Info_Software_Xcb));
170    if (!info) return NULL;
171    info->magic.magic = rand();
172    info->info.debug = 0;
173    info->info.alloc_grayscale = 0;
174    info->info.alloc_colors_max = 216;
175    info->func.best_visual_get = _best_visual_get;
176    info->func.best_colormap_get = _best_colormap_get;
177    info->func.best_depth_get = _best_depth_get;
178    return info;
179    e = NULL;
180 }
181
182 static void
183 eng_info_free(Evas *e, void *info)
184 {
185    Evas_Engine_Info_Software_Xcb *in;
186
187    in = (Evas_Engine_Info_Software_Xcb *)info;
188    free(in);
189 }
190
191 static void
192 eng_setup(Evas *e, void *in)
193 {
194    Render_Engine *re;
195    Evas_Engine_Info_Software_Xcb *info;
196
197    info = (Evas_Engine_Info_Software_Xcb *)in;
198    if (!e->engine.data.output)
199      e->engine.data.output =
200      _output_setup(e->output.w,
201                    e->output.h,
202                    info->info.rotation,
203                    info->info.conn,
204                    info->info.screen,
205                    info->info.drawable,
206                    info->info.visual,
207                    info->info.colormap,
208                    info->info.depth,
209                    info->info.debug,
210                    info->info.alloc_grayscale,
211                    info->info.alloc_colors_max,
212                    info->info.mask,
213                    info->info.shape_dither,
214                    info->info.destination_alpha);
215    else
216      {
217         int ponebuf = 0;
218
219         re = e->engine.data.output;
220         ponebuf = re->ob->onebuf;
221         evas_software_xcb_outbuf_free(re->ob);
222         re->ob = evas_software_xcb_outbuf_setup_x(e->output.w,
223                                                   e->output.h,
224                                                   info->info.rotation,
225                                                   OUTBUF_DEPTH_INHERIT,
226                                                   info->info.conn,
227                                                   info->info.screen,
228                                                   info->info.drawable,
229                                                   info->info.visual,
230                                                   info->info.colormap,
231                                                   info->info.depth,
232                                                   info->info.alloc_grayscale,
233                                                   info->info.alloc_colors_max,
234                                                   info->info.mask,
235                                                   info->info.shape_dither,
236                                                   info->info.destination_alpha);
237         evas_software_xcb_outbuf_debug_set(re->ob, info->info.debug);
238         re->ob->onebuf = ponebuf;
239      }
240
241    if (!e->engine.data.output) return;
242    if (!e->engine.data.context)
243      e->engine.data.context =
244      e->engine.func->context_new(e->engine.data.output);
245
246    re = e->engine.data.output;
247    evas_software_xcb_outbuf_drawable_set(re->ob, info->info.drawable);
248    evas_software_xcb_outbuf_mask_set(re->ob, info->info.mask);
249    evas_software_xcb_outbuf_rotation_set(re->ob, info->info.rotation);
250 }
251
252 static void
253 eng_output_free(void *data)
254 {
255    Render_Engine *re;
256
257    if (!data) return;
258
259    re = (Render_Engine *)data;
260    evas_software_xcb_outbuf_free(re->ob);
261    evas_common_tilebuf_free(re->tb);
262    if (re->rects) evas_common_tilebuf_free_render_rects(re->rects);
263    free(re);
264
265    evas_common_font_shutdown();
266    evas_common_image_shutdown();
267 }
268
269 static void
270 eng_output_resize(void *data, int w, int h)
271 {
272    Render_Engine *re;
273
274    re = (Render_Engine *)data;
275    evas_software_xcb_outbuf_reconfigure(re->ob, w, h,
276                                         evas_software_xcb_outbuf_get_rot(re->ob),
277                                         OUTBUF_DEPTH_INHERIT);
278    evas_common_tilebuf_free(re->tb);
279    re->tb = evas_common_tilebuf_new(w, h);
280    if (re->tb)
281      evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
282 }
283
284 static void
285 eng_output_tile_size_set(void *data, int w, int h)
286 {
287    Render_Engine *re;
288
289    re = (Render_Engine *)data;
290    evas_common_tilebuf_set_tile_size(re->tb, w, h);
291 }
292
293 static void
294 eng_output_redraws_rect_add(void *data, int x, int y, int w, int h)
295 {
296    Render_Engine *re;
297
298    re = (Render_Engine *)data;
299    evas_common_tilebuf_add_redraw(re->tb, x, y, w, h);
300 }
301
302 static void
303 eng_output_redraws_rect_del(void *data, int x, int y, int w, int h)
304 {
305    Render_Engine *re;
306
307    re = (Render_Engine *)data;
308    evas_common_tilebuf_del_redraw(re->tb, x, y, w, h);
309 }
310
311 static void
312 eng_output_redraws_clear(void *data)
313 {
314    Render_Engine *re;
315
316    re = (Render_Engine *)data;
317    evas_common_tilebuf_clear(re->tb);
318 }
319
320 static void *
321 eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, int *cx, int *cy, int *cw, int *ch)
322 {
323    Render_Engine *re;
324    RGBA_Image *surface;
325    Tilebuf_Rect *rect;
326    int ux, uy, uw, uh;
327
328    re = (Render_Engine *)data;
329    if (re->end)
330      {
331         re->end = 0;
332         return NULL;
333      }
334    if (!re->rects)
335      {
336         re->rects = evas_common_tilebuf_get_render_rects(re->tb);
337         re->cur_rect = (Evas_Object_List *)re->rects;
338      }
339    if (!re->cur_rect) return NULL;
340    rect = (Tilebuf_Rect *)re->cur_rect;
341    ux = rect->x; uy = rect->y; uw = rect->w; uh = rect->h;
342    re->cur_rect = re->cur_rect->next;
343    if (!re->cur_rect)
344      {
345         evas_common_tilebuf_free_render_rects(re->rects);
346         re->rects = NULL;
347         re->end = 1;
348      }
349
350    surface = evas_software_xcb_outbuf_new_region_for_update(re->ob,
351                                                             ux, uy, uw, uh,
352                                                             cx, cy, cw, ch);
353    *x = ux; *y = uy; *w = uw; *h = uh;
354    return surface;
355 }
356
357 static void
358 eng_output_redraws_next_update_push(void *data, void *surface, int x, int y, int w, int h)
359 {
360    Render_Engine *re;
361
362    re = (Render_Engine *)data;
363    evas_common_pipe_begin(surface);
364    evas_common_pipe_flush(surface);
365    evas_software_xcb_outbuf_push_updated_region(re->ob, surface, x, y, w, h);
366    evas_software_xcb_outbuf_free_region_for_update(re->ob, surface);
367    evas_common_cpu_end_opt();
368 }
369
370 static void
371 eng_output_flush(void *data)
372 {
373    Render_Engine *re;
374
375    re = (Render_Engine *)data;
376    evas_software_xcb_outbuf_flush(re->ob);
377 }
378
379 static void
380 eng_output_idle_flush(void *data)
381 {
382    Render_Engine *re;
383
384    re = (Render_Engine *)data;
385    evas_software_xcb_outbuf_idle_flush(re->ob);
386 }
387
388 /* module advertising code */
389 EAPI int
390 module_open(Evas_Module *em)
391 {
392    if (!em) return 0;
393    /* get whatever engine module we inherit from */
394    if (!_evas_module_engine_inherit(&pfunc, "software_generic")) return 0;
395    /* store it for later use */
396    func = pfunc;
397    /* now to override methods */
398 #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
399    ORD(info);
400    ORD(info_free);
401    ORD(setup);
402    ORD(output_free);
403    ORD(output_resize);
404    ORD(output_tile_size_set);
405    ORD(output_redraws_rect_add);
406    ORD(output_redraws_rect_del);
407    ORD(output_redraws_clear);
408    ORD(output_redraws_next_update_get);
409    ORD(output_redraws_next_update_push);
410    ORD(output_flush);
411    ORD(output_idle_flush);
412    /* now advertise out own api */
413    em->functions = (void *)(&func);
414    return 1;
415 }
416
417 EAPI void
418 module_close(void)
419 {
420 }
421
422 EAPI Evas_Module_Api evas_modapi =
423 {
424    EVAS_MODULE_API_VERSION,
425      EVAS_MODULE_TYPE_ENGINE,
426      "software_xcb",
427      "none"
428 };