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 void SwapBuffers ( vout_thread_t * );
75 /*****************************************************************************
77 *****************************************************************************/
78 static int CheckGLX ( vlc_object_t *, vlc_bool_t * );
79 static int InitGLX12 ( vout_thread_t * );
80 static int InitGLX13 ( vout_thread_t * );
81 static void SwitchContext( vout_thread_t * );
83 /*****************************************************************************
85 *****************************************************************************/
87 set_description( _("X11 OpenGL provider") );
88 set_capability( "opengl provider", 50 );
89 set_callbacks( CreateOpenGL, DestroyOpenGL );
92 /*****************************************************************************
94 *****************************************************************************/
95 extern int E_(Activate) ( vlc_object_t * );
96 extern void E_(Deactivate) ( vlc_object_t * );
98 /*****************************************************************************
99 * CreateOpenGL: initialize an OpenGL provider
100 *****************************************************************************/
101 static int CreateOpenGL( vlc_object_t *p_this )
103 vout_thread_t *p_vout = (vout_thread_t *)p_this;
106 if( CheckGLX( p_this, &b_glx13 ) != VLC_SUCCESS )
108 msg_Err( p_vout, "no GLX support" );
112 if( E_(Activate)( p_this ) != VLC_SUCCESS )
117 /* Set the function pointer */
118 p_vout->pf_swap = SwapBuffers;
119 p_vout->p_sys->b_glx13 = b_glx13;
124 if( InitGLX12( p_vout ) != VLC_SUCCESS )
131 if( InitGLX13( p_vout ) != VLC_SUCCESS )
137 /* Set the OpenGL context _for the current thread_ */
138 SwitchContext( p_vout );
143 /*****************************************************************************
144 * DestroyOpenGL: destroys an OpenGL provider
145 *****************************************************************************/
146 static void DestroyOpenGL( vlc_object_t *p_this )
148 vout_thread_t *p_vout = (vout_thread_t *)p_this;
149 vout_sys_t *p_sys = p_vout->p_sys;
151 glXDestroyContext( p_sys->p_display, p_sys->gwctx );
154 glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
157 E_(Deactivate)( p_this );
160 /*****************************************************************************
161 * OpenDisplay: open and initialize OpenGL device
162 *****************************************************************************/
163 static int CheckGLX( vlc_object_t *p_this, vlc_bool_t *b_glx13 )
166 int i_opcode, i_evt, i_err;
169 /* Open the display */
170 p_display = XOpenDisplay( NULL );
173 msg_Err( p_this, "Cannot open display" );
177 /* Check for GLX extension */
178 if( !XQueryExtension( p_display, "GLX", &i_opcode, &i_evt, &i_err ) )
180 msg_Err( p_this, "GLX extension not supported" );
183 if( !glXQueryExtension( p_display, &i_err, &i_evt ) )
185 msg_Err( p_this, "glXQueryExtension failed" );
189 /* Check GLX version */
190 if (!glXQueryVersion( p_display, &i_maj, &i_min ) )
192 msg_Err( p_this, "glXQueryVersion failed" );
195 if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
197 *b_glx13 = VLC_FALSE;
198 msg_Dbg( p_this, "Using GLX 1.2 API" );
203 msg_Dbg( p_this, "Using GLX 1.3 API" );
209 int InitGLX12( vout_thread_t *p_vout )
211 vout_sys_t *p_sys = p_vout->p_sys;
213 int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
214 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
216 p_vi = glXChooseVisual( p_sys->p_display,
217 DefaultScreen( p_sys->p_display), p_attr );
220 msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
224 /* Create an OpenGL context */
225 p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
229 msg_Err( p_vout, "Cannot create OpenGL context");
236 int InitGLX13( vout_thread_t *p_vout )
238 vout_sys_t *p_sys = p_vout->p_sys;
240 GLXFBConfig *p_fbconfs, fbconf;
242 int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
243 GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
244 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
246 /* Get the FB configuration */
247 p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
248 if( (i_nbelem <= 0) || !p_fbconfs )
250 msg_Err( p_vout, "Cannot get FB configurations");
251 if( p_fbconfs ) XFree( p_fbconfs );
254 fbconf = p_fbconfs[0];
256 /* Get the X11 visual */
257 p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
260 msg_Err( p_vout, "Cannot get X11 visual" );
266 /* Create the GLX window */
267 p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
268 p_sys->p_win->video_window, NULL );
269 if( p_sys->gwnd == None )
271 msg_Err( p_vout, "Cannot create GLX window" );
275 /* Create an OpenGL context */
276 p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
277 GLX_RGBA_TYPE, NULL, True );
281 msg_Err( p_vout, "Cannot create OpenGL context");
288 /*****************************************************************************
289 * SwapBuffers: swap front/back buffers
290 *****************************************************************************/
291 static void SwapBuffers( vout_thread_t *p_vout )
293 vout_sys_t *p_sys = p_vout->p_sys;
297 glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
301 glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
305 void SwitchContext( vout_thread_t *p_vout )
307 vout_sys_t *p_sys = p_vout->p_sys;
309 /* Change the current OpenGL context */
312 glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
313 p_sys->gwnd, p_sys->gwctx );
317 glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,