1 /*****************************************************************************
2 * glx.c: GLX video output
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Cyril Deguet <asmax@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <stdlib.h> /* malloc(), free() */
36 #include <X11/Xutil.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
45 static int Init ( vout_thread_t * );
46 static void End ( vout_thread_t * );
47 static int Manage ( vout_thread_t * );
48 static void Render ( vout_thread_t *p_vout, picture_t *p_pic );
49 static void DisplayVideo( vout_thread_t *, picture_t * );
51 static int OpenDisplay ( vout_thread_t * );
52 static void CloseDisplay( vout_thread_t * );
53 static int InitGLX12 ( vout_thread_t *p_vout );
54 static int InitGLX13 ( vout_thread_t *p_vout );
55 static void CreateWindow( vout_thread_t *p_vout, XVisualInfo *p_vi );
56 static void SwitchContext( vout_thread_t *p_vout );
58 static inline int GetAlignedSize( int i_size );
60 /*****************************************************************************
62 *****************************************************************************/
64 set_description( _("X11 OpenGL (GLX) video output") );
65 set_capability( "video output", 20 );
66 add_shortcut( "glx" );
67 set_callbacks( Create, Destroy );
70 /*****************************************************************************
71 * vout_sys_t: GLX video output method descriptor
72 *****************************************************************************
73 * This structure is part of the video output thread descriptor.
74 * It describes the GLX specific properties of an output thread.
75 *****************************************************************************/
100 #define MWM_HINTS_DECORATIONS (1L << 1)
101 #define PROP_MWM_HINTS_ELEMENTS 5
102 typedef struct mwmhints_t
106 uint32_t decorations;
112 /*****************************************************************************
113 * Create: allocates GLX video thread output method
114 *****************************************************************************
115 * This function allocates and initializes a GLX vout method.
116 *****************************************************************************/
117 static int Create( vlc_object_t *p_this )
119 vout_thread_t *p_vout = (vout_thread_t *)p_this;
121 /* Allocate structure */
122 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
123 if( p_vout->p_sys == NULL )
125 msg_Err( p_vout, "out of memory" );
129 //XXX set to 0 to disable the cube effect
130 p_vout->p_sys->i_effect = 1;
132 /* p_vout->p_sys->i_width = p_vout->i_window_width;
133 p_vout->p_sys->i_height = p_vout->i_window_height; */
134 p_vout->p_sys->i_width = 700;
135 p_vout->p_sys->i_height = 700;
136 p_vout->p_sys->b_fullscreen = 0;
138 /* A texture must have a size aligned on a power of 2 */
139 p_vout->p_sys->i_tex_width = GetAlignedSize( p_vout->render.i_width );
140 p_vout->p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
142 msg_Dbg( p_vout, "Texture size: %dx%d", p_vout->p_sys->i_tex_width,
143 p_vout->p_sys->i_tex_height );
145 /* Open and initialize device */
146 if( OpenDisplay( p_vout ) )
148 msg_Err( p_vout, "cannot open display" );
149 free( p_vout->p_sys );
153 p_vout->pf_init = Init;
154 p_vout->pf_end = End;
155 p_vout->pf_manage = Manage;
156 p_vout->pf_render = Render;
157 p_vout->pf_display = DisplayVideo;
162 /*****************************************************************************
163 * Init: initialize GLX video thread output method
164 *****************************************************************************/
165 static int Init( vout_thread_t *p_vout )
170 /* No YUV textures :( */
171 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
172 p_vout->output.i_rmask = 0x000000ff;
173 p_vout->output.i_gmask = 0x0000ff00;
174 p_vout->output.i_bmask = 0x00ff0000;
176 p_vout->output.i_width = p_vout->render.i_width;
177 p_vout->output.i_height = p_vout->render.i_height;
178 p_vout->output.i_aspect = p_vout->render.i_aspect;
180 I_OUTPUTPICTURES = 0;
184 /* Find an empty picture slot */
185 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
187 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
189 p_pic = p_vout->p_picture + i_index;
199 /* We know the chroma, allocate a buffer which will be used
200 * directly by the decoder */
204 p_pic->p->p_pixels = p_vout->p_sys->p_buffer
205 + 2 * p_vout->p_sys->i_tex_width *
206 (p_vout->p_sys->i_tex_height - p_vout->output.i_height)
207 + 2 * (p_vout->p_sys->i_tex_width - p_vout->output.i_width);
208 p_pic->p->i_lines = p_vout->output.i_height;
209 p_pic->p->i_pitch = p_vout->p_sys->i_tex_width * 4;
210 p_pic->p->i_pixel_pitch = 4;
211 p_pic->p->i_visible_pitch = p_vout->output.i_width * 4;
213 p_pic->i_status = DESTROYED_PICTURE;
214 p_pic->i_type = DIRECT_PICTURE;
216 PP_OUTPUTPICTURE[ 0 ] = p_pic;
218 I_OUTPUTPICTURES = 1;
220 SwitchContext( p_vout );
222 /* Set the texture parameters */
223 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
224 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
226 if( p_vout->p_sys->i_effect )
228 glEnable( GL_CULL_FACE);
229 /*glDisable( GL_DEPTH_TEST );
230 glEnable( GL_BLEND );
231 glBlendFunc( GL_SRC_ALPHA, GL_ONE );*/
233 /* Set the perpective */
234 glMatrixMode( GL_PROJECTION );
236 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
237 glMatrixMode( GL_MODELVIEW );
239 glTranslatef( 0.0, 0.0, - 5.0 );
245 /*****************************************************************************
246 * End: terminate GLX video thread output method
247 *****************************************************************************/
248 static void End( vout_thread_t *p_vout )
253 /*****************************************************************************
254 * Destroy: destroy GLX video thread output method
255 *****************************************************************************
256 * Terminate an output method created by Create
257 *****************************************************************************/
258 static void Destroy( vlc_object_t *p_this )
260 vout_thread_t *p_vout = (vout_thread_t *)p_this;
262 CloseDisplay( p_vout );
264 /* Free the texture buffer*/
265 if( p_vout->p_sys->p_buffer )
267 free( p_vout->p_sys->p_buffer );
270 free( p_vout->p_sys );
273 /*****************************************************************************
274 * Manage: handle X11 events
275 *****************************************************************************
276 * This function should be called regularly by video output thread. It manages
277 * console events. It returns a non null value on error.
278 *****************************************************************************/
279 static int Manage( vout_thread_t *p_vout )
283 p_display = p_vout->p_sys->p_display;
285 /* loop on X11 events */
286 while( XPending( p_display ) > 0 )
289 XNextEvent( p_display, &evt );
294 /* Delete notification */
295 if( (evt.xclient.format == 32) &&
296 ((Atom)evt.xclient.data.l[0] == p_vout->p_sys->wm_delete) )
298 /* the user wants to close the window */
299 playlist_t * p_playlist =
300 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
302 if( p_playlist != NULL )
304 playlist_Stop( p_playlist );
305 vlc_object_release( p_playlist );
315 /*****************************************************************************
316 * Render: render previously calculated output
317 *****************************************************************************/
318 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
320 glClear(GL_COLOR_BUFFER_BIT);
321 /*glTexImage2D (GL_TEXTURE_2D, 0, 3, p_vout->output.i_width,
322 p_vout->output.i_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
323 p_vout->p_sys->p_buffer);*/
324 glTexImage2D (GL_TEXTURE_2D, 0, 3, p_vout->p_sys->i_tex_width,
325 p_vout->p_sys->i_tex_height , 0, GL_RGBA, GL_UNSIGNED_BYTE,
326 p_vout->p_sys->p_buffer);
328 if( !p_vout->p_sys->i_effect )
330 glEnable( GL_TEXTURE_2D);
331 glBegin( GL_POLYGON);
332 glTexCoord2f(0.0,0.0); glVertex2f(-1.0,1.0);
333 glTexCoord2f(1.0,0.0); glVertex2f(1.0,1.0);
334 glTexCoord2f(1.0,1.0); glVertex2f(1.0,-1.0);
335 glTexCoord2f(0.0,1.0); glVertex2f(-1.0,-1.0);
340 glRotatef( 1.0, 0.3, 0.5, 0.7 );
342 glEnable( GL_TEXTURE_2D);
345 float f_width = p_vout->output.i_width;
346 float f_height = p_vout->output.i_height;
348 /* Correct the aspect ratio */
349 /* float f_aspect = (float)p_vout->output.i_aspect / VOUT_ASPECT_FACTOR;
352 f_height *= f_aspect;
359 float f_offset_x = (1.0 - f_width / p_vout->p_sys->i_tex_width) / 2.0;
360 float f_offset_y = (1.0 - f_height / p_vout->p_sys->i_tex_height) / 2.0;
363 glTexCoord2f( f_offset_x, f_offset_y );
364 glVertex3f( - 1.0, 1.0, 1.0 );
365 glTexCoord2f( f_offset_x, 1.0 - f_offset_y );
366 glVertex3f( - 1.0, - 1.0, 1.0 );
367 glTexCoord2f( 1.0 - f_offset_x, 1.0 - f_offset_y );
368 glVertex3f( 1.0, - 1.0, 1.0 );
369 glTexCoord2f( 1.0 - f_offset_x, f_offset_y );
370 glVertex3f( 1.0, 1.0, 1.0 );
373 glTexCoord2f( f_offset_x, f_offset_y );
374 glVertex3f( - 1.0, 1.0, - 1.0 );
375 glTexCoord2f( f_offset_x, 1.0 - f_offset_y );
376 glVertex3f( - 1.0, - 1.0, - 1.0 );
377 glTexCoord2f( 1.0 - f_offset_x, 1.0 - f_offset_y );
378 glVertex3f( - 1.0, - 1.0, 1.0 );
379 glTexCoord2f( 1.0 - f_offset_x, f_offset_y );
380 glVertex3f( - 1.0, 1.0, 1.0 );
383 glTexCoord2f( f_offset_x, f_offset_y );
384 glVertex3f( 1.0, 1.0, - 1.0 );
385 glTexCoord2f( f_offset_x, 1.0 - f_offset_y );
386 glVertex3f( 1.0, - 1.0, - 1.0 );
387 glTexCoord2f( 1.0 - f_offset_x, 1.0 - f_offset_y );
388 glVertex3f( - 1.0, - 1.0, - 1.0 );
389 glTexCoord2f( 1.0 - f_offset_x, f_offset_y );
390 glVertex3f( - 1.0, 1.0, - 1.0 );
393 glTexCoord2f( f_offset_x, f_offset_y );
394 glVertex3f( 1.0, 1.0, 1.0 );
395 glTexCoord2f( f_offset_x, 1.0 - f_offset_y );
396 glVertex3f( 1.0, - 1.0, 1.0 );
397 glTexCoord2f( 1.0 - f_offset_x, 1.0 - f_offset_y );
398 glVertex3f( 1.0, - 1.0, - 1.0 );
399 glTexCoord2f( 1.0 - f_offset_x, f_offset_y );
400 glVertex3f( 1.0, 1.0, - 1.0 );
403 glTexCoord2f( f_offset_x, f_offset_y );
404 glVertex3f( - 1.0, 1.0, - 1.0 );
405 glTexCoord2f( f_offset_x, 1.0 - f_offset_y );
406 glVertex3f( - 1.0, 1.0, 1.0 );
407 glTexCoord2f( 1.0 - f_offset_x, 1.0 - f_offset_y );
408 glVertex3f( 1.0, 1.0, 1.0 );
409 glTexCoord2f( 1.0 - f_offset_x, f_offset_y );
410 glVertex3f( 1.0, 1.0, - 1.0 );
413 glTexCoord2f( f_offset_x, f_offset_y );
414 glVertex3f( - 1.0, - 1.0, 1.0 );
415 glTexCoord2f( f_offset_x, 1.0 - f_offset_y );
416 glVertex3f( - 1.0, - 1.0, - 1.0 );
417 glTexCoord2f( 1.0 - f_offset_x, 1.0 - f_offset_y );
418 glVertex3f( 1.0, - 1.0, - 1.0 );
419 glTexCoord2f( 1.0 - f_offset_x, f_offset_y );
420 glVertex3f( 1.0, - 1.0, 1.0 );
423 glDisable( GL_TEXTURE_2D);
428 /*****************************************************************************
429 * DisplayVideo: displays previously rendered output
430 *****************************************************************************/
431 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
433 if( p_vout->p_sys->b_glx13 )
435 glXSwapBuffers( p_vout->p_sys->p_display, p_vout->p_sys->gwnd );
439 glXSwapBuffers( p_vout->p_sys->p_display, p_vout->p_sys->wnd );
444 /* following functions are local */
446 /*****************************************************************************
447 * OpenDisplay: open and initialize OpenGL device
448 *****************************************************************************/
450 static int OpenDisplay( vout_thread_t *p_vout )
453 int i_opcode, i_evt, i_err;
456 /* Open the display */
457 p_vout->p_sys->p_display = p_display = XOpenDisplay( NULL );
460 msg_Err( p_vout, "Cannot open display" );
464 /* Check for GLX extension */
465 if( !XQueryExtension( p_display, "GLX", &i_opcode, &i_evt, &i_err ) )
467 msg_Err( p_vout, "GLX extension not supported" );
470 if( !glXQueryExtension( p_display, &i_err, &i_evt ) )
472 msg_Err( p_vout, "glXQueryExtension failed" );
476 /* Check GLX version */
477 if (!glXQueryVersion( p_display, &i_maj, &i_min ) )
479 msg_Err( p_vout, "glXQueryVersion failed" );
482 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
484 p_vout->p_sys->b_glx13 = 0;
485 msg_Dbg( p_vout, "Using GLX 1.2 API" );
486 if( InitGLX12( p_vout ) == -1 )
493 p_vout->p_sys->b_glx13 = 1;
494 msg_Dbg( p_vout, "Using GLX 1.3 API" );
495 if( InitGLX13( p_vout ) == -1 )
501 XMapWindow( p_display, p_vout->p_sys->wnd );
502 if( p_vout->p_sys->b_fullscreen )
505 XMoveWindow( p_display, p_vout->p_sys->wnd, 0, 0 );
509 /* Allocate the texture buffer */
510 p_vout->p_sys->p_buffer =
511 malloc( p_vout->p_sys->i_tex_width * p_vout->p_sys->i_tex_height * 4 );
512 if( !p_vout->p_sys->p_buffer )
514 msg_Err( p_vout, "Out of memory" );
520 /*****************************************************************************
521 * CloseDisplay: close and reset OpenGL device
522 *****************************************************************************
523 * Returns all resources allocated by OpenDisplay and restore the original
524 * state of the device.
525 *****************************************************************************/
526 static void CloseDisplay( vout_thread_t *p_vout )
532 p_display = p_vout->p_sys->p_display;
533 glXDestroyContext( p_display, p_vout->p_sys->gwctx );
534 if( p_vout->p_sys->b_glx13 )
536 glXDestroyWindow( p_display, p_vout->p_sys->gwnd );
538 XDestroyWindow( p_display, p_vout->p_sys->wnd );
539 XCloseDisplay( p_display );
544 int InitGLX12( vout_thread_t *p_vout )
549 int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
550 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER,
553 p_display = p_vout->p_sys->p_display;
555 p_vi = glXChooseVisual( p_display, DefaultScreen( p_display), p_attr );
558 msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
562 /* Create the window */
563 CreateWindow( p_vout, p_vi );
565 /* Create an OpenGL context */
566 p_vout->p_sys->gwctx = gwctx = glXCreateContext( p_display, p_vi, 0, True );
569 msg_Err( p_vout, "Cannot create OpenGL context");
579 int InitGLX13( vout_thread_t *p_vout )
583 GLXFBConfig *p_fbconfs, fbconf;
586 int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
587 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
588 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
590 p_display = p_vout->p_sys->p_display;
592 /* Get the FB configuration */
593 p_fbconfs = glXChooseFBConfig( p_display, 0, p_attr, &i_nbelem );
594 if( (i_nbelem <= 0) || !p_fbconfs )
596 msg_Err( p_vout, "Cannot get FB configurations");
603 fbconf = p_fbconfs[0];
605 /* Get the X11 visual */
606 p_vi = glXGetVisualFromFBConfig( p_display, fbconf );
609 msg_Err( p_vout, "Cannot get X11 visual" );
614 /* Create the window */
615 CreateWindow( p_vout, p_vi );
619 /* Create the GLX window */
620 p_vout->p_sys->gwnd = glXCreateWindow( p_display, fbconf, p_vout->p_sys->wnd, NULL );
621 if( p_vout->p_sys->gwnd == None )
623 msg_Err( p_vout, "Cannot create GLX window" );
627 /* Create an OpenGL context */
628 p_vout->p_sys->gwctx = gwctx = glXCreateNewContext( p_display, fbconf,
629 GLX_RGBA_TYPE, NULL, True );
632 msg_Err( p_vout, "Cannot create OpenGL context");
643 void CreateWindow( vout_thread_t *p_vout, XVisualInfo *p_vi )
646 XSetWindowAttributes xattr;
649 XSizeHints* p_size_hints;
653 p_display = p_vout->p_sys->p_display;
655 /* Create a colormap */
656 cm = XCreateColormap( p_display, RootWindow( p_display, p_vi->screen ),
657 p_vi->visual, AllocNone );
659 /* Create the window */
660 xattr.background_pixel = BlackPixel( p_display, DefaultScreen(p_display) );
661 xattr.border_pixel = 0;
663 p_vout->p_sys->wnd = wnd = XCreateWindow( p_display, DefaultRootWindow(p_display),
664 0, 0, p_vout->p_sys->i_width, p_vout->p_sys->i_height, 0, p_vi->depth,
665 InputOutput, p_vi->visual,
666 CWBackPixel | CWBorderPixel | CWColormap, &xattr);
668 /* Allow the window to be deleted by the window manager */
669 p_vout->p_sys->wm_delete = XInternAtom( p_display, "WM_DELETE_WINDOW", False );
670 XSetWMProtocols( p_display, wnd, &p_vout->p_sys->wm_delete, 1 );
672 if( p_vout->p_sys->b_fullscreen )
674 mwmhints.flags = MWM_HINTS_DECORATIONS;
675 mwmhints.decorations = False;
677 prop = XInternAtom( p_display, "_MOTIF_WM_HINTS", False );
678 XChangeProperty( p_display, wnd, prop, prop, 32, PropModeReplace,
679 (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS );
683 /* Prevent the window from being resized */
684 p_size_hints = XAllocSizeHints();
685 p_size_hints->flags = PMinSize | PMaxSize;
686 p_size_hints->min_width = p_vout->p_sys->i_width;
687 p_size_hints->min_height = p_vout->p_sys->i_height;
688 p_size_hints->max_width = p_vout->p_sys->i_width;
689 p_size_hints->max_height = p_vout->p_sys->i_height;
690 XSetWMNormalHints( p_display, wnd, p_size_hints );
691 XFree( p_size_hints );
693 XSelectInput( p_display, wnd, KeyPressMask );
697 void SwitchContext( vout_thread_t *p_vout )
699 /* Change the current OpenGL context */
700 if( p_vout->p_sys->b_glx13 )
702 glXMakeContextCurrent( p_vout->p_sys->p_display, p_vout->p_sys->gwnd,
703 p_vout->p_sys->gwnd, p_vout->p_sys->gwctx );
707 glXMakeCurrent( p_vout->p_sys->p_display, p_vout->p_sys->wnd,
708 p_vout->p_sys->gwctx );
713 int GetAlignedSize( int i_size )
715 /* Return the nearest power of 2 */
717 while( i_result < i_size )