Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

opengl.c

00001 /*****************************************************************************
00002  * opengl.c: OpenGL video output
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: opengl.c 12994 2005-10-28 08:21:18Z gbazin $
00006  *
00007  * Authors: Cyril Deguet <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *          Eric Petit <[email protected]>
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027  * Preamble
00028  *****************************************************************************/
00029 #include <errno.h>                                                 /* ENOMEM */
00030 #include <stdlib.h>                                      /* malloc(), free() */
00031 #include <string.h>
00032 
00033 #include <vlc/vlc.h>
00034 #include <vlc/vout.h>
00035 
00036 #ifdef SYS_DARWIN
00037 #include <OpenGL/gl.h>
00038 #include <OpenGL/glext.h>
00039 
00040 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
00041    This allows sizes which are not powers of 2 */
00042 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
00043 
00044 /* OS X OpenGL supports YUV. Hehe. */
00045 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
00046 #define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
00047 #else
00048 
00049 #include <GL/gl.h>
00050 #define VLCGL_TARGET GL_TEXTURE_2D
00051 
00052 /* RV16 */
00053 #ifndef GL_UNSIGNED_SHORT_5_6_5
00054 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
00055 #endif
00056 //#define VLCGL_RGB_FORMAT GL_RGB
00057 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
00058 
00059 /* RV24 */
00060 //#define VLCGL_RGB_FORMAT GL_RGB
00061 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
00062 
00063 /* RV32 */
00064 #define VLCGL_RGB_FORMAT GL_RGBA
00065 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
00066 
00067 /* YUY2 */
00068 #ifndef YCBCR_MESA
00069 #define YCBCR_MESA 0x8757
00070 #endif
00071 #ifndef UNSIGNED_SHORT_8_8_MESA
00072 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
00073 #endif
00074 #define VLCGL_YUV_FORMAT YCBCR_MESA
00075 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
00076 
00077 /* Use RGB on Win32/GLX */
00078 #define VLCGL_FORMAT VLCGL_RGB_FORMAT
00079 #define VLCGL_TYPE   VLCGL_RGB_TYPE
00080 //#define VLCGL_FORMAT VLCGL_YUV_FORMAT
00081 //#define VLCGL_TYPE   VLCGL_YUV_TYPE
00082 #endif
00083 
00084 #ifndef GL_CLAMP_TO_EDGE
00085 #   define GL_CLAMP_TO_EDGE 0x812F
00086 #endif
00087 
00088 /* OpenGL effects */
00089 #define OPENGL_EFFECT_NONE             1
00090 #define OPENGL_EFFECT_CUBE             2
00091 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
00092 
00093 /*****************************************************************************
00094  * Vout interface
00095  *****************************************************************************/
00096 static int  CreateVout   ( vlc_object_t * );
00097 static void DestroyVout  ( vlc_object_t * );
00098 static int  Init         ( vout_thread_t * );
00099 static void End          ( vout_thread_t * );
00100 static int  Manage       ( vout_thread_t * );
00101 static void Render       ( vout_thread_t *, picture_t * );
00102 static void DisplayVideo ( vout_thread_t *, picture_t * );
00103 static int  Control      ( vout_thread_t *, int, va_list );
00104 
00105 static inline int GetAlignedSize( int );
00106 
00107 static int InitTextures( vout_thread_t * );
00108 static int SendEvents( vlc_object_t *, char const *,
00109                        vlc_value_t, vlc_value_t, void * );
00110 
00111 /*****************************************************************************
00112  * Module descriptor
00113  *****************************************************************************/
00114 #define SPEED_TEXT N_( "OpenGL cube rotation speed" )
00115 /*****************************************************************************
00116  * Module descriptor
00117  *****************************************************************************/
00118 #define SPEED_TEXT N_( "OpenGL cube rotation speed" )
00119 #define SPEED_LONGTEXT N_( "If the OpenGL cube effect is enabled, this " \
00120                            "controls its rotation speed." )
00121 
00122 #define EFFECT_TEXT N_("Select effect")
00123 #define EFFECT_LONGTEXT N_( \
00124     "Allows you to select different visual effects.")
00125 
00126 static char *ppsz_effects[] = {
00127         "none", "cube", "transparent-cube" };
00128 static char *ppsz_effects_text[] = {
00129         N_("None"), N_("Cube"), N_("Transparent Cube") };
00130 
00131 vlc_module_begin();
00132     set_shortname( "OpenGL" );
00133     set_category( CAT_VIDEO );
00134     set_subcategory( SUBCAT_VIDEO_VOUT );
00135     set_description( _("OpenGL video output") );
00136 #ifdef SYS_DARWIN
00137     set_capability( "video output", 200 );
00138 #else
00139     set_capability( "video output", 20 );
00140 #endif
00141     add_shortcut( "opengl" );
00142     add_float( "opengl-cube-speed", 2.0, NULL, SPEED_TEXT,
00143                     SPEED_LONGTEXT, VLC_TRUE );
00144     set_callbacks( CreateVout, DestroyVout );
00145     add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
00146                  EFFECT_LONGTEXT, VLC_FALSE );
00147         change_string_list( ppsz_effects, ppsz_effects_text, 0 );
00148 vlc_module_end();
00149 
00150 /*****************************************************************************
00151  * vout_sys_t: video output method descriptor
00152  *****************************************************************************
00153  * This structure is part of the video output thread descriptor.
00154  * It describes the OpenGL specific properties of the output thread.
00155  *****************************************************************************/
00156 struct vout_sys_t
00157 {
00158     vout_thread_t *p_vout;
00159 
00160     uint8_t    *pp_buffer[2];
00161     int         i_index;
00162     int         i_tex_width;
00163     int         i_tex_height;
00164     GLuint      p_textures[2];
00165 
00166     int         i_effect;
00167 
00168     float       f_speed;
00169 };
00170 
00171 /*****************************************************************************
00172  * CreateVout: This function allocates and initializes the OpenGL vout method.
00173  *****************************************************************************/
00174 static int CreateVout( vlc_object_t *p_this )
00175 {
00176     vout_thread_t *p_vout = (vout_thread_t *)p_this;
00177     vout_sys_t *p_sys;
00178 
00179     /* Allocate structure */
00180     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
00181     if( p_sys == NULL )
00182     {
00183         msg_Err( p_vout, "out of memory" );
00184         return VLC_EGENERIC;
00185     }
00186 
00187     var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
00188 
00189     p_sys->i_index = 0;
00190 #ifdef SYS_DARWIN
00191     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
00192     p_sys->i_tex_height = p_vout->fmt_in.i_height;
00193 #else
00194     /* A texture must have a size aligned on a power of 2 */
00195     p_sys->i_tex_width  = GetAlignedSize( p_vout->fmt_in.i_width );
00196     p_sys->i_tex_height = GetAlignedSize( p_vout->fmt_in.i_height );
00197 #endif
00198 
00199     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
00200              p_sys->i_tex_height );
00201 
00202     /* Get window */
00203     p_sys->p_vout =
00204         (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
00205     if( p_sys->p_vout == NULL )
00206     {
00207         msg_Err( p_vout, "out of memory" );
00208         return VLC_ENOMEM;
00209     }
00210     vlc_object_attach( p_sys->p_vout, p_this );
00211 
00212     p_sys->p_vout->i_window_width = p_vout->i_window_width;
00213     p_sys->p_vout->i_window_height = p_vout->i_window_height;
00214     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
00215     p_sys->p_vout->render.i_width = p_vout->render.i_width;
00216     p_sys->p_vout->render.i_height = p_vout->render.i_height;
00217     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
00218     p_sys->p_vout->fmt_render = p_vout->fmt_render;
00219     p_sys->p_vout->fmt_in = p_vout->fmt_in;
00220     p_sys->p_vout->b_scale = p_vout->b_scale;
00221     p_sys->p_vout->i_alignment = p_vout->i_alignment;
00222 
00223     p_sys->p_vout->p_module =
00224         module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
00225     if( p_sys->p_vout->p_module == NULL )
00226     {
00227         msg_Warn( p_vout, "No OpenGL provider found" );
00228         vlc_object_detach( p_sys->p_vout );
00229         vlc_object_destroy( p_sys->p_vout );
00230         return VLC_ENOOBJ;
00231     }
00232 
00233     p_sys->f_speed = var_CreateGetFloat( p_vout, "opengl-cube-speed" );
00234 
00235     p_vout->pf_init = Init;
00236     p_vout->pf_end = End;
00237     p_vout->pf_manage = Manage;
00238     p_vout->pf_render = Render;
00239     p_vout->pf_display = DisplayVideo;
00240     p_vout->pf_control = Control;
00241 
00242     /* Forward events from the opengl provider */
00243     var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
00244     var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
00245     var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
00246     var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_INTEGER );
00247     var_Create( p_sys->p_vout, "mouse-button-down", VLC_VAR_INTEGER );
00248     var_Create( p_sys->p_vout, "video-on-top",
00249                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
00250 
00251     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
00252     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
00253     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
00254     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
00255     var_AddCallback( p_sys->p_vout, "mouse-button-down", SendEvents, p_vout );
00256 
00257     return VLC_SUCCESS;
00258 }
00259 
00260 /*****************************************************************************
00261  * Init: initialize the OpenGL video thread output method
00262  *****************************************************************************/
00263 static int Init( vout_thread_t *p_vout )
00264 {
00265     vout_sys_t *p_sys = p_vout->p_sys;
00266     int i_pixel_pitch;
00267     vlc_value_t val;
00268 
00269     p_sys->p_vout->pf_init( p_sys->p_vout );
00270 
00271 #if defined( SYS_DARWIN ) || (VLCGL_FORMAT == YCBCR_MESA)
00272     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
00273     i_pixel_pitch = 2;
00274 
00275 #elif VLCGL_FORMAT == GL_RGB
00276 #   if VLCGL_TYPE == GL_UNSIGNED_BYTE
00277     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
00278     p_vout->output.i_rmask = 0x000000ff;
00279     p_vout->output.i_gmask = 0x0000ff00;
00280     p_vout->output.i_bmask = 0x00ff0000;
00281     i_pixel_pitch = 3;
00282 #   else
00283     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
00284     p_vout->output.i_rmask = 0xf800;
00285     p_vout->output.i_gmask = 0x07e0;
00286     p_vout->output.i_bmask = 0x001f;
00287     i_pixel_pitch = 2;
00288 #   endif
00289 #else
00290     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
00291     p_vout->output.i_rmask = 0x000000ff;
00292     p_vout->output.i_gmask = 0x0000ff00;
00293     p_vout->output.i_bmask = 0x00ff0000;
00294     i_pixel_pitch = 4;
00295 #endif
00296 
00297     /* Since OpenGL can do rescaling for us, stick to the default
00298      * coordinates and aspect. */
00299     p_vout->output.i_width  = p_vout->render.i_width;
00300     p_vout->output.i_height = p_vout->render.i_height;
00301     p_vout->output.i_aspect = p_vout->render.i_aspect;
00302 
00303     p_vout->fmt_out = p_vout->fmt_in;
00304     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
00305 
00306     /* We know the chroma, allocate one buffer which will be used
00307      * directly by the decoder */
00308     p_sys->pp_buffer[0] =
00309         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
00310     if( !p_sys->pp_buffer[0] )
00311     {
00312         msg_Err( p_vout, "Out of memory" );
00313         return -1;
00314     }
00315     p_sys->pp_buffer[1] =
00316         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
00317     if( !p_sys->pp_buffer[1] )
00318     {
00319         msg_Err( p_vout, "Out of memory" );
00320         return -1;
00321     }
00322 
00323     p_vout->p_picture[0].i_planes = 1;
00324     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
00325     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
00326     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
00327     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
00328     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
00329         p_vout->p_picture[0].p->i_pixel_pitch;
00330     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
00331         p_vout->p_picture[0].p->i_pixel_pitch;
00332 
00333     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
00334     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
00335 
00336     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
00337 
00338     I_OUTPUTPICTURES = 1;
00339 
00340     if( p_sys->p_vout->pf_lock &&
00341         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
00342     {
00343         msg_Warn( p_vout, "could not lock OpenGL provider" );
00344         return 0;
00345     }
00346 
00347     InitTextures( p_vout );
00348 
00349     glDisable(GL_BLEND);
00350     glDisable(GL_DEPTH_TEST);
00351     glDepthMask(GL_FALSE);
00352     glDisable(GL_CULL_FACE);
00353     glClear( GL_COLOR_BUFFER_BIT );
00354 
00355     /* Check if the user asked for useless visual effects */
00356     var_Get( p_vout, "opengl-effect", &val );
00357     if( !val.psz_string || !strcmp( val.psz_string, "none" ))
00358     {
00359         p_sys->i_effect = OPENGL_EFFECT_NONE;
00360     }
00361     else if( !strcmp( val.psz_string, "cube" ) )
00362     {
00363         p_sys->i_effect = OPENGL_EFFECT_CUBE;
00364 
00365         glEnable( GL_CULL_FACE);
00366         //glEnable( GL_DEPTH_TEST );
00367     }
00368     else if( !strcmp( val.psz_string, "transparent-cube" ) )
00369     {
00370         p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
00371 
00372         glDisable( GL_DEPTH_TEST );
00373         glEnable( GL_BLEND );
00374         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
00375     }
00376     else
00377     {
00378         msg_Warn( p_vout, "no valid opengl effect provided, using "
00379                   "\"none\"" );
00380         p_sys->i_effect = OPENGL_EFFECT_NONE;
00381     }
00382     if( val.psz_string ) free( val.psz_string );
00383 
00384     if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
00385                 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
00386     {
00387         /* Set the perpective */
00388         glMatrixMode( GL_PROJECTION );
00389         glLoadIdentity();
00390         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
00391         glMatrixMode( GL_MODELVIEW );
00392         glLoadIdentity();
00393         glTranslatef( 0.0, 0.0, - 5.0 );
00394     }
00395 
00396     if( p_sys->p_vout->pf_unlock )
00397     {
00398         p_sys->p_vout->pf_unlock( p_sys->p_vout );
00399     }
00400 
00401     return 0;
00402 }
00403 
00404 /*****************************************************************************
00405  * End: terminate GLX video thread output method
00406  *****************************************************************************/
00407 static void End( vout_thread_t *p_vout )
00408 {
00409     vout_sys_t *p_sys = p_vout->p_sys;
00410 
00411     if( p_sys->p_vout->pf_lock &&
00412         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
00413     {
00414         msg_Warn( p_vout, "could not lock OpenGL provider" );
00415         return;
00416     }
00417 
00418     glFinish();
00419     glFlush();
00420 
00421     if( p_sys->p_vout->pf_unlock )
00422     {
00423         p_sys->p_vout->pf_unlock( p_sys->p_vout );
00424     }
00425 }
00426 
00427 /*****************************************************************************
00428  * Destroy: destroy GLX video thread output method
00429  *****************************************************************************
00430  * Terminate an output method created by CreateVout
00431  *****************************************************************************/
00432 static void DestroyVout( vlc_object_t *p_this )
00433 {
00434     vout_thread_t *p_vout = (vout_thread_t *)p_this;
00435     vout_sys_t *p_sys = p_vout->p_sys;
00436 
00437     module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
00438     vlc_object_detach( p_sys->p_vout );
00439     vlc_object_destroy( p_sys->p_vout );
00440 
00441     /* Free the texture buffer*/
00442     if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
00443     if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
00444 
00445     free( p_sys );
00446 }
00447 
00448 /*****************************************************************************
00449  * Manage: handle Sys events
00450  *****************************************************************************
00451  * This function should be called regularly by video output thread. It returns
00452  * a non null value if an error occurred.
00453  *****************************************************************************/
00454 static int Manage( vout_thread_t *p_vout )
00455 {
00456     vout_sys_t *p_sys = p_vout->p_sys;
00457     int i_ret, i_fullscreen_change;
00458 
00459     i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
00460 
00461     p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
00462         p_vout->fmt_in.i_x_offset;
00463     p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
00464         p_vout->fmt_in.i_y_offset;
00465     p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
00466         p_vout->fmt_in.i_visible_width;
00467     p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
00468         p_vout->fmt_in.i_visible_height;
00469     p_vout->fmt_out.i_aspect = p_sys->p_vout->fmt_in.i_aspect =
00470         p_vout->fmt_in.i_aspect;
00471     p_vout->fmt_out.i_sar_num = p_sys->p_vout->fmt_in.i_sar_num =
00472         p_vout->fmt_in.i_sar_num;
00473     p_vout->fmt_out.i_sar_den = p_sys->p_vout->fmt_in.i_sar_den =
00474         p_vout->fmt_in.i_sar_den;
00475     p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
00476 
00477     p_sys->p_vout->i_changes = p_vout->i_changes;
00478     i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
00479     p_vout->i_changes = p_sys->p_vout->i_changes;
00480 
00481 #ifdef SYS_DARWIN
00482     if( p_sys->p_vout->pf_lock &&
00483         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
00484     {
00485         msg_Warn( p_vout, "could not lock OpenGL provider" );
00486         return i_ret;
00487     }
00488 
00489     /* On OS X, we create the window and the GL view when entering
00490        fullscreen - the textures have to be inited again */
00491     if( i_fullscreen_change )
00492     {
00493         InitTextures( p_vout );
00494 
00495         switch( p_sys->i_effect )
00496         {
00497             case OPENGL_EFFECT_CUBE:
00498                 glEnable( GL_CULL_FACE );
00499                 break;
00500 
00501             case OPENGL_EFFECT_TRANSPARENT_CUBE:
00502                 glDisable( GL_DEPTH_TEST );
00503                 glEnable( GL_BLEND );
00504                 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
00505                 break;
00506         }
00507 
00508         if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
00509                     OPENGL_EFFECT_TRANSPARENT_CUBE ) )
00510         {
00511             /* Set the perpective */
00512             glMatrixMode( GL_PROJECTION );
00513             glLoadIdentity();
00514             glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
00515             glMatrixMode( GL_MODELVIEW );
00516             glLoadIdentity();
00517             glTranslatef( 0.0, 0.0, - 5.0 );
00518         }
00519     }
00520 
00521     if( p_sys->p_vout->pf_unlock )
00522     {
00523         p_sys->p_vout->pf_unlock( p_sys->p_vout );
00524     }
00525 #endif
00526 
00527     return i_ret;
00528 }
00529 
00530 /*****************************************************************************
00531  * Render: render previously calculated output
00532  *****************************************************************************/
00533 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
00534 {
00535     vout_sys_t *p_sys = p_vout->p_sys;
00536 
00537     /* On Win32/GLX, we do this the usual way:
00538        + Fill the buffer with new content,
00539        + Reload the texture,
00540        + Use the texture.
00541 
00542        On OS X with VRAM or AGP texturing, the order has to be:
00543        + Reload the texture,
00544        + Fill the buffer with new content,
00545        + Use the texture.
00546 
00547        (Thanks to gcc from the Arstechnica forums for the tip)
00548 
00549        Therefore, we have to use two buffers and textures. On Win32/GLX,
00550        we reload the texture to be displayed and use it right away. On
00551        OS X, we first render, then reload the texture to be used next
00552        time. */
00553 
00554     if( p_sys->p_vout->pf_lock &&
00555         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
00556     {
00557         msg_Warn( p_vout, "could not lock OpenGL provider" );
00558         return;
00559     }
00560 
00561 #ifdef SYS_DARWIN
00562     int i_new_index;
00563     i_new_index = ( p_sys->i_index + 1 ) & 1;
00564 
00565 
00566     /* Update the texture */
00567     glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
00568     glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
00569                      p_vout->fmt_out.i_width,
00570                      p_vout->fmt_out.i_height,
00571                      VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
00572 
00573     /* Bind to the previous texture for drawing */
00574     glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
00575 
00576     /* Switch buffers */
00577     p_sys->i_index = i_new_index;
00578     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
00579 
00580 #else
00581     /* Update the texture */
00582     glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
00583                      p_vout->fmt_out.i_width,
00584                      p_vout->fmt_out.i_height,
00585                      VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[0] );
00586 #endif
00587 
00588     if( p_sys->p_vout->pf_unlock )
00589     {
00590         p_sys->p_vout->pf_unlock( p_sys->p_vout );
00591     }
00592 }
00593 
00594 /*****************************************************************************
00595  * DisplayVideo: displays previously rendered output
00596  *****************************************************************************/
00597 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
00598 {
00599     vout_sys_t *p_sys = p_vout->p_sys;
00600     float f_width, f_height, f_x, f_y;
00601 
00602     if( p_sys->p_vout->pf_lock &&
00603         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
00604     {
00605         msg_Warn( p_vout, "could not lock OpenGL provider" );
00606         return;
00607     }
00608 
00609     /* glTexCoord works differently with GL_TEXTURE_2D and
00610        GL_TEXTURE_RECTANGLE_EXT */
00611 #ifdef SYS_DARWIN
00612     f_x = (float)p_vout->fmt_out.i_x_offset;
00613     f_y = (float)p_vout->fmt_out.i_y_offset;
00614     f_width = (float)p_vout->fmt_out.i_x_offset +
00615               (float)p_vout->fmt_out.i_visible_width;
00616     f_height = (float)p_vout->fmt_out.i_y_offset +
00617                (float)p_vout->fmt_out.i_visible_height;
00618 #else
00619     f_x = (float)p_vout->fmt_out.i_x_offset / p_sys->i_tex_width;
00620     f_y = (float)p_vout->fmt_out.i_y_offset / p_sys->i_tex_height;
00621     f_width = ( (float)p_vout->fmt_out.i_x_offset +
00622                 p_vout->fmt_out.i_visible_width ) / p_sys->i_tex_width;
00623     f_height = ( (float)p_vout->fmt_out.i_y_offset +
00624                  p_vout->fmt_out.i_visible_height ) / p_sys->i_tex_height;
00625 #endif
00626 
00627     /* Why drawing here and not in Render()? Because this way, the
00628        OpenGL providers can call pf_display to force redraw. Currently,
00629        the OS X provider uses it to get a smooth window resizing */
00630 
00631     glClear( GL_COLOR_BUFFER_BIT );
00632 
00633     if( p_sys->i_effect == OPENGL_EFFECT_NONE )
00634     {
00635         glEnable( VLCGL_TARGET );
00636         glBegin( GL_POLYGON );
00637         glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
00638         glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
00639         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
00640         glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
00641         glEnd();
00642     }
00643     else
00644     {
00645         glRotatef( 0.5 * p_sys->f_speed , 0.3, 0.5, 0.7 );
00646 
00647         glEnable( VLCGL_TARGET );
00648         glBegin( GL_QUADS );
00649 
00650         /* Front */
00651         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
00652         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
00653         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
00654         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
00655 
00656         /* Left */
00657         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
00658         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
00659         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
00660         glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
00661 
00662         /* Back */
00663         glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
00664         glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
00665         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
00666         glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
00667 
00668         /* Right */
00669         glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
00670         glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
00671         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
00672         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
00673 
00674         /* Top */
00675         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
00676         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
00677         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
00678         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
00679 
00680         /* Bottom */
00681         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, - 1.0, 1.0 );
00682         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
00683         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
00684         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, - 1.0, 1.0 );
00685         glEnd();
00686     }
00687 
00688     glDisable( VLCGL_TARGET );
00689 
00690     p_sys->p_vout->pf_swap( p_sys->p_vout );
00691 
00692     if( p_sys->p_vout->pf_unlock )
00693     {
00694         p_sys->p_vout->pf_unlock( p_sys->p_vout );
00695     }
00696 }
00697 
00698 int GetAlignedSize( int i_size )
00699 {
00700     /* Return the nearest power of 2 */
00701     int i_result = 1;
00702     while( i_result < i_size )
00703     {
00704         i_result *= 2;
00705     }
00706     return i_result;
00707 }
00708 
00709 /*****************************************************************************
00710  * Control: control facility for the vout
00711  *****************************************************************************/
00712 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
00713 {
00714     vout_sys_t *p_sys = p_vout->p_sys;
00715 
00716     switch( i_query )
00717     {
00718     case VOUT_SNAPSHOT:
00719         return vout_vaControlDefault( p_vout, i_query, args );
00720 
00721     default:
00722         if( p_sys->p_vout->pf_control )
00723             return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
00724         else
00725             return vout_vaControlDefault( p_vout, i_query, args );
00726     }
00727 }
00728 
00729 static int InitTextures( vout_thread_t *p_vout )
00730 {
00731     vout_sys_t *p_sys = p_vout->p_sys;
00732     int i_index;
00733 
00734     glDeleteTextures( 2, p_sys->p_textures );
00735     glGenTextures( 2, p_sys->p_textures );
00736 
00737     for( i_index = 0; i_index < 2; i_index++ )
00738     {
00739         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
00740     
00741         /* Set the texture parameters */
00742         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
00743     
00744         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
00745         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
00746         
00747         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00748         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00749 
00750         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00751 
00752 #ifdef SYS_DARWIN
00753         /* Tell the driver not to make a copy of the texture but to use
00754            our buffer */
00755         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
00756         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
00757     
00758 #if 0
00759         /* Use VRAM texturing */
00760         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
00761                          GL_STORAGE_CACHED_APPLE );
00762 #else
00763         /* Use AGP texturing */
00764         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
00765                          GL_STORAGE_SHARED_APPLE );
00766 #endif
00767 #endif
00768 
00769         /* Call glTexImage2D only once, and use glTexSubImage2D later */
00770         glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
00771                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
00772                       p_sys->pp_buffer[i_index] );
00773     }
00774 
00775     return 0;
00776 }
00777 
00778 /*****************************************************************************
00779  * SendEvents: forward mouse and keyboard events to the parent p_vout
00780  *****************************************************************************/
00781 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
00782                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
00783 {
00784     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
00785 }

Generated on Tue Dec 20 10:14:55 2005 for vlc-0.8.4a by  doxygen 1.4.2