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-10-26 10:44:35 +00:00
|
|
|
NSOpenGLContext *gl_context;
|
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;
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
void gst_gl_window_init_platform ()
|
|
|
|
{
|
|
|
|
#ifndef GNUSTEP
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
[NSApplication sharedApplication];
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
/* 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-27 07:58:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_gl_window_nsapp_iteration (gpointer data)
|
2010-01-05 23:13:46 +00:00
|
|
|
{
|
2009-07-27 07:58:20 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
NSEvent *event = nil;
|
|
|
|
|
2009-07-27 07:58:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
[pool release];
|
|
|
|
|
2009-07-27 07:58:20 +00:00
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called in the gl thread */
|
|
|
|
GstGLWindow *
|
2012-08-27 14:08:16 +00:00
|
|
|
gst_gl_window_new (guintptr 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;
|
|
|
|
NSRect rect;
|
|
|
|
|
2009-10-26 10:44:35 +00:00
|
|
|
NSAutoreleasePool *pool = nil;
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
priv->internal_win_id = nil;
|
2009-10-26 10:44:35 +00:00
|
|
|
priv->gl_context = 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];
|
2010-01-05 23:13:46 +00:00
|
|
|
|
|
|
|
#ifdef GNUSTEP
|
|
|
|
[NSApplication sharedApplication];
|
|
|
|
#endif
|
2009-02-26 00:06:58 +00:00
|
|
|
|
2009-03-07 02:13:38 +00:00
|
|
|
rect.origin.x = 0;
|
|
|
|
rect.origin.y = 0;
|
2010-01-05 23:13:46 +00:00
|
|
|
rect.size.width = 320;
|
|
|
|
rect.size.height = 240;
|
2009-02-26 00:06:58 +00:00
|
|
|
|
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-10-26 10:44:35 +00:00
|
|
|
g_debug ("NSWindow id: %lud\n", (gulong) priv->internal_win_id);
|
2009-03-07 02:13:38 +00:00
|
|
|
|
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
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifndef GNUSTEP
|
|
|
|
priv->source_id = g_timeout_add_seconds (1, gst_gl_window_nsapp_iteration, NULL);
|
|
|
|
#endif
|
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");
|
|
|
|
}
|
|
|
|
|
2012-08-27 14:08:16 +00:00
|
|
|
guintptr
|
2009-10-26 10:44:35 +00:00
|
|
|
gst_gl_window_get_internal_gl_context (GstGLWindow * window)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
2012-08-27 14:08:16 +00:00
|
|
|
return (guintptr) priv->gl_context;
|
2009-10-26 10:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
callback_activate_gl_context (GstGLWindowPrivate * priv)
|
|
|
|
{
|
|
|
|
[priv->gl_context makeCurrentContext];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
callback_inactivate_gl_context (GstGLWindowPrivate * priv)
|
|
|
|
{
|
2010-01-05 23:13:46 +00:00
|
|
|
//FIXME: [priv->gl_context clearCurrentContext];
|
2009-10-26 10:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_gl_window_activate_gl_context (GstGLWindow * window, gboolean activate)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
if (activate)
|
|
|
|
gst_gl_window_send_message (window,
|
|
|
|
GST_GL_WINDOW_CB (callback_activate_gl_context), priv);
|
|
|
|
else
|
|
|
|
gst_gl_window_send_message (window,
|
|
|
|
GST_GL_WINDOW_CB (callback_inactivate_gl_context), priv);
|
|
|
|
}
|
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
void
|
2012-08-27 14:08:16 +00:00
|
|
|
gst_gl_window_set_external_window_id (GstGLWindow * window, guintptr 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)
|
2010-01-05 23:13:46 +00:00
|
|
|
withObject:0 waitUntilDone:YES];
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
GSUnregisterCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("failed to register current thread, cannot set external window id\n");
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2009-10-26 10:44:35 +00:00
|
|
|
gst_gl_window_draw_unlocked (GstGLWindow * window, gint width, gint height)
|
2009-02-26 00:06:58 +00:00
|
|
|
{
|
2009-10-26 10:44:35 +00:00
|
|
|
gst_gl_window_draw (window, width, height);
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread safe */
|
|
|
|
void
|
2009-10-26 10:44:35 +00:00
|
|
|
gst_gl_window_draw (GstGLWindow * window, gint width, gint height)
|
2009-02-26 00:06:58 +00:00
|
|
|
{
|
|
|
|
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) {
|
2009-10-26 10:44:35 +00:00
|
|
|
static gint x = 0;
|
|
|
|
static gint y = 0;
|
|
|
|
|
|
|
|
NSRect mainRect = [[NSScreen mainScreen] visibleFrame];
|
|
|
|
NSRect windowRect = [priv->internal_win_id frame];
|
|
|
|
|
|
|
|
g_debug ("main screen rect: %d %d %d %d\n", (int) mainRect.origin.x, (int) mainRect.origin.y,
|
|
|
|
(int) mainRect.size.width, (int) mainRect.size.height);
|
|
|
|
|
|
|
|
windowRect.origin.x += x;
|
|
|
|
windowRect.origin.y += mainRect.size.height > y ? (mainRect.size.height - y) * 0.5 : y;
|
|
|
|
windowRect.size.width = width;
|
|
|
|
windowRect.size.height = height;
|
|
|
|
[priv->internal_win_id setFrame:windowRect display:NO];
|
|
|
|
|
|
|
|
g_debug ("window rect: %d %d %d %d\n", (int) windowRect.origin.x, (int) windowRect.origin.y,
|
|
|
|
(int) windowRect.size.width, (int) windowRect.size.height);
|
|
|
|
|
|
|
|
x += 20;
|
|
|
|
y += 20;
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifndef GNUSTEP
|
|
|
|
g_debug ("make the window available\n");
|
2009-07-24 08:12:07 +00:00
|
|
|
[priv->internal_win_id makeMainWindow];
|
2010-01-05 23:13:46 +00:00
|
|
|
#endif
|
2009-07-24 08:12:07 +00:00
|
|
|
[priv->internal_win_id orderFront:priv->internal_win_id];
|
2010-01-05 23:13:46 +00:00
|
|
|
//[priv->internal_win_id setViewsNeedDisplay:YES];
|
2009-07-24 08:12:07 +00:00
|
|
|
priv->visible = TRUE;
|
2009-08-03 08:13:02 +00:00
|
|
|
}
|
2009-02-26 00:06:58 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
|
|
|
GSUnregisterCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("failed to register current thread, cannot draw\n");
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_gl_window_run_loop (GstGLWindow * window)
|
|
|
|
{
|
|
|
|
GstGLWindowPrivate *priv = window->priv;
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifndef GNUSTEP
|
2009-07-17 14:47:41 +00:00
|
|
|
NSRunLoop *run_loop = [NSRunLoop currentRunLoop];
|
2010-01-05 23:13:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
g_debug ("begin loop\n");
|
2009-07-27 07:58:20 +00:00
|
|
|
|
2009-02-26 00:06:58 +00:00
|
|
|
if (priv->internal_win_id != nil) {
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifndef GNUSTEP
|
|
|
|
while (priv->running)
|
2009-08-03 08:13:02 +00:00
|
|
|
[run_loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
2010-01-05 23:13:46 +00:00
|
|
|
#else
|
|
|
|
[NSApp run];
|
|
|
|
#endif
|
|
|
|
|
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) {
|
2010-01-05 23:13:46 +00:00
|
|
|
if (GSRegisterCurrentThread() || 1) {
|
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
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("failed to register current thread, application thread is lost\n");
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("failed to register current thread, cannot send message\n");
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("initializing GstGLNSWindow\n");
|
2009-02-26 00:06:58 +00:00
|
|
|
|
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];
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifndef GNUSTEP
|
2009-07-17 14:47:41 +00:00
|
|
|
glContext = [[NSOpenGLContext alloc] initWithFormat:fmt
|
|
|
|
shareContext:m_priv->external_gl_context];
|
2009-10-26 10:44:35 +00:00
|
|
|
|
|
|
|
g_debug ("NSOpenGL context created: %lud\n", (gulong) glContext);
|
|
|
|
|
|
|
|
priv->gl_context = glContext;
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
[glView setOpenGLContext:glContext];
|
2010-01-05 23:13:46 +00:00
|
|
|
#else
|
|
|
|
glContext = [glView openGLContext];
|
|
|
|
#endif
|
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) {
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifdef GNUSTEP
|
|
|
|
const long swapInterval = 1;
|
|
|
|
#else
|
|
|
|
const GLint swapInterval = 1;
|
|
|
|
#endif
|
|
|
|
[[glView openGLContext] setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
} NS_HANDLER {
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("your back-end does not implement NSOpenglContext::setValues\n");
|
2009-02-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
g_debug ("opengl GstGLNSWindow initialized: %d x %d\n",
|
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];
|
2010-01-05 23:13:46 +00:00
|
|
|
|
|
|
|
g_debug ("user clicked the close button\n");
|
2009-07-24 08:12:07 +00:00
|
|
|
|
|
|
|
[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;
|
|
|
|
|
2010-01-05 23:13:46 +00:00
|
|
|
#ifndef GNUSTEP
|
2009-08-03 08:13:02 +00:00
|
|
|
[self setWantsLayer:NO];
|
2010-01-05 23:13:46 +00:00
|
|
|
#endif
|
2009-07-17 14:47:41 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reshape {
|
2010-01-05 23:13:46 +00:00
|
|
|
|
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);
|
2010-01-05 23:13:46 +00:00
|
|
|
|
|
|
|
#ifdef GNUSTEP
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
if ([NSApp isRunning])
|
|
|
|
[NSApp stop:self];
|
|
|
|
[pool release];
|
|
|
|
#endif
|
2009-07-24 08:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|