1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
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>
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
51 vout_thread_t * p_vout;
54 - (id) initWithVout: (vout_thread_t *) p_vout;
59 NSAutoreleasePool * o_pool;
61 VLCVoutView * o_vout_view;
62 vlc_bool_t b_saved_frame;
64 vlc_bool_t b_got_frame;
66 /* Mozilla plugin-related variables */
67 vlc_bool_t b_embedded;
69 AGLDrawable agl_drawable;
71 int i_width, i_height;
74 /*****************************************************************************
76 *****************************************************************************/
78 static int Init ( vout_thread_t * p_vout );
79 static void End ( vout_thread_t * p_vout );
80 static int Manage ( vout_thread_t * p_vout );
81 static int Control( vout_thread_t *, int, va_list );
82 static void Swap ( vout_thread_t * p_vout );
83 static int Lock ( vout_thread_t * p_vout );
84 static void Unlock ( vout_thread_t * p_vout );
86 static int aglInit ( vout_thread_t * p_vout );
87 static void aglEnd ( vout_thread_t * p_vout );
88 static int aglManage ( vout_thread_t * p_vout );
89 static int aglControl( vout_thread_t *, int, va_list );
90 static void aglSwap ( vout_thread_t * p_vout );
92 int E_(OpenVideoGL) ( vlc_object_t * p_this )
94 vout_thread_t * p_vout = (vout_thread_t *) p_this;
95 vlc_value_t value_drawable;
97 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
99 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
100 "Video display will be slow" );
103 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
105 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106 if( p_vout->p_sys == NULL )
108 msg_Err( p_vout, "out of memory" );
112 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
114 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
116 var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
117 if( value_drawable.i_int != 0 )
119 static const GLint ATTRIBUTES[] = {
132 AGLPixelFormat pixFormat;
134 p_vout->p_sys->b_embedded = VLC_TRUE;
136 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
137 if( NULL == pixFormat )
139 msg_Err( p_vout, "no screen renderer available for required attributes." );
143 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
144 aglDestroyPixelFormat(pixFormat);
145 if( NULL == p_vout->p_sys->agl_ctx )
147 msg_Err( p_vout, "cannot create AGL context." );
151 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
153 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
154 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
157 p_vout->pf_init = aglInit;
158 p_vout->pf_end = aglEnd;
159 p_vout->pf_manage = aglManage;
160 p_vout->pf_control = aglControl;
161 p_vout->pf_swap = aglSwap;
162 p_vout->pf_lock = Lock;
163 p_vout->pf_unlock = Unlock;
167 p_vout->p_sys->b_embedded = VLC_FALSE;
169 p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
171 /* Create the GL view */
172 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
173 [p_vout->p_sys->o_glview autorelease];
175 /* Spawn the window */
177 if( !(p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
178 subView: p_vout->p_sys->o_glview frame: nil]) )
182 p_vout->pf_init = Init;
183 p_vout->pf_end = End;
184 p_vout->pf_manage = Manage;
185 p_vout->pf_control= Control;
186 p_vout->pf_swap = Swap;
187 p_vout->pf_lock = Lock;
188 p_vout->pf_unlock = Unlock;
190 p_vout->p_sys->b_got_frame = VLC_FALSE;
195 void E_(CloseVideoGL) ( vlc_object_t * p_this )
197 vout_thread_t * p_vout = (vout_thread_t *) p_this;
198 if( p_vout->p_sys->b_embedded )
200 aglDestroyContext(p_vout->p_sys->agl_ctx);
204 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
206 /* Close the window */
207 [p_vout->p_sys->o_vout_view closeVout];
212 vlc_mutex_destroy( &p_vout->p_sys->lock );
213 free( p_vout->p_sys );
216 static int Init( vout_thread_t * p_vout )
218 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
222 static void End( vout_thread_t * p_vout )
224 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
227 static int Manage( vout_thread_t * p_vout )
229 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
231 [p_vout->p_sys->o_glview reshape];
232 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
234 if( p_vout->i_changes & VOUT_CROP_CHANGE )
236 [p_vout->p_sys->o_glview reshape];
237 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
240 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
242 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
244 if( !p_vout->b_fullscreen )
246 /* Save window size and position */
247 p_vout->p_sys->s_frame.size =
248 [p_vout->p_sys->o_vout_view frame].size;
249 p_vout->p_sys->s_frame.origin =
250 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
251 p_vout->p_sys->b_saved_frame = VLC_TRUE;
253 [p_vout->p_sys->o_vout_view closeVout];
255 p_vout->b_fullscreen = !p_vout->b_fullscreen;
257 #define o_glview p_vout->p_sys->o_glview
258 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
259 [o_glview autorelease];
261 if( p_vout->p_sys->b_saved_frame )
263 p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
265 frame: &p_vout->p_sys->s_frame];
269 p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
270 subView: o_glview frame: nil];
274 [[o_glview openGLContext] makeCurrentContext];
279 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
282 [p_vout->p_sys->o_vout_view manage];
286 /*****************************************************************************
287 * Control: control facility for the vout
288 *****************************************************************************/
289 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
295 case VOUT_SET_STAY_ON_TOP:
296 b_arg = va_arg( args, vlc_bool_t );
297 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
303 return vout_vaControlDefault( p_vout, i_query, args );
307 static void Swap( vout_thread_t * p_vout )
309 p_vout->p_sys->b_got_frame = VLC_TRUE;
310 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
314 static int Lock( vout_thread_t * p_vout )
316 vlc_mutex_lock( &p_vout->p_sys->lock );
320 static void Unlock( vout_thread_t * p_vout )
322 vlc_mutex_unlock( &p_vout->p_sys->lock );
325 /*****************************************************************************
326 * VLCGLView implementation
327 *****************************************************************************/
328 @implementation VLCGLView
330 - (id) initWithVout: (vout_thread_t *) vout
334 NSOpenGLPixelFormatAttribute attribs[] =
336 NSOpenGLPFAAccelerated,
337 NSOpenGLPFANoRecovery,
338 NSOpenGLPFAColorSize, 24,
339 NSOpenGLPFAAlphaSize, 8,
340 NSOpenGLPFADepthSize, 24,
345 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
346 initWithAttributes: attribs];
350 msg_Warn( p_vout, "could not create OpenGL video output" );
354 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
357 [[self openGLContext] makeCurrentContext];
358 [[self openGLContext] update];
360 /* Swap buffers only during the vertical retrace of the monitor.
361 http://developer.apple.com/documentation/GraphicsImaging/
362 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
363 long params[] = { 1 };
364 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
375 NSRect bounds = [self bounds];
377 [[self openGLContext] makeCurrentContext];
379 var_Get( p_vout, "macosx-stretch", &val );
382 x = bounds.size.width;
383 y = bounds.size.height;
385 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
386 p_vout->fmt_in.i_sar_num <
387 bounds.size.width * p_vout->fmt_in.i_visible_height *
388 p_vout->fmt_in.i_sar_den )
390 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
391 p_vout->fmt_in.i_sar_num ) /
392 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
394 y = bounds.size.height;
398 x = bounds.size.width;
399 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
400 p_vout->fmt_in.i_sar_den) /
401 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
404 glViewport( ( bounds.size.width - x ) / 2,
405 ( bounds.size.height - y ) / 2, x, y );
407 if( p_vout->p_sys->b_got_frame )
409 /* Ask the opengl module to redraw */
410 vout_thread_t * p_parent;
411 p_parent = (vout_thread_t *) p_vout->p_parent;
413 if( p_parent && p_parent->pf_display )
415 p_parent->pf_display( p_parent, NULL );
420 glClear( GL_COLOR_BUFFER_BIT );
433 - (void) drawRect: (NSRect) rect
436 [[self openGLContext] makeCurrentContext];
438 [super drawRect:rect];
444 /*****************************************************************************
445 * embedded AGL context implementation
446 *****************************************************************************/
448 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
449 static void aglReshape( vout_thread_t * p_vout );
451 static int aglInit( vout_thread_t * p_vout )
458 var_Get( p_vout->p_libvlc, "drawable", &val );
459 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
460 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
462 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
463 viewBounds.top = val.i_int;
464 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
465 viewBounds.left = val.i_int;
466 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
467 viewBounds.bottom = val.i_int;
468 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
469 viewBounds.right = val.i_int;
470 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
471 clipBounds.top = val.i_int;
472 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
473 clipBounds.left = val.i_int;
474 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
475 clipBounds.bottom = val.i_int;
476 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
477 clipBounds.right = val.i_int;
479 aglSetViewport(p_vout, viewBounds, clipBounds);
481 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
485 static void aglEnd( vout_thread_t * p_vout )
487 aglSetCurrentContext(NULL);
490 static void aglReshape( vout_thread_t * p_vout )
493 unsigned int i_height = p_vout->p_sys->i_height;
494 unsigned int i_width = p_vout->p_sys->i_width;
498 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
500 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
502 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
504 if( p_vout->p_sys->b_got_frame )
506 /* Ask the opengl module to redraw */
507 vout_thread_t * p_parent;
508 p_parent = (vout_thread_t *) p_vout->p_parent;
510 if( p_parent && p_parent->pf_display )
512 p_parent->pf_display( p_parent, NULL );
517 glClear( GL_COLOR_BUFFER_BIT );
522 static int aglManage( vout_thread_t * p_vout )
524 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
527 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
529 if( p_vout->i_changes & VOUT_CROP_CHANGE )
532 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
537 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
541 case VOUT_SET_VIEWPORT:
543 Rect viewBounds, clipBounds;
544 viewBounds.top = va_arg( args, int);
545 viewBounds.left = va_arg( args, int);
546 viewBounds.bottom = va_arg( args, int);
547 viewBounds.right = va_arg( args, int);
548 clipBounds.top = va_arg( args, int);
549 clipBounds.left = va_arg( args, int);
550 clipBounds.bottom = va_arg( args, int);
551 clipBounds.right = va_arg( args, int);
552 aglSetViewport(p_vout, viewBounds, clipBounds);
558 AGLDrawable drawable = (AGLDrawable)va_arg( args, int);
559 if( drawable != p_vout->p_sys->agl_drawable )
561 p_vout->p_sys->agl_drawable = drawable;
562 aglSetDrawable(p_vout->p_sys->agl_ctx, drawable);
568 return vout_vaControlDefault( p_vout, i_query, args );
572 static void aglSwap( vout_thread_t * p_vout )
574 p_vout->p_sys->b_got_frame = VLC_TRUE;
575 aglSwapBuffers(p_vout->p_sys->agl_ctx);
578 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
580 // mozilla plugin provides coordinates based on port bounds
581 // however AGL coordinates are based on window structure region
582 // and are vertically flipped
584 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
585 Rect winBounds, clientBounds;
587 GetWindowBounds(GetWindowFromPort(port),
588 kWindowStructureRgn, &winBounds);
589 GetWindowBounds(GetWindowFromPort(port),
590 kWindowContentRgn, &clientBounds);
592 /* update video clipping bounds in drawable */
593 rect[0] = (clientBounds.left-winBounds.left)
594 + clipBounds.left; // from window left edge
595 rect[1] = (winBounds.bottom-winBounds.top)
596 - (clientBounds.top-winBounds.top)
597 - clipBounds.bottom; // from window bottom edge
598 rect[2] = clipBounds.right-clipBounds.left; // width
599 rect[3] = clipBounds.bottom-clipBounds.top; // height
600 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
601 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
603 /* update video internal bounds in drawable */
604 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
605 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
606 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
607 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
608 - p_vout->p_sys->i_height;
610 aglUpdateContext(p_vout->p_sys->agl_ctx);
611 aglReshape( p_vout );