]> git.sesse.net Git - vlc/blob - modules/gui/macosx/voutgl.m
For consistency, remove references to vlc from libvlc
[vlc] / modules / gui / macosx / voutgl.m
1 /*****************************************************************************
2  * voutgl.m: MacOS X OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
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  *          Benjamin Pracht <bigben at videolan dot org>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <stdlib.h>                                                /* free() */
34 #include <string.h>                                            /* strerror() */
35
36 #include <vlc_keys.h>
37
38 #include "intf.h"
39 #include "vout.h"
40
41 #include <OpenGL/OpenGL.h>
42 #include <OpenGL/gl.h>
43
44 #include <AGL/agl.h>
45
46 /*****************************************************************************
47  * VLCView interface
48  *****************************************************************************/
49 @interface VLCGLView : NSOpenGLView
50 {
51     vout_thread_t * p_vout;
52 }
53
54 - (id) initWithVout: (vout_thread_t *) p_vout;
55 @end
56
57 struct vout_sys_t
58 {
59     NSAutoreleasePool * o_pool;
60     VLCGLView         * o_glview;
61     VLCVoutView       * o_vout_view;
62     vlc_bool_t          b_saved_frame;
63     NSRect              s_frame;
64     vlc_bool_t          b_got_frame;
65     vlc_mutex_t         lock;
66     /* Mozilla plugin-related variables */
67     vlc_bool_t          b_embedded;
68     AGLContext          agl_ctx;
69     AGLDrawable         agl_drawable;
70     int                 i_offx, i_offy;
71     int                 i_width, i_height;
72 };
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77
78 static int  Init   ( vout_thread_t * p_vout );
79 static void End    ( vout_thread_t * p_vout );
80 static int  Manage ( vout_thread_t * p_vout );
81 static int  Control( vout_thread_t *, int, va_list );
82 static void Swap   ( vout_thread_t * p_vout );
83 static int  Lock   ( vout_thread_t * p_vout );
84 static void Unlock ( vout_thread_t * p_vout );
85
86 static int  aglInit   ( vout_thread_t * p_vout );
87 static void aglEnd    ( vout_thread_t * p_vout );
88 static int  aglManage ( vout_thread_t * p_vout );
89 static int  aglControl( vout_thread_t *, int, va_list );
90 static void aglSwap   ( vout_thread_t * p_vout );
91
92 int E_(OpenVideoGL)  ( vlc_object_t * p_this )
93 {
94     vout_thread_t * p_vout = (vout_thread_t *) p_this;
95     vlc_value_t value_drawable;
96
97     if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
98     {
99         msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
100                           "Video display will be slow" );
101         return( 1 );
102     }
103     msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
104
105     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106     if( p_vout->p_sys == NULL )
107     {
108         msg_Err( p_vout, "out of memory" );
109         return( 1 );
110     }
111
112     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
113
114     vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
115
116     var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
117     if( value_drawable.i_int != 0 )
118     {
119         static const GLint ATTRIBUTES[] = { 
120             AGL_WINDOW,
121             AGL_RGBA,
122             AGL_NO_RECOVERY,
123             AGL_ACCELERATED,
124             AGL_DOUBLEBUFFER,
125             AGL_RED_SIZE,   8,
126             AGL_GREEN_SIZE, 8,
127             AGL_BLUE_SIZE,  8,
128             AGL_ALPHA_SIZE, 8,
129             AGL_DEPTH_SIZE, 24,
130             AGL_NONE };
131
132         AGLPixelFormat pixFormat;
133
134         p_vout->p_sys->b_embedded = VLC_TRUE;
135
136         pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
137         if( NULL == pixFormat )
138         {
139             msg_Err( p_vout, "no screen renderer available for required attributes." );
140             return VLC_EGENERIC;
141         }
142         
143         p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
144         aglDestroyPixelFormat(pixFormat);
145         if( NULL == p_vout->p_sys->agl_ctx )
146         {
147             msg_Err( p_vout, "cannot create AGL context." );
148             return VLC_EGENERIC;
149         }
150         else {
151             // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
152             GLint param = 0;
153             aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, &param);
154             aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
155         }
156
157         p_vout->pf_init             = aglInit;
158         p_vout->pf_end              = aglEnd;
159         p_vout->pf_manage           = aglManage;
160         p_vout->pf_control          = aglControl;
161         p_vout->pf_swap             = aglSwap;
162         p_vout->pf_lock             = Lock;
163         p_vout->pf_unlock           = Unlock;
164     }
165     else
166     {
167         p_vout->p_sys->b_embedded = VLC_FALSE;
168
169         p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
170
171         /* Create the GL view */
172         p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
173         [p_vout->p_sys->o_glview autorelease];
174
175         /* Spawn the window */
176
177         if( !(p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
178                         subView: p_vout->p_sys->o_glview frame: nil]) )
179         {
180             return VLC_EGENERIC;
181         }
182         p_vout->pf_init   = Init;
183         p_vout->pf_end    = End;
184         p_vout->pf_manage = Manage;
185         p_vout->pf_control= Control;
186         p_vout->pf_swap   = Swap;
187         p_vout->pf_lock   = Lock;
188         p_vout->pf_unlock = Unlock;
189     }
190     p_vout->p_sys->b_got_frame = VLC_FALSE;
191
192     return VLC_SUCCESS;
193 }
194
195 void E_(CloseVideoGL) ( vlc_object_t * p_this )
196 {
197     vout_thread_t * p_vout = (vout_thread_t *) p_this;
198     if( p_vout->p_sys->b_embedded )
199     {
200         aglDestroyContext(p_vout->p_sys->agl_ctx);
201     }
202     else
203     {
204         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
205
206         /* Close the window */
207         [p_vout->p_sys->o_vout_view closeVout];
208
209         [o_pool release];
210     }
211     /* Clean up */
212     vlc_mutex_destroy( &p_vout->p_sys->lock );
213     free( p_vout->p_sys );
214 }
215
216 static int Init( vout_thread_t * p_vout )
217 {
218     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
219     return VLC_SUCCESS;
220 }
221
222 static void End( vout_thread_t * p_vout )
223 {
224     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
225 }
226
227 static int Manage( vout_thread_t * p_vout )
228 {
229     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
230     {
231         [p_vout->p_sys->o_glview reshape];
232         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
233     }
234     if( p_vout->i_changes & VOUT_CROP_CHANGE )
235     {
236         [p_vout->p_sys->o_glview reshape];
237         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
238     }
239
240     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
241     {
242         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
243
244         if( !p_vout->b_fullscreen )
245         {
246             /* Save window size and position */
247             p_vout->p_sys->s_frame.size =
248                 [p_vout->p_sys->o_vout_view frame].size;
249             p_vout->p_sys->s_frame.origin =
250                 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
251             p_vout->p_sys->b_saved_frame = VLC_TRUE;
252         }
253         [p_vout->p_sys->o_vout_view closeVout];
254
255         p_vout->b_fullscreen = !p_vout->b_fullscreen;
256
257 #define o_glview p_vout->p_sys->o_glview
258         o_glview = [[VLCGLView alloc] initWithVout: p_vout];
259         [o_glview autorelease];
260
261         if( p_vout->p_sys->b_saved_frame )
262         {
263             p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
264                         subView: o_glview
265                         frame: &p_vout->p_sys->s_frame];
266         }
267         else
268         {
269             p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
270                         subView: o_glview frame: nil];
271
272         }
273
274         [[o_glview openGLContext] makeCurrentContext];
275 #undef o_glview
276
277         [o_pool release];
278
279         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
280     }
281
282     [p_vout->p_sys->o_vout_view manage];
283     return VLC_SUCCESS;
284 }
285
286 /*****************************************************************************
287  * Control: control facility for the vout
288  *****************************************************************************/
289 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
290 {
291     vlc_bool_t b_arg;
292
293     switch( i_query )
294     {
295         case VOUT_SET_STAY_ON_TOP:
296             b_arg = va_arg( args, vlc_bool_t );
297             [p_vout->p_sys->o_vout_view setOnTop: b_arg];
298             return VLC_SUCCESS;
299
300         case VOUT_CLOSE:
301         case VOUT_REPARENT:
302         default:
303             return vout_vaControlDefault( p_vout, i_query, args );
304     }
305 }
306
307 static void Swap( vout_thread_t * p_vout )
308 {
309     p_vout->p_sys->b_got_frame = VLC_TRUE;
310     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
311     glFlush();
312 }
313
314 static int Lock( vout_thread_t * p_vout )
315 {
316     vlc_mutex_lock( &p_vout->p_sys->lock );
317     return 0;
318 }
319
320 static void Unlock( vout_thread_t * p_vout )
321 {
322     vlc_mutex_unlock( &p_vout->p_sys->lock );
323 }
324
325 /*****************************************************************************
326  * VLCGLView implementation
327  *****************************************************************************/
328 @implementation VLCGLView
329
330 - (id) initWithVout: (vout_thread_t *) vout
331 {
332     p_vout = vout;
333
334     NSOpenGLPixelFormatAttribute attribs[] =
335     {
336         NSOpenGLPFAAccelerated,
337         NSOpenGLPFANoRecovery,
338         NSOpenGLPFAColorSize, 24,
339         NSOpenGLPFAAlphaSize, 8,
340         NSOpenGLPFADepthSize, 24,
341         NSOpenGLPFAWindow,
342         0
343     };
344
345     NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
346         initWithAttributes: attribs];
347
348     if( !fmt )
349     {
350         msg_Warn( p_vout, "could not create OpenGL video output" );
351         return nil;
352     }
353
354     self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
355     [fmt release];
356
357     [[self openGLContext] makeCurrentContext];
358     [[self openGLContext] update];
359
360     /* Swap buffers only during the vertical retrace of the monitor.
361        http://developer.apple.com/documentation/GraphicsImaging/
362        Conceptual/OpenGL/chap5/chapter_5_section_44.html */
363     long params[] = { 1 };
364     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
365                      params );
366     return self;
367 }
368
369 - (void) reshape
370 {
371     int x, y;
372     vlc_value_t val;
373
374     Lock( p_vout );
375     NSRect bounds = [self bounds];
376
377     [[self openGLContext] makeCurrentContext];
378
379     var_Get( p_vout, "macosx-stretch", &val );
380     if( val.b_bool )
381     {
382         x = bounds.size.width;
383         y = bounds.size.height;
384     }
385     else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
386              p_vout->fmt_in.i_sar_num <
387              bounds.size.width * p_vout->fmt_in.i_visible_height *
388              p_vout->fmt_in.i_sar_den )
389     {
390         x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
391               p_vout->fmt_in.i_sar_num ) /
392             ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
393
394         y = bounds.size.height;
395     }
396     else
397     {
398         x = bounds.size.width;
399         y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
400               p_vout->fmt_in.i_sar_den) /
401             ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num  );
402     }
403
404     glViewport( ( bounds.size.width - x ) / 2,
405                 ( bounds.size.height - y ) / 2, x, y );
406
407     if( p_vout->p_sys->b_got_frame )
408     {
409         /* Ask the opengl module to redraw */
410         vout_thread_t * p_parent;
411         p_parent = (vout_thread_t *) p_vout->p_parent;
412         Unlock( p_vout );
413         if( p_parent && p_parent->pf_display )
414         {
415             p_parent->pf_display( p_parent, NULL );
416         }
417     }
418     else
419     {
420         glClear( GL_COLOR_BUFFER_BIT );
421         Unlock( p_vout );
422     }
423     [super reshape];
424 }
425
426 - (void) update
427 {
428     Lock( p_vout );
429     [super update];
430     Unlock( p_vout );
431 }
432
433 - (void) drawRect: (NSRect) rect
434 {
435     Lock( p_vout );
436     [[self openGLContext] makeCurrentContext];
437     glFlush();
438     [super drawRect:rect];
439     Unlock( p_vout );
440 }
441
442 @end
443
444 /*****************************************************************************
445  * embedded AGL context implementation
446  *****************************************************************************/
447
448 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
449 static void aglReshape( vout_thread_t * p_vout );
450
451 static int aglInit( vout_thread_t * p_vout )
452 {
453     vlc_value_t val;
454
455     Rect viewBounds;    
456     Rect clipBounds;
457     
458     var_Get( p_vout->p_libvlc, "drawable", &val );
459     p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
460     aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
461
462     var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
463     viewBounds.top = val.i_int;
464     var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
465     viewBounds.left = val.i_int;
466     var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
467     viewBounds.bottom = val.i_int;
468     var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
469     viewBounds.right = val.i_int;
470     var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
471     clipBounds.top = val.i_int;
472     var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
473     clipBounds.left = val.i_int;
474     var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
475     clipBounds.bottom = val.i_int;
476     var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
477     clipBounds.right = val.i_int;
478
479     aglSetViewport(p_vout, viewBounds, clipBounds);
480
481     aglSetCurrentContext(p_vout->p_sys->agl_ctx);
482     return VLC_SUCCESS;
483 }
484
485 static void aglEnd( vout_thread_t * p_vout )
486 {
487     aglSetCurrentContext(NULL);
488 }
489
490 static void aglReshape( vout_thread_t * p_vout )
491 {
492     unsigned int x, y;
493     unsigned int i_height = p_vout->p_sys->i_height;
494     unsigned int i_width  = p_vout->p_sys->i_width;
495
496     Lock( p_vout );
497
498     vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height); 
499
500     aglSetCurrentContext(p_vout->p_sys->agl_ctx);
501
502     glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
503
504     if( p_vout->p_sys->b_got_frame )
505     {
506         /* Ask the opengl module to redraw */
507         vout_thread_t * p_parent;
508         p_parent = (vout_thread_t *) p_vout->p_parent;
509         Unlock( p_vout );
510         if( p_parent && p_parent->pf_display )
511         {
512             p_parent->pf_display( p_parent, NULL );
513         }
514     }
515     else
516     {
517         glClear( GL_COLOR_BUFFER_BIT );
518         Unlock( p_vout );
519     }
520 }
521
522 static int aglManage( vout_thread_t * p_vout )
523 {
524     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
525     {
526         aglReshape(p_vout);
527         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
528     }
529     if( p_vout->i_changes & VOUT_CROP_CHANGE )
530     {
531         aglReshape(p_vout);
532         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
533     }
534     return VLC_SUCCESS;
535 }
536
537 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
538 {
539     switch( i_query )
540     {
541         case VOUT_SET_VIEWPORT:
542         {
543             Rect viewBounds, clipBounds;
544             viewBounds.top = va_arg( args, int);
545             viewBounds.left = va_arg( args, int);
546             viewBounds.bottom = va_arg( args, int);
547             viewBounds.right = va_arg( args, int);
548             clipBounds.top = va_arg( args, int);
549             clipBounds.left = va_arg( args, int);
550             clipBounds.bottom = va_arg( args, int);
551             clipBounds.right = va_arg( args, int);
552             aglSetViewport(p_vout, viewBounds, clipBounds);
553             return VLC_SUCCESS;
554         }
555
556         case VOUT_REPARENT:
557         {
558             AGLDrawable drawable = (AGLDrawable)va_arg( args, int);
559             if( drawable != p_vout->p_sys->agl_drawable )
560             {
561                 p_vout->p_sys->agl_drawable = drawable;
562                 aglSetDrawable(p_vout->p_sys->agl_ctx, drawable);
563             }
564             return VLC_SUCCESS;
565         }
566
567         default:
568             return vout_vaControlDefault( p_vout, i_query, args );
569     }
570 }
571
572 static void aglSwap( vout_thread_t * p_vout )
573 {
574     p_vout->p_sys->b_got_frame = VLC_TRUE;
575     aglSwapBuffers(p_vout->p_sys->agl_ctx);
576 }
577
578 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
579 {
580     // mozilla plugin provides coordinates based on port bounds
581     // however AGL coordinates are based on window structure region
582     // and are vertically flipped
583     GLint rect[4];
584     CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
585     Rect winBounds, clientBounds;
586
587     GetWindowBounds(GetWindowFromPort(port),
588         kWindowStructureRgn, &winBounds);
589     GetWindowBounds(GetWindowFromPort(port),
590         kWindowContentRgn, &clientBounds);
591
592     /* update video clipping bounds in drawable */
593     rect[0] = (clientBounds.left-winBounds.left)
594             + clipBounds.left;                  // from window left edge
595     rect[1] = (winBounds.bottom-winBounds.top)
596             - (clientBounds.top-winBounds.top)
597             - clipBounds.bottom;                // from window bottom edge
598     rect[2] = clipBounds.right-clipBounds.left; // width
599     rect[3] = clipBounds.bottom-clipBounds.top; // height
600     aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
601     aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
602
603     /* update video internal bounds in drawable */
604     p_vout->p_sys->i_width  = viewBounds.right-viewBounds.left;
605     p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
606     p_vout->p_sys->i_offx   = -clipBounds.left - viewBounds.left;
607     p_vout->p_sys->i_offy   = clipBounds.bottom + viewBounds.top
608                             - p_vout->p_sys->i_height; 
609
610     aglUpdateContext(p_vout->p_sys->agl_ctx);
611     aglReshape( p_vout );
612 }
613