]> git.sesse.net Git - vlc/blob - modules/video_output/opengllayer.m
Move x11-display config to core
[vlc] / modules / video_output / opengllayer.m
1 /*****************************************************************************
2  * opengl.c: CAOpenGLLayer (Mac OS X) video output. Display a video output in
3  * a layer. The layer will register itself to the drawable object stored in 
4  * the "drawable" variable. 
5  *****************************************************************************
6  * Copyright (C) 2004-2009 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Cyril Deguet <asmax@videolan.org>
10  *          Gildas Bazin <gbazin@videolan.org>
11  *          Eric Petit <titer@m0k.org>
12  *          Cedric Cocquebert <cedric.cocquebert@supelec.fr>
13  *          Pierre d'Herbemont <pdherbemont # videolan.org>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_vout.h>
42
43 #import <QuartzCore/QuartzCore.h>
44 #import <Cocoa/Cocoa.h>
45 #import <OpenGL/OpenGL.h>
46
47 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
48    This allows sizes which are not powers of 2 */
49 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
50
51 /* OS X OpenGL supports YUV. Hehe. */
52 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
53 #define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
54
55 /* RV32 */
56 #define VLCGL_RGB_FORMAT GL_RGBA
57 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
58
59 /* YUY2 */
60 #ifndef YCBCR_MESA
61 #define YCBCR_MESA 0x8757
62 #endif
63 #ifndef UNSIGNED_SHORT_8_8_MESA
64 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
65 #endif
66 #define VLCGL_YUV_FORMAT YCBCR_MESA
67 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
68
69
70 #ifndef GL_CLAMP_TO_EDGE
71 #   define GL_CLAMP_TO_EDGE 0x812F
72 #endif
73
74 @interface VLCVideoView : NSObject
75 - (void)addVoutLayer:(CALayer *)layer;
76 @end
77
78 /*****************************************************************************
79  * Vout interface
80  *****************************************************************************/
81 static int  CreateVout   ( vlc_object_t * );
82 static void DestroyVout  ( vlc_object_t * );
83 static int  Init         ( vout_thread_t * );
84 static void End          ( vout_thread_t * );
85 static int  Manage       ( vout_thread_t * );
86 static void Render       ( vout_thread_t *, picture_t * );
87 static void DisplayVideo ( vout_thread_t *, picture_t * );
88 static int  Control      ( vout_thread_t *, int, va_list );
89
90 static int InitTextures  ( vout_thread_t * );
91
92 vlc_module_begin ()
93     set_shortname( "OpenGLLayer" )
94     set_category( CAT_VIDEO )
95     set_subcategory( SUBCAT_VIDEO_VOUT )
96     set_description( N_("Core Animation OpenGL Layer (Mac OS X)") )
97     set_capability( "video output", 20 )
98     add_shortcut( "opengllayer" )
99     set_callbacks( CreateVout, DestroyVout )
100 vlc_module_end ()
101
102 @interface VLCVoutLayer : CAOpenGLLayer {
103     vout_thread_t * p_vout;
104 }
105 + (id)layerWithVout:(vout_thread_t*)_p_vout; 
106 @end
107
108 /*****************************************************************************
109  * vout_sys_t: video output method descriptor
110  *****************************************************************************
111  * This structure is part of the video output thread descriptor.
112  * It describes the OpenGL specific properties of the output thread.
113  *****************************************************************************/
114 struct vout_sys_t
115 {
116     vout_thread_t * p_vout;
117
118     uint8_t    *pp_buffer[2]; /* one last rendered, one to be rendered */
119     int         i_index;
120     bool  b_frame_available;
121     
122     CGLContextObj glContext;
123
124     int         i_tex_width;
125     int         i_tex_height;
126     GLuint      p_textures[2];
127
128     NSAutoreleasePool *autorealease_pool;
129     VLCVoutLayer * o_layer;
130     id          o_cocoa_container;
131 };
132
133 /*****************************************************************************
134  * CreateVout: This function allocates and initializes the OpenGL vout method.
135  *****************************************************************************/
136 static int CreateVout( vlc_object_t *p_this )
137 {
138     vout_thread_t *p_vout = (vout_thread_t *)p_this;
139     vout_sys_t *p_sys;
140     char * psz;
141
142     /* Allocate structure */
143     p_vout->p_sys = p_sys = calloc( 1, sizeof( vout_sys_t ) );
144     if( p_sys == NULL )
145         return VLC_EGENERIC;
146
147     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
148     p_sys->i_tex_height = p_vout->fmt_in.i_height;
149
150     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
151              p_sys->i_tex_height );
152
153     p_vout->pf_init = Init;
154     p_vout->pf_end = End;
155     p_vout->pf_manage = Manage;
156     p_vout->pf_render = Render;
157     p_vout->pf_display = DisplayVideo;
158     p_vout->pf_control = Control;
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Init: initialize the OpenGL video thread output method
165  *****************************************************************************/
166 static int Init( vout_thread_t *p_vout )
167 {
168     vout_sys_t *p_sys = p_vout->p_sys;
169     int i_pixel_pitch;
170
171 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
172     p_vout->output.i_chroma = VLC_CODEC_YUYV;
173     i_pixel_pitch = 2;
174 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
175     p_vout->output.i_chroma = VLC_CODEC_UYVY;
176     i_pixel_pitch = 2;
177 #endif
178
179     /* Since OpenGL can do rescaling for us, stick to the default
180      * coordinates and aspect. */
181     p_vout->output.i_width  = p_vout->render.i_width;
182     p_vout->output.i_height = p_vout->render.i_height;
183     p_vout->output.i_aspect = p_vout->render.i_aspect;
184
185     /* We do need a drawable to work properly */
186     p_vout->p_sys->o_cocoa_container = (id)var_CreateGetAddress( p_vout,
187                                                     "drawable-nsobject" );
188
189     p_vout->fmt_out = p_vout->fmt_in;
190     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
191
192     /* We know the chroma, allocate two buffer which will be used
193      * directly by the decoder */
194     int i;
195     for( i = 0; i < 2; i++ )
196     {
197         p_sys->pp_buffer[i] =
198             malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
199         if( !p_sys->pp_buffer[i] )
200             return VLC_EGENERIC;
201     }
202     p_sys->b_frame_available = false;
203     p_sys->i_index = 0;
204
205     p_vout->p_picture[0].i_planes = 1;
206     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
207     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
208     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
209     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
210     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
211         p_vout->p_picture[0].p->i_pixel_pitch;
212     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
213         p_vout->p_picture[0].p->i_pixel_pitch;
214
215     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
216     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
217
218     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
219
220     I_OUTPUTPICTURES = 1;
221     p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
222
223     [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
224                              withObject:[NSValue valueWithPointer:p_vout]
225                              waitUntilDone:YES];
226
227     return 0;
228 }
229
230 /*****************************************************************************
231  * End: terminate GLX video thread output method
232  *****************************************************************************/
233 static void End( vout_thread_t *p_vout )
234 {
235     vout_sys_t *p_sys = p_vout->p_sys;
236
237     p_vout->p_sys->b_frame_available = false;
238
239     [p_vout->p_sys->o_cocoa_container performSelectorOnMainThread:@selector(removeVoutLayer:) withObject:p_vout->p_sys->o_layer waitUntilDone:YES];
240
241     // Should be done automatically
242     [p_sys->o_layer release];
243     [p_sys->autorealease_pool release];
244
245     /* Free the texture buffer*/
246     free( p_sys->pp_buffer[0] );
247     free( p_sys->pp_buffer[1] );
248 }
249
250 /*****************************************************************************
251  * Destroy: destroy GLX video thread output method
252  *****************************************************************************
253  * Terminate an output method created by CreateVout
254  *****************************************************************************/
255 static void DestroyVout( vlc_object_t *p_this )
256 {
257     vout_thread_t *p_vout = (vout_thread_t *)p_this;
258     vout_sys_t *p_sys = p_vout->p_sys;
259     free( p_sys );
260 }
261
262 /*****************************************************************************
263  * Manage: handle Sys events
264  *****************************************************************************
265  * This function should be called regularly by video output thread. It returns
266  * a non null value if an error occurred.
267  *****************************************************************************/
268 static int Manage( vout_thread_t *p_vout )
269 {
270     vout_sys_t *p_sys = p_vout->p_sys;
271
272     return VLC_SUCCESS;
273 }
274
275 /*****************************************************************************
276  * Render: render previously calculated output
277  *****************************************************************************/
278 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
279 {
280     vout_sys_t *p_sys = p_vout->p_sys;
281
282     @synchronized( p_sys->o_layer ) /* Make sure the p_sys->glContext isn't edited */
283     {
284         if( p_sys->glContext )
285         {
286             CGLLockContext(p_sys->glContext);
287             CGLSetCurrentContext(p_sys->glContext);
288             int i_new_index;
289             i_new_index = ( p_sys->i_index + 1 ) & 1;
290
291
292             /* Update the texture */
293             glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_new_index] );
294             glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
295                          p_vout->fmt_out.i_width,
296                          p_vout->fmt_out.i_height,
297                          VLCGL_FORMAT, VLCGL_TYPE, p_sys->pp_buffer[i_new_index] );
298
299             /* Bind to the previous texture for drawing */
300             glBindTexture( VLCGL_TARGET, p_sys->p_textures[p_sys->i_index] );
301
302             /* Switch buffers */
303             p_sys->i_index = i_new_index;
304             p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
305             CGLUnlockContext(p_sys->glContext);
306             
307             p_sys->b_frame_available = true;
308         }
309     }
310
311     /* Give a buffer where the image will be rendered */
312     p_pic->p->p_pixels = p_sys->pp_buffer[p_sys->i_index];
313 }
314
315 /*****************************************************************************
316  * DisplayVideo: displays previously rendered output
317  *****************************************************************************/
318 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
319 {
320     vout_sys_t *p_sys = p_vout->p_sys;
321     
322     [p_sys->o_layer performSelectorOnMainThread:@selector(display)
323                     withObject:nil waitUntilDone:YES];
324 }
325
326 /*****************************************************************************
327  * Control: control facility for the vout
328  *****************************************************************************/
329 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
330 {
331     vout_sys_t *p_sys = p_vout->p_sys;
332
333     if( p_sys->p_vout->pf_control )
334         return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
335     return VLC_EGENERIC;
336 }
337
338 /*****************************************************************************
339  * InitTextures
340  *****************************************************************************/
341 static int InitTextures( vout_thread_t *p_vout )
342 {
343     vout_sys_t *p_sys = p_vout->p_sys;
344     int i_index;
345
346     glDeleteTextures( 2, p_sys->p_textures );
347     glGenTextures( 2, p_sys->p_textures );
348
349     for( i_index = 0; i_index < 2; i_index++ )
350     {
351         glBindTexture( VLCGL_TARGET, p_sys->p_textures[i_index] );
352
353         /* Set the texture parameters */
354         glTexParameterf( VLCGL_TARGET, GL_TEXTURE_PRIORITY, 1.0 );
355
356         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
357         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
358
359         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
360         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
361
362         glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
363
364         /* Note: It seems that we can't bypass those, and even
365          * disabled they are used. They are the cause of the flickering */
366
367         /* Tell the driver not to make a copy of the texture but to use
368            our buffer */
369         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
370         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
371
372         /* Use AGP texturing */
373         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
374
375         /* Call glTexImage2D only once, and use glTexSubImage2D later */
376         glTexImage2D( VLCGL_TARGET, 0, 4, p_sys->i_tex_width,
377                       p_sys->i_tex_height, 0, VLCGL_FORMAT, VLCGL_TYPE,
378                       p_sys->pp_buffer[i_index] );
379     }
380
381     return 0;
382 }
383
384 /*****************************************************************************
385  * @implementation VLCVoutLayer
386  */
387 @implementation VLCVoutLayer
388
389 /*****************************************************************************
390  * autoinitInVout: Called from the video thread to create a layer.
391  * The created layer is stored in the p_vout. We do that way because, cocoa
392  * doesn't support layer creation on non-main thread.
393  *****************************************************************************/
394 + (void)autoinitInVout:(NSValue*)arg
395 {
396     vout_thread_t * p_vout = [arg pointerValue];
397     p_vout->p_sys->o_layer = [[VLCVoutLayer layerWithVout:p_vout] retain];
398     [p_vout->p_sys->o_cocoa_container addVoutLayer:p_vout->p_sys->o_layer];
399 }
400
401 + (id)layerWithVout:(vout_thread_t*)_p_vout 
402 {
403     VLCVoutLayer* me = [[[self alloc] init] autorelease];
404     if( me )
405     {
406         me->p_vout = _p_vout;
407         me.asynchronous = NO;
408         me.bounds = CGRectMake( 0.0, 0.0, 
409                                 (float)_p_vout->fmt_in.i_visible_width * _p_vout->fmt_in.i_sar_num,
410                                 (float)_p_vout->fmt_in.i_visible_height * _p_vout->fmt_in.i_sar_den );
411     }
412     return me;
413 }
414
415 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
416 {
417     /* Only draw the frame if we have a frame that was previously rendered */
418         return p_vout->p_sys->b_frame_available; // Flag is cleared by drawInCGLContext:pixelFormat:forLayerTime:displayTime:
419 }
420
421 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
422 {
423     CGLLockContext( glContext );
424     CGLSetCurrentContext( glContext );
425
426     float f_width, f_height, f_x, f_y;
427
428     f_x = (float)p_vout->fmt_out.i_x_offset;
429     f_y = (float)p_vout->fmt_out.i_y_offset;
430     f_width = (float)p_vout->fmt_out.i_x_offset +
431               (float)p_vout->fmt_out.i_visible_width;
432     f_height = (float)p_vout->fmt_out.i_y_offset +
433                (float)p_vout->fmt_out.i_visible_height;
434
435     glClear( GL_COLOR_BUFFER_BIT );
436
437     glEnable( VLCGL_TARGET );
438     glBegin( GL_POLYGON );
439     glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
440     glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
441     glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
442     glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
443     glEnd();
444
445     glDisable( VLCGL_TARGET );
446
447     glFlush();
448
449     CGLUnlockContext( glContext );
450 }
451
452 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
453 {
454     CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
455
456     CGLLockContext( context );
457
458     CGLSetCurrentContext( context );
459
460     /* Swap buffers only during the vertical retrace of the monitor.
461     http://developer.apple.com/documentation/GraphicsImaging/
462     Conceptual/OpenGL/chap5/chapter_5_section_44.html */
463
464     GLint params = 1;
465     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
466                      &params );
467
468     InitTextures( p_vout );
469
470     glDisable( GL_BLEND );
471     glDisable( GL_DEPTH_TEST );
472     glDepthMask( GL_FALSE );
473     glDisable( GL_CULL_FACE) ;
474     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
475     glClear( GL_COLOR_BUFFER_BIT );
476
477     CGLUnlockContext( context );
478     @synchronized( self )
479     {
480         p_vout->p_sys->glContext = context;
481     }
482
483     return context;
484 }
485
486 - (void)releaseCGLContext:(CGLContextObj)glContext
487 {
488     @synchronized( self )
489     {
490         p_vout->p_sys->glContext = nil;
491     }
492
493     CGLLockContext( glContext );
494     CGLSetCurrentContext( glContext );
495
496     glDeleteTextures( 2, p_vout->p_sys->p_textures );
497
498     CGLUnlockContext( glContext );
499 }
500 @end