]> git.sesse.net Git - vlc/blob - modules/video_output/x11/glx.c
add_bool wants booleans.
[vlc] / modules / video_output / x11 / glx.c
1 /*****************************************************************************
2  * glx.c: GLX OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_vout.h>
39
40 #ifdef HAVE_SYS_SHM_H
41 #   include <sys/shm.h>                                /* shmget(), shmctl() */
42 #endif
43
44 #include <X11/Xlib.h>
45 #include <X11/Xmd.h>
46 #include <X11/Xutil.h>
47 #include <X11/keysym.h>
48 #ifdef HAVE_SYS_SHM_H
49 #   include <X11/extensions/XShm.h>
50 #endif
51 #ifdef DPMSINFO_IN_DPMS_H
52 #   include <X11/extensions/dpms.h>
53 #endif
54
55 #include <GL/glx.h>
56
57 #include "xcommon.h"
58
59 /* RV16 */
60 //#define VLCGL_RGB_FORMAT GL_RGB
61 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
62
63 /* RV24 */
64 //#define VLCGL_RGB_FORMAT GL_RGB
65 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
66
67 /* RV32 */
68 #define VLCGL_RGB_FORMAT GL_RGBA
69 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
70
71
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 * );
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static int  InitGLX12    ( vout_thread_t * );
84 static int  InitGLX13    ( vout_thread_t * );
85 static void SwitchContext( vout_thread_t * );
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 #define ADAPTOR_TEXT N_("XVideo adaptor number")
91 #define ADAPTOR_LONGTEXT N_( \
92     "If your graphics card provides several adaptors, you have " \
93     "to choose which one will be used (you shouldn't have to change this).")
94
95 #define DISPLAY_TEXT N_("X11 display")
96 #define DISPLAY_LONGTEXT N_( \
97     "X11 hardware display to use. By default VLC will " \
98     "use the value of the DISPLAY environment variable.")
99
100 #define SHM_TEXT N_("Use shared memory")
101 #define SHM_LONGTEXT N_( \
102     "Use shared memory to communicate between VLC and the X server.")
103
104 vlc_module_begin ()
105     set_shortname( "OpenGL(GLX)" )
106     set_category( CAT_VIDEO )
107     set_subcategory( SUBCAT_VIDEO_VOUT )
108     set_description( N_("OpenGL(GLX) provider") )
109     set_capability( "opengl provider", 50 )
110     set_callbacks( CreateOpenGL, DestroyOpenGL )
111
112     add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true )
113     add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true )
114 #ifdef HAVE_SYS_SHM_H
115     add_bool( "glx-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true )
116 #endif
117 vlc_module_end ()
118
119 /*****************************************************************************
120  * Exported prototypes
121  *****************************************************************************/
122 extern int  Activate   ( vlc_object_t * );
123 extern void Deactivate ( vlc_object_t * );
124
125 /*****************************************************************************
126  * CreateOpenGL: initialize an OpenGL provider
127  *****************************************************************************/
128 static int CreateOpenGL( vlc_object_t *p_this )
129 {
130     vout_thread_t *p_vout = (vout_thread_t *)p_this;
131
132     if( Activate( p_this ) != VLC_SUCCESS )
133     {
134         return VLC_EGENERIC;
135     }
136
137     /* Set the function pointer */
138     p_vout->pf_init = InitOpenGL;
139     p_vout->pf_swap = SwapBuffers;
140
141     return VLC_SUCCESS;
142 }
143
144 /*****************************************************************************
145  * DestroyOpenGL: destroys an OpenGL provider
146  *****************************************************************************/
147 static void DestroyOpenGL( vlc_object_t *p_this )
148 {
149     vout_thread_t *p_vout = (vout_thread_t *)p_this;
150     vout_sys_t *p_sys = p_vout->p_sys;
151
152     glXDestroyContext( p_sys->p_display, p_sys->gwctx );
153     if( p_sys->b_glx13 )
154     {
155         glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
156     }
157
158     Deactivate( p_this );
159 }
160
161 /*****************************************************************************
162  * InitOpenGL: initializes OpenGL provider
163  *****************************************************************************/
164 static int InitOpenGL( vout_thread_t *p_vout )
165 {
166     /* Initialize GLX */
167     if( !p_vout->p_sys->b_glx13 )
168     {
169         if( InitGLX12( p_vout ) != VLC_SUCCESS )
170         {
171             return VLC_EGENERIC;
172         }
173     }
174     else
175     {
176         if( InitGLX13( p_vout ) != VLC_SUCCESS )
177         {
178             return VLC_EGENERIC;
179         }
180     }
181
182     /* Set the OpenGL context _for the current thread_ */
183     SwitchContext( p_vout );
184
185     return VLC_SUCCESS;
186 }
187
188 int InitGLX12( vout_thread_t *p_vout )
189 {
190     vout_sys_t *p_sys = p_vout->p_sys;
191     XVisualInfo *p_vi;
192     int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
193                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
194
195     p_vi = glXChooseVisual( p_sys->p_display,
196                             DefaultScreen( p_sys->p_display), p_attr );
197     if(! p_vi )
198     {
199         msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
200         return VLC_EGENERIC;
201     }
202
203     /* Create an OpenGL context */
204     p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
205     XFree( p_vi );
206     if( !p_sys->gwctx )
207     {
208         msg_Err( p_vout, "Cannot create OpenGL context");
209         return VLC_EGENERIC;
210     }
211
212     return VLC_SUCCESS;
213 }
214
215 int InitGLX13( vout_thread_t *p_vout )
216 {
217     vout_sys_t *p_sys = p_vout->p_sys;
218     int i_nb, ret = VLC_EGENERIC;
219     GLXFBConfig *p_fbconfs = NULL, fbconf = NULL;
220     XWindowAttributes att;
221     static const int p_attr[] = {
222         GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5, GLX_BLUE_SIZE, 5,
223         GLX_DOUBLEBUFFER, True, GLX_X_RENDERABLE, True,
224         GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
225         None,
226     };
227
228     /* Get the FB configuration */
229     p_fbconfs = glXChooseFBConfig( p_sys->p_display, p_sys->i_screen, p_attr, &i_nb );
230     if( p_fbconfs == NULL )
231     {
232         msg_Err( p_vout, "Cannot get FB configurations");
233         return VLC_EGENERIC;
234     }
235
236     /* We should really create the window _after_ the frame buffer
237      * configuration was chosen, instead of selecting the frame buffer from
238      * the window. That requires reworking xcommon.c though.
239      * -- Courmisch */
240     XGetWindowAttributes( p_sys->p_display, p_sys->window.video_window, &att );
241     for( int i = 0; i < i_nb && !fbconf; i++ )
242     {
243         XVisualInfo *p_vi;
244
245         /* Get the X11 visual */
246         p_vi = glXGetVisualFromFBConfig( p_sys->p_display, p_fbconfs[i] );
247         if( !p_vi )
248             continue; /* OoM? */
249
250         if( p_vi->visualid == att.visual->visualid )
251             fbconf = p_fbconfs[i];
252         XFree( p_vi );
253     }
254     if( !fbconf )
255     {
256         msg_Err( p_vout, "Cannot find matching frame buffer" );
257         goto out;
258     }
259
260     /* Create the GLX window */
261     p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
262                                    p_sys->window.video_window, NULL );
263     if( p_sys->gwnd == None )
264     {
265         msg_Err( p_vout, "Cannot create GLX window" );
266         goto out;
267     }
268
269     /* Create an OpenGL context */
270     p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
271                                         GLX_RGBA_TYPE, NULL, True );
272     if( !p_sys->gwctx )
273         msg_Err( p_vout, "Cannot create OpenGL context");
274     else
275         ret = VLC_SUCCESS;
276
277 out:
278     XFree( p_fbconfs );
279     return ret;
280 }
281
282 /*****************************************************************************
283  * SwapBuffers: swap front/back buffers
284  *****************************************************************************/
285 static void SwapBuffers( vout_thread_t *p_vout )
286 {
287     vout_sys_t *p_sys = p_vout->p_sys;
288     unsigned int i_width, i_height, i_x, i_y;
289
290     vout_PlacePicture( p_vout, p_vout->p_sys->window.i_width,
291                        p_vout->p_sys->window.i_height,
292                        &i_x, &i_y, &i_width, &i_height );
293
294     glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
295
296     if( p_sys->b_glx13 )
297     {
298         glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
299     }
300     else
301     {
302         glXSwapBuffers( p_sys->p_display, p_sys->window.video_window );
303     }
304 }
305
306 void SwitchContext( vout_thread_t *p_vout )
307 {
308     vout_sys_t *p_sys = p_vout->p_sys;
309
310     /* Change the current OpenGL context */
311     if( p_sys->b_glx13 )
312     {
313         glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
314                                p_sys->gwnd, p_sys->gwctx );
315     }
316     else
317     {
318         glXMakeCurrent( p_sys->p_display, p_sys->window.video_window,
319                         p_sys->gwctx );
320     }
321 }