VLCOpenGLVoutView.m 11.7 KB
Newer Older
1
2
3
/*****************************************************************************
 * VLCOpenGLVoutView.m: MacOS X OpenGL provider
 *****************************************************************************
4
 * Copyright (C) 2001-2009 the VideoLAN team
5
6
7
8
9
10
11
12
13
 * $Id$
 *
 * Authors: Colin Delacroix <colin@zoy.org>
 *          Florian G. Pflug <fgp@phlo.org>
 *          Jon Lech Johansen <jon-vl@nanocrew.net>
 *          Derk-Jan Hartman <hartman at videolan dot org>
 *          Eric Petit <titer@m0k.org>
 *          Benjamin Pracht <bigben at videolan dot org>
 *          Damien Fouilleul <damienf at videolan dot org>
14
 *          Pierre d'Herbemont <pdherbemont at videolan dot org>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "intf.h"
#include "voutgl.h"
#include "VLCOpenGLVoutView.h"
#include "VLCMinimalVoutWindow.h"

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>

/*****************************************************************************
 * cocoaglvoutviewInit
 *****************************************************************************/
int cocoaglvoutviewInit( vout_thread_t * p_vout )
{
    vlc_value_t value_drawable;
    id <VLCOpenGLVoutEmbedding> o_cocoaglview_container;

    msg_Dbg( p_vout, "Mac OS X Vout is opening" );

Pierre's avatar
Pierre committed
52
53
    var_Create( p_vout, "drawable-nsobject", VLC_VAR_DOINHERIT );
    var_Get( p_vout, "drawable-nsobject", &value_drawable );
54

55
56
    p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];

Pierre's avatar
Pierre committed
57
    o_cocoaglview_container = (id) value_drawable.p_address;
58
59
60
61
62
    if (!o_cocoaglview_container)
    {
        msg_Warn( p_vout, "No drawable!, spawing a window" );
    }

63
    p_vout->p_sys->b_embedded = false;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83


    /* Create the GL view */
    struct args { vout_thread_t * p_vout; id <VLCOpenGLVoutEmbedding> container; } args = { p_vout, o_cocoaglview_container };

    [VLCOpenGLVoutView performSelectorOnMainThread:@selector(autoinitOpenGLVoutViewIntVoutWithContainer:)
                        withObject:[NSData dataWithBytes: &args length: sizeof(struct args)] waitUntilDone:YES];

    [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
    return VLC_SUCCESS;
}

/*****************************************************************************
 * cocoaglvoutviewEnd
 *****************************************************************************/
void cocoaglvoutviewEnd( vout_thread_t * p_vout )
{
    id <VLCOpenGLVoutEmbedding> o_cocoaglview_container;

    msg_Dbg( p_vout, "Mac OS X Vout is closing" );
Pierre's avatar
Pierre committed
84
    var_Destroy( p_vout, "drawable-nsobject" );
85
86
87
88
89
90
91
92
93
94
95
96
97

    o_cocoaglview_container = [p_vout->p_sys->o_glview container];

    /* Make sure our view won't request the vout now */
    [p_vout->p_sys->o_glview detachFromVout];
    msg_Dbg( p_vout, "Mac OS X Vout is closing" );

    /* Let the view go, _without_blocking_ */
    [p_vout->p_sys->o_glview performSelectorOnMainThread:@selector(removeFromSuperview) withObject:NULL waitUntilDone:NO];

    if( [(id)o_cocoaglview_container respondsToSelector:@selector(removeVoutSubview:)] )
        [o_cocoaglview_container removeVoutSubview: p_vout->p_sys->o_glview];

98
99
    [p_vout->p_sys->o_glview release];

100
    [p_vout->p_sys->o_pool release];
101
 
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
}

/*****************************************************************************
 * cocoaglvoutviewManage
 *****************************************************************************/
int cocoaglvoutviewManage( vout_thread_t * p_vout )
{
    if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
    {
        [p_vout->p_sys->o_glview reshape];
        p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
    }
    if( p_vout->i_changes & VOUT_CROP_CHANGE )
    {
        [p_vout->p_sys->o_glview reshape];
        p_vout->i_changes &= ~VOUT_CROP_CHANGE;
    }

    if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
    {
        NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

        p_vout->b_fullscreen = !p_vout->b_fullscreen;

        if( p_vout->b_fullscreen )
            [[p_vout->p_sys->o_glview container] enterFullscreen];
        else
            [[p_vout->p_sys->o_glview container] leaveFullscreen];

        [o_pool release];

        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
    }

    //[[p_vout->p_sys->o_glview container] manage];
    return VLC_SUCCESS;
}

/*****************************************************************************
 * cocoaglvoutviewControl: control facility for the vout
 *****************************************************************************/
int cocoaglvoutviewControl( vout_thread_t *p_vout, int i_query, va_list args )
{
145
    bool b_arg;
146
147
148
149

    switch( i_query )
    {
        case VOUT_SET_STAY_ON_TOP:
150
            b_arg = (bool) va_arg( args, int );
151
152
153
154
            [[p_vout->p_sys->o_glview container] setOnTop: b_arg];
            return VLC_SUCCESS;

        default:
155
            return VLC_EGENERIC;
156
157
158
159
160
161
162
163
    }
}

/*****************************************************************************
 * cocoaglvoutviewSwap
 *****************************************************************************/
void cocoaglvoutviewSwap( vout_thread_t * p_vout )
{
164
    p_vout->p_sys->b_got_frame = true;
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
}

/*****************************************************************************
 * cocoaglvoutviewLock
 *****************************************************************************/
int cocoaglvoutviewLock( vout_thread_t * p_vout )
{
    if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
    {
        [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
        return 0;
    }
    return 1;
}

/*****************************************************************************
 * cocoaglvoutviewUnlock
 *****************************************************************************/
void cocoaglvoutviewUnlock( vout_thread_t * p_vout )
{
    CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
}

/*****************************************************************************
 * VLCOpenGLVoutView implementation
 *****************************************************************************/
@implementation VLCOpenGLVoutView

/* Init a new gl view and register it to both the framework and the
 * vout_thread_t. Must be called from main thread. */
+ (void) autoinitOpenGLVoutViewIntVoutWithContainer: (NSData *) argsAsData
{
198
    NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];
199
200
201
202
    struct args { vout_thread_t * p_vout; id <VLCOpenGLVoutEmbedding> container; } *
        args = (struct args *)[argsAsData bytes];
    VLCOpenGLVoutView * oglview;

203
204
205
206
207
    if( !args->container )
    {
        args->container = [[VLCMinimalVoutWindow alloc] initWithContentRect: NSMakeRect( 0, 0, args->p_vout->i_window_width, args->p_vout->i_window_height )];
        [(VLCMinimalVoutWindow *)args->container makeKeyAndOrderFront: nil];
    }
208
209
    oglview = [[VLCOpenGLVoutView alloc] initWithVout: args->p_vout container: args->container];

210
    args->p_vout->p_sys->o_glview = oglview;
211
    [args->container addVoutSubview: oglview];
212
213

    [pool release];
214
215
216
217
}

- (void)dealloc
{
218
    [objectLock dealloc];
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    [super dealloc];
}

- (void)removeFromSuperview
{
    [super removeFromSuperview];
}


- (id) initWithVout: (vout_thread_t *) vout container: (id <VLCOpenGLVoutEmbedding>) aContainer
{
    NSOpenGLPixelFormatAttribute attribs[] =
    {
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFAAccelerated,
        NSOpenGLPFANoRecovery,
        NSOpenGLPFAColorSize, 24,
        NSOpenGLPFAAlphaSize, 8,
        NSOpenGLPFADepthSize, 24,
        NSOpenGLPFAWindow,
        0
    };

    NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
        initWithAttributes: attribs];

    if( !fmt )
    {
        msg_Warn( p_vout, "could not create OpenGL video output" );
        return nil;
    }

    if( self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt] )
    {
        p_vout = vout;
        container = aContainer;
        objectLock = [[NSLock alloc] init];

        [fmt release];

        [[self openGLContext] makeCurrentContext];
        [[self openGLContext] update];

        /* Swap buffers only during the vertical retrace of the monitor.
        http://developer.apple.com/documentation/GraphicsImaging/
        Conceptual/OpenGL/chap5/chapter_5_section_44.html */
265
        GLint params[] = { 1 };
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
                     params );
    }
    return self;
}

- (void) detachFromVout
{
    [objectLock lock];
    p_vout = NULL;
    [objectLock unlock];
}

- (id <VLCOpenGLVoutEmbedding>) container
{
    return container;
}

- (void) destroyVout
{
    [objectLock lock];
    if( p_vout )
    {
        vlc_object_detach( p_vout );
        vlc_object_release( p_vout );
291
        vlc_object_release( p_vout );
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    }
    [objectLock unlock];
}

- (void) reshape
{
    int x, y;
    vlc_value_t val;

    [objectLock lock];
    if( !p_vout )
    {
        [objectLock unlock];
        return;
    }

    cocoaglvoutviewLock( p_vout );
    NSRect bounds = [self bounds];

    if( [[self container] stretchesVideo] )
    {
        x = bounds.size.width;
        y = bounds.size.height;
    }
    else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
             p_vout->fmt_in.i_sar_num <
             bounds.size.width * p_vout->fmt_in.i_visible_height *
             p_vout->fmt_in.i_sar_den )
    {
        x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
              p_vout->fmt_in.i_sar_num ) /
            ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);

        y = bounds.size.height;
    }
    else
    {
        x = bounds.size.width;
        y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
              p_vout->fmt_in.i_sar_den) /
            ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num  );
    }

    glViewport( ( bounds.size.width - x ) / 2,
                ( bounds.size.height - y ) / 2, x, y );

    if( p_vout->p_sys->b_got_frame )
    {
        /* Ask the opengl module to redraw */
        vout_thread_t * p_parent;
        p_parent = (vout_thread_t *) p_vout->p_parent;
        cocoaglvoutviewUnlock( p_vout );
        if( p_parent && p_parent->pf_display )
        {
            p_parent->pf_display( p_parent, NULL );
        }
    }
    else
    {
        glClear( GL_COLOR_BUFFER_BIT );
        cocoaglvoutviewUnlock( p_vout );
    }
    [objectLock unlock];
    [super reshape];
}

- (void) update
{
    if( kCGLNoError != CGLLockContext([[self openGLContext] CGLContextObj]) )
        return;
    [super update];
    CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
}

- (void) drawRect: (NSRect) rect
{
    if( kCGLNoError != CGLLockContext([[self openGLContext] CGLContextObj]) )
        return;
    [[self openGLContext] flushBuffer];
    [super drawRect:rect];
    CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
}

375
376
377
378
- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}
379
380
@end