]> git.sesse.net Git - vlc/blob - modules/gui/macosx/voutgl.m
macosx: fail the video output if Quartz Extreme isn't supported, removed specific...
[vlc] / modules / gui / macosx / voutgl.m
1 /*****************************************************************************
2  * voutgl.m: MacOS X OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2007-2009, 2011 the VideoLAN team
5  * $Id$
6  *
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>
13  *          Damien Fouilleul <damienf at videolan dot org>
14  *          Felix Paul Kuehne <fkuehne at videolan dot org>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>
36
37 #include <vlc_common.h>
38 #include <vlc_keys.h>
39
40 #include "intf.h"
41 #include "vout.h"
42
43 #include <OpenGL/OpenGL.h>
44 #include <OpenGL/gl.h>
45
46 #if 0
47
48 /*****************************************************************************
49  * VLCGLView interface
50  *****************************************************************************/
51 @interface VLCGLView : NSOpenGLView <VLCVoutViewResetting>
52 {
53     vout_thread_t * p_vout;
54 }
55
56 + (void)resetVout: (NSValue *) voutValue;
57 - (id) initWithVout: (vout_thread_t *) p_vout;
58 @end
59
60 struct vout_sys_t
61 {
62     VLCGLView         * o_glview;
63     VLCVoutView       * o_vout_view;
64     bool                b_saved_frame;
65     NSRect              s_frame;
66     bool                b_got_frame;
67
68     bool                b_embedded;
69 };
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74
75 static int  Init   ( vout_thread_t * p_vout );
76 static void End    ( vout_thread_t * p_vout );
77 static int  Manage ( vout_thread_t * p_vout );
78 static int  Control( vout_thread_t *, int, va_list );
79 static void Swap   ( vout_thread_t * p_vout );
80 static int  Lock   ( vout_thread_t * p_vout );
81 static void Unlock ( vout_thread_t * p_vout );
82
83 int OpenVideoGL  ( vlc_object_t * p_this )
84 {
85     vout_thread_t * p_vout = (vout_thread_t *) p_this;
86
87     if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
88     {
89         msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
90                           "Video display might be slow" );
91     }
92     msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
93
94     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
95     if( p_vout->p_sys == NULL )
96         return VLC_ENOMEM;
97
98     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
99
100     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
101
102     p_vout->p_sys->b_embedded = false;
103
104     [VLCGLView performSelectorOnMainThread:@selector(initVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
105
106     [o_pool release];
107
108     /* Check to see if initVout: was successfull */
109     if( !p_vout->p_sys->o_vout_view )
110     {
111         free( p_vout->p_sys );
112         return VLC_EGENERIC;
113     }
114
115     p_vout->pf_init   = Init;
116     p_vout->pf_end    = End;
117     p_vout->pf_manage = Manage;
118     p_vout->pf_control= Control;
119     p_vout->pf_swap   = Swap;
120     p_vout->pf_lock   = Lock;
121     p_vout->pf_unlock = Unlock;
122     p_vout->p_sys->b_got_frame = false;
123
124     return VLC_SUCCESS;
125 }
126
127 void CloseVideoGL ( vlc_object_t * p_this )
128 {
129     vout_thread_t * p_vout = (vout_thread_t *) p_this;
130
131     if(VLCIntf && vlc_object_alive (VLCIntf))
132     {
133         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
134
135         /* Close the window */
136         [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
137
138         [o_pool release];
139     }
140     /* Clean up */
141     free( p_vout->p_sys );
142 }
143
144 static int Init( vout_thread_t * p_vout )
145 {
146     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
147     return VLC_SUCCESS;
148 }
149
150 static void End( vout_thread_t * p_vout )
151 {
152     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
153 }
154
155 static int Manage( vout_thread_t * p_vout )
156 {
157     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
158     {
159         [p_vout->p_sys->o_glview reshape];
160         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
161     }
162     if( p_vout->i_changes & VOUT_CROP_CHANGE )
163     {
164         [p_vout->p_sys->o_glview reshape];
165         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
166     }
167
168     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
169     {
170         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
171
172         p_vout->b_fullscreen = !p_vout->b_fullscreen;
173
174         if( p_vout->b_fullscreen )
175             [p_vout->p_sys->o_vout_view enterFullscreen];
176         else
177             [p_vout->p_sys->o_vout_view leaveFullscreen];
178
179         [o_pool release];
180
181         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
182     }
183
184     if( p_vout->p_sys->o_vout_view )
185         [p_vout->p_sys->o_vout_view manage];
186     return VLC_SUCCESS;
187 }
188
189 /*****************************************************************************
190  * Control: control facility for the vout
191  *****************************************************************************/
192 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
193 {
194     bool b_arg;
195
196     switch( i_query )
197     {
198         case VOUT_SET_STAY_ON_TOP:
199             b_arg = (bool) va_arg( args, int );
200             [p_vout->p_sys->o_vout_view setOnTop: b_arg];
201             return VLC_SUCCESS;
202
203         default:
204             return VLC_EGENERIC;
205     }
206 }
207
208 static void Swap( vout_thread_t * p_vout )
209 {
210     p_vout->p_sys->b_got_frame = true;
211     [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
212 }
213
214 static int Lock( vout_thread_t * p_vout )
215 {
216     if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
217     {
218         [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
219         return 0;
220     }
221     return 1;
222 }
223
224 static void Unlock( vout_thread_t * p_vout )
225 {
226     CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
227 }
228
229 /*****************************************************************************
230  * VLCGLView implementation
231  *****************************************************************************/
232 @implementation VLCGLView
233 + (void)initVout:(NSValue *)arg
234 {
235     vout_thread_t * p_vout = [arg pointerValue];
236
237     /* Create the GL view */
238     p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
239     [p_vout->p_sys->o_glview autorelease];
240
241     /* Spawn the window */
242     id old_vout = p_vout->p_sys->o_vout_view;
243     p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
244                                                 subView: p_vout->p_sys->o_glview frame: nil] retain];
245     [old_vout release];
246 }
247
248 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
249 + (void)resetVout:(NSValue *) voutValue
250 {
251     vout_thread_t * p_vout = [voutValue pointerValue];
252     if( p_vout->b_fullscreen )
253     {
254         /* Save window size and position */
255         p_vout->p_sys->s_frame.size =
256             [p_vout->p_sys->o_vout_view frame].size;
257         p_vout->p_sys->s_frame.origin =
258             [[p_vout->p_sys->o_vout_view voutWindow]frame].origin;
259         p_vout->p_sys->b_saved_frame = true;
260     }
261
262     [p_vout->p_sys->o_vout_view closeVout];
263
264 #define o_glview p_vout->p_sys->o_glview
265     o_glview = [[VLCGLView alloc] initWithVout: p_vout];
266     [o_glview autorelease];
267
268     if( p_vout->p_sys->b_saved_frame )
269     {
270         id old_vout = p_vout->p_sys->o_vout_view;
271         p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
272                                                     subView: o_glview
273                                                       frame: &p_vout->p_sys->s_frame] retain];
274         [old_vout release];
275     }
276     else
277     {
278         id old_vout = p_vout->p_sys->o_vout_view;
279         p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
280                                                     subView: o_glview frame: nil] retain];
281         [old_vout release];
282     }
283 #undef o_glview
284 }
285
286 - (id) initWithVout: (vout_thread_t *) vout
287 {
288     /* Must be called from main thread:
289      * "The NSView class is generally thread-safe, with a few exceptions. You
290      * should create, destroy, resize, move, and perform other operations on NSView
291      * objects only from the main thread of an application. Drawing from secondary
292      * threads is thread-safe as long as you bracket drawing calls with calls to
293      * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
294
295     p_vout = vout;
296
297     NSOpenGLPixelFormatAttribute attribs[] =
298     {
299         NSOpenGLPFADoubleBuffer,
300         NSOpenGLPFAAccelerated,
301         NSOpenGLPFANoRecovery,
302         NSOpenGLPFAColorSize, 24,
303         NSOpenGLPFAAlphaSize, 8,
304         NSOpenGLPFADepthSize, 24,
305         NSOpenGLPFAWindow,
306         0
307     };
308
309     NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
310         initWithAttributes: attribs];
311
312     if( !fmt )
313     {
314         msg_Warn( p_vout, "could not create OpenGL video output" );
315         return nil;
316     }
317
318     self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
319     [fmt release];
320
321     [[self openGLContext] makeCurrentContext];
322     [[self openGLContext] update];
323
324     /* Swap buffers only during the vertical retrace of the monitor.
325        http://developer.apple.com/documentation/GraphicsImaging/
326        Conceptual/OpenGL/chap5/chapter_5_section_44.html */
327     GLint params[] = { 1 };
328     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
329     return self;
330 }
331
332 - (BOOL)mouseDownCanMoveWindow
333 {
334     return YES;
335 }
336
337 - (void) reshape
338 {
339     int x, y;
340
341     Lock( p_vout );
342     NSRect bounds = [self bounds];
343
344     if( var_GetBool( p_vout, "macosx-stretch" ) )
345     {
346         x = bounds.size.width;
347         y = bounds.size.height;
348     }
349     else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
350              p_vout->fmt_in.i_sar_num <
351              bounds.size.width * p_vout->fmt_in.i_visible_height *
352              p_vout->fmt_in.i_sar_den )
353     {
354         x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
355               p_vout->fmt_in.i_sar_num ) /
356             ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
357
358         y = bounds.size.height;
359     }
360     else
361     {
362         x = bounds.size.width;
363         y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
364               p_vout->fmt_in.i_sar_den) /
365             ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num  );
366     }
367
368     glViewport( ( bounds.size.width - x ) / 2,
369                 ( bounds.size.height - y ) / 2, x, y );
370
371     [super reshape];
372
373     if( p_vout->p_sys->b_got_frame )
374     {
375         /* Ask the opengl module to redraw */
376         vout_thread_t * p_parent;
377         p_parent = (vout_thread_t *) p_vout->p_parent;
378         Unlock( p_vout );
379         if( p_parent && p_parent->pf_display )
380         {
381             p_parent->pf_display( p_parent, NULL );
382         }
383     }
384     else
385     {
386         glClear( GL_COLOR_BUFFER_BIT );
387         Unlock( p_vout );
388     }
389 }
390
391 - (void) update
392 {
393     Lock( p_vout );
394     [super update];
395     Unlock( p_vout );
396 }
397
398 - (void) drawRect: (NSRect) rect
399 {
400     Lock( p_vout );
401     [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
402     [super drawRect:rect];
403     Unlock( p_vout );
404 }
405
406 - (void) renewGState
407 {
408     NSWindow *window = [self window];
409
410         if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
411         {
412                 [window disableScreenUpdatesUntilFlush];
413         }
414
415     [super renewGState];
416 }
417
418 @end
419 #endif