move around - flatter.
[profile/ivi/evas.git] / src / modules / engines / software_16_x11 / evas_engine.c
1 #include "evas_common.h"
2 #include "evas_private.h"
3 #include "evas_engine.h"
4 #include "Evas_Engine_Software_16_X11.h"
5 #include "evas_common_soft16.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    Display          *disp;
16    Drawable          draw;
17    GC                gc;
18    int               w, h, rot;
19    Tilebuf          *tb;
20    Tilebuf_Rect     *rects;
21    Tilebuf_Rect     *cur_rect;
22    X_Output_Buffer  *shbuf;
23    Soft16_Image     *tmp_out; /* used by indirect render, like rotation */
24    Region            clip_rects;
25    unsigned char     end : 1;
26    unsigned char     shm : 1;
27 };
28
29 /* prototypes we will use here */
30
31 static void *eng_info(Evas *e);
32 static void eng_info_free(Evas *e, void *info);
33 static void eng_setup(Evas *e, void *info);
34 static void eng_output_free(void *data);
35 static void eng_output_resize(void *data, int w, int h);
36 static void eng_output_tile_size_set(void *data, int w, int h);
37 static void eng_output_redraws_rect_add(void *data, int x, int y, int w, int h);
38 static void eng_output_redraws_rect_del(void *data, int x, int y, int w, int h);
39 static void eng_output_redraws_clear(void *data);
40 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);
41 static void eng_output_redraws_next_update_push(void *data, void *surface, int x, int y, int w, int h);
42 static void eng_output_flush(void *data);
43 static void eng_output_idle_flush(void *data);
44
45 /* engine api this module provides */
46 static void *
47 eng_info(Evas *e)
48 {
49    Evas_Engine_Info_Software_16_X11 *info;
50
51    info = calloc(1, sizeof(Evas_Engine_Info_Software_16_X11));
52    if (!info) return NULL;
53    info->magic.magic = rand();
54    return info;
55    e = NULL;
56 }
57
58 static void
59 eng_info_free(Evas *e, void *info)
60 {
61    Evas_Engine_Info_Software_16_X11 *in;
62
63    in = (Evas_Engine_Info_Software_16_X11 *)info;
64    free(in);
65 }
66
67 static void
68 _tmp_out_alloc(Render_Engine *re)
69 {
70    Tilebuf_Rect *r;
71    int w = 0, h = 0;
72
73    for (r = re->rects; r; r = (Tilebuf_Rect *)(r->_list_data.next))
74      {
75         if (r->w > w) w = r->w;
76         if (r->h > h) h = r->h;
77      }
78
79    if (re->tmp_out)
80      {
81         if ((re->tmp_out->cache_entry.w < w) || (re->tmp_out->cache_entry.h < h))
82           {
83              evas_cache_image_drop(&re->tmp_out->cache_entry);
84              re->tmp_out = NULL;
85           }
86      }
87
88    if (!re->tmp_out)
89      {
90         Soft16_Image *im;
91
92         im = (Soft16_Image *) evas_cache_image_empty(evas_common_soft16_image_cache_get());
93         im->cache_entry.flags.alpha = 0;
94         evas_cache_image_surface_alloc(&im->cache_entry, w, h);
95
96         re->tmp_out = im;
97      }
98 }
99
100
101 static void
102 eng_setup(Evas *e, void *in)
103 {
104    Render_Engine *re;
105    Evas_Engine_Info_Software_16_X11 *info;
106 /*    X_Output_Buffer *xob; */
107    XGCValues gcv;
108    
109    info = (Evas_Engine_Info_Software_16_X11 *)in;
110    if (!e->engine.data.output)
111      {
112         /* the only check - simplistic, i know, but enough for this 
113          * "special purpose" engine. Remember it is meant to be used
114          * for limited power devices that have a 16bit display mode
115          * and no real other acceleration, and high resolution so we
116          * can pre-dither into 16bpp. */
117 //      if (DefaultDepth(info->info.display, 
118 //                       DefaultScreen(info->info.display)) != 16)
119 //        return;
120         /* do common routine init - we wil at least use it for core
121          * image loading and font loading/glyph rendering & placement */
122         evas_common_cpu_init();
123         
124         evas_common_blend_init();
125         evas_common_image_init();
126         evas_common_convert_init();
127         evas_common_scale_init();
128         evas_common_rectangle_init();
129         evas_common_gradient_init();
130         evas_common_polygon_init();
131         evas_common_line_init();
132         evas_common_font_init();
133         evas_common_draw_init();
134         evas_common_tilebuf_init();
135         evas_common_soft16_image_init();
136
137         /* render engine specific data */
138         re = calloc(1, sizeof(Render_Engine));
139         e->engine.data.output = re;
140         re->disp = info->info.display;
141         re->draw = info->info.drawable;
142         re->gc = XCreateGC(re->disp, re->draw, 0, &gcv);
143         re->w = e->output.w;
144         re->h = e->output.h;
145         re->rot = info->info.rotation;
146         re->tb = evas_common_tilebuf_new(e->output.w, e->output.h);
147         if (re->tb)
148           evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
149      }
150    else
151      {
152         /* we changed the info after first init - do a re-eval where
153          * appropriate */
154 //      if (DefaultDepth(info->info.display, 
155 //                       DefaultScreen(info->info.display)) != 16)
156 //        return;
157         re = e->engine.data.output;
158         if (re->tb) evas_common_tilebuf_free(re->tb);
159         re->disp = info->info.display;
160         re->draw = info->info.drawable;
161         XFreeGC(re->disp, re->gc);
162         re->gc = XCreateGC(re->disp, re->draw, 0, &gcv);
163         re->w = e->output.w;
164         re->h = e->output.h;
165         re->rot = info->info.rotation;
166         re->tb = evas_common_tilebuf_new(e->output.w, e->output.h);
167         if (re->tb)
168           evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
169         if (re->tmp_out)
170           {
171              evas_cache_image_drop(&re->tmp_out->cache_entry);
172              re->tmp_out = NULL;
173           }
174      }
175    if (!e->engine.data.output) return;
176    /* add a draw context if we dont have one */
177    if (!e->engine.data.context)
178      e->engine.data.context =
179      e->engine.func->context_new(e->engine.data.output);
180    /* check if the display can do shm */
181    re->shm = evas_software_x11_x_can_do_shm(re->disp);
182 }
183
184 static void
185 eng_output_free(void *data)
186 {
187    Render_Engine *re;
188
189    re = (Render_Engine *)data;
190    if (re->shbuf) evas_software_x11_x_output_buffer_free(re->shbuf, 0);
191    if (re->clip_rects) XDestroyRegion(re->clip_rects);
192    if (re->gc) XFreeGC(re->disp, re->gc);
193    if (re->tb) evas_common_tilebuf_free(re->tb);
194    if (re->rects) evas_common_tilebuf_free_render_rects(re->rects);
195    if (re->tmp_out) evas_cache_image_drop(&re->tmp_out->cache_entry);
196    free(re);
197
198    evas_common_font_shutdown();
199    evas_common_image_shutdown();
200    evas_common_soft16_image_shutdown();
201 }
202
203 static void
204 eng_output_resize(void *data, int w, int h)
205 {
206    Render_Engine *re;
207
208    re = (Render_Engine *)data;
209
210    if ((re->w == w) && (re->h == h)) return;
211
212    evas_common_tilebuf_free(re->tb);
213    re->w = w;
214    re->h = h;
215    re->tb = evas_common_tilebuf_new(w, h);
216    if (re->tb)
217      evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
218    if (re->shbuf)
219      {
220         evas_software_x11_x_output_buffer_free(re->shbuf, 0);
221         re->shbuf = NULL;
222      }
223    if (re->clip_rects)
224      {
225         XDestroyRegion(re->clip_rects);
226         re->clip_rects = NULL;
227      }
228    if (re->tmp_out)
229      {
230         evas_cache_image_drop(&re->tmp_out->cache_entry);
231         re->tmp_out = NULL;
232      }
233 }
234
235 static void
236 eng_output_tile_size_set(void *data, int w, int h)
237 {
238    Render_Engine *re;
239
240    re = (Render_Engine *)data;
241    evas_common_tilebuf_set_tile_size(re->tb, w, h);
242 }
243
244 static void
245 eng_output_redraws_rect_add(void *data, int x, int y, int w, int h)
246 {
247    Render_Engine *re;
248
249    re = (Render_Engine *)data;
250    evas_common_tilebuf_add_redraw(re->tb, x, y, w, h);
251 }
252
253 static void
254 eng_output_redraws_rect_del(void *data, int x, int y, int w, int h)
255 {
256    Render_Engine *re;
257
258    re = (Render_Engine *)data;
259    evas_common_tilebuf_del_redraw(re->tb, x, y, w, h);
260 }
261
262 static void
263 eng_output_redraws_clear(void *data)
264 {
265    Render_Engine *re;
266
267    re = (Render_Engine *)data;
268    evas_common_tilebuf_clear(re->tb);
269 }
270
271 static inline void
272 _output_buffer_alloc(Render_Engine *re)
273 {
274    int w, h;
275    if (re->shbuf) return;
276
277    if ((re->rot == 0) || (re->rot == 180))
278      {
279         w = re->w;
280         h = re->h;
281      }
282    else
283      {
284         w = re->h;
285         h = re->w;
286      }
287
288    re->shbuf = evas_software_x11_x_output_buffer_new
289      (re->disp, DefaultVisual(re->disp, DefaultScreen(re->disp)),
290       DefaultDepth(re->disp, DefaultScreen(re->disp)),
291       w, h, 1, NULL);
292 }
293
294 static void *
295 eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, int *cx, int *cy, int *cw, int *ch)
296 {
297    Render_Engine *re;
298    Tilebuf_Rect *rect;
299    int ux, uy, uw, uh;
300
301    re = (Render_Engine *)data;
302    if (re->end)
303      {
304         re->end = 0;
305         return NULL;
306      }
307    if (!re->rects)
308      {
309         re->rects = evas_common_tilebuf_get_render_rects(re->tb);
310         if (!re->rects) return NULL;
311
312         re->cur_rect = re->rects;
313         _output_buffer_alloc(re);
314         if (re->rot != 0) _tmp_out_alloc(re); /* grows if required */
315      }
316    if (!re->cur_rect)
317      {
318         if (re->rects) evas_common_tilebuf_free_render_rects(re->rects);
319         re->rects = NULL;
320         return NULL;
321      }
322    rect = re->cur_rect;
323    ux = rect->x; uy = rect->y; uw = rect->w; uh = rect->h;
324    re->cur_rect = (Tilebuf_Rect *)(re->cur_rect->_list_data.next);
325    if (!re->cur_rect)
326      {
327         evas_common_tilebuf_free_render_rects(re->rects);
328         re->rects = NULL;
329         re->end = 1;
330      }
331
332    *x = ux; *y = uy; *w = uw; *h = uh;
333    if (re->rot == 0)
334      {
335         *cx = ux; *cy = uy; *cw = uw; *ch = uh;
336         return re->shbuf->im;
337      }
338    else
339      {
340         *cx = 0; *cy = 0; *cw = uw; *ch = uh;
341         return re->tmp_out;
342      }
343 }
344
345 static void
346 _blit_rot_90(Soft16_Image *dst, const Soft16_Image *src,
347              int out_x, int out_y, int w, int h)
348 {
349    DATA16 *dp, *sp;
350    int x, y;
351
352    sp = src->pixels;
353    dp = dst->pixels + (out_x +
354                        (w + out_y - 1) * dst->stride);
355
356    for (y = 0; y < h; y++)
357      {
358         DATA16 *dp_itr, *sp_itr;
359
360         sp_itr = sp;
361         dp_itr = dp;
362
363         for (x = 0; x < w; x++)
364           {
365              *dp_itr = *sp_itr;
366
367              sp_itr++;
368              dp_itr -= dst->stride;
369           }
370         sp += src->stride;
371         dp++;
372      }
373 }
374
375 static void
376 _blit_rot_180(Soft16_Image *dst, const Soft16_Image *src,
377               int out_x, int out_y, int w, int h)
378 {
379    DATA16 *dp, *sp;
380    int x, y;
381
382    sp = src->pixels;
383    dp = dst->pixels + ((w + out_x - 1) +
384                        (h + out_y - 1) * dst->stride);
385
386    for (y = 0; y < h; y++)
387      {
388         DATA16 *dp_itr, *sp_itr;
389
390         sp_itr = sp;
391         dp_itr = dp;
392
393         for (x = 0; x < w; x++)
394           {
395              *dp_itr = *sp_itr;
396
397              sp_itr++;
398              dp_itr--;
399           }
400         sp += src->stride;
401         dp -= dst->stride;
402      }
403 }
404
405 static void
406 _blit_rot_270(Soft16_Image *dst, const Soft16_Image *src,
407               int out_x, int out_y, int w, int h)
408 {
409    DATA16 *dp, *sp;
410    int x, y;
411
412    sp = src->pixels;
413    dp = dst->pixels + ((h + out_x - 1) +
414                        out_y * dst->stride);
415
416    for (y = 0; y < h; y++)
417      {
418         DATA16 *dp_itr, *sp_itr;
419
420         sp_itr = sp;
421         dp_itr = dp;
422
423         for (x = 0; x < w; x++)
424           {
425              *dp_itr = *sp_itr;
426
427              sp_itr++;
428              dp_itr += dst->stride;
429           }
430         sp += src->stride;
431         dp--;
432      }
433 }
434
435 static void
436 _tmp_out_process(Render_Engine *re, int out_x, int out_y, int w, int h)
437 {
438    Soft16_Image *d, *s;
439    DATA16 *dp, *sp;
440    int y, x, d_dir;
441
442    d = re->shbuf->im;
443    s = re->tmp_out;
444
445    if ((w < 1) || (h < 1) || (out_x >= d->cache_entry.w) || (out_y >= d->cache_entry.h))
446      return;
447
448    if (re->rot == 90)
449      _blit_rot_90(d, s, out_x, out_y, w, h);
450    else if (re->rot == 180)
451      _blit_rot_180(d, s, out_x, out_y, w, h);
452    else if (re->rot == 270)
453      _blit_rot_270(d, s, out_x, out_y, w, h);
454 }
455
456 static void
457 eng_output_redraws_next_update_push(void *data, void *surface, int x, int y, int w, int h)
458 {
459    Render_Engine *re;
460    XRectangle r;
461
462    re = (Render_Engine *)data;
463
464    if (!re->clip_rects)
465       re->clip_rects = XCreateRegion();
466
467    if (re->rot == 0)
468      {
469         r.x = x;
470         r.y = y;
471         r.width = w;
472         r.height = h;
473      }
474    else if (re->rot == 90)
475      {
476         r.x = y;
477         r.y = re->w - w - x;
478         r.width = h;
479         r.height = w;
480      }
481    else if (re->rot == 180)
482      {
483         r.x = re->w - w - x;
484         r.y = re->h - h - y;
485         r.width = w;
486         r.height = h;
487      }
488    else if (re->rot == 270)
489      {
490         r.x = re->h - h - y;
491         r.y = x;
492         r.width = h;
493         r.height = w;
494      }
495
496    if (re->rot != 0)
497      _tmp_out_process(re, r.x, r.y, w, h);
498    XUnionRectWithRegion(&r, re->clip_rects, re->clip_rects);
499 }
500
501 static void
502 eng_output_flush(void *data)
503 {
504    Render_Engine *re;
505    int w, h;
506
507    re = (Render_Engine *)data;
508    if (re->clip_rects)
509      {
510         XSetRegion(re->disp, re->gc, re->clip_rects);
511         XDestroyRegion(re->clip_rects);
512         re->clip_rects = NULL;
513      }
514    else return;
515
516    evas_software_x11_x_output_buffer_paste
517      (re->shbuf, re->draw, re->gc, 0, 0, re->shbuf->im->cache_entry.w, re->shbuf->im->cache_entry.h, 1);
518    XSetClipMask(re->disp, re->gc, None);
519 }
520
521 static void
522 eng_output_idle_flush(void *data)
523 {
524    Render_Engine *re;
525
526    re = (Render_Engine *)data;
527    if (re->shbuf)
528      {
529         evas_software_x11_x_output_buffer_free(re->shbuf, 0);
530         re->shbuf = NULL;
531      }
532    if (re->clip_rects)
533      {
534         XDestroyRegion(re->clip_rects);
535         re->clip_rects = NULL;
536      }
537    if (re->tmp_out)
538      {
539         evas_cache_image_drop(&re->tmp_out->cache_entry);
540         re->tmp_out = NULL;
541      }
542 }
543
544
545 /* module advertising code */
546 EAPI int
547 module_open(Evas_Module *em)
548 {
549    if (!em) return 0;
550    /* get whatever engine module we inherit from */
551    if (!_evas_module_engine_inherit(&pfunc, "software_16")) return 0;
552    /* store it for later use */
553    func = pfunc;
554    /* now to override methods */
555 #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
556    ORD(info);
557    ORD(info_free);
558    ORD(setup);
559    ORD(output_free);
560    ORD(output_resize);
561    ORD(output_tile_size_set);
562    ORD(output_redraws_rect_add);
563    ORD(output_redraws_rect_del);
564    ORD(output_redraws_clear);
565    ORD(output_redraws_next_update_get);
566    ORD(output_redraws_next_update_push);
567    ORD(output_flush);
568    ORD(output_idle_flush);
569    /* now advertise out own api */
570    em->functions = (void *)(&func);
571    return 1;
572 }
573
574 EAPI void
575 module_close(void)
576 {
577 }
578
579 EAPI Evas_Module_Api evas_modapi = 
580 {
581    EVAS_MODULE_API_VERSION, 
582      EVAS_MODULE_TYPE_ENGINE,
583      "software_16_x11",
584      "none"
585 };