gl/cocoa: move to CGL and CAOpenGLLayer for rendering

Removes the use of NSOpenGL* variety and functions.  Any Cocoa
specific functions that took/returned a NSOpenGL* object now
take/return the CGL equivalents.
This commit is contained in:
Matthew Waters 2015-01-20 22:01:39 +11:00
parent fe8fdbdc7c
commit e10d2417e2
8 changed files with 328 additions and 106 deletions

View file

@ -1007,7 +1007,7 @@ case $host in
fi
if test "x$NEED_COCOA" != "xno"; then
GL_LIBS="$LIBS -framework OpenGL -framework Cocoa"
GL_LIBS="$LIBS -framework OpenGL -framework Cocoa -framework QuartzCore -framework CoreFoundation"
GL_CFLAGS="$GL_CFLAGS"
USE_COCOA=yes
HAVE_WINDOW_COCOA=yes

View file

@ -4,7 +4,8 @@ noinst_LTLIBRARIES = libgstgl-cocoa.la
libgstgl_cocoa_la_SOURCES = \
gstglwindow_cocoa.m \
gstglcontext_cocoa.m
gstglcontext_cocoa.m \
gstglcaopengllayer.m
noinst_HEADERS = \
gstglwindow_cocoa.h \

View file

@ -32,8 +32,9 @@ G_BEGIN_DECLS
struct _GstGLContextCocoaPrivate
{
NSOpenGLContext *gl_context;
NSOpenGLContext *external_gl_context;
CGLContextObj gl_context;
CGLPixelFormatObj pixel_format;
CGLContextObj external_gl_context;
gint source_id;
};
@ -44,10 +45,20 @@ struct _GstGLContextCocoaPrivate
/* */
/* =============================================================*/
@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);

View file

@ -0,0 +1,141 @@
/*
* 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

View file

@ -62,6 +62,9 @@ GType gst_gl_context_cocoa_get_type (void);
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

View file

@ -211,6 +211,76 @@ gst_gl_context_cocoa_new (void)
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)
@ -219,7 +289,7 @@ gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
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);
@ -227,21 +297,25 @@ gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
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];
@ -253,56 +327,55 @@ gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
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 (priv->external_gl_context) {
fmt = CGLGetPixelFormat (priv->external_gl_context);
}
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;
}
}
gst_gl_context_cocoa_dump_pixel_format (fmt);
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);
GST_WARNING ("cannot create NSOpenGLPixelFormat");
return;
}
glView = [[GstGLNSView alloc] initWithFrame:window_cocoa rect:rect];
[window_handle setContentView:glView];
glContext = [[NSOpenGLContext alloc] initWithFormat:fmt
shareContext:context_cocoa->priv->external_gl_context];
GST_DEBUG ("NSOpenGL context created: %"G_GUINTPTR_FORMAT, (guintptr) glContext);
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);
@ -331,15 +404,10 @@ gst_gl_context_cocoa_get_gl_context (GstGLContext * context)
static gboolean
gst_gl_context_cocoa_activate (GstGLContext * context, gboolean activate)
{
GstGLContextCocoa *context_cocoa;
GstGLContextCocoa *context_cocoa = GST_GL_CONTEXT_COCOA (context);
gpointer context_handle = activate ? context_cocoa->priv->gl_context : NULL;
context_cocoa = GST_GL_CONTEXT_COCOA (context);
if (activate)
[context_cocoa->priv->gl_context makeCurrentContext];
else
[NSOpenGLContext clearCurrentContext];
return TRUE;
return kCGLNoError == CGLSetCurrentContext (context_handle);
}
static GstGLAPI
@ -357,5 +425,5 @@ gst_gl_context_cocoa_get_gl_platform (GstGLContext * context)
guintptr
gst_gl_context_cocoa_get_current_context (void)
{
return (guintptr) [NSOpenGLContext currentContext];
return (guintptr) CGLGetCurrentContext ();
}

View file

@ -60,6 +60,10 @@ GType gst_gl_window_cocoa_get_type (void);
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__ */

View file

@ -211,18 +211,9 @@ gst_gl_window_cocoa_set_window_handle (GstGLWindow * window, guintptr handle)
}
}
/* Thread safe */
struct draw
void
gst_gl_window_cocoa_draw_thread (GstGLWindowCocoa *window_cocoa, guint width, guint height)
{
GstGLWindowCocoa *window;
guint width, height;
};
static void
draw_cb (gpointer data)
{
struct draw *draw_data = data;
GstGLWindowCocoa *window_cocoa = draw_data->window;
GstGLWindowCocoaPrivate *priv = window_cocoa->priv;
/* useful when set_window_handle is called before
@ -233,51 +224,41 @@ draw_cb (gpointer data)
}
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);
}
}
}
@ -285,13 +266,15 @@ draw_cb (gpointer data)
static void
gst_gl_window_cocoa_draw (GstGLWindow * window, guint width, guint height)
{
struct draw draw_data;
GstGLWindowCocoa *window_cocoa = GST_GL_WINDOW_COCOA (window);
GstGLNSView *view = (GstGLNSView *)[window_cocoa->priv->internal_win_id contentView];
draw_data.window = GST_GL_WINDOW_COCOA (window);
draw_data.width = width;
draw_data.height = height;
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
@ -385,7 +368,7 @@ gst_gl_window_cocoa_send_message_async (GstGLWindow * window,
[self setTitle:@"OpenGL renderer"];
[self setBackgroundColor:[NSColor clearColor]];
[self setBackgroundColor:[NSColor blackColor]];
[self orderOut:window_cocoa->priv->internal_win_id];
@ -453,13 +436,22 @@ close_window_cb (gpointer data)
@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];
@ -471,6 +463,8 @@ close_window_cb (gpointer data)
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self];
[self->layer release];
[super dealloc];
}
@ -488,12 +482,10 @@ resize_cb (gpointer data)
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;
@ -506,8 +498,10 @@ resize_cb (gpointer data)
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];