2009-02-26 00:06:58 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2009-07-17 14:47:41 +00:00
|
|
|
* modify it un der the terms of the GNU Library General Public
|
2009-02-26 00:06:58 +00:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstglwindow.h"
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* GstGLNSWindow declaration */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
@interface GstGLNSWindow: NSWindow {
|
|
|
|
BOOL m_isClosed;
|
|
|
|
GstGLWindowPrivate *m_priv;
|
|
|
|
}
|
|
|
|
- (id)initWithContentRect:(NSRect)contentRect
|
2009-07-17 14:47:41 +00:00
|
|
|
styleMask: (unsigned int) styleMask
|
|
|
|
backing: (NSBackingStoreType) bufferingType
|
|
|
|
defer: (BOOL) flag screen: (NSScreen *) aScreen
|
2009-02-26 00:06:58 +00:00
|
|
|
gstWin: (GstGLWindowPrivate *) priv;
|
2009-07-24 08:12:07 +00:00
|
|
|
- (void) setClosed;
|
2009-07-17 14:47:41 +00:00
|
|
|
- (BOOL) isClosed;
|
2009-07-24 08:12:07 +00:00
|
|
|
- (BOOL) canBecomeMainWindow;
|
|
|
|
- (BOOL) canBecomeKeyWindow;
|
2009-02-26 00:06:58 +00:00
|
|
|
@end
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* GstGLNSOpenGLView declaration */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
|
|
|
|
|
|
|
@interface GstGLNSOpenGLView: NSOpenGLView {
|
|
|
|
GstGLWindowPrivate *m_priv;
|
2009-08-03 08:13:02 +00:00
|
|
|
gint m_resizeCount;
|
2009-07-17 14:47:41 +00:00
|
|
|
}
|
|
|
|
- (id) initWithFrame:(NSRect)contentRect pixelFormat:(NSOpenGLPixelFormat *)fmt
|
|
|
|
private: (GstGLWindowPrivate *) priv;
|
|
|
|
@end
|
2009-02-26 00:06:58 +00:00
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* AppThreadPerformer declaration */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
/* Perform actions in the Application thread */
|
|
|
|
@interface AppThreadPerformer : NSObject {
|
|
|
|
GstGLWindowPrivate *m_priv;
|
|
|
|
GstGLWindowCB m_callback;
|
2009-07-17 14:47:41 +00:00
|
|
|
GstGLWindowCB2 m_callback2;
|
2009-02-26 00:06:58 +00:00
|
|
|
gpointer m_data;
|
2009-07-17 14:47:41 +00:00
|
|
|
gint m_width;
|
|
|
|
gint m_height;
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
2009-07-17 14:47:41 +00:00
|
|
|
- (id) initWithPrivate: (GstGLWindowPrivate *) priv;
|
|
|
|
- (id) initWithCallback: (GstGLWindowCB) callback userData: (gpointer) data;
|
|
|
|
- (id) initWithSize: (GstGLWindowCB2) callback userData: (gpointer) data toSize: (NSSize) size private: (GstGLWindowPrivate *) priv;
|
2009-02-26 00:06:58 +00:00
|
|
|
- (id) initWithAll: (GstGLWindowCB) callback userData: (gpointer) data private: (GstGLWindowPrivate *) priv;
|
|
|
|
- (void) updateWindow;
|
|
|
|
- (void) sendToApp;
|
2009-07-17 14:47:41 +00:00
|
|
|
- (void) setWindow;
|
2009-02-26 00:06:58 +00:00
|
|
|
- (void) stopApp;
|
2009-07-24 08:12:07 +00:00
|
|
|
- (void) closeWindow;
|
2009-02-26 00:06:58 +00:00
|
|
|
@end
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* GstGLWindow */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
#ifndef GNUSTEP
|
2009-04-13 05:24:06 +00:00
|
|
|
static BOOL GSRegisterCurrentThread(void) { return TRUE; };
|
|
|
|
static void GSUnregisterCurrentThread(void) {};
|
2009-02-26 00:06:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GST_GL_WINDOW_GET_PRIVATE(o) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE((o), GST_GL_TYPE_WINDOW, GstGLWindowPrivate))
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstGLWindowPrivate
|
|
|
|
{
|
|
|
|
GstGLNSWindow *internal_win_id;
|
2009-07-17 14:47:41 +00:00
|
|
|
NSOpenGLContext *external_gl_context;
|
2009-02-26 00:06:58 +00:00
|
|
|
GstGLWindowCB draw_cb;
|
|
|
|
gpointer draw_data;
|
|
|
|
GstGLWindowCB2 resize_cb;
|
|
|
|
gpointer resize_data;
|
|
|
|
GstGLWindowCB close_cb;
|
|
|
|
gpointer close_data;
|
|
|
|
gboolean visible;
|
|
|
|
NSWindow *parent;
|
2009-07-17 14:47:41 +00:00
|
|
|
NSThread *thread;
|
|
|
|
gboolean running;
|
2009-07-27 07:58:20 +00:00
|
|
|
guint source_id;
|
2009-02-26 00:06:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstGLWindow, gst_gl_window, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "GstGLWindow"
|
|
|
|
|
|
|
|
gboolean _gst_gl_window_debug = FALSE;
|
|
|
|
|
|
|
|
/* Must be called in the gl thread */
|
|
|
|
static void
|
|
|
|
gst_gl_window_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (gst_gl_window_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_window_log_handler (const gchar * domain, GLogLevelFlags flags,
|
|
|
|
const gchar * message, gpointer user_data)
|
|
|
|
{
|
|
|
|
if (_gst_gl_window_debug) {
|
|
|
|
g_log_default_handler (domain, flags, message, user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
gboolean
|
2009-07-27 07:58:20 +00:00
|
|
|
gst_gl_window_nsapp_init (GCond *nsapp_init_cond)
|
2009-07-17 14:47:41 +00:00
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
[NSApplication sharedApplication];
|
2009-07-27 07:58:20 +00:00
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
[pool release];
|
|
|
|
|
2009-07-27 07:58:20 +00:00
|
|
|
g_cond_signal (nsapp_init_cond);
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-07-27 07:58:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_gl_window_nsapp_iteration (gpointer data)
|
|
|
|
{
|
|
|
|
NSEvent *event = nil;
|
|
|
|
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
if ([NSThread isMainThread]) {
|
|
|
|
|
2009-08-03 08:13:02 +00:00
|
|
|
while ((event = ([NSApp nextEventMatchingMask:NSAnyEventMask
|
|
|
|
untilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]
|
2009-07-27 07:58:20 +00:00
|
|
|
inMode:NSDefaultRunLoopMode dequeue:YES])) != nil)
|
2009-08-03 08:13:02 +00:00
|
|
|
|
|
|
|
[NSApp sendEvent:event];
|
2009-07-27 07:58:20 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
static void
|
|
|
|
gst_gl_window_class_init (GstGLWindowClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *obj_class = G_OBJECT_CLASS (klass);
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-07-27 07:58:20 +00:00
|
|
|
GMutex *nsapp_init_mutex = g_mutex_new ();
|
|
|
|
GCond *nsapp_init_cond = g_cond_new ();
|
|
|
|
|
|
|
|
g_mutex_lock (nsapp_init_mutex);
|
|
|
|
|
|
|
|
g_idle_add ((GSourceFunc) gst_gl_window_nsapp_init, nsapp_init_cond);
|
|
|
|
|
|
|
|
g_cond_wait (nsapp_init_cond, nsapp_init_mutex);
|
|
|
|
g_mutex_unlock (nsapp_init_mutex);
|
|
|
|
|
|
|
|
g_mutex_free (nsapp_init_mutex);
|
|
|
|
g_cond_free (nsapp_init_cond);
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (GstGLWindowPrivate));
|
|
|
|
|
|
|
|
obj_class->finalize = gst_gl_window_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_window_init (GstGLWindow * window)
|
|
|
|
{
|
|
|
|
window->priv = GST_GL_WINDOW_GET_PRIVATE (window);
|
|
|
|
|
|
|
|
if (g_getenv ("GST_GL_WINDOW_DEBUG") != NULL)
|
|
|
|
_gst_gl_window_debug = TRUE;
|
|
|
|
|
|
|
|
g_log_set_handler ("GstGLWindow", G_LOG_LEVEL_DEBUG,
|
|
|
|
gst_gl_window_log_handler, NULL);
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
/* Must be called in the gl thread */
|
|
|
|
GstGLWindow *
|
2009-04-20 21:55:51 +00:00
|
|
|
gst_gl_window_new (gint width, gint height, gulong external_gl_context)
|
2009-02-26 00:06:58 +00:00
|
|
|
{
|
|
|
|
GstGLWindow *window = g_object_new (GST_GL_TYPE_WINDOW, NULL);
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
|
|
|
|
NSAutoreleasePool *pool = nil;
|
|
|
|
NSRect rect;
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
static gint x = 0;
|
|
|
|
static gint y = 0;
|
|
|
|
|
|
|
|
x += 20;
|
|
|
|
y += 20;
|
|
|
|
|
|
|
|
priv->internal_win_id = nil;
|
2009-07-17 14:47:41 +00:00
|
|
|
priv->external_gl_context = (NSOpenGLContext *) external_gl_context;
|
2009-02-26 00:06:58 +00:00
|
|
|
priv->draw_cb = NULL;
|
|
|
|
priv->draw_data = NULL;
|
|
|
|
priv->resize_cb = NULL;
|
|
|
|
priv->resize_data = NULL;
|
|
|
|
priv->close_cb = NULL;
|
|
|
|
priv->close_data = NULL;
|
|
|
|
priv->visible = FALSE;
|
|
|
|
priv->parent = nil;
|
2009-07-17 14:47:41 +00:00
|
|
|
priv->thread = nil;
|
|
|
|
priv->running = TRUE;
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
GSRegisterCurrentThread();
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2009-03-07 02:13:38 +00:00
|
|
|
rect.origin.x = 0;
|
|
|
|
rect.origin.y = 0;
|
2009-02-26 00:06:58 +00:00
|
|
|
rect.size.width = width;
|
|
|
|
rect.size.height = height;
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
priv->internal_win_id =[[GstGLNSWindow alloc] initWithContentRect:rect styleMask:
|
|
|
|
(NSTitledWindowMask | NSClosableWindowMask |
|
2009-07-24 08:12:07 +00:00
|
|
|
NSResizableWindowMask | NSMiniaturizableWindowMask)
|
2009-02-26 00:06:58 +00:00
|
|
|
backing: NSBackingStoreBuffered defer: NO screen: nil gstWin: priv];
|
|
|
|
|
2009-03-07 02:13:38 +00:00
|
|
|
if (priv->internal_win_id) {
|
2009-04-13 05:24:06 +00:00
|
|
|
NSRect windowRect;
|
2009-03-07 02:13:38 +00:00
|
|
|
NSRect mainRect = [[NSScreen mainScreen] visibleFrame];
|
2009-07-17 14:47:41 +00:00
|
|
|
g_debug ("main screen rect: %d %d %d %d\n", (int) mainRect.origin.x, (int) mainRect.origin.y,
|
2009-03-07 02:13:38 +00:00
|
|
|
(int) mainRect.size.width, (int) mainRect.size.height);
|
|
|
|
|
2009-04-13 05:24:06 +00:00
|
|
|
windowRect = [priv->internal_win_id frame];
|
2009-07-17 14:47:41 +00:00
|
|
|
g_debug ("window rect: %d %d %d %d\n", (int) windowRect.origin.x, (int) windowRect.origin.y,
|
2009-03-07 02:13:38 +00:00
|
|
|
(int) windowRect.size.width, (int) windowRect.size.height);
|
|
|
|
|
|
|
|
windowRect.origin.x += x;
|
|
|
|
windowRect.origin.y += mainRect.size.height > y ? (mainRect.size.height - y) * 0.5 : y;
|
|
|
|
[priv->internal_win_id setFrame:windowRect display:NO];
|
|
|
|
}
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
priv->thread = [NSThread currentThread];
|
|
|
|
|
|
|
|
[NSApp setDelegate: priv->internal_win_id];
|
2009-03-07 02:13:38 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
[pool release];
|
2009-07-27 07:58:20 +00:00
|
|
|
|
|
|
|
priv->source_id = g_timeout_add_seconds (1, gst_gl_window_nsapp_iteration, priv->internal_win_id);
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
GQuark
|
|
|
|
gst_gl_window_error_quark (void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string ("gst-gl-window-error");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-20 21:55:51 +00:00
|
|
|
gst_gl_window_set_external_window_id (GstGLWindow * window, gulong id)
|
2009-02-26 00:06:58 +00:00
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
2009-07-27 07:58:20 +00:00
|
|
|
|
|
|
|
g_source_remove (priv->source_id);
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
if (GSRegisterCurrentThread()) {
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
AppThreadPerformer* app_thread_performer = [[AppThreadPerformer alloc] initWithPrivate:priv];
|
2009-07-17 14:47:41 +00:00
|
|
|
priv->parent = (NSWindow*) id;
|
|
|
|
[app_thread_performer performSelectorOnMainThread:@selector(setWindow)
|
|
|
|
withObject:nil waitUntilDone:YES];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
GSUnregisterCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_debug ("failed to register current thread, cannot set external window id");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called in the gl thread */
|
|
|
|
void
|
|
|
|
gst_gl_window_set_draw_callback (GstGLWindow * window, GstGLWindowCB callback,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
|
|
|
|
priv->draw_cb = callback;
|
|
|
|
priv->draw_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called in the gl thread */
|
|
|
|
void
|
|
|
|
gst_gl_window_set_resize_callback (GstGLWindow * window,
|
|
|
|
GstGLWindowCB2 callback, gpointer data)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
|
|
|
|
priv->resize_cb = callback;
|
|
|
|
priv->resize_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called in the gl thread */
|
|
|
|
void
|
|
|
|
gst_gl_window_set_close_callback (GstGLWindow * window, GstGLWindowCB callback,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
|
|
|
|
priv->close_cb = callback;
|
|
|
|
priv->close_data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_gl_window_draw_unlocked (GstGLWindow * window)
|
|
|
|
{
|
|
|
|
gst_gl_window_draw (window);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread safe */
|
|
|
|
void
|
|
|
|
gst_gl_window_draw (GstGLWindow * window)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
|
|
|
|
if (GSRegisterCurrentThread()) {
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
AppThreadPerformer* app_thread_performer = [[AppThreadPerformer alloc] initWithPrivate:priv];
|
2009-07-17 14:47:41 +00:00
|
|
|
[app_thread_performer performSelector:@selector(updateWindow)
|
|
|
|
onThread:priv->thread withObject:nil waitUntilDone:YES];
|
2009-07-24 08:12:07 +00:00
|
|
|
|
|
|
|
if (!priv->parent && !priv->visible) {
|
|
|
|
g_debug ("make the window available");
|
|
|
|
[priv->internal_win_id makeMainWindow];
|
|
|
|
[priv->internal_win_id orderFront:priv->internal_win_id];
|
|
|
|
priv->visible = TRUE;
|
2009-08-03 08:13:02 +00:00
|
|
|
}
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
GSUnregisterCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_debug ("failed to register current thread, cannot draw");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_gl_window_run_loop (GstGLWindow * window)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2009-07-17 14:47:41 +00:00
|
|
|
NSRunLoop *run_loop = [NSRunLoop currentRunLoop];
|
|
|
|
|
|
|
|
g_debug ("begin loop: %lud\n", (gulong) run_loop);
|
2009-07-27 07:58:20 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
if (priv->internal_win_id != nil) {
|
2009-07-27 07:58:20 +00:00
|
|
|
while(priv->running)
|
2009-08-03 08:13:02 +00:00
|
|
|
[run_loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
[priv->internal_win_id release];
|
2009-07-17 14:47:41 +00:00
|
|
|
priv->internal_win_id = nil;
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
g_debug ("end loop\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread safe */
|
|
|
|
void
|
|
|
|
gst_gl_window_quit_loop (GstGLWindow * window, GstGLWindowCB callback,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
if (window) {
|
|
|
|
if (GSRegisterCurrentThread()) {
|
2009-07-17 14:47:41 +00:00
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
2009-02-26 00:06:58 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
AppThreadPerformer* app_thread_performer = [[AppThreadPerformer alloc] initWithAll:callback
|
|
|
|
userData:data private:priv];
|
|
|
|
|
|
|
|
[app_thread_performer performSelector:@selector(stopApp) onThread:priv->thread
|
|
|
|
withObject:nil waitUntilDone:YES];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
GSUnregisterCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_debug ("failed to register current thread, application thread is lost");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread safe */
|
|
|
|
void
|
|
|
|
gst_gl_window_send_message (GstGLWindow * window, GstGLWindowCB callback,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
if (window) {
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
if (GSRegisterCurrentThread()) {
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
AppThreadPerformer* app_thread_performer = [[AppThreadPerformer alloc] initWithAll:callback
|
|
|
|
userData:data private:priv];
|
|
|
|
|
|
|
|
[app_thread_performer performSelector:@selector(sendToApp) onThread:priv->thread
|
|
|
|
withObject:nil waitUntilDone:YES];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
GSUnregisterCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_debug ("failed to register current thread, cannot send message");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* GstGLNSWindow implementation */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
@implementation GstGLNSWindow
|
|
|
|
|
|
|
|
- (id) initWithContentRect: (NSRect) contentRect
|
2009-07-17 14:47:41 +00:00
|
|
|
styleMask: (unsigned int) styleMask
|
2009-02-26 00:06:58 +00:00
|
|
|
backing: (NSBackingStoreType) bufferingType
|
|
|
|
defer: (BOOL) flag screen: (NSScreen *) aScreen
|
|
|
|
gstWin: (GstGLWindowPrivate *) priv {
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
GstGLNSOpenGLView *glView = nil;
|
2009-02-26 00:06:58 +00:00
|
|
|
NSOpenGLPixelFormat *fmt = nil;
|
|
|
|
NSOpenGLContext *glContext = nil;
|
|
|
|
NSOpenGLPixelFormatAttribute attribs[] = {
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
2009-07-17 14:47:41 +00:00
|
|
|
NSOpenGLPFADepthSize, 32,
|
2009-02-26 00:06:58 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2009-04-13 05:24:06 +00:00
|
|
|
m_isClosed = NO;
|
|
|
|
m_priv = priv;
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
self = [super initWithContentRect: contentRect
|
2009-07-17 14:47:41 +00:00
|
|
|
styleMask: styleMask backing: bufferingType
|
|
|
|
defer: flag screen:aScreen];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[self setReleasedWhenClosed:NO];
|
|
|
|
|
|
|
|
g_debug ("initializing GstGLNSWindow");
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
glView = [GstGLNSOpenGLView alloc];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
|
|
|
|
|
|
|
|
if (!fmt) {
|
|
|
|
g_warning ("cannot create NSOpenGLPixelFormat");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
glView = [glView initWithFrame:contentRect pixelFormat:fmt private: m_priv];
|
|
|
|
|
|
|
|
[self setContentView:glView];
|
|
|
|
|
|
|
|
glContext = [[NSOpenGLContext alloc] initWithFormat:fmt
|
|
|
|
shareContext:m_priv->external_gl_context];
|
|
|
|
|
|
|
|
[glView setOpenGLContext:glContext];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
/* OpenGL context is made current only one time threre.
|
|
|
|
* Indeed, all OpenGL calls are made in only one thread,
|
|
|
|
* the Application thread */
|
|
|
|
[glContext makeCurrentContext];
|
|
|
|
|
|
|
|
[glContext update];
|
|
|
|
|
|
|
|
/* Back and front buffers are swapped only during the vertical retrace of the monitor.
|
|
|
|
* Discarded if you configured your driver to Never-use-V-Sync.
|
|
|
|
*/
|
|
|
|
NS_DURING {
|
|
|
|
if (glContext) {
|
|
|
|
long swapInterval = 1;
|
2009-07-17 14:47:41 +00:00
|
|
|
[[glView openGLContext] setValues:(const GLint *)&swapInterval forParameter:NSOpenGLCPSwapInterval];
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
} NS_HANDLER {
|
|
|
|
g_debug ("your back-end does not implement NSOpenglContext::setValues");
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
|
|
|
g_debug ("opengl GstGLNSWindow initialized: %d x %d",
|
2009-07-17 14:47:41 +00:00
|
|
|
(gint) contentRect.size.width, (gint) contentRect.size.height);
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[self setTitle:@"OpenGL renderer"];
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
[self setBackgroundColor:[NSColor clearColor]];
|
2009-07-24 08:12:07 +00:00
|
|
|
|
|
|
|
[self orderOut:m_priv->internal_win_id];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-07-24 08:12:07 +00:00
|
|
|
- (void) setClosed {
|
|
|
|
m_isClosed = YES;
|
|
|
|
}
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
- (BOOL) isClosed {
|
|
|
|
return m_isClosed;
|
|
|
|
}
|
|
|
|
|
2009-07-24 08:12:07 +00:00
|
|
|
- (BOOL) canBecomeMainWindow {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) canBecomeKeyWindow {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called in the main thread which is never the gl thread */
|
2009-02-26 00:06:58 +00:00
|
|
|
- (BOOL) windowShouldClose:(id)sender {
|
2009-07-24 08:12:07 +00:00
|
|
|
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
AppThreadPerformer* app_thread_performer = [[AppThreadPerformer alloc]
|
|
|
|
initWithPrivate:m_priv];
|
|
|
|
|
|
|
|
[app_thread_performer performSelector:@selector(closeWindow) onThread:m_priv->thread
|
|
|
|
withObject:nil waitUntilDone:YES];
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) applicationDidFinishLaunching: (NSNotification *) not {
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) applicationWillFinishLaunching: (NSNotification *) not {
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app {
|
|
|
|
/* the application is manually stopped by calling stopApp on the AppThreadPerformer */
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) applicationWillTerminate:(NSNotification *)aNotification {
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* GstGLNSOpenGLView implementation */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
|
|
|
|
|
|
|
@implementation GstGLNSOpenGLView
|
|
|
|
|
|
|
|
- (id)initWithFrame:(NSRect)contentRect pixelFormat:(NSOpenGLPixelFormat *)fmt private: (GstGLWindowPrivate *) priv {
|
|
|
|
|
|
|
|
self = [super initWithFrame: contentRect pixelFormat: fmt];
|
|
|
|
|
|
|
|
m_priv = priv;
|
2009-08-03 08:13:02 +00:00
|
|
|
m_resizeCount = 0;
|
|
|
|
|
|
|
|
[self setWantsLayer:NO];
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)reshape {
|
2009-08-03 08:13:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (m_resizeCount % 5 == 0) {
|
|
|
|
m_resizeCount = 0;
|
|
|
|
if (m_priv->resize_cb) {
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-08-03 08:13:02 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
NSRect bounds = [self bounds];
|
|
|
|
AppThreadPerformer* app_thread_performer = [[AppThreadPerformer alloc]
|
|
|
|
initWithSize:m_priv->resize_cb userData:m_priv->resize_data
|
|
|
|
toSize:bounds.size private:m_priv];
|
|
|
|
|
|
|
|
[app_thread_performer performSelector:@selector(resizeWindow) onThread:m_priv->thread
|
|
|
|
withObject:nil waitUntilDone:YES];
|
2009-07-17 14:47:41 +00:00
|
|
|
|
2009-08-03 08:13:02 +00:00
|
|
|
[pool release];
|
|
|
|
}
|
2009-07-17 14:47:41 +00:00
|
|
|
}
|
2009-08-03 08:13:02 +00:00
|
|
|
m_resizeCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) update {
|
2009-07-17 14:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
/* =============================================================*/
|
|
|
|
/* */
|
|
|
|
/* AppThreadPerformer implementation */
|
|
|
|
/* */
|
|
|
|
/* =============================================================*/
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
@implementation AppThreadPerformer
|
|
|
|
|
|
|
|
- (id) initWithPrivate: (GstGLWindowPrivate *) priv {
|
|
|
|
m_priv = priv;
|
|
|
|
m_callback = NULL;
|
2009-07-17 14:47:41 +00:00
|
|
|
m_callback2 = NULL;
|
2009-02-26 00:06:58 +00:00
|
|
|
m_data = NULL;
|
2009-07-17 14:47:41 +00:00
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
2009-02-26 00:06:58 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCallback: (GstGLWindowCB) callback userData: (gpointer) data {
|
|
|
|
m_priv = NULL;
|
|
|
|
m_callback = callback;
|
2009-07-17 14:47:41 +00:00
|
|
|
m_callback2 = NULL;
|
|
|
|
m_data = data;
|
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithSize: (GstGLWindowCB2) callback userData: (gpointer) data
|
|
|
|
toSize: (NSSize) size private: (GstGLWindowPrivate *) priv {
|
|
|
|
m_priv = priv;
|
|
|
|
m_callback = NULL;
|
|
|
|
m_callback2 = callback;
|
2009-02-26 00:06:58 +00:00
|
|
|
m_data = data;
|
2009-07-17 14:47:41 +00:00
|
|
|
m_width = size.width;
|
|
|
|
m_height = size.height;
|
2009-02-26 00:06:58 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
- (id) initWithAll: (GstGLWindowCB) callback userData: (gpointer) data
|
|
|
|
private: (GstGLWindowPrivate *) priv {
|
2009-02-26 00:06:58 +00:00
|
|
|
m_priv = priv;
|
|
|
|
m_callback = callback;
|
2009-07-17 14:47:41 +00:00
|
|
|
m_callback2 = NULL;
|
2009-02-26 00:06:58 +00:00
|
|
|
m_data = data;
|
2009-07-17 14:47:41 +00:00
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
2009-02-26 00:06:58 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) updateWindow {
|
2009-07-17 14:47:41 +00:00
|
|
|
if (m_priv->running) {
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
if (![m_priv->internal_win_id isClosed]) {
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2009-08-03 08:13:02 +00:00
|
|
|
/* draw opengl scene in the back buffer */
|
|
|
|
m_priv->draw_cb (m_priv->draw_data);
|
|
|
|
/* Copy the back buffer to the front buffer */
|
|
|
|
[[[m_priv->internal_win_id contentView] openGLContext] flushBuffer];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
- (void) resizeWindow {
|
2009-07-24 08:12:07 +00:00
|
|
|
if (m_priv->running && ![m_priv->internal_win_id isClosed]) {
|
2009-07-17 14:47:41 +00:00
|
|
|
m_callback2 (m_data, m_width, m_height);
|
2009-08-03 08:13:02 +00:00
|
|
|
[[[m_priv->internal_win_id contentView] openGLContext] update];
|
|
|
|
m_priv->draw_cb (m_priv->draw_data);
|
|
|
|
[[[m_priv->internal_win_id contentView] openGLContext] flushBuffer];
|
2009-07-24 08:12:07 +00:00
|
|
|
}
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
- (void) sendToApp {
|
2009-07-24 08:12:07 +00:00
|
|
|
if (m_priv->running && ![m_priv->internal_win_id isClosed])
|
2009-07-17 14:47:41 +00:00
|
|
|
m_callback (m_data);
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
- (void) setWindow {
|
2009-02-26 00:06:58 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2009-07-17 14:47:41 +00:00
|
|
|
NSWindow *window = m_priv->parent;
|
|
|
|
|
|
|
|
[m_priv->internal_win_id orderOut:m_priv->internal_win_id];
|
|
|
|
|
2009-07-24 08:12:07 +00:00
|
|
|
[window setContentView: [m_priv->internal_win_id contentView]];
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2009-07-17 14:47:41 +00:00
|
|
|
- (void) stopApp {
|
|
|
|
m_priv->running = FALSE;
|
2009-07-24 08:12:07 +00:00
|
|
|
m_callback (m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) closeWindow {
|
|
|
|
[m_priv->internal_win_id setClosed];
|
|
|
|
if (m_priv->close_cb) {
|
|
|
|
m_priv->close_cb (m_priv->close_data);
|
|
|
|
}
|
|
|
|
m_priv->draw_cb = NULL;
|
|
|
|
m_priv->draw_data = NULL;
|
|
|
|
m_priv->resize_cb = NULL;
|
|
|
|
m_priv->resize_data = NULL;
|
|
|
|
m_priv->close_cb = NULL;
|
|
|
|
m_priv->close_data = NULL;
|
2009-07-17 14:47:41 +00:00
|
|
|
}
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
@end
|