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