1 /*****************************************************************************
2 * glx.c: GLX OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
41 # include <sys/shm.h> /* shmget(), shmctl() */
46 #include <X11/Xutil.h>
47 #include <X11/keysym.h>
49 # include <X11/extensions/XShm.h>
51 #ifdef DPMSINFO_IN_DPMS_H
52 # include <X11/extensions/dpms.h>
60 //#define VLCGL_RGB_FORMAT GL_RGB
61 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
64 //#define VLCGL_RGB_FORMAT GL_RGB
65 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
68 #define VLCGL_RGB_FORMAT GL_RGBA
69 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
72 /*****************************************************************************
73 * OpenGL provider interface
74 *****************************************************************************/
75 static int CreateOpenGL ( vlc_object_t * );
76 static void DestroyOpenGL( vlc_object_t * );
77 static int InitOpenGL ( vout_thread_t * );
78 static void SwapBuffers ( vout_thread_t * );
80 /*****************************************************************************
82 *****************************************************************************/
83 static int InitGLX12 ( vout_thread_t * );
84 static int InitGLX13 ( vout_thread_t * );
85 static void SwitchContext( vout_thread_t * );
87 /*****************************************************************************
89 *****************************************************************************/
90 #define DISPLAY_TEXT N_("X11 display")
91 #define DISPLAY_LONGTEXT N_( \
92 "X11 hardware display to use. By default VLC will " \
93 "use the value of the DISPLAY environment variable.")
95 #define SHM_TEXT N_("Use shared memory")
96 #define SHM_LONGTEXT N_( \
97 "Use shared memory to communicate between VLC and the X server.")
100 set_shortname( "OpenGL(GLX)" )
101 set_category( CAT_VIDEO )
102 set_subcategory( SUBCAT_VIDEO_VOUT )
103 set_description( N_("OpenGL(GLX) provider") )
104 set_capability( "opengl provider", 50 )
105 set_callbacks( CreateOpenGL, DestroyOpenGL )
107 add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true )
108 add_obsolete_integer( "glx-adaptor" ) /* Deprecated since 1.0.4 */
109 #ifdef HAVE_SYS_SHM_H
110 add_bool( "glx-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true )
114 /*****************************************************************************
115 * Exported prototypes
116 *****************************************************************************/
117 extern int Activate ( vlc_object_t * );
118 extern void Deactivate ( vlc_object_t * );
120 /*****************************************************************************
121 * CreateOpenGL: initialize an OpenGL provider
122 *****************************************************************************/
123 static int CreateOpenGL( vlc_object_t *p_this )
125 vout_thread_t *p_vout = (vout_thread_t *)p_this;
127 if( Activate( p_this ) != VLC_SUCCESS )
132 /* Set the function pointer */
133 p_vout->pf_init = InitOpenGL;
134 p_vout->pf_swap = SwapBuffers;
139 /*****************************************************************************
140 * DestroyOpenGL: destroys an OpenGL provider
141 *****************************************************************************/
142 static void DestroyOpenGL( vlc_object_t *p_this )
144 vout_thread_t *p_vout = (vout_thread_t *)p_this;
145 vout_sys_t *p_sys = p_vout->p_sys;
147 glXDestroyContext( p_sys->p_display, p_sys->gwctx );
150 glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
153 Deactivate( p_this );
156 /*****************************************************************************
157 * InitOpenGL: initializes OpenGL provider
158 *****************************************************************************/
159 static int InitOpenGL( vout_thread_t *p_vout )
162 if( !p_vout->p_sys->b_glx13 )
164 if( InitGLX12( p_vout ) != VLC_SUCCESS )
171 if( InitGLX13( p_vout ) != VLC_SUCCESS )
177 /* Set the OpenGL context _for the current thread_ */
178 SwitchContext( p_vout );
183 int InitGLX12( vout_thread_t *p_vout )
185 vout_sys_t *p_sys = p_vout->p_sys;
187 int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
188 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
190 p_vi = glXChooseVisual( p_sys->p_display,
191 DefaultScreen( p_sys->p_display), p_attr );
194 msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
198 /* Create an OpenGL context */
199 p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
203 msg_Err( p_vout, "Cannot create OpenGL context");
210 int InitGLX13( vout_thread_t *p_vout )
212 vout_sys_t *p_sys = p_vout->p_sys;
213 int i_nb, ret = VLC_EGENERIC;
214 GLXFBConfig *p_fbconfs = NULL, fbconf = NULL;
215 XWindowAttributes att;
216 static const int p_attr[] = {
217 GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5, GLX_BLUE_SIZE, 5,
218 GLX_DOUBLEBUFFER, True, GLX_X_RENDERABLE, True,
219 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
223 /* Get the FB configuration */
224 p_fbconfs = glXChooseFBConfig( p_sys->p_display, p_sys->i_screen, p_attr, &i_nb );
225 if( p_fbconfs == NULL )
227 msg_Err( p_vout, "Cannot get FB configurations");
231 /* We should really create the window _after_ the frame buffer
232 * configuration was chosen, instead of selecting the frame buffer from
233 * the window. That requires reworking xcommon.c though.
235 XGetWindowAttributes( p_sys->p_display, p_sys->window.video_window, &att );
236 for( int i = 0; i < i_nb && !fbconf; i++ )
240 /* Get the X11 visual */
241 p_vi = glXGetVisualFromFBConfig( p_sys->p_display, p_fbconfs[i] );
245 if( p_vi->visualid == att.visual->visualid )
246 fbconf = p_fbconfs[i];
251 msg_Err( p_vout, "Cannot find matching frame buffer" );
255 /* Create the GLX window */
256 p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
257 p_sys->window.video_window, NULL );
258 if( p_sys->gwnd == None )
260 msg_Err( p_vout, "Cannot create GLX window" );
264 /* Create an OpenGL context */
265 p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
266 GLX_RGBA_TYPE, NULL, True );
268 msg_Err( p_vout, "Cannot create OpenGL context");
277 /*****************************************************************************
278 * SwapBuffers: swap front/back buffers
279 *****************************************************************************/
280 static void SwapBuffers( vout_thread_t *p_vout )
282 vout_sys_t *p_sys = p_vout->p_sys;
283 unsigned int i_width, i_height, i_x, i_y;
285 vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
286 p_vout->p_sys->window.i_height,
287 &i_x, &i_y, &i_width, &i_height );
289 glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
293 glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
297 glXSwapBuffers( p_sys->p_display, p_sys->window.video_window );
301 void SwitchContext( vout_thread_t *p_vout )
303 vout_sys_t *p_sys = p_vout->p_sys;
305 /* Change the current OpenGL context */
308 glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
309 p_sys->gwnd, p_sys->gwctx );
313 glXMakeCurrent( p_sys->p_display, p_sys->window.video_window,