]> git.sesse.net Git - vlc/blob - modules/gui/minimal_macosx/VLCOpenGLVoutView.m
Removes trailing spaces. Removes tabs.
[vlc] / modules / gui / minimal_macosx / VLCOpenGLVoutView.m
1 /*****************************************************************************
2  * VLCOpenGLVoutView.m: MacOS X OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2007 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  *          Pierre d'Herbemont <pdherbemont 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 "intf.h"
35 #include "voutgl.h"
36 #include "VLCOpenGLVoutView.h"
37 #include "VLCMinimalVoutWindow.h"
38
39 #include <OpenGL/OpenGL.h>
40 #include <OpenGL/gl.h>
41
42 /*****************************************************************************
43  * cocoaglvoutviewInit
44  *****************************************************************************/
45 int cocoaglvoutviewInit( vout_thread_t * p_vout )
46 {
47     vlc_value_t value_drawable;
48     id <VLCOpenGLVoutEmbedding> o_cocoaglview_container;
49
50     msg_Dbg( p_vout, "Mac OS X Vout is opening" );
51
52     var_Create( p_vout, "drawable", VLC_VAR_DOINHERIT );
53     var_Get( p_vout, "drawable", &value_drawable );
54
55     p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
56
57     o_cocoaglview_container = (id) value_drawable.i_int;
58     if (!o_cocoaglview_container)
59     {
60         msg_Warn( p_vout, "No drawable!, spawing a window" );
61     }
62
63     p_vout->p_sys->b_embedded = VLC_FALSE;
64
65
66     /* Create the GL view */
67     struct args { vout_thread_t * p_vout; id <VLCOpenGLVoutEmbedding> container; } args = { p_vout, o_cocoaglview_container };
68
69     [VLCOpenGLVoutView performSelectorOnMainThread:@selector(autoinitOpenGLVoutViewIntVoutWithContainer:)
70                         withObject:[NSData dataWithBytes: &args length: sizeof(struct args)] waitUntilDone:YES];
71
72     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
73     return VLC_SUCCESS;
74 }
75
76 /*****************************************************************************
77  * cocoaglvoutviewEnd
78  *****************************************************************************/
79 void cocoaglvoutviewEnd( vout_thread_t * p_vout )
80 {
81     id <VLCOpenGLVoutEmbedding> o_cocoaglview_container;
82
83     msg_Dbg( p_vout, "Mac OS X Vout is closing" );
84     var_Destroy( p_vout, "drawable" );
85
86     o_cocoaglview_container = [p_vout->p_sys->o_glview container];
87
88     /* Make sure our view won't request the vout now */
89     [p_vout->p_sys->o_glview detachFromVout];
90     msg_Dbg( p_vout, "Mac OS X Vout is closing" );
91
92     /* Let the view go, _without_blocking_ */
93     [p_vout->p_sys->o_glview performSelectorOnMainThread:@selector(removeFromSuperview) withObject:NULL waitUntilDone:NO];
94
95     if( [(id)o_cocoaglview_container respondsToSelector:@selector(removeVoutSubview:)] )
96         [o_cocoaglview_container removeVoutSubview: p_vout->p_sys->o_glview];
97
98     [p_vout->p_sys->o_glview release];
99
100     [p_vout->p_sys->o_pool release];
101  
102 }
103
104 /*****************************************************************************
105  * cocoaglvoutviewManage
106  *****************************************************************************/
107 int cocoaglvoutviewManage( vout_thread_t * p_vout )
108 {
109     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
110     {
111         [p_vout->p_sys->o_glview reshape];
112         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
113     }
114     if( p_vout->i_changes & VOUT_CROP_CHANGE )
115     {
116         [p_vout->p_sys->o_glview reshape];
117         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
118     }
119
120     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
121     {
122         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
123
124         p_vout->b_fullscreen = !p_vout->b_fullscreen;
125
126         if( p_vout->b_fullscreen )
127             [[p_vout->p_sys->o_glview container] enterFullscreen];
128         else
129             [[p_vout->p_sys->o_glview container] leaveFullscreen];
130
131         [o_pool release];
132
133         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
134     }
135
136     //[[p_vout->p_sys->o_glview container] manage];
137     return VLC_SUCCESS;
138 }
139
140 /*****************************************************************************
141  * cocoaglvoutviewControl: control facility for the vout
142  *****************************************************************************/
143 int cocoaglvoutviewControl( vout_thread_t *p_vout, int i_query, va_list args )
144 {
145     vlc_bool_t b_arg;
146
147     switch( i_query )
148     {
149         case VOUT_SET_STAY_ON_TOP:
150             b_arg = va_arg( args, vlc_bool_t );
151             [[p_vout->p_sys->o_glview container] setOnTop: b_arg];
152             return VLC_SUCCESS;
153
154         case VOUT_CLOSE:
155         case VOUT_REPARENT:
156         default:
157             return vout_vaControlDefault( p_vout, i_query, args );
158     }
159 }
160
161 /*****************************************************************************
162  * cocoaglvoutviewSwap
163  *****************************************************************************/
164 void cocoaglvoutviewSwap( vout_thread_t * p_vout )
165 {
166     p_vout->p_sys->b_got_frame = VLC_TRUE;
167     [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
168 }
169
170 /*****************************************************************************
171  * cocoaglvoutviewLock
172  *****************************************************************************/
173 int cocoaglvoutviewLock( vout_thread_t * p_vout )
174 {
175     if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
176     {
177         [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
178         return 0;
179     }
180     return 1;
181 }
182
183 /*****************************************************************************
184  * cocoaglvoutviewUnlock
185  *****************************************************************************/
186 void cocoaglvoutviewUnlock( vout_thread_t * p_vout )
187 {
188     CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
189 }
190
191 /*****************************************************************************
192  * VLCOpenGLVoutView implementation
193  *****************************************************************************/
194 @implementation VLCOpenGLVoutView
195
196 /* Init a new gl view and register it to both the framework and the
197  * vout_thread_t. Must be called from main thread. */
198 + (void) autoinitOpenGLVoutViewIntVoutWithContainer: (NSData *) argsAsData
199 {
200     NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];
201     struct args { vout_thread_t * p_vout; id <VLCOpenGLVoutEmbedding> container; } *
202         args = (struct args *)[argsAsData bytes];
203     VLCOpenGLVoutView * oglview;
204
205     if( !args->container )
206     {
207         args->container = [[VLCMinimalVoutWindow alloc] initWithContentRect: NSMakeRect( 0, 0, args->p_vout->i_window_width, args->p_vout->i_window_height )];
208         [(VLCMinimalVoutWindow *)args->container makeKeyAndOrderFront: nil];
209     }
210     oglview = [[VLCOpenGLVoutView alloc] initWithVout: args->p_vout container: args->container];
211
212     args->p_vout->p_sys->o_glview = oglview;
213     [args->container addVoutSubview: oglview];
214
215     [pool release];
216 }
217
218 - (void)dealloc
219 {
220     [objectLock dealloc];
221     [super dealloc];
222 }
223
224 - (void)removeFromSuperview
225 {
226     [super removeFromSuperview];
227 }
228
229
230 - (id) initWithVout: (vout_thread_t *) vout container: (id <VLCOpenGLVoutEmbedding>) aContainer
231 {
232     NSOpenGLPixelFormatAttribute attribs[] =
233     {
234         NSOpenGLPFADoubleBuffer,
235         NSOpenGLPFAAccelerated,
236         NSOpenGLPFANoRecovery,
237         NSOpenGLPFAColorSize, 24,
238         NSOpenGLPFAAlphaSize, 8,
239         NSOpenGLPFADepthSize, 24,
240         NSOpenGLPFAWindow,
241         0
242     };
243
244     NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
245         initWithAttributes: attribs];
246
247     if( !fmt )
248     {
249         msg_Warn( p_vout, "could not create OpenGL video output" );
250         return nil;
251     }
252
253     if( self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt] )
254     {
255         p_vout = vout;
256         container = aContainer;
257         objectLock = [[NSLock alloc] init];
258
259         [fmt release];
260
261         [[self openGLContext] makeCurrentContext];
262         [[self openGLContext] update];
263
264         /* Swap buffers only during the vertical retrace of the monitor.
265         http://developer.apple.com/documentation/GraphicsImaging/
266         Conceptual/OpenGL/chap5/chapter_5_section_44.html */
267         long params[] = { 1 };
268         CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
269                      params );
270     }
271     return self;
272 }
273
274 - (void) detachFromVout
275 {
276     [objectLock lock];
277     p_vout = NULL;
278     [objectLock unlock];
279 }
280
281 - (id <VLCOpenGLVoutEmbedding>) container
282 {
283     return container;
284 }
285
286 - (void) destroyVout
287 {
288     [objectLock lock];
289     if( p_vout )
290     {
291         vlc_object_detach( p_vout );
292         vlc_object_release( p_vout );
293         vout_Destroy( (vout_thread_t *)p_vout );
294     }
295     [objectLock unlock];
296 }
297
298 - (void) reshape
299 {
300     int x, y;
301     vlc_value_t val;
302
303     [objectLock lock];
304     if( !p_vout )
305     {
306         [objectLock unlock];
307         return;
308     }
309
310     cocoaglvoutviewLock( p_vout );
311     NSRect bounds = [self bounds];
312
313     if( [[self container] stretchesVideo] )
314     {
315         x = bounds.size.width;
316         y = bounds.size.height;
317     }
318     else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
319              p_vout->fmt_in.i_sar_num <
320              bounds.size.width * p_vout->fmt_in.i_visible_height *
321              p_vout->fmt_in.i_sar_den )
322     {
323         x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
324               p_vout->fmt_in.i_sar_num ) /
325             ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
326
327         y = bounds.size.height;
328     }
329     else
330     {
331         x = bounds.size.width;
332         y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
333               p_vout->fmt_in.i_sar_den) /
334             ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num  );
335     }
336
337     glViewport( ( bounds.size.width - x ) / 2,
338                 ( bounds.size.height - y ) / 2, x, y );
339
340     if( p_vout->p_sys->b_got_frame )
341     {
342         /* Ask the opengl module to redraw */
343         vout_thread_t * p_parent;
344         p_parent = (vout_thread_t *) p_vout->p_parent;
345         cocoaglvoutviewUnlock( p_vout );
346         if( p_parent && p_parent->pf_display )
347         {
348             p_parent->pf_display( p_parent, NULL );
349         }
350     }
351     else
352     {
353         glClear( GL_COLOR_BUFFER_BIT );
354         cocoaglvoutviewUnlock( p_vout );
355     }
356     [objectLock unlock];
357     [super reshape];
358 }
359
360 - (void) update
361 {
362     if( kCGLNoError != CGLLockContext([[self openGLContext] CGLContextObj]) )
363         return;
364     [super update];
365     CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
366 }
367
368 - (void) drawRect: (NSRect) rect
369 {
370     if( kCGLNoError != CGLLockContext([[self openGLContext] CGLContextObj]) )
371         return;
372     [[self openGLContext] flushBuffer];
373     [super drawRect:rect];
374     CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
375 }
376
377 - (BOOL)mouseDownCanMoveWindow
378 {
379     return YES;
380 }
381 @end
382