dx50 = DX50
[mplayer/glamo.git] / libvo / vo_gl2.c
blob4b249e777b4128636b4e7ec4183b3e4e3e573218
1 /*
2 * video_out_gl.c, X11/OpenGL interface
3 * based on video_out_x11 by Aaron Holtzman,
4 * and WS opengl window manager by Pontscho/Fresh!
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <math.h>
11 #include <errno.h>
13 #include "config.h"
14 #include "mp_msg.h"
15 #include "video_out.h"
16 #include "video_out_internal.h"
17 #include "sub.h"
19 #include <GL/gl.h>
20 #ifdef GL_WIN32
21 #include <windows.h>
22 #include <GL/glext.h>
23 #else
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <GL/glx.h>
27 #endif
28 #include <errno.h>
30 #ifdef GL_WIN32
31 #include "w32_common.h"
32 #else
33 #include "x11_common.h"
34 #endif
35 #include "aspect.h"
37 #define NDEBUG
38 //#undef NDEBUG
40 #undef TEXTUREFORMAT_ALWAYS_RGB24
42 static vo_info_t info =
44 "X11 (OpenGL) - multiple textures version",
45 "gl2",
46 "Arpad Gereoffy & Sven Goethel",
50 LIBVO_EXTERN(gl2)
52 /* private prototypes */
54 #define MODE_BGR 1
55 #define MODE_RGB 0
57 /* local data */
58 static unsigned char *ImageDataLocal=NULL;
59 static unsigned char *ImageData=NULL;
61 /* X11 related variables */
62 //static Window vo_window;
64 //static int texture_id=1;
66 #ifndef GL_WIN32
67 static GLXContext wsGLXContext;
68 #endif
70 static uint32_t image_width;
71 static uint32_t image_height;
72 static uint32_t image_format;
73 static uint32_t image_bpp;
74 static int image_mode;
75 static uint32_t image_bytes;
77 static int int_pause;
79 static uint32_t texture_width;
80 static uint32_t texture_height;
81 static int texnumx, texnumy, memory_x_len, memory_x_start_offset, raw_line_len;
82 static GLfloat texpercx, texpercy;
83 static struct TexSquare * texgrid;
84 static GLint gl_internal_format;
85 static char * gl_internal_format_s;
86 static int rgb_sz, r_sz, g_sz, b_sz, a_sz;
87 static GLint gl_bitmap_format;
88 static char * gl_bitmap_format_s;
89 static GLint gl_bitmap_type;
90 static char * gl_bitmap_type_s;
91 static int gl_alignment;
92 static int isGL12 = GL_FALSE;
94 static int gl_bilinear=1;
95 static int gl_antialias=0;
97 static void (*draw_alpha_fnc)
98 (int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride);
101 /* The squares that are tiled to make up the game screen polygon */
103 struct TexSquare
105 GLubyte *texture;
106 GLuint texobj;
107 int isTexture;
108 GLfloat fx1, fy1, fx2, fy2, fx3, fy3, fx4, fy4;
109 GLfloat xcov, ycov;
110 int isDirty;
111 int dirtyXoff, dirtyYoff, dirtyWidth, dirtyHeight;
114 static void CalcFlatPoint(int x,int y,GLfloat *px,GLfloat *py)
116 *px=(float)x*texpercx;
117 if(*px>1.0) *px=1.0;
118 *py=(float)y*texpercy;
119 if(*py>1.0) *py=1.0;
122 static int initTextures()
124 unsigned char *line_1=0, *line_2=0, *mem_start=0;
125 struct TexSquare *tsq=0;
126 int e_x, e_y, s, i=0;
127 int x=0, y=0;
128 GLint format=0;
129 GLenum err;
131 /* achieve the 2**e_x:=texture_width, 2**e_y:=texture_height */
132 e_x=0; s=1;
133 while (s<texture_width)
134 { s*=2; e_x++; }
135 texture_width=s;
137 e_y=0; s=1;
138 while (s<texture_height)
139 { s*=2; e_y++; }
140 texture_height=s;
143 /* Test the max texture size */
146 glTexImage2D (GL_PROXY_TEXTURE_2D, 0,
147 gl_internal_format,
148 texture_width, texture_height,
149 0, gl_bitmap_format, gl_bitmap_type, NULL);
151 glGetTexLevelParameteriv
152 (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format);
154 if (format != gl_internal_format)
156 fprintf (stderr, "[gl2] Needed texture [%dx%d] too big, trying ",
157 texture_height, texture_width);
159 if (texture_width > texture_height)
161 e_x--;
162 texture_width = 1;
163 for (i = e_x; i > 0; i--)
164 texture_width *= 2;
166 else
168 e_y--;
169 texture_height = 1;
170 for (i = e_y; i > 0; i--)
171 texture_height *= 2;
174 fprintf (stderr, "[%dx%d] !\n", texture_height, texture_width);
176 if(texture_width < 64 || texture_height < 64)
178 fprintf (stderr, "GLERROR: Give up .. usable texture size not avaiable, or texture config error !\n");
179 return -1;
183 while (format != gl_internal_format && texture_width > 1 && texture_height > 1);
185 texnumx = image_width / texture_width;
186 if ((image_width % texture_width) > 0)
187 texnumx++;
189 texnumy = image_height / texture_height;
190 if ((image_height % texture_height) > 0)
191 texnumy++;
193 printf("[gl2] Creating %dx%d textures of size %dx%d ...\n",
194 texnumx, texnumy, texture_width,texture_height);
196 /* Allocate the texture memory */
198 texpercx = (GLfloat) texture_width / (GLfloat) image_width;
199 if (texpercx > 1.0)
200 texpercx = 1.0;
202 texpercy = (GLfloat) texture_height / (GLfloat) image_height;
203 if (texpercy > 1.0)
204 texpercy = 1.0;
206 texgrid = (struct TexSquare *)
207 calloc (texnumx * texnumy, sizeof (struct TexSquare));
209 line_1 = (unsigned char *) ImageDataLocal;
210 line_2 = (unsigned char *) ImageDataLocal+(image_width*image_bytes);
212 mem_start = (unsigned char *) ImageDataLocal;
214 raw_line_len = line_2 - line_1;
216 memory_x_len = raw_line_len / image_bytes;
218 #ifndef NDEBUG
219 fprintf (stderr, "[gl2] texture-usage %d*width=%d, %d*height=%d\n",
220 (int) texnumx, (int) texture_width, (int) texnumy,
221 (int) texture_height);
222 #endif
224 for (y = 0; y < texnumy; y++)
226 for (x = 0; x < texnumx; x++)
228 tsq = texgrid + y * texnumx + x;
230 if (x == texnumx - 1 && image_width % texture_width)
231 tsq->xcov =
232 (GLfloat) (image_width % texture_width) / (GLfloat) texture_width;
233 else
234 tsq->xcov = 1.0;
236 if (y == texnumy - 1 && image_height % texture_height)
237 tsq->ycov =
238 (GLfloat) (image_height % texture_height) / (GLfloat) texture_height;
239 else
240 tsq->ycov = 1.0;
242 CalcFlatPoint (x, y, &(tsq->fx1), &(tsq->fy1));
243 CalcFlatPoint (x + 1, y, &(tsq->fx2), &(tsq->fy2));
244 CalcFlatPoint (x + 1, y + 1, &(tsq->fx3), &(tsq->fy3));
245 CalcFlatPoint (x, y + 1, &(tsq->fx4), &(tsq->fy4));
247 /* calculate the pixel store data,
248 to use the machine-bitmap for our texture
250 memory_x_start_offset = 0 * image_bytes +
251 x * texture_width * image_bytes;
253 tsq->texture = line_1 +
254 y * texture_height * raw_line_len +
255 memory_x_start_offset;
257 tsq->isDirty=GL_TRUE;
258 tsq->isTexture=GL_FALSE;
259 tsq->texobj=0;
260 tsq->dirtyXoff=0; tsq->dirtyYoff=0; tsq->dirtyWidth=-1; tsq->dirtyHeight=-1;
262 glGenTextures (1, &(tsq->texobj));
264 glBindTexture (GL_TEXTURE_2D, tsq->texobj);
265 err = glGetError ();
266 if(err==GL_INVALID_ENUM)
268 fprintf (stderr, "GLERROR glBindTexture (glGenText) := GL_INVALID_ENUM, texnum x=%d, y=%d, texture=%d\n", x, y, tsq->texobj);
271 if(glIsTexture(tsq->texobj) == GL_FALSE)
273 fprintf (stderr, "GLERROR ain't a texture (glGenText): texnum x=%d, y=%d, texture=%d\n",
274 x, y, tsq->texobj);
275 } else {
276 tsq->isTexture=GL_TRUE;
279 glTexImage2D (GL_TEXTURE_2D, 0,
280 gl_internal_format,
281 texture_width, texture_height,
282 0, gl_bitmap_format, gl_bitmap_type, NULL);
284 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
286 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
287 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
289 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
290 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
292 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
294 } /* for all texnumx */
295 } /* for all texnumy */
297 return 0;
300 static void resetTexturePointers(unsigned char *imageSource)
302 unsigned char *line_1=0, *line_2=0, *mem_start=0;
303 struct TexSquare *tsq=0;
304 int x=0, y=0;
306 line_1 = (unsigned char *) imageSource;
307 line_2 = (unsigned char *) imageSource+(image_width*image_bytes);
309 mem_start = (unsigned char *) imageSource;
311 for (y = 0; y < texnumy; y++)
313 for (x = 0; x < texnumx; x++)
315 tsq = texgrid + y * texnumx + x;
317 /* calculate the pixel store data,
318 to use the machine-bitmap for our texture
320 memory_x_start_offset = 0 * image_bytes +
321 x * texture_width * image_bytes;
323 tsq->texture = line_1 +
324 y * texture_height * raw_line_len +
325 memory_x_start_offset;
327 } /* for all texnumx */
328 } /* for all texnumy */
331 static void setupTextureDirtyArea(int x, int y, int w,int h)
333 struct TexSquare *square;
334 int xi, yi, wd, ht, wh, hh;
335 int wdecr, hdecr, xh, yh;
337 wdecr=w; hdecr=h; xh=x; yh=y;
339 for (yi = 0; hdecr>0 && yi < texnumy; yi++)
341 if (yi < texnumy - 1)
342 ht = texture_height;
343 else
344 ht = image_height - texture_height * yi;
346 xh =x;
347 wdecr =w;
349 for (xi = 0; wdecr>0 && xi < texnumx; xi++)
351 square = texgrid + yi * texnumx + xi;
353 if (xi < texnumx - 1)
354 wd = texture_width;
355 else
356 wd = image_width - texture_width * xi;
358 if( 0 <= xh && xh < wd &&
359 0 <= yh && yh < ht
362 square->isDirty=GL_TRUE;
364 wh=(wdecr<wd)?wdecr:wd-xh;
365 if(wh<0) wh=0;
367 hh=(hdecr<ht)?hdecr:ht-yh;
368 if(hh<0) hh=0;
371 #ifndef NDEBUG
372 printf("\t %dx%d, %d/%d (%dx%d): %d/%d (%dx%d)\n",
373 xi, yi, xh, yh, wdecr, hdecr, xh, yh, wh, hh);
374 #endif
377 if(xh<square->dirtyXoff)
378 square->dirtyXoff=xh;
380 if(yh<square->dirtyYoff)
381 square->dirtyYoff=yh;
383 square->dirtyWidth = wd-square->dirtyXoff;
384 square->dirtyHeight = ht-square->dirtyYoff;
386 wdecr-=wh;
388 if ( xi == texnumx - 1 )
389 hdecr-=hh;
392 xh-=wd;
393 if(xh<0) xh=0;
395 yh-=ht;
396 if(yh<0) yh=0;
400 static void gl_set_bilinear (int val)
402 int x, y;
404 if(val>=0)
405 gl_bilinear = val;
406 else
407 gl_bilinear++;
409 gl_bilinear=gl_bilinear%2;
410 /* no mipmap yet .. */
412 for (y = 0; y < texnumy; y++)
414 for (x = 0; x < texnumx; x++)
416 glBindTexture (GL_TEXTURE_2D, texgrid[y * texnumx + x].texobj);
418 switch (gl_bilinear)
420 case 0:
421 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
422 GL_NEAREST);
423 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
424 GL_NEAREST);
425 printf("[gl2] bilinear off\n");
426 break;
427 case 1:
428 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
429 GL_LINEAR);
430 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
431 GL_LINEAR);
432 printf("[gl2] bilinear linear\n");
433 break;
434 case 2:
435 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
436 GL_LINEAR_MIPMAP_NEAREST);
437 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
438 GL_LINEAR_MIPMAP_NEAREST);
439 printf("[gl2] bilinear mipmap nearest\n");
440 break;
441 case 3:
442 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
443 GL_LINEAR_MIPMAP_LINEAR);
444 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
445 GL_LINEAR_MIPMAP_LINEAR);
446 printf("[gl2] bilinear mipmap linear\n");
447 break;
451 fflush(0);
454 static void gl_set_antialias (int val)
456 gl_antialias=val;
458 if (gl_antialias)
460 glShadeModel (GL_SMOOTH);
461 glEnable (GL_POLYGON_SMOOTH);
462 glEnable (GL_LINE_SMOOTH);
463 glEnable (GL_POINT_SMOOTH);
464 printf("[gl2] antialiasing on\n");
466 else
468 glShadeModel (GL_FLAT);
469 glDisable (GL_POLYGON_SMOOTH);
470 glDisable (GL_LINE_SMOOTH);
471 glDisable (GL_POINT_SMOOTH);
472 printf("[gl2] antialiasing off\n");
474 fflush(0);
478 static void drawTextureDisplay ()
480 struct TexSquare *square;
481 int x, y/*, xoff=0, yoff=0, wd, ht*/;
482 GLenum err;
484 glColor3f(1.0,1.0,1.0);
486 for (y = 0; y < texnumy; y++)
488 for (x = 0; x < texnumx; x++)
490 square = texgrid + y * texnumx + x;
492 if(square->isTexture==GL_FALSE)
494 #ifndef NDEBUG
495 fprintf (stderr, "[gl2] ain't a texture(update): texnum x=%d, y=%d, texture=%d\n",
496 x, y, square->texobj);
497 #endif
498 continue;
501 glBindTexture (GL_TEXTURE_2D, square->texobj);
502 err = glGetError ();
503 if(err==GL_INVALID_ENUM)
505 fprintf (stderr, "GLERROR glBindTexture := GL_INVALID_ENUM, texnum x=%d, y=%d, texture=%d\n", x, y, square->texobj);
507 #ifndef NDEBUG
508 else if(err==GL_INVALID_OPERATION) {
509 fprintf (stderr, "GLERROR glBindTexture := GL_INVALID_OPERATION, texnum x=%d, y=%d, texture=%d\n", x, y, square->texobj);
511 #endif
513 if(glIsTexture(square->texobj) == GL_FALSE)
515 square->isTexture=GL_FALSE;
516 fprintf (stderr, "GLERROR ain't a texture(update): texnum x=%d, y=%d, texture=%d\n",
517 x, y, square->texobj);
520 if(square->isDirty)
522 glTexSubImage2D (GL_TEXTURE_2D, 0,
523 square->dirtyXoff, square->dirtyYoff,
524 square->dirtyWidth, square->dirtyHeight,
525 gl_bitmap_format, gl_bitmap_type, square->texture);
527 square->isDirty=GL_FALSE;
528 square->dirtyXoff=0; square->dirtyYoff=0; square->dirtyWidth=-1; square->dirtyHeight=-1;
531 #ifndef NDEBUG
532 fprintf (stdout, "[gl2] glTexSubImage2D texnum x=%d, y=%d, %d/%d - %d/%d\n",
533 x, y, square->dirtyXoff, square->dirtyYoff, square->dirtyWidth, square->dirtyHeight);
534 #endif
536 glBegin(GL_QUADS);
538 glTexCoord2f (0, 0);
539 glVertex2f (square->fx1, square->fy1);
541 glTexCoord2f (0, square->ycov);
542 glVertex2f (square->fx4, square->fy4);
544 glTexCoord2f (square->xcov, square->ycov);
545 glVertex2f (square->fx3, square->fy3);
547 glTexCoord2f (square->xcov, 0);
548 glVertex2f (square->fx2, square->fy2);
550 glEnd();
552 #ifndef NDEBUG
553 fprintf (stdout, "[gl2] GL_QUADS texnum x=%d, y=%d, %f/%f %f/%f %f/%f %f/%f\n\n", x, y, square->fx1, square->fy1, square->fx4, square->fy4,
554 square->fx3, square->fy3, square->fx2, square->fy2);
555 #endif
557 } /* for all texnumx */
558 } /* for all texnumy */
560 /* YES - let's catch this error ...
562 (void) glGetError ();
566 static void resize(int *x,int *y){
567 mp_msg(MSGT_VO,MSGL_V,"[gl2] Resize: %dx%d\n",*x,*y);
568 if( vo_fs )
570 glClear(GL_COLOR_BUFFER_BIT);
571 aspect(x, y, A_ZOOM);
572 glViewport( (vo_screenwidth-*x)/2, (vo_screenheight-*y)/2, *x, *y);
573 } else {
574 //aspect(x, y, A_NOZOOM);
575 glViewport( 0, 0, *x, *y );
578 glMatrixMode(GL_PROJECTION);
579 glLoadIdentity();
580 glOrtho (0, 1, 1, 0, -1.0, 1.0);
582 glMatrixMode(GL_MODELVIEW);
583 glLoadIdentity();
586 static void draw_alpha_32(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
587 vo_draw_alpha_rgb32(w,h,src,srca,stride,ImageData+4*(y0*image_width+x0),4*image_width);
590 static void draw_alpha_24(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
591 vo_draw_alpha_rgb24(w,h,src,srca,stride,ImageData+3*(y0*image_width+x0),3*image_width);
594 static void draw_alpha_16(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
595 vo_draw_alpha_rgb16(w,h,src,srca,stride,ImageData+2*(y0*image_width+x0),2*image_width);
598 static void draw_alpha_15(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
599 vo_draw_alpha_rgb15(w,h,src,srca,stride,ImageData+2*(y0*image_width+x0),2*image_width);
602 static void draw_alpha_null(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
605 #ifdef GL_WIN32
607 static int config_w32(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format) {
608 PIXELFORMATDESCRIPTOR pfd;
609 int pf;
611 o_dwidth = d_width;
612 o_dheight = d_height;
613 vo_fs = flags & VOFLAG_FULLSCREEN;
614 vo_vm = flags & VOFLAG_MODESWITCHING;
616 vo_dwidth = d_width;
617 vo_dheight = d_height;
619 destroyRenderingContext();
620 if (!createRenderingContext())
621 return -1;
623 if (vo_fs)
624 aspect(&d_width, &d_height, A_ZOOM);
626 pf = GetPixelFormat(vo_hdc);
627 if (!DescribePixelFormat(vo_hdc, pf, sizeof pfd, &pfd)) {
628 r_sz = g_sz = b_sz = a_sz = 0;
629 } else {
630 r_sz = pfd.cRedBits;
631 g_sz = pfd.cGreenBits;
632 b_sz = pfd.cBlueBits;
633 a_sz = pfd.cAlphaBits;
636 return 0;
639 #else
641 static int choose_glx_visual(Display *dpy, int scr, XVisualInfo *res_vi)
643 XVisualInfo template, *vi_list;
644 int vi_num, i, best_i, best_weight;
646 template.screen = scr;
647 vi_list = XGetVisualInfo(dpy, VisualScreenMask, &template, &vi_num);
648 if (!vi_list) return -1;
649 best_weight = 1000000; best_i=0;
650 for (i = 0; i < vi_num; i++) {
651 int val, res, w = 0;
652 /* of course, the visual must support OpenGL rendering... */
653 res = glXGetConfig(dpy, vi_list + i, GLX_USE_GL, &val);
654 if (res || val == False) continue;
655 /* also it must be doublebuffered ... */
656 res = glXGetConfig(dpy, vi_list + i, GLX_DOUBLEBUFFER, &val);
657 if (res || val == False) continue;
658 /* furthermore it must be RGBA (not color indexed) ... */
659 res = glXGetConfig(dpy, vi_list + i, GLX_RGBA, &val);
660 if (res || val == False) continue;
661 /* prefer less depth buffer size, */
662 res = glXGetConfig(dpy, vi_list + i, GLX_DEPTH_SIZE, &val);
663 if (res) continue;
664 w += val*2;
665 /* stencil buffer size */
666 res = glXGetConfig(dpy, vi_list + i, GLX_STENCIL_SIZE, &val);
667 if (res) continue;
668 w += val*2;
669 /* and colorbuffer alpha size */
670 res = glXGetConfig(dpy, vi_list + i, GLX_ALPHA_SIZE, &val);
671 if (res) continue;
672 w += val;
673 /* and finally, prefer DirectColor-ed visuals to allow color corrections */
674 if (vi_list[i].class != DirectColor) w += 100;
675 if (w < best_weight) {
676 best_weight = w;
677 best_i = i;
680 if (best_weight < 1000000) *res_vi = vi_list[best_i];
681 XFree(vi_list);
682 return (best_weight < 1000000) ? 0 : -1;
685 static uint32_t config_glx(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format) {
686 XSizeHints hint;
687 XVisualInfo *vinfo, vinfo_buf;
688 XEvent xev;
690 if( flags&0x01 )
692 vo_fs = VO_TRUE;
693 aspect(&d_width,&d_height,A_ZOOM);
694 hint.x = 0;
695 hint.y = 0;
696 hint.width = vo_screenwidth;
697 hint.height = vo_screenheight;
698 hint.flags = PPosition | PSize;
699 } else {
700 vo_fs = VO_FALSE;
701 hint.x = 0;
702 hint.y = 0;
703 hint.width = d_width;
704 hint.height = d_height;
705 hint.flags = PPosition | PSize;
708 /* Make the window */
710 // XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
712 // XMatchVisualInfo(mDisplay, screen, depth, TrueColor, &vinfo);
713 vinfo = choose_glx_visual(mDisplay,mScreen,&vinfo_buf) < 0 ? NULL : &vinfo_buf;
714 if (vinfo == NULL)
716 printf("[gl2] no GLX support present\n");
717 return -1;
720 if ( vo_window == None )
722 vo_window = vo_x11_create_smooth_window(mDisplay, RootWindow(mDisplay,mScreen),
723 vinfo->visual, hint.x, hint.y, hint.width, hint.height, vinfo->depth, vo_x11_create_colormap(vinfo));
724 if ( flags&0x01 ) vo_x11_decoration( mDisplay,vo_window,0 );
726 XSelectInput(mDisplay, vo_window, StructureNotifyMask);
728 /* Tell other applications about this window */
730 XSetStandardProperties(mDisplay, vo_window, title, title, None, NULL, 0, &hint);
732 /* Map window. */
733 XMapWindow(mDisplay, vo_window);
734 #ifdef HAVE_XINERAMA
735 vo_x11_xinerama_move(mDisplay,vo_window);
736 #endif
737 vo_x11_sizehint( hint.x, hint.y, hint.width, hint.height,0 );
738 XClearWindow(mDisplay,vo_window);
740 /* Wait for map. */
743 XNextEvent(mDisplay, &xev);
745 while (xev.type != MapNotify || xev.xmap.event != vo_window);
747 XSelectInput(mDisplay, vo_window, NoEventMask);
750 else {
751 vo_x11_sizehint( hint.x, hint.y, hint.width, hint.height,0 );
752 if ( !(flags&1) ) XMoveResizeWindow( mDisplay,vo_window,hint.x,hint.y,hint.width,hint.height );
755 vo_x11_classhint( mDisplay,vo_window,"gl2" );
756 vo_hidecursor(mDisplay,vo_window);
758 if ( vo_config_count ) glXDestroyContext( mDisplay,wsGLXContext );
760 wsGLXContext=glXCreateContext( mDisplay,vinfo,NULL,True );
762 glXMakeCurrent( mDisplay,vo_window,wsGLXContext );
764 XFlush(mDisplay);
765 XSync(mDisplay, False);
767 //XSelectInput(mDisplay, vo_window, StructureNotifyMask); // !!!!
768 vo_x11_selectinput_witherr(mDisplay, vo_window, StructureNotifyMask | KeyPressMask | PointerMotionMask
769 | ButtonPressMask | ButtonReleaseMask | ExposureMask
772 if(glXGetConfig(mDisplay,vinfo,GLX_RED_SIZE, &r_sz)!=0)
773 r_sz=0;
774 if(glXGetConfig(mDisplay,vinfo,GLX_GREEN_SIZE, &g_sz)!=0)
775 g_sz=0;
776 if(glXGetConfig(mDisplay,vinfo,GLX_BLUE_SIZE, &b_sz)!=0)
777 b_sz=0;
778 if(glXGetConfig(mDisplay,vinfo,GLX_ALPHA_SIZE, &a_sz)!=0)
779 a_sz=0;
781 return 0;
784 #endif
786 static int initGl(uint32_t d_width, uint32_t d_height)
788 ImageDataLocal=malloc(image_width*image_height*image_bytes);
789 memset(ImageDataLocal,128,image_width*image_height*image_bytes);
791 ImageData=ImageDataLocal;
793 texture_width=image_width;
794 texture_height=image_height;
796 if (initTextures() < 0)
797 return -1;
799 glDisable(GL_BLEND);
800 glDisable(GL_DEPTH_TEST);
801 glDepthMask(GL_FALSE);
802 glDisable(GL_CULL_FACE);
804 glPixelStorei (GL_UNPACK_ROW_LENGTH, memory_x_len);
807 * may give a little speed up for a kinda burst read ..
809 if( (image_width*image_bpp)%8 == 0 )
810 gl_alignment=8;
811 else if( (image_width*image_bpp)%4 == 0 )
812 gl_alignment=4;
813 else if( (image_width*image_bpp)%2 == 0 )
814 gl_alignment=2;
815 else
816 gl_alignment=1;
818 glPixelStorei (GL_UNPACK_ALIGNMENT, gl_alignment);
820 glEnable (GL_TEXTURE_2D);
822 gl_set_antialias(0);
823 gl_set_bilinear(1);
825 drawTextureDisplay ();
827 printf("[gl2] Using image_bpp=%d, image_bytes=%d, isBGR=%d, \n\tgl_bitmap_format=%s, gl_bitmap_type=%s, \n\tgl_alignment=%d, rgb_size=%d (%d,%d,%d), a_sz=%d, \n\tgl_internal_format=%s\n",
828 image_bpp, image_bytes, image_mode==MODE_BGR,
829 gl_bitmap_format_s, gl_bitmap_type_s, gl_alignment,
830 rgb_sz, r_sz, g_sz, b_sz, a_sz, gl_internal_format_s);
832 resize(&d_width, &d_height);
834 glClearColor( 0.0f,0.0f,0.0f,0.0f );
835 glClear( GL_COLOR_BUFFER_BIT );
837 return 0;
840 /* connect to server, create and map window,
841 * allocate colors and (shared) memory
843 static uint32_t
844 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
846 const unsigned char * glVersion;
848 image_height = height;
849 image_width = width;
850 vo_dwidth = d_width;
851 vo_dheight = d_height;
852 image_format = format;
854 int_pause = 0;
856 aspect_save_orig(width,height);
857 aspect_save_prescale(d_width,d_height);
858 aspect_save_screenres(vo_screenwidth,vo_screenheight);
860 aspect(&d_width,&d_height,A_NOZOOM);
862 #ifdef GL_WIN32
863 if (config_w32(width, height, d_width, d_height, flags, title, format) == -1)
864 #else
865 if (config_glx(width, height, d_width, d_height, flags, title, format) == -1)
866 #endif
867 return -1;
869 glVersion = glGetString(GL_VERSION);
871 printf("[gl2] OpenGL Driver Information:\n");
872 printf("\tvendor: %s,\n\trenderer %s,\n\tversion %s\n",
873 glGetString(GL_VENDOR),
874 glGetString(GL_RENDERER),
875 glVersion);
877 if(glVersion[0]>'1' ||
878 (glVersion[0]=='1' && glVersion[2]>='2') )
879 isGL12 = GL_TRUE;
880 else
881 isGL12 = GL_FALSE;
883 if(isGL12)
885 printf("[gl2] You have OpenGL >= 1.2 capable drivers, GOOD (16bpp and BGR is ok!)\n");
886 } else {
887 printf("[gl2] You have OpenGL < 1.2 drivers, BAD (16bpp and BGR may be damaged!)\n");
890 rgb_sz=r_sz+g_sz+b_sz;
891 if(rgb_sz<=0) rgb_sz=24;
893 if(r_sz==3 && g_sz==3 && b_sz==2 && a_sz==0) {
894 gl_internal_format=GL_R3_G3_B2;
895 gl_internal_format_s="GL_R3_G3_B2";
896 } else if(r_sz==4 && g_sz==4 && b_sz==4 && a_sz==0) {
897 gl_internal_format=GL_RGB4;
898 gl_internal_format_s="GL_RGB4";
899 } else if(r_sz==5 && g_sz==5 && b_sz==5 && a_sz==0) {
900 gl_internal_format=GL_RGB5;
901 gl_internal_format_s="GL_RGB5";
902 } else if(r_sz==8 && g_sz==8 && b_sz==8 && a_sz==0) {
903 gl_internal_format=GL_RGB8;
904 gl_internal_format_s="GL_RGB8";
905 } else if(r_sz==10 && g_sz==10 && b_sz==10 && a_sz==0) {
906 gl_internal_format=GL_RGB10;
907 gl_internal_format_s="GL_RGB10";
908 } else if(r_sz==2 && g_sz==2 && b_sz==2 && a_sz==2) {
909 gl_internal_format=GL_RGBA2;
910 gl_internal_format_s="GL_RGBA2";
911 } else if(r_sz==4 && g_sz==4 && b_sz==4 && a_sz==4) {
912 gl_internal_format=GL_RGBA4;
913 gl_internal_format_s="GL_RGBA4";
914 } else if(r_sz==5 && g_sz==5 && b_sz==5 && a_sz==1) {
915 gl_internal_format=GL_RGB5_A1;
916 gl_internal_format_s="GL_RGB5_A1";
917 } else if(r_sz==8 && g_sz==8 && b_sz==8 && a_sz==8) {
918 gl_internal_format=GL_RGBA8;
919 gl_internal_format_s="GL_RGBA8";
920 } else if(r_sz==10 && g_sz==10 && b_sz==10 && a_sz==2) {
921 gl_internal_format=GL_RGB10_A2;
922 gl_internal_format_s="GL_RGB10_A2";
923 } else {
924 gl_internal_format=GL_RGB;
925 gl_internal_format_s="GL_RGB";
928 #ifdef TEXTUREFORMAT_ALWAYS_RGB24
929 gl_internal_format=GL_RGB8;
930 gl_internal_format_s="GL_RGB8";
931 #endif
933 if (IMGFMT_IS_BGR(format))
935 image_mode=MODE_BGR;
936 image_bpp=IMGFMT_BGR_DEPTH(format);
938 else
940 image_mode=MODE_RGB;
941 image_bpp=IMGFMT_RGB_DEPTH(format);
944 image_bytes=(image_bpp+7)/8;
946 draw_alpha_fnc=draw_alpha_null;
948 switch(image_bpp)
950 case 15:
951 case 16:
952 if(image_mode!=MODE_BGR)
954 gl_bitmap_format = GL_RGB;
955 gl_bitmap_format_s ="GL_RGB";
956 gl_bitmap_type = GL_UNSIGNED_SHORT_5_6_5;
957 gl_bitmap_type_s ="GL_UNSIGNED_SHORT_5_6_5";
958 } else {
959 gl_bitmap_format = GL_BGR;
960 gl_bitmap_format_s ="GL_BGR";
961 gl_bitmap_type = GL_UNSIGNED_SHORT_5_6_5;
962 gl_bitmap_type_s ="GL_UNSIGNED_SHORT_5_6_5";
965 if (image_bpp==15)
966 draw_alpha_fnc=draw_alpha_15;
967 else
968 draw_alpha_fnc=draw_alpha_16;
970 break;
971 case 24:
972 if(image_mode!=MODE_BGR)
974 /* RGB888 */
975 gl_bitmap_format = GL_RGB;
976 gl_bitmap_format_s ="GL_RGB";
977 } else {
978 /* BGR888 */
979 gl_bitmap_format = GL_BGR;
980 gl_bitmap_format_s ="GL_BGR";
982 gl_bitmap_type = GL_UNSIGNED_BYTE;
983 gl_bitmap_type_s ="GL_UNSIGNED_BYTE";
985 draw_alpha_fnc=draw_alpha_24; break;
986 break;
987 case 32:
988 /* RGBA8888 */
989 gl_bitmap_format = GL_BGRA;
990 gl_bitmap_format_s ="GL_BGRA";
992 if(image_mode!=MODE_BGR)
994 gl_bitmap_type = GL_UNSIGNED_INT_8_8_8_8_REV;
995 gl_bitmap_type_s ="GL_UNSIGNED_INT_8_8_8_8_REV";
996 } else {
997 gl_bitmap_type = GL_UNSIGNED_INT_8_8_8_8;
998 gl_bitmap_type_s ="GL_UNSIGNED_INT_8_8_8_8";
1001 draw_alpha_fnc=draw_alpha_32; break;
1002 break;
1005 if (initGl(d_width, d_height) == -1)
1006 return -1;
1007 #ifndef GL_WIN32
1008 saver_off(mDisplay);
1009 if (vo_ontop) vo_x11_setlayer(mDisplay,vo_window, vo_ontop);
1010 #endif
1012 return 0;
1015 static int gl_handlekey(int key)
1017 if(key=='a'||key=='A')
1019 gl_set_antialias(!gl_antialias);
1020 return 0;
1022 else if(key=='b'||key=='B')
1024 gl_set_bilinear(-1);
1025 return 0;
1027 return 1;
1030 #ifdef GL_WIN32
1032 static void check_events(void) {
1033 int e=vo_w32_check_events();
1034 if(e&VO_EVENT_RESIZE) resize(&vo_dwidth, &vo_dheight);
1035 if(e&VO_EVENT_EXPOSE && int_pause) flip_page();
1038 #else
1040 static void check_events(void)
1042 XEvent Event;
1043 char buf[100];
1044 KeySym keySym;
1045 int key;
1046 static XComposeStatus stat;
1047 int e;
1049 while ( XPending( mDisplay ) )
1051 XNextEvent( mDisplay,&Event );
1052 if( Event.type == KeyPress )
1055 XLookupString( &Event.xkey,buf,sizeof(buf),&keySym,&stat );
1056 key = (keySym&0xff00) != 0? ( (keySym&0x00ff) + 256 )
1057 : ( keySym ) ;
1058 if(gl_handlekey(key))
1059 XPutBackEvent(mDisplay, &Event);
1060 break;
1061 } else {
1062 XPutBackEvent(mDisplay, &Event);
1063 break;
1066 e=vo_x11_check_events(mDisplay);
1067 if(e&VO_EVENT_RESIZE) resize(&vo_dwidth, &vo_dheight);
1068 if(e&VO_EVENT_EXPOSE && int_pause) flip_page();
1071 #endif
1073 static void draw_osd(void)
1074 { vo_draw_text(image_width,image_height,draw_alpha_fnc); }
1076 static void
1077 flip_page(void)
1080 drawTextureDisplay();
1082 // glFlush();
1083 glFinish();
1084 #ifdef GL_WIN32
1085 SwapBuffers(vo_hdc);
1086 #else
1087 glXSwapBuffers( mDisplay,vo_window );
1088 #endif
1091 //static inline uint32_t draw_slice_x11(uint8_t *src[], uint32_t slice_num)
1092 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y)
1094 return 0;
1097 static inline uint32_t
1098 draw_frame_x11_bgr(uint8_t *src[])
1100 resetTexturePointers((unsigned char *)src[0]);
1101 ImageData=(unsigned char *)src[0];
1103 // for(i=0;i<image_height;i++) ImageData[image_width*image_bytes*i+20]=128;
1105 setupTextureDirtyArea(0, 0, image_width, image_height);
1106 return 0;
1109 static inline uint32_t
1110 draw_frame_x11_rgb(uint8_t *src[])
1112 resetTexturePointers((unsigned char *)src[0]);
1113 ImageData=(unsigned char *)src[0];
1115 setupTextureDirtyArea(0, 0, image_width, image_height);
1116 return 0;
1120 static uint32_t
1121 draw_frame(uint8_t *src[])
1123 uint32_t res = 0;
1125 if (IMGFMT_IS_RGB(image_format))
1126 res = draw_frame_x11_rgb(src);
1127 else
1128 res = draw_frame_x11_bgr(src);
1130 return res;
1133 static uint32_t
1134 query_format(uint32_t format)
1136 switch(format){
1137 case IMGFMT_RGB24:
1138 case IMGFMT_BGR24:
1139 // case IMGFMT_RGB32:
1140 // case IMGFMT_BGR32:
1141 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_OSD;
1143 return 0;
1147 static void
1148 uninit(void)
1150 if ( !vo_config_count ) return;
1151 #ifdef GL_WIN32
1152 vo_w32_uninit();
1153 #else
1154 vo_x11_uninit();
1155 #endif
1158 static uint32_t preinit(const char *arg)
1160 if(arg)
1162 printf("[gl2] Unknown subdevice: %s\n",arg);
1163 return ENOSYS;
1165 if( !vo_init() ) return -1; // Can't open X11
1166 return 0;
1169 static uint32_t control(uint32_t request, void *data, ...)
1171 switch (request) {
1172 case VOCTRL_PAUSE: return (int_pause=1);
1173 case VOCTRL_RESUME: return (int_pause=0);
1174 case VOCTRL_QUERY_FORMAT:
1175 return query_format(*((uint32_t*)data));
1176 case VOCTRL_ONTOP:
1177 #ifdef GL_WIN32
1178 vo_w32_ontop();
1179 #else
1180 vo_x11_ontop();
1181 #endif
1182 return VO_TRUE;
1183 case VOCTRL_FULLSCREEN:
1184 #ifdef GL_WIN32
1185 vo_w32_fullscreen();
1186 initGl(vo_dwidth, vo_dheight);
1187 #else
1188 vo_x11_fullscreen();
1189 #endif
1190 return VO_TRUE;
1191 #ifndef GL_WIN32
1192 case VOCTRL_SET_EQUALIZER:
1194 va_list ap;
1195 int value;
1197 va_start(ap, data);
1198 value = va_arg(ap, int);
1199 va_end(ap);
1200 return vo_x11_set_equalizer(data, value);
1202 case VOCTRL_GET_EQUALIZER:
1204 va_list ap;
1205 int *value;
1207 va_start(ap, data);
1208 value = va_arg(ap, int *);
1209 va_end(ap);
1210 return vo_x11_get_equalizer(data, value);
1212 #endif
1214 return VO_NOTIMPL;