]> git.sesse.net Git - vlc/blob - modules/video_output/macosx.m
vout_macosx: Remove trailing spaces.
[vlc] / modules / video_output / macosx.m
1 /*****************************************************************************
2  * voutgl.m: MacOS X OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2009 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
35 #import <Cocoa/Cocoa.h>
36 #import <OpenGL/OpenGL.h>
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_vout_display.h>
45 #include <vlc_vout_opengl.h>
46 #include "opengl.h"
47
48 /**
49  * Forward declarations
50  */
51 static int Open(vlc_object_t *);
52 static void Close(vlc_object_t *);
53
54 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count);
55 static void PictureRender(vout_display_t *vd, picture_t *pic);
56 static void PictureDisplay(vout_display_t *vd, picture_t *pic);
57 static int Control (vout_display_t *vd, int query, va_list ap);
58
59 static int OpenglLock(vout_opengl_t *gl);
60 static void OpenglUnlock(vout_opengl_t *gl);
61 static void OpenglSwap(vout_opengl_t *gl);
62
63 /**
64  * Module declaration
65  */
66 vlc_module_begin ()
67     /* Will be loaded even without interface module. see voutgl.m */
68     set_shortname("Mac OS X")
69     set_description( N_("Mac OS X OpenGL video output (requires drawable-nsobject)"))
70     set_category(CAT_VIDEO)
71     set_subcategory(SUBCAT_VIDEO_VOUT )
72     set_capability("vout display", 300)
73     set_callbacks(Open, Close)
74
75     add_shortcut("macosx")
76     add_shortcut("vout_macosx")
77 vlc_module_end ()
78
79 /**
80  * Obj-C protocol declaration that drawable-nsobject should follow
81  */
82 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
83 - (void)addVoutSubview:(NSView *)view;
84 - (void)removeVoutSubview:(NSView *)view;
85 @end
86
87 @interface VLCOpenGLVideoView : NSOpenGLView
88 {
89     vout_display_t *vd;
90 }
91 - (void)setVoutDisplay:(vout_display_t *)vd;
92 @end
93
94
95 struct vout_display_sys_t
96 {
97     VLCOpenGLVideoView *glView;
98     id<VLCOpenGLVideoViewEmbedding> container;
99
100     vout_opengl_t gl;
101     vout_display_opengl_t vgl;
102
103     picture_pool_t *pool;
104     picture_t *current;
105     bool has_first_frame;
106 };
107
108 static int Open(vlc_object_t *this)
109 {
110     vout_display_t *vd = (vout_display_t *)this;
111     vout_display_sys_t *sys = calloc(1, sizeof(*sys));
112     NSAutoreleasePool *nsPool = nil;
113
114     if (!sys)
115         return VLC_ENOMEM;
116
117     vd->sys = sys;
118     sys->pool = NULL;
119     sys->gl.sys = NULL;
120
121     /* Get the drawable object */
122     id container = var_CreateGetAddress(vd, "drawable-nsobject");
123     if (!container)
124     {
125         msg_Dbg(vd, "No drawable-nsobject, passing over.");
126         goto error;
127     }
128
129     /* This will be released in Close(), on
130      * main thread, after we are done using it. */
131     sys->container = [container retain];
132
133     /* Get our main view*/
134     nsPool = [[NSAutoreleasePool alloc] init];
135
136     [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
137     if (!sys->glView)
138         goto error;
139
140     [sys->glView setVoutDisplay:vd];
141
142     /* We don't wait, that means that we'll have to be careful about releasing
143      * container.
144      * That's why we'll release on main thread in Close(). */
145     [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
146     [nsPool release];
147     nsPool = nil;
148
149     /* Initialize common OpenGL video display */
150     sys->gl.lock = OpenglLock;
151     sys->gl.unlock = OpenglUnlock;
152     sys->gl.swap = OpenglSwap;
153     sys->gl.sys = sys;
154
155     if (vout_display_opengl_Init(&sys->vgl, &vd->fmt, &sys->gl))
156     {
157         sys->gl.sys = NULL;
158         goto error;
159     }
160
161     /* */
162     vout_display_info_t info = vd->info;
163     info.has_pictures_invalid = false;
164
165     /* Setup vout_display_t once everything is fine */
166     vd->info = info;
167
168     vd->pool = Pool;
169     vd->prepare = PictureRender;
170     vd->display = PictureDisplay;
171     vd->control = Control;
172
173     /* */
174     vout_display_SendEventFullscreen (vd, false);
175     vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
176
177     return VLC_SUCCESS;
178
179 error:
180     [nsPool release];
181     Close(this);
182     return VLC_EGENERIC;
183 }
184
185 void Close(vlc_object_t *this)
186 {
187     vout_display_t *vd = (vout_display_t *)this;
188     vout_display_sys_t *sys = vd->sys;
189
190     [sys->glView setVoutDisplay:nil];
191
192     var_Destroy(vd, "drawable-nsobject");
193     /* This will retain sys->glView */
194     [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
195     /* release on main thread as explained in Open() */
196     [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
197     [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
198
199     [sys->glView release];
200
201     if (sys->gl.sys != NULL)
202         vout_display_opengl_Clean(&sys->vgl);
203
204     free (sys);
205 }
206
207 /*****************************************************************************
208  * vout display callbacks
209  *****************************************************************************/
210
211 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
212 {
213     vout_display_sys_t *sys = vd->sys;
214     VLC_UNUSED(requested_count);
215
216     if (!sys->pool)
217         sys->pool = vout_display_opengl_GetPool (&sys->vgl);
218     assert(sys->pool);
219     return sys->pool;
220 }
221
222 static void PictureRender(vout_display_t *vd, picture_t *pic)
223 {
224
225     vout_display_sys_t *sys = vd->sys;
226
227     vout_display_opengl_Prepare( &sys->vgl, pic );
228 }
229
230 static void PictureDisplay(vout_display_t *vd, picture_t *pic)
231 {
232     vout_display_sys_t *sys = vd->sys;
233     vout_display_opengl_Display(&sys->vgl, &vd->fmt );
234     picture_Release (pic);
235     sys->has_first_frame = true;
236 }
237
238 static int Control (vout_display_t *vd, int query, va_list ap)
239 {
240     vout_display_sys_t *sys = vd->sys;
241
242     switch (query)
243     {
244         case VOUT_DISPLAY_CHANGE_FULLSCREEN:
245         case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
246         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
247         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
248         case VOUT_DISPLAY_CHANGE_ZOOM:
249         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
250         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
251         {
252             /* todo */
253             return VLC_EGENERIC;
254         }
255         case VOUT_DISPLAY_HIDE_MOUSE:
256             return VLC_SUCCESS;
257
258         case VOUT_DISPLAY_GET_OPENGL:
259         {
260             vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
261             *gl = &sys->gl;
262             return VLC_SUCCESS;
263         }
264
265         case VOUT_DISPLAY_RESET_PICTURES:
266             assert (0);
267         default:
268             msg_Err (vd, "Unknown request in Mac OS X vout display");
269             return VLC_EGENERIC;
270     }
271 }
272
273 /*****************************************************************************
274  * vout opengl callbacks
275  *****************************************************************************/
276 static int OpenglLock(vout_opengl_t *gl)
277 {
278     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
279     NSOpenGLContext *context = [sys->glView openGLContext];
280     CGLError err = CGLLockContext([context CGLContextObj]);
281     if (kCGLNoError == err)
282     {
283         [context makeCurrentContext];
284         return 0;
285     }
286     return 1;
287 }
288
289 static void OpenglUnlock(vout_opengl_t *gl)
290 {
291     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
292     CGLUnlockContext([[sys->glView openGLContext] CGLContextObj]);
293 }
294
295 static void OpenglSwap(vout_opengl_t *gl)
296 {
297     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
298     [[sys->glView openGLContext] flushBuffer];
299 }
300
301 /*****************************************************************************
302  * Our NSView object
303  *****************************************************************************/
304 @implementation VLCOpenGLVideoView
305
306 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
307
308 + (void)getNewView:(NSValue *)value
309 {
310     id *ret = [value pointerValue];
311     *ret = [[self alloc] init];
312 }
313
314 /**
315  * Gets called by the Open() method.
316  */
317 - (id)init
318 {
319     VLCAssertMainThread();
320
321     /* Warning - this may be called on non main thread */
322
323     NSOpenGLPixelFormatAttribute attribs[] =
324     {
325         NSOpenGLPFADoubleBuffer,
326         NSOpenGLPFAAccelerated,
327         NSOpenGLPFANoRecovery,
328         NSOpenGLPFAColorSize, 24,
329         NSOpenGLPFAAlphaSize, 8,
330         NSOpenGLPFADepthSize, 24,
331         NSOpenGLPFAWindow,
332         0
333     };
334
335     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
336
337     if (!fmt)
338         return nil;
339
340     self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
341     if (!self)
342         return nil;
343
344     /* Swap buffers only during the vertical retrace of the monitor.
345      http://developer.apple.com/documentation/GraphicsImaging/
346      Conceptual/OpenGL/chap5/chapter_5_section_44.html */
347     GLint params[] = { 1 };
348     CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
349
350     return self;
351 }
352
353 /**
354  * Gets called by the Close and Open methods.
355  * (Non main thread).
356  */
357 - (void)setVoutDisplay:(vout_display_t *)aVd
358 {
359     @synchronized(self) {
360         vd = aVd;
361     }
362 }
363
364 /**
365  * Local method that locks the gl context.
366  */
367 - (BOOL)lockgl
368 {
369     VLCAssertMainThread();
370     CGLError err = CGLLockContext([[self openGLContext] CGLContextObj]);
371     return err == kCGLNoError;
372 }
373
374 /**
375  * Local method that unlocks the gl context.
376  */
377 - (void)unlockgl
378 {
379     VLCAssertMainThread();
380     CGLUnlockContext([[self openGLContext] CGLContextObj]);
381 }
382
383 /**
384  * Local method that force a rendering of a frame.
385  * This will get called if Cocoa forces us to redraw (via -drawRect).
386  */
387 - (void)render
388 {
389     VLCAssertMainThread();
390
391     @synchronized(self) { // vd can be accessed from multiple threads
392         if (vd && vd->sys->has_first_frame)
393         {
394             // This will lock gl.
395             vout_display_opengl_Display( &vd->sys->vgl, &vd->source );
396         }
397         else {
398             glClear(GL_COLOR_BUFFER_BIT);
399         }
400
401     }
402 }
403
404 /**
405  * Method called by Cocoa when the view is resized.
406  */
407 - (void)reshape
408 {
409     VLCAssertMainThread();
410
411     NSRect bounds = [self bounds];
412
413     CGFloat height = bounds.size.height;
414     CGFloat width = bounds.size.width;
415
416     GLint x = width, y = height;
417
418     @synchronized(self) {
419         if (vd) {
420             CGFloat videoHeight = vd->source.i_visible_height;
421             CGFloat videoWidth = vd->source.i_visible_width;
422
423             GLint sarNum = vd->source.i_sar_num;
424             GLint sarDen = vd->source.i_sar_den;
425
426             if (height * videoWidth * sarNum < width * videoHeight * sarDen)
427             {
428                 x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
429                 y = height;
430             }
431             else
432             {
433                 x = width;
434                 y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
435             }
436         }
437     }
438
439     [self lockgl];
440     glClearColor(0, 0, 0, 1);
441     glViewport((width - x) / 2, (height - y) / 2, x, y);
442     [self unlockgl];
443
444     [super reshape];
445 }
446
447 /**
448  * Method called by Cocoa when the view is resized or the location has changed.
449  * We just need to make sure we are locking here.
450  */
451 - (void)update
452 {
453     VLCAssertMainThread();
454     BOOL success = [self lockgl];
455     if (!success)
456         return;
457
458     [super update];
459
460     [self unlockgl];
461 }
462
463 /**
464  * Method called by Cocoa to force redraw.
465  */
466 - (void)drawRect:(NSRect) rect
467 {
468     VLCAssertMainThread();
469     BOOL success = [self lockgl];
470     if (!success)
471         return;
472
473     [self render];
474
475     [self unlockgl];
476 }
477
478 - (void)renewGState
479 {
480     NSWindow *window = [self window];
481
482     // Remove flashes with splitter view.
483         if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
484                 [window disableScreenUpdatesUntilFlush];
485
486     [super renewGState];
487 }
488
489 - (BOOL)mouseDownCanMoveWindow
490 {
491     return YES;
492 }
493
494 - (BOOL)isOpaque
495 {
496     return YES;
497 }
498 @end