]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
Patch to force alignment in OpenGL if it isn't correctlt positionned before, as panor...
[vlc] / modules / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: OpenGL video output
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  *          Eric Petit <titer@m0k.org>
10  *          Cedric Cocquebert <cedric.cocquebert@supelec.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <string.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc_vout.h>
36
37 #ifdef __APPLE__
38 #include <OpenGL/gl.h>
39 #include <OpenGL/glext.h>
40
41 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
42    This allows sizes which are not powers of 2 */
43 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
44
45 /* OS X OpenGL supports YUV. Hehe. */
46 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
47 #define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
48 #else
49
50 #include <GL/gl.h>
51 #define VLCGL_TARGET GL_TEXTURE_2D
52
53 /* RV16 */
54 #ifndef GL_UNSIGNED_SHORT_5_6_5
55 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
56 #endif
57 //#define VLCGL_RGB_FORMAT GL_RGB
58 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
59
60 /* RV24 */
61 //#define VLCGL_RGB_FORMAT GL_RGB
62 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
63
64 /* RV32 */
65 #define VLCGL_RGB_FORMAT GL_RGBA
66 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
67
68 /* YUY2 */
69 #ifndef YCBCR_MESA
70 #define YCBCR_MESA 0x8757
71 #endif
72 #ifndef UNSIGNED_SHORT_8_8_MESA
73 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
74 #endif
75 #define VLCGL_YUV_FORMAT YCBCR_MESA
76 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
77
78 /* Use RGB on Win32/GLX */
79 #define VLCGL_FORMAT VLCGL_RGB_FORMAT
80 #define VLCGL_TYPE   VLCGL_RGB_TYPE
81 //#define VLCGL_FORMAT VLCGL_YUV_FORMAT
82 //#define VLCGL_TYPE   VLCGL_YUV_TYPE
83 #endif
84
85 #ifndef GL_CLAMP_TO_EDGE
86 #   define GL_CLAMP_TO_EDGE 0x812F
87 #endif
88
89 /* OpenGL effects */
90 #define OPENGL_EFFECT_NONE             1
91 #define OPENGL_EFFECT_CUBE             2
92 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
93 #if defined( HAVE_GL_GLU_H ) || defined( __APPLE__ )
94     #define OPENGL_MORE_EFFECT           8
95 #endif
96
97 #ifdef OPENGL_MORE_EFFECT
98     #include <math.h>
99     #ifdef __APPLE__
100         #include <OpenGL/glu.h>
101     #else
102         #include <GL/glu.h>
103     #endif
104 /* 3D MODEL */
105     #define CYLINDER 8
106     #define TORUS     16
107     #define SPHERE   32
108 /*GRID2D TRANSFORMATION */
109     #define SQUAREXY 64
110     #define SQUARER  128
111     #define ASINXY   256
112     #define ASINR    512
113     #define SINEXY   1024
114     #define SINER    2048
115     #define INIFILE     4096                    // not used, just for mark end ...
116     #define SIGN(x)     (x < 0 ? (-1) : 1)
117     #define PID2     1.570796326794896619231322
118
119     static const char *ppsz_effects[] = {
120             "none", "cube", "transparent-cube", "cylinder", "torus", "sphere","SQUAREXY","SQUARER", "ASINXY", "ASINR", "SINEXY", "SINER" };
121     static const char *ppsz_effects_text[] = {
122             N_("None"), N_("Cube"), N_("Transparent Cube"), 
123             N_("Cylinder"), N_("Torus"), N_("Sphere"), N_("SQUAREXY"),N_("SQUARER"), N_("ASINXY"), N_("ASINR"), N_("SINEXY"), N_("SINER") };
124 #endif
125
126 /*****************************************************************************
127  * Vout interface
128  *****************************************************************************/
129 static int  CreateVout   ( vlc_object_t * );
130 static void DestroyVout  ( vlc_object_t * );
131 static int  Init         ( vout_thread_t * );
132 static void End          ( vout_thread_t * );
133 static int  Manage       ( vout_thread_t * );
134 static void Render       ( vout_thread_t *, picture_t * );
135 static void DisplayVideo ( vout_thread_t *, picture_t * );
136 static int  Control      ( vout_thread_t *, int, va_list );
137
138 static inline int GetAlignedSize( int );
139
140 static int InitTextures  ( vout_thread_t * );
141 static int SendEvents    ( vlc_object_t *, char const *,
142                            vlc_value_t, vlc_value_t, void * );
143
144 #ifdef OPENGL_MORE_EFFECT                       
145 static float Z_Compute   ( float, int, float, float );
146 static void Transform    ( float, int, float, float, int, int, int, int, double *, double * );
147
148 /*****************************************************************************
149  * Module descriptor
150  *****************************************************************************/
151 #define ACCURACY_TEXT N_( "OpenGL sampling accuracy " )
152 #define ACCURACY_LONGTEXT N_( "Select the accuracy of 3D object sampling(1 = min and 10 = max)" )
153 #define RADIUS_TEXT N_( "OpenGL Cylinder radius" )
154 #define RADIUS_LONGTEXT N_( "Radius of the OpenGL cylinder effect, if enabled" )
155 #define POV_X_TEXT N_("Point of view x-coordinate")
156 #define POV_X_LONGTEXT N_("Point of view (X coordinate) of the cube/cylinder "\
157                            "effect, if enabled.")
158 #define POV_Y_TEXT N_("Point of view y-coordinate")
159 #define POV_Y_LONGTEXT N_("Point of view (Y coordinate) of the cube/cylinder "\
160                            "effect, if enabled.")
161 #define POV_Z_TEXT N_("Point of view z-coordinate")
162 #define POV_Z_LONGTEXT N_("Point of view (Z coordinate) of the cube/cylinder "\
163                            "effect, if enabled.")
164 #endif
165 #define SPEED_TEXT N_( "OpenGL cube rotation speed" )
166 #define SPEED_LONGTEXT N_( "Rotation speed of the OpenGL cube effect, if " \
167         "enabled." )
168 #define EFFECT_TEXT N_("Effect")
169 #define EFFECT_LONGTEXT N_( \
170     "Several visual OpenGL effects are available." )
171
172 #ifndef OPENGL_MORE_EFFECT
173 static const char *ppsz_effects[] = {
174         "none", "cube", "transparent-cube" };
175 static const char *ppsz_effects_text[] = {
176         N_("None"), N_("Cube"), N_("Transparent Cube") };
177 #endif
178
179 vlc_module_begin();
180     set_shortname( "OpenGL" );
181     set_category( CAT_VIDEO );
182     set_subcategory( SUBCAT_VIDEO_VOUT );
183     set_description( _("OpenGL video output") );
184 #ifdef __APPLE__
185     set_capability( "video output", 200 );
186 #else
187     set_capability( "video output", 20 );
188 #endif
189     add_shortcut( "opengl" );
190     add_float( "opengl-cube-speed", 2.0, NULL, SPEED_TEXT,
191                     SPEED_LONGTEXT, VLC_TRUE );
192 #ifdef OPENGL_MORE_EFFECT
193     add_integer_with_range( "opengl-accuracy", 4, 1, 10, NULL, ACCURACY_TEXT,
194                     ACCURACY_LONGTEXT, VLC_TRUE );
195     add_float_with_range( "opengl-pov-x", 0.0, -1.0, 1.0, NULL, POV_X_TEXT,
196                     POV_X_LONGTEXT, VLC_TRUE );
197     add_float_with_range( "opengl-pov-y", 0.0, -1.0, 1.0, NULL, POV_Y_TEXT,
198                     POV_Y_LONGTEXT, VLC_TRUE );
199     add_float_with_range( "opengl-pov-z", -1.0, -1.0, 1.0, NULL, POV_Z_TEXT,
200                     POV_Z_LONGTEXT, VLC_TRUE );
201     add_float( "opengl-cylinder-radius", -100.0, NULL, RADIUS_TEXT,
202                     RADIUS_LONGTEXT, VLC_TRUE );
203
204 #endif
205     set_callbacks( CreateVout, DestroyVout );
206     add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
207                  EFFECT_LONGTEXT, VLC_FALSE );
208         change_string_list( ppsz_effects, ppsz_effects_text, 0 );
209 vlc_module_end();
210
211 /*****************************************************************************
212  * vout_sys_t: video output method descriptor
213  *****************************************************************************
214  * This structure is part of the video output thread descriptor.
215  * It describes the OpenGL specific properties of the output thread.
216  *****************************************************************************/
217 struct vout_sys_t
218 {
219     vout_thread_t *p_vout;
220
221     uint8_t    *pp_buffer[2];
222     int         i_index;
223     int         i_tex_width;
224     int         i_tex_height;
225     GLuint      p_textures[2];
226
227     int         i_effect;
228
229     float       f_speed;
230     float       f_radius;
231 };
232
233 /*****************************************************************************
234  * CreateVout: This function allocates and initializes the OpenGL vout method.
235  *****************************************************************************/
236 static int CreateVout( vlc_object_t *p_this )
237 {
238     vout_thread_t *p_vout = (vout_thread_t *)p_this;
239     vout_sys_t *p_sys;
240
241     /* Allocate structure */
242     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
243     if( p_sys == NULL )
244     {
245         msg_Err( p_vout, "out of memory" );
246         return VLC_EGENERIC;
247     }
248
249     var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
250
251     p_sys->i_index = 0;
252 #ifdef __APPLE__
253     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
254     p_sys->i_tex_height = p_vout->fmt_in.i_height;
255 #else
256     /* A texture must have a size aligned on a power of 2 */
257     p_sys->i_tex_width  = GetAlignedSize( p_vout->fmt_in.i_width );
258     p_sys->i_tex_height = GetAlignedSize( p_vout->fmt_in.i_height );
259 #endif
260
261     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
262              p_sys->i_tex_height );
263
264     /* Get window */
265     p_sys->p_vout =
266         (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
267     if( p_sys->p_vout == NULL )
268     {
269         msg_Err( p_vout, "out of memory" );
270         return VLC_ENOMEM;
271     }
272     vlc_object_attach( p_sys->p_vout, p_this );
273
274     p_sys->p_vout->i_window_width = p_vout->i_window_width;
275     p_sys->p_vout->i_window_height = p_vout->i_window_height;
276     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
277     p_sys->p_vout->render.i_width = p_vout->render.i_width;
278     p_sys->p_vout->render.i_height = p_vout->render.i_height;
279     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
280     p_sys->p_vout->fmt_render = p_vout->fmt_render;
281     p_sys->p_vout->fmt_in = p_vout->fmt_in;
282     p_sys->p_vout->b_scale = p_vout->b_scale;
283     p_sys->p_vout->i_alignment = p_vout->i_alignment;
284
285     p_sys->p_vout->p_module =
286         module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
287     if( p_sys->p_vout->p_module == NULL )
288     {
289         msg_Warn( p_vout, "No OpenGL provider found" );
290         vlc_object_detach( p_sys->p_vout );
291         vlc_object_destroy( p_sys->p_vout );
292         return VLC_ENOOBJ;
293     }
294
295     p_sys->f_speed = var_CreateGetFloat( p_vout, "opengl-cube-speed" );
296     p_sys->f_radius = var_CreateGetFloat( p_vout, "opengl-cylinder-radius" );
297
298     p_vout->pf_init = Init;
299     p_vout->pf_end = End;
300     p_vout->pf_manage = Manage;
301     p_vout->pf_render = Render;
302     p_vout->pf_display = DisplayVideo;
303     p_vout->pf_control = Control;
304
305     /* Forward events from the opengl provider */
306     var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
307     var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
308     var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
309     var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_INTEGER );
310     var_Create( p_sys->p_vout, "mouse-button-down", VLC_VAR_INTEGER );
311     var_Create( p_sys->p_vout, "video-on-top",
312                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
313
314     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
315     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
316     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
317     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
318     var_AddCallback( p_sys->p_vout, "mouse-button-down", SendEvents, p_vout );
319
320     return VLC_SUCCESS;
321 }
322
323 /*****************************************************************************
324  * Init: initialize the OpenGL video thread output method
325  *****************************************************************************/
326 static int Init( vout_thread_t *p_vout )
327 {
328     vout_sys_t *p_sys = p_vout->p_sys;
329     int i_pixel_pitch;
330     vlc_value_t val;
331
332     p_sys->p_vout->pf_init( p_sys->p_vout );
333
334 /* TODO: We use YCbCr on Mac which is Y422, but on OSX it seems to == YUY2. Verify */
335 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
336     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
337     i_pixel_pitch = 2;
338
339 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
340     p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
341     i_pixel_pitch = 2;
342
343 #elif VLCGL_FORMAT == GL_RGB
344 #   if VLCGL_TYPE == GL_UNSIGNED_BYTE
345     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
346 #       if defined( WORDS_BIGENDIAN )
347     p_vout->output.i_rmask = 0x00ff0000;
348     p_vout->output.i_gmask = 0x0000ff00;
349     p_vout->output.i_bmask = 0x000000ff;
350 #       else
351     p_vout->output.i_rmask = 0x000000ff;
352     p_vout->output.i_gmask = 0x0000ff00;
353     p_vout->output.i_bmask = 0x00ff0000;
354 #       endif
355     i_pixel_pitch = 3;
356 #   else
357     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
358 #       if defined( WORDS_BIGENDIAN )
359     p_vout->output.i_rmask = 0x001f;
360     p_vout->output.i_gmask = 0x07e0;
361     p_vout->output.i_bmask = 0xf800;
362 #       else
363     p_vout->output.i_rmask = 0xf800;
364     p_vout->output.i_gmask = 0x07e0;
365     p_vout->output.i_bmask = 0x001f;
366 #       endif
367     i_pixel_pitch = 2;
368 #   endif
369 #else
370     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
371 #       if defined( WORDS_BIGENDIAN )
372     p_vout->output.i_rmask = 0xff000000;
373     p_vout->output.i_gmask = 0x00ff0000;
374     p_vout->output.i_bmask = 0x0000ff00;
375 #       else
376     p_vout->output.i_rmask = 0x000000ff;
377     p_vout->output.i_gmask = 0x0000ff00;
378     p_vout->output.i_bmask = 0x00ff0000;
379 #       endif
380     i_pixel_pitch = 4;
381 #endif
382
383     /* Since OpenGL can do rescaling for us, stick to the default
384      * coordinates and aspect. */
385     p_vout->output.i_width  = p_vout->render.i_width;
386     p_vout->output.i_height = p_vout->render.i_height;
387     p_vout->output.i_aspect = p_vout->render.i_aspect;
388
389     p_vout->fmt_out = p_vout->fmt_in;
390     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
391
392     /* We know the chroma, allocate one buffer which will be used
393      * directly by the decoder */
394     p_sys->pp_buffer[0] =
395         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
396     if( !p_sys->pp_buffer[0] )
397     {
398         msg_Err( p_vout, "out of memory" );
399         return -1;
400     }
401     p_sys->pp_buffer[1] =
402         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
403     if( !p_sys->pp_buffer[1] )
404     {
405         msg_Err( p_vout, "out of memory" );
406         return -1;
407     }
408
409     p_vout->p_picture[0].i_planes = 1;
410     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
411     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
412     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
413     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
414     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
415         p_vout->p_picture[0].p->i_pixel_pitch;
416     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
417         p_vout->p_picture[0].p->i_pixel_pitch;
418
419     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
420     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
421
422     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
423
424     I_OUTPUTPICTURES = 1;
425
426     if( p_sys->p_vout->pf_lock &&
427         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
428     {
429         msg_Warn( p_vout, "could not lock OpenGL provider" );
430         return 0;
431     }
432
433     InitTextures( p_vout );
434
435     glDisable(GL_BLEND);
436     glDisable(GL_DEPTH_TEST);
437     glDepthMask(GL_FALSE);
438     glDisable(GL_CULL_FACE);
439     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
440     glClear( GL_COLOR_BUFFER_BIT );
441
442     /* Check if the user asked for useless visual effects */
443     var_Get( p_vout, "opengl-effect", &val );
444     if( !val.psz_string || !strcmp( val.psz_string, "none" ))
445     {
446         p_sys->i_effect = OPENGL_EFFECT_NONE;
447     }
448     else if( !strcmp( val.psz_string, "cube" ) )
449     {
450         p_sys->i_effect = OPENGL_EFFECT_CUBE;
451
452         glEnable( GL_CULL_FACE);
453     }
454     else if( !strcmp( val.psz_string, "transparent-cube" ) )
455     {
456         p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
457
458         glDisable( GL_DEPTH_TEST );
459         glEnable( GL_BLEND );
460         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
461     }
462     else
463     {
464 #ifdef OPENGL_MORE_EFFECT
465         p_sys->i_effect = 3;
466         while (( strcmp( val.psz_string, ppsz_effects[p_sys->i_effect]) ) && (pow(2,p_sys->i_effect) < INIFILE))
467         {
468             p_sys->i_effect ++;
469         }
470         if (pow(2,p_sys->i_effect) < INIFILE) 
471             p_sys->i_effect = pow(2,p_sys->i_effect);
472         else if ( strcmp( val.psz_string, ppsz_effects[p_sys->i_effect]))
473         {
474             msg_Warn( p_vout, "no valid opengl effect provided, using "
475                       "\"none\"" );
476             p_sys->i_effect = OPENGL_EFFECT_NONE;
477         }
478 #else
479         msg_Warn( p_vout, "no valid opengl effect provided, using "
480                   "\"none\"" );
481         p_sys->i_effect = OPENGL_EFFECT_NONE;
482 #endif
483     }
484     if( val.psz_string ) free( val.psz_string );
485
486     if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
487                 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
488     {
489         /* Set the perpective */
490         glMatrixMode( GL_PROJECTION );
491         glLoadIdentity();
492         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
493         glMatrixMode( GL_MODELVIEW );
494         glLoadIdentity();
495         glTranslatef( 0.0, 0.0, - 5.0 );
496     }
497 #ifdef OPENGL_MORE_EFFECT
498     else 
499     {
500         /* Set the perpective */
501         glMatrixMode( GL_PROJECTION );
502         glLoadIdentity();
503         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
504         glMatrixMode( GL_MODELVIEW );
505         glLoadIdentity();
506         glTranslatef( 0.0, 0.0, -3.0 );
507
508         float f_pov_x, f_pov_y, f_pov_z;
509         f_pov_x = var_CreateGetFloat( p_vout, "opengl-pov-x");
510         f_pov_y = var_CreateGetFloat( p_vout, "opengl-pov-y");
511         f_pov_z = var_CreateGetFloat( p_vout, "opengl-pov-z");
512         gluLookAt(0, 0, 0, f_pov_x, f_pov_y, f_pov_z, 0, 1, 0);
513     }
514 #endif
515     if( p_sys->p_vout->pf_unlock )
516     {
517         p_sys->p_vout->pf_unlock( p_sys->p_vout );
518     }
519
520     return 0;
521 }
522
523 /*****************************************************************************
524  * End: terminate GLX video thread output method
525  *****************************************************************************/
526 static void End( vout_thread_t *p_vout )
527 {
528     vout_sys_t *p_sys = p_vout->p_sys;
529
530     if( p_sys->p_vout->pf_lock &&
531         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
532     {
533         msg_Warn( p_vout, "could not lock OpenGL provider" );
534         return;
535     }
536
537     glFinish();
538     glFlush();
539
540     /* Free the texture buffer*/
541     glDeleteTextures( 2, p_sys->p_textures );
542     if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
543     if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
544
545     if( p_sys->p_vout->pf_unlock )
546     {
547         p_sys->p_vout->pf_unlock( p_sys->p_vout );
548     }
549 }
550
551 /*****************************************************************************
552  * Destroy: destroy GLX video thread output method
553  *****************************************************************************
554  * Terminate an output method created by CreateVout
555  *****************************************************************************/
556 static void DestroyVout( vlc_object_t *p_this )
557 {
558     vout_thread_t *p_vout = (vout_thread_t *)p_this;
559     vout_sys_t *p_sys = p_vout->p_sys;
560
561     module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
562     vlc_object_detach( p_sys->p_vout );
563     vlc_object_destroy( p_sys->p_vout );
564
565     free( p_sys );
566 }
567
568 /*****************************************************************************
569  * Manage: handle Sys events
570  *****************************************************************************
571  * This function should be called regularly by video output thread. It returns
572  * a non null value if an error occurred.
573  *****************************************************************************/
574 static int Manage( vout_thread_t *p_vout )
575 {
576     vout_sys_t *p_sys = p_vout->p_sys;
577     int i_ret, i_fullscreen_change;
578
579     i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
580
581     p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
582         p_vout->fmt_in.i_x_offset;
583     p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
584         p_vout->fmt_in.i_y_offset;
585     p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
586         p_vout->fmt_in.i_visible_width;
587     p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
588         p_vout->fmt_in.i_visible_height;
589     p_vout->fmt_out.i_aspect = p_sys->p_vout->fmt_in.i_aspect =
590         p_vout->fmt_in.i_aspect;
591     p_vout->fmt_out.i_sar_num = p_sys->p_vout->fmt_in.i_sar_num =
592         p_vout->fmt_in.i_sar_num;
593     p_vout->fmt_out.i_sar_den = p_sys->p_vout->fmt_in.i_sar_den =
594         p_vout->fmt_in.i_sar_den;
595     p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
596
597     p_sys->p_vout->i_changes = p_vout->i_changes;
598     i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
599     p_vout->i_changes = p_sys->p_vout->i_changes;
600
601 #ifdef __APPLE__
602     if( p_sys->p_vout->pf_lock &&
603         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
604     {
605         msg_Warn( p_vout, "could not lock OpenGL provider" );
606         return i_ret;
607     }
608
609     /* On OS X, we create the window and the GL view when entering
610        fullscreen - the textures have to be inited again */
611     if( i_fullscreen_change )
612     {
613         InitTextures( p_vout );
614
615         switch( p_sys->i_effect )
616         {
617             case OPENGL_EFFECT_CUBE:
618 #ifdef OPENGL_MORE_EFFECT
619             case CYLINDER:
620             case TORUS:
621             case SPHERE:
622             case SQUAREXY:
623             case SQUARER:
624             case ASINXY:
625             case ASINR:
626             case SINEXY:
627             case SINER:
628 #endif            
629                 glEnable( GL_CULL_FACE );
630                 break;
631
632             case OPENGL_EFFECT_TRANSPARENT_CUBE:
633                 glDisable( GL_DEPTH_TEST );
634                 glEnable( GL_BLEND );
635                 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
636                 break;
637         }
638
639         if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
640                     OPENGL_EFFECT_TRANSPARENT_CUBE ) )
641         {
642             /* Set the perpective */
643             glMatrixMode( GL_PROJECTION );
644             glLoadIdentity();
645             glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
646             glMatrixMode( GL_MODELVIEW );
647             glLoadIdentity();
648             glTranslatef( 0.0, 0.0, - 5.0 );
649         }
650 #ifdef OPENGL_MORE_EFFECT
651         else 
652         {
653             glMatrixMode( GL_PROJECTION );
654             glLoadIdentity();
655             glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
656             glMatrixMode( GL_MODELVIEW );
657             glLoadIdentity();
658             glTranslatef( 0.0, 0.0, -3.0 );
659     
660             float f_pov_x, f_pov_y, f_pov_z;
661             f_pov_x = var_CreateGetFloat( p_vout, "opengl-pov-x");
662             f_pov_y = var_CreateGetFloat( p_vout, "opengl-pov-y");
663             f_pov_z = var_CreateGetFloat( p_vout, "opengl-pov-z");
664             gluLookAt(0, 0, 0, f_pov_x, f_pov_y, f_pov_z, 0, 1, 0);
665         }
666 #endif        
667     }
668
669     if( p_sys->p_vout->pf_unlock )
670     {
671         p_sys->p_vout->pf_unlock( p_sys->p_vout );
672     }
673 #endif
674 // to align in real time in OPENGL
675         if (p_sys->p_vout->i_alignment != p_vout->i_alignment)
676         {
677                 p_vout->i_changes = VOUT_CROP_CHANGE;           //to force change
678         p_sys->p_vout->i_alignment = p_vout->i_alignment;       
679         }
680         
681     return i_ret;
682 }
683
684 /*****************************************************************************
685  * Render: render previously calculated output
686  *****************************************************************************/
687 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
688 {
689     vout_sys_t *p_sys = p_vout->p_sys;
690
691     /* On Win32/GLX, we do this the usual way:
692        + Fill the buffer with new content,
693        + Reload the texture,
694        + Use the texture.
695
696        On OS X with VRAM or AGP texturing, the order has to be:
697        + Reload the texture,
698        + Fill the buffer with new content,
699        + Use the texture.
700
701        (Thanks to gcc from the Arstechnica forums for the tip)
702
703        Therefore, we have to use two buffers and textures. On Win32/GLX,
704        we reload the texture to be displayed and use it right away. On
705        OS X, we first render, then reload the texture to be used next
706        time. */
707
708     if( p_sys->p_vout->pf_lock &&
709         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
710     {
711         msg_Warn( p_vout, "could not lock OpenGL provider" );
712         return;
713     }
714
715 #ifdef __APPLE__
716     int i_new_index;
717     i_new_index = ( p_sys->i_index + 1 ) & 1;
718
719
720     /* Update the texture */
721     glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
722     glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
723                      p_vout->fmt_out.i_width,
724                      p_vout->fmt_out.i_height,
725                      VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
726
727     /* Bind to the previous texture for drawing */
728     glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
729
730     /* Switch buffers */
731     p_sys->i_index = i_new_index;
732     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
733
734 #else
735     /* Update the texture */
736     glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
737                      p_vout->fmt_out.i_width,
738                      p_vout->fmt_out.i_height,
739                      VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[0] );
740 #endif
741
742     if( p_sys->p_vout->pf_unlock )
743     {
744         p_sys->p_vout->pf_unlock( p_sys->p_vout );
745     }
746 }
747
748
749 #ifdef OPENGL_MORE_EFFECT
750 /*****************************************************************************
751  *   Transform: Calculate the distorted grid coordinates
752  *****************************************************************************/
753 static void Transform(float p, int distortion, float width, float height,int i, int j, int i_visible_width, int i_visible_height, double *ix,double *iy)
754 {
755     double x,y,xnew,ynew;
756     double r,theta,rnew,thetanew;
757
758     x = (double)i * (width / ((double)i_visible_width));
759     y = (double)j * (height / ((double)i_visible_height));
760
761     x = (2.0 * (double)x / width) - 1;
762     y = (2.0 * (double)y / height) - 1;
763     xnew = x;
764     ynew = y;
765     r = sqrt(x*x+y*y);
766     theta = atan2(y,x);
767
768     switch (distortion) 
769     {
770 /* GRID2D TRANSFORMATION */
771         case SINEXY:
772             xnew = sin(PID2*x);
773             ynew = sin(PID2*y);
774             break;
775         case SINER:
776             rnew = sin(PID2*r);
777             thetanew = theta;
778             xnew = rnew * cos(thetanew);
779             ynew = rnew * sin(thetanew);
780             break;
781         case SQUAREXY:
782             xnew = x*x*SIGN(x);
783             ynew = y*y*SIGN(y);
784             break;
785         case SQUARER:
786             rnew = r*r;
787             thetanew = theta;
788             xnew = rnew * cos(thetanew);
789             ynew = rnew * sin(thetanew);
790             break;
791         case ASINXY:
792             xnew = asin(x) / PID2;
793             ynew = asin(y) / PID2;
794             break;
795         case ASINR:
796             rnew = asin(r) / PID2;
797             thetanew = theta;
798             xnew = rnew * cos(thetanew);
799             ynew = rnew * sin(thetanew);
800             break;
801 /* OTHER WAY: 3D MODEL */
802         default:
803             xnew = x;
804             ynew = y;
805     }
806
807     *ix = width * (xnew + 1) / (2.0);
808     *iy = height * (ynew + 1) / (2.0);
809 }
810
811 /*****************************************************************************
812  *   Z_Compute: Calculate the Z-coordinate
813  *****************************************************************************/
814 static float Z_Compute(float p, int distortion, float x, float y)
815 {
816     float f_z = 0.0;
817     double d_p = p / 100.0;
818
819     switch (distortion) 
820     {
821 /* 3D MODEL */
822         case CYLINDER:
823             if (d_p > 0) 
824                 f_z = (1 - d_p * d_p) / (2 * d_p) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x));
825             else
826                 f_z = (1 - d_p * d_p) / (2 * d_p) + d_p + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x));
827             break;
828         case TORUS:
829             if (d_p > 0) 
830                 f_z =  (1 - d_p * d_p) / (d_p) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x)) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - y * y));
831             else
832                 f_z =  (1 - d_p * d_p) / (d_p) + 2 * d_p + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x)) + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - y * y));
833             break;
834         case SPHERE:
835             if (d_p > 0) 
836                 f_z = (1 - d_p * d_p) / (2 * d_p) - sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x - y * y));
837             else
838                 f_z = (1 - d_p * d_p) / (2 * d_p) + d_p + sqrt(fabs((d_p * d_p + 1) / (2 * d_p) * (d_p * d_p + 1) / (2 * d_p) - x * x - y * y));
839             break;
840 /* OTHER WAY: GRID2D TRANSFORMATION */
841         case SINEXY:;
842         case SINER:
843         case SQUAREXY:
844         case SQUARER:;
845         case ASINXY:
846         case ASINR:
847             f_z = 0.0;
848             break;
849         default:
850             f_z = 0.0;
851     }
852     return f_z;
853 }
854 #endif
855
856
857 /*****************************************************************************
858  * DisplayVideo: displays previously rendered output
859  *****************************************************************************/
860 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
861 {
862     vout_sys_t *p_sys = p_vout->p_sys;
863     float f_width, f_height, f_x, f_y;
864
865     if( p_sys->p_vout->pf_lock &&
866         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
867     {
868         msg_Warn( p_vout, "could not lock OpenGL provider" );
869         return;
870     }
871
872     /* glTexCoord works differently with GL_TEXTURE_2D and
873        GL_TEXTURE_RECTANGLE_EXT */
874 #ifdef __APPLE__
875     f_x = (float)p_vout->fmt_out.i_x_offset;
876     f_y = (float)p_vout->fmt_out.i_y_offset;
877     f_width = (float)p_vout->fmt_out.i_x_offset +
878               (float)p_vout->fmt_out.i_visible_width;
879     f_height = (float)p_vout->fmt_out.i_y_offset +
880                (float)p_vout->fmt_out.i_visible_height;
881 #else
882     f_x = (float)p_vout->fmt_out.i_x_offset / p_sys->i_tex_width;
883     f_y = (float)p_vout->fmt_out.i_y_offset / p_sys->i_tex_height;
884     f_width = ( (float)p_vout->fmt_out.i_x_offset +
885                 p_vout->fmt_out.i_visible_width ) / p_sys->i_tex_width;
886     f_height = ( (float)p_vout->fmt_out.i_y_offset +
887                  p_vout->fmt_out.i_visible_height ) / p_sys->i_tex_height;
888 #endif
889
890     /* Why drawing here and not in Render()? Because this way, the
891        OpenGL providers can call pf_display to force redraw. Currently,
892        the OS X provider uses it to get a smooth window resizing */
893
894     glClear( GL_COLOR_BUFFER_BIT );
895
896     if( p_sys->i_effect == OPENGL_EFFECT_NONE )
897     {
898         glEnable( VLCGL_TARGET );
899         glBegin( GL_POLYGON );
900         glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
901         glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
902         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
903         glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
904         glEnd();
905     }
906     else
907 #ifdef OPENGL_MORE_EFFECT
908     if ((p_sys->i_effect > OPENGL_EFFECT_TRANSPARENT_CUBE) || 
909         ((p_sys->i_effect == OPENGL_EFFECT_NONE)))
910     {
911        unsigned int i_i, i_j;
912        unsigned int i_accuracy  = config_GetInt( p_vout, "opengl-accuracy");
913        unsigned int i_n = pow(2, i_accuracy);    
914        unsigned int i_n_x = (p_vout->fmt_out.i_visible_width / (i_n * 2));
915        unsigned int i_n_y = (p_vout->fmt_out.i_visible_height / i_n);
916        double d_x, d_y;
917        int i_distortion = p_sys->i_effect;
918        float f_p = p_sys->f_radius; 
919             
920        glEnable( VLCGL_TARGET );
921        glBegin(GL_QUADS);                                   
922        for (i_i = 0; i_i < p_vout->fmt_out.i_visible_width; i_i += i_n_x) 
923        {
924           if ( i_i == i_n_x * i_n / 2) i_n_x += p_vout->fmt_out.i_visible_width % i_n;
925           if ((i_i == (p_vout->fmt_out.i_visible_width / i_n) * i_n / 2 + i_n_x) &&
926               (p_vout->fmt_out.i_visible_width / i_n != i_n_x))
927                 i_n_x -= p_vout->fmt_out.i_visible_width % i_n; 
928
929           int i_m;
930           int i_index_max = 0;
931                 
932           for (i_j = 0; i_j < p_vout->fmt_out.i_visible_height; i_j += i_n_y) 
933           {
934             if ( i_j == i_n_y * i_n / 2) i_n_y += p_vout->fmt_out.i_visible_height % i_n;
935             if ((i_j == (p_vout->fmt_out.i_visible_height / i_n) * i_n / 2 + i_n_y) &&
936                 (p_vout->fmt_out.i_visible_height / i_n != i_n_y))
937                     i_n_y -= p_vout->fmt_out.i_visible_height % i_n;
938
939             for (i_m = i_index_max; i_m < i_index_max + 4; i_m++)
940             {
941                 int i_k = ((i_m % 4) == 1) || ((i_m % 4) == 2);
942                 int i_l = ((i_m % 4) == 2) || ((i_m % 4) == 3);
943
944                 Transform(f_p, i_distortion, f_width, f_height, i_i + i_k * i_n_x, i_j + i_l * i_n_y, p_vout->fmt_out.i_visible_width, p_vout->fmt_out.i_visible_height, &d_x, &d_y);                                
945                 glTexCoord2f(f_x + d_x, f_y + d_y);
946                 d_x =  - 1.0 + 2.0 * ((double)(i_k * i_n_x + i_i) / (double)p_vout->fmt_out.i_visible_width);
947                 d_y =    1.0 - 2.0 * (((double)i_l * i_n_y + i_j) / (double)p_vout->fmt_out.i_visible_height);
948                 glVertex3f((float)d_x, (float)d_y, Z_Compute(f_p, i_distortion, (float)d_x, (float)d_y));    
949             }
950           }
951        } 
952        glEnd();
953     }
954     else
955 #endif
956     {
957         glRotatef( 0.5 * p_sys->f_speed , 0.3, 0.5, 0.7 );
958
959         glEnable( VLCGL_TARGET );
960         glBegin( GL_QUADS );
961
962         /* Front */
963         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
964         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
965         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
966         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
967
968         /* Left */
969         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
970         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
971         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
972         glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, 1.0 );
973
974         /* Back */
975         glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
976         glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
977         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
978         glTexCoord2f( f_width, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
979
980         /* Right */
981         glTexCoord2f( f_x, f_y ); glVertex3f( 1.0, 1.0, 1.0 );
982         glTexCoord2f( f_x, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
983         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
984         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
985
986         /* Top */
987         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, 1.0, - 1.0 );
988         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
989         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
990         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, 1.0, - 1.0 );
991
992         /* Bottom */
993         glTexCoord2f( f_x, f_y ); glVertex3f( - 1.0, - 1.0, 1.0 );
994         glTexCoord2f( f_x, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
995         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
996         glTexCoord2f( f_width, f_y ); glVertex3f( 1.0, - 1.0, 1.0 );
997         glEnd();
998     }
999
1000     glDisable( VLCGL_TARGET );
1001
1002     p_sys->p_vout->pf_swap( p_sys->p_vout );
1003
1004     if( p_sys->p_vout->pf_unlock )
1005     {
1006         p_sys->p_vout->pf_unlock( p_sys->p_vout );
1007     }
1008 }
1009
1010 int GetAlignedSize( int i_size )
1011 {
1012     /* Return the nearest power of 2 */
1013     int i_result = 1;
1014     while( i_result < i_size )
1015     {
1016         i_result *= 2;
1017     }
1018     return i_result;
1019 }
1020
1021 /*****************************************************************************
1022  * Control: control facility for the vout
1023  *****************************************************************************/
1024 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1025 {
1026     vout_sys_t *p_sys = p_vout->p_sys;
1027
1028     switch( i_query )
1029     {
1030     case VOUT_SNAPSHOT:
1031         return vout_vaControlDefault( p_vout, i_query, args );
1032
1033     default:
1034         if( p_sys->p_vout->pf_control )
1035             return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
1036         else
1037             return vout_vaControlDefault( p_vout, i_query, args );
1038     }
1039 }
1040
1041 static int InitTextures( vout_thread_t *p_vout )
1042 {
1043     vout_sys_t *p_sys = p_vout->p_sys;
1044     int i_index;
1045
1046     glDeleteTextures( 2, p_sys->p_textures );
1047     glGenTextures( 2, p_sys->p_textures );
1048
1049     for( i_index = 0; i_index < 2; i_index++ )
1050     {
1051         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
1052
1053         /* Set the texture parameters */
1054         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
1055
1056         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
1057         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
1058
1059         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
1060         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
1061
1062         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1063
1064 #ifdef __APPLE__
1065         /* Tell the driver not to make a copy of the texture but to use
1066            our buffer */
1067         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
1068         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
1069
1070 #if 0
1071         /* Use VRAM texturing */
1072         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
1073                          GL_STORAGE_CACHED_APPLE );
1074 #else
1075         /* Use AGP texturing */
1076         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
1077                          GL_STORAGE_SHARED_APPLE );
1078 #endif
1079 #endif
1080
1081         /* Call glTexImage2D only once, and use glTexSubImage2D later */
1082         glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
1083                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
1084                       p_sys->pp_buffer[i_index] );
1085     }
1086
1087     return 0;
1088 }
1089
1090 /*****************************************************************************
1091  * SendEvents: forward mouse and keyboard events to the parent p_vout
1092  *****************************************************************************/
1093 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1094                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1095 {
1096     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
1097 }