]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
90e4416fab9417c0e51e5bf17c5043a8c002c1c5
[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 <assert.h>
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout.h>
40 #include "opengl.h"
41
42 /*****************************************************************************
43  * Vout interface
44  *****************************************************************************/
45 static int  CreateVout   ( vlc_object_t * );
46 static void DestroyVout  ( vlc_object_t * );
47 static int  Init         ( vout_thread_t * );
48 static void End          ( vout_thread_t * );
49 static int  Manage       ( vout_thread_t * );
50 static void Render       ( vout_thread_t *, picture_t * );
51 static void DisplayVideo ( vout_thread_t *, picture_t * );
52 static int  Control      ( vout_thread_t *, int, va_list );
53
54 static int SendEvents    ( vlc_object_t *, char const *,
55                            vlc_value_t, vlc_value_t, void * );
56
57 #define PROVIDER_TEXT N_("OpenGL Provider")
58 #define PROVIDER_LONGTEXT N_("Allows you to modify what OpenGL provider should be used")
59
60 vlc_module_begin ()
61     set_shortname( "OpenGL" )
62     set_category( CAT_VIDEO )
63     set_subcategory( SUBCAT_VIDEO_VOUT )
64     set_description( N_("OpenGL video output") )
65 #ifdef __APPLE__
66     set_capability( "video output", 200 )
67 #else
68     set_capability( "video output", 20 )
69 #endif
70     add_shortcut( "opengl" )
71     /* Allow opengl provider plugin selection */
72     add_module( "opengl-provider", "opengl provider", NULL, NULL,
73                 PROVIDER_TEXT, PROVIDER_LONGTEXT, true )
74     set_callbacks( CreateVout, DestroyVout )
75 vlc_module_end ()
76
77 /*****************************************************************************
78  * vout_sys_t: video output method descriptor
79  *****************************************************************************
80  * This structure is part of the video output thread descriptor.
81  * It describes the OpenGL specific properties of the output thread.
82  *****************************************************************************/
83 struct vout_sys_t
84 {
85     vout_thread_t *p_vout;
86     vout_opengl_t gl;
87     vout_display_opengl_t vgl;
88
89     picture_pool_t *p_pool;
90     picture_t *p_current;
91 };
92
93 /*****************************************************************************
94  * CreateVout: This function allocates and initializes the OpenGL vout method.
95  *****************************************************************************/
96 static int CreateVout( vlc_object_t *p_this )
97 {
98     vout_thread_t *p_vout = (vout_thread_t *)p_this;
99     vout_sys_t *p_sys;
100     char * psz;
101
102     /* Allocate structure */
103     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
104     if( p_sys == NULL )
105         return VLC_ENOMEM;
106
107     /* Get window */
108     p_sys->p_vout =
109         (vout_thread_t *)vlc_object_create( p_this, sizeof( vout_thread_t ) );
110     if( p_sys->p_vout == NULL )
111     {
112         free( p_sys );
113         return VLC_ENOMEM;
114     }
115     vlc_object_attach( p_sys->p_vout, p_this );
116
117     p_sys->p_vout->i_window_width = p_vout->i_window_width;
118     p_sys->p_vout->i_window_height = p_vout->i_window_height;
119     p_sys->p_vout->b_fullscreen = p_vout->b_fullscreen;
120     p_sys->p_vout->render.i_width = p_vout->render.i_width;
121     p_sys->p_vout->render.i_height = p_vout->render.i_height;
122     p_sys->p_vout->render.i_aspect = p_vout->render.i_aspect;
123     p_sys->p_vout->fmt_render = p_vout->fmt_render;
124     p_sys->p_vout->fmt_in = p_vout->fmt_in;
125     p_sys->p_vout->b_autoscale = p_vout->b_autoscale;
126     p_sys->p_vout->i_zoom = p_vout->i_zoom;
127     p_sys->p_vout->i_alignment = p_vout->i_alignment;
128     var_Create( p_sys->p_vout, "video-deco",
129                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
130
131     /* Forward events from the opengl provider */
132     var_Create( p_sys->p_vout, "mouse-x", VLC_VAR_INTEGER );
133     var_Create( p_sys->p_vout, "mouse-y", VLC_VAR_INTEGER );
134     var_Create( p_sys->p_vout, "mouse-moved", VLC_VAR_BOOL );
135     var_Create( p_sys->p_vout, "mouse-clicked", VLC_VAR_BOOL );
136     var_Create( p_sys->p_vout, "mouse-button-down", VLC_VAR_INTEGER );
137     var_Create( p_sys->p_vout, "video-on-top",
138                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
139     var_Create( p_sys->p_vout, "autoscale",
140                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
141     var_Create( p_sys->p_vout, "scale",
142                 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
143
144     var_AddCallback( p_sys->p_vout, "mouse-x", SendEvents, p_vout );
145     var_AddCallback( p_sys->p_vout, "mouse-y", SendEvents, p_vout );
146     var_AddCallback( p_sys->p_vout, "mouse-moved", SendEvents, p_vout );
147     var_AddCallback( p_sys->p_vout, "mouse-clicked", SendEvents, p_vout );
148     var_AddCallback( p_sys->p_vout, "mouse-button-down", SendEvents, p_vout );
149     var_AddCallback( p_sys->p_vout, "video-on-top", SendEvents, p_vout );
150     var_AddCallback( p_vout, "autoscale", SendEvents, p_sys->p_vout );
151     var_AddCallback( p_vout, "scale", SendEvents, p_sys->p_vout );
152
153     psz = var_CreateGetString( p_vout, "opengl-provider" );
154     p_sys->p_vout->p_module =
155         module_need( p_sys->p_vout, "opengl provider", psz, false );
156     free( psz );
157     if( p_sys->p_vout->p_module == NULL )
158     {
159         msg_Warn( p_vout, "No OpenGL provider found" );
160         vlc_object_detach( p_sys->p_vout );
161         /* no need for var_DelCallback here :-) */
162         vlc_object_release( p_sys->p_vout );
163         free( p_sys );
164         return VLC_ENOOBJ;
165     }
166
167     p_vout->pf_init = Init;
168     p_vout->pf_end = End;
169     p_vout->pf_manage = Manage;
170     p_vout->pf_render = Render;
171     p_vout->pf_display = DisplayVideo;
172     p_vout->pf_control = Control;
173
174     return VLC_SUCCESS;
175 }
176
177 static int OpenglLock(vout_opengl_t *gl)
178 {
179     vout_thread_t *p_vout = gl->sys;
180
181     if( !p_vout->pf_lock )
182         return VLC_SUCCESS;
183     return p_vout->pf_lock( p_vout );
184 }
185 static void OpenglUnlock(vout_opengl_t *gl)
186 {
187     vout_thread_t *p_vout = gl->sys;
188
189     if( p_vout->pf_unlock )
190         p_vout->pf_unlock( p_vout );
191 }
192 static void OpenglSwap(vout_opengl_t *gl)
193 {
194     vout_thread_t *p_vout = gl->sys;
195     p_vout->pf_swap( p_vout );
196 }
197
198 /*****************************************************************************
199  * Init: initialize the OpenGL video thread output method
200  *****************************************************************************/
201 static int Init( vout_thread_t *p_vout )
202 {
203     vout_sys_t *p_sys = p_vout->p_sys;
204
205     p_sys->p_vout->pf_init( p_sys->p_vout );
206
207     p_sys->gl.lock = OpenglLock;
208     p_sys->gl.unlock = OpenglUnlock;
209     p_sys->gl.swap = OpenglSwap;
210     p_sys->gl.sys = p_sys->p_vout;
211
212     video_format_t fmt;
213     video_format_Init( &fmt, 0 );
214     video_format_Setup( &fmt,
215                         p_vout->render.i_chroma,
216                         p_vout->render.i_width,
217                         p_vout->render.i_height,
218                         p_vout->render.i_aspect * p_vout->render.i_height,
219                         VOUT_ASPECT_FACTOR      * p_vout->render.i_width );
220
221
222     if( vout_display_opengl_Init( &p_sys->vgl, &fmt, &p_sys->gl ) )
223     {
224         I_OUTPUTPICTURES = 0;
225         return VLC_EGENERIC;
226     }
227     p_sys->p_pool = vout_display_opengl_GetPool( &p_sys->vgl );
228     if( !p_sys->p_pool )
229     {
230         vout_display_opengl_Clean( &p_sys->vgl );
231         I_OUTPUTPICTURES = 0;
232         return VLC_EGENERIC;
233     }
234
235     /* */
236     p_vout->output.i_chroma = fmt.i_chroma;
237     p_vout->output.i_rmask  = fmt.i_rmask;
238     p_vout->output.i_gmask  = fmt.i_gmask;
239     p_vout->output.i_bmask  = fmt.i_bmask;
240
241     /* Since OpenGL can do rescaling for us, stick to the default
242      * coordinates and aspect. */
243     p_vout->output.i_width  = p_vout->render.i_width;
244     p_vout->output.i_height = p_vout->render.i_height;
245     p_vout->output.i_aspect = p_vout->render.i_aspect;
246
247     p_vout->fmt_out = p_vout->fmt_in;
248     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
249
250     /* */
251     p_sys->p_current = picture_pool_Get( p_sys->p_pool );
252     p_vout->p_picture[0] = *p_sys->p_current;
253     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
254     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
255     p_vout->p_picture[0].i_refcount = 0;
256     p_vout->p_picture[0].p_sys = NULL;
257     PP_OUTPUTPICTURE[0] = &p_vout->p_picture[0];
258
259     I_OUTPUTPICTURES = 1;
260
261     return VLC_SUCCESS;
262 }
263
264 /*****************************************************************************
265  * End: terminate GLX video thread output method
266  *****************************************************************************/
267 static void End( vout_thread_t *p_vout )
268 {
269     vout_sys_t *p_sys = p_vout->p_sys;
270
271     if( I_OUTPUTPICTURES > 0 )
272     {
273
274         if( p_sys->p_current )
275             picture_Release( p_sys->p_current );
276         vout_display_opengl_Clean( &p_sys->vgl );
277
278         p_vout->p_picture[0].i_status = FREE_PICTURE;
279         I_OUTPUTPICTURES = 0;
280     }
281
282     /* We must release the opengl provider here: opengl requiere init and end
283        to be done in the same thread */
284     module_unneed( p_sys->p_vout, p_sys->p_vout->p_module );
285     vlc_object_release( p_sys->p_vout );
286 }
287
288 /*****************************************************************************
289  * Destroy: destroy GLX video thread output method
290  *****************************************************************************
291  * Terminate an output method created by CreateVout
292  *****************************************************************************/
293 static void DestroyVout( vlc_object_t *p_this )
294 {
295     vout_thread_t *p_vout = (vout_thread_t *)p_this;
296     vout_sys_t *p_sys = p_vout->p_sys;
297
298     free( p_sys );
299 }
300
301 /*****************************************************************************
302  * Manage: handle Sys events
303  *****************************************************************************
304  * This function should be called regularly by video output thread. It returns
305  * a non null value if an error occurred.
306  *****************************************************************************/
307 static int Manage( vout_thread_t *p_vout )
308 {
309     vout_sys_t *p_sys = p_vout->p_sys;
310     int i_ret, i_fullscreen_change;
311
312     i_fullscreen_change = ( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE );
313
314     p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
315         p_vout->fmt_in.i_x_offset;
316     p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
317         p_vout->fmt_in.i_y_offset;
318     p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
319         p_vout->fmt_in.i_visible_width;
320     p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
321         p_vout->fmt_in.i_visible_height;
322     p_vout->fmt_out.i_sar_num = p_sys->p_vout->fmt_in.i_sar_num =
323         p_vout->fmt_in.i_sar_num;
324     p_vout->fmt_out.i_sar_den = p_sys->p_vout->fmt_in.i_sar_den =
325         p_vout->fmt_in.i_sar_den;
326     p_vout->output.i_aspect = (int64_t)p_vout->fmt_in.i_sar_num * p_vout->fmt_in.i_width * VOUT_ASPECT_FACTOR /
327                               p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_height;
328
329     p_sys->p_vout->i_changes = p_vout->i_changes;
330     i_ret = p_sys->p_vout->pf_manage( p_sys->p_vout );
331     p_vout->i_changes = p_sys->p_vout->i_changes;
332
333 #ifdef __APPLE__
334     /* On OS X, we create the window and the GL view when entering
335        fullscreen - the textures have to be inited again */
336     if( i_fullscreen_change )
337     {
338         /* FIXME should we release p_current ? */
339         vout_display_opengl_ResetTextures( &p_sys->vgl );
340     }
341 #endif
342
343 // to align in real time in OPENGL
344     if (p_sys->p_vout->i_alignment != p_vout->i_alignment)
345     {
346         p_vout->i_changes |= VOUT_CROP_CHANGE;        //to force change
347         p_sys->p_vout->i_alignment = p_vout->i_alignment;    
348     }
349
350     /* forward signal that autoscale toggle has changed */
351     if (p_vout->i_changes & VOUT_SCALE_CHANGE )
352     {
353         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
354
355         p_sys->p_vout->i_changes |= VOUT_SCALE_CHANGE;
356     }
357
358     /* forward signal that scale has changed */
359     if (p_vout->i_changes & VOUT_ZOOM_CHANGE )
360     {
361         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
362
363         p_sys->p_vout->i_changes |= VOUT_ZOOM_CHANGE;
364     }
365
366
367     return i_ret;
368 }
369
370 /*****************************************************************************
371  * Render: render previously calculated output
372  *****************************************************************************/
373 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
374 {
375     vout_sys_t *p_sys = p_vout->p_sys;
376
377     picture_t *p_next = p_sys->p_current;
378
379     if( VLCGL_TEXTURE_COUNT > 1 )
380     {
381         /* Get the next picture to display */
382         p_next = picture_pool_Get( p_sys->p_pool );
383         assert( p_next );
384     }
385   
386     if( p_sys->p_current )
387     {
388         assert( p_sys->p_current->p[0].p_pixels == p_pic->p[0].p_pixels );
389
390         /* Make sure we have the prepare after the picture_pool_Get,
391          * because picture_pool_Get() will bind the new picture texture,
392          * and vout_display_opengl_Prepare() bind the current rendered picture
393          * texture.
394          * DisplayVideo() will effectively use the last binded texture. */
395
396         vout_display_opengl_Prepare( &p_sys->vgl, p_sys->p_current );
397     }
398
399     if( p_sys->p_current != p_next ) {
400         if( p_sys->p_current )
401             picture_Release( p_sys->p_current );
402         
403         /* Swap the picture texture on opengl vout side. */
404         p_sys->p_current = p_next;
405
406         /* Now, switch the only picture that is being used
407          * to render in the backend to point to our "next"
408          * picture texture */
409         p_pic->p[0].p_pixels = p_sys->p_current->p[0].p_pixels;
410     }
411
412     VLC_UNUSED( p_pic );
413 }
414
415 /*****************************************************************************
416  * DisplayVideo: displays previously rendered output
417  *****************************************************************************/
418 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
419 {
420     vout_sys_t *p_sys = p_vout->p_sys;
421
422     vout_display_opengl_Display( &p_sys->vgl, &p_vout->fmt_out );
423     VLC_UNUSED( p_pic );
424 }
425
426 /*****************************************************************************
427  * Control: control facility for the vout
428  *****************************************************************************/
429 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
430 {
431     vout_sys_t *p_sys = p_vout->p_sys;
432
433     if( p_sys->p_vout->pf_control )
434         return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
435     return VLC_EGENERIC;
436 }
437
438 /*****************************************************************************
439  * SendEvents: forward mouse and keyboard events to the parent p_vout
440  *****************************************************************************/
441 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
442                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
443 {
444     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
445     return var_Set( (vlc_object_t *)_p_vout, psz_var, newval );
446 }
447