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_description( _("X11 OpenGL provider") );
113 set_capability( "opengl provider", 50 );
114 set_callbacks( CreateOpenGL, DestroyOpenGL );
116 add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
117 add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, VLC_TRUE );
118 add_bool( "glx-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, VLC_TRUE );
120 add_integer ( "glx-xineramascreen", 0, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, VLC_TRUE );
124 /*****************************************************************************
125 * Exported prototypes
126 *****************************************************************************/
127 extern int E_(Activate) ( vlc_object_t * );
128 extern void E_(Deactivate) ( vlc_object_t * );
130 /*****************************************************************************
131 * CreateOpenGL: initialize an OpenGL provider
132 *****************************************************************************/
133 static int CreateOpenGL( vlc_object_t *p_this )
135 vout_thread_t *p_vout = (vout_thread_t *)p_this;
138 if( CheckGLX( p_this, &b_glx13 ) != VLC_SUCCESS )
140 msg_Err( p_vout, "no GLX support" );
144 if( E_(Activate)( p_this ) != VLC_SUCCESS )
149 /* Set the function pointer */
150 p_vout->pf_init = InitOpenGL;
151 p_vout->pf_swap = SwapBuffers;
152 p_vout->p_sys->b_glx13 = b_glx13;
157 /*****************************************************************************
158 * DestroyOpenGL: destroys an OpenGL provider
159 *****************************************************************************/
160 static void DestroyOpenGL( vlc_object_t *p_this )
162 vout_thread_t *p_vout = (vout_thread_t *)p_this;
163 vout_sys_t *p_sys = p_vout->p_sys;
165 glXDestroyContext( p_sys->p_display, p_sys->gwctx );
168 glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
171 E_(Deactivate)( p_this );
174 /*****************************************************************************
175 * OpenDisplay: open and initialize OpenGL device
176 *****************************************************************************/
177 static int CheckGLX( vlc_object_t *p_this, vlc_bool_t *b_glx13 )
179 Display *p_display = NULL;
180 int i_opcode, i_evt, i_err = 0;
181 int i_maj, i_min = 0;
183 /* Open the display */
184 p_display = XOpenDisplay( NULL );
185 if( p_display == NULL )
187 msg_Err( p_this, "Cannot open display" );
191 /* Check for GLX extension */
192 if( !XQueryExtension( p_display, "GLX", &i_opcode, &i_evt, &i_err ) )
194 msg_Err( p_this, "GLX extension not supported" );
195 XCloseDisplay( p_display );
198 if( !glXQueryExtension( p_display, &i_err, &i_evt ) )
200 msg_Err( p_this, "glXQueryExtension failed" );
201 XCloseDisplay( p_display );
205 /* Check GLX version */
206 if (!glXQueryVersion( p_display, &i_maj, &i_min ) )
208 msg_Err( p_this, "glXQueryVersion failed" );
209 XCloseDisplay( p_display );
212 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
214 *b_glx13 = VLC_FALSE;
215 msg_Dbg( p_this, "Using GLX 1.2 API" );
220 msg_Dbg( p_this, "Using GLX 1.3 API" );
223 XCloseDisplay( p_display );
227 /*****************************************************************************
228 * InitOpenGL: initializes OpenGL provider
229 *****************************************************************************/
230 static int InitOpenGL( vout_thread_t *p_vout )
233 if( !p_vout->p_sys->b_glx13 )
235 if( InitGLX12( p_vout ) != VLC_SUCCESS )
242 if( InitGLX13( p_vout ) != VLC_SUCCESS )
248 /* Set the OpenGL context _for the current thread_ */
249 SwitchContext( p_vout );
254 int InitGLX12( vout_thread_t *p_vout )
256 vout_sys_t *p_sys = p_vout->p_sys;
258 int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
259 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
261 p_vi = glXChooseVisual( p_sys->p_display,
262 DefaultScreen( p_sys->p_display), p_attr );
265 msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
269 /* Create an OpenGL context */
270 p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
274 msg_Err( p_vout, "Cannot create OpenGL context");
281 int InitGLX13( vout_thread_t *p_vout )
283 vout_sys_t *p_sys = p_vout->p_sys;
285 GLXFBConfig *p_fbconfs, fbconf;
287 int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
288 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
289 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
291 /* Get the FB configuration */
292 p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
293 if( (i_nbelem <= 0) || !p_fbconfs )
295 msg_Err( p_vout, "Cannot get FB configurations");
296 if( p_fbconfs ) XFree( p_fbconfs );
299 fbconf = p_fbconfs[0];
301 /* Get the X11 visual */
302 p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
305 msg_Err( p_vout, "Cannot get X11 visual" );
311 /* Create the GLX window */
312 p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
313 p_sys->p_win->video_window, NULL );
314 if( p_sys->gwnd == None )
316 msg_Err( p_vout, "Cannot create GLX window" );
320 /* Create an OpenGL context */
321 p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
322 GLX_RGBA_TYPE, NULL, True );
326 msg_Err( p_vout, "Cannot create OpenGL context");
333 /*****************************************************************************
334 * SwapBuffers: swap front/back buffers
335 *****************************************************************************/
336 static void SwapBuffers( vout_thread_t *p_vout )
338 vout_sys_t *p_sys = p_vout->p_sys;
339 int i_width, i_height, i_x, i_y;
341 vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
342 p_vout->p_sys->p_win->i_height,
343 &i_x, &i_y, &i_width, &i_height );
345 glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
349 glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
353 glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
357 void SwitchContext( vout_thread_t *p_vout )
359 vout_sys_t *p_sys = p_vout->p_sys;
361 /* Change the current OpenGL context */
364 glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
365 p_sys->gwnd, p_sys->gwctx );
369 glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,