1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Eric Petit <titer@m0k.org>
12 * Benjamin Pracht <bigben at videolan dot org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <string.h> /* strerror() */
41 #include <OpenGL/OpenGL.h>
42 #include <OpenGL/gl.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 @interface VLCGLView : NSOpenGLView <VLCVoutViewResetting>
51 vout_thread_t * p_vout;
54 + (void)resetVout: (vout_thread_t *)p_vout;
55 - (id) initWithVout: (vout_thread_t *) p_vout;
60 NSAutoreleasePool * o_pool;
62 VLCVoutView * o_vout_view;
63 vlc_bool_t b_saved_frame;
65 vlc_bool_t b_got_frame;
67 /* Mozilla plugin-related variables */
68 vlc_bool_t b_embedded;
70 AGLDrawable agl_drawable;
72 int i_width, i_height;
75 /*****************************************************************************
77 *****************************************************************************/
79 static int Init ( vout_thread_t * p_vout );
80 static void End ( vout_thread_t * p_vout );
81 static int Manage ( vout_thread_t * p_vout );
82 static int Control( vout_thread_t *, int, va_list );
83 static void Swap ( vout_thread_t * p_vout );
84 static int Lock ( vout_thread_t * p_vout );
85 static void Unlock ( vout_thread_t * p_vout );
87 static int aglInit ( vout_thread_t * p_vout );
88 static void aglEnd ( vout_thread_t * p_vout );
89 static int aglManage ( vout_thread_t * p_vout );
90 static int aglControl( vout_thread_t *, int, va_list );
91 static void aglSwap ( vout_thread_t * p_vout );
93 int E_(OpenVideoGL) ( vlc_object_t * p_this )
95 vout_thread_t * p_vout = (vout_thread_t *) p_this;
96 vlc_value_t value_drawable;
98 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
100 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
101 "Video display will be slow" );
104 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
106 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
107 if( p_vout->p_sys == NULL )
109 msg_Err( p_vout, "out of memory" );
113 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
115 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
117 var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
118 if( value_drawable.i_int != 0 )
120 static const GLint ATTRIBUTES[] = {
133 AGLPixelFormat pixFormat;
135 p_vout->p_sys->b_embedded = VLC_TRUE;
137 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
138 if( NULL == pixFormat )
140 msg_Err( p_vout, "no screen renderer available for required attributes." );
144 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
145 aglDestroyPixelFormat(pixFormat);
146 if( NULL == p_vout->p_sys->agl_ctx )
148 msg_Err( p_vout, "cannot create AGL context." );
152 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
154 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
155 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
158 p_vout->pf_init = aglInit;
159 p_vout->pf_end = aglEnd;
160 p_vout->pf_manage = aglManage;
161 p_vout->pf_control = aglControl;
162 p_vout->pf_swap = aglSwap;
163 p_vout->pf_lock = Lock;
164 p_vout->pf_unlock = Unlock;
168 p_vout->p_sys->b_embedded = VLC_FALSE;
170 p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
172 /* Create the GL view */
173 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
174 [p_vout->p_sys->o_glview autorelease];
176 /* Spawn the window */
178 if( !(p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
179 subView: p_vout->p_sys->o_glview frame: nil]) )
183 p_vout->pf_init = Init;
184 p_vout->pf_end = End;
185 p_vout->pf_manage = Manage;
186 p_vout->pf_control= Control;
187 p_vout->pf_swap = Swap;
188 p_vout->pf_lock = Lock;
189 p_vout->pf_unlock = Unlock;
191 p_vout->p_sys->b_got_frame = VLC_FALSE;
196 void E_(CloseVideoGL) ( vlc_object_t * p_this )
198 vout_thread_t * p_vout = (vout_thread_t *) p_this;
199 if( p_vout->p_sys->b_embedded )
201 aglDestroyContext(p_vout->p_sys->agl_ctx);
203 else if(!VLCIntf->b_die)
205 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
207 /* Close the window */
208 [p_vout->p_sys->o_vout_view closeVout];
213 vlc_mutex_destroy( &p_vout->p_sys->lock );
214 free( p_vout->p_sys );
217 static int Init( vout_thread_t * p_vout )
219 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
223 static void End( vout_thread_t * p_vout )
225 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
228 static int Manage( vout_thread_t * p_vout )
230 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
232 [p_vout->p_sys->o_glview reshape];
233 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
235 if( p_vout->i_changes & VOUT_CROP_CHANGE )
237 [p_vout->p_sys->o_glview reshape];
238 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
241 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
243 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
245 p_vout->b_fullscreen = !p_vout->b_fullscreen;
247 if( p_vout->b_fullscreen )
248 [p_vout->p_sys->o_vout_view enterFullscreen];
250 [p_vout->p_sys->o_vout_view leaveFullscreen];
254 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
257 [p_vout->p_sys->o_vout_view manage];
261 /*****************************************************************************
262 * Control: control facility for the vout
263 *****************************************************************************/
264 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
270 case VOUT_SET_STAY_ON_TOP:
271 b_arg = va_arg( args, vlc_bool_t );
272 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
278 return vout_vaControlDefault( p_vout, i_query, args );
282 static void Swap( vout_thread_t * p_vout )
284 p_vout->p_sys->b_got_frame = VLC_TRUE;
285 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
289 static int Lock( vout_thread_t * p_vout )
291 vlc_mutex_lock( &p_vout->p_sys->lock );
292 CGLLockContext( (CGLContextObj)p_vout->p_sys->agl_ctx );
296 static void Unlock( vout_thread_t * p_vout )
298 CGLUnlockContext( (CGLContextObj)p_vout->p_sys->agl_ctx );
299 vlc_mutex_unlock( &p_vout->p_sys->lock );
302 /*****************************************************************************
303 * VLCGLView implementation
304 *****************************************************************************/
305 @implementation VLCGLView
307 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
308 + (void)resetVout: (vout_thread_t *)p_vout
310 if( p_vout->b_fullscreen )
312 /* Save window size and position */
313 p_vout->p_sys->s_frame.size =
314 [p_vout->p_sys->o_vout_view frame].size;
315 p_vout->p_sys->s_frame.origin =
316 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
317 p_vout->p_sys->b_saved_frame = VLC_TRUE;
320 [p_vout->p_sys->o_vout_view closeVout];
322 #define o_glview p_vout->p_sys->o_glview
323 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
324 [o_glview autorelease];
326 if( p_vout->p_sys->b_saved_frame )
328 p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
330 frame: &p_vout->p_sys->s_frame];
334 p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
335 subView: o_glview frame: nil];
339 [[o_glview openGLContext] makeCurrentContext];
343 - (id) initWithVout: (vout_thread_t *) vout
347 NSOpenGLPixelFormatAttribute attribs[] =
349 NSOpenGLPFAAccelerated,
350 NSOpenGLPFANoRecovery,
351 NSOpenGLPFAColorSize, 24,
352 NSOpenGLPFAAlphaSize, 8,
353 NSOpenGLPFADepthSize, 24,
358 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
359 initWithAttributes: attribs];
363 msg_Warn( p_vout, "could not create OpenGL video output" );
367 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
370 [[self openGLContext] makeCurrentContext];
371 [[self openGLContext] update];
373 /* Swap buffers only during the vertical retrace of the monitor.
374 http://developer.apple.com/documentation/GraphicsImaging/
375 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
376 long params[] = { 1 };
377 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
388 NSRect bounds = [self bounds];
390 [[self openGLContext] makeCurrentContext];
392 var_Get( p_vout, "macosx-stretch", &val );
395 x = bounds.size.width;
396 y = bounds.size.height;
398 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
399 p_vout->fmt_in.i_sar_num <
400 bounds.size.width * p_vout->fmt_in.i_visible_height *
401 p_vout->fmt_in.i_sar_den )
403 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
404 p_vout->fmt_in.i_sar_num ) /
405 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
407 y = bounds.size.height;
411 x = bounds.size.width;
412 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
413 p_vout->fmt_in.i_sar_den) /
414 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
417 glViewport( ( bounds.size.width - x ) / 2,
418 ( bounds.size.height - y ) / 2, x, y );
420 if( p_vout->p_sys->b_got_frame )
422 /* Ask the opengl module to redraw */
423 vout_thread_t * p_parent;
424 p_parent = (vout_thread_t *) p_vout->p_parent;
426 if( p_parent && p_parent->pf_display )
428 p_parent->pf_display( p_parent, NULL );
433 glClear( GL_COLOR_BUFFER_BIT );
446 - (void) drawRect: (NSRect) rect
449 [[self openGLContext] makeCurrentContext];
450 /* FIXME: we should use CGLFlushDrawable instead, as glFlush is pretty slow */
452 [super drawRect:rect];
458 /*****************************************************************************
459 * embedded AGL context implementation
460 *****************************************************************************/
462 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
463 static void aglReshape( vout_thread_t * p_vout );
465 static int aglInit( vout_thread_t * p_vout )
472 var_Get( p_vout->p_libvlc, "drawable", &val );
473 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
474 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
476 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
477 viewBounds.top = val.i_int;
478 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
479 viewBounds.left = val.i_int;
480 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
481 viewBounds.bottom = val.i_int;
482 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
483 viewBounds.right = val.i_int;
484 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
485 clipBounds.top = val.i_int;
486 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
487 clipBounds.left = val.i_int;
488 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
489 clipBounds.bottom = val.i_int;
490 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
491 clipBounds.right = val.i_int;
493 aglSetViewport(p_vout, viewBounds, clipBounds);
495 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
499 static void aglEnd( vout_thread_t * p_vout )
501 aglSetCurrentContext(NULL);
504 static void aglReshape( vout_thread_t * p_vout )
507 unsigned int i_height = p_vout->p_sys->i_height;
508 unsigned int i_width = p_vout->p_sys->i_width;
512 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
514 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
516 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
518 if( p_vout->p_sys->b_got_frame )
520 /* Ask the opengl module to redraw */
521 vout_thread_t * p_parent;
522 p_parent = (vout_thread_t *) p_vout->p_parent;
524 if( p_parent && p_parent->pf_display )
526 p_parent->pf_display( p_parent, NULL );
531 glClear( GL_COLOR_BUFFER_BIT );
536 static int aglManage( vout_thread_t * p_vout )
538 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
541 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
543 if( p_vout->i_changes & VOUT_CROP_CHANGE )
546 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
551 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
555 case VOUT_SET_VIEWPORT:
557 Rect viewBounds, clipBounds;
558 viewBounds.top = va_arg( args, int);
559 viewBounds.left = va_arg( args, int);
560 viewBounds.bottom = va_arg( args, int);
561 viewBounds.right = va_arg( args, int);
562 clipBounds.top = va_arg( args, int);
563 clipBounds.left = va_arg( args, int);
564 clipBounds.bottom = va_arg( args, int);
565 clipBounds.right = va_arg( args, int);
566 aglSetViewport(p_vout, viewBounds, clipBounds);
572 AGLDrawable drawable = (AGLDrawable)va_arg( args, int);
573 if( drawable != p_vout->p_sys->agl_drawable )
575 p_vout->p_sys->agl_drawable = drawable;
576 aglSetDrawable(p_vout->p_sys->agl_ctx, drawable);
582 return vout_vaControlDefault( p_vout, i_query, args );
586 static void aglSwap( vout_thread_t * p_vout )
588 p_vout->p_sys->b_got_frame = VLC_TRUE;
589 aglSwapBuffers(p_vout->p_sys->agl_ctx);
592 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
594 // mozilla plugin provides coordinates based on port bounds
595 // however AGL coordinates are based on window structure region
596 // and are vertically flipped
598 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
599 Rect winBounds, clientBounds;
601 GetWindowBounds(GetWindowFromPort(port),
602 kWindowStructureRgn, &winBounds);
603 GetWindowBounds(GetWindowFromPort(port),
604 kWindowContentRgn, &clientBounds);
606 /* update video clipping bounds in drawable */
607 rect[0] = (clientBounds.left-winBounds.left)
608 + clipBounds.left; // from window left edge
609 rect[1] = (winBounds.bottom-winBounds.top)
610 - (clientBounds.top-winBounds.top)
611 - clipBounds.bottom; // from window bottom edge
612 rect[2] = clipBounds.right-clipBounds.left; // width
613 rect[3] = clipBounds.bottom-clipBounds.top; // height
614 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
615 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
617 /* update video internal bounds in drawable */
618 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
619 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
620 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
621 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
622 - p_vout->p_sys->i_height;
624 aglUpdateContext(p_vout->p_sys->agl_ctx);
625 aglReshape( p_vout );