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