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