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