042b1ee9e607561eb16771711188dae7b5f10ccb
[vlc.git] / 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 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 #include <vlc/vlc.h>
36 #include <vlc_vout.h>
37
38 #import <QuartzCore/QuartzCore.h>
39 #import <Cocoa/Cocoa.h>
40 #import <OpenGL/OpenGL.h>
41
42 /* On OS X, use GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D.
43    This allows sizes which are not powers of 2 */
44 #define VLCGL_TARGET GL_TEXTURE_RECTANGLE_EXT
45
46 /* OS X OpenGL supports YUV. Hehe. */
47 #define VLCGL_FORMAT GL_YCBCR_422_APPLE
48 #define VLCGL_TYPE   GL_UNSIGNED_SHORT_8_8_APPLE
49
50 /* RV32 */
51 #define VLCGL_RGB_FORMAT GL_RGBA
52 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
53
54 /* YUY2 */
55 #ifndef YCBCR_MESA
56 #define YCBCR_MESA 0x8757
57 #endif
58 #ifndef UNSIGNED_SHORT_8_8_MESA
59 #define UNSIGNED_SHORT_8_8_MESA 0x85BA
60 #endif
61 #define VLCGL_YUV_FORMAT YCBCR_MESA
62 #define VLCGL_YUV_TYPE UNSIGNED_SHORT_8_8_MESA
63
64
65 #ifndef GL_CLAMP_TO_EDGE
66 #   define GL_CLAMP_TO_EDGE 0x812F
67 #endif
68
69 @interface VLCVideoView : NSObject
70 - (void)addVoutLayer:(CALayer *)layer;
71 @end
72
73 /*****************************************************************************
74  * Vout interface
75  *****************************************************************************/
76 static int  CreateVout   ( vlc_object_t * );
77 static void DestroyVout  ( vlc_object_t * );
78 static int  Init         ( vout_thread_t * );
79 static void End          ( vout_thread_t * );
80 static int  Manage       ( vout_thread_t * );
81 static void Render       ( vout_thread_t *, picture_t * );
82 static void DisplayVideo ( vout_thread_t *, picture_t * );
83 static int  Control      ( vout_thread_t *, int, va_list );
84
85 static int InitTextures  ( vout_thread_t * );
86
87 vlc_module_begin();
88     set_shortname( "OpenGLLayer" );
89     set_category( CAT_VIDEO );
90     set_subcategory( SUBCAT_VIDEO_VOUT );
91     set_description( _("Core Animation OpenGL Layer (Mac OS X)") );
92     set_capability( "video output", 20 );
93     add_shortcut( "opengllayer" );
94     set_callbacks( CreateVout, DestroyVout );
95 vlc_module_end();
96
97 @interface VLCVoutLayer : CAOpenGLLayer {
98     vout_thread_t * p_vout;
99 }
100 + (id)layerWithVout:(vout_thread_t*)_p_vout; 
101 @end
102
103 /*****************************************************************************
104  * vout_sys_t: video output method descriptor
105  *****************************************************************************
106  * This structure is part of the video output thread descriptor.
107  * It describes the OpenGL specific properties of the output thread.
108  *****************************************************************************/
109 struct vout_sys_t
110 {
111     vout_thread_t * p_vout;
112
113     uint8_t    *pp_buffer[2]; /* one last rendered, one to be rendered */
114     int         i_index;
115     vlc_bool_t  b_frame_available;
116
117     int         i_tex_width;
118     int         i_tex_height;
119     GLuint      p_textures[2];
120
121     NSAutoreleasePool *autorealease_pool;
122     VLCVoutLayer * o_layer;
123     id          o_cocoa_container;
124 };
125
126 /*****************************************************************************
127  * CreateVout: This function allocates and initializes the OpenGL vout method.
128  *****************************************************************************/
129 static int CreateVout( vlc_object_t *p_this )
130 {
131     vout_thread_t *p_vout = (vout_thread_t *)p_this;
132     vout_sys_t *p_sys;
133     char * psz;
134
135     /* Allocate structure */
136     p_vout->p_sys = p_sys = calloc( sizeof( vout_sys_t ), 1 );
137     if( p_sys == NULL )
138     {
139         msg_Err( p_vout, "out of memory" );
140         return VLC_EGENERIC;
141     }
142
143     p_sys->i_tex_width  = p_vout->fmt_in.i_width;
144     p_sys->i_tex_height = p_vout->fmt_in.i_height;
145
146     msg_Dbg( p_vout, "Texture size: %dx%d", p_sys->i_tex_width,
147              p_sys->i_tex_height );
148
149
150     p_vout->pf_init = Init;
151     p_vout->pf_end = End;
152     p_vout->pf_manage = Manage;
153     p_vout->pf_render = Render;
154     p_vout->pf_display = DisplayVideo;
155     p_vout->pf_control = Control;
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * Init: initialize the OpenGL video thread output method
162  *****************************************************************************/
163 static int Init( vout_thread_t *p_vout )
164 {
165     vout_sys_t *p_sys = p_vout->p_sys;
166     int i_pixel_pitch;
167     vlc_value_t val;
168
169 #if ( defined( WORDS_BIGENDIAN ) && VLCGL_FORMAT == GL_YCBCR_422_APPLE ) || (VLCGL_FORMAT == YCBCR_MESA)
170     p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
171     i_pixel_pitch = 2;
172 #elif (VLCGL_FORMAT == GL_YCBCR_422_APPLE)
173     p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
174     i_pixel_pitch = 2;
175 #endif
176
177     /* Since OpenGL can do rescaling for us, stick to the default
178      * coordinates and aspect. */
179     p_vout->output.i_width  = p_vout->render.i_width;
180     p_vout->output.i_height = p_vout->render.i_height;
181     p_vout->output.i_aspect = p_vout->render.i_aspect;
182
183     /* We do need a drawable to work properly */
184     vlc_value_t value_drawable;
185     var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
186     var_Get( p_vout, "drawable", &value_drawable );
187
188     p_vout->p_sys->o_cocoa_container = (id) value_drawable.i_int;
189     
190     p_vout->fmt_out = p_vout->fmt_in;
191     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
192
193     /* We know the chroma, allocate two buffer which will be used
194      * directly by the decoder */
195     int i;
196     for( i = 0; i < 2; i++ )
197     {
198         p_sys->pp_buffer[i] =
199             malloc( p_sys->i_tex_width * p_sys->i_tex_height * i_pixel_pitch );
200         if( !p_sys->pp_buffer[i] )
201         {
202             msg_Err( p_vout, "out of memory" );
203             return VLC_EGENERIC;
204         }
205     }
206     p_sys->b_frame_available = VLC_FALSE;
207     p_sys->i_index = 0;
208     
209     p_vout->p_picture[0].i_planes = 1;
210     p_vout->p_picture[0].p->p_pixels = p_sys->pp_buffer[0];
211     p_vout->p_picture[0].p->i_lines = p_vout->output.i_height;
212     p_vout->p_picture[0].p->i_visible_lines = p_vout->output.i_height;
213     p_vout->p_picture[0].p->i_pixel_pitch = i_pixel_pitch;
214     p_vout->p_picture[0].p->i_pitch = p_vout->output.i_width *
215         p_vout->p_picture[0].p->i_pixel_pitch;
216     p_vout->p_picture[0].p->i_visible_pitch = p_vout->output.i_width *
217         p_vout->p_picture[0].p->i_pixel_pitch;
218
219     p_vout->p_picture[0].i_status = DESTROYED_PICTURE;
220     p_vout->p_picture[0].i_type   = DIRECT_PICTURE;
221
222     PP_OUTPUTPICTURE[ 0 ] = &p_vout->p_picture[0];
223
224     I_OUTPUTPICTURES = 1;
225     p_sys->autorealease_pool = [[NSAutoreleasePool alloc] init];
226
227     [VLCVoutLayer performSelectorOnMainThread:@selector(autoinitInVout:)
228                              withObject:[NSValue valueWithPointer:p_vout]
229                              waitUntilDone:YES];
230
231     return 0;
232 }
233
234 /*****************************************************************************
235  * End: terminate GLX video thread output method
236  *****************************************************************************/
237 static void End( vout_thread_t *p_vout )
238 {
239     vout_sys_t *p_sys = p_vout->p_sys;
240
241     p_vout->p_sys->b_frame_available = 0;
242
243     [CATransaction performSelectorOnMainThread:@selector(begin)
244                     withObject:nil waitUntilDone:YES];
245
246     [p_sys->o_layer performSelectorOnMainThread:@selector(removeFromSuperlayer)
247                     withObject:nil waitUntilDone:YES];
248     [CATransaction performSelectorOnMainThread:@selector(commit)
249                     withObject:nil waitUntilDone:YES];
250
251     // Should be done automatically
252     [p_sys->o_layer release];
253     [p_sys->autorealease_pool release];
254
255     /* Free the texture buffer*/
256     if( p_sys->pp_buffer[0] ) free( p_sys->pp_buffer[0] );
257     if( p_sys->pp_buffer[1] ) free( p_sys->pp_buffer[1] );
258 }
259
260 /*****************************************************************************
261  * Destroy: destroy GLX video thread output method
262  *****************************************************************************
263  * Terminate an output method created by CreateVout
264  *****************************************************************************/
265 static void DestroyVout( vlc_object_t *p_this )
266 {
267     vout_thread_t *p_vout = (vout_thread_t *)p_this;
268     vout_sys_t *p_sys = p_vout->p_sys;
269     free( p_sys );
270 }
271
272 /*****************************************************************************
273  * Manage: handle Sys events
274  *****************************************************************************
275  * This function should be called regularly by video output thread. It returns
276  * a non null value if an error occurred.
277  *****************************************************************************/
278 static int Manage( vout_thread_t *p_vout )
279 {    
280     return VLC_SUCCESS;
281 }
282
283 /*****************************************************************************
284  * Render: render previously calculated output
285  *****************************************************************************/
286 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
287 {
288     vout_sys_t *p_sys = p_vout->p_sys;
289
290     /* Switch buffers */
291     int p_new_index;
292     @synchronized(p_sys->o_layer)
293     {
294         p_new_index = (p_sys->i_index + 1) & 1;
295     }
296
297     /* Give a buffer where the image will be rendered */
298     p_pic->p->p_pixels = p_sys->pp_buffer[p_new_index];
299
300 }
301
302 /*****************************************************************************
303  * DisplayVideo: displays previously rendered output
304  *****************************************************************************/
305 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
306 {
307     vout_sys_t *p_sys = p_vout->p_sys;
308
309     /* The frame is ready, give its number so the o_layer can display it */
310     @synchronized(p_sys->o_layer)
311     {
312         p_sys->i_index = (p_sys->i_index + 1) & 1; /* Indicate the layer should use that index */
313     }
314
315     p_sys->b_frame_available = 1;
316 }
317
318 /*****************************************************************************
319  * Control: control facility for the vout
320  *****************************************************************************/
321 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
322 {
323     vout_sys_t *p_sys = p_vout->p_sys;
324
325     switch( i_query )
326     {
327     case VOUT_SNAPSHOT:
328         return vout_vaControlDefault( p_vout, i_query, args );
329
330     default:
331         if( p_sys->p_vout->pf_control )
332             return p_sys->p_vout->pf_control( p_sys->p_vout, i_query, args );
333         else
334             return vout_vaControlDefault( p_vout, i_query, args );
335     }
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 #ifdef __DISABLED_FOR_NOW__
365         /* Tell the driver not to make a copy of the texture but to use
366            our buffer */
367         glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
368         glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
369
370         /* Use AGP texturing */
371         glTexParameteri( VLCGL_TARGET, GL_TEXTURE_STORAGE_HINT_APPLE,
372                          GL_STORAGE_SHARED_APPLE );
373 #endif
374
375         /* Call glTexImage2D only once, and use glTexSubImage2D later */
376         glTexImage2D( VLCGL_TARGET, 0, 3, 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 - (void)setVout:(vout_thread_t*)_p_vout 
402 {
403     p_vout = _p_vout;
404 }
405
406 + (id)layerWithVout:(vout_thread_t*)_p_vout 
407 {
408     VLCVoutLayer* me = [super layer];
409     if( me )
410     {
411         me.asynchronous = YES;
412         [me setVout: _p_vout];
413         me.bounds = CGRectMake( 0.0, 0.0, 
414                                 (float)_p_vout->fmt_in.i_visible_width * _p_vout->fmt_in.i_sar_num,
415                                 (float)_p_vout->fmt_in.i_visible_height * _p_vout->fmt_in.i_sar_den );
416     }
417     return me;
418 }
419
420 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
421 {
422     /* Only draw the frame when if have a frame that was previously rendered */
423     BOOL ret = p_vout->p_sys->b_frame_available;
424     p_vout->p_sys->b_frame_available = VLC_FALSE;
425     return ret;
426 }
427
428 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
429 {
430     /* Init */
431     CGLLockContext( glContext );
432     CGLSetCurrentContext( glContext );
433
434     float f_width, f_height, f_x, f_y;
435
436     f_x = (float)p_vout->fmt_out.i_x_offset;
437     f_y = (float)p_vout->fmt_out.i_y_offset;
438     f_width = (float)p_vout->fmt_out.i_x_offset +
439               (float)p_vout->fmt_out.i_visible_width;
440     f_height = (float)p_vout->fmt_out.i_y_offset +
441                (float)p_vout->fmt_out.i_visible_height;
442
443     @synchronized(self)
444     {
445         glBindTexture( VLCGL_TARGET, p_vout->p_sys->p_textures[p_vout->p_sys->i_index] );
446
447         glTexSubImage2D( VLCGL_TARGET, 0, 0, 0,
448                      p_vout->fmt_out.i_width,
449                      p_vout->fmt_out.i_height,
450                      VLCGL_FORMAT, VLCGL_TYPE, p_vout->p_sys->pp_buffer[p_vout->p_sys->i_index] );
451
452
453         glClear( GL_COLOR_BUFFER_BIT );
454
455         glEnable( VLCGL_TARGET );
456         glBegin( GL_POLYGON );
457         glTexCoord2f( f_x, f_y ); glVertex2f( -1.0, 1.0 );
458         glTexCoord2f( f_width, f_y ); glVertex2f( 1.0, 1.0 );
459         glTexCoord2f( f_width, f_height ); glVertex2f( 1.0, -1.0 );
460         glTexCoord2f( f_x, f_height ); glVertex2f( -1.0, -1.0 );
461         glEnd();
462
463         glDisable( VLCGL_TARGET );
464
465         glFlush();
466     }
467
468     CGLUnlockContext( glContext );
469     p_vout->p_sys->b_frame_available = VLC_FALSE;
470 }
471
472 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
473 {
474         GLuint attribs[] = 
475         {
476                 NSOpenGLPFANoRecovery,
477                 NSOpenGLPFAWindow,
478                 NSOpenGLPFAAccelerated,
479                 NSOpenGLPFADoubleBuffer,
480                 NSOpenGLPFAColorSize, 24,
481                 NSOpenGLPFAAlphaSize, 8,
482                 NSOpenGLPFADepthSize, 24,
483                 NSOpenGLPFAStencilSize, 8,
484                 NSOpenGLPFAAccumSize, 0,
485                 0
486         };
487
488         NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: (NSOpenGLPixelFormatAttribute*) attribs]; 
489     
490     return [super copyCGLPixelFormatForDisplayMask:mask];//[fmt CGLPixelFormatObj];
491 }
492
493 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
494 {
495     CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
496
497
498     /* Swap buffers only during the vertical retrace of the monitor.
499     http://developer.apple.com/documentation/GraphicsImaging/
500     Conceptual/OpenGL/chap5/chapter_5_section_44.html */
501
502     GLint params = 1;
503     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
504                     &params );
505     
506     /* Make sure our texture will gets drawn at the right resolution */
507     GLint dim[2] = { p_vout->p_sys->i_tex_width, p_vout->p_sys->i_tex_height};
508     NSLog(@"asking for %dx%d", p_vout->p_sys->i_tex_width, p_vout->p_sys->i_tex_height);
509     CGLSetParameter(context, kCGLCPSurfaceBackingSize, dim);
510     CGLEnable (context, kCGLCESurfaceBackingSize);
511
512     CGLSetCurrentContext( context );
513     InitTextures( p_vout );
514
515     glDisable(GL_BLEND);
516     glDisable(GL_DEPTH_TEST);
517     glDepthMask(GL_FALSE);
518     glDisable(GL_CULL_FACE);
519     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
520     glClear( GL_COLOR_BUFFER_BIT );
521
522     return context;
523 }
524
525 - (void)releaseCGLContext:(CGLContextObj)glContext
526 {
527     CGLLockContext( glContext );
528     CGLSetCurrentContext( glContext );
529
530     glDeleteTextures( 2, p_vout->p_sys->p_textures );
531
532     CGLUnlockContext( glContext );
533 }
534
535 @end