]> git.sesse.net Git - vlc/blob - modules/video_output/x11/glx.c
Merge branch 'master' of git@git.videolan.org:vlc
[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 #include <errno.h>                                                 /* ENOMEM */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc/vlc.h>
35 #include <vlc_interface.h>
36 #include <vlc_vout.h>
37
38 #ifdef HAVE_SYS_SHM_H
39 #   include <sys/shm.h>                                /* shmget(), shmctl() */
40 #endif
41
42 #include <X11/Xlib.h>
43 #include <X11/Xmd.h>
44 #include <X11/Xutil.h>
45 #include <X11/keysym.h>
46 #ifdef HAVE_SYS_SHM_H
47 #   include <X11/extensions/XShm.h>
48 #endif
49 #ifdef DPMSINFO_IN_DPMS_H
50 #   include <X11/extensions/dpms.h>
51 #endif
52
53 #include <GL/glx.h>
54
55 #include "xcommon.h"
56
57 /* RV16 */
58 //#define VLCGL_RGB_FORMAT GL_RGB
59 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
60
61 /* RV24 */
62 //#define VLCGL_RGB_FORMAT GL_RGB
63 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
64
65 /* RV32 */
66 #define VLCGL_RGB_FORMAT GL_RGBA
67 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
68
69
70 /*****************************************************************************
71  * OpenGL provider interface
72  *****************************************************************************/
73 static int  CreateOpenGL ( vlc_object_t * );
74 static void DestroyOpenGL( vlc_object_t * );
75 static int  InitOpenGL   ( vout_thread_t * );
76 static void SwapBuffers  ( vout_thread_t * );
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int  InitGLX12    ( vout_thread_t * );
82 static int  InitGLX13    ( vout_thread_t * );
83 static void SwitchContext( vout_thread_t * );
84
85 /*****************************************************************************
86  * Module descriptor
87  *****************************************************************************/
88 #define ADAPTOR_TEXT N_("XVideo adaptor number")
89 #define ADAPTOR_LONGTEXT N_( \
90     "If your graphics card provides several adaptors, you have " \
91     "to choose which one will be used (you shouldn't have to change this).")
92
93 #define ALT_FS_TEXT N_("Alternate fullscreen method")
94 #define ALT_FS_LONGTEXT N_( \
95     "There are two ways to make a fullscreen window, unfortunately each one " \
96     "has its drawbacks.\n" \
97     "1) Let the window manager handle your fullscreen window (default), but " \
98     "things like taskbars will likely show on top of the video.\n" \
99     "2) Completely bypass the window manager, but then nothing will be able " \
100     "to show on top of the video.")
101
102 #define DISPLAY_TEXT N_("X11 display")
103 #define DISPLAY_LONGTEXT N_( \
104     "X11 hardware display to use. By default VLC will " \
105     "use the value of the DISPLAY environment variable.")
106
107 #define SCREEN_TEXT N_("Screen for fullscreen mode.")
108 #define SCREEN_LONGTEXT N_( \
109     "Screen to use in fullscreen mode. For instance " \
110     "set it to 0 for first screen, 1 for the second.")
111
112 vlc_module_begin();
113     set_shortname( "OpenGL(GLX)" );
114     set_category( CAT_VIDEO );
115     set_subcategory( SUBCAT_VIDEO_VOUT );
116     set_description( _("OpenGL(GLX) provider") );
117     set_capability( "opengl provider", 50 );
118     set_callbacks( CreateOpenGL, DestroyOpenGL );
119
120     add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true );
121     add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true );
122     add_bool( "glx-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, true );
123 #ifdef HAVE_XINERAMA
124     add_integer ( "glx-xineramascreen", 0, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, true );
125 #endif
126 vlc_module_end();
127
128 /*****************************************************************************
129  * Exported prototypes
130  *****************************************************************************/
131 extern int  E_(Activate)   ( vlc_object_t * );
132 extern void E_(Deactivate) ( vlc_object_t * );
133
134 /*****************************************************************************
135  * CreateOpenGL: initialize an OpenGL provider
136  *****************************************************************************/
137 static int CreateOpenGL( vlc_object_t *p_this )
138 {
139     vout_thread_t *p_vout = (vout_thread_t *)p_this;
140
141     if( E_(Activate)( p_this ) != VLC_SUCCESS )
142     {
143         return VLC_EGENERIC;
144     }
145
146     /* Set the function pointer */
147     p_vout->pf_init = InitOpenGL;
148     p_vout->pf_swap = SwapBuffers;
149
150     return VLC_SUCCESS;
151 }
152
153 /*****************************************************************************
154  * DestroyOpenGL: destroys an OpenGL provider
155  *****************************************************************************/
156 static void DestroyOpenGL( vlc_object_t *p_this )
157 {
158     vout_thread_t *p_vout = (vout_thread_t *)p_this;
159     vout_sys_t *p_sys = p_vout->p_sys;
160
161     glXDestroyContext( p_sys->p_display, p_sys->gwctx );
162     if( p_sys->b_glx13 )
163     {
164         glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
165     }
166
167     E_(Deactivate)( p_this );
168 }
169
170 /*****************************************************************************
171  * InitOpenGL: initializes OpenGL provider
172  *****************************************************************************/
173 static int InitOpenGL( vout_thread_t *p_vout )
174 {
175     /* Initialize GLX */
176     if( !p_vout->p_sys->b_glx13 )
177     {
178         if( InitGLX12( p_vout ) != VLC_SUCCESS )
179         {
180             return VLC_EGENERIC;
181         }
182     }
183     else
184     {
185         if( InitGLX13( p_vout ) != VLC_SUCCESS )
186         {
187             return VLC_EGENERIC;
188         }
189     }
190
191     /* Set the OpenGL context _for the current thread_ */
192     SwitchContext( p_vout );
193
194     return VLC_SUCCESS;
195 }
196
197 int InitGLX12( vout_thread_t *p_vout )
198 {
199     vout_sys_t *p_sys = p_vout->p_sys;
200     XVisualInfo *p_vi;
201     int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
202                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
203
204     p_vi = glXChooseVisual( p_sys->p_display,
205                             DefaultScreen( p_sys->p_display), p_attr );
206     if(! p_vi )
207     {
208         msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
209         return VLC_EGENERIC;
210     }
211
212     /* Create an OpenGL context */
213     p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
214     XFree( p_vi );
215     if( !p_sys->gwctx )
216     {
217         msg_Err( p_vout, "Cannot create OpenGL context");
218         return VLC_EGENERIC;
219     }
220
221     return VLC_SUCCESS;
222 }
223
224 int InitGLX13( vout_thread_t *p_vout )
225 {
226     vout_sys_t *p_sys = p_vout->p_sys;
227     int i_nbelem;
228     GLXFBConfig *p_fbconfs, fbconf;
229     XVisualInfo *p_vi;
230     int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
231                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
232                      GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
233
234     /* Get the FB configuration */
235     p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
236     if( (i_nbelem <= 0) || !p_fbconfs )
237     {
238         msg_Err( p_vout, "Cannot get FB configurations");
239         if( p_fbconfs ) XFree( p_fbconfs );
240         return VLC_EGENERIC;
241     }
242     fbconf = p_fbconfs[0];
243
244     /* Get the X11 visual */
245     p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
246     if( !p_vi )
247     {
248         msg_Err( p_vout, "Cannot get X11 visual" );
249         XFree( p_fbconfs );
250         return VLC_EGENERIC;
251     }
252     XFree( p_vi );
253
254     /* Create the GLX window */
255     p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
256                                    p_sys->p_win->video_window, NULL );
257     if( p_sys->gwnd == None )
258     {
259         msg_Err( p_vout, "Cannot create GLX window" );
260         return VLC_EGENERIC;
261     }
262
263     /* Create an OpenGL context */
264     p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
265                                         GLX_RGBA_TYPE, NULL, True );
266     XFree( p_fbconfs );
267     if( !p_sys->gwctx )
268     {
269         msg_Err( p_vout, "Cannot create OpenGL context");
270         return VLC_EGENERIC;
271     }
272
273     return VLC_SUCCESS;
274 }
275
276 /*****************************************************************************
277  * SwapBuffers: swap front/back buffers
278  *****************************************************************************/
279 static void SwapBuffers( vout_thread_t *p_vout )
280 {
281     vout_sys_t *p_sys = p_vout->p_sys;
282     unsigned int i_width, i_height, i_x, i_y;
283
284     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
285                        p_vout->p_sys->p_win->i_height,
286                        &i_x, &i_y, &i_width, &i_height );
287
288     glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
289
290     if( p_sys->b_glx13 )
291     {
292         glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
293     }
294     else
295     {
296         glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
297     }
298 }
299
300 void SwitchContext( vout_thread_t *p_vout )
301 {
302     vout_sys_t *p_sys = p_vout->p_sys;
303
304     /* Change the current OpenGL context */
305     if( p_sys->b_glx13 )
306     {
307         glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
308                                p_sys->gwnd, p_sys->gwctx );
309     }
310     else
311     {
312         glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,
313                         p_sys->gwctx );
314     }
315 }