move around - flatter.
[profile/ivi/evas.git] / src / modules / engines / gl_x11 / evas_engine.c
1 #ifdef HAVE_GL_GLEW_H
2 # include <GL/glxew.h>
3 #else
4 # include <GL/glx.h>
5 #endif
6
7 #include "evas_gl_common.h"
8
9 #include <X11/Xlib.h>
10 #include <GL/gl.h>
11 #include <GL/glu.h>
12
13 #include "evas_common.h"
14 #include "evas_private.h"
15 #include "evas_engine.h"
16 #include "Evas_Engine_GL_X11.h"
17
18
19 /* function tables - filled in later (func and parent func) */
20 static Evas_Func func, pfunc;
21
22 static Visual *eng_best_visual_get(Display *disp, int screen);
23 static Colormap eng_best_colormap_get(Display *disp, int screen);
24 static int eng_best_depth_get(Display *disp, int screen);
25
26 typedef struct _Render_Engine Render_Engine;
27
28 struct _Render_Engine
29 {
30    Evas_GL_X11_Window *win;
31    int                 end;
32 };
33
34 static void *
35 eng_info(Evas *e)
36 {
37    Evas_Engine_Info_GL_X11 *info;
38
39    info = calloc(1, sizeof(Evas_Engine_Info_GL_X11));
40    if (!info) return NULL;
41    info->magic.magic = rand();
42    info->func.best_visual_get = eng_best_visual_get;
43    info->func.best_colormap_get = eng_best_colormap_get;
44    info->func.best_depth_get = eng_best_depth_get;
45    return info;
46    e = NULL;
47 }
48
49 static void
50 eng_info_free(Evas *e, void *info)
51 {
52    Evas_Engine_Info_GL_X11 *in;
53
54    in = (Evas_Engine_Info_GL_X11 *)info;
55    free(in);
56 }
57
58 static void
59 eng_setup(Evas *e, void *in)
60 {
61    Render_Engine *re;
62    Evas_Engine_Info_GL_X11 *info;
63    int eb, evb;
64
65    info = (Evas_Engine_Info_GL_X11 *)in;
66    if (!e->engine.data.output)
67      {
68         if (!glXQueryExtension(info->info.display, &eb, &evb)) return;
69         re = calloc(1, sizeof(Render_Engine));
70         if (!re) return;
71         e->engine.data.output = re;
72         re->win = eng_window_new(info->info.display,
73                                  info->info.drawable,
74                                  0 /* FIXME: screen 0 assumption */,
75                                  info->info.visual,
76                                  info->info.colormap,
77                                  info->info.depth,
78                                  e->output.w,
79                                  e->output.h);
80         if (!re->win)
81           {
82              free(re);
83              e->engine.data.output = NULL;
84              return;
85           }
86
87         evas_common_cpu_init();
88         
89         evas_common_blend_init();
90         evas_common_image_init();
91         evas_common_convert_init();
92         evas_common_scale_init();
93         evas_common_rectangle_init();
94         evas_common_gradient_init();
95         evas_common_polygon_init();
96         evas_common_line_init();
97         evas_common_font_init();
98         evas_common_draw_init();
99         evas_common_tilebuf_init();
100      }
101    else
102      {
103         re = e->engine.data.output;
104         eng_window_free(re->win);
105         re->win = eng_window_new(info->info.display,
106                                  info->info.drawable,
107                                  0,/* FIXME: screen 0 assumption */
108                                  info->info.visual,
109                                  info->info.colormap,
110                                  info->info.depth,
111                                  e->output.w,
112                                  e->output.h);
113      }
114    if (!e->engine.data.output) return;
115    if (!e->engine.data.context)
116      e->engine.data.context =
117      e->engine.func->context_new(e->engine.data.output);
118 }
119
120 static void
121 eng_output_free(void *data)
122 {
123    Render_Engine *re;
124
125    re = (Render_Engine *)data;
126    eng_window_free(re->win);
127    free(re);
128
129    evas_common_font_shutdown();
130    evas_common_image_shutdown();
131 }
132
133 static void
134 eng_output_resize(void *data, int w, int h)
135 {
136    Render_Engine *re;
137
138    re = (Render_Engine *)data;
139    re->win->w = w;
140    re->win->h = h;
141    evas_gl_common_context_resize(re->win->gl_context, w, h);
142 }
143
144 static void
145 eng_output_tile_size_set(void *data, int w, int h)
146 {
147    Render_Engine *re;
148
149    re = (Render_Engine *)data;
150 }
151
152 static void
153 eng_output_redraws_rect_add(void *data, int x, int y, int w, int h)
154 {
155    Render_Engine *re;
156
157    re = (Render_Engine *)data;
158    evas_gl_common_context_resize(re->win->gl_context, re->win->w, re->win->h);
159    /* smple bounding box */
160    if (!re->win->draw.redraw)
161      {
162 #if 0
163         re->win->draw.x1 = x;
164         re->win->draw.y1 = y;
165         re->win->draw.x2 = x + w - 1;
166         re->win->draw.y2 = y + h - 1;
167 #else
168         re->win->draw.x1 = 0;
169         re->win->draw.y1 = 0;
170         re->win->draw.x2 = re->win->w - 1;
171         re->win->draw.y2 = re->win->h - 1;
172 #endif
173      }
174    else
175      {
176         if (x < re->win->draw.x1) re->win->draw.x1 = x;
177         if (y < re->win->draw.y1) re->win->draw.y1 = y;
178         if ((x + w - 1) > re->win->draw.x2) re->win->draw.x2 = x + w - 1;
179         if ((y + h - 1) > re->win->draw.y2) re->win->draw.y2 = y + h - 1;
180      }
181    re->win->draw.redraw = 1;
182 }
183
184 static void
185 eng_output_redraws_rect_del(void *data, int x, int y, int w, int h)
186 {
187    Render_Engine *re;
188
189    re = (Render_Engine *)data;
190 }
191
192 static void
193 eng_output_redraws_clear(void *data)
194 {
195    Render_Engine *re;
196
197    re = (Render_Engine *)data;
198    re->win->draw.redraw = 0;
199 //   printf("GL: finish update cycle!\n");
200 }
201
202 /* at least the nvidia drivers are so abysmal that copying from the backbuffer
203  * to the front using glCopyPixels() that you literally can WATCH it draw the
204  * pixels slowly across the screen with a window update taking multiple
205  * seconds - so workaround by doing a full buffer render as frankly GL isn't
206  * up to doing anything that isn't done by quake (etc.)
207  */
208 #define SLOW_GL_COPY_RECT 1
209 /* vsync games - not for now though */
210 //#define VSYNC_TO_SCREEN 1
211
212 static void *
213 eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, int *cx, int *cy, int *cw, int *ch)
214 {
215    Render_Engine *re;
216
217    re = (Render_Engine *)data;
218    /* get the upate rect surface - return engine data as dummy */
219    if (!re->win->draw.redraw)
220      {
221 //      printf("GL: NO updates!\n");
222         return NULL;
223      }
224 //   printf("GL: update....!\n");
225 #ifdef SLOW_GL_COPY_RECT
226    /* if any update - just return the whole canvas - works with swap 
227     * buffers then */
228    if (x) *x = 0;
229    if (y) *y = 0;
230    if (w) *w = re->win->w;
231    if (h) *h = re->win->h;
232    if (cx) *cx = 0;
233    if (cy) *cy = 0;
234    if (cw) *cw = re->win->w;
235    if (ch) *ch = re->win->h;
236 #else
237    /* 1 update - INCREDIBLY SLOW if combined with swap_rect in flush. a gl
238     * problem where there just is no hardware path for somethnig that
239     * obviously SHOULD be there */
240    /* only 1 update to minimise gl context games and rendering multiple update
241     * regions as evas does with other engines
242     */
243    if (x) *x = re->win->draw.x1;
244    if (y) *y = re->win->draw.y1;
245    if (w) *w = re->win->draw.x2 - re->win->draw.x1 + 1;
246    if (h) *h = re->win->draw.y2 - re->win->draw.y1 + 1;
247    if (cx) *cx = re->win->draw.x1;
248    if (cy) *cy = re->win->draw.y1;
249    if (cw) *cw = re->win->draw.x2 - re->win->draw.x1 + 1;
250    if (ch) *ch = re->win->draw.y2 - re->win->draw.y1 + 1;
251 #endif   
252    return re;
253 }
254
255 static void
256 eng_output_redraws_next_update_push(void *data, void *surface, int x, int y, int w, int h)
257 {
258    Render_Engine *re;
259
260    re = (Render_Engine *)data;
261    /* put back update surface.. in this case just unflag redraw */
262 //   printf("GL: update done.\n");
263    re->win->draw.redraw = 0;
264    re->win->draw.drew = 1;
265 }
266
267 static void
268 eng_output_flush(void *data)
269 {
270    Render_Engine *re;
271
272    re = (Render_Engine *)data;
273    if (!re->win->draw.drew) return;
274    re->win->draw.drew = 0;
275 //   printf("GL: flush your mush!\n");
276    eng_window_use(re->win);
277
278 #ifdef VSYNC_TO_SCREEN
279    glFlush();
280      {
281         unsigned int rc;
282    
283         glXGetVideoSyncSGI(&rc);
284         glXWaitVideoSyncSGI(2, (rc + 1) % 2, &rc);
285      }
286 #endif   
287 #ifdef SLOW_GL_COPY_RECT
288    glXSwapBuffers(re->win->disp, re->win->win);
289 #else
290    /* SLOW AS ALL HELL! */
291    evas_gl_common_swap_rect(re->win->gl_context,
292                             re->win->draw.x1, re->win->draw.y1,
293                             re->win->draw.x2 - re->win->draw.x1 + 1,
294                             re->win->draw.y2 - re->win->draw.y1 + 1);
295 #endif
296 //   glFlush();
297 //   glXWaitGL();
298 //   XSync(re->win->disp, False);
299 //   printf("SYNC! %i\n", fr++);
300 }
301
302 static void
303 eng_output_idle_flush(void *data)
304 {
305    Render_Engine *re;
306
307    re = (Render_Engine *)data;
308 }
309
310 static void
311 eng_context_cutout_add(void *data, void *context, int x, int y, int w, int h)
312 {
313    Render_Engine *re;
314
315    re = (Render_Engine *)data;
316    /* not used in gl engine */
317 }
318
319 static void
320 eng_context_cutout_clear(void *data, void *context)
321 {
322    Render_Engine *re;
323
324    re = (Render_Engine *)data;
325    /* not used in gl engine */
326 }
327
328 static void
329 eng_rectangle_draw(void *data, void *context, void *surface, int x, int y, int w, int h)
330 {
331    Render_Engine *re;
332
333    re = (Render_Engine *)data;
334    eng_window_use(re->win);
335    re->win->gl_context->dc = context;
336    evas_gl_common_rect_draw(re->win->gl_context, x, y, w, h);
337 }
338
339 static void
340 eng_line_draw(void *data, void *context, void *surface, int x1, int y1, int x2, int y2)
341 {
342    Render_Engine *re;
343
344    re = (Render_Engine *)data;
345    re->win->gl_context->dc = context;
346    evas_gl_common_line_draw(re->win->gl_context, x1, y1, x2, y2);
347 }
348
349 static void *
350 eng_polygon_point_add(void *data, void *context, void *polygon, int x, int y)
351 {
352    Render_Engine *re;
353
354    re = (Render_Engine *)data;
355    return evas_gl_common_poly_point_add(polygon, x, y);
356
357 }
358
359 static void *
360 eng_polygon_points_clear(void *data, void *context, void *polygon)
361 {
362    Render_Engine *re;
363
364    re = (Render_Engine *)data;
365    return evas_gl_common_poly_points_clear(polygon);
366 }
367
368 static void
369 eng_polygon_draw(void *data, void *context, void *surface, void *polygon)
370 {
371    Render_Engine *re;
372
373    re = (Render_Engine *)data;
374    re->win->gl_context->dc = context;
375    evas_gl_common_poly_draw(re->win->gl_context, polygon);
376 }
377
378 static void *
379 eng_gradient_new(void *data)
380 {
381    return evas_gl_common_gradient_new();
382 }
383
384 static void
385 eng_gradient_color_stop_add(void *data, void *gradient, int r, int g, int b, int a, int delta)
386 {
387    evas_gl_common_gradient_color_stop_add(gradient, r, g, b, a, delta);
388 }
389
390 static void
391 eng_gradient_alpha_stop_add(void *data, void *gradient, int a, int delta)
392 {
393    evas_gl_common_gradient_alpha_stop_add(gradient, a, delta);
394 }
395
396 static void
397 eng_gradient_clear(void *data, void *gradient)
398 {
399    evas_gl_common_gradient_clear(gradient);
400 }
401
402 static void
403 eng_gradient_color_data_set(void *data, void *gradient, void *map, int len, int has_alpha)
404 {
405    evas_gl_common_gradient_color_data_set(gradient, map, len, has_alpha);
406 }
407
408 static void
409 eng_gradient_alpha_data_set(void *data, void *gradient, void *alpha_map, int len)
410 {
411    evas_gl_common_gradient_alpha_data_set(gradient, alpha_map, len);
412 }
413
414 static void
415 eng_gradient_free(void *data, void *gradient)
416 {
417    evas_gl_common_gradient_free(gradient);
418 }
419
420 static void
421 eng_gradient_fill_set(void *data, void *gradient, int x, int y, int w, int h)
422 {
423    evas_gl_common_gradient_fill_set(gradient, x, y, w, h);
424 }
425
426 static void
427 eng_gradient_fill_angle_set(void *data, void *gradient, double angle)
428 {
429    evas_gl_common_gradient_fill_angle_set(gradient, angle);
430 }
431
432 static void
433 eng_gradient_fill_spread_set(void *data, void *gradient, int spread)
434 {
435    evas_gl_common_gradient_fill_spread_set(gradient, spread);
436 }
437
438 static void
439 eng_gradient_angle_set(void *data, void *gradient, double angle)
440 {
441    evas_gl_common_gradient_map_angle_set(gradient, angle);
442 }
443
444 static void
445 eng_gradient_offset_set(void *data, void *gradient, float offset)
446 {
447    evas_gl_common_gradient_map_offset_set(gradient, offset);
448 }
449
450 static void
451 eng_gradient_direction_set(void *data, void *gradient, int direction)
452 {
453    evas_gl_common_gradient_map_direction_set(gradient, direction);
454 }
455
456 static void
457 eng_gradient_type_set(void *data, void *gradient, char *name, char *params)
458 {
459    evas_gl_common_gradient_type_set(gradient, name, params);
460 }
461
462 static int
463 eng_gradient_is_opaque(void *data, void *context, void *gradient, int x, int y, int w, int h)
464 {
465    Render_Engine *re = (Render_Engine *)data;
466
467    re->win->gl_context->dc = context;
468    return evas_gl_common_gradient_is_opaque(re->win->gl_context, gradient, x, y, w, h);
469 }
470
471 static int
472 eng_gradient_is_visible(void *data, void *context, void *gradient, int x, int y, int w, int h)
473 {
474    Render_Engine *re = (Render_Engine *)data;
475
476    re->win->gl_context->dc = context;
477    return evas_gl_common_gradient_is_visible(re->win->gl_context, gradient, x, y, w, h);
478 }
479
480 static void
481 eng_gradient_render_pre(void *data, void *context, void *gradient)
482 {
483    Render_Engine *re = (Render_Engine *)data;
484
485    re->win->gl_context->dc = context;
486    evas_gl_common_gradient_render_pre(re->win->gl_context, gradient);
487 }
488
489 static void
490 eng_gradient_render_post(void *data, void *gradient)
491 {
492    evas_gl_common_gradient_render_post(gradient);
493 }
494
495 static void
496 eng_gradient_draw(void *data, void *context, void *surface, void *gradient, int x, int y, int w, int h)
497 {
498    Render_Engine *re;
499
500    re = (Render_Engine *)data;
501    eng_window_use(re->win);
502    re->win->gl_context->dc = context;
503    evas_gl_common_gradient_draw(re->win->gl_context, gradient, x, y, w, h);
504 }
505
506 static int
507 eng_image_alpha_get(void *data, void *image)
508 {
509    Render_Engine *re;
510    Evas_GL_Image *im;
511
512    re = (Render_Engine *)data;
513    if (!image) return 1;
514    im = image;
515    /* FIXME: can move to gl_common */
516    switch (im->cs.space)
517      {
518       case EVAS_COLORSPACE_ARGB8888:
519         if (im->im->cache_entry.flags.alpha) return 1;
520       default:
521         break;
522      }
523    return 0;
524 }
525
526 static int
527 eng_image_colorspace_get(void *data, void *image)
528 {
529    Render_Engine *re;
530    Evas_GL_Image *im;
531    
532    re = (Render_Engine *)data;
533    if (!image) return EVAS_COLORSPACE_ARGB8888;
534    im = image;
535    return im->cs.space;
536 }
537
538 static void *
539 eng_image_alpha_set(void *data, void *image, int has_alpha)
540 {
541    Render_Engine *re;
542    Evas_GL_Image *im;
543
544    re = (Render_Engine *)data;
545    if (!image) return NULL;
546    eng_window_use(re->win);
547    im = image;
548    /* FIXME: can move to gl_common */
549    if (im->cs.space != EVAS_COLORSPACE_ARGB8888) return im;
550    if ((has_alpha) && (im->im->cache_entry.flags.alpha)) return image;
551    else if ((!has_alpha) && (!im->im->cache_entry.flags.alpha)) return image;
552    if (im->references > 1)
553     {
554         Evas_GL_Image *im_new;
555
556         im_new = evas_gl_common_image_new_from_copied_data(im->gc, im->im->cache_entry.w, im->im->cache_entry.h, im->im->image.data,
557                                                            eng_image_alpha_get(data, image),
558                                                            eng_image_colorspace_get(data, image));
559         if (!im_new) return im;
560         evas_gl_common_image_free(im);
561         im = im_new;
562      }
563    else
564      evas_gl_common_image_dirty(im);
565    im->im->cache_entry.flags.alpha = has_alpha ? 1 : 0;
566    return image;
567 }
568
569 static void *
570 eng_image_border_set(void *data, void *image, int l, int r, int t, int b)
571 {
572    Render_Engine *re;
573
574    re = (Render_Engine *)data;
575    return image;
576 }
577
578 static void
579 eng_image_border_get(void *data, void *image, int *l, int *r, int *t, int *b)
580 {
581    Render_Engine *re;
582
583    re = (Render_Engine *)data;
584 }
585
586 static char *
587 eng_image_comment_get(void *data, void *image, char *key)
588 {
589    Render_Engine *re;
590    Evas_GL_Image *im;
591
592    re = (Render_Engine *)data;
593    if (!image) return NULL;
594    im = image;
595    return im->im->info.comment;
596 }
597
598 static char *
599 eng_image_format_get(void *data, void *image)
600 {
601    Render_Engine *re;
602    Evas_GL_Image *im;
603
604    re = (Render_Engine *)data;
605    im = image;
606    return NULL;
607 }
608
609 static void
610 eng_image_colorspace_set(void *data, void *image, int cspace)
611 {
612    Render_Engine *re;
613    Evas_GL_Image *im;
614    
615    re = (Render_Engine *)data;
616    if (!image) return;
617    im = image;
618    /* FIXME: can move to gl_common */
619    if (im->cs.space == cspace) return;
620    evas_cache_image_colorspace(&im->im->cache_entry, cspace);
621    switch (cspace)
622      {
623       case EVAS_COLORSPACE_ARGB8888:
624         if (im->cs.data)
625           {
626              if (!im->cs.no_free) free(im->cs.data);
627              im->cs.data = NULL;
628              im->cs.no_free = 0;
629           }
630         break;
631       case EVAS_COLORSPACE_YCBCR422P601_PL:
632       case EVAS_COLORSPACE_YCBCR422P709_PL:
633         if (im->tex) evas_gl_common_texture_free(im->tex);
634         im->tex = NULL;
635         if (im->cs.data)
636           {
637              if (!im->cs.no_free) free(im->cs.data);
638           }
639         im->cs.data = calloc(1, im->im->cache_entry.h * sizeof(unsigned char *) * 2);
640         im->cs.no_free = 0;
641         break;
642       default:
643         abort();
644         break;
645      }
646    im->cs.space = cspace;
647 }
648
649 static void
650 eng_image_native_set(void *data, void *image, void *native)
651 {
652 }
653
654 static void *
655 eng_image_native_get(void *data, void *image)
656 {
657    return NULL;
658 }
659
660 static void *
661 eng_image_load(void *data, const char *file, const char *key, int *error, Evas_Image_Load_Opts *lo)
662 {
663    Render_Engine *re;
664
665    re = (Render_Engine *)data;
666    *error = 0;
667    eng_window_use(re->win);
668    return evas_gl_common_image_load(re->win->gl_context, file, key, lo);
669 }
670
671 static void *
672 eng_image_new_from_data(void *data, int w, int h, DATA32 *image_data, int alpha, int cspace)
673 {
674    Render_Engine *re;
675
676    re = (Render_Engine *)data;
677    eng_window_use(re->win);
678    return evas_gl_common_image_new_from_data(re->win->gl_context, w, h, image_data, alpha, cspace);
679 }
680
681 static void *
682 eng_image_new_from_copied_data(void *data, int w, int h, DATA32 *image_data, int alpha, int cspace)
683 {
684    Render_Engine *re;
685
686    re = (Render_Engine *)data;
687    eng_window_use(re->win);
688    return evas_gl_common_image_new_from_copied_data(re->win->gl_context, w, h, image_data, alpha, cspace);
689 }
690
691 static void
692 eng_image_free(void *data, void *image)
693 {
694    Render_Engine *re;
695
696    re = (Render_Engine *)data;
697    if (!image) return;
698    eng_window_use(re->win);
699    evas_gl_common_image_free(image);
700 }
701
702 static void
703 eng_image_size_get(void *data, void *image, int *w, int *h)
704 {
705    Render_Engine *re;
706
707    re = (Render_Engine *)data;
708    if (!image)
709      {
710         *w = 0;
711         *h = 0;
712         return;
713      }
714    if (w) *w = ((Evas_GL_Image *)image)->im->cache_entry.w;
715    if (h) *h = ((Evas_GL_Image *)image)->im->cache_entry.h;
716 }
717
718 static void *
719 eng_image_size_set(void *data, void *image, int w, int h)
720 {
721    Render_Engine *re;
722    Evas_GL_Image *im, *im_old;
723
724    re = (Render_Engine *)data;
725    if (!image) return NULL;
726    eng_window_use(re->win);
727    im_old = image;
728    if ((eng_image_colorspace_get(data, image) == EVAS_COLORSPACE_YCBCR422P601_PL) ||
729        (eng_image_colorspace_get(data, image) == EVAS_COLORSPACE_YCBCR422P709_PL))
730      w &= ~0x1;
731    if ((im_old) && (im_old->im->cache_entry.w == w) && (im_old->im->cache_entry.h == h))
732      return image;
733    if (im_old)
734      {
735         im = evas_gl_common_image_new(re->win->gl_context, w, h, 
736                                       eng_image_alpha_get(data, image),
737                                       eng_image_colorspace_get(data, image));
738 /*      
739         evas_common_load_image_data_from_file(im_old->im);
740         if (im_old->im->image->data)
741           {
742              evas_common_blit_rectangle(im_old->im, im->im, 0, 0, w, h, 0, 0);
743              evas_common_cpu_end_opt();
744           }
745  */
746         evas_gl_common_image_free(im_old);
747      }
748    else
749      im = evas_gl_common_image_new(re->win->gl_context, w, h, 1, EVAS_COLORSPACE_ARGB8888);
750    return im;
751 }
752
753 static void *
754 eng_image_dirty_region(void *data, void *image, int x, int y, int w, int h)
755 {
756    Render_Engine *re;
757
758    re = (Render_Engine *)data;
759    if (!image) return NULL;
760    evas_gl_common_image_dirty(image);
761    return image;
762 }
763
764 static void *
765 eng_image_data_get(void *data, void *image, int to_write, DATA32 **image_data)
766 {
767    Render_Engine *re;
768    Evas_GL_Image *im;
769
770    re = (Render_Engine *)data;
771    if (!image)
772      {
773         *image_data = NULL;
774         return NULL;
775      }
776    im = image;
777    eng_window_use(re->win);
778    evas_cache_image_load_data(&im->im->cache_entry);
779    switch (im->cs.space)
780      {
781       case EVAS_COLORSPACE_ARGB8888:
782         if (to_write)
783           {
784              if (im->references > 1)
785                {
786                   Evas_GL_Image *im_new;
787                   
788                   im_new = evas_gl_common_image_new_from_copied_data(im->gc, im->im->cache_entry.w, im->im->cache_entry.h, im->im->image.data,
789                                                                      eng_image_alpha_get(data, image),
790                                                                      eng_image_colorspace_get(data, image));
791                   if (!im_new)
792                     {
793                        *image_data = NULL;
794                        return im;
795                     }
796                   evas_gl_common_image_free(im);
797                   im = im_new;
798                }
799              else
800                evas_gl_common_image_dirty(im);
801           }
802         *image_data = im->im->image.data;
803         break;
804       case EVAS_COLORSPACE_YCBCR422P601_PL:
805       case EVAS_COLORSPACE_YCBCR422P709_PL:
806         *image_data = im->cs.data;
807         break;
808       default:
809         abort();
810         break;
811      }
812    return im;
813 }
814
815 static void *
816 eng_image_data_put(void *data, void *image, DATA32 *image_data)
817 {
818    Render_Engine *re;
819    Evas_GL_Image *im, *im2;
820
821    re = (Render_Engine *)data;
822    if (!image) return NULL;
823    im = image;
824    eng_window_use(re->win);
825    switch (im->cs.space)
826      {
827       case EVAS_COLORSPACE_ARGB8888:
828         if (image_data != im->im->image.data)
829           {
830              int w, h;
831              
832              w = im->im->cache_entry.w;
833              h = im->im->cache_entry.h;
834              im2 = eng_image_new_from_data(data, w, h, image_data,
835                                            eng_image_alpha_get(data, image),
836                                            eng_image_colorspace_get(data, image));
837              if (!im2) return im;
838              evas_gl_common_image_free(im);
839              im = im2;
840           }
841         break;
842       case EVAS_COLORSPACE_YCBCR422P601_PL:
843       case EVAS_COLORSPACE_YCBCR422P709_PL:
844         if (image_data != im->cs.data)
845           {
846              if (im->cs.data)
847                {
848                   if (!im->cs.no_free) free(im->cs.data);
849                }
850              im->cs.data = image_data;
851           }
852         break;
853       default:
854         abort();
855         break;
856      }
857    /* hmmm - but if we wrote... why bother? */
858    evas_gl_common_image_dirty(im);
859    return im;
860 }
861
862 static void
863 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)
864 {
865    Render_Engine *re;
866
867    re = (Render_Engine *)data;
868    if (!image) return;
869    eng_window_use(re->win);
870    re->win->gl_context->dc = context;
871    evas_gl_common_image_draw(re->win->gl_context, image,
872                              src_x, src_y, src_w, src_h,
873                              dst_x, dst_y, dst_w, dst_h,
874                              smooth);
875 }
876
877 static void
878 eng_font_draw(void *data, void *context, void *surface, void *font, int x, int y, int w, int h, int ow, int oh, const char *text)
879 {
880    Render_Engine *re;
881
882    re = (Render_Engine *)data;
883      {
884         static RGBA_Image *im = NULL;
885
886         if (!im)
887           im = (RGBA_Image *) evas_cache_image_empty(evas_common_image_cache_get());
888         evas_cache_image_surface_alloc(&im->cache_entry, re->win->w, re->win->h);
889
890         evas_common_draw_context_font_ext_set(context,
891                                               re->win->gl_context,
892                                               evas_gl_font_texture_new,
893                                               evas_gl_font_texture_free,
894                                               evas_gl_font_texture_draw);
895         evas_common_font_draw(im, context, font, x, y, text);
896         evas_common_draw_context_font_ext_set(context,
897                                               NULL,
898                                               NULL,
899                                               NULL,
900                                               NULL);
901      }
902 }
903
904 /* private engine functions the calling prog can use */
905
906 static Visual *
907 eng_best_visual_get(Display *disp, int screen)
908 {
909    if (!disp) return NULL;
910    if (!_evas_gl_x11_vi)
911      _evas_gl_x11_vi = glXChooseVisual(disp, screen,
912                                        _evas_gl_x11_configuration);
913    if (!_evas_gl_x11_vi) return NULL;
914    return _evas_gl_x11_vi->visual;
915 }
916
917 static Colormap
918 eng_best_colormap_get(Display *disp, int screen)
919 {
920    if (!disp) return 0;
921    if (!_evas_gl_x11_vi)
922      eng_best_visual_get(disp, screen);
923    if (!_evas_gl_x11_vi) return 0;
924    _evas_gl_x11_cmap = XCreateColormap(disp, RootWindow(disp, screen),
925                                 _evas_gl_x11_vi->visual, 0);
926    return _evas_gl_x11_cmap;
927 }
928
929 static int
930 eng_best_depth_get(Display *disp, int screen)
931 {
932    if (!disp) return 0;
933    if (!_evas_gl_x11_vi)
934      eng_best_visual_get(disp, screen);
935    if (!_evas_gl_x11_vi) return 0;
936    return _evas_gl_x11_vi->depth;
937 }
938
939 EAPI int
940 module_open(Evas_Module *em)
941 {
942    if (!em) return 0;
943    /* get whatever engine module we inherit from */
944    if (!_evas_module_engine_inherit(&pfunc, "software_generic")) return 0;
945    /* store it for later use */
946    func = pfunc;
947    /* now to override methods */
948    #define ORD(f) EVAS_API_OVERRIDE(f, &func, eng_)
949    ORD(info);
950    ORD(info_free);
951    ORD(setup);
952    ORD(output_free);
953    ORD(output_resize);
954    ORD(output_tile_size_set);
955    ORD(output_redraws_rect_add);
956    ORD(output_redraws_rect_del);
957    ORD(output_redraws_clear);
958    ORD(output_redraws_next_update_get);
959    ORD(output_redraws_next_update_push);
960    ORD(context_cutout_add);
961    ORD(context_cutout_clear);
962    ORD(output_flush);
963    ORD(output_idle_flush);
964    ORD(rectangle_draw);
965    ORD(line_draw);
966    ORD(polygon_point_add);
967    ORD(polygon_points_clear);
968    ORD(polygon_draw);
969    ORD(gradient_new);
970    ORD(gradient_free);
971    ORD(gradient_color_stop_add);
972    ORD(gradient_alpha_stop_add);
973    ORD(gradient_color_data_set);
974    ORD(gradient_alpha_data_set);
975    ORD(gradient_clear);
976    ORD(gradient_fill_set);
977    ORD(gradient_fill_angle_set);
978    ORD(gradient_fill_spread_set);
979    ORD(gradient_angle_set);
980    ORD(gradient_offset_set);
981    ORD(gradient_direction_set);
982    ORD(gradient_type_set);
983    ORD(gradient_is_opaque);
984    ORD(gradient_is_visible);
985    ORD(gradient_render_pre);
986    ORD(gradient_render_post);
987    ORD(gradient_draw);
988    ORD(image_load);
989    ORD(image_new_from_data);
990    ORD(image_new_from_copied_data);
991    ORD(image_free);
992    ORD(image_size_get);
993    ORD(image_size_set);
994    ORD(image_dirty_region);
995    ORD(image_data_get);
996    ORD(image_data_put);
997    ORD(image_alpha_set);
998    ORD(image_alpha_get);
999    ORD(image_border_set);
1000    ORD(image_border_get);
1001    ORD(image_draw);
1002    ORD(image_comment_get);
1003    ORD(image_format_get);
1004    ORD(image_colorspace_set);
1005    ORD(image_colorspace_get);
1006    ORD(image_native_set);
1007    ORD(image_native_get);
1008    ORD(font_draw);
1009    /* now advertise out own api */
1010    em->functions = (void *)(&func);
1011    return 1;
1012 }
1013
1014 EAPI void
1015 module_close(void)
1016 {
1017    
1018 }
1019
1020 EAPI Evas_Module_Api evas_modapi =
1021 {
1022    EVAS_MODULE_API_VERSION,
1023      EVAS_MODULE_TYPE_ENGINE,
1024      "gl_x11",
1025      "none"
1026 };