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