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!
13 #include "subopt-helper.h"
14 #include "video_out.h"
15 #include "video_out_internal.h"
18 #include "gl_common.h"
21 #include "gui/interface.h"
24 #undef TEXTUREFORMAT_ALWAYS
26 #define TEXTUREFORMAT_ALWAYS GL_RGBA8
29 //! force texture height, useful for debugging
30 #define TEXTURE_HEIGHT 128
32 //! force texture width, useful for debugging
33 #define TEXTURE_WIDTH 128
36 static const vo_info_t info
=
38 "X11 (OpenGL) - multiple textures version",
40 "Arpad Gereoffy & Sven Goethel",
44 const LIBVO_EXTERN(gl2
)
47 static unsigned char *ImageData
=NULL
;
50 static int gl_vinfo
= 0;
51 static HGLRC gl_context
= 0;
52 #define update_xinerama_info w32_update_xinerama_info
53 #define vo_init vo_w32_init
54 #define vo_window vo_w32_window
56 static XVisualInfo
*gl_vinfo
= NULL
;
57 static GLXContext gl_context
= 0;
60 static uint32_t image_width
;
61 static uint32_t image_height
;
62 static uint32_t image_format
;
63 static uint32_t image_bpp
;
64 static uint32_t image_bytes
;
68 static uint32_t texture_width
;
69 static uint32_t texture_height
;
70 static int texnumx
, texnumy
, raw_line_len
;
72 static struct TexSquare
* texgrid
= NULL
;
73 static GLuint fragprog
;
74 static GLuint lookupTex
;
75 static GLint gl_internal_format
;
76 static int rgb_sz
, r_sz
, g_sz
, b_sz
, a_sz
;
77 static GLenum gl_bitmap_format
;
78 static GLenum gl_bitmap_type
;
79 static int isGL12
= GL_FALSE
;
81 static int gl_bilinear
=1;
82 static int gl_antialias
=0;
84 static int use_glFinish
;
86 static void (*draw_alpha_fnc
)
87 (int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
);
90 /* The squares that are tiled to make up the game screen polygon */
97 GLfloat fx
, fy
, fw
, fh
;
100 static GLint
getInternalFormat(void)
103 PIXELFORMATDESCRIPTOR pfd
;
104 HDC vo_hdc
= GetDC(vo_window
);
105 int pf
= GetPixelFormat(vo_hdc
);
106 if (!DescribePixelFormat(vo_hdc
, pf
, sizeof pfd
, &pfd
)) {
107 r_sz
= g_sz
= b_sz
= a_sz
= 0;
110 g_sz
= pfd
.cGreenBits
;
111 b_sz
= pfd
.cBlueBits
;
112 a_sz
= pfd
.cAlphaBits
;
114 ReleaseDC(vo_window
, vo_hdc
);
116 if (glXGetConfig(mDisplay
, gl_vinfo
, GLX_RED_SIZE
, &r_sz
) != 0) r_sz
= 0;
117 if (glXGetConfig(mDisplay
, gl_vinfo
, GLX_GREEN_SIZE
, &g_sz
) != 0) g_sz
= 0;
118 if (glXGetConfig(mDisplay
, gl_vinfo
, GLX_BLUE_SIZE
, &b_sz
) != 0) b_sz
= 0;
119 if (glXGetConfig(mDisplay
, gl_vinfo
, GLX_ALPHA_SIZE
, &a_sz
) != 0) a_sz
= 0;
122 rgb_sz
=r_sz
+g_sz
+b_sz
;
123 if(rgb_sz
<=0) rgb_sz
=24;
125 #ifdef TEXTUREFORMAT_ALWAYS
126 return TEXTUREFORMAT_ALWAYS
;
128 if(r_sz
==3 && g_sz
==3 && b_sz
==2 && a_sz
==0)
130 if(r_sz
==4 && g_sz
==4 && b_sz
==4 && a_sz
==0)
132 if(r_sz
==5 && g_sz
==5 && b_sz
==5 && a_sz
==0)
134 if(r_sz
==8 && g_sz
==8 && b_sz
==8 && a_sz
==0)
136 if(r_sz
==10 && g_sz
==10 && b_sz
==10 && a_sz
==0)
138 if(r_sz
==2 && g_sz
==2 && b_sz
==2 && a_sz
==2)
140 if(r_sz
==4 && g_sz
==4 && b_sz
==4 && a_sz
==4)
142 if(r_sz
==5 && g_sz
==5 && b_sz
==5 && a_sz
==1)
144 if(r_sz
==8 && g_sz
==8 && b_sz
==8 && a_sz
==8)
146 if(r_sz
==10 && g_sz
==10 && b_sz
==10 && a_sz
==2)
152 static int initTextures(void)
154 struct TexSquare
*tsq
=0;
155 GLfloat texpercx
, texpercy
;
160 // textures smaller than 64x64 might not be supported
162 while (s
<image_width
)
167 while (s
<image_height
)
171 if (image_format
!= IMGFMT_YV12
)
172 gl_internal_format
= getInternalFormat();
174 /* Test the max texture size */
176 glTexImage2D (GL_PROXY_TEXTURE_2D
, 0,
178 texture_width
, texture_height
,
179 0, gl_bitmap_format
, gl_bitmap_type
, NULL
);
181 glGetTexLevelParameteriv
182 (GL_PROXY_TEXTURE_2D
, 0, GL_TEXTURE_INTERNAL_FORMAT
, &format
);
184 if (format
!= gl_internal_format
)
186 mp_msg (MSGT_VO
, MSGL_V
, "[gl2] Needed texture [%dx%d] too big, trying ",
187 texture_height
, texture_width
);
189 if (texture_width
> texture_height
)
194 mp_msg (MSGT_VO
, MSGL_V
, "[%dx%d] !\n", texture_height
, texture_width
);
196 if(texture_width
< 64 || texture_height
< 64) {
197 mp_msg (MSGT_VO
, MSGL_FATAL
, "[gl2] Give up .. usable texture size not avaiable, or texture config error !\n");
202 while (format
!= gl_internal_format
&& texture_width
> 1 && texture_height
> 1);
204 texture_width
= TEXTURE_WIDTH
;
206 #ifdef TEXTURE_HEIGHT
207 texture_height
= TEXTURE_HEIGHT
;
210 texnumx
= image_width
/ texture_width
;
211 if ((image_width
% texture_width
) > 0)
214 texnumy
= image_height
/ texture_height
;
215 if ((image_height
% texture_height
) > 0)
218 mp_msg(MSGT_VO
, MSGL_V
, "[gl2] Creating %dx%d textures of size %dx%d ...\n",
219 texnumx
, texnumy
, texture_width
,texture_height
);
221 /* Allocate the texture memory */
223 texpercx
= (GLfloat
) texture_width
/ (GLfloat
) image_width
;
224 texpercy
= (GLfloat
) texture_height
/ (GLfloat
) image_height
;
228 texgrid
= calloc (texnumx
* texnumy
, sizeof (struct TexSquare
));
230 raw_line_len
= image_width
* image_bytes
;
232 mp_msg (MSGT_VO
, MSGL_DBG2
, "[gl2] texture-usage %d*width=%d, %d*height=%d\n",
233 (int) texnumx
, (int) texture_width
, (int) texnumy
,
234 (int) texture_height
);
237 for (y
= 0; y
< texnumy
; y
++) {
238 for (x
= 0; x
< texnumx
; x
++) {
239 tsq
->fx
= x
* texpercx
;
240 tsq
->fy
= y
* texpercy
;
245 tsq
->uvtexobjs
[0] = tsq
->uvtexobjs
[1] = 0;
247 glGenTextures (1, &(tsq
->texobj
));
249 glBindTexture (GL_TEXTURE_2D
, tsq
->texobj
);
250 if (image_format
== IMGFMT_YV12
) {
251 glGenTextures(2, tsq
->uvtexobjs
);
252 ActiveTexture(GL_TEXTURE1
);
253 glBindTexture (GL_TEXTURE_2D
, tsq
->uvtexobjs
[0]);
254 ActiveTexture(GL_TEXTURE2
);
255 glBindTexture (GL_TEXTURE_2D
, tsq
->uvtexobjs
[1]);
256 ActiveTexture(GL_TEXTURE0
);
259 glCreateClearTex(GL_TEXTURE_2D
, gl_internal_format
, GL_LINEAR
,
260 texture_width
, texture_height
, 0);
262 glTexEnvf (GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
263 if (image_format
== IMGFMT_YV12
) {
264 ActiveTexture(GL_TEXTURE1
);
265 glCreateClearTex(GL_TEXTURE_2D
, gl_internal_format
, GL_LINEAR
,
266 texture_width
/ 2, texture_height
/ 2, 128);
267 ActiveTexture(GL_TEXTURE2
);
268 glCreateClearTex(GL_TEXTURE_2D
, gl_internal_format
, GL_LINEAR
,
269 texture_width
/ 2, texture_height
/ 2, 128);
270 ActiveTexture(GL_TEXTURE0
);
274 } /* for all texnumx */
275 } /* for all texnumy */
280 static void resetTexturePointers(unsigned char *imageSource
)
282 unsigned char *texdata_start
, *line_start
;
283 struct TexSquare
*tsq
= texgrid
;
286 line_start
= (unsigned char *) imageSource
;
288 for (y
= 0; y
< texnumy
; y
++) {
289 texdata_start
= line_start
;
290 for (x
= 0; x
< texnumx
; x
++) {
291 tsq
->texture
= texdata_start
;
292 texdata_start
+= texture_width
* image_bytes
;
294 } /* for all texnumx */
295 line_start
+= texture_height
* raw_line_len
;
296 } /* for all texnumy */
299 static void gl_set_bilinear (int val
)
308 gl_bilinear
=gl_bilinear
%2;
309 /* no mipmap yet .. */
311 for (y
= 0; y
< texnumy
; y
++) {
312 for (x
= 0; x
< texnumx
; x
++) {
313 glBindTexture (GL_TEXTURE_2D
, texgrid
[y
* texnumx
+ x
].texobj
);
315 switch (gl_bilinear
) {
317 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
318 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
319 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] bilinear off\n");
322 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
323 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
324 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] bilinear linear\n");
327 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_NEAREST
);
328 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR_MIPMAP_NEAREST
);
329 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] bilinear mipmap nearest\n");
332 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
333 glTexParameteri (GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
334 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] bilinear mipmap linear\n");
341 static void gl_set_antialias (int val
)
346 glShadeModel (GL_SMOOTH
);
347 glEnable (GL_POLYGON_SMOOTH
);
348 glEnable (GL_LINE_SMOOTH
);
349 glEnable (GL_POINT_SMOOTH
);
350 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] antialiasing on\n");
352 glShadeModel (GL_FLAT
);
353 glDisable (GL_POLYGON_SMOOTH
);
354 glDisable (GL_LINE_SMOOTH
);
355 glDisable (GL_POINT_SMOOTH
);
356 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] antialiasing off\n");
361 static void drawTextureDisplay (void)
363 struct TexSquare
*square
= texgrid
;
366 glColor3f(1.0,1.0,1.0);
368 if (image_format
== IMGFMT_YV12
)
369 glEnableYUVConversion(GL_TEXTURE_2D
, use_yuv
);
370 for (y
= 0; y
< texnumy
; y
++) {
371 int thish
= texture_height
;
372 if (y
== texnumy
- 1 && image_height
% texture_height
)
373 thish
= image_height
% texture_height
;
374 for (x
= 0; x
< texnumx
; x
++) {
375 int thisw
= texture_width
;
376 if (x
== texnumx
- 1 && image_width
% texture_width
)
377 thisw
= image_width
% texture_width
;
378 glBindTexture (GL_TEXTURE_2D
, square
->texobj
);
379 if (image_format
== IMGFMT_YV12
) {
380 ActiveTexture(GL_TEXTURE1
);
381 glBindTexture (GL_TEXTURE_2D
, square
->uvtexobjs
[0]);
382 ActiveTexture(GL_TEXTURE2
);
383 glBindTexture (GL_TEXTURE_2D
, square
->uvtexobjs
[1]);
384 ActiveTexture(GL_TEXTURE0
);
388 glUploadTex(GL_TEXTURE_2D
, gl_bitmap_format
, gl_bitmap_type
,
389 square
->texture
, image_width
* image_bytes
,
390 0, 0, thisw
, thish
, 0);
393 glDrawTex(square
->fx
, square
->fy
, square
->fw
, square
->fh
,
394 0, 0, texture_width
, texture_height
,
395 texture_width
, texture_height
,
396 0, image_format
== IMGFMT_YV12
, 0);
398 } /* for all texnumx */
399 } /* for all texnumy */
400 if (image_format
== IMGFMT_YV12
)
401 glDisableYUVConversion(GL_TEXTURE_2D
, use_yuv
);
406 static void resize(int *x
,int *y
){
407 mp_msg(MSGT_VO
,MSGL_V
,"[gl2] Resize: %dx%d\n",*x
,*y
);
409 glClear(GL_COLOR_BUFFER_BIT
);
410 aspect(x
, y
, A_ZOOM
);
414 glViewport( (vo_screenwidth
-*x
)/2, (vo_screenheight
-*y
)/2, *x
, *y
);
416 //aspect(x, y, A_NOZOOM);
418 int top
= 0, left
= 0, w
= *x
, h
= *y
;
419 geometry(&top
, &left
, &w
, &h
, vo_screenwidth
, vo_screenheight
);
420 glViewport(top
, left
, w
, h
);
422 glViewport( 0, 0, *x
, *y
);
425 glMatrixMode(GL_PROJECTION
);
427 glOrtho (0, 1, 1, 0, -1.0, 1.0);
429 glMatrixMode(GL_MODELVIEW
);
433 static void draw_alpha_32(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
){
434 vo_draw_alpha_rgb32(w
,h
,src
,srca
,stride
,ImageData
+4*(y0
*image_width
+x0
),4*image_width
);
437 static void draw_alpha_24(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
){
438 vo_draw_alpha_rgb24(w
,h
,src
,srca
,stride
,ImageData
+3*(y0
*image_width
+x0
),3*image_width
);
441 static void draw_alpha_16(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
){
442 vo_draw_alpha_rgb16(w
,h
,src
,srca
,stride
,ImageData
+2*(y0
*image_width
+x0
),2*image_width
);
445 static void draw_alpha_15(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
){
446 vo_draw_alpha_rgb15(w
,h
,src
,srca
,stride
,ImageData
+2*(y0
*image_width
+x0
),2*image_width
);
449 static void draw_alpha_null(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
){
454 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
) {
455 if (!vo_w32_config(d_width
, d_height
, flags
))
459 aspect(&d_width
, &d_height
, A_ZOOM
);
466 static int choose_glx_visual(Display
*dpy
, int scr
, XVisualInfo
*res_vi
)
468 XVisualInfo
template, *vi_list
;
469 int vi_num
, i
, best_i
, best_weight
;
471 template.screen
= scr
;
472 vi_list
= XGetVisualInfo(dpy
, VisualScreenMask
, &template, &vi_num
);
473 if (!vi_list
) return -1;
474 best_weight
= 1000000; best_i
=0;
475 for (i
= 0; i
< vi_num
; i
++) {
477 /* of course, the visual must support OpenGL rendering... */
478 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_USE_GL
, &val
);
479 if (res
|| val
== False
) continue;
480 /* also it must be doublebuffered ... */
481 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_DOUBLEBUFFER
, &val
);
482 if (res
|| val
== False
) continue;
483 /* furthermore it must be RGBA (not color indexed) ... */
484 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_RGBA
, &val
);
485 if (res
|| val
== False
) continue;
486 /* prefer less depth buffer size, */
487 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_DEPTH_SIZE
, &val
);
490 /* stencil buffer size */
491 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_STENCIL_SIZE
, &val
);
494 /* and colorbuffer alpha size */
495 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_ALPHA_SIZE
, &val
);
498 /* and finally, prefer DirectColor-ed visuals to allow color corrections */
499 if (vi_list
[i
].class != DirectColor
) w
+= 100;
501 // avoid bad-looking visual with less that 8bit per color
502 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_RED_SIZE
, &val
);
504 if (val
< 8) w
+= 50;
505 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_GREEN_SIZE
, &val
);
507 if (val
< 8) w
+= 70;
508 res
= glXGetConfig(dpy
, vi_list
+ i
, GLX_BLUE_SIZE
, &val
);
510 if (val
< 8) w
+= 50;
512 if (w
< best_weight
) {
517 if (best_weight
< 1000000) *res_vi
= vi_list
[best_i
];
519 return (best_weight
< 1000000) ? 0 : -1;
522 static int config_glx(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t flags
, char *title
, uint32_t format
) {
523 XVisualInfo
*vinfo
, vinfo_buf
;
524 vo_mouse_autohide
= 1;
526 vo_window
= WinID
? (Window
)WinID
: mRootWin
;
527 vo_x11_selectinput_witherr(mDisplay
, vo_window
,
528 StructureNotifyMask
| KeyPressMask
| PointerMotionMask
|
529 ButtonPressMask
| ButtonReleaseMask
| ExposureMask
);
532 vinfo
= choose_glx_visual(mDisplay
,mScreen
,&vinfo_buf
) < 0 ? NULL
: &vinfo_buf
;
534 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl2] no GLX support present\n");
538 vo_x11_create_vo_window(vinfo
, vo_dx
, vo_dy
, d_width
, d_height
,
539 flags
, vo_x11_create_colormap(vinfo
), "gl2", title
);
546 static int config_glx_gui(uint32_t d_width
, uint32_t d_height
) {
547 guiGetEvent( guiSetShVideo
,0 ); // the GUI will set up / resize the window
552 static int initGl(uint32_t d_width
, uint32_t d_height
)
554 fragprog
= lookupTex
= 0;
555 if (initTextures() < 0)
559 glDisable(GL_DEPTH_TEST
);
560 glDepthMask(GL_FALSE
);
561 glDisable(GL_CULL_FACE
);
562 glEnable (GL_TEXTURE_2D
);
563 if (image_format
== IMGFMT_YV12
) {
565 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
566 glGenTextures(1, &lookupTex
);
567 ActiveTexture(GL_TEXTURE3
);
568 glBindTexture(GL_TEXTURE_2D
, lookupTex
);
569 ActiveTexture(GL_TEXTURE0
);
570 glBindTexture(GL_TEXTURE_2D
, 0);
571 case YUV_CONVERSION_FRAGMENT_POW
:
572 case YUV_CONVERSION_FRAGMENT
:
573 if (!GenPrograms
|| !BindProgram
) {
574 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] fragment program functions missing!\n");
577 GenPrograms(1, &fragprog
);
578 BindProgram(GL_FRAGMENT_PROGRAM
, fragprog
);
581 glSetupYUVConversion(GL_TEXTURE_2D
, use_yuv
, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
582 texture_width
, texture_height
);
588 mp_msg(MSGT_VO
, MSGL_V
, "[gl2] Using image_bpp=%d, image_bytes=%d, \n\tgl_bitmap_format=%s, gl_bitmap_type=%s, \n\trgb_size=%d (%d,%d,%d), a_sz=%d, \n\tgl_internal_format=%s\n",
589 image_bpp
, image_bytes
,
590 glValName(gl_bitmap_format
), glValName(gl_bitmap_type
),
591 rgb_sz
, r_sz
, g_sz
, b_sz
, a_sz
, glValName(gl_internal_format
));
593 resize(&d_width
, &d_height
);
595 glClearColor( 0.0f
,0.0f
,0.0f
,0.0f
);
596 glClear( GL_COLOR_BUFFER_BIT
);
598 drawTextureDisplay ();
603 /* connect to server, create and map window,
604 * allocate colors and (shared) memory
607 config(uint32_t width
, uint32_t height
, uint32_t d_width
, uint32_t d_height
, uint32_t flags
, char *title
, uint32_t format
)
609 const unsigned char * glVersion
;
611 image_height
= height
;
613 image_format
= format
;
619 if (config_glx_gui(d_width
, d_height
) == -1)
627 if (config_w32(width
, height
, d_width
, d_height
, flags
, title
, format
) == -1)
629 if (config_glx(width
, height
, d_width
, d_height
, flags
, title
, format
) == -1)
633 setGlWindow(&gl_vinfo
, &gl_context
, vo_window
);
635 glVersion
= glGetString(GL_VERSION
);
637 mp_msg(MSGT_VO
, MSGL_V
, "[gl2] OpenGL Driver Information:\n");
638 mp_msg(MSGT_VO
, MSGL_V
, "\tvendor: %s,\n\trenderer %s,\n\tversion %s\n",
639 glGetString(GL_VENDOR
), glGetString(GL_RENDERER
), glVersion
);
641 if(glVersion
[0]>'1' || (glVersion
[0]=='1' && glVersion
[2]>='2') )
647 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] You have OpenGL >= 1.2 capable drivers, GOOD (16bpp and BGR is ok!)\n");
649 mp_msg(MSGT_VO
, MSGL_INFO
, "[gl2] You have OpenGL < 1.2 drivers, BAD (16bpp and BGR may be damaged!)\n");
652 glFindFormat(format
, &image_bpp
, &gl_internal_format
, &gl_bitmap_format
, &gl_bitmap_type
);
654 image_bytes
=(image_bpp
+7)/8;
656 draw_alpha_fnc
=draw_alpha_null
;
660 draw_alpha_fnc
=draw_alpha_15
; break;
662 draw_alpha_fnc
=draw_alpha_16
; break;
664 draw_alpha_fnc
=draw_alpha_24
; break;
666 draw_alpha_fnc
=draw_alpha_32
; break;
669 if (initGl(vo_dwidth
, vo_dheight
) == -1)
672 if (vo_ontop
) vo_x11_setlayer(mDisplay
,vo_window
, vo_ontop
);
678 static int gl_handlekey(int key
)
680 if(key
=='a'||key
=='A') {
681 gl_set_antialias(!gl_antialias
);
683 } else if(key
=='b'||key
=='B') {
690 static void check_events(void)
698 static XComposeStatus stat
;
700 while ( XPending( mDisplay
) ) {
701 XNextEvent( mDisplay
,&Event
);
702 if( Event
.type
== KeyPress
) {
703 XLookupString( &Event
.xkey
,buf
,sizeof(buf
),&keySym
,&stat
);
704 key
= (keySym
&0xff00) != 0 ? (keySym
&0x00ff) + 256 : keySym
;
705 if(gl_handlekey(key
))
706 XPutBackEvent(mDisplay
, &Event
);
709 XPutBackEvent(mDisplay
, &Event
);
715 if(e
&VO_EVENT_RESIZE
) resize(&vo_dwidth
, &vo_dheight
);
716 if(e
&VO_EVENT_EXPOSE
&& int_pause
) flip_page();
719 static void draw_osd(void)
722 vo_draw_text(image_width
,image_height
,draw_alpha_fnc
);
728 drawTextureDisplay();
735 if (vo_fs
) // Avoid flickering borders in fullscreen mode
736 glClear (GL_COLOR_BUFFER_BIT
);
739 static int draw_slice(uint8_t *src
[], int stride
[], int w
,int h
,int x
,int y
)
741 uint8_t *yptr
= src
[0], *uptr
= src
[1], *vptr
= src
[2];
742 int ystride
= stride
[0], ustride
= stride
[1], vstride
= stride
[2];
744 struct TexSquare
*texline
= &texgrid
[y
/ texture_height
* texnumx
];
745 int subtex_y
= y
% texture_width
;
748 struct TexSquare
*tsq
= &texline
[x
/ texture_width
];
749 int subtex_x
= x
% texture_height
;
750 int subtex_h
= rem_h
;
751 if (subtex_y
+ subtex_h
> texture_height
)
752 subtex_h
= texture_height
- subtex_y
;
754 int subtex_w
= rem_w
;
755 if (subtex_x
+ subtex_w
> texture_width
)
756 subtex_w
= texture_width
- subtex_x
;
757 ActiveTexture(GL_TEXTURE0
);
758 glBindTexture(GL_TEXTURE_2D
, tsq
->texobj
);
759 glUploadTex(GL_TEXTURE_2D
, gl_bitmap_format
, gl_bitmap_type
,
760 yptr
, ystride
, subtex_x
, subtex_y
,
761 subtex_w
, subtex_h
, 0);
762 ActiveTexture(GL_TEXTURE1
);
763 glBindTexture(GL_TEXTURE_2D
, tsq
->uvtexobjs
[0]);
764 glUploadTex(GL_TEXTURE_2D
, gl_bitmap_format
, gl_bitmap_type
,
765 uptr
, ustride
, subtex_x
/ 2, subtex_y
/ 2,
766 subtex_w
/ 2, subtex_h
/ 2, 0);
767 ActiveTexture(GL_TEXTURE2
);
768 glBindTexture(GL_TEXTURE_2D
, tsq
->uvtexobjs
[1]);
769 glUploadTex(GL_TEXTURE_2D
, gl_bitmap_format
, gl_bitmap_type
,
770 vptr
, vstride
, subtex_x
/ 2, subtex_y
/ 2,
771 subtex_w
/ 2, subtex_h
/ 2, 0);
774 uptr
+= subtex_w
/ 2;
775 vptr
+= subtex_w
/ 2;
780 yptr
+= subtex_h
* ystride
- w
;
781 uptr
+= subtex_h
/ 2 * ustride
- w
/ 2;
782 vptr
+= subtex_h
/ 2 * vstride
- w
/ 2;
786 ActiveTexture(GL_TEXTURE0
);
791 draw_frame(uint8_t *src
[])
793 if (image_format
== IMGFMT_YV12
) {
794 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl2] error: draw_frame called for YV12!\n");
797 ImageData
=(unsigned char *)src
[0];
798 resetTexturePointers(ImageData
);
804 query_format(uint32_t format
)
809 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_OSD
|
810 VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_ACCEPT_STRIDE
;
817 // case IMGFMT_RGB32:
818 // case IMGFMT_BGR32:
820 return VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_OSD
;
829 if ( !vo_config_count
) return;
830 releaseGlContext(&gl_vinfo
, &gl_context
);
838 static opt_t subopts
[] = {
839 {"yuv", OPT_ARG_INT
, &use_yuv
, (opt_test_f
)int_non_neg
},
840 {"glfinish", OPT_ARG_BOOL
, &use_glFinish
, NULL
},
844 static int preinit(const char *arg
)
849 if (subopt_parse(arg
, subopts
) != 0) {
850 mp_msg(MSGT_VO
, MSGL_FATAL
,
851 "\n-vo gl2 command line help:\n"
852 "Example: mplayer -vo gl2:noglfinish\n"
855 " Do not call glFinish() before swapping buffers\n"
857 " 0: use software YUV to RGB conversion.\n"
858 " 1: use register combiners (nVidia only, for older cards).\n"
859 " 2: use fragment program.\n"
860 " 3: use fragment program with gamma correction.\n"
861 " 4: use fragment program with gamma correction via lookup.\n"
862 " 5: use ATI-specific method (for older cards).\n"
866 if( !vo_init() ) return -1; // Can't open X11
870 static int control(uint32_t request
, void *data
, ...)
873 case VOCTRL_PAUSE
: return (int_pause
=1);
874 case VOCTRL_RESUME
: return (int_pause
=0);
875 case VOCTRL_QUERY_FORMAT
:
876 return query_format(*((uint32_t*)data
));
877 case VOCTRL_GUISUPPORT
:
882 case VOCTRL_FULLSCREEN
:
884 if (setGlWindow(&gl_vinfo
, &gl_context
, vo_window
) == SET_WINDOW_REINIT
)
885 initGl(vo_dwidth
, vo_dheight
);
886 resize(&vo_dwidth
, &vo_dheight
);
893 case VOCTRL_GET_PANSCAN
:
895 case VOCTRL_SET_PANSCAN
:
896 resize (&vo_dwidth
, &vo_dheight
);
899 case VOCTRL_SET_EQUALIZER
:
905 value
= va_arg(ap
, int);
907 return vo_x11_set_equalizer(data
, value
);
909 case VOCTRL_GET_EQUALIZER
:
915 value
= va_arg(ap
, int *);
917 return vo_x11_get_equalizer(data
, value
);
920 case VOCTRL_UPDATE_SCREENINFO
:
921 update_xinerama_info();