]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
Todo update
[vlc] / modules / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: OpenGL video output
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/vout.h>
34
35 #ifdef SYS_DARWIN
36 #include <OpenGL/gl.h>
37 #include <OpenGL/glext.h>
38
39 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
40    This allows sizes which are not powers of 2 */
41 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
42
43 /* OS X OpenGL supports YUV. Hehe. */
44 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
45 #define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
46 #else
47
48 #include <GL/gl.h>
49 #define VLCGL_TARGET GL_TEXTURE_2D
50
51 /* RV16 */
52 #ifndef GL_UNSIGNED_SHORT_5_6_5
53 #define GL_UNSIGNED_SHORT_5_6_5 0x8363
54 #endif
55 //#define VLCGL_RGB_FORMAT GL_RGB
56 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
57
58 /* RV24 */
59 //#define VLCGL_RGB_FORMAT GL_RGB
60 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
61
62 /* RV32 */
63 #define VLCGL_RGB_FORMAT GL_RGBA
64 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
65
66 /* Use RGB on Win32/GLX */
67 #define VLCGL_FORMAT VLCGL_RGB_FORMAT
68 #define VLCGL_TYPE   VLCGL_RGB_TYPE
69 #endif
70
71 /* OpenGL effects */
72 #define OPENGL_EFFECT_NONE             1
73 #define OPENGL_EFFECT_CUBE             2
74 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
75
76 /*****************************************************************************
77  * Vout interface
78  *****************************************************************************/
79 static int  CreateVout   ( vlc_object_t * );
80 static void DestroyVout  ( vlc_object_t * );
81 static int  Init         ( vout_thread_t * );
82 static void End          ( vout_thread_t * );
83 static int  Manage       ( vout_thread_t * );
84 static void Render       ( vout_thread_t *, picture_t * );
85 static void DisplayVideo ( vout_thread_t *, picture_t * );
86 static int  Control      ( vout_thread_t *, int, va_list );
87
88 static inline int GetAlignedSize( int );
89
90 static int InitTextures( vout_thread_t * );
91 static int SendEvents( vlc_object_t *, char const *,
92                        vlc_value_t, vlc_value_t, void * );
93
94 /*****************************************************************************
95  * Module descriptor
96  *****************************************************************************/
97 #define SPEED_TEXT N_( "OpenGL cube rotation speed" )
98 #define SPEED_LONGTEXT N_( "If the OpenGL cube effect is enabled, this " \
99                            "controls its rotation speed." )
100
101 #define EFFECT_TEXT N_("Select effect")
102 #define EFFECT_LONGTEXT N_( \
103     "Allows you to select different visual effects.")
104
105 static char *ppsz_effects[] = {
106         "none", "cube", "transparent-cube" };
107 static char *ppsz_effects_text[] = {
108         N_("None"), N_("Cube"), N_("Transparent Cube") };
109
110 vlc_module_begin();
111     set_description( _("OpenGL video output") );
112 #ifdef SYS_DARWIN
113     set_capability( "video output", 0 );
114 #else
115     set_capability( "video output", 20 );
116 #endif
117     add_shortcut( "opengl" );
118     add_float( "opengl-cube-speed", 2.0, NULL, SPEED_TEXT,
119                     SPEED_LONGTEXT, VLC_FALSE );
120     set_callbacks( CreateVout, DestroyVout );
121     add_string( "opengl-effect", "none", NULL, EFFECT_TEXT,
122                  EFFECT_LONGTEXT, VLC_TRUE );
123         change_string_list( ppsz_effects, ppsz_effects_text, 0 );
124 vlc_module_end();
125
126 /*****************************************************************************
127  * vout_sys_t: video output method descriptor
128  *****************************************************************************
129  * This structure is part of the video output thread descriptor.
130  * It describes the OpenGL specific properties of the output thread.
131  *****************************************************************************/
132 struct vout_sys_t
133 {
134     vout_thread_t *p_vout;
135
136     uint8_t    *pp_buffer[2];
137     int         i_index;
138     int         i_tex_width;
139     int         i_tex_height;
140     GLuint      p_textures[2];
141
142     int         i_effect;
143
144     float       f_speed;
145 };
146
147 /*****************************************************************************
148  * CreateVout: This function allocates and initializes the OpenGL vout method.
149  *****************************************************************************/
150 static int CreateVout( vlc_object_t *p_this )
151 {
152     vout_thread_t *p_vout = (vout_thread_t *)p_this;
153     vout_sys_t *p_sys;
154
155     /* Allocate structure */
156     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
157     if( p_sys == NULL )
158     {
159         msg_Err( p_vout, "out of memory" );
160         return VLC_EGENERIC;
161     }
162
163     var_Create( p_vout, "opengl-effect", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
164
165 #ifdef SYS_DARWIN
166     p_sys->i_tex_width  = p_vout->render.i_width;
167     p_sys->i_tex_height = p_vout->render.i_height;
168 #else
169     /* A texture must have a size aligned on a power of 2 */
170     p_sys->i_tex_width  = GetAlignedSize( p_vout->render.i_width );
171     p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
172 #endif
173
174     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
175              p_sys->i_tex_height );
176
177     /* Get window */
178     p_sys->p_vout =
179         (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
180     if( p_sys->p_vout == NULL )
181     {
182         msg_Err( p_vout, "out of memory" );
183         return VLC_ENOMEM;
184     }
185     vlc_object_attach( p_sys->p_vout, p_this );
186
187     p_sys->p_vout->i_window_width = p_vout->i_window_width;
188     p_sys->p_vout->i_window_height = p_vout->i_window_height;
189     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
190     p_sys->p_vout->render.i_width = p_vout->render.i_width;
191     p_sys->p_vout->render.i_height = p_vout->render.i_height;
192     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
193     p_sys->p_vout->b_scale = p_vout->b_scale;
194     p_sys->p_vout->i_alignment = p_vout->i_alignment;
195
196     p_sys->p_vout->p_module =
197         module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
198     if( p_sys->p_vout->p_module == NULL )
199     {
200         msg_Warn( p_vout, "No OpenGL provider found" );
201         vlc_object_detach( p_sys->p_vout );
202         vlc_object_destroy( p_sys->p_vout );
203         return VLC_ENOOBJ;
204     }
205
206     p_sys->f_speed = var_CreateGetFloat( p_vout, "opengl-cube-speed" );
207
208     p_vout->pf_init = Init;
209     p_vout->pf_end = End;
210     p_vout->pf_manage = Manage;
211     p_vout->pf_render = Render;
212     p_vout->pf_display = DisplayVideo;
213     p_vout->pf_control = Control;
214
215     /* Forward events from the opengl provider */
216     var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
217     var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
218     var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
219     var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_INTEGER );
220     var_Create( p_sys->p_vout, "video-on-top",
221                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222
223     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
224     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
225     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
226     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * Init: initialize the OpenGL video thread output method
233  *****************************************************************************/
234 static int Init( vout_thread_t *p_vout )
235 {
236     vout_sys_t *p_sys = p_vout->p_sys;
237     int i_pixel_pitch;
238     vlc_value_t val;
239
240     p_sys->p_vout->pf_init( p_sys->p_vout );
241
242 #ifdef SYS_DARWIN
243     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
244     p_vout->output.i_rmask = 0x00ff0000;
245     p_vout->output.i_gmask = 0x0000ff00;
246     p_vout->output.i_bmask = 0x000000ff;
247     i_pixel_pitch = 2;
248 #else
249 #if VLCGL_RGB_FORMAT == GL_RGB
250 #   if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
251     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
252     p_vout->output.i_rmask = 0x000000ff;
253     p_vout->output.i_gmask = 0x0000ff00;
254     p_vout->output.i_bmask = 0x00ff0000;
255     i_pixel_pitch = 3;
256 #   else
257     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
258     p_vout->output.i_rmask = 0xf800;
259     p_vout->output.i_gmask = 0x07e0;
260     p_vout->output.i_bmask = 0x001f;
261     i_pixel_pitch = 2;
262 #   endif
263 #else
264     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
265     p_vout->output.i_rmask = 0x000000ff;
266     p_vout->output.i_gmask = 0x0000ff00;
267     p_vout->output.i_bmask = 0x00ff0000;
268     i_pixel_pitch = 4;
269 #endif
270 #endif
271
272     /* Since OpenGL can do rescaling for us, stick to the default
273      * coordinates and aspect. */
274     p_vout->output.i_width  = p_vout->render.i_width;
275     p_vout->output.i_height = p_vout->render.i_height;
276     p_vout->output.i_aspect = p_vout->render.i_aspect;
277
278     /* We know the chroma, allocate one buffer which will be used
279      * directly by the decoder */
280     p_sys->pp_buffer[0] =
281         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
282     if( !p_sys->pp_buffer[0] )
283     {
284         msg_Err( p_vout, "Out of memory" );
285         return -1;
286     }
287     p_sys->pp_buffer[1] =
288         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
289     if( !p_sys->pp_buffer[1] )
290     {
291         msg_Err( p_vout, "Out of memory" );
292         return -1;
293     }
294
295     p_vout->p_picture[0].i_planes = 1;
296     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
297     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
298     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
299     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
300     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
301         p_vout->p_picture[0].p->i_pixel_pitch;
302     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
303         p_vout->p_picture[0].p->i_pixel_pitch;
304
305     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
306     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
307
308     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
309
310     I_OUTPUTPICTURES = 1;
311
312     InitTextures( p_vout );
313
314     glDisable(GL_BLEND);
315     glDisable(GL_DEPTH_TEST);
316     glDepthMask(GL_FALSE);
317     glDisable(GL_CULL_FACE);
318     glClear( GL_COLOR_BUFFER_BIT );
319
320     /* Check if the user asked for useless visual effects */
321     var_Get( p_vout, "opengl-effect", &val );
322     if( !val.psz_string || !strcmp( val.psz_string, "none" ))
323     {
324         p_sys->i_effect = OPENGL_EFFECT_NONE;
325     }
326     else if( !strcmp( val.psz_string, "cube" ) )
327     {
328         p_sys->i_effect = OPENGL_EFFECT_CUBE;
329
330         glEnable( GL_CULL_FACE);
331         //glEnable( GL_DEPTH_TEST );
332     }
333     else if( !strcmp( val.psz_string, "transparent-cube" ) )
334     {
335         p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
336
337         glDisable( GL_DEPTH_TEST );
338         glEnable( GL_BLEND );
339         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
340     }
341     else
342     {
343         msg_Warn( p_vout, "no valid opengl effect provided, using "
344                   "\"none\"" );
345         p_sys->i_effect = OPENGL_EFFECT_NONE;
346     }
347     if( val.psz_string ) free( val.psz_string );
348
349     if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
350                 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
351     {
352         /* Set the perpective */
353         glMatrixMode( GL_PROJECTION );
354         glLoadIdentity();
355         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
356         glMatrixMode( GL_MODELVIEW );
357         glLoadIdentity();
358         glTranslatef( 0.0, 0.0, - 5.0 );
359     }
360
361     return 0;
362 }
363
364 /*****************************************************************************
365  * End: terminate GLX video thread output method
366  *****************************************************************************/
367 static void End( vout_thread_t *p_vout )
368 {
369     glFinish();
370     glFlush();
371 }
372
373 /*****************************************************************************
374  * Destroy: destroy GLX video thread output method
375  *****************************************************************************
376  * Terminate an output method created by CreateVout
377  *****************************************************************************/
378 static void DestroyVout( vlc_object_t *p_this )
379 {
380     vout_thread_t *p_vout = (vout_thread_t *)p_this;
381     vout_sys_t *p_sys = p_vout->p_sys;
382
383     module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
384     vlc_object_detach( p_sys->p_vout );
385     vlc_object_destroy( p_sys->p_vout );
386
387     /* Free the texture buffer*/
388     if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
389     if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
390
391     free( p_sys );
392 }
393
394 /*****************************************************************************
395  * Manage: handle Sys events
396  *****************************************************************************
397  * This function should be called regularly by video output thread. It returns
398  * a non null value if an error occurred.
399  *****************************************************************************/
400 static int Manage( vout_thread_t *p_vout )
401 {
402     vout_sys_t *p_sys = p_vout->p_sys;
403     int i_ret, i_fullscreen_change;
404
405     i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
406
407     p_sys->p_vout->i_changes = p_vout->i_changes;
408     i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
409     p_vout->i_changes = p_sys->p_vout->i_changes;
410
411 #ifdef SYS_DARWIN
412     /* On OS X, we create the window and the GL view when entering
413        fullscreen - the textures have to be inited again */
414     if( i_fullscreen_change )
415     {
416         InitTextures( p_vout );
417
418         switch( p_sys->i_effect )
419         {
420             case OPENGL_EFFECT_CUBE:
421                 glEnable( GL_CULL_FACE );
422                 break;
423
424             case OPENGL_EFFECT_TRANSPARENT_CUBE:
425                 glDisable( GL_DEPTH_TEST );
426                 glEnable( GL_BLEND );
427                 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
428                 break;
429         }
430
431         if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
432                     OPENGL_EFFECT_TRANSPARENT_CUBE ) )
433         {
434             /* Set the perpective */
435             glMatrixMode( GL_PROJECTION );
436             glLoadIdentity();
437             glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
438             glMatrixMode( GL_MODELVIEW );
439             glLoadIdentity();
440             glTranslatef( 0.0, 0.0, - 5.0 );
441         }
442     }
443 #endif
444
445     return i_ret;
446 }
447
448 /*****************************************************************************
449  * Render: render previously calculated output
450  *****************************************************************************/
451 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
452 {
453     vout_sys_t *p_sys = p_vout->p_sys;
454     float f_width, f_height;
455
456     /* glTexCoord works differently with GL_TEXTURE_2D and
457        GL_TEXTURE_RECTANGLE_EXT */
458 #ifdef SYS_DARWIN
459     f_width = (float)p_vout->output.i_width;
460     f_height = (float)p_vout->output.i_height;
461 #else
462     f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
463     f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
464 #endif
465
466     glClear( GL_COLOR_BUFFER_BIT );
467
468     /* On Win32/GLX, we do this the usual way:
469        + Fill the buffer with new content,
470        + Reload the texture,
471        + Use the texture.
472
473        On OS X with VRAM or AGP texturing, the order has to be:
474        + Reload the texture,
475        + Fill the buffer with new content,
476        + Use the texture.
477
478        (Thanks to gcc from the Arstechnica forums for the tip)
479
480        Therefore, we have to use two buffers and textures. On Win32/GLX,
481        we reload the texture to be displayed and use it right away. On
482        OS X, we first render, then reload the texture to be used next
483        time. */
484
485 #ifdef SYS_DARWIN
486     glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
487 #else
488
489     /* Update the texture */
490     glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
491                      p_vout->render.i_width, p_vout->render.i_height,
492                      VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->pp_buffer[0] );
493 #endif
494
495     if( p_sys->i_effect == OPENGL_EFFECT_NONE )
496     {
497         glEnable( VLCGL_TARGET );
498         glBegin( GL_POLYGON );
499         glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
500         glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
501         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
502         glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
503         glEnd();
504     }
505     else
506     {
507         glRotatef( 0.5 * p_sys->f_speed , 0.3, 0.5, 0.7 );
508
509         glEnable( VLCGL_TARGET );
510         glBegin( GL_QUADS );
511
512         /* Front */
513         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
514         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
515         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
516         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
517
518         /* Left */
519         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
520         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
521         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
522         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
523
524         /* Back */
525         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
526         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
527         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
528         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
529
530         /* Right */
531         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
532         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
533         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
534         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
535
536         /* Top */
537         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
538         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
539         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
540         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
541
542         /* Bottom */
543         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
544         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
545         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
546         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
547         glEnd();
548     }
549
550     glDisable( VLCGL_TARGET );
551
552 #ifdef SYS_DARWIN
553     /* Switch buffers */
554     p_sys->i_index = ( p_sys->i_index + 1 ) & 1;
555     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
556
557     /* Update the texture */
558     glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
559     glTexSubImage2D( VLCGL_TARGET, 0, 0, 0, p_sys->i_tex_width,
560                      p_sys->i_tex_height, VLCGL_FORMAT, VLCGL_TYPE,
561                      p_sys->pp_buffer[p_sys->i_index] );
562 #endif
563 }
564
565 /*****************************************************************************
566  * DisplayVideo: displays previously rendered output
567  *****************************************************************************/
568 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
569 {
570     vout_sys_t *p_sys = p_vout->p_sys;
571     p_sys->p_vout->pf_swap( p_sys->p_vout );
572 }
573
574 int GetAlignedSize( int i_size )
575 {
576     /* Return the nearest power of 2 */
577     int i_result = 1;
578     while( i_result < i_size )
579     {
580         i_result *= 2;
581     }
582     return i_result;
583 }
584
585 /*****************************************************************************
586  * Control: control facility for the vout
587  *****************************************************************************/
588 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
589 {
590     vout_sys_t *p_sys = p_vout->p_sys;
591
592     if( p_sys->p_vout->pf_control )
593         return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
594     else
595         return vout_vaControlDefault( p_vout, i_query, args );
596 }
597
598 static int InitTextures( vout_thread_t *p_vout )
599 {
600     vout_sys_t *p_sys = p_vout->p_sys;
601     int i_index;
602
603     glDeleteTextures( 2, p_sys->p_textures );
604     glGenTextures( 2, p_sys->p_textures );
605
606     for( i_index = 0; i_index < 2; i_index++ )
607     {
608         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
609     
610         /* Set the texture parameters */
611         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
612     
613         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
614         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
615     
616         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP );
617         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP );
618     
619         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
620
621 #ifdef SYS_DARWIN
622         /* Tell the driver not to make a copy of the texture but to use
623            our buffer */
624         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
625         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
626     
627 #if 0
628         /* Use VRAM texturing */
629         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
630                          GL_STORAGE_CACHED_APPLE );
631 #else
632         /* Use AGP texturing */
633         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
634                          GL_STORAGE_SHARED_APPLE );
635 #endif
636 #endif
637
638         /* Call glTexImage2D only once, and use glTexSubImage2D later */
639         glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
640                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
641                       p_sys->pp_buffer[i_index] );
642     }
643
644     return 0;
645 }
646
647 /*****************************************************************************
648  * SendEvents: forward mouse and keyboard events to the parent p_vout
649  *****************************************************************************/
650 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
651                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
652 {
653     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
654 }