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