]> git.sesse.net Git - vlc/blob - modules/video_output/x11/glx.c
* modules/video_output/x11/glx.c: vlc_opengl.h was removed.
[vlc] / modules / video_output / x11 / glx.c
1 /*****************************************************************************
2  * glx.c: GLX OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34 #include <vlc/vout.h>
35
36 #ifdef HAVE_SYS_SHM_H
37 #   include <sys/shm.h>                                /* shmget(), shmctl() */
38 #endif
39
40 #include <X11/Xlib.h>
41 #include <X11/Xmd.h>
42 #include <X11/Xutil.h>
43 #include <X11/keysym.h>
44 #ifdef HAVE_SYS_SHM_H
45 #   include <X11/extensions/XShm.h>
46 #endif
47 #ifdef DPMSINFO_IN_DPMS_H
48 #   include <X11/extensions/dpms.h>
49 #endif
50
51 #include <GL/glx.h>
52
53 #include "xcommon.h"
54
55 /* RV16 */
56 //#define VLCGL_RGB_FORMAT GL_RGB
57 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
58
59 /* RV24 */
60 //#define VLCGL_RGB_FORMAT GL_RGB
61 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
62
63 /* RV32 */
64 #define VLCGL_RGB_FORMAT GL_RGBA
65 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
66
67
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 * );
74
75 /*****************************************************************************
76  * Local prototypes
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 * );
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86 vlc_module_begin();
87     set_description( _("X11 OpenGL provider") );
88     set_capability( "opengl provider", 50 );
89     set_callbacks( CreateOpenGL, DestroyOpenGL );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Exported prototypes
94  *****************************************************************************/
95 extern int  E_(Activate)   ( vlc_object_t * );
96 extern void E_(Deactivate) ( vlc_object_t * );
97
98 /*****************************************************************************
99  * CreateOpenGL: initialize an OpenGL provider
100  *****************************************************************************/
101 static int CreateOpenGL( vlc_object_t *p_this )
102 {
103     vout_thread_t *p_vout = (vout_thread_t *)p_this;
104     vlc_bool_t b_glx13;
105
106     if( CheckGLX( p_this, &b_glx13 ) != VLC_SUCCESS )
107     {
108         msg_Err( p_vout, "no GLX support" );
109         return VLC_EGENERIC;
110     }
111
112     if( E_(Activate)( p_this ) != VLC_SUCCESS )
113     {
114         return VLC_EGENERIC;
115     }
116
117     /* Set the function pointer */
118     p_vout->pf_swap = SwapBuffers;
119     p_vout->p_sys->b_glx13 = b_glx13;
120
121     /* Initialize GLX */
122     if( !b_glx13 )
123     {
124         if( InitGLX12( p_vout ) != VLC_SUCCESS )
125         {
126             return VLC_EGENERIC;
127         }
128     }
129     else
130     {
131         if( InitGLX13( p_vout ) != VLC_SUCCESS )
132         {
133             return VLC_EGENERIC;
134         }
135     }
136
137     /* Set the OpenGL context _for the current thread_ */
138     SwitchContext( p_vout );
139
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * DestroyOpenGL: destroys an OpenGL provider
145  *****************************************************************************/
146 static void DestroyOpenGL( vlc_object_t *p_this )
147 {
148     vout_thread_t *p_vout = (vout_thread_t *)p_this;
149     vout_sys_t *p_sys = p_vout->p_sys;
150
151     glXDestroyContext( p_sys->p_display, p_sys->gwctx );
152     if( p_sys->b_glx13 )
153     {
154         glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
155     }
156
157     E_(Deactivate)( p_this );
158 }
159
160 /*****************************************************************************
161  * OpenDisplay: open and initialize OpenGL device
162  *****************************************************************************/
163 static int CheckGLX( vlc_object_t *p_this, vlc_bool_t *b_glx13 )
164 {
165     Display *p_display;
166     int i_opcode, i_evt, i_err;
167     int i_maj, i_min;
168
169     /* Open the display */
170     p_display = XOpenDisplay( NULL );
171     if( !p_display )
172     {
173         msg_Err( p_this, "Cannot open display" );
174         return VLC_EGENERIC;
175     }
176
177     /* Check for GLX extension */
178     if( !XQueryExtension( p_display, "GLX", &i_opcode, &i_evt, &i_err ) )
179     {
180         msg_Err( p_this, "GLX extension not supported" );
181         return VLC_EGENERIC;
182     }
183     if( !glXQueryExtension( p_display, &i_err, &i_evt ) )
184     {
185         msg_Err( p_this, "glXQueryExtension failed" );
186         return VLC_EGENERIC;
187     }
188
189     /* Check GLX version */
190     if (!glXQueryVersion( p_display, &i_maj, &i_min ) )
191     {
192         msg_Err( p_this, "glXQueryVersion failed" );
193         return VLC_EGENERIC;
194     }
195     if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
196     {
197         *b_glx13 = VLC_FALSE;
198         msg_Dbg( p_this, "Using GLX 1.2 API" );
199     }
200     else
201     {
202         *b_glx13 = VLC_TRUE;
203         msg_Dbg( p_this, "Using GLX 1.3 API" );
204     }
205
206     return VLC_SUCCESS;
207 }
208
209 int InitGLX12( vout_thread_t *p_vout )
210 {
211     vout_sys_t *p_sys = p_vout->p_sys;
212     XVisualInfo *p_vi;
213     int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
214                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
215
216     p_vi = glXChooseVisual( p_sys->p_display,
217                             DefaultScreen( p_sys->p_display), p_attr );
218     if(! p_vi )
219     {
220         msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
221         return VLC_EGENERIC;
222     }
223
224     /* Create an OpenGL context */
225     p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
226     XFree( p_vi );
227     if( !p_sys->gwctx )
228     {
229         msg_Err( p_vout, "Cannot create OpenGL context");
230         return VLC_EGENERIC;
231     }
232
233     return VLC_SUCCESS;
234 }
235
236 int InitGLX13( vout_thread_t *p_vout )
237 {
238     vout_sys_t *p_sys = p_vout->p_sys;
239     int i_nbelem;
240     GLXFBConfig *p_fbconfs, fbconf;
241     XVisualInfo *p_vi;
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 };
245
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 )
249     {
250         msg_Err( p_vout, "Cannot get FB configurations");
251         if( p_fbconfs ) XFree( p_fbconfs );
252         return VLC_EGENERIC;
253     }
254     fbconf = p_fbconfs[0];
255
256     /* Get the X11 visual */
257     p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
258     if( !p_vi )
259     {
260         msg_Err( p_vout, "Cannot get X11 visual" );
261         XFree( p_fbconfs );
262         return VLC_EGENERIC;
263     }
264     XFree( p_vi );
265
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 )
270     {
271         msg_Err( p_vout, "Cannot create GLX window" );
272         return VLC_EGENERIC;
273     }
274
275     /* Create an OpenGL context */
276     p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
277                                         GLX_RGBA_TYPE, NULL, True );
278     XFree( p_fbconfs );
279     if( !p_sys->gwctx )
280     {
281         msg_Err( p_vout, "Cannot create OpenGL context");
282         return VLC_EGENERIC;
283     }
284
285     return VLC_SUCCESS;
286 }
287
288 /*****************************************************************************
289  * SwapBuffers: swap front/back buffers
290  *****************************************************************************/
291 static void SwapBuffers( vout_thread_t *p_vout )
292 {
293     vout_sys_t *p_sys = p_vout->p_sys;
294
295     if( p_sys->b_glx13 )
296     {
297         glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
298     }
299     else
300     {
301         glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
302     }
303 }
304
305 void SwitchContext( vout_thread_t *p_vout )
306 {
307     vout_sys_t *p_sys = p_vout->p_sys;
308
309     /* Change the current OpenGL context */
310     if( p_sys->b_glx13 )
311     {
312         glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
313                                p_sys->gwnd, p_sys->gwctx );
314     }
315     else
316     {
317         glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,
318                         p_sys->gwctx );
319     }
320 }