big patch from Samsung SAIT (Advanced research group) for async multi-frame
[framework/uifw/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 int _evas_engine_soft16_x11_log_dom = -1;
8 /* function tables - filled in later (func and parent func) */
9 static Evas_Func func, pfunc;
10
11 struct xrdb_user
12 {
13    time_t last_stat;
14    time_t last_mtime;
15    XrmDatabase db;
16 };
17 static struct xrdb_user xrdb_user = {0, 0, NULL};
18
19 static Eina_Bool
20 xrdb_user_query(const char *name, const char *cls, char **type, XrmValue *val)
21 {
22    time_t last = xrdb_user.last_stat, now = time(NULL);
23
24    xrdb_user.last_stat = now;
25    if (last != now) /* don't stat() more than once every second */
26      {
27         struct stat st;
28         const char *home = getenv("HOME");
29         char tmp[PATH_MAX];
30
31         if (!home) goto failed;
32         snprintf(tmp, sizeof(tmp), "%s/.Xdefaults", home);
33         if (stat(tmp, &st) != 0) goto failed;
34         if (xrdb_user.last_mtime != st.st_mtime)
35           {
36              if (xrdb_user.db) XrmDestroyDatabase(xrdb_user.db);
37              xrdb_user.db = XrmGetFileDatabase(tmp);
38              if (!xrdb_user.db) goto failed;
39              xrdb_user.last_mtime = st.st_mtime;
40           }
41      }
42
43    if (!xrdb_user.db) return EINA_FALSE;
44    return XrmGetResource(xrdb_user.db, name, cls, type, val);
45
46  failed:
47    if (xrdb_user.db)
48      {
49         XrmDestroyDatabase(xrdb_user.db);
50         xrdb_user.db = NULL;
51      }
52    xrdb_user.last_mtime = 0;
53    return EINA_FALSE;
54 }
55
56 /* engine struct data */
57 typedef struct _Render_Engine Render_Engine;
58
59 struct _Render_Engine
60 {
61    Display          *disp;
62    Drawable          draw;
63    GC                gc;
64    int               w, h, rot;
65    Tilebuf          *tb;
66    Tilebuf_Rect     *rects;
67    Tilebuf_Rect     *cur_rect;
68    
69    XrmDatabase   xrdb; // xres - dpi
70    struct { // xres - dpi
71       int        dpi; // xres - dpi
72    } xr; // xres - dpi
73    
74    X_Output_Buffer  *shbuf;
75    Soft16_Image     *tmp_out; /* used by indirect render, like rotation */
76    Region            clip_rects;
77    unsigned char     end : 1;
78    unsigned char     shm : 1;
79 };
80
81 /* prototypes we will use here */
82
83 static void *eng_info(Evas *e);
84 static void eng_info_free(Evas *e, void *info);
85 static int eng_setup(Evas *e, void *info);
86 static void eng_output_free(void *data);
87 static void eng_output_resize(void *data, int w, int h);
88 static void eng_output_tile_size_set(void *data, int w, int h);
89 static void eng_output_redraws_rect_add(void *data, int x, int y, int w, int h);
90 static void eng_output_redraws_rect_del(void *data, int x, int y, int w, int h);
91 static void eng_output_redraws_clear(void *data);
92 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);
93 static void eng_output_redraws_next_update_push(void *data, void *surface, int x, int y, int w, int h);
94 static void eng_output_flush(void *data);
95 static void eng_output_idle_flush(void *data);
96
97 /* engine api this module provides */
98 static void *
99 eng_info(Evas *e)
100 {
101    Evas_Engine_Info_Software_16_X11 *info;
102    
103    info = calloc(1, sizeof(Evas_Engine_Info_Software_16_X11));
104    if (!info) return NULL;
105    info->magic.magic = rand();
106    info->render_mode = EVAS_RENDER_MODE_BLOCKING;
107    return info;
108    e = NULL;
109 }
110
111 static void
112 eng_info_free(Evas *e __UNUSED__, void *info)
113 {
114    Evas_Engine_Info_Software_16_X11 *in;
115    in = (Evas_Engine_Info_Software_16_X11 *)info;
116    free(in);
117 }
118
119 static void
120 _tmp_out_alloc(Render_Engine *re)
121 {
122    Tilebuf_Rect *r;
123    int w = 0, h = 0;
124
125    EINA_INLIST_FOREACH(re->rects, r)
126      {
127         if (r->w > w) w = r->w;
128         if (r->h > h) h = r->h;
129      }
130
131    if (re->tmp_out)
132      {
133         if ((re->tmp_out->cache_entry.w < w) || (re->tmp_out->cache_entry.h < h))
134           {
135              evas_cache_image_drop(&re->tmp_out->cache_entry);
136              re->tmp_out = NULL;
137           }
138      }
139
140    if (!re->tmp_out)
141      {
142         Soft16_Image *im;
143
144         im = (Soft16_Image *) evas_cache_image_empty(evas_common_soft16_image_cache_get());
145         im->cache_entry.flags.alpha = 0;
146         evas_cache_image_surface_alloc(&im->cache_entry, w, h);
147
148         re->tmp_out = im;
149      }
150 }
151
152
153 static int
154 eng_setup(Evas *e, void *in)
155 {
156    Render_Engine *re;
157    Evas_Engine_Info_Software_16_X11 *info;
158 /*    X_Output_Buffer *xob; */
159    XGCValues gcv;
160    
161    info = (Evas_Engine_Info_Software_16_X11 *)in;
162    if (!e->engine.data.output)
163      {
164         /* the only check - simplistic, i know, but enough for this 
165          * "special purpose" engine. Remember it is meant to be used
166          * for limited power devices that have a 16bit display mode
167          * and no real other acceleration, and high resolution so we
168          * can pre-dither into 16bpp. */
169 //      if (DefaultDepth(info->info.display, 
170 //                       DefaultScreen(info->info.display)) != 16)
171 //        return;
172         /* do common routine init - we wil at least use it for core
173          * image loading and font loading/glyph rendering & placement */
174         evas_common_cpu_init();
175         
176         evas_common_blend_init();
177         evas_common_image_init();
178         evas_common_convert_init();
179         evas_common_scale_init();
180         evas_common_rectangle_init();
181         evas_common_gradient_init();
182         evas_common_polygon_init();
183         evas_common_line_init();
184         evas_common_font_init();
185         evas_common_draw_init();
186         evas_common_tilebuf_init();
187         evas_common_soft16_image_init();
188
189         /* render engine specific data */
190         re = calloc(1, sizeof(Render_Engine));
191         if (!re)
192           return 0;
193         e->engine.data.output = re;
194         re->disp = info->info.display;
195         re->draw = info->info.drawable;
196         re->gc = XCreateGC(re->disp, re->draw, 0, &gcv);
197         re->w = e->output.w;
198         re->h = e->output.h;
199         re->rot = info->info.rotation;
200         re->tb = evas_common_tilebuf_new(e->output.w, e->output.h);
201         if (re->tb)
202           evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
203      }
204    else
205      {
206         /* we changed the info after first init - do a re-eval where
207          * appropriate */
208 //      if (DefaultDepth(info->info.display, 
209 //                       DefaultScreen(info->info.display)) != 16)
210 //        return;
211         re = e->engine.data.output;
212         if (re->tb) evas_common_tilebuf_free(re->tb);
213         re->disp = info->info.display;
214         re->draw = info->info.drawable;
215         XFreeGC(re->disp, re->gc);
216         re->gc = XCreateGC(re->disp, re->draw, 0, &gcv);
217         re->w = e->output.w;
218         re->h = e->output.h;
219         re->rot = info->info.rotation;
220         re->tb = evas_common_tilebuf_new(e->output.w, e->output.h);
221         if (re->tb)
222           evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
223         if (re->tmp_out)
224           {
225              evas_cache_image_drop(&re->tmp_out->cache_entry);
226              re->tmp_out = NULL;
227           }
228      }
229    if (!e->engine.data.output) return 0;
230    
231    
232      {   
233         int status;
234         char *type = NULL;
235         XrmValue val;
236         
237         re->xr.dpi = 75000; // dpy * 1000
238
239         status = xrdb_user_query("Xft.dpi", "Xft.Dpi", &type, &val);
240         if ((!status) || (!type))
241           {
242              if (!re->xrdb) re->xrdb = XrmGetDatabase(re->disp);
243              if (re->xrdb)
244                status = XrmGetResource(re->xrdb,
245                                        "Xft.dpi", "Xft.Dpi", &type, &val);
246           }
247
248         if ((status) && (type))
249           {
250              if (!strcmp(type, "String"))
251                {
252                   const char *str, *dp;
253                   
254                   str = val.addr;
255                   dp = strchr(str, '.');
256                   if (!dp) dp = strchr(str, ',');
257                   
258                   if (dp)
259                     {
260                        int subdpi, len, i;
261                        char *buf;
262                        
263                        buf = alloca(dp - str + 1);
264                        strncpy(buf, str, dp - str);
265                        buf[dp - str] = 0;
266                        len = strlen(dp + 1);
267                        subdpi = atoi(dp + 1);
268                        
269                        if (len < 3)
270                          {
271                             for (i = len; i < 3; i++) subdpi *= 10;
272                          }
273                        else if (len > 3)
274                          {
275                             for (i = len; i > 3; i--) subdpi /= 10;
276                          }
277                        re->xr.dpi = atoi(buf) * 1000;
278                     }
279                   else
280                     re->xr.dpi = atoi(str) * 1000;
281                }
282           }
283         evas_common_font_dpi_set(re->xr.dpi / 1000);
284      }
285    
286    /* add a draw context if we dont have one */
287    if (!e->engine.data.context)
288      e->engine.data.context =
289      e->engine.func->context_new(e->engine.data.output);
290    /* check if the display can do shm */
291    re->shm = evas_software_x11_x_can_do_shm(re->disp);
292
293    return 1;
294 }
295
296 static void
297 eng_output_free(void *data)
298 {
299    Render_Engine *re;
300
301    re = (Render_Engine *)data;
302    
303 // NOTE: XrmGetDatabase() result is shared per connection, do not free it.
304 //   if (re->xrdb) XrmDestroyDatabase(re->xrdb);
305    
306    if (re->shbuf) evas_software_x11_x_output_buffer_free(re->shbuf, 0);
307    if (re->clip_rects) XDestroyRegion(re->clip_rects);
308    if (re->gc) XFreeGC(re->disp, re->gc);
309    if (re->tb) evas_common_tilebuf_free(re->tb);
310    if (re->rects) evas_common_tilebuf_free_render_rects(re->rects);
311    if (re->tmp_out) evas_cache_image_drop(&re->tmp_out->cache_entry);
312    free(re);
313
314    evas_common_font_shutdown();
315    evas_common_image_shutdown();
316    evas_common_soft16_image_shutdown();
317 }
318
319 static void
320 eng_output_resize(void *data, int w, int h)
321 {
322    Render_Engine *re;
323
324    re = (Render_Engine *)data;
325
326    if ((re->w == w) && (re->h == h)) return;
327
328    evas_common_tilebuf_free(re->tb);
329    re->w = w;
330    re->h = h;
331    re->tb = evas_common_tilebuf_new(w, h);
332    if (re->tb)
333      evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
334    if (re->shbuf)
335      {
336         evas_software_x11_x_output_buffer_free(re->shbuf, 0);
337         re->shbuf = NULL;
338      }
339    if (re->clip_rects)
340      {
341         XDestroyRegion(re->clip_rects);
342         re->clip_rects = NULL;
343      }
344    if (re->tmp_out)
345      {
346         evas_cache_image_drop(&re->tmp_out->cache_entry);
347         re->tmp_out = NULL;
348      }
349 }
350
351 static void
352 eng_output_tile_size_set(void *data, int w, int h)
353 {
354    Render_Engine *re;
355
356    re = (Render_Engine *)data;
357    evas_common_tilebuf_set_tile_size(re->tb, w, h);
358 }
359
360 static void
361 eng_output_redraws_rect_add(void *data, int x, int y, int w, int h)
362 {
363    Render_Engine *re;
364
365    re = (Render_Engine *)data;
366    evas_common_tilebuf_add_redraw(re->tb, x, y, w, h);
367 }
368
369 static void
370 eng_output_redraws_rect_del(void *data, int x, int y, int w, int h)
371 {
372    Render_Engine *re;
373
374    re = (Render_Engine *)data;
375    evas_common_tilebuf_del_redraw(re->tb, x, y, w, h);
376 }
377
378 static void
379 eng_output_redraws_clear(void *data)
380 {
381    Render_Engine *re;
382
383    re = (Render_Engine *)data;
384    evas_common_tilebuf_clear(re->tb);
385 }
386
387 static inline void
388 _output_buffer_alloc(Render_Engine *re)
389 {
390    int w, h;
391    if (re->shbuf) return;
392
393    if ((re->rot == 0) || (re->rot == 180))
394      {
395         w = re->w;
396         h = re->h;
397      }
398    else
399      {
400         w = re->h;
401         h = re->w;
402      }
403
404    re->shbuf = evas_software_x11_x_output_buffer_new
405      (re->disp, DefaultVisual(re->disp, DefaultScreen(re->disp)),
406       DefaultDepth(re->disp, DefaultScreen(re->disp)),
407       w, h, 1, NULL);
408 }
409
410 static void *
411 eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, int *cx, int *cy, int *cw, int *ch)
412 {
413    Render_Engine *re;
414    Tilebuf_Rect *rect;
415    int ux, uy, uw, uh;
416
417    re = (Render_Engine *)data;
418    if (re->end)
419      {
420         re->end = 0;
421         return NULL;
422      }
423    if (!re->rects)
424      {
425         re->rects = evas_common_tilebuf_get_render_rects(re->tb);
426         if (!re->rects) return NULL;
427
428         re->cur_rect = re->rects;
429         _output_buffer_alloc(re);
430         if (re->rot != 0) _tmp_out_alloc(re); /* grows if required */
431      }
432    if (!re->cur_rect)
433      {
434         if (re->rects) evas_common_tilebuf_free_render_rects(re->rects);
435         re->rects = NULL;
436         return NULL;
437      }
438    rect = re->cur_rect;
439    ux = rect->x; uy = rect->y; uw = rect->w; uh = rect->h;
440    re->cur_rect = (Tilebuf_Rect *)((EINA_INLIST_GET(re->cur_rect))->next);
441    if (!re->cur_rect)
442      {
443         evas_common_tilebuf_free_render_rects(re->rects);
444         re->rects = NULL;
445         re->end = 1;
446      }
447
448    *x = ux; *y = uy; *w = uw; *h = uh;
449    if (re->rot == 0)
450      {
451         *cx = ux; *cy = uy; *cw = uw; *ch = uh;
452         return re->shbuf->im;
453      }
454    else
455      {
456         *cx = 0; *cy = 0; *cw = uw; *ch = uh;
457         return re->tmp_out;
458      }
459 }
460
461 static void
462 _blit_rot_90(Soft16_Image *dst, const Soft16_Image *src,
463              int out_x, int out_y, int w, int h)
464 {
465    DATA16 *dp, *sp;
466    int x, y;
467
468    sp = src->pixels;
469    dp = dst->pixels + (out_x +
470                        (w + out_y - 1) * dst->stride);
471
472    for (y = 0; y < h; y++)
473      {
474         DATA16 *dp_itr, *sp_itr;
475
476         sp_itr = sp;
477         dp_itr = dp;
478
479         for (x = 0; x < w; x++)
480           {
481              *dp_itr = *sp_itr;
482
483              sp_itr++;
484              dp_itr -= dst->stride;
485           }
486         sp += src->stride;
487         dp++;
488      }
489 }
490
491 static void
492 _blit_rot_180(Soft16_Image *dst, const Soft16_Image *src,
493               int out_x, int out_y, int w, int h)
494 {
495    DATA16 *dp, *sp;
496    int x, y;
497
498    sp = src->pixels;
499    dp = dst->pixels + ((w + out_x - 1) +
500                        (h + out_y - 1) * dst->stride);
501
502    for (y = 0; y < h; y++)
503      {
504         DATA16 *dp_itr, *sp_itr;
505
506         sp_itr = sp;
507         dp_itr = dp;
508
509         for (x = 0; x < w; x++)
510           {
511              *dp_itr = *sp_itr;
512
513              sp_itr++;
514              dp_itr--;
515           }
516         sp += src->stride;
517         dp -= dst->stride;
518      }
519 }
520
521 static void
522 _blit_rot_270(Soft16_Image *dst, const Soft16_Image *src,
523               int out_x, int out_y, int w, int h)
524 {
525    DATA16 *dp, *sp;
526    int x, y;
527
528    sp = src->pixels;
529    dp = dst->pixels + ((h + out_x - 1) +
530                        out_y * dst->stride);
531
532    for (y = 0; y < h; y++)
533      {
534         DATA16 *dp_itr, *sp_itr;
535
536         sp_itr = sp;
537         dp_itr = dp;
538
539         for (x = 0; x < w; x++)
540           {
541              *dp_itr = *sp_itr;
542
543              sp_itr++;
544              dp_itr += dst->stride;
545           }
546         sp += src->stride;
547         dp--;
548      }
549 }
550
551 static void
552 _tmp_out_process(Render_Engine *re, int out_x, int out_y, int w, int h)
553 {
554    Soft16_Image *d, *s;
555
556    d = re->shbuf->im;
557    s = re->tmp_out;
558
559    if ((w < 1) || (h < 1) || (out_x >= d->cache_entry.w) || (out_y >= d->cache_entry.h))
560      return;
561
562    if (re->rot == 90)
563      _blit_rot_90(d, s, out_x, out_y, w, h);
564    else if (re->rot == 180)
565      _blit_rot_180(d, s, out_x, out_y, w, h);
566    else if (re->rot == 270)
567      _blit_rot_270(d, s, out_x, out_y, w, h);
568 }
569
570 static void
571 eng_output_redraws_next_update_push(void *data, void *surface __UNUSED__, int x, int y, int w, int h)
572 {
573    Render_Engine *re;
574    XRectangle r = { 0, 0, 0, 0 };
575
576    re = (Render_Engine *)data;
577
578    if (!re->clip_rects)
579       re->clip_rects = XCreateRegion();
580
581    if (re->rot == 0)
582      {
583         r.x = x;
584         r.y = y;
585         r.width = w;
586         r.height = h;
587      }
588    else if (re->rot == 90)
589      {
590         r.x = y;
591         r.y = re->w - w - x;
592         r.width = h;
593         r.height = w;
594      }
595    else if (re->rot == 180)
596      {
597         r.x = re->w - w - x;
598         r.y = re->h - h - y;
599         r.width = w;
600         r.height = h;
601      }
602    else if (re->rot == 270)
603      {
604         r.x = re->h - h - y;
605         r.y = x;
606         r.width = h;
607         r.height = w;
608      }
609
610    if (re->rot != 0)
611      _tmp_out_process(re, r.x, r.y, w, h);
612    XUnionRectWithRegion(&r, re->clip_rects, re->clip_rects);
613 }
614
615 static void
616 eng_output_flush(void *data)
617 {
618    Render_Engine *re;
619
620    re = (Render_Engine *)data;
621    if (re->clip_rects)
622      {
623         XSetRegion(re->disp, re->gc, re->clip_rects);
624         XDestroyRegion(re->clip_rects);
625         re->clip_rects = NULL;
626      }
627    else return;
628
629    evas_software_x11_x_output_buffer_paste
630      (re->shbuf, re->draw, re->gc, 0, 0, re->shbuf->im->cache_entry.w, re->shbuf->im->cache_entry.h, 1);
631    XSetClipMask(re->disp, re->gc, None);
632 }
633
634 static void
635 eng_output_idle_flush(void *data)
636 {
637    Render_Engine *re;
638
639    re = (Render_Engine *)data;
640    if (re->shbuf)
641      {
642         evas_software_x11_x_output_buffer_free(re->shbuf, 0);
643         re->shbuf = NULL;
644      }
645    if (re->clip_rects)
646      {
647         XDestroyRegion(re->clip_rects);
648         re->clip_rects = NULL;
649      }
650    if (re->tmp_out)
651      {
652         evas_cache_image_drop(&re->tmp_out->cache_entry);
653         re->tmp_out = NULL;
654      }
655 }
656
657 static Eina_Bool
658 eng_canvas_alpha_get(void *data __UNUSED__, void *context __UNUSED__)
659 {
660    return EINA_FALSE;
661 }
662
663 /* module advertising code */
664 static int
665 module_open(Evas_Module *em)
666 {
667    static Eina_Bool xrm_inited = EINA_FALSE;
668    if (!xrm_inited)
669      {
670         xrm_inited = EINA_TRUE;
671         XrmInitialize();
672      }
673
674    if (!em) return 0;
675    /* get whatever engine module we inherit from */
676    if (!_evas_module_engine_inherit(&pfunc, "software_16")) return 0;
677    _evas_engine_soft16_x11_log_dom = eina_log_domain_register("EvasSoft16X11", EVAS_DEFAULT_LOG_COLOR);
678    if(_evas_engine_soft16_x11_log_dom < 0) 
679      {
680        EINA_LOG_ERR("Impossible to create a log domain for the Soft16_X11 engine.\n");
681        return 0;
682      }
683
684    /* store it for later use */
685    func = pfunc;
686    /* now to override methods */
687 #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
688    ORD(info);
689    ORD(info_free);
690    ORD(setup);
691    ORD(canvas_alpha_get);
692    ORD(output_free);
693    ORD(output_resize);
694    ORD(output_tile_size_set);
695    ORD(output_redraws_rect_add);
696    ORD(output_redraws_rect_del);
697    ORD(output_redraws_clear);
698    ORD(output_redraws_next_update_get);
699    ORD(output_redraws_next_update_push);
700    ORD(output_flush);
701    ORD(output_idle_flush);
702    /* now advertise out own api */
703    em->functions = (void *)(&func);
704    return 1;
705 }
706
707 static void
708 module_close(Evas_Module *em)
709 {
710   eina_log_domain_unregister(_evas_engine_soft16_x11_log_dom);
711   if (xrdb_user.db)
712     {
713        XrmDestroyDatabase(xrdb_user.db);
714        xrdb_user.last_stat = 0;
715        xrdb_user.last_mtime = 0;
716        xrdb_user.db = NULL;
717     }
718 }
719
720 static Evas_Module_Api evas_modapi =
721 {
722    EVAS_MODULE_API_VERSION,
723    "software_16_x11",
724    "none",
725    {
726      module_open,
727      module_close
728    }
729 };
730
731 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_ENGINE, engine, software_16_x11);
732
733 #ifndef EVAS_STATIC_BUILD_SOFTWARE_16_X11
734 EVAS_EINA_MODULE_DEFINE(engine, software_16_x11);
735 #endif
736