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
36 *****************************************************************************/
38 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
39 static void aglReshape( vout_thread_t * p_vout );
40 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
42 int aglInit( vout_thread_t * p_vout )
49 static const GLint ATTRIBUTES[] = {
62 AGLPixelFormat pixFormat;
64 p_vout->p_sys->b_embedded = true;
66 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
67 if( NULL == pixFormat )
69 msg_Err( p_vout, "No screen renderer available for required attributes." );
73 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
74 aglDestroyPixelFormat(pixFormat);
75 if( NULL == p_vout->p_sys->agl_ctx )
77 msg_Err( p_vout, "Cannot create AGL context." );
82 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
84 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
85 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
88 var_Get( p_vout->p_libvlc, "drawable-agl", &val );
89 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
90 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
92 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
93 viewBounds.top = val.i_int;
94 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
95 viewBounds.left = val.i_int;
96 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
97 viewBounds.bottom = val.i_int;
98 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
99 viewBounds.right = val.i_int;
100 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
101 clipBounds.top = val.i_int;
102 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
103 clipBounds.left = val.i_int;
104 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
105 clipBounds.bottom = val.i_int;
106 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
107 clipBounds.right = val.i_int;
109 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
110 || (clipBounds.left == clipBounds.right);
111 if( ! p_vout->p_sys->b_clipped_out )
114 aglSetViewport(p_vout, viewBounds, clipBounds);
118 p_vout->p_sys->clipBounds = clipBounds;
119 p_vout->p_sys->viewBounds = viewBounds;
124 void aglEnd( vout_thread_t * p_vout )
126 aglSetCurrentContext(NULL);
127 if( p_vout->p_sys->theWindow )
128 DisposeWindow( p_vout->p_sys->theWindow );
129 aglDestroyContext(p_vout->p_sys->agl_ctx);
132 void aglReshape( vout_thread_t * p_vout )
135 unsigned int i_height = p_vout->p_sys->i_height;
136 unsigned int i_width = p_vout->p_sys->i_width;
138 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
140 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
142 if( p_vout->p_sys->b_got_frame )
144 /* Ask the opengl module to redraw */
145 vout_thread_t * p_parent;
146 p_parent = (vout_thread_t *) p_vout->p_parent;
147 if( p_parent && p_parent->pf_display )
149 p_parent->pf_display( p_parent, NULL );
154 glClear( GL_COLOR_BUFFER_BIT );
158 /* private event class */
161 kEventClassVLCPlugin = 'vlcp',
163 /* private event kinds */
166 kEventVLCPluginShowFullscreen = 32768,
167 kEventVLCPluginHideFullscreen,
170 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
173 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
175 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
177 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
179 ReleaseEvent(myEvent);
183 int aglManage( vout_thread_t * p_vout )
185 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
190 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
192 if( p_vout->i_changes & VOUT_CROP_CHANGE )
197 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
199 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
201 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
203 if( p_vout->b_fullscreen )
205 /* Close the fullscreen window and resume normal drawing */
210 var_Get( p_vout->p_libvlc, "drawable-agl", &val );
211 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
212 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
214 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
215 viewBounds.top = val.i_int;
216 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
217 viewBounds.left = val.i_int;
218 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
219 viewBounds.bottom = val.i_int;
220 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
221 viewBounds.right = val.i_int;
222 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
223 clipBounds.top = val.i_int;
224 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
225 clipBounds.left = val.i_int;
226 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
227 clipBounds.bottom = val.i_int;
228 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
229 clipBounds.right = val.i_int;
231 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
232 aglSetViewport(p_vout, viewBounds, clipBounds);
234 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
235 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
241 GDHandle deviceHdl = GetMainDevice();
242 deviceRect = (*deviceHdl)->gdRect;
244 if( !p_vout->p_sys->theWindow )
246 /* Create a window */
247 WindowAttributes windowAttrs;
249 windowAttrs = kWindowStandardDocumentAttributes
250 | kWindowStandardHandlerAttribute
251 | kWindowLiveResizeAttribute
252 | kWindowNoShadowAttribute;
254 windowAttrs &= (~kWindowResizableAttribute);
256 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
257 if( !p_vout->p_sys->winGroup )
259 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
260 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
261 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
265 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
266 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
267 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
269 CFRelease(windowTitle);
271 //Install event handler
272 static const EventTypeSpec win_events[] = {
273 { kEventClassMouse, kEventMouseDown },
274 { kEventClassMouse, kEventMouseMoved },
275 { kEventClassMouse, kEventMouseUp },
276 { kEventClassWindow, kEventWindowClosed },
277 { kEventClassWindow, kEventWindowBoundsChanged },
278 { kEventClassCommand, kEventCommandProcess },
279 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
280 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
282 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
286 /* just in case device resolution changed */
287 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
289 glClear( GL_COLOR_BUFFER_BIT );
290 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
291 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
292 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
293 aglSetViewport(p_vout, deviceRect, deviceRect);
294 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
296 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
297 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
301 p_vout->b_fullscreen = !p_vout->b_fullscreen;
302 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
307 int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
311 case VOUT_SET_VIEWPORT:
313 Rect viewBounds, clipBounds;
314 viewBounds.top = va_arg( args, int);
315 viewBounds.left = va_arg( args, int);
316 viewBounds.bottom = va_arg( args, int);
317 viewBounds.right = va_arg( args, int);
318 clipBounds.top = va_arg( args, int);
319 clipBounds.left = va_arg( args, int);
320 clipBounds.bottom = va_arg( args, int);
321 clipBounds.right = va_arg( args, int);
323 if( !p_vout->b_fullscreen )
326 ** check that the clip rect is not empty, as this is used
327 ** by Firefox to prevent a plugin from displaying during
328 ** a scrolling event. In this case we just prevent buffers
329 ** from being swapped and ignore clipping as this is less
330 ** disruptive than a GL geometry change
333 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
334 || (clipBounds.left == clipBounds.right);
335 if( ! p_vout->p_sys->b_clipped_out )
337 /* ignore consecutive viewport update with identical parameters */
338 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
339 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
342 aglSetViewport(p_vout, viewBounds, clipBounds);
343 aglReshape( p_vout );
345 p_vout->p_sys->clipBounds = clipBounds;
346 p_vout->p_sys->viewBounds = viewBounds;
353 case VOUT_REDRAW_RECT:
355 vout_thread_t * p_parent;
358 areaBounds.top = va_arg( args, int);
359 areaBounds.left = va_arg( args, int);
360 areaBounds.bottom = va_arg( args, int);
361 areaBounds.right = va_arg( args, int);
363 /* Ask the opengl module to redraw */
364 p_parent = (vout_thread_t *) p_vout->p_parent;
365 if( p_parent && p_parent->pf_display )
367 p_parent->pf_display( p_parent, NULL );
377 void aglSwap( vout_thread_t * p_vout )
379 if( ! p_vout->p_sys->b_clipped_out )
381 p_vout->p_sys->b_got_frame = true;
382 aglSwapBuffers(p_vout->p_sys->agl_ctx);
391 /* Enter this function with the p_vout locked */
392 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
394 // mozilla plugin provides coordinates based on port bounds
395 // however AGL coordinates are based on window structure region
396 // and are vertically flipped
398 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
399 Rect winBounds, clientBounds;
401 GetWindowBounds(GetWindowFromPort(port),
402 kWindowStructureRgn, &winBounds);
403 GetWindowBounds(GetWindowFromPort(port),
404 kWindowContentRgn, &clientBounds);
406 /* update video clipping bounds in drawable */
407 rect[0] = (clientBounds.left-winBounds.left)
408 + clipBounds.left; // from window left edge
409 rect[1] = (winBounds.bottom-winBounds.top)
410 - (clientBounds.top-winBounds.top)
411 - clipBounds.bottom; // from window bottom edge
412 rect[2] = clipBounds.right-clipBounds.left; // width
413 rect[3] = clipBounds.bottom-clipBounds.top; // height
414 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
415 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
417 /* update video internal bounds in drawable */
418 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
419 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
420 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
421 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
422 - p_vout->p_sys->i_height;
424 aglUpdateContext(p_vout->p_sys->agl_ctx);
427 //default window event handler
428 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
430 OSStatus result = noErr;
431 UInt32 class = GetEventClass (event);
432 UInt32 kind = GetEventKind (event);
433 vout_thread_t *p_vout = (vout_thread_t *)userData;
435 result = CallNextEventHandler(nextHandler, event);
436 if(class == kEventClassCommand)
438 HICommand theHICommand;
439 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
441 switch ( theHICommand.commandID )
444 result = eventNotHandledErr;
447 else if(class == kEventClassWindow)
450 Rect rectPort = {0,0,0,0};
452 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
456 GetPortBounds(GetWindowPort(window), &rectPort);
461 case kEventWindowClosed:
462 case kEventWindowZoomed:
463 case kEventWindowBoundsChanged:
467 result = eventNotHandledErr;
470 else if(class == kEventClassMouse)
474 case kEventMouseDown:
478 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
481 case kEventMouseButtonPrimary:
485 var_Get( p_vout, "mouse-button-down", &val );
487 var_Set( p_vout, "mouse-button-down", val );
490 case kEventMouseButtonSecondary:
494 var_Get( p_vout, "mouse-button-down", &val );
496 var_Set( p_vout, "mouse-button-down", val );
499 case kEventMouseButtonTertiary:
503 var_Get( p_vout, "mouse-button-down", &val );
505 var_Set( p_vout, "mouse-button-down", val );
509 result = eventNotHandledErr;
518 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
521 case kEventMouseButtonPrimary:
523 UInt32 clickCount = 0;
524 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
530 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
536 var_SetBool( p_vout, "mouse-clicked", true );
538 var_Get( p_vout, "mouse-button-down", &val );
540 var_Set( p_vout, "mouse-button-down", val );
544 case kEventMouseButtonSecondary:
548 var_Get( p_vout, "mouse-button-down", &val );
550 var_Set( p_vout, "mouse-button-down", val );
553 case kEventMouseButtonTertiary:
557 var_Get( p_vout, "mouse-button-down", &val );
559 var_Set( p_vout, "mouse-button-down", val );
563 result = eventNotHandledErr;
568 case kEventMouseMoved:
573 unsigned int i_x, i_y;
574 unsigned int i_height = p_vout->p_sys->i_height;
575 unsigned int i_width = p_vout->p_sys->i_width;
577 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
579 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
581 val.i_int = ( ((int)ml.h) - i_x ) *
582 p_vout->render.i_width / i_width;
583 var_Set( p_vout, "mouse-x", val );
585 val.i_int = ( ((int)ml.v) - i_y ) *
586 p_vout->render.i_height / i_height;
588 var_Set( p_vout, "mouse-y", val );
591 var_Set( p_vout, "mouse-moved", val );
597 result = eventNotHandledErr;
600 else if(class == kEventClassTextInput)
604 case kEventTextInputUnicodeForKeyEvent:
609 result = eventNotHandledErr;
612 else if(class == kEventClassVLCPlugin)
616 case kEventVLCPluginShowFullscreen:
617 ShowWindow (p_vout->p_sys->theWindow);
618 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
619 //CGDisplayHideCursor(kCGDirectMainDisplay);
621 case kEventVLCPluginHideFullscreen:
622 HideWindow (p_vout->p_sys->theWindow);
623 SetSystemUIMode( kUIModeNormal, 0);
624 CGDisplayShowCursor(kCGDirectMainDisplay);
627 result = eventNotHandledErr;
634 int aglLock( vout_thread_t * p_vout )
638 * before 10.4, we set the AGL context as current and
639 * then we retrieve and use the matching CGL context
641 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
642 return kCGLNoError != CGLLockContext( CGLGetCurrentContext() );
644 /* since 10.4, this is the safe way to get the underlying CGL context */
645 CGLContextObj cglContext;
646 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
648 if( kCGLNoError == CGLLockContext( cglContext ) )
650 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
658 void aglUnlock( vout_thread_t * p_vout )
662 * before 10.4, we assume that the AGL context is current.
663 * therefore, we use the current CGL context
665 CGLUnlockContext( CGLGetCurrentContext() );
667 /* since 10.4, this is the safe way to get the underlying CGL context */
668 CGLContextObj cglContext;
669 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
671 CGLUnlockContext( cglContext );