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