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!
15 #include "video_out.h"
16 #include "video_out_internal.h"
25 #include <X11/Xutil.h>
31 #include "w32_common.h"
33 #include "x11_common.h"
40 #undef TEXTUREFORMAT_ALWAYS_RGB24
42 static vo_info_t info
=
44 "X11 (OpenGL) - multiple textures version",
46 "Arpad Gereoffy & Sven Goethel",
52 /* private prototypes */
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;
67 static GLXContext wsGLXContext
;
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
;
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 */
108 GLfloat fx1
, fy1
, fx2
, fy2
, fx3
, fy3
, fx4
, fy4
;
111 int dirtyXoff
, dirtyYoff
, dirtyWidth
, dirtyHeight
;
114 static void CalcFlatPoint(int x
,int y
,GLfloat
*px
,GLfloat
*py
)
116 *px
=(float)x
*texpercx
;
118 *py
=(float)y
*texpercy
;
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;
131 /* achieve the 2**e_x:=texture_width, 2**e_y:=texture_height */
133 while (s
<texture_width
)
138 while (s
<texture_height
)
143 /* Test the max texture size */
146 glTexImage2D (GL_PROXY_TEXTURE_2D
, 0,
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
)
163 for (i
= e_x
; i
> 0; i
--)
170 for (i
= e_y
; i
> 0; i
--)
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");
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)
189 texnumy
= image_height
/ texture_height
;
190 if ((image_height
% texture_height
) > 0)
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
;
202 texpercy
= (GLfloat
) texture_height
/ (GLfloat
) image_height
;
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
;
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
);
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
)
232 (GLfloat
) (image_width
% texture_width
) / (GLfloat
) texture_width
;
236 if (y
== texnumy
- 1 && image_height
% texture_height
)
238 (GLfloat
) (image_height
% texture_height
) / (GLfloat
) texture_height
;
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
;
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
);
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",
276 tsq
->isTexture
=GL_TRUE
;
279 glTexImage2D (GL_TEXTURE_2D
, 0,
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 */
300 static void resetTexturePointers(unsigned char *imageSource
)
302 unsigned char *line_1
=0, *line_2
=0, *mem_start
=0;
303 struct TexSquare
*tsq
=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)
344 ht
= image_height
- texture_height
* yi
;
349 for (xi
= 0; wdecr
>0 && xi
< texnumx
; xi
++)
351 square
= texgrid
+ yi
* texnumx
+ xi
;
353 if (xi
< texnumx
- 1)
356 wd
= image_width
- texture_width
* xi
;
358 if( 0 <= xh
&& xh
< wd
&&
362 square
->isDirty
=GL_TRUE
;
364 wh
=(wdecr
<wd
)?wdecr
:wd
-xh
;
367 hh
=(hdecr
<ht
)?hdecr
:ht
-yh
;
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);
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
;
388 if ( xi
== texnumx
- 1 )
400 static void gl_set_bilinear (int val
)
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
);
421 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
423 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
,
425 printf("[gl2] bilinear off\n");
428 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
430 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
,
432 printf("[gl2] bilinear linear\n");
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");
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");
454 static void gl_set_antialias (int val
)
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");
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");
478 static void drawTextureDisplay ()
480 struct TexSquare
*square
;
481 int x
, y
/*, xoff=0, yoff=0, wd, ht*/;
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
)
495 fprintf (stderr
, "[gl2] ain't a texture(update): texnum x=%d, y=%d, texture=%d\n",
496 x
, y
, square
->texobj
);
501 glBindTexture (GL_TEXTURE_2D
, square
->texobj
);
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
);
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
);
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
);
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;
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
);
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
);
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);
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
);
570 glClear(GL_COLOR_BUFFER_BIT
);
571 aspect(x
, y
, A_ZOOM
);
572 glViewport( (vo_screenwidth
-*x
)/2, (vo_screenheight
-*y
)/2, *x
, *y
);
574 //aspect(x, y, A_NOZOOM);
575 glViewport( 0, 0, *x
, *y
);
578 glMatrixMode(GL_PROJECTION
);
580 glOrtho (0, 1, 1, 0, -1.0, 1.0);
582 glMatrixMode(GL_MODELVIEW
);
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
){
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
;
612 o_dheight
= d_height
;
613 vo_fs
= flags
& VOFLAG_FULLSCREEN
;
614 vo_vm
= flags
& VOFLAG_MODESWITCHING
;
617 vo_dheight
= d_height
;
619 destroyRenderingContext();
620 if (!createRenderingContext())
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;
631 g_sz
= pfd
.cGreenBits
;
632 b_sz
= pfd
.cBlueBits
;
633 a_sz
= pfd
.cAlphaBits
;
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
++) {
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
);
665 /* stencil buffer size */
666 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_STENCIL_SIZE
, &val
);
669 /* and colorbuffer alpha size */
670 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_ALPHA_SIZE
, &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
) {
680 if (best_weight
< 1000000) *res_vi
= vi_list
[best_i
];
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
) {
687 XVisualInfo
*vinfo
, vinfo_buf
;
693 aspect(&d_width
,&d_height
,A_ZOOM
);
696 hint
.width
= vo_screenwidth
;
697 hint
.height
= vo_screenheight
;
698 hint
.flags
= PPosition
| PSize
;
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
;
716 printf("[gl2] no GLX support present\n");
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
);
733 XMapWindow(mDisplay
, vo_window
);
735 vo_x11_xinerama_move(mDisplay
,vo_window
);
737 vo_x11_sizehint( hint
.x
, hint
.y
, hint
.width
, hint
.height
,0 );
738 XClearWindow(mDisplay
,vo_window
);
743 XNextEvent(mDisplay
, &xev
);
745 while (xev
.type
!= MapNotify
|| xev
.xmap
.event
!= vo_window
);
747 XSelectInput(mDisplay
, vo_window
, NoEventMask
);
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
);
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)
774 if(glXGetConfig(mDisplay
,vinfo
,GLX_GREEN_SIZE
, &g_sz
)!=0)
776 if(glXGetConfig(mDisplay
,vinfo
,GLX_BLUE_SIZE
, &b_sz
)!=0)
778 if(glXGetConfig(mDisplay
,vinfo
,GLX_ALPHA_SIZE
, &a_sz
)!=0)
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)
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 )
811 else if( (image_width
*image_bpp
)%4 == 0 )
813 else if( (image_width
*image_bpp
)%2 == 0 )
818 glPixelStorei (GL_UNPACK_ALIGNMENT
, gl_alignment
);
820 glEnable (GL_TEXTURE_2D
);
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
);
840 /* connect to server, create and map window,
841 * allocate colors and (shared) memory
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
;
851 vo_dheight
= d_height
;
852 image_format
= format
;
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
);
863 if (config_w32(width
, height
, d_width
, d_height
, flags
, title
, format
) == -1)
865 if (config_glx(width
, height
, d_width
, d_height
, flags
, title
, format
) == -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
),
877 if(glVersion
[0]>'1' ||
878 (glVersion
[0]=='1' && glVersion
[2]>='2') )
885 printf("[gl2] You have OpenGL >= 1.2 capable drivers, GOOD (16bpp and BGR is ok!)\n");
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";
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";
933 if (IMGFMT_IS_BGR(format
))
936 image_bpp
=IMGFMT_BGR_DEPTH(format
);
941 image_bpp
=IMGFMT_RGB_DEPTH(format
);
944 image_bytes
=(image_bpp
+7)/8;
946 draw_alpha_fnc
=draw_alpha_null
;
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";
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";
966 draw_alpha_fnc
=draw_alpha_15
;
968 draw_alpha_fnc
=draw_alpha_16
;
972 if(image_mode
!=MODE_BGR
)
975 gl_bitmap_format
= GL_RGB
;
976 gl_bitmap_format_s
="GL_RGB";
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;
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";
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;
1005 if (initGl(d_width
, d_height
) == -1)
1008 saver_off(mDisplay
);
1009 if (vo_ontop
) vo_x11_setlayer(mDisplay
,vo_window
, vo_ontop
);
1015 static int gl_handlekey(int key
)
1017 if(key
=='a'||key
=='A')
1019 gl_set_antialias(!gl_antialias
);
1022 else if(key
=='b'||key
=='B')
1024 gl_set_bilinear(-1);
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();
1040 static void check_events(void)
1046 static XComposeStatus stat
;
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 )
1058 if(gl_handlekey(key
))
1059 XPutBackEvent(mDisplay
, &Event
);
1062 XPutBackEvent(mDisplay
, &Event
);
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();
1073 static void draw_osd(void)
1074 { vo_draw_text(image_width
,image_height
,draw_alpha_fnc
); }
1080 drawTextureDisplay();
1085 SwapBuffers(vo_hdc
);
1087 glXSwapBuffers( mDisplay
,vo_window
);
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
)
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
);
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
);
1121 draw_frame(uint8_t *src
[])
1125 if (IMGFMT_IS_RGB(image_format
))
1126 res
= draw_frame_x11_rgb(src
);
1128 res
= draw_frame_x11_bgr(src
);
1134 query_format(uint32_t format
)
1139 // case IMGFMT_RGB32:
1140 // case IMGFMT_BGR32:
1141 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_OSD
;
1150 if ( !vo_config_count
) return;
1158 static uint32_t preinit(const char *arg
)
1162 printf("[gl2] Unknown subdevice: %s\n",arg
);
1165 if( !vo_init() ) return -1; // Can't open X11
1169 static uint32_t control(uint32_t request
, void *data
, ...)
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
));
1183 case VOCTRL_FULLSCREEN
:
1185 vo_w32_fullscreen();
1186 initGl(vo_dwidth
, vo_dheight
);
1188 vo_x11_fullscreen();
1192 case VOCTRL_SET_EQUALIZER
:
1198 value
= va_arg(ap
, int);
1200 return vo_x11_set_equalizer(data
, value
);
1202 case VOCTRL_GET_EQUALIZER
:
1208 value
= va_arg(ap
, int *);
1210 return vo_x11_get_equalizer(data
, value
);