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