1 /*****************************************************************************
2 * glx.c: GLX OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Cyril Deguet <asmax@videolan.org>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* malloc(), free() */
37 # include <sys/shm.h> /* shmget(), shmctl() */
42 #include <X11/Xutil.h>
43 #include <X11/keysym.h>
45 # include <X11/extensions/XShm.h>
47 #ifdef DPMSINFO_IN_DPMS_H
48 # include <X11/extensions/dpms.h>
56 //#define VLCGL_RGB_FORMAT GL_RGB
57 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
60 //#define VLCGL_RGB_FORMAT GL_RGB
61 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
64 #define VLCGL_RGB_FORMAT GL_RGBA
65 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
68 /*****************************************************************************
69 * OpenGL provider interface
70 *****************************************************************************/
71 static int CreateOpenGL ( vlc_object_t * );
72 static void DestroyOpenGL( vlc_object_t * );
73 static int InitOpenGL ( vout_thread_t * );
74 static void SwapBuffers ( vout_thread_t * );
76 /*****************************************************************************
78 *****************************************************************************/
79 static int CheckGLX ( vlc_object_t *, vlc_bool_t * );
80 static int InitGLX12 ( vout_thread_t * );
81 static int InitGLX13 ( vout_thread_t * );
82 static void SwitchContext( vout_thread_t * );
84 /*****************************************************************************
86 *****************************************************************************/
87 #define ADAPTOR_TEXT N_("XVideo adaptor number")
88 #define ADAPTOR_LONGTEXT N_( \
89 "If you graphics card provides several adaptors, this option allows you " \
90 "to choose which one will be used (you shouldn't have to change this).")
92 #define ALT_FS_TEXT N_("Alternate fullscreen method")
93 #define ALT_FS_LONGTEXT N_( \
94 "There are two ways to make a fullscreen window, unfortunately each one " \
95 "has its drawbacks.\n" \
96 "1) Let the window manager handle your fullscreen window (default), but " \
97 "things like taskbars will likely show on top of the video.\n" \
98 "2) Completely bypass the window manager, but then nothing will be able " \
99 "to show on top of the video.")
101 #define DISPLAY_TEXT N_("X11 display name")
102 #define DISPLAY_LONGTEXT N_( \
103 "Specify the X11 hardware display you want to use. By default VLC will " \
104 "use the value of the DISPLAY environment variable.")
106 #define SCREEN_TEXT N_("Screen to be used for fullscreen mode.")
107 #define SCREEN_LONGTEXT N_( \
108 "Choose the screen you want to use in fullscreen mode. For instance " \
109 "set it to 0 for first screen, 1 for the second.")
112 set_shortname( N_("OpenGL") );
113 set_category( CAT_VIDEO );
114 set_subcategory( SUBCAT_VIDEO_VOUT );
115 set_description( _("X11 OpenGL provider") );
116 set_capability( "opengl provider", 50 );
117 set_callbacks( CreateOpenGL, DestroyOpenGL );
119 add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
120 add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, VLC_TRUE );
121 add_bool( "glx-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, VLC_TRUE );
123 add_integer ( "glx-xineramascreen", 0, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, VLC_TRUE );
127 /*****************************************************************************
128 * Exported prototypes
129 *****************************************************************************/
130 extern int E_(Activate) ( vlc_object_t * );
131 extern void E_(Deactivate) ( vlc_object_t * );
133 /*****************************************************************************
134 * CreateOpenGL: initialize an OpenGL provider
135 *****************************************************************************/
136 static int CreateOpenGL( vlc_object_t *p_this )
138 vout_thread_t *p_vout = (vout_thread_t *)p_this;
141 if( CheckGLX( p_this, &b_glx13 ) != VLC_SUCCESS )
143 msg_Err( p_vout, "no GLX support" );
147 if( E_(Activate)( p_this ) != VLC_SUCCESS )
152 /* Set the function pointer */
153 p_vout->pf_init = InitOpenGL;
154 p_vout->pf_swap = SwapBuffers;
155 p_vout->p_sys->b_glx13 = b_glx13;
160 /*****************************************************************************
161 * DestroyOpenGL: destroys an OpenGL provider
162 *****************************************************************************/
163 static void DestroyOpenGL( vlc_object_t *p_this )
165 vout_thread_t *p_vout = (vout_thread_t *)p_this;
166 vout_sys_t *p_sys = p_vout->p_sys;
168 glXDestroyContext( p_sys->p_display, p_sys->gwctx );
171 glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
174 E_(Deactivate)( p_this );
177 /*****************************************************************************
178 * OpenDisplay: open and initialize OpenGL device
179 *****************************************************************************/
180 static int CheckGLX( vlc_object_t *p_this, vlc_bool_t *b_glx13 )
182 Display *p_display = NULL;
183 int i_opcode, i_evt, i_err = 0;
184 int i_maj, i_min = 0;
186 /* Open the display */
187 p_display = XOpenDisplay( NULL );
188 if( p_display == NULL )
190 msg_Err( p_this, "Cannot open display" );
194 /* Check for GLX extension */
195 if( !XQueryExtension( p_display, "GLX", &i_opcode, &i_evt, &i_err ) )
197 msg_Err( p_this, "GLX extension not supported" );
198 XCloseDisplay( p_display );
201 if( !glXQueryExtension( p_display, &i_err, &i_evt ) )
203 msg_Err( p_this, "glXQueryExtension failed" );
204 XCloseDisplay( p_display );
208 /* Check GLX version */
209 if (!glXQueryVersion( p_display, &i_maj, &i_min ) )
211 msg_Err( p_this, "glXQueryVersion failed" );
212 XCloseDisplay( p_display );
215 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
217 *b_glx13 = VLC_FALSE;
218 msg_Dbg( p_this, "Using GLX 1.2 API" );
223 msg_Dbg( p_this, "Using GLX 1.3 API" );
226 XCloseDisplay( p_display );
230 /*****************************************************************************
231 * InitOpenGL: initializes OpenGL provider
232 *****************************************************************************/
233 static int InitOpenGL( vout_thread_t *p_vout )
236 if( !p_vout->p_sys->b_glx13 )
238 if( InitGLX12( p_vout ) != VLC_SUCCESS )
245 if( InitGLX13( p_vout ) != VLC_SUCCESS )
251 /* Set the OpenGL context _for the current thread_ */
252 SwitchContext( p_vout );
257 int InitGLX12( vout_thread_t *p_vout )
259 vout_sys_t *p_sys = p_vout->p_sys;
261 int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
262 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
264 p_vi = glXChooseVisual( p_sys->p_display,
265 DefaultScreen( p_sys->p_display), p_attr );
268 msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
272 /* Create an OpenGL context */
273 p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
277 msg_Err( p_vout, "Cannot create OpenGL context");
284 int InitGLX13( vout_thread_t *p_vout )
286 vout_sys_t *p_sys = p_vout->p_sys;
288 GLXFBConfig *p_fbconfs, fbconf;
290 int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
291 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
292 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
294 /* Get the FB configuration */
295 p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
296 if( (i_nbelem <= 0) || !p_fbconfs )
298 msg_Err( p_vout, "Cannot get FB configurations");
299 if( p_fbconfs ) XFree( p_fbconfs );
302 fbconf = p_fbconfs[0];
304 /* Get the X11 visual */
305 p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
308 msg_Err( p_vout, "Cannot get X11 visual" );
314 /* Create the GLX window */
315 p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
316 p_sys->p_win->video_window, NULL );
317 if( p_sys->gwnd == None )
319 msg_Err( p_vout, "Cannot create GLX window" );
323 /* Create an OpenGL context */
324 p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
325 GLX_RGBA_TYPE, NULL, True );
329 msg_Err( p_vout, "Cannot create OpenGL context");
336 /*****************************************************************************
337 * SwapBuffers: swap front/back buffers
338 *****************************************************************************/
339 static void SwapBuffers( vout_thread_t *p_vout )
341 vout_sys_t *p_sys = p_vout->p_sys;
342 int i_width, i_height, i_x, i_y;
344 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
345 p_vout->p_sys->p_win->i_height,
346 &i_x, &i_y, &i_width, &i_height );
348 glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
352 glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
356 glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
360 void SwitchContext( vout_thread_t *p_vout )
362 vout_sys_t *p_sys = p_vout->p_sys;
364 /* Change the current OpenGL context */
367 glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
368 p_sys->gwnd, p_sys->gwctx );
372 glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,