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