5624e785a6cd851522c5c5922b5aea1538d04283
[framework/uifw/evas.git] / src / modules / engines / gl_common / evas_gl_common.h
1 #ifndef EVAS_GL_COMMON_H
2 #define EVAS_GL_COMMON_H
3
4 #include "evas_common.h"
5 #include "evas_private.h"
6 #include "config.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <math.h>
13 #include <sys/time.h>
14 #include <unistd.h>
15
16 #ifdef BUILD_ENGINE_GL_GLEW
17 # include <GL/glew.h>
18 #else
19 # define GL_GLEXT_PROTOTYPES
20 #endif
21
22 #ifdef BUILD_ENGINE_GL_QUARTZ
23 # include <OpenGL/gl.h>
24 # include <OpenGL/glext.h>
25 #else
26 # ifdef _EVAS_ENGINE_SDL_H
27 #  if defined(GLES_VARIETY_S3C6410) || defined(GLES_VARIETY_SGX)
28 #   include <SDL/SDL_opengles.h>
29 #  else
30 #   include <SDL/SDL_opengl.h>
31 #  endif
32 # else
33 #  if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
34 #   if defined(GLES_VARIETY_S3C6410)
35 #    include <GLES2/gl2.h>
36 #   elif defined(GLES_VARIETY_SGX)
37 #    include <GLES2/gl2.h>
38 #    include <GLES2/gl2ext.h>
39 #   endif
40 #  else
41 #   include <GL/gl.h>
42 #   include <GL/glext.h>
43 #  endif
44 # endif
45 #endif
46
47 #ifndef GL_TEXTURE_RECTANGLE_NV
48 # define GL_TEXTURE_RECTANGLE_NV 0x84F5
49 #endif
50 #ifndef GL_BGRA
51 # define GL_BGRA 0x80E1
52 #endif
53
54 #ifndef EGL_NO_CONTEXT
55 # define EGL_NO_CONTEXT 0
56 #endif
57 #ifndef EGL_NONE
58 # define EGL_NONE 0x3038
59 #endif
60 #ifndef EGL_TRUE
61 # define EGL_TRUE 1
62 #endif
63 #ifndef EGL_FALSE
64 # define EGL_FALSE 0
65 #endif
66
67 #ifndef EGL_MAP_GL_TEXTURE_2D_SEC
68 # define EGL_MAP_GL_TEXTURE_2D_SEC 0x3201
69 #endif
70 #ifndef  EGL_MAP_GL_TEXTURE_HEIGHT_SEC
71 # define EGL_MAP_GL_TEXTURE_HEIGHT_SEC 0x3202
72 #endif
73 #ifndef EGL_MAP_GL_TEXTURE_WIDTH_SEC
74 # define EGL_MAP_GL_TEXTURE_WIDTH_SEC 0x3203
75 #endif
76 #ifndef EGL_MAP_GL_TEXTURE_FORMAT_SEC
77 # define EGL_MAP_GL_TEXTURE_FORMAT_SEC 0x3204
78 #endif
79 #ifndef EGL_MAP_GL_TEXTURE_RGB_SEC
80 # define EGL_MAP_GL_TEXTURE_RGB_SEC 0x3205
81 #endif
82 #ifndef EGL_MAP_GL_TEXTURE_RGBA_SEC
83 # define EGL_MAP_GL_TEXTURE_RGBA_SEC 0x3206
84 #endif
85 #ifndef EGL_MAP_GL_TEXTURE_PIXEL_TYPE_SEC
86 # define EGL_MAP_GL_TEXTURE_PIXEL_TYPE_SEC 0x3206
87 #endif
88 #ifndef EGL_MAP_GL_TEXTURE_UNSIGNED_BYTE_SEC
89 # define EGL_MAP_GL_TEXTURE_UNSIGNED_BYTE_SEC 0x3207
90 #endif
91 #ifndef EGL_MAP_GL_TEXTURE_STRIDE_IN_BYTES_SEC
92 # define EGL_MAP_GL_TEXTURE_STRIDE_IN_BYTES_SEC 0x3208
93 #endif
94
95 #define SHAD_VERTEX 0
96 #define SHAD_COLOR  1
97 #define SHAD_TEXUV  2
98 #define SHAD_TEXUV2 3
99 #define SHAD_TEXUV3 4
100
101 typedef struct _Evas_GL_Program                      Evas_GL_Program;
102 typedef struct _Evas_GL_Program_Source               Evas_GL_Program_Source;
103 typedef struct _Evas_GL_Shared                       Evas_GL_Shared;
104 typedef struct _Evas_GL_Context                      Evas_GL_Context;
105 typedef struct _Evas_GL_Texture_Pool                 Evas_GL_Texture_Pool;
106 typedef struct _Evas_GL_Texture                      Evas_GL_Texture;
107 typedef struct _Evas_GL_Image                        Evas_GL_Image;
108 typedef struct _Evas_GL_Font_Texture                 Evas_GL_Font_Texture;
109 typedef struct _Evas_GL_Polygon                      Evas_GL_Polygon;
110 typedef struct _Evas_GL_Polygon_Point                Evas_GL_Polygon_Point;
111
112 struct _Evas_GL_Program
113 {
114    GLuint vert, frag, prog;
115 };
116
117 struct _Evas_GL_Program_Source
118 {
119    const char *src;
120    const unsigned int *bin;
121    int bin_size;
122 };
123
124 struct _Evas_GL_Shared
125 {
126    Eina_List          *images;
127    
128    struct {
129       GLint max_texture_units;
130       GLint max_texture_size;
131       GLint max_vertex_elements;
132       GLfloat anisotropic;
133       Eina_Bool rgb : 1;
134       Eina_Bool bgra : 1;
135       Eina_Bool tex_npo2 : 1;
136       Eina_Bool tex_rect : 1;
137       Eina_Bool sec_image_map : 1;
138       // tuning params - per gpu/cpu combo?
139 #define MAX_CUTOUT             512
140 #define DEF_CUTOUT                  512
141       
142 // FIXME bug with pipes > 1 right now, should default to 32      
143 #define MAX_PIPES              128
144 #define DEF_PIPES                    32
145 #define DEF_PIPES_SGX_540            32
146 #define DEF_PIPES_TEGRA_2             1
147       
148 #define MIN_ATLAS_ALLOC         16
149 #define MAX_ATLAS_ALLOC       1024
150 #define DEF_ATLAS_ALLOC            1024
151       
152 #define MIN_ATLAS_ALLOC_ALPHA   16
153 #define MAX_ATLAS_ALLOC_ALPHA 4096
154 #define DEF_ATLAS_ALLOC_ALPHA      4096
155       
156 #define MAX_ATLAS_W            512
157 #define DEF_ATLAS_W                 512
158       
159 #define MAX_ATLAS_H            512
160 #define DEF_ATLAS_H                 512
161       
162 #define MIN_ATLAS_SLOT          16
163 #define MAX_ATLAS_SLOT         512
164 #define DEF_ATLAS_SLOT               16
165       
166       struct {
167          struct {
168             int max;
169          } cutout;
170          struct {
171             int max;
172          } pipes;
173          struct {
174             int max_alloc_size;
175             int max_alloc_alpha_size;
176             int max_w;
177             int max_h;
178             int slot_size;
179          } atlas;
180       } tune;
181    } info;
182    
183    struct {
184       Eina_List       *whole;
185       Eina_List       *atlas[33][3];
186    } tex;
187    
188    Eina_Hash          *native_hash;
189    
190    struct {
191       Evas_GL_Program  rect;
192       Evas_GL_Program  font;
193       
194       Evas_GL_Program  img,            img_nomul;
195       Evas_GL_Program  img_bgra,       img_bgra_nomul;
196       Evas_GL_Program  yuv,            yuv_nomul;
197       Evas_GL_Program  tex,            tex_nomul;
198    } shader;
199    int references;
200    int w, h;
201    int rot;
202    int mflip;
203    // persp map
204    int foc, z0, px, py;
205    int ax, ay;
206 };
207
208 #define RTYPE_RECT  1
209 #define RTYPE_IMAGE 2
210 #define RTYPE_FONT  3
211 #define RTYPE_YUV   4
212 #define RTYPE_MAP   5 /* need to merge with image */ 
213
214
215
216 struct _Evas_GL_Context
217 {
218    int                references;
219    int                w, h;
220    int                rot;
221    int                foc, z0, px, py;
222    RGBA_Draw_Context *dc;
223    
224    Evas_GL_Shared     *shared;
225
226    int flushnum;
227    struct {
228       int                top_pipe;
229       struct {
230          GLuint          cur_prog;
231          GLuint          cur_tex, cur_texu, cur_texv;
232          int             render_op;
233          int             cx, cy, cw, ch;
234          int             smooth;
235          int             blend;
236          int             clip;
237       } current;
238    } state;
239    
240    struct {
241       struct {
242          int             x, y, w, h;
243          int             type;
244       } region;
245       struct {
246          int             x, y, w, h;
247          Eina_Bool       active : 1;
248       } clip;
249       struct {
250          Evas_GL_Image  *surface;
251          GLuint          cur_prog;
252          GLuint          cur_tex, cur_texu, cur_texv;
253          int             render_op;
254          int             cx, cy, cw, ch;
255          int             smooth;
256          int             blend;
257          int             clip;
258       } shader;
259       struct {
260          int num, alloc;
261          GLshort *vertex;
262          GLubyte *color;
263          GLfloat *texuv;
264          GLfloat *texuv2;
265          GLfloat *texuv3;
266          Eina_Bool line : 1;
267          Eina_Bool use_vertex : 1;
268          Eina_Bool use_color : 1;
269          Eina_Bool use_texuv : 1;
270          Eina_Bool use_texuv2 : 1;
271          Eina_Bool use_texuv3 : 1;
272          Evas_GL_Image *im;
273       } array;
274    } pipe[MAX_PIPES];
275    
276    struct {
277       Eina_Bool size : 1;
278    } change;
279    
280    Evas_GL_Image *def_surface;
281    
282 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
283 // FIXME: hack. expose egl display to gl core for egl image sec extn.   
284    void *egldisp;
285 #endif   
286 };
287
288 struct _Evas_GL_Texture_Pool
289 {
290    Evas_GL_Context *gc;
291    GLuint           texture, fb;
292    GLuint           intformat, format, dataformat;
293    int              w, h;
294    int              references;
295    int              slot, fslot;
296    struct {
297       void         *img;
298       unsigned int *data;
299       int           w, h;
300       int           stride;
301    } dyn;
302    Eina_List       *allocations;
303    Eina_Bool        whole : 1;
304    Eina_Bool        render : 1;
305    Eina_Bool        native : 1;
306    Eina_Bool        dynamic : 1;
307 };
308
309 struct _Evas_GL_Texture
310 {
311    Evas_GL_Context *gc;
312    Evas_GL_Image   *im;
313    Evas_GL_Texture_Pool *pt, *ptu, *ptv;
314    int              x, y, w, h;
315    double           sx1, sy1, sx2, sy2;
316    int              references;
317    
318    Eina_Bool        alpha : 1;
319 };
320
321 struct _Evas_GL_Image
322 {
323    Evas_GL_Context *gc;
324    RGBA_Image      *im;
325    Evas_GL_Texture *tex;
326    RGBA_Image_Loadopts load_opts;
327    int              references;
328    // if im->im == NULL, it's a render-surface so these here are used
329    int              w, h;
330    struct {
331       int           space;
332       void         *data;
333       unsigned char no_free : 1;
334    } cs;
335    
336    struct {
337       void         *data;
338       struct {
339          void     (*bind)   (void *data, void *image);
340          void     (*unbind) (void *data, void *image);
341          void     (*free)   (void *data, void *image);
342          void      *data;
343       } func;
344       int           yinvert;
345       int           target;
346       int           mipmap;
347       unsigned char loose : 1;
348    } native;
349
350    int scale_hint, content_hint;
351    
352    unsigned char    dirty : 1;
353    unsigned char    cached : 1;
354    unsigned char    alpha : 1;
355    unsigned char    tex_only : 1;
356 };
357
358 struct _Evas_GL_Font_Texture
359 {
360    Evas_GL_Texture *tex;
361 };
362
363 struct _Evas_GL_Polygon
364 {
365    Eina_List *points;
366    Eina_Bool  changed : 1;
367 };
368
369 struct _Evas_GL_Polygon_Point
370 {
371    int x, y;
372 };
373
374 extern Evas_GL_Program_Source shader_rect_frag_src;
375 extern Evas_GL_Program_Source shader_rect_vert_src;
376 extern Evas_GL_Program_Source shader_font_frag_src;
377 extern Evas_GL_Program_Source shader_font_vert_src;
378
379 extern Evas_GL_Program_Source shader_img_frag_src;
380 extern Evas_GL_Program_Source shader_img_vert_src;
381 extern Evas_GL_Program_Source shader_img_nomul_frag_src;
382 extern Evas_GL_Program_Source shader_img_nomul_vert_src;
383 extern Evas_GL_Program_Source shader_img_bgra_frag_src;
384 extern Evas_GL_Program_Source shader_img_bgra_vert_src;
385 extern Evas_GL_Program_Source shader_img_bgra_nomul_frag_src;
386 extern Evas_GL_Program_Source shader_img_bgra_nomul_vert_src;
387
388 extern Evas_GL_Program_Source shader_yuv_frag_src;
389 extern Evas_GL_Program_Source shader_yuv_vert_src;
390 extern Evas_GL_Program_Source shader_yuv_nomul_frag_src;
391 extern Evas_GL_Program_Source shader_yuv_nomul_vert_src;
392
393 extern Evas_GL_Program_Source shader_tex_frag_src;
394 extern Evas_GL_Program_Source shader_tex_vert_src;
395 extern Evas_GL_Program_Source shader_tex_nomul_frag_src;
396 extern Evas_GL_Program_Source shader_tex_nomul_vert_src;
397
398 void glerr(int err, const char *file, const char *func, int line, const char *op);
399  
400 Evas_GL_Context  *evas_gl_common_context_new(void);
401 void              evas_gl_common_context_free(Evas_GL_Context *gc);
402 void              evas_gl_common_context_use(Evas_GL_Context *gc);
403 void              evas_gl_common_context_newframe(Evas_GL_Context *gc);
404 void              evas_gl_common_context_resize(Evas_GL_Context *gc, int w, int h, int rot);
405 void              evas_gl_common_context_target_surface_set(Evas_GL_Context *gc, Evas_GL_Image *surface);
406
407 void              evas_gl_common_context_line_push(Evas_GL_Context *gc,
408                                                    int x1, int y1, int x2, int y2,
409                                                    int clip, int cx, int cy, int cw, int ch,
410                                                    int r, int g, int b, int a);
411 void              evas_gl_common_context_rectangle_push(Evas_GL_Context *gc,
412                                                         int x, int y, int w, int h,
413                                                         int r, int g, int b, int a);
414 void              evas_gl_common_context_image_push(Evas_GL_Context *gc,
415                                                     Evas_GL_Texture *tex,
416                                                     double sx, double sy, double sw, double sh,
417                                                     int x, int y, int w, int h,
418                                                     int r, int g, int b, int a,
419                                                     Eina_Bool smooth, Eina_Bool tex_only);
420 void              evas_gl_common_context_font_push(Evas_GL_Context *gc,
421                                                    Evas_GL_Texture *tex,
422                                                    double sx, double sy, double sw, double sh,
423                                                    int x, int y, int w, int h,
424                                                    int r, int g, int b, int a);
425 void             evas_gl_common_context_yuv_push(Evas_GL_Context *gc,
426                                                  Evas_GL_Texture *tex,
427                                                  double sx, double sy, double sw, double sh,
428                                                  int x, int y, int w, int h,
429                                                  int r, int g, int b, int a,
430                                                  Eina_Bool smooth);
431 void             evas_gl_common_context_image_map4_push(Evas_GL_Context *gc,
432                                                         Evas_GL_Texture *tex,
433                                                         RGBA_Map_Point *p,
434                                                         int clip, int cx, int cy, int cw, int ch,
435                                                         int r, int g, int b, int a,
436                                                         Eina_Bool smooth, 
437                                                         Eina_Bool tex_only,
438                                                         Eina_Bool yuv);
439 void              evas_gl_common_context_flush(Evas_GL_Context *gc);
440
441 int               evas_gl_common_shader_program_init(Evas_GL_Program *p,
442                                                      Evas_GL_Program_Source *vert,
443                                                      Evas_GL_Program_Source *frag,
444                                                      const char *name);
445 void              evas_gl_common_shader_program_shutdown(Evas_GL_Program *p);
446     
447 void              evas_gl_common_rect_draw(Evas_GL_Context *gc, int x, int y, int w, int h);
448
449 Evas_GL_Texture  *evas_gl_common_texture_new(Evas_GL_Context *gc, RGBA_Image *im);
450 Evas_GL_Texture  *evas_gl_common_texture_native_new(Evas_GL_Context *gc, unsigned int w, unsigned int h, int alpha, Evas_GL_Image *im);
451 Evas_GL_Texture  *evas_gl_common_texture_render_new(Evas_GL_Context *gc, unsigned int w, unsigned int h, int alpha);
452 Evas_GL_Texture  *evas_gl_common_texture_dynamic_new(Evas_GL_Context *gc, Evas_GL_Image *im);
453 void              evas_gl_common_texture_update(Evas_GL_Texture *tex, RGBA_Image *im);
454 void              evas_gl_common_texture_free(Evas_GL_Texture *tex);
455 Evas_GL_Texture  *evas_gl_common_texture_alpha_new(Evas_GL_Context *gc, DATA8 *pixels, unsigned int w, unsigned int h, int fh);
456 void              evas_gl_common_texture_alpha_update(Evas_GL_Texture *tex, DATA8 *pixels, unsigned int w, unsigned int h, int fh);
457 Evas_GL_Texture  *evas_gl_common_texture_yuv_new(Evas_GL_Context *gc, DATA8 **rows, unsigned int w, unsigned int h);
458 void              evas_gl_common_texture_yuv_update(Evas_GL_Texture *tex, DATA8 **rows, unsigned int w, unsigned int h);
459
460 void              evas_gl_common_image_all_unload(Evas_GL_Context *gc);
461
462 Evas_GL_Image    *evas_gl_common_image_load(Evas_GL_Context *gc, const char *file, const char *key, Evas_Image_Load_Opts *lo, int *error);
463 Evas_GL_Image    *evas_gl_common_image_new_from_data(Evas_GL_Context *gc, unsigned int w, unsigned int h, DATA32 *data, int alpha, int cspace);
464 Evas_GL_Image    *evas_gl_common_image_new_from_copied_data(Evas_GL_Context *gc, unsigned int w, unsigned int h, DATA32 *data, int alpha, int cspace);
465 Evas_GL_Image    *evas_gl_common_image_new(Evas_GL_Context *gc, unsigned int w, unsigned int h, int alpha, int cspace);
466 Evas_GL_Image    *evas_gl_common_image_alpha_set(Evas_GL_Image *im, int alpha);
467 void              evas_gl_common_image_native_enable(Evas_GL_Image *im);
468 void              evas_gl_common_image_native_disable(Evas_GL_Image *im);
469 void              evas_gl_common_image_scale_hint_set(Evas_GL_Image *im, int hint);
470 void              evas_gl_common_image_content_hint_set(Evas_GL_Image *im, int hint);
471 void              evas_gl_common_image_free(Evas_GL_Image *im);
472 Evas_GL_Image    *evas_gl_common_image_surface_new(Evas_GL_Context *gc, unsigned int w, unsigned int h, int alpha);
473 void              evas_gl_common_image_dirty(Evas_GL_Image *im, unsigned int x, unsigned int y, unsigned int w, unsigned int h);
474 void              evas_gl_common_image_map4_draw(Evas_GL_Context *gc, Evas_GL_Image *im, RGBA_Map_Point *p, int smooth, int level);
475 void              evas_gl_common_image_draw(Evas_GL_Context *gc, Evas_GL_Image *im, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, int smooth);
476
477 void             *evas_gl_font_texture_new(void *gc, RGBA_Font_Glyph *fg);
478 void              evas_gl_font_texture_free(void *);
479 void              evas_gl_font_texture_draw(void *gc, void *surface, void *dc, RGBA_Font_Glyph *fg, int x, int y);
480
481 Evas_GL_Polygon  *evas_gl_common_poly_point_add(Evas_GL_Polygon *poly, int x, int y);
482 Evas_GL_Polygon  *evas_gl_common_poly_points_clear(Evas_GL_Polygon *poly);
483 void              evas_gl_common_poly_draw(Evas_GL_Context *gc, Evas_GL_Polygon *poly, int x, int y);
484
485 void              evas_gl_common_line_draw(Evas_GL_Context *gc, int x1, int y1, int x2, int y2);
486
487 extern void (*glsym_glGenFramebuffers)      (GLsizei a, GLuint *b);
488 extern void (*glsym_glBindFramebuffer)      (GLenum a, GLuint b);
489 extern void (*glsym_glFramebufferTexture2D) (GLenum a, GLenum b, GLenum c, GLuint d, GLint e);
490 extern void (*glsym_glDeleteFramebuffers)   (GLsizei a, const GLuint *b);
491
492 #if defined (GLES_VARIETY_S3C6410) || defined (GLES_VARIETY_SGX)
493 extern void          *(*secsym_eglCreateImage)               (void *a, void *b, GLenum c, void *d, const int *e);
494 extern unsigned int   (*secsym_eglDestroyImage)              (void *a, void *b);
495 extern void           (*secsym_glEGLImageTargetTexture2DOES) (int a, void *b);
496 extern void          *(*secsym_eglMapImageSEC)               (void *a, void *b);
497 extern unsigned int   (*secsym_eglUnmapImageSEC)             (void *a, void *b);
498 extern unsigned int   (*secsym_eglGetImageAttribSEC)         (void *a, void *b, int c, int *d);
499 #endif
500
501 #define GL_ERRORS 1
502
503 #ifdef GL_ERRORS
504 # define GLERR(fn, fl, ln, op) \
505    { \
506       int __gl_err = glGetError(); \
507       if (__gl_err != GL_NO_ERROR) glerr(__gl_err, fl, fn, ln, op); \
508    }
509 #else
510 # define GLERR(fn, fl, ln, op)
511 #endif
512
513 Eina_Bool evas_gl_common_module_open(void);
514 void      evas_gl_common_module_close(void);
515
516 #endif