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