libgstgl_cocoa_la_SOURCES = \
gstglwindow_cocoa.m \
- gstglcontext_cocoa.m
+ gstglcontext_cocoa.m \
+ gstglcaopengllayer.m
noinst_HEADERS = \
gstglwindow_cocoa.h \
struct _GstGLContextCocoaPrivate
{
- NSOpenGLContext *gl_context;
- NSOpenGLContext *external_gl_context;
+ CGLContextObj gl_context;
+ CGLPixelFormatObj pixel_format;
+ CGLContextObj external_gl_context;
gint source_id;
};
/* */
/* =============================================================*/
+@interface GstGLCAOpenGLLayer : CAOpenGLLayer {
+ GstGLContextCocoa *gst_gl_context;
+ CGLContextObj gl_context;
+ gint expected_dims[4];
+}
+- (id)initWithGstGLContext:(GstGLContextCocoa *)context;
+- (void)resize:(NSRect)bounds;
+@end
+
@interface GstGLNSView: NSView {
GstGLWindowCocoa *window_cocoa;
+ GstGLCAOpenGLLayer *layer;
}
-- (id) initWithFrame:(GstGLWindowCocoa *)window rect:(NSRect)contentRect;
+- (id) initWithFrameLayer:(GstGLWindowCocoa *)window rect:(NSRect)contentRect layer:(CALayer *)layerContent;
@end
gboolean gst_gl_window_cocoa_create_window (GstGLWindowCocoa *window_cocoa, NSRect rect);
--- /dev/null
+/*
+ * GStreamer
+ * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * 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., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <Cocoa/Cocoa.h>
+
+#include "gstgl_cocoa_private.h"
+
+@implementation GstGLCAOpenGLLayer
+- (void)dealloc {
+ GST_TRACE ("dealloc GstGLCAOpenGLLayer %p context %p", self, self->gst_gl_context);
+
+ [super dealloc];
+}
+
+- (id)initWithGstGLContext:(GstGLContextCocoa *)parent_gl_context {
+ [super init];
+
+ GST_TRACE ("init CAOpenGLLayer");
+
+ self->gst_gl_context = parent_gl_context;
+
+ return self;
+}
+
+- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
+ CGLPixelFormatObj fmt = NULL;
+
+ if (self->gst_gl_context)
+ fmt = gst_gl_context_cocoa_get_pixel_format (self->gst_gl_context);
+
+ if (!fmt) {
+ CGLPixelFormatAttribute attribs[] = {
+ kCGLPFADoubleBuffer,
+ kCGLPFAAccumSize, 32,
+ 0
+ };
+ CGLError ret;
+ gint npix = 0;
+
+ GST_DEBUG ("creating new pixel format for CAOpenGLLayer %p", self);
+
+ ret = CGLChoosePixelFormat (attribs, &fmt, &npix);
+ if (ret != kCGLNoError) {
+ GST_ERROR ("CAOpenGLLayer cannot choose a pixel format: %s", CGLErrorString (ret));
+ }
+ }
+
+ return fmt;
+}
+
+- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
+ CGLContextObj external_context = NULL;
+ CGLError ret;
+
+ if (self->gst_gl_context)
+ external_context = (CGLContextObj) gst_gl_context_get_gl_context (GST_GL_CONTEXT (self->gst_gl_context));
+
+ GST_INFO ("attempting to create CGLContext for CAOpenGLLayer with "
+ "share context %p", external_context);
+
+ ret = CGLCreateContext (pixelFormat, external_context, &self->gl_context);
+ if (ret != kCGLNoError) {
+ GST_ERROR ("failed to create CGL context in CAOpenGLLayer with share context %p: %s", external_context, CGLErrorString(ret));
+ }
+
+ return self->gl_context;
+}
+
+- (void)resize:(NSRect)bounds {
+ const GstGLFuncs *gl = ((GstGLContext *)self->gst_gl_context)->gl_vtable;
+
+ gl->GetIntegerv (GL_VIEWPORT, self->expected_dims);
+}
+
+- (void)releaseCGLContext:(CGLContextObj)glContext {
+ CGLReleaseContext (glContext);
+}
+
+- (void)drawInCGLContext:(CGLContextObj)glContext
+ pixelFormat:(CGLPixelFormatObj)pixelFormat
+ forLayerTime:(CFTimeInterval)interval
+ displayTime:(const CVTimeStamp *)timeStamp {
+ GstGLWindow *window = gst_gl_context_get_window (GST_GL_CONTEXT (self->gst_gl_context));
+ const GstGLFuncs *gl = ((GstGLContext *)self->gst_gl_context)->gl_vtable;
+ gint ca_viewport[4];
+
+ GST_LOG ("CAOpenGLLayer drawing with window %p cgl context %p", window, glContext);
+
+ /* attempt to get the correct viewport back due to CA being too smart
+ * and messing around with it so center the expected viewport into
+ * the CA viewport set up on entry to this function */
+ gl->GetIntegerv (GL_VIEWPORT, ca_viewport);
+
+ GstVideoRectangle src, dst, result;
+
+ src.x = self->expected_dims[0];
+ src.y = self->expected_dims[1];
+ src.w = self->expected_dims[2];
+ src.h = self->expected_dims[3];
+
+ dst.x = ca_viewport[0];
+ dst.y = ca_viewport[1];
+ dst.w = ca_viewport[2];
+ dst.h = ca_viewport[3];
+
+ gst_video_sink_center_rect (src, dst, &result, TRUE);
+
+ gl->Viewport (result.x, result.y, result.w, result.h);
+
+ if (window) {
+ gst_gl_window_cocoa_draw_thread (GST_GL_WINDOW_COCOA (window), 320, 240);
+
+ gst_object_unref (window);
+ }
+
+ /* flushes the buffer */
+ [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:interval displayTime:timeStamp];
+}
+
+@end
GstGLContextCocoa * gst_gl_context_cocoa_new (void);
guintptr gst_gl_context_cocoa_get_current_context (void);
+CGLPixelFormatObj gst_gl_context_cocoa_get_pixel_format (GstGLContextCocoa *context);
+void gst_gl_context_cocoa_dump_pixel_format (CGLPixelFormatObj fmt);
+
G_END_DECLS
return context;
}
+struct pixel_attr
+{
+ CGLPixelFormatAttribute attr;
+ const gchar *attr_name;
+};
+
+static struct pixel_attr pixel_attrs[] = {
+ {kCGLPFAAllRenderers, "All Renderers"},
+ {kCGLPFADoubleBuffer, "Double Buffered"},
+ {kCGLPFAStereo, "Stereo"},
+ {kCGLPFAAuxBuffers, "Aux Buffers"},
+ {kCGLPFAColorSize, "Color Size"},
+ {kCGLPFAAlphaSize, "Alpha Size"},
+ {kCGLPFADepthSize, "Depth Size"},
+ {kCGLPFAStencilSize, "Stencil Size"},
+ {kCGLPFAAccumSize, "Accum Size"},
+ {kCGLPFAMinimumPolicy, "Minimum Policy"},
+ {kCGLPFAMaximumPolicy, "Maximum Policy"},
+// {kCGLPFAOffScreen, "Off Screen"},
+// {kCGLPFAFullScreen, "Full Screen"},
+ {kCGLPFASampleBuffers, "Sample Buffers"},
+ {kCGLPFASamples, "Samples"},
+ {kCGLPFAAuxDepthStencil, "Aux Depth Stencil"},
+ {kCGLPFAColorFloat, "Color Float"},
+ {kCGLPFAMultisample, "Multisample"},
+ {kCGLPFASupersample, "Supersample"},
+ {kCGLPFARendererID, "Renderer ID"},
+ {kCGLPFASingleRenderer, "Single Renderer"},
+ {kCGLPFANoRecovery, "No Recovery"},
+ {kCGLPFAAccelerated, "Accelerated"},
+ {kCGLPFAClosestPolicy, "Closest Policy"},
+// {kCGLPFARobust, "Robust"},
+ {kCGLPFABackingStore, "Backing Store"},
+// {kCGLPFAMPSafe, "MP Safe"},
+ {kCGLPFAWindow, "Window"},
+// {kCGLPFAMultiScreen, "Multi Screen"},
+ {kCGLPFACompliant, "Compliant"},
+ {kCGLPFADisplayMask, "Display Mask"},
+// {kCGLPFAPBuffer, "PBuffer"},
+ {kCGLPFARemotePBuffer, "Remote PBuffer"},
+ {kCGLPFAAllowOfflineRenderers, "Allow Offline Renderers"},
+ {kCGLPFAAcceleratedCompute, "Accelerated Compute"},
+ {kCGLPFAOpenGLProfile, "OpenGL Profile"},
+ {kCGLPFAVirtualScreenCount, "Virtual Screen Count"},
+};
+
+void
+gst_gl_context_cocoa_dump_pixel_format (CGLPixelFormatObj fmt)
+{
+ int i;
+
+ for (i = 0; i < G_N_ELEMENTS (pixel_attrs); i++) {
+ gint val;
+ CGLError ret = CGLDescribePixelFormat (fmt, 0, pixel_attrs[i].attr, &val);
+
+ if (ret != kCGLNoError) {
+ GST_WARNING ("failed to get pixel format %p attribute %s", fmt, pixel_attrs[i].attr_name);
+ } else {
+ GST_DEBUG ("Pixel format %p attr %s = %i", fmt, pixel_attrs[i].attr_name,
+ val);
+ }
+ }
+}
+
+CGLPixelFormatObj
+gst_gl_context_cocoa_get_pixel_format (GstGLContextCocoa *context)
+{
+ return context->priv->pixel_format;
+}
+
static gboolean
gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
GstGLContext *other_context, GError **error)
GstGLContextCocoaPrivate *priv = context_cocoa->priv;
GstGLWindow *window = gst_gl_context_get_window (context);
GstGLWindowCocoa *window_cocoa = GST_GL_WINDOW_COCOA (window);
- __block NSOpenGLContext *glContext = nil;
+ const GLint swapInterval = 1;
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
priv->source_id = g_timeout_add (200, gst_gl_window_cocoa_nsapp_iteration, NULL);
priv->gl_context = nil;
if (other_context)
- priv->external_gl_context = (NSOpenGLContext *) gst_gl_context_get_gl_context (other_context);
+ priv->external_gl_context = (CGLContextObj) gst_gl_context_get_gl_context (other_context);
else
priv->external_gl_context = NULL;
dispatch_sync (dispatch_get_main_queue (), ^{
NSAutoreleasePool *pool;
- NSOpenGLPixelFormat *fmt = nil;
- GstGLNSView *glView = nil;
- NSOpenGLPixelFormatAttribute attribs[] = {
- NSOpenGLPFADoubleBuffer,
- NSOpenGLPFAAccumSize, 32,
+ CGLPixelFormatObj fmt = NULL;
+ GstGLNSView *glView = NULL;
+ GstGLCAOpenGLLayer *layer;
+ CGLContextObj glContext;
+ CGLPixelFormatAttribute attribs[] = {
+ kCGLPFADoubleBuffer,
+ kCGLPFAAccumSize, 32,
0
};
+ CGLError ret;
NSRect rect;
NSWindow *window_handle;
+ gint npix;
pool = [[NSAutoreleasePool alloc] init];
gst_gl_window_cocoa_create_window (window_cocoa, rect);
window_handle = (NSWindow *) gst_gl_window_get_window_handle (window);
- fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
- if (!fmt) {
- gst_object_unref (window);
- GST_WARNING ("cannot create NSOpenGLPixelFormat");
- return;
+ if (priv->external_gl_context) {
+ fmt = CGLGetPixelFormat (priv->external_gl_context);
}
- glView = [[GstGLNSView alloc] initWithFrame:window_cocoa rect:rect];
-
- [window_handle setContentView:glView];
+ if (!fmt) {
+ ret = CGLChoosePixelFormat (attribs, &fmt, &npix);
+ if (ret != kCGLNoError) {
+ gst_object_unref (window);
+ g_set_error (error, GST_GL_CONTEXT_ERROR,
+ GST_GL_CONTEXT_ERROR_WRONG_CONFIG, "cannot choose a pixel format: %s", CGLErrorString (ret));
+ return;
+ }
+ }
- glContext = [[NSOpenGLContext alloc] initWithFormat:fmt
- shareContext:context_cocoa->priv->external_gl_context];
+ gst_gl_context_cocoa_dump_pixel_format (fmt);
- GST_DEBUG ("NSOpenGL context created: %"G_GUINTPTR_FORMAT, (guintptr) glContext);
+ ret = CGLCreateContext (fmt, priv->external_gl_context, &glContext);
+ if (ret != kCGLNoError) {
+ g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_CREATE_CONTEXT,
+ "failed to create context: %s", CGLErrorString (ret));
+ gst_object_unref (window);
+ return;
+ }
+ context_cocoa->priv->pixel_format = fmt;
context_cocoa->priv->gl_context = glContext;
- [glContext setView:glView];
+ layer = [[GstGLCAOpenGLLayer alloc] initWithGstGLContext:context_cocoa];
+ glView = [[GstGLNSView alloc] initWithFrameLayer:window_cocoa rect:rect layer:layer];
+
+ [window_handle setContentView:glView];
[pool release];
});
- if (!glContext) {
+ if (!context_cocoa->priv->gl_context) {
g_source_remove (priv->source_id);
priv->source_id = 0;
return FALSE;
}
- /* OpenGL context is made current only one time threre.
- * Indeed, all OpenGL calls are made in only one thread,
- * the Application thread */
- [glContext makeCurrentContext];
+ GST_INFO_OBJECT (context, "GL context created: %p", context_cocoa->priv->gl_context);
- [glContext update];
+ CGLSetCurrentContext (context_cocoa->priv->gl_context);
/* 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) {
- const GLint swapInterval = 1;
- [glContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
- }
- } NS_HANDLER {
- GST_DEBUG ("your back-end does not implement NSOpenglContext::setValues\n");
- }
- NS_ENDHANDLER
-
- GST_DEBUG ("opengl GstGLNSWindow initialized");
+ CGLSetParameter (context_cocoa->priv->gl_context, kCGLCPSwapInterval, &swapInterval);
gst_object_unref (window);
static gboolean
gst_gl_context_cocoa_activate (GstGLContext * context, gboolean activate)
{
- GstGLContextCocoa *context_cocoa;
-
- context_cocoa = GST_GL_CONTEXT_COCOA (context);
+ GstGLContextCocoa *context_cocoa = GST_GL_CONTEXT_COCOA (context);
+ gpointer context_handle = activate ? context_cocoa->priv->gl_context : NULL;
- if (activate)
- [context_cocoa->priv->gl_context makeCurrentContext];
- else
- [NSOpenGLContext clearCurrentContext];
- return TRUE;
+ return kCGLNoError == CGLSetCurrentContext (context_handle);
}
static GstGLAPI
guintptr
gst_gl_context_cocoa_get_current_context (void)
{
- return (guintptr) [NSOpenGLContext currentContext];
+ return (guintptr) CGLGetCurrentContext ();
}
GstGLWindowCocoa * gst_gl_window_cocoa_new (void);
+void gst_gl_window_cocoa_draw_thread (GstGLWindowCocoa *window_cocoa,
+ guint width, guint height);
+
+
G_END_DECLS
#endif /* __GST_GL_WINDOW_COCOA_H__ */
}
}
-/* Thread safe */
-struct draw
-{
- GstGLWindowCocoa *window;
- guint width, height;
-};
-
-static void
-draw_cb (gpointer data)
+void
+gst_gl_window_cocoa_draw_thread (GstGLWindowCocoa *window_cocoa, guint width, guint height)
{
- struct draw *draw_data = data;
- GstGLWindowCocoa *window_cocoa = draw_data->window;
GstGLWindowCocoaPrivate *priv = window_cocoa->priv;
/* useful when set_window_handle is called before
}
if (!priv->external_view && !priv->visible) {
- dispatch_sync (dispatch_get_main_queue (), ^{
- NSRect mainRect = [[NSScreen mainScreen] visibleFrame];
- NSRect windowRect = [priv->internal_win_id frame];
- gint x = 0;
- gint y = 0;
+ NSRect mainRect = [[NSScreen mainScreen] visibleFrame];
+ NSRect windowRect = [priv->internal_win_id frame];
+ gint x = 0;
+ gint y = 0;
- GST_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);
+ GST_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 = draw_data->width;
- windowRect.size.height = draw_data->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;
- GST_DEBUG ("window rect: %d %d %d %d\n", (int) windowRect.origin.x,
- (int) windowRect.origin.y, (int) windowRect.size.width,
- (int) windowRect.size.height);
+ GST_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;
+ x += 20;
+ y += 20;
- [priv->internal_win_id setFrame:windowRect display:NO];
- GST_DEBUG ("make the window available\n");
- [priv->internal_win_id makeMainWindow];
+ [priv->internal_win_id setFrame:windowRect display:NO];
+ GST_DEBUG ("make the window available\n");
- [priv->internal_win_id orderFrontRegardless];
+ [priv->internal_win_id makeMainWindow];
+ [priv->internal_win_id orderFrontRegardless];
+ [priv->internal_win_id setViewsNeedDisplay:YES];
- [priv->internal_win_id setViewsNeedDisplay:YES];
- });
priv->visible = TRUE;
}
if (g_main_loop_is_running (priv->loop)) {
if (![priv->internal_win_id isClosed]) {
- GstGLContext *context = gst_gl_window_get_context (GST_GL_WINDOW (window_cocoa));
- NSOpenGLContext * glContext = (NSOpenGLContext *) gst_gl_context_get_gl_context (context);
-
/* draw opengl scene in the back buffer */
GST_GL_WINDOW (window_cocoa)->draw (GST_GL_WINDOW (window_cocoa)->draw_data);
-
- /* Copy the back buffer to the front buffer */
- [glContext flushBuffer];
-
- gst_object_unref (context);
}
}
}
static void
gst_gl_window_cocoa_draw (GstGLWindow * window, guint width, guint height)
{
- struct draw draw_data;
-
- draw_data.window = GST_GL_WINDOW_COCOA (window);
- draw_data.width = width;
- draw_data.height = height;
+ GstGLWindowCocoa *window_cocoa = GST_GL_WINDOW_COCOA (window);
+ GstGLNSView *view = (GstGLNSView *)[window_cocoa->priv->internal_win_id contentView];
- gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, &draw_data);
+ /* this redraws the GstGLCAOpenGLLayer which calls
+ * gst_gl_window_cocoa_draw_thread()
+ */
+ dispatch_sync (dispatch_get_main_queue(), ^{
+ [view setNeedsDisplay:YES];
+ });
}
static void
[self setTitle:@"OpenGL renderer"];
- [self setBackgroundColor:[NSColor clearColor]];
+ [self setBackgroundColor:[NSColor blackColor]];
[self orderOut:window_cocoa->priv->internal_win_id];
@implementation GstGLNSView
/* Must be called from the application main thread */
-- (id)initWithFrame:(GstGLWindowCocoa *)window rect:(NSRect)contentRect {
+- (id)initWithFrameLayer:(GstGLWindowCocoa *)window rect:(NSRect)contentRect layer:(CALayer *)layerContent {
self = [super initWithFrame: contentRect];
window_cocoa = window;
- [self setWantsLayer:NO];
+ /* The order of the next two calls matters. This creates a layer-hosted
+ * NSView. Calling setWantsLayer before setLayer will create a
+ * layer-backed NSView. See the apple developer documentation on the
+ * difference.
+ */
+ [self setLayer:layerContent];
+ [self setWantsLayer:YES];
+ self->layer = (GstGLCAOpenGLLayer *)layerContent;
+
+ [self setLayerContentsRedrawPolicy:NSViewLayerContentsRedrawOnSetNeedsDisplay];
/* Get notified about changes */
[self setPostsFrameChangedNotifications:YES];
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self];
+ [self->layer release];
+
[super dealloc];
}
GstGLWindowCocoa *window_cocoa = resize_data->window;
GstGLWindow *window = GST_GL_WINDOW (window_cocoa);
GstGLContext *context = gst_gl_window_get_context (window);
- NSOpenGLContext * glContext = (NSOpenGLContext *) gst_gl_context_get_gl_context (context);
if (g_main_loop_is_running (window_cocoa->priv->loop) && ![window_cocoa->priv->internal_win_id isClosed]) {
const GstGLFuncs *gl;
-
- [glContext update];
+ GstGLCAOpenGLLayer *gl_layer;
gl = context->gl_vtable;
window_cocoa->priv->viewport_dim[1] - resize_data->visibleRect.origin.y,
window_cocoa->priv->viewport_dim[2], window_cocoa->priv->viewport_dim[3]);
- GST_GL_WINDOW (window_cocoa)->draw (GST_GL_WINDOW (window_cocoa)->draw_data);
- [glContext flushBuffer];
+ gl_layer = ((GstGLNSView *)[window_cocoa->priv->internal_win_id contentView])->layer;
+ [gl_layer resize:resize_data->bounds];
+
+ gst_gl_window_draw (window, resize_data->bounds.size.width, resize_data->bounds.size.height);
}
gst_object_unref (context);
[pool release];