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