2013-08-15 07:09:04 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2012 Matthew Waters <ystreet00@gmail.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 "gstglcontext_cocoa.h"
|
|
|
|
#include "gstgl_cocoa_private.h"
|
|
|
|
|
|
|
|
static gboolean gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
|
2013-08-29 10:10:42 +00:00
|
|
|
GstGLContext * other_context, GError **error);
|
2013-09-25 02:26:57 +00:00
|
|
|
static void gst_gl_context_cocoa_destroy_context (GstGLContext *context);
|
2013-08-15 07:09:04 +00:00
|
|
|
static guintptr gst_gl_context_cocoa_get_gl_context (GstGLContext * window);
|
|
|
|
static gboolean gst_gl_context_cocoa_activate (GstGLContext * context, gboolean activate);
|
|
|
|
static GstGLAPI gst_gl_context_cocoa_get_gl_api (GstGLContext * context);
|
2014-02-10 21:57:29 +00:00
|
|
|
static GstGLPlatform gst_gl_context_cocoa_get_gl_platform (GstGLContext * context);
|
2013-08-15 07:09:04 +00:00
|
|
|
|
|
|
|
#define GST_GL_CONTEXT_COCOA_GET_PRIVATE(o) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE((o), GST_GL_TYPE_CONTEXT_COCOA, GstGLContextCocoaPrivate))
|
|
|
|
|
2015-01-22 05:08:11 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_gl_context_cocoa_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_context_cocoa_debug
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLContextCocoa, gst_gl_context_cocoa,
|
|
|
|
GST_GL_TYPE_CONTEXT, GST_DEBUG_CATEGORY_INIT (gst_gl_context_cocoa_debug, "glcontext_cocoa", 0, "Cocoa GL Context"); );
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-03-01 16:54:10 +00:00
|
|
|
static void
|
|
|
|
gst_gl_context_cocoa_class_init (GstGLContextCocoaClass * klass)
|
|
|
|
{
|
|
|
|
GstGLContextClass *context_class = (GstGLContextClass *) klass;
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GstGLContextCocoaPrivate));
|
|
|
|
|
|
|
|
context_class->destroy_context =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_destroy_context);
|
|
|
|
context_class->create_context =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_create_context);
|
|
|
|
context_class->get_gl_context =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_get_gl_context);
|
|
|
|
context_class->activate = GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_activate);
|
|
|
|
context_class->get_gl_api =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_get_gl_api);
|
|
|
|
context_class->get_gl_platform =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_gl_context_cocoa_get_gl_platform);
|
2013-08-15 07:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_context_cocoa_init (GstGLContextCocoa * context)
|
|
|
|
{
|
|
|
|
context->priv = GST_GL_CONTEXT_COCOA_GET_PRIVATE (context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called in the gl thread */
|
|
|
|
GstGLContextCocoa *
|
2015-07-24 06:11:38 +00:00
|
|
|
gst_gl_context_cocoa_new (GstGLDisplay * display)
|
2013-08-15 07:09:04 +00:00
|
|
|
{
|
2015-07-24 06:11:38 +00:00
|
|
|
if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_COCOA) == 0)
|
|
|
|
/* we require an cocoa display to create CGL contexts */
|
|
|
|
return NULL;
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-07-24 06:11:38 +00:00
|
|
|
return g_object_new (GST_GL_TYPE_CONTEXT_COCOA, NULL);
|
2013-08-15 07:09:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
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"},
|
|
|
|
{kCGLPFASampleBuffers, "Sample Buffers"},
|
|
|
|
{kCGLPFASamples, "Samples"},
|
|
|
|
{kCGLPFAAuxDepthStencil, "Aux Depth Stencil"},
|
|
|
|
{kCGLPFAColorFloat, "Color Float"},
|
|
|
|
{kCGLPFAMultisample, "Multisample"},
|
|
|
|
{kCGLPFASupersample, "Supersample"},
|
|
|
|
{kCGLPFARendererID, "Renderer ID"},
|
|
|
|
{kCGLPFANoRecovery, "No Recovery"},
|
|
|
|
{kCGLPFAAccelerated, "Accelerated"},
|
|
|
|
{kCGLPFAClosestPolicy, "Closest Policy"},
|
|
|
|
{kCGLPFABackingStore, "Backing Store"},
|
|
|
|
{kCGLPFADisplayMask, "Display Mask"},
|
|
|
|
{kCGLPFAAllowOfflineRenderers, "Allow Offline Renderers"},
|
|
|
|
{kCGLPFAAcceleratedCompute, "Accelerated Compute"},
|
|
|
|
{kCGLPFAOpenGLProfile, "OpenGL Profile"},
|
|
|
|
{kCGLPFAVirtualScreenCount, "Virtual Screen Count"},
|
2015-02-26 23:04:09 +00:00
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1090
|
|
|
|
{kCGLPFACompliant, "Compliant"},
|
|
|
|
{kCGLPFARemotePBuffer, "Remote PBuffer"},
|
|
|
|
{kCGLPFASingleRenderer, "Single Renderer"},
|
|
|
|
{kCGLPFAWindow, "Window"},
|
|
|
|
#endif
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
|
|
|
|
// {kCGLPFAOffScreen, "Off Screen"},
|
|
|
|
// {kCGLPFAPBuffer, "PBuffer"},
|
|
|
|
#endif
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
|
|
|
|
// {kCGLPFAFullScreen, "Full Screen"},
|
|
|
|
#endif
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
|
|
|
|
// {kCGLPFAMPSafe, "MP Safe"},
|
|
|
|
// {kCGLPFAMultiScreen, "Multi Screen"},
|
|
|
|
// {kCGLPFARobust, "Robust"},
|
|
|
|
#endif
|
2015-01-20 11:01:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-15 07:09:04 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
|
2013-08-29 10:10:42 +00:00
|
|
|
GstGLContext *other_context, GError **error)
|
2013-08-15 07:09:04 +00:00
|
|
|
{
|
|
|
|
GstGLContextCocoa *context_cocoa = GST_GL_CONTEXT_COCOA (context);
|
|
|
|
GstGLContextCocoaPrivate *priv = context_cocoa->priv;
|
|
|
|
GstGLWindow *window = gst_gl_context_get_window (context);
|
|
|
|
GstGLWindowCocoa *window_cocoa = GST_GL_WINDOW_COCOA (window);
|
2015-01-22 07:00:36 +00:00
|
|
|
GstGLAPI context_api = GST_GL_API_NONE;
|
2015-01-20 11:01:39 +00:00
|
|
|
const GLint swapInterval = 1;
|
2015-03-01 16:54:10 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2015-02-17 07:17:59 +00:00
|
|
|
CGLPixelFormatObj fmt = NULL;
|
|
|
|
CGLContextObj glContext;
|
|
|
|
CGLPixelFormatAttribute attribs[] = {
|
|
|
|
kCGLPFADoubleBuffer,
|
|
|
|
kCGLPFAAccumSize, 32,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
CGLError ret;
|
2015-01-22 07:00:36 +00:00
|
|
|
gint pix_fmt_i = 0;
|
2015-02-17 07:17:59 +00:00
|
|
|
gint npix;
|
|
|
|
|
2015-01-22 07:00:36 +00:00
|
|
|
if ((gl_api & (GST_GL_API_OPENGL | GST_GL_API_OPENGL3)) == GST_GL_API_NONE) {
|
|
|
|
g_set_error (error, GST_GL_CONTEXT_ERROR,
|
|
|
|
GST_GL_CONTEXT_ERROR_CREATE_CONTEXT,
|
|
|
|
"The CGL backend only supports GL and GL3");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-08-15 07:09:04 +00:00
|
|
|
priv->gl_context = nil;
|
2013-08-29 10:10:42 +00:00
|
|
|
if (other_context)
|
2015-01-20 11:01:39 +00:00
|
|
|
priv->external_gl_context = (CGLContextObj) gst_gl_context_get_gl_context (other_context);
|
2013-08-29 10:10:42 +00:00
|
|
|
else
|
|
|
|
priv->external_gl_context = NULL;
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
if (priv->external_gl_context) {
|
2015-01-22 07:00:36 +00:00
|
|
|
gint profile;
|
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
fmt = CGLGetPixelFormat (priv->external_gl_context);
|
2015-01-22 07:00:36 +00:00
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
|
|
|
|
/* core profile is only available in >= 10.7 */
|
|
|
|
if (kCGLNoError == CGLDescribePixelFormat (fmt, 0, kCGLPFAOpenGLProfile,
|
|
|
|
&profile)) {
|
|
|
|
if (profile == kCGLOGLPVersion_3_2_Core) {
|
|
|
|
context_api = GST_GL_API_OPENGL3;
|
|
|
|
} else {
|
|
|
|
context_api =GST_GL_API_OPENGL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
context_api = GST_GL_API_OPENGL;
|
|
|
|
#endif
|
2015-02-17 07:17:59 +00:00
|
|
|
}
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
if (!fmt) {
|
2015-01-22 07:00:36 +00:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
|
|
|
|
if (gl_api & GST_GL_API_OPENGL3) {
|
|
|
|
attribs[pix_fmt_i++] = kCGLPFAOpenGLProfile;
|
|
|
|
attribs[pix_fmt_i++] = (int) kCGLOGLPVersion_3_2_Core;
|
|
|
|
context_api = GST_GL_API_OPENGL3;
|
|
|
|
} else {
|
|
|
|
context_api = GST_GL_API_OPENGL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
context_api = GST_GL_API_OPENGL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
attribs[pix_fmt_i++] = 0;
|
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
ret = CGLChoosePixelFormat (attribs, &fmt, &npix);
|
2015-01-20 11:01:39 +00:00
|
|
|
if (ret != kCGLNoError) {
|
2015-02-17 07:17:59 +00:00
|
|
|
g_set_error (error, GST_GL_CONTEXT_ERROR,
|
|
|
|
GST_GL_CONTEXT_ERROR_WRONG_CONFIG, "cannot choose a pixel format: %s", CGLErrorString (ret));
|
|
|
|
goto error;
|
2015-01-20 11:01:39 +00:00
|
|
|
}
|
2015-02-17 07:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_gl_context_cocoa_dump_pixel_format (fmt);
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
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));
|
|
|
|
goto error;
|
|
|
|
}
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
context_cocoa->priv->pixel_format = fmt;
|
|
|
|
context_cocoa->priv->gl_context = glContext;
|
2014-04-24 07:49:54 +00:00
|
|
|
|
2015-02-17 07:17:59 +00:00
|
|
|
_invoke_on_main ((GstGLWindowCB) gst_gl_window_cocoa_create_window,
|
|
|
|
window_cocoa);
|
2014-09-26 11:21:46 +00:00
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
if (!context_cocoa->priv->gl_context) {
|
2015-02-17 07:17:59 +00:00
|
|
|
goto error;
|
2014-09-26 11:21:46 +00:00
|
|
|
}
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
GST_INFO_OBJECT (context, "GL context created: %p", context_cocoa->priv->gl_context);
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
CGLSetCurrentContext (context_cocoa->priv->gl_context);
|
2013-08-15 07:09:04 +00:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2015-01-20 11:01:39 +00:00
|
|
|
CGLSetParameter (context_cocoa->priv->gl_context, kCGLCPSwapInterval, &swapInterval);
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-01-22 07:00:36 +00:00
|
|
|
context_cocoa->priv->context_api = context_api;
|
|
|
|
|
|
|
|
if (window)
|
|
|
|
gst_object_unref (window);
|
2015-02-17 07:17:59 +00:00
|
|
|
[pool release];
|
2013-08-15 07:09:04 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2015-02-17 07:17:59 +00:00
|
|
|
|
|
|
|
error:
|
2015-01-22 07:00:36 +00:00
|
|
|
{
|
|
|
|
if (window)
|
|
|
|
gst_object_unref (window);
|
|
|
|
[pool release];
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-08-15 07:09:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 02:26:57 +00:00
|
|
|
static void
|
|
|
|
gst_gl_context_cocoa_destroy_context (GstGLContext *context)
|
|
|
|
{
|
2014-09-26 11:21:46 +00:00
|
|
|
/* FIXME: Need to release context and other things? */
|
2013-09-25 02:26:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 07:09:04 +00:00
|
|
|
static guintptr
|
|
|
|
gst_gl_context_cocoa_get_gl_context (GstGLContext * context)
|
|
|
|
{
|
|
|
|
return (guintptr) GST_GL_CONTEXT_COCOA (context)->priv->gl_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_context_cocoa_activate (GstGLContext * context, gboolean activate)
|
|
|
|
{
|
2015-01-20 11:01:39 +00:00
|
|
|
GstGLContextCocoa *context_cocoa = GST_GL_CONTEXT_COCOA (context);
|
|
|
|
gpointer context_handle = activate ? context_cocoa->priv->gl_context : NULL;
|
2013-08-15 07:09:04 +00:00
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
return kCGLNoError == CGLSetCurrentContext (context_handle);
|
2013-08-15 07:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstGLAPI
|
|
|
|
gst_gl_context_cocoa_get_gl_api (GstGLContext * context)
|
|
|
|
{
|
2015-01-22 07:00:36 +00:00
|
|
|
GstGLContextCocoa *context_cocoa = GST_GL_CONTEXT_COCOA (context);
|
|
|
|
|
|
|
|
if (context_cocoa->priv->gl_context)
|
|
|
|
return context_cocoa->priv->context_api;
|
|
|
|
|
|
|
|
return GST_GL_API_OPENGL | GST_GL_API_OPENGL3;
|
2013-08-15 07:09:04 +00:00
|
|
|
}
|
2014-02-10 21:57:29 +00:00
|
|
|
|
|
|
|
static GstGLPlatform
|
|
|
|
gst_gl_context_cocoa_get_gl_platform (GstGLContext * context)
|
|
|
|
{
|
2014-03-17 13:06:22 +00:00
|
|
|
return GST_GL_PLATFORM_CGL;
|
2014-02-10 21:57:29 +00:00
|
|
|
}
|
2014-10-28 06:31:37 +00:00
|
|
|
|
|
|
|
guintptr
|
|
|
|
gst_gl_context_cocoa_get_current_context (void)
|
|
|
|
{
|
2015-01-20 11:01:39 +00:00
|
|
|
return (guintptr) CGLGetCurrentContext ();
|
2014-10-28 06:31:37 +00:00
|
|
|
}
|