]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
opengl: Re-order #defines and fix a non defined token warning.
[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  *          Cedric Cocquebert <cedric.cocquebert@supelec.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <errno.h>                                                 /* ENOMEM */
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout.h>
40
41 #ifdef __APPLE__
42 # include <OpenGL/gl.h>
43 # include <OpenGL/glext.h>
44 #endif
45
46 #ifndef YCBCR_MESA
47 # define YCBCR_MESA 0x8757
48 #endif
49 #ifndef UNSIGNED_SHORT_8_8_MESA
50 # define UNSIGNED_SHORT_8_8_MESA 0x85BA
51 #endif
52 /* RV16 */
53 #ifndef GL_UNSIGNED_SHORT_5_6_5
54 # define GL_UNSIGNED_SHORT_5_6_5 0x8363
55 #endif
56 #ifndef GL_CLAMP_TO_EDGE
57 # define GL_CLAMP_TO_EDGE 0x812F
58 #endif
59
60 #ifdef __APPLE__
61 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
62    This allows sizes which are not powers of 2 */
63 # define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
64
65 /* OS X OpenGL supports YUV. Hehe. */
66 # define VLCGL_FORMAT GL_YCBCR_422_APPLE
67 # define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
68 #else
69
70 # include <GL/gl.h>
71 # define VLCGL_TARGET GL_TEXTURE_2D
72
73 /* RV32 */
74 # define VLCGL_RGB_FORMAT GL_RGBA
75 # define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
76
77 /* YUY2 */
78 # define VLCGL_YUV_FORMAT YCBCR_MESA
79 # define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
80
81 /* Use RGB on Win32/GLX */
82 # define VLCGL_FORMAT VLCGL_RGB_FORMAT
83 # define VLCGL_TYPE   VLCGL_RGB_TYPE
84 #endif
85
86
87 /*****************************************************************************
88  * Vout interface
89  *****************************************************************************/
90 static int  CreateVout   ( vlc_object_t * );
91 static void DestroyVout  ( vlc_object_t * );
92 static int  Init         ( vout_thread_t * );
93 static void End          ( vout_thread_t * );
94 static int  Manage       ( vout_thread_t * );
95 static void Render       ( vout_thread_t *, picture_t * );
96 static void DisplayVideo ( vout_thread_t *, picture_t * );
97 static int  Control      ( vout_thread_t *, int, va_list );
98
99 static inline int GetAlignedSize( int );
100
101 static int InitTextures  ( vout_thread_t * );
102 static int SendEvents    ( vlc_object_t *, char const *,
103                            vlc_value_t, vlc_value_t, void * );
104
105 #define PROVIDER_TEXT N_("OpenGL Provider")
106 #define PROVIDER_LONGTEXT N_("Allows you to modify what OpenGL provider should be used")
107
108 vlc_module_begin ()
109     set_shortname( "OpenGL" )
110     set_category( CAT_VIDEO )
111     set_subcategory( SUBCAT_VIDEO_VOUT )
112     set_description( N_("OpenGL video output") )
113 #ifdef __APPLE__
114     set_capability( "video output", 200 )
115 #else
116     set_capability( "video output", 20 )
117 #endif
118     add_shortcut( "opengl" )
119     /* Allow opengl provider plugin selection */
120     add_module( "opengl-provider", "opengl provider", NULL, NULL,
121                 PROVIDER_TEXT, PROVIDER_LONGTEXT, true )
122     set_callbacks( CreateVout, DestroyVout )
123 vlc_module_end ()
124
125 /*****************************************************************************
126  * vout_sys_t: video output method descriptor
127  *****************************************************************************
128  * This structure is part of the video output thread descriptor.
129  * It describes the OpenGL specific properties of the output thread.
130  *****************************************************************************/
131 struct vout_sys_t
132 {
133     vout_thread_t *p_vout;
134
135     uint8_t    *pp_buffer[2];
136     int         i_index;
137     int         i_tex_width;
138     int         i_tex_height;
139     GLuint      p_textures[2];
140 };
141
142 /*****************************************************************************
143  * CreateVout: This function allocates and initializes the OpenGL vout method.
144  *****************************************************************************/
145 static int CreateVout( vlc_object_t *p_this )
146 {
147     vout_thread_t *p_vout = (vout_thread_t *)p_this;
148     vout_sys_t *p_sys;
149     char * psz;
150
151     /* Allocate structure */
152     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
153     if( p_sys == NULL )
154         return VLC_ENOMEM;
155
156     p_sys->i_index = 0;
157 #ifdef __APPLE__
158     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
159     p_sys->i_tex_height = p_vout->fmt_in.i_height;
160 #else
161     /* A texture must have a size aligned on a power of 2 */
162     p_sys->i_tex_width  = GetAlignedSize( p_vout->fmt_in.i_width );
163     p_sys->i_tex_height = GetAlignedSize( p_vout->fmt_in.i_height );
164 #endif
165
166     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
167              p_sys->i_tex_height );
168
169     /* Get window */
170     p_sys->p_vout =
171         (vout_thread_t *)vlc_object_create( p_this, sizeof( vout_thread_t ) );
172     if( p_sys->p_vout == NULL )
173     {
174         free( p_sys );
175         return VLC_ENOMEM;
176     }
177     vlc_object_attach( p_sys->p_vout, p_this );
178
179     p_sys->p_vout->i_window_width = p_vout->i_window_width;
180     p_sys->p_vout->i_window_height = p_vout->i_window_height;
181     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
182     p_sys->p_vout->render.i_width = p_vout->render.i_width;
183     p_sys->p_vout->render.i_height = p_vout->render.i_height;
184     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
185     p_sys->p_vout->fmt_render = p_vout->fmt_render;
186     p_sys->p_vout->fmt_in = p_vout->fmt_in;
187     p_sys->p_vout->b_autoscale = p_vout->b_autoscale;
188     p_sys->p_vout->i_zoom = p_vout->i_zoom;
189     p_sys->p_vout->i_alignment = p_vout->i_alignment;
190     var_Create( p_sys->p_vout, "video-deco",
191                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
192
193     /* Forward events from the opengl provider */
194     var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
195     var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
196     var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
197     var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_BOOL );
198     var_Create( p_sys->p_vout, "mouse-button-down", VLC_VAR_INTEGER );
199     var_Create( p_sys->p_vout, "video-on-top",
200                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
201     var_Create( p_sys->p_vout, "autoscale",
202                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
203     var_Create( p_sys->p_vout, "scale",
204                 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
205
206     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
207     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
208     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
209     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
210     var_AddCallback( p_sys->p_vout, "mouse-button-down", SendEvents, p_vout );
211     var_AddCallback( p_sys->p_vout, "video-on-top", SendEvents, p_vout );
212     var_AddCallback( p_vout, "autoscale", SendEvents, p_sys->p_vout );
213     var_AddCallback( p_vout, "scale", SendEvents, p_sys->p_vout );
214
215     psz = var_CreateGetString( p_vout, "opengl-provider" );
216     p_sys->p_vout->p_module =
217         module_need( p_sys->p_vout, "opengl provider", psz, false );
218     free( psz );
219     if( p_sys->p_vout->p_module == NULL )
220     {
221         msg_Warn( p_vout, "No OpenGL provider found" );
222         vlc_object_detach( p_sys->p_vout );
223         /* no need for var_DelCallback here :-) */
224         vlc_object_release( p_sys->p_vout );
225         free( p_sys );
226         return VLC_ENOOBJ;
227     }
228
229     p_vout->pf_init = Init;
230     p_vout->pf_end = End;
231     p_vout->pf_manage = Manage;
232     p_vout->pf_render = Render;
233     p_vout->pf_display = DisplayVideo;
234     p_vout->pf_control = Control;
235
236     return VLC_SUCCESS;
237 }
238
239 /*****************************************************************************
240  * Init: initialize the OpenGL video thread output method
241  *****************************************************************************/
242 static int Init( vout_thread_t *p_vout )
243 {
244     vout_sys_t *p_sys = p_vout->p_sys;
245     int i_pixel_pitch;
246
247     p_sys->p_vout->pf_init( p_sys->p_vout );
248
249 /* TODO: We use YCbCr on Mac which is Y422, but on OSX it seems to == YUY2. Verify */
250 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
251     p_vout->output.i_chroma = VLC_CODEC_YUYV;
252     i_pixel_pitch = 2;
253
254 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
255     p_vout->output.i_chroma = VLC_CODEC_UYVY;
256     i_pixel_pitch = 2;
257
258 #elif VLCGL_FORMAT == GL_RGB
259 #   if VLCGL_TYPE == GL_UNSIGNED_BYTE
260     p_vout->output.i_chroma = VLC_CODEC_RGB24;
261 #       if defined( WORDS_BIGENDIAN )
262     p_vout->output.i_rmask = 0x00ff0000;
263     p_vout->output.i_gmask = 0x0000ff00;
264     p_vout->output.i_bmask = 0x000000ff;
265 #       else
266     p_vout->output.i_rmask = 0x000000ff;
267     p_vout->output.i_gmask = 0x0000ff00;
268     p_vout->output.i_bmask = 0x00ff0000;
269 #       endif
270     i_pixel_pitch = 3;
271 #   else
272     p_vout->output.i_chroma = VLC_CODEC_RGB16;
273 #       if defined( WORDS_BIGENDIAN )
274     p_vout->output.i_rmask = 0x001f;
275     p_vout->output.i_gmask = 0x07e0;
276     p_vout->output.i_bmask = 0xf800;
277 #       else
278     p_vout->output.i_rmask = 0xf800;
279     p_vout->output.i_gmask = 0x07e0;
280     p_vout->output.i_bmask = 0x001f;
281 #       endif
282     i_pixel_pitch = 2;
283 #   endif
284 #else
285     p_vout->output.i_chroma = VLC_CODEC_RGB32;
286 #       if defined( WORDS_BIGENDIAN )
287     p_vout->output.i_rmask = 0xff000000;
288     p_vout->output.i_gmask = 0x00ff0000;
289     p_vout->output.i_bmask = 0x0000ff00;
290 #       else
291     p_vout->output.i_rmask = 0x000000ff;
292     p_vout->output.i_gmask = 0x0000ff00;
293     p_vout->output.i_bmask = 0x00ff0000;
294 #       endif
295     i_pixel_pitch = 4;
296 #endif
297
298     /* Since OpenGL can do rescaling for us, stick to the default
299      * coordinates and aspect. */
300     p_vout->output.i_width  = p_vout->render.i_width;
301     p_vout->output.i_height = p_vout->render.i_height;
302     p_vout->output.i_aspect = p_vout->render.i_aspect;
303
304     p_vout->fmt_out = p_vout->fmt_in;
305     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
306
307     /* We know the chroma, allocate one buffer which will be used
308      * directly by the decoder */
309     p_sys->pp_buffer[0] =
310         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
311     if( !p_sys->pp_buffer[0] )
312         return -1;
313     p_sys->pp_buffer[1] =
314         malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
315     if( !p_sys->pp_buffer[1] )
316         return -1;
317
318     p_vout->p_picture[0].i_planes = 1;
319     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
320     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
321     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
322     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
323     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
324         p_vout->p_picture[0].p->i_pixel_pitch;
325     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
326         p_vout->p_picture[0].p->i_pixel_pitch;
327
328     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
329     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
330
331     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
332
333     I_OUTPUTPICTURES = 1;
334
335     if( p_sys->p_vout->pf_lock &&
336         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
337     {
338         msg_Warn( p_vout, "could not lock OpenGL provider" );
339         return 0;
340     }
341
342     InitTextures( p_vout );
343
344     glDisable(GL_BLEND);
345     glDisable(GL_DEPTH_TEST);
346     glDepthMask(GL_FALSE);
347     glDisable(GL_CULL_FACE);
348     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
349     glClear( GL_COLOR_BUFFER_BIT );
350
351     if( p_sys->p_vout->pf_unlock )
352     {
353         p_sys->p_vout->pf_unlock( p_sys->p_vout );
354     }
355
356     return 0;
357 }
358
359 /*****************************************************************************
360  * End: terminate GLX video thread output method
361  *****************************************************************************/
362 static void End( vout_thread_t *p_vout )
363 {
364     vout_sys_t *p_sys = p_vout->p_sys;
365
366     if( p_sys->p_vout->pf_lock &&
367         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
368     {
369         msg_Warn( p_vout, "could not lock OpenGL provider" );
370         return;
371     }
372
373     glFinish();
374     glFlush();
375
376     /* Free the texture buffer*/
377     glDeleteTextures( 2, p_sys->p_textures );
378     free( p_sys->pp_buffer[0] );
379     free( p_sys->pp_buffer[1] );
380
381     if( p_sys->p_vout->pf_unlock )
382     {
383         p_sys->p_vout->pf_unlock( p_sys->p_vout );
384     }
385
386     /* We must release the opengl provider here: opengl requiere init and end
387        to be done in the same thread */
388     module_unneed( p_sys->p_vout, p_sys->p_vout->p_module );
389     vlc_object_release( p_sys->p_vout );
390 }
391
392 /*****************************************************************************
393  * Destroy: destroy GLX video thread output method
394  *****************************************************************************
395  * Terminate an output method created by CreateVout
396  *****************************************************************************/
397 static void DestroyVout( vlc_object_t *p_this )
398 {
399     vout_thread_t *p_vout = (vout_thread_t *)p_this;
400     vout_sys_t *p_sys = p_vout->p_sys;
401
402     free( p_sys );
403 }
404
405 /*****************************************************************************
406  * Manage: handle Sys events
407  *****************************************************************************
408  * This function should be called regularly by video output thread. It returns
409  * a non null value if an error occurred.
410  *****************************************************************************/
411 static int Manage( vout_thread_t *p_vout )
412 {
413     vout_sys_t *p_sys = p_vout->p_sys;
414     int i_ret, i_fullscreen_change;
415
416     i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
417
418     p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
419         p_vout->fmt_in.i_x_offset;
420     p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
421         p_vout->fmt_in.i_y_offset;
422     p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
423         p_vout->fmt_in.i_visible_width;
424     p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
425         p_vout->fmt_in.i_visible_height;
426     p_vout->fmt_out.i_aspect = p_sys->p_vout->fmt_in.i_aspect =
427         p_vout->fmt_in.i_aspect;
428     p_vout->fmt_out.i_sar_num = p_sys->p_vout->fmt_in.i_sar_num =
429         p_vout->fmt_in.i_sar_num;
430     p_vout->fmt_out.i_sar_den = p_sys->p_vout->fmt_in.i_sar_den =
431         p_vout->fmt_in.i_sar_den;
432     p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
433
434     p_sys->p_vout->i_changes = p_vout->i_changes;
435     i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
436     p_vout->i_changes = p_sys->p_vout->i_changes;
437
438 #ifdef __APPLE__
439     if( p_sys->p_vout->pf_lock &&
440         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
441     {
442         msg_Warn( p_vout, "could not lock OpenGL provider" );
443         return i_ret;
444     }
445
446     /* On OS X, we create the window and the GL view when entering
447        fullscreen - the textures have to be inited again */
448     if( i_fullscreen_change )
449     {
450         InitTextures( p_vout );
451     }
452
453     if( p_sys->p_vout->pf_unlock )
454     {
455         p_sys->p_vout->pf_unlock( p_sys->p_vout );
456     }
457 #endif
458 // to align in real time in OPENGL
459     if (p_sys->p_vout->i_alignment != p_vout->i_alignment)
460     {
461         p_vout->i_changes |= VOUT_CROP_CHANGE;        //to force change
462         p_sys->p_vout->i_alignment = p_vout->i_alignment;    
463     }
464
465     /* forward signal that autoscale toggle has changed */
466     if (p_vout->i_changes & VOUT_SCALE_CHANGE )
467     {
468         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
469
470         p_sys->p_vout->i_changes |= VOUT_SCALE_CHANGE;
471     }
472
473     /* forward signal that scale has changed */
474     if (p_vout->i_changes & VOUT_ZOOM_CHANGE )
475     {
476         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
477
478         p_sys->p_vout->i_changes |= VOUT_ZOOM_CHANGE;
479     }
480
481
482     return i_ret;
483 }
484
485 /*****************************************************************************
486  * Render: render previously calculated output
487  *****************************************************************************/
488 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
489 {
490     VLC_UNUSED(p_pic);
491     vout_sys_t *p_sys = p_vout->p_sys;
492
493     /* On Win32/GLX, we do this the usual way:
494        + Fill the buffer with new content,
495        + Reload the texture,
496        + Use the texture.
497
498        On OS X with VRAM or AGP texturing, the order has to be:
499        + Reload the texture,
500        + Fill the buffer with new content,
501        + Use the texture.
502
503        (Thanks to gcc from the Arstechnica forums for the tip)
504
505        Therefore, we have to use two buffers and textures. On Win32/GLX,
506        we reload the texture to be displayed and use it right away. On
507        OS X, we first render, then reload the texture to be used next
508        time. */
509
510     if( p_sys->p_vout->pf_lock &&
511         p_sys->p_vout->pf_lock( p_sys->p_vout ) )
512     {
513         msg_Warn( p_vout, "could not lock OpenGL provider" );
514         return;
515     }
516
517 #ifdef __APPLE__
518     int i_new_index;
519     i_new_index = ( p_sys->i_index + 1 ) & 1;
520
521
522     /* Update the texture */
523     glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
524     glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
525                      p_vout->fmt_out.i_width,
526                      p_vout->fmt_out.i_height,
527                      VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
528
529     /* Bind to the previous texture for drawing */
530     glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
531
532     /* Switch buffers */
533     p_sys->i_index = i_new_index;
534     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
535
536 #else
537     /* Update the texture */
538     glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0,
539                      p_vout->fmt_out.i_width,
540                      p_vout->fmt_out.i_height,
541                      VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[0] );
542 #endif
543
544     if( p_sys->p_vout->pf_unlock )
545     {
546         p_sys->p_vout->pf_unlock( p_sys->p_vout );
547     }
548 }
549
550 /*****************************************************************************
551  * DisplayVideo: displays previously rendered output
552  *****************************************************************************/
553 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
554 {
555     VLC_UNUSED(p_pic);
556     vout_sys_t *p_sys = p_vout->p_sys;
557     float f_width, f_height, f_x, f_y;
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     /* glTexCoord works differently with GL_TEXTURE_2D and
567        GL_TEXTURE_RECTANGLE_EXT */
568 #ifdef __APPLE__
569     f_x = (float)p_vout->fmt_out.i_x_offset;
570     f_y = (float)p_vout->fmt_out.i_y_offset;
571     f_width = (float)p_vout->fmt_out.i_x_offset +
572               (float)p_vout->fmt_out.i_visible_width;
573     f_height = (float)p_vout->fmt_out.i_y_offset +
574                (float)p_vout->fmt_out.i_visible_height;
575 #else
576     f_x = (float)p_vout->fmt_out.i_x_offset / p_sys->i_tex_width;
577     f_y = (float)p_vout->fmt_out.i_y_offset / p_sys->i_tex_height;
578     f_width = ( (float)p_vout->fmt_out.i_x_offset +
579                 p_vout->fmt_out.i_visible_width ) / p_sys->i_tex_width;
580     f_height = ( (float)p_vout->fmt_out.i_y_offset +
581                  p_vout->fmt_out.i_visible_height ) / p_sys->i_tex_height;
582 #endif
583
584     /* Why drawing here and not in Render()? Because this way, the
585        OpenGL providers can call pf_display to force redraw. Currently,
586        the OS X provider uses it to get a smooth window resizing */
587
588     glClear( GL_COLOR_BUFFER_BIT );
589
590     glEnable( VLCGL_TARGET );
591     glBegin( GL_POLYGON );
592     glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
593     glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
594     glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
595     glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
596     glEnd();
597
598     glDisable( VLCGL_TARGET );
599
600     p_sys->p_vout->pf_swap( p_sys->p_vout );
601
602     if( p_sys->p_vout->pf_unlock )
603     {
604         p_sys->p_vout->pf_unlock( p_sys->p_vout );
605     }
606 }
607
608 int GetAlignedSize( int i_size )
609 {
610     /* Return the nearest power of 2 */
611     int i_result = 1;
612     while( i_result < i_size )
613     {
614         i_result *= 2;
615     }
616     return i_result;
617 }
618
619 /*****************************************************************************
620  * Control: control facility for the vout
621  *****************************************************************************/
622 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
623 {
624     vout_sys_t *p_sys = p_vout->p_sys;
625
626     if( p_sys->p_vout->pf_control )
627         return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
628     return VLC_EGENERIC;
629 }
630
631 static int InitTextures( vout_thread_t *p_vout )
632 {
633     vout_sys_t *p_sys = p_vout->p_sys;
634     int i_index;
635
636     glDeleteTextures( 2, p_sys->p_textures );
637     glGenTextures( 2, p_sys->p_textures );
638
639     for( i_index = 0; i_index < 2; i_index++ )
640     {
641         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
642
643         /* Set the texture parameters */
644         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
645
646         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
647         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
648
649         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
650         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
651
652         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
653
654 #ifdef __APPLE__
655         /* Tell the driver not to make a copy of the texture but to use
656            our buffer */
657         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
658         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
659
660 #if 0
661         /* Use VRAM texturing */
662         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
663                          GL_STORAGE_CACHED_APPLE );
664 #else
665         /* Use AGP texturing */
666         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
667                          GL_STORAGE_SHARED_APPLE );
668 #endif
669 #endif
670
671         /* Call glTexImage2D only once, and use glTexSubImage2D later */
672         glTexImage2D( VLCGL_TARGET, 0, 3, p_sys->i_tex_width,
673                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
674                       p_sys->pp_buffer[i_index] );
675     }
676
677     return 0;
678 }
679
680 /*****************************************************************************
681  * SendEvents: forward mouse and keyboard events to the parent p_vout
682  *****************************************************************************/
683 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
684                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
685 {
686     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
687     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
688 }