]> git.sesse.net Git - vlc/blob - modules/gui/macosx/voutgl.m
opengl.c, voutgl.m: restored smooth resizing on OS X
[vlc] / modules / gui / macosx / voutgl.m
1 /*****************************************************************************
2  * vout.m: MacOS X video output module
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: vout.m 8351 2004-08-02 13:06:38Z hartman $
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  *
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.
17  * 
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.
22  *
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  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                                /* free() */
33 #include <string.h>                                            /* strerror() */
34
35 #include <vlc_keys.h>
36
37 #include "intf.h"
38 #include "vout.h"
39
40 #include <OpenGL/OpenGL.h>
41 #include <OpenGL/gl.h>
42
43 /*****************************************************************************
44  * VLCView interface
45  *****************************************************************************/
46 @interface VLCGLView : NSOpenGLView
47 {
48     vout_thread_t * p_vout;
49 }
50
51 - (id)initWithFrame: (NSRect) frame vout: (vout_thread_t*) p_vout;
52
53 @end
54
55
56 struct vout_sys_t
57 {
58     NSAutoreleasePool * o_pool;
59     VLCWindow         * o_window;
60     VLCGLView         * o_glview;
61     vlc_bool_t          b_saved_frame;
62     NSRect              s_frame;
63     vlc_bool_t        * b_got_frame;
64 };
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69
70 static int  Init   ( vout_thread_t * p_vout );
71 static void End    ( vout_thread_t * p_vout );
72 static int  Manage ( vout_thread_t * p_vout );
73 static int  Control( vout_thread_t *, int, va_list );
74 static void Swap   ( vout_thread_t * p_vout );
75
76 int E_(OpenVideoGL)  ( vlc_object_t * p_this )
77 {
78     vout_thread_t * p_vout = (vout_thread_t *) p_this;
79     int i_timeout;
80     vlc_value_t val;
81
82 /* OpenGL interface disabled until
83  * - the green line is gone
84  * - other problems?????
85  */
86
87     if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
88     {
89         msg_Warn( p_vout, "no hardware acceleration" );
90         return( 1 );
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     {
97         msg_Err( p_vout, "out of memory" );
98         return( 1 );
99     }
100
101     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
102
103     /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
104     for( i_timeout = 20 ; i_timeout-- ; )
105     {
106         if( NSApp == NULL )     
107         {
108             msleep( INTF_IDLE_SLEEP );
109         }
110     }
111
112     if( NSApp == NULL )
113     {
114         /* No MacOS X intf, unable to communicate with MT */
115         msg_Err( p_vout, "no MacOS X interface present" );
116         return VLC_EGENERIC;
117     }
118
119     p_vout->pf_init   = Init;
120     p_vout->pf_end    = End;
121     p_vout->pf_manage = Manage;
122     p_vout->pf_control= Control;
123     p_vout->pf_swap   = Swap;
124
125
126     p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
127
128     var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
129     var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
130     var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131     var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
132
133     /* Setup the menuitem for the multiple displays. Read the vlc preference (macosx-vdev) for the primary display */
134     NSArray * o_screens = [NSScreen screens];
135     if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
136     {
137         int i = 1;
138         vlc_value_t val2, text;
139         NSScreen * o_screen;
140
141         var_Get( p_vout, "macosx-vdev", &val );
142
143         var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
144                                             VLC_VAR_HASCHOICE ); 
145         text.psz_string = _("Video device");
146         var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
147         
148         NSEnumerator * o_enumerator = [o_screens objectEnumerator];
149
150         while( (o_screen = [o_enumerator nextObject]) != NULL )
151         {
152             char psz_temp[255];
153             NSRect s_rect = [o_screen frame];
154
155             snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1, 
156                       "%s %d (%dx%d)", _("Screen"), i,
157                       (int)s_rect.size.width, (int)s_rect.size.height ); 
158
159             text.psz_string = psz_temp;
160             val2.i_int = i;
161             var_Change( p_vout, "video-device",
162                         VLC_VAR_ADDCHOICE, &val2, &text );
163
164             if( ( i - 1 ) == val.i_int )
165             {
166                 var_Set( p_vout, "video-device", val2 );
167             }
168             i++;
169         }
170
171         var_AddCallback( p_vout, "video-device", vout_VarCallback,
172                          NULL );
173
174         val2.b_bool = VLC_TRUE;
175         var_Set( p_vout, "intf-change", val2 );
176     }
177
178     /* Spawn window */
179     p_vout->p_sys->b_got_frame = VLC_FALSE;
180     p_vout->p_sys->o_window = [[VLCWindow alloc] initWithVout: p_vout
181                                                  frame: nil];
182     
183     /* Add OpenGL view */
184 #define o_glview p_vout->p_sys->o_glview
185     o_glview = [[VLCGLView alloc] initWithFrame:
186                 [p_vout->p_sys->o_window frame] vout: p_vout];
187     [p_vout->p_sys->o_window setContentView: o_glview];
188     [o_glview autorelease];
189 #undef o_glview
190
191     return VLC_SUCCESS;
192 }
193
194 int E_(CloseVideoGL) ( vlc_object_t * p_this )
195 {
196     vout_thread_t * p_vout = (vout_thread_t *) p_this;
197     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init]; 
198
199     [p_vout->p_sys->o_window close];
200
201     [o_pool release];
202     return VLC_SUCCESS;
203 }
204
205 static int Init( vout_thread_t * p_vout )
206 {
207     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
208     return VLC_SUCCESS;
209 }
210
211 static void End( vout_thread_t * p_vout )
212 {
213     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
214 }
215
216 static int Manage( vout_thread_t * p_vout )
217 {
218     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
219     {
220         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
221         
222         if( !p_vout->b_fullscreen )
223         {
224             /* Save window size and position */
225             p_vout->p_sys->s_frame.size =
226                 [[p_vout->p_sys->o_window contentView] frame].size;
227             p_vout->p_sys->s_frame.origin =
228                 [p_vout->p_sys->o_window frame].origin;
229             p_vout->p_sys->b_saved_frame = VLC_TRUE;
230         }
231         [p_vout->p_sys->o_window close];
232
233         p_vout->b_fullscreen = !p_vout->b_fullscreen;
234
235         if( p_vout->p_sys->b_saved_frame )
236         {
237             p_vout->p_sys->o_window = [[VLCWindow alloc]
238                 initWithVout: p_vout frame: &p_vout->p_sys->s_frame];
239         }
240         else
241         {
242             p_vout->p_sys->o_window = [[VLCWindow alloc]
243                 initWithVout: p_vout frame: nil];
244         }
245
246 #define o_glview p_vout->p_sys->o_glview
247         o_glview = [[VLCGLView alloc] initWithFrame: [p_vout->p_sys->o_window frame] vout: p_vout];
248         [p_vout->p_sys->o_window setContentView: o_glview];
249         [o_glview autorelease];
250         [[o_glview openGLContext] makeCurrentContext];
251 #undef o_glview
252         [o_pool release];
253
254         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
255     }
256     [p_vout->p_sys->o_window manage];
257     return VLC_SUCCESS;
258 }
259
260 /*****************************************************************************
261  * Control: control facility for the vout
262  *****************************************************************************/
263 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
264 {
265     vlc_bool_t b_arg;
266
267     switch( i_query )
268     {
269         case VOUT_SET_STAY_ON_TOP:
270             b_arg = va_arg( args, vlc_bool_t );
271             [p_vout->p_sys->o_window setOnTop: b_arg];
272             return VLC_SUCCESS;
273
274         case VOUT_CLOSE:
275         case VOUT_REPARENT:
276         default:
277             return vout_vaControlDefault( p_vout, i_query, args );
278     }
279 }
280
281 static void Swap( vout_thread_t * p_vout )
282 {
283     p_vout->p_sys->b_got_frame = VLC_TRUE;
284     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
285     if( [p_vout->p_sys->o_glview lockFocusIfCanDraw] )
286     {
287         glFlush();
288         [p_vout->p_sys->o_glview unlockFocus];
289     }
290 }
291
292 /*****************************************************************************
293  * VLCGLView implementation
294  *****************************************************************************/
295 @implementation VLCGLView
296
297 - (id) initWithFrame: (NSRect) frame vout: (vout_thread_t*) _p_vout
298 {
299     p_vout = _p_vout;
300     
301     NSOpenGLPixelFormatAttribute attribs[] =
302     {
303         NSOpenGLPFAAccelerated,
304         NSOpenGLPFANoRecovery,
305         NSOpenGLPFAColorSize, 24,
306         NSOpenGLPFAAlphaSize, 8,
307         NSOpenGLPFADepthSize, 24,
308         NSOpenGLPFAWindow,
309         0
310     };
311
312     NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
313         initWithAttributes: attribs];
314
315     if( !fmt )
316     {
317         msg_Warn( p_vout, "Cannot create NSOpenGLPixelFormat" );
318         return nil;
319     }
320
321     self = [super initWithFrame:frame pixelFormat: fmt];
322     [fmt release];
323
324     [[self openGLContext] makeCurrentContext];
325     [[self openGLContext] update];
326
327     /* Swap buffers only during the vertical retrace of the monitor.
328        http://developer.apple.com/documentation/GraphicsImaging/
329        Conceptual/OpenGL/chap5/chapter_5_section_44.html */
330     long params[] = { 1 };
331     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
332                      params );
333     return self;
334 }
335
336 - (void)reshape
337 {
338     int x, y;
339     NSRect bounds = [self bounds];
340     [[self openGLContext] makeCurrentContext];
341     if( bounds.size.height * p_vout->render.i_aspect <
342             bounds.size.width * VOUT_ASPECT_FACTOR )
343     {
344         x = bounds.size.height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
345         y = bounds.size.height;
346     }
347     else
348     {
349         x = bounds.size.width;
350         y = bounds.size.width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
351     }
352     glViewport( ( bounds.size.width - x ) / 2,
353                 ( bounds.size.height - y ) / 2, x, y );
354
355     if( p_vout->p_sys->b_got_frame )
356     {
357         /* Ask the opengl module to redraw */
358         vout_thread_t * p_parent;
359         p_parent = (vout_thread_t *) p_vout->p_parent;
360         if( p_parent && p_parent->pf_display )
361         {
362             p_parent->pf_display( p_parent, NULL );
363         }
364     }
365     else
366     {
367         glClear( GL_COLOR_BUFFER_BIT );
368     }
369 }
370
371 - (void) drawRect: (NSRect) rect
372 {
373     [[self openGLContext] makeCurrentContext];
374     glFlush();
375 }
376
377 @end
378
379