1 /*****************************************************************************
2 * voutagl.c: MacOS X agl OpenGL provider (used by webbrowser.plugin)
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
34 /*****************************************************************************
35 * embedded AGL context implementation (not 64bit compatible)
36 *****************************************************************************/
39 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
40 static void aglReshape( vout_thread_t * p_vout );
41 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
43 int aglInit( vout_thread_t * p_vout )
50 static const GLint ATTRIBUTES[] = {
63 AGLPixelFormat pixFormat;
65 p_vout->p_sys->b_embedded = true;
67 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
68 if( NULL == pixFormat )
70 msg_Err( p_vout, "No screen renderer available for required attributes." );
74 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
75 aglDestroyPixelFormat(pixFormat);
76 if( NULL == p_vout->p_sys->agl_ctx )
78 msg_Err( p_vout, "Cannot create AGL context." );
83 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
85 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
86 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
89 var_Get( p_vout->p_libvlc, "drawable-agl", &val );
90 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
91 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
93 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
94 viewBounds.top = val.i_int;
95 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
96 viewBounds.left = val.i_int;
97 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
98 viewBounds.bottom = val.i_int;
99 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
100 viewBounds.right = val.i_int;
101 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
102 clipBounds.top = val.i_int;
103 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
104 clipBounds.left = val.i_int;
105 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
106 clipBounds.bottom = val.i_int;
107 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
108 clipBounds.right = val.i_int;
110 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
111 || (clipBounds.left == clipBounds.right);
112 if( ! p_vout->p_sys->b_clipped_out )
115 aglSetViewport(p_vout, viewBounds, clipBounds);
119 p_vout->p_sys->clipBounds = clipBounds;
120 p_vout->p_sys->viewBounds = viewBounds;
125 void aglEnd( vout_thread_t * p_vout )
127 aglSetCurrentContext(NULL);
128 if( p_vout->p_sys->theWindow )
129 DisposeWindow( p_vout->p_sys->theWindow );
130 aglDestroyContext(p_vout->p_sys->agl_ctx);
133 void aglReshape( vout_thread_t * p_vout )
136 unsigned int i_height = p_vout->p_sys->i_height;
137 unsigned int i_width = p_vout->p_sys->i_width;
139 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
141 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
143 if( p_vout->p_sys->b_got_frame )
145 /* Ask the opengl module to redraw */
146 vout_thread_t * p_parent;
147 p_parent = (vout_thread_t *) p_vout->p_parent;
148 if( p_parent && p_parent->pf_display )
150 p_parent->pf_display( p_parent, NULL );
155 glClear( GL_COLOR_BUFFER_BIT );
159 /* private event class */
162 kEventClassVLCPlugin = 'vlcp',
164 /* private event kinds */
167 kEventVLCPluginShowFullscreen = 32768,
168 kEventVLCPluginHideFullscreen,
171 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
174 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
176 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
178 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
180 ReleaseEvent(myEvent);
184 int aglManage( vout_thread_t * p_vout )
186 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
191 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
193 if( p_vout->i_changes & VOUT_CROP_CHANGE )
198 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
200 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
202 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
204 if( p_vout->b_fullscreen )
206 /* Close the fullscreen window and resume normal drawing */
211 var_Get( p_vout->p_libvlc, "drawable-agl", &val );
212 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
213 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
215 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
216 viewBounds.top = val.i_int;
217 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
218 viewBounds.left = val.i_int;
219 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
220 viewBounds.bottom = val.i_int;
221 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
222 viewBounds.right = val.i_int;
223 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
224 clipBounds.top = val.i_int;
225 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
226 clipBounds.left = val.i_int;
227 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
228 clipBounds.bottom = val.i_int;
229 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
230 clipBounds.right = val.i_int;
232 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
233 aglSetViewport(p_vout, viewBounds, clipBounds);
235 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
236 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
242 GDHandle deviceHdl = GetMainDevice();
243 deviceRect = (*deviceHdl)->gdRect;
245 if( !p_vout->p_sys->theWindow )
247 /* Create a window */
248 WindowAttributes windowAttrs;
250 windowAttrs = kWindowStandardDocumentAttributes
251 | kWindowStandardHandlerAttribute
252 | kWindowLiveResizeAttribute
253 | kWindowNoShadowAttribute;
255 windowAttrs &= (~kWindowResizableAttribute);
257 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
258 if( !p_vout->p_sys->winGroup )
260 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
261 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
262 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
266 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
267 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
268 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
270 CFRelease(windowTitle);
272 //Install event handler
273 static const EventTypeSpec win_events[] = {
274 { kEventClassMouse, kEventMouseDown },
275 { kEventClassMouse, kEventMouseMoved },
276 { kEventClassMouse, kEventMouseUp },
277 { kEventClassWindow, kEventWindowClosed },
278 { kEventClassWindow, kEventWindowBoundsChanged },
279 { kEventClassCommand, kEventCommandProcess },
280 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
281 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
283 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
287 /* just in case device resolution changed */
288 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
290 glClear( GL_COLOR_BUFFER_BIT );
291 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
292 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
293 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
294 aglSetViewport(p_vout, deviceRect, deviceRect);
295 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
297 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
298 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
302 p_vout->b_fullscreen = !p_vout->b_fullscreen;
303 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
308 int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
312 case VOUT_SET_VIEWPORT:
314 Rect viewBounds, clipBounds;
315 viewBounds.top = va_arg( args, int);
316 viewBounds.left = va_arg( args, int);
317 viewBounds.bottom = va_arg( args, int);
318 viewBounds.right = va_arg( args, int);
319 clipBounds.top = va_arg( args, int);
320 clipBounds.left = va_arg( args, int);
321 clipBounds.bottom = va_arg( args, int);
322 clipBounds.right = va_arg( args, int);
324 if( !p_vout->b_fullscreen )
327 ** check that the clip rect is not empty, as this is used
328 ** by Firefox to prevent a plugin from displaying during
329 ** a scrolling event. In this case we just prevent buffers
330 ** from being swapped and ignore clipping as this is less
331 ** disruptive than a GL geometry change
334 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
335 || (clipBounds.left == clipBounds.right);
336 if( ! p_vout->p_sys->b_clipped_out )
338 /* ignore consecutive viewport update with identical parameters */
339 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
340 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
343 aglSetViewport(p_vout, viewBounds, clipBounds);
344 aglReshape( p_vout );
346 p_vout->p_sys->clipBounds = clipBounds;
347 p_vout->p_sys->viewBounds = viewBounds;
354 case VOUT_REDRAW_RECT:
356 vout_thread_t * p_parent;
359 areaBounds.top = va_arg( args, int);
360 areaBounds.left = va_arg( args, int);
361 areaBounds.bottom = va_arg( args, int);
362 areaBounds.right = va_arg( args, int);
364 /* Ask the opengl module to redraw */
365 p_parent = (vout_thread_t *) p_vout->p_parent;
366 if( p_parent && p_parent->pf_display )
368 p_parent->pf_display( p_parent, NULL );
378 void aglSwap( vout_thread_t * p_vout )
380 if( ! p_vout->p_sys->b_clipped_out )
382 p_vout->p_sys->b_got_frame = true;
383 aglSwapBuffers(p_vout->p_sys->agl_ctx);
392 /* Enter this function with the p_vout locked */
393 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
395 // mozilla plugin provides coordinates based on port bounds
396 // however AGL coordinates are based on window structure region
397 // and are vertically flipped
399 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
400 Rect winBounds, clientBounds;
402 GetWindowBounds(GetWindowFromPort(port),
403 kWindowStructureRgn, &winBounds);
404 GetWindowBounds(GetWindowFromPort(port),
405 kWindowContentRgn, &clientBounds);
407 /* update video clipping bounds in drawable */
408 rect[0] = (clientBounds.left-winBounds.left)
409 + clipBounds.left; // from window left edge
410 rect[1] = (winBounds.bottom-winBounds.top)
411 - (clientBounds.top-winBounds.top)
412 - clipBounds.bottom; // from window bottom edge
413 rect[2] = clipBounds.right-clipBounds.left; // width
414 rect[3] = clipBounds.bottom-clipBounds.top; // height
415 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
416 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
418 /* update video internal bounds in drawable */
419 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
420 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
421 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
422 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
423 - p_vout->p_sys->i_height;
425 aglUpdateContext(p_vout->p_sys->agl_ctx);
428 //default window event handler
429 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
431 OSStatus result = noErr;
432 UInt32 class = GetEventClass (event);
433 UInt32 kind = GetEventKind (event);
434 vout_thread_t *p_vout = (vout_thread_t *)userData;
436 result = CallNextEventHandler(nextHandler, event);
437 if(class == kEventClassCommand)
439 HICommand theHICommand;
440 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
442 switch ( theHICommand.commandID )
445 result = eventNotHandledErr;
448 else if(class == kEventClassWindow)
451 Rect rectPort = {0,0,0,0};
453 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
457 GetPortBounds(GetWindowPort(window), &rectPort);
462 case kEventWindowClosed:
463 case kEventWindowZoomed:
464 case kEventWindowBoundsChanged:
468 result = eventNotHandledErr;
471 else if(class == kEventClassMouse)
475 case kEventMouseDown:
479 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
482 case kEventMouseButtonPrimary:
486 var_Get( p_vout, "mouse-button-down", &val );
488 var_Set( p_vout, "mouse-button-down", val );
491 case kEventMouseButtonSecondary:
495 var_Get( p_vout, "mouse-button-down", &val );
497 var_Set( p_vout, "mouse-button-down", val );
500 case kEventMouseButtonTertiary:
504 var_Get( p_vout, "mouse-button-down", &val );
506 var_Set( p_vout, "mouse-button-down", val );
510 result = eventNotHandledErr;
519 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
522 case kEventMouseButtonPrimary:
524 UInt32 clickCount = 0;
525 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
531 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
537 var_SetBool( p_vout, "mouse-clicked", true );
539 var_Get( p_vout, "mouse-button-down", &val );
541 var_Set( p_vout, "mouse-button-down", val );
545 case kEventMouseButtonSecondary:
549 var_Get( p_vout, "mouse-button-down", &val );
551 var_Set( p_vout, "mouse-button-down", val );
554 case kEventMouseButtonTertiary:
558 var_Get( p_vout, "mouse-button-down", &val );
560 var_Set( p_vout, "mouse-button-down", val );
564 result = eventNotHandledErr;
569 case kEventMouseMoved:
574 unsigned int i_x, i_y;
575 unsigned int i_height = p_vout->p_sys->i_height;
576 unsigned int i_width = p_vout->p_sys->i_width;
578 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
580 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
582 val.i_int = ( ((int)ml.h) - i_x ) *
583 p_vout->render.i_width / i_width;
584 var_Set( p_vout, "mouse-x", val );
586 val.i_int = ( ((int)ml.v) - i_y ) *
587 p_vout->render.i_height / i_height;
589 var_Set( p_vout, "mouse-y", val );
592 var_Set( p_vout, "mouse-moved", val );
598 result = eventNotHandledErr;
601 else if(class == kEventClassTextInput)
605 case kEventTextInputUnicodeForKeyEvent:
610 result = eventNotHandledErr;
613 else if(class == kEventClassVLCPlugin)
617 case kEventVLCPluginShowFullscreen:
618 ShowWindow (p_vout->p_sys->theWindow);
619 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
620 //CGDisplayHideCursor(kCGDirectMainDisplay);
622 case kEventVLCPluginHideFullscreen:
623 HideWindow (p_vout->p_sys->theWindow);
624 SetSystemUIMode( kUIModeNormal, 0);
625 CGDisplayShowCursor(kCGDirectMainDisplay);
628 result = eventNotHandledErr;
635 int aglLock( vout_thread_t * p_vout )
639 * before 10.4, we set the AGL context as current and
640 * then we retrieve and use the matching CGL context
642 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
643 return kCGLNoError != CGLLockContext( CGLGetCurrentContext() );
645 /* since 10.4, this is the safe way to get the underlying CGL context */
646 CGLContextObj cglContext;
647 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
649 if( kCGLNoError == CGLLockContext( cglContext ) )
651 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
659 void aglUnlock( vout_thread_t * p_vout )
663 * before 10.4, we assume that the AGL context is current.
664 * therefore, we use the current CGL context
666 CGLUnlockContext( CGLGetCurrentContext() );
668 /* since 10.4, this is the safe way to get the underlying CGL context */
669 CGLContextObj cglContext;
670 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
672 CGLUnlockContext( cglContext );