1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN (Centrale Réseaux) and its contributors
5 * $Id: vout.m 8351 2004-08-02 13:06:38Z hartman $
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>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* strerror() */
40 #include <OpenGL/OpenGL.h>
41 #include <OpenGL/gl.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 @interface VLCGLView : NSOpenGLView
48 vout_thread_t * p_vout;
51 - (id) initWithVout: (vout_thread_t *) p_vout;
56 NSAutoreleasePool * o_pool;
59 vlc_bool_t b_saved_frame;
61 vlc_bool_t b_got_frame;
65 /*****************************************************************************
67 *****************************************************************************/
69 static int Init ( vout_thread_t * p_vout );
70 static void End ( vout_thread_t * p_vout );
71 static int Manage ( vout_thread_t * p_vout );
72 static int Control( vout_thread_t *, int, va_list );
73 static void Swap ( vout_thread_t * p_vout );
74 static int Lock ( vout_thread_t * p_vout );
75 static void Unlock ( vout_thread_t * p_vout );
77 int E_(OpenVideoGL) ( vlc_object_t * p_this )
79 vout_thread_t * p_vout = (vout_thread_t *) p_this;
81 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
83 msg_Warn( p_vout, "no hardware acceleration" );
86 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
88 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
89 if( p_vout->p_sys == NULL )
91 msg_Err( p_vout, "out of memory" );
95 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
97 p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
98 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
100 /* Create the GL view */
101 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
102 [p_vout->p_sys->o_glview autorelease];
104 /* Spawn the window */
105 p_vout->p_sys->b_got_frame = VLC_FALSE;
106 p_vout->p_sys->o_window = [[VLCWindow alloc] initWithVout: p_vout
107 view: p_vout->p_sys->o_glview frame: nil];
108 if( !p_vout->p_sys->o_window )
113 p_vout->pf_init = Init;
114 p_vout->pf_end = End;
115 p_vout->pf_manage = Manage;
116 p_vout->pf_control= Control;
117 p_vout->pf_swap = Swap;
118 p_vout->pf_lock = Lock;
119 p_vout->pf_unlock = Unlock;
124 void E_(CloseVideoGL) ( vlc_object_t * p_this )
126 vout_thread_t * p_vout = (vout_thread_t *) p_this;
127 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
129 /* Close the window */
130 [p_vout->p_sys->o_window close];
133 vlc_mutex_destroy( &p_vout->p_sys->lock );
135 free( p_vout->p_sys );
138 static int Init( vout_thread_t * p_vout )
140 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
144 static void End( vout_thread_t * p_vout )
146 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
149 static int Manage( vout_thread_t * p_vout )
151 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
153 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
155 if( !p_vout->b_fullscreen )
157 /* Save window size and position */
158 p_vout->p_sys->s_frame.size =
159 [[p_vout->p_sys->o_window contentView] frame].size;
160 p_vout->p_sys->s_frame.origin =
161 [p_vout->p_sys->o_window frame].origin;
162 p_vout->p_sys->b_saved_frame = VLC_TRUE;
164 [p_vout->p_sys->o_window close];
166 p_vout->b_fullscreen = !p_vout->b_fullscreen;
168 #define o_glview p_vout->p_sys->o_glview
169 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
170 [o_glview autorelease];
172 if( p_vout->p_sys->b_saved_frame )
174 p_vout->p_sys->o_window = [[VLCWindow alloc]
175 initWithVout: p_vout view: o_glview
176 frame: &p_vout->p_sys->s_frame];
180 p_vout->p_sys->o_window = [[VLCWindow alloc]
181 initWithVout: p_vout view: o_glview frame: nil];
184 [[o_glview openGLContext] makeCurrentContext];
189 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
191 [p_vout->p_sys->o_window manage];
195 /*****************************************************************************
196 * Control: control facility for the vout
197 *****************************************************************************/
198 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
204 case VOUT_SET_STAY_ON_TOP:
205 b_arg = va_arg( args, vlc_bool_t );
206 [p_vout->p_sys->o_window setOnTop: b_arg];
212 return vout_vaControlDefault( p_vout, i_query, args );
216 static void Swap( vout_thread_t * p_vout )
218 p_vout->p_sys->b_got_frame = VLC_TRUE;
219 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
223 static int Lock( vout_thread_t * p_vout )
225 vlc_mutex_lock( &p_vout->p_sys->lock );
229 static void Unlock( vout_thread_t * p_vout )
231 vlc_mutex_unlock( &p_vout->p_sys->lock );
234 /*****************************************************************************
235 * VLCGLView implementation
236 *****************************************************************************/
237 @implementation VLCGLView
239 - (id) initWithVout: (vout_thread_t *) vout
243 NSOpenGLPixelFormatAttribute attribs[] =
245 NSOpenGLPFAAccelerated,
246 NSOpenGLPFANoRecovery,
247 NSOpenGLPFAColorSize, 24,
248 NSOpenGLPFAAlphaSize, 8,
249 NSOpenGLPFADepthSize, 24,
254 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
255 initWithAttributes: attribs];
259 msg_Warn( p_vout, "Cannot create NSOpenGLPixelFormat" );
263 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
266 [[self openGLContext] makeCurrentContext];
267 [[self openGLContext] update];
269 /* Swap buffers only during the vertical retrace of the monitor.
270 http://developer.apple.com/documentation/GraphicsImaging/
271 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
272 long params[] = { 1 };
273 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
284 NSRect bounds = [self bounds];
286 [[self openGLContext] makeCurrentContext];
288 var_Get( p_vout, "macosx-stretch", &val );
291 x = bounds.size.width;
292 y = bounds.size.height;
294 else if( bounds.size.height * p_vout->render.i_aspect <
295 bounds.size.width * VOUT_ASPECT_FACTOR )
297 x = bounds.size.height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
298 y = bounds.size.height;
302 x = bounds.size.width;
303 y = bounds.size.width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
306 glViewport( ( bounds.size.width - x ) / 2,
307 ( bounds.size.height - y ) / 2, x, y );
309 if( p_vout->p_sys->b_got_frame )
311 /* Ask the opengl module to redraw */
312 vout_thread_t * p_parent;
313 p_parent = (vout_thread_t *) p_vout->p_parent;
315 if( p_parent && p_parent->pf_display )
317 p_parent->pf_display( p_parent, NULL );
322 glClear( GL_COLOR_BUFFER_BIT );
335 - (void) drawRect: (NSRect) rect
338 [[self openGLContext] makeCurrentContext];
340 [super drawRect:rect];