]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
opengl.c: higher priority on OS X
[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", 200 );
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     p_sys->i_index = 0;
166 #ifdef SYS_DARWIN
167     p_sys->i_tex_width  = p_vout->render.i_width;
168     p_sys->i_tex_height = p_vout->render.i_height;
169 #else
170     /* A texture must have a size aligned on a power of 2 */
171     p_sys->i_tex_width  = GetAlignedSize( p_vout->render.i_width );
172     p_sys->i_tex_height = GetAlignedSize( p_vout->render.i_height );
173 #endif
174
175     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
176              p_sys->i_tex_height );
177
178     /* Get window */
179     p_sys->p_vout =
180         (vout_thread_t *)vlc_object_create( p_this, VLC_OBJECT_OPENGL );
181     if( p_sys->p_vout == NULL )
182     {
183         msg_Err( p_vout, "out of memory" );
184         return VLC_ENOMEM;
185     }
186     vlc_object_attach( p_sys->p_vout, p_this );
187
188     p_sys->p_vout->i_window_width = p_vout->i_window_width;
189     p_sys->p_vout->i_window_height = p_vout->i_window_height;
190     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
191     p_sys->p_vout->render.i_width = p_vout->render.i_width;
192     p_sys->p_vout->render.i_height = p_vout->render.i_height;
193     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
194     p_sys->p_vout->b_scale = p_vout->b_scale;
195     p_sys->p_vout->i_alignment = p_vout->i_alignment;
196
197     p_sys->p_vout->p_module =
198         module_Need( p_sys->p_vout, "opengl provider", NULL, 0 );
199     if( p_sys->p_vout->p_module == NULL )
200     {
201         msg_Warn( p_vout, "No OpenGL provider found" );
202         vlc_object_detach( p_sys->p_vout );
203         vlc_object_destroy( p_sys->p_vout );
204         return VLC_ENOOBJ;
205     }
206
207     p_sys->f_speed = var_CreateGetFloat( p_vout, "opengl-cube-speed" );
208
209     p_vout->pf_init = Init;
210     p_vout->pf_end = End;
211     p_vout->pf_manage = Manage;
212     p_vout->pf_render = Render;
213     p_vout->pf_display = DisplayVideo;
214     p_vout->pf_control = Control;
215
216     /* Forward events from the opengl provider */
217     var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
218     var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
219     var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
220     var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_INTEGER );
221     var_Create( p_sys->p_vout, "video-on-top",
222                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
223
224     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
225     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
226     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
227     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
228
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * Init: initialize the OpenGL video thread output method
234  *****************************************************************************/
235 static int Init( vout_thread_t *p_vout )
236 {
237     vout_sys_t *p_sys = p_vout->p_sys;
238     int i_pixel_pitch;
239     vlc_value_t val;
240
241     p_sys->p_vout->pf_init( p_sys->p_vout );
242
243 #ifdef SYS_DARWIN
244     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
245     p_vout->output.i_rmask = 0x00ff0000;
246     p_vout->output.i_gmask = 0x0000ff00;
247     p_vout->output.i_bmask = 0x000000ff;
248     i_pixel_pitch = 2;
249 #else
250 #if VLCGL_RGB_FORMAT == GL_RGB
251 #   if VLCGL_RGB_TYPE == GL_UNSIGNED_BYTE
252     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
253     p_vout->output.i_rmask = 0x000000ff;
254     p_vout->output.i_gmask = 0x0000ff00;
255     p_vout->output.i_bmask = 0x00ff0000;
256     i_pixel_pitch = 3;
257 #   else
258     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
259     p_vout->output.i_rmask = 0xf800;
260     p_vout->output.i_gmask = 0x07e0;
261     p_vout->output.i_bmask = 0x001f;
262     i_pixel_pitch = 2;
263 #   endif
264 #else
265     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
266     p_vout->output.i_rmask = 0x000000ff;
267     p_vout->output.i_gmask = 0x0000ff00;
268     p_vout->output.i_bmask = 0x00ff0000;
269     i_pixel_pitch = 4;
270 #endif
271 #endif
272
273     /* Since OpenGL can do rescaling for us, stick to the default
274      * coordinates and aspect. */
275     p_vout->output.i_width  = p_vout->render.i_width;
276     p_vout->output.i_height = p_vout->render.i_height;
277     p_vout->output.i_aspect = p_vout->render.i_aspect;
278
279     /* We know the chroma, allocate one buffer which will be used
280      * directly by the decoder */
281     p_sys->pp_buffer[0] =
282         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
283     if( !p_sys->pp_buffer[0] )
284     {
285         msg_Err( p_vout, "Out of memory" );
286         return -1;
287     }
288     p_sys->pp_buffer[1] =
289         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
290     if( !p_sys->pp_buffer[1] )
291     {
292         msg_Err( p_vout, "Out of memory" );
293         return -1;
294     }
295
296     p_vout->p_picture[0].i_planes = 1;
297     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
298     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
299     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
300     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
301     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
302         p_vout->p_picture[0].p->i_pixel_pitch;
303     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
304         p_vout->p_picture[0].p->i_pixel_pitch;
305
306     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
307     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
308
309     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
310
311     I_OUTPUTPICTURES = 1;
312
313     InitTextures( p_vout );
314
315     glDisable(GL_BLEND);
316     glDisable(GL_DEPTH_TEST);
317     glDepthMask(GL_FALSE);
318     glDisable(GL_CULL_FACE);
319     glClear( GL_COLOR_BUFFER_BIT );
320
321     /* Check if the user asked for useless visual effects */
322     var_Get( p_vout, "opengl-effect", &val );
323     if( !val.psz_string || !strcmp( val.psz_string, "none" ))
324     {
325         p_sys->i_effect = OPENGL_EFFECT_NONE;
326     }
327     else if( !strcmp( val.psz_string, "cube" ) )
328     {
329         p_sys->i_effect = OPENGL_EFFECT_CUBE;
330
331         glEnable( GL_CULL_FACE);
332         //glEnable( GL_DEPTH_TEST );
333     }
334     else if( !strcmp( val.psz_string, "transparent-cube" ) )
335     {
336         p_sys->i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
337
338         glDisable( GL_DEPTH_TEST );
339         glEnable( GL_BLEND );
340         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
341     }
342     else
343     {
344         msg_Warn( p_vout, "no valid opengl effect provided, using "
345                   "\"none\"" );
346         p_sys->i_effect = OPENGL_EFFECT_NONE;
347     }
348     if( val.psz_string ) free( val.psz_string );
349
350     if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
351                 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
352     {
353         /* Set the perpective */
354         glMatrixMode( GL_PROJECTION );
355         glLoadIdentity();
356         glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
357         glMatrixMode( GL_MODELVIEW );
358         glLoadIdentity();
359         glTranslatef( 0.0, 0.0, - 5.0 );
360     }
361
362     return 0;
363 }
364
365 /*****************************************************************************
366  * End: terminate GLX video thread output method
367  *****************************************************************************/
368 static void End( vout_thread_t *p_vout )
369 {
370     glFinish();
371     glFlush();
372 }
373
374 /*****************************************************************************
375  * Destroy: destroy GLX video thread output method
376  *****************************************************************************
377  * Terminate an output method created by CreateVout
378  *****************************************************************************/
379 static void DestroyVout( vlc_object_t *p_this )
380 {
381     vout_thread_t *p_vout = (vout_thread_t *)p_this;
382     vout_sys_t *p_sys = p_vout->p_sys;
383
384     module_Unneed( p_sys->p_vout, p_sys->p_vout->p_module );
385     vlc_object_detach( p_sys->p_vout );
386     vlc_object_destroy( p_sys->p_vout );
387
388     /* Free the texture buffer*/
389     if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
390     if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
391
392     free( p_sys );
393 }
394
395 /*****************************************************************************
396  * Manage: handle Sys events
397  *****************************************************************************
398  * This function should be called regularly by video output thread. It returns
399  * a non null value if an error occurred.
400  *****************************************************************************/
401 static int Manage( vout_thread_t *p_vout )
402 {
403     vout_sys_t *p_sys = p_vout->p_sys;
404     int i_ret, i_fullscreen_change;
405
406     i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
407
408     p_sys->p_vout->i_changes = p_vout->i_changes;
409     i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
410     p_vout->i_changes = p_sys->p_vout->i_changes;
411
412 #ifdef SYS_DARWIN
413     /* On OS X, we create the window and the GL view when entering
414        fullscreen - the textures have to be inited again */
415     if( i_fullscreen_change )
416     {
417         InitTextures( p_vout );
418
419         switch( p_sys->i_effect )
420         {
421             case OPENGL_EFFECT_CUBE:
422                 glEnable( GL_CULL_FACE );
423                 break;
424
425             case OPENGL_EFFECT_TRANSPARENT_CUBE:
426                 glDisable( GL_DEPTH_TEST );
427                 glEnable( GL_BLEND );
428                 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
429                 break;
430         }
431
432         if( p_sys->i_effect & ( OPENGL_EFFECT_CUBE |
433                     OPENGL_EFFECT_TRANSPARENT_CUBE ) )
434         {
435             /* Set the perpective */
436             glMatrixMode( GL_PROJECTION );
437             glLoadIdentity();
438             glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
439             glMatrixMode( GL_MODELVIEW );
440             glLoadIdentity();
441             glTranslatef( 0.0, 0.0, - 5.0 );
442         }
443     }
444 #endif
445
446     return i_ret;
447 }
448
449 /*****************************************************************************
450  * Render: render previously calculated output
451  *****************************************************************************/
452 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
453 {
454     vout_sys_t *p_sys = p_vout->p_sys;
455
456     /* On Win32/GLX, we do this the usual way:
457        + Fill the buffer with new content,
458        + Reload the texture,
459        + Use the texture.
460
461        On OS X with VRAM or AGP texturing, the order has to be:
462        + Reload the texture,
463        + Fill the buffer with new content,
464        + Use the texture.
465
466        (Thanks to gcc from the Arstechnica forums for the tip)
467
468        Therefore, we have to use two buffers and textures. On Win32/GLX,
469        we reload the texture to be displayed and use it right away. On
470        OS X, we first render, then reload the texture to be used next
471        time. */
472
473 #ifdef SYS_DARWIN
474     int i_new_index;
475     i_new_index = ( p_sys->i_index + 1 ) & 1;
476
477     /* Update the texture */
478     glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
479     glTexSubImage2D( VLCGL_TARGET, 0, 0, 0, p_sys->i_tex_width,
480                      p_sys->i_tex_height, VLCGL_FORMAT, VLCGL_TYPE,
481                      p_sys->pp_buffer[i_new_index] );
482
483     /* Bind to the previous texture for drawing */
484     glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
485
486     /* Switch buffers */
487     p_sys->i_index = i_new_index;
488     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
489
490 #else
491     /* Update the texture */
492     glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
493                      p_vout->render.i_width, p_vout->render.i_height,
494                      VLCGL_RGB_FORMAT, VLCGL_RGB_TYPE, p_sys->pp_buffer[0] );
495 #endif
496 }
497
498 /*****************************************************************************
499  * DisplayVideo: displays previously rendered output
500  *****************************************************************************/
501 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
502 {
503     vout_sys_t *p_sys = p_vout->p_sys;
504     float f_width, f_height;
505
506     /* glTexCoord works differently with GL_TEXTURE_2D and
507        GL_TEXTURE_RECTANGLE_EXT */
508 #ifdef SYS_DARWIN
509     f_width = (float)p_vout->output.i_width;
510     f_height = (float)p_vout->output.i_height;
511 #else
512     f_width = (float)p_vout->output.i_width / p_sys->i_tex_width;
513     f_height = (float)p_vout->output.i_height / p_sys->i_tex_height;
514 #endif
515
516     /* Why drawing here and not in Render()? Because this way, the
517        OpenGL providers can call pf_display to force redraw. Currently,
518        the OS X provider uses it to get a smooth window resizing */
519
520     glClear( GL_COLOR_BUFFER_BIT );
521
522     if( p_sys->i_effect == OPENGL_EFFECT_NONE )
523     {
524         glEnable( VLCGL_TARGET );
525         glBegin( GL_POLYGON );
526         glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
527         glTexCoord2f( f_width, 0.0 ); glVertex2f( 1.0, 1.0 );
528         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
529         glTexCoord2f( 0.0, f_height ); glVertex2f( -1.0, -1.0 );
530         glEnd();
531     }
532     else
533     {
534         glRotatef( 0.5 * p_sys->f_speed , 0.3, 0.5, 0.7 );
535
536         glEnable( VLCGL_TARGET );
537         glBegin( GL_QUADS );
538
539         /* Front */
540         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
541         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
542         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
543         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
544
545         /* Left */
546         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
547         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
548         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, 1.0 );
549         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, 1.0 );
550
551         /* Back */
552         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
553         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
554         glTexCoord2f( f_width, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
555         glTexCoord2f( f_width, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
556
557         /* Right */
558         glTexCoord2f( 0, 0 ); glVertex3f( 1.0, 1.0, 1.0 );
559         glTexCoord2f( 0, f_height ); glVertex3f( 1.0, - 1.0, 1.0 );
560         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
561         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
562
563         /* Top */
564         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, 1.0, - 1.0 );
565         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, 1.0, 1.0 );
566         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, 1.0, 1.0 );
567         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, 1.0, - 1.0 );
568
569         /* Bottom */
570         glTexCoord2f( 0, 0 ); glVertex3f( - 1.0, - 1.0, 1.0 );
571         glTexCoord2f( 0, f_height ); glVertex3f( - 1.0, - 1.0, - 1.0 );
572         glTexCoord2f( f_width, f_height ); glVertex3f( 1.0, - 1.0, - 1.0 );
573         glTexCoord2f( f_width, 0 ); glVertex3f( 1.0, - 1.0, 1.0 );
574         glEnd();
575     }
576
577     glDisable( VLCGL_TARGET );
578
579     p_sys->p_vout->pf_swap( p_sys->p_vout );
580 }
581
582 int GetAlignedSize( int i_size )
583 {
584     /* Return the nearest power of 2 */
585     int i_result = 1;
586     while( i_result < i_size )
587     {
588         i_result *= 2;
589     }
590     return i_result;
591 }
592
593 /*****************************************************************************
594  * Control: control facility for the vout
595  *****************************************************************************/
596 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
597 {
598     vout_sys_t *p_sys = p_vout->p_sys;
599
600     if( p_sys->p_vout->pf_control )
601         return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
602     else
603         return vout_vaControlDefault( p_vout, i_query, args );
604 }
605
606 static int InitTextures( vout_thread_t *p_vout )
607 {
608     vout_sys_t *p_sys = p_vout->p_sys;
609     int i_index;
610
611     glDeleteTextures( 2, p_sys->p_textures );
612     glGenTextures( 2, p_sys->p_textures );
613
614     for( i_index = 0; i_index < 2; i_index++ )
615     {
616         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
617     
618         /* Set the texture parameters */
619         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
620     
621         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
622         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
623     
624         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP );
625         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP );
626     
627         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
628
629 #ifdef SYS_DARWIN
630         /* Tell the driver not to make a copy of the texture but to use
631            our buffer */
632         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
633         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
634     
635 #if 0
636         /* Use VRAM texturing */
637         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
638                          GL_STORAGE_CACHED_APPLE );
639 #else
640         /* Use AGP texturing */
641         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
642                          GL_STORAGE_SHARED_APPLE );
643 #endif
644 #endif
645
646         /* Call glTexImage2D only once, and use glTexSubImage2D later */
647         glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
648                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
649                       p_sys->pp_buffer[i_index] );
650     }
651
652     return 0;
653 }
654
655 /*****************************************************************************
656  * SendEvents: forward mouse and keyboard events to the parent p_vout
657  *****************************************************************************/
658 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
659                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
660 {
661     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
662 }