glx.c 11.8 KB
Newer Older
1
/*****************************************************************************
2
 * glx.c: GLX OpenGL provider
3 4 5 6 7
 *****************************************************************************
 * Copyright (C) 2004 VideoLAN
 * $Id$
 *
 * Authors: Cyril Deguet <asmax@videolan.org>
8
 *          Gildas Bazin <gbazin@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <vlc/vout.h>

36 37 38 39
#ifdef HAVE_SYS_SHM_H
#   include <sys/shm.h>                                /* shmget(), shmctl() */
#endif

40
#include <X11/Xlib.h>
41
#include <X11/Xmd.h>
42
#include <X11/Xutil.h>
43 44 45 46 47 48 49 50
#include <X11/keysym.h>
#ifdef HAVE_SYS_SHM_H
#   include <X11/extensions/XShm.h>
#endif
#ifdef DPMSINFO_IN_DPMS_H
#   include <X11/extensions/dpms.h>
#endif

51 52
#include <GL/glx.h>

53
#include "xcommon.h"
54

55 56 57 58 59 60 61 62 63 64 65 66
/* RV16 */
//#define VLCGL_RGB_FORMAT GL_RGB
//#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5

/* RV24 */
//#define VLCGL_RGB_FORMAT GL_RGB
//#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE

/* RV32 */
#define VLCGL_RGB_FORMAT GL_RGBA
#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE

67 68

/*****************************************************************************
69
 * OpenGL provider interface
70 71 72
 *****************************************************************************/
static int  CreateOpenGL ( vlc_object_t * );
static void DestroyOpenGL( vlc_object_t * );
73
static int  InitOpenGL   ( vout_thread_t * );
74
static void SwapBuffers  ( vout_thread_t * );
75

76 77 78
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
79 80 81 82
static int  CheckGLX     ( vlc_object_t *, vlc_bool_t * );
static int  InitGLX12    ( vout_thread_t * );
static int  InitGLX13    ( vout_thread_t * );
static void SwitchContext( vout_thread_t * );
83 84 85 86

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
gbazin's avatar
gbazin committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#define ADAPTOR_TEXT N_("XVideo adaptor number")
#define ADAPTOR_LONGTEXT N_( \
    "If you graphics card provides several adaptors, this option allows you " \
    "to choose which one will be used (you shouldn't have to change this).")

#define ALT_FS_TEXT N_("Alternate fullscreen method")
#define ALT_FS_LONGTEXT N_( \
    "There are two ways to make a fullscreen window, unfortunately each one " \
    "has its drawbacks.\n" \
    "1) Let the window manager handle your fullscreen window (default), but " \
    "things like taskbars will likely show on top of the video.\n" \
    "2) Completely bypass the window manager, but then nothing will be able " \
    "to show on top of the video.")

#define DISPLAY_TEXT N_("X11 display name")
#define DISPLAY_LONGTEXT N_( \
    "Specify the X11 hardware display you want to use. By default VLC will " \
    "use the value of the DISPLAY environment variable.")

#define SCREEN_TEXT N_("Screen to be used for fullscreen mode.")
#define SCREEN_LONGTEXT N_( \
    "Choose the screen you want to use in fullscreen mode. For instance " \
    "set it to 0 for first screen, 1 for the second.")

111
vlc_module_begin();
zorglub's avatar
zorglub committed
112 113 114
    set_name( N_("OpenGL") );
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VOUT );
115 116 117
    set_description( _("X11 OpenGL provider") );
    set_capability( "opengl provider", 50 );
    set_callbacks( CreateOpenGL, DestroyOpenGL );
gbazin's avatar
gbazin committed
118 119 120 121 122 123 124

    add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
    add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, VLC_TRUE );
    add_bool( "glx-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, VLC_TRUE );
#ifdef HAVE_XINERAMA
    add_integer ( "glx-xineramascreen", 0, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, VLC_TRUE );
#endif
125 126 127
vlc_module_end();

/*****************************************************************************
128
 * Exported prototypes
129
 *****************************************************************************/
130 131
extern int  E_(Activate)   ( vlc_object_t * );
extern void E_(Deactivate) ( vlc_object_t * );
132

133 134 135 136 137
/*****************************************************************************
 * CreateOpenGL: initialize an OpenGL provider
 *****************************************************************************/
static int CreateOpenGL( vlc_object_t *p_this )
{
138 139
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
    vlc_bool_t b_glx13;
140

141
    if( CheckGLX( p_this, &b_glx13 ) != VLC_SUCCESS )
142
    {
143
        msg_Err( p_vout, "no GLX support" );
144 145 146
        return VLC_EGENERIC;
    }

147
    if( E_(Activate)( p_this ) != VLC_SUCCESS )
148
    {
149
        return VLC_EGENERIC;
150 151
    }

152
    /* Set the function pointer */
153
    p_vout->pf_init = InitOpenGL;
154 155
    p_vout->pf_swap = SwapBuffers;
    p_vout->p_sys->b_glx13 = b_glx13;
156

157
    return VLC_SUCCESS;
158 159 160
}

/*****************************************************************************
161
 * DestroyOpenGL: destroys an OpenGL provider
162
 *****************************************************************************/
163
static void DestroyOpenGL( vlc_object_t *p_this )
164
{
165
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
166 167
    vout_sys_t *p_sys = p_vout->p_sys;

168 169
    glXDestroyContext( p_sys->p_display, p_sys->gwctx );
    if( p_sys->b_glx13 )
170
    {
171
        glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
172 173
    }

174
    E_(Deactivate)( p_this );
175 176 177 178 179
}

/*****************************************************************************
 * OpenDisplay: open and initialize OpenGL device
 *****************************************************************************/
180
static int CheckGLX( vlc_object_t *p_this, vlc_bool_t *b_glx13 )
181
{
182 183 184
    Display *p_display = NULL;
    int i_opcode, i_evt, i_err = 0;
    int i_maj, i_min = 0;
185 186

    /* Open the display */
187
    p_display = XOpenDisplay( NULL );
188
    if( p_display == NULL )
189
    {
190 191
        msg_Err( p_this, "Cannot open display" );
        return VLC_EGENERIC;
192 193 194 195 196
    }

    /* Check for GLX extension */
    if( !XQueryExtension( p_display, "GLX", &i_opcode, &i_evt, &i_err ) )
    {
197
        msg_Err( p_this, "GLX extension not supported" );
198
        XCloseDisplay( p_display );
199
        return VLC_EGENERIC;
200 201 202
    }
    if( !glXQueryExtension( p_display, &i_err, &i_evt ) )
    {
203
        msg_Err( p_this, "glXQueryExtension failed" );
204
        XCloseDisplay( p_display );
205
        return VLC_EGENERIC;
206 207 208 209 210
    }

    /* Check GLX version */
    if (!glXQueryVersion( p_display, &i_maj, &i_min ) )
    {
211
        msg_Err( p_this, "glXQueryVersion failed" );
212
        XCloseDisplay( p_display );
213
        return VLC_EGENERIC;
214 215 216
    }
    if( i_maj <= 0 || ((i_maj == 1) && (i_min < 3)) )
    {
217 218
        *b_glx13 = VLC_FALSE;
        msg_Dbg( p_this, "Using GLX 1.2 API" );
219 220 221
    }
    else
    {
222 223
        *b_glx13 = VLC_TRUE;
        msg_Dbg( p_this, "Using GLX 1.3 API" );
224 225
    }

226
    XCloseDisplay( p_display );
227
    return VLC_SUCCESS;
228 229
}

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
/*****************************************************************************
 * InitOpenGL: initializes OpenGL provider
 *****************************************************************************/
static int InitOpenGL( vout_thread_t *p_vout )
{
    /* Initialize GLX */
    if( !p_vout->p_sys->b_glx13 )
    {
        if( InitGLX12( p_vout ) != VLC_SUCCESS )
        {
            return VLC_EGENERIC;
        }
    }
    else
    {
        if( InitGLX13( p_vout ) != VLC_SUCCESS )
        {
            return VLC_EGENERIC;
        }
    }

    /* Set the OpenGL context _for the current thread_ */
    SwitchContext( p_vout );

    return VLC_SUCCESS;
}

257
int InitGLX12( vout_thread_t *p_vout )
258
{
259
    vout_sys_t *p_sys = p_vout->p_sys;
260 261
    XVisualInfo *p_vi;
    int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
262
                     GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
263

264 265
    p_vi = glXChooseVisual( p_sys->p_display,
                            DefaultScreen( p_sys->p_display), p_attr );
266 267
    if(! p_vi )
    {
268 269
        msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
        return VLC_EGENERIC;
270 271
    }

272 273 274 275
    /* Create an OpenGL context */
    p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
    XFree( p_vi );
    if( !p_sys->gwctx )
276
    {
277 278
        msg_Err( p_vout, "Cannot create OpenGL context");
        return VLC_EGENERIC;
279 280
    }

281
    return VLC_SUCCESS;
282 283
}

284
int InitGLX13( vout_thread_t *p_vout )
285
{
286
    vout_sys_t *p_sys = p_vout->p_sys;
287 288 289 290 291 292 293 294
    int i_nbelem;
    GLXFBConfig *p_fbconfs, fbconf;
    XVisualInfo *p_vi;
    int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
                     GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
                     GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };

    /* Get the FB configuration */
295
    p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
296 297
    if( (i_nbelem <= 0) || !p_fbconfs )
    {
298 299 300
        msg_Err( p_vout, "Cannot get FB configurations");
        if( p_fbconfs ) XFree( p_fbconfs );
        return VLC_EGENERIC;
301 302 303 304
    }
    fbconf = p_fbconfs[0];

    /* Get the X11 visual */
305
    p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
306 307
    if( !p_vi )
    {
308
        msg_Err( p_vout, "Cannot get X11 visual" );
309
        XFree( p_fbconfs );
310
        return VLC_EGENERIC;
311 312 313 314
    }
    XFree( p_vi );

    /* Create the GLX window */
315 316 317
    p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
                                   p_sys->p_win->video_window, NULL );
    if( p_sys->gwnd == None )
318
    {
319 320
        msg_Err( p_vout, "Cannot create GLX window" );
        return VLC_EGENERIC;
321 322 323
    }

    /* Create an OpenGL context */
324 325 326 327
    p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
                                        GLX_RGBA_TYPE, NULL, True );
    XFree( p_fbconfs );
    if( !p_sys->gwctx )
328
    {
329 330
        msg_Err( p_vout, "Cannot create OpenGL context");
        return VLC_EGENERIC;
331 332
    }

333
    return VLC_SUCCESS;
334 335
}

336 337 338 339
/*****************************************************************************
 * SwapBuffers: swap front/back buffers
 *****************************************************************************/
static void SwapBuffers( vout_thread_t *p_vout )
340
{
341
    vout_sys_t *p_sys = p_vout->p_sys;
342 343 344 345 346 347 348
    int i_width, i_height, i_x, i_y;

    vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
                       p_vout->p_sys->p_win->i_height,
                       &i_x, &i_y, &i_width, &i_height );

    glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
349

350 351 352
    if( p_sys->b_glx13 )
    {
        glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
353 354 355
    }
    else
    {
356
        glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
357 358 359
    }
}

360
void SwitchContext( vout_thread_t *p_vout )
361
{
362 363
    vout_sys_t *p_sys = p_vout->p_sys;

364
    /* Change the current OpenGL context */
365
    if( p_sys->b_glx13 )
366
    {
367 368
        glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
                               p_sys->gwnd, p_sys->gwctx );
369 370 371
    }
    else
    {
372 373
        glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,
                        p_sys->gwctx );
374 375
    }
}