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