2015-01-20 11:01:39 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2015-01-28 06:05:14 +00:00
|
|
|
#include "gstglcaopengllayer.h"
|
2015-01-20 11:01:39 +00:00
|
|
|
#include "gstgl_cocoa_private.h"
|
|
|
|
|
2016-11-15 10:43:59 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_gl_ca_opengl_layer_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
_init_debug (void)
|
|
|
|
{
|
|
|
|
static volatile gsize _init = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&_init)) {
|
|
|
|
GST_DEBUG_CATEGORY_INIT(gst_gl_ca_opengl_layer_debug, "glcaopengllayer",
|
|
|
|
0, "CAOpenGLLayer");
|
|
|
|
|
|
|
|
g_once_init_leave (&_init, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
@implementation GstGLCAOpenGLLayer
|
|
|
|
- (void)dealloc {
|
2015-01-28 06:16:14 +00:00
|
|
|
if (self->draw_notify)
|
|
|
|
self->draw_notify (self->draw_data);
|
|
|
|
|
2015-05-19 06:19:41 +00:00
|
|
|
if (self->draw_context)
|
|
|
|
gst_object_unref (self->draw_context);
|
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
GST_TRACE ("dealloc GstGLCAOpenGLLayer %p context %p", self, self->gst_gl_context);
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:41:17 +00:00
|
|
|
static void
|
|
|
|
_context_ready (gpointer data)
|
|
|
|
{
|
2017-02-03 13:46:39 +00:00
|
|
|
GstGLCAOpenGLLayer *ca_layer = (__bridge GstGLCAOpenGLLayer *) data;
|
2015-02-17 05:41:17 +00:00
|
|
|
|
|
|
|
g_atomic_int_set (&ca_layer->can_draw, 1);
|
|
|
|
}
|
|
|
|
|
2017-11-27 03:44:58 +00:00
|
|
|
- (id)initWithGstGLContext:(GstGLContext *)parent_gl_context {
|
|
|
|
g_return_val_if_fail (GST_IS_GL_CONTEXT_COCOA (parent_gl_context), nil);
|
|
|
|
|
2017-02-03 13:46:39 +00:00
|
|
|
self = [super init];
|
2015-01-20 11:01:39 +00:00
|
|
|
|
2016-11-15 10:43:59 +00:00
|
|
|
_init_debug();
|
|
|
|
|
2015-01-29 04:26:21 +00:00
|
|
|
GST_LOG ("init CAOpenGLLayer");
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
self->gst_gl_context = parent_gl_context;
|
2015-01-29 04:26:21 +00:00
|
|
|
self.needsDisplayOnBoundsChange = YES;
|
2015-01-20 11:01:39 +00:00
|
|
|
|
2017-11-27 03:44:58 +00:00
|
|
|
gst_gl_window_send_message_async (parent_gl_context->window,
|
2017-02-03 13:46:39 +00:00
|
|
|
(GstGLWindowCB) _context_ready, (__bridge_retained gpointer)self, (GDestroyNotify)CFRelease);
|
2015-02-17 05:41:17 +00:00
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
|
|
|
|
CGLPixelFormatObj fmt = NULL;
|
|
|
|
|
|
|
|
if (self->gst_gl_context)
|
2017-11-27 03:44:58 +00:00
|
|
|
fmt = gst_gl_context_cocoa_get_pixel_format (GST_GL_CONTEXT_COCOA (self->gst_gl_context));
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
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 {
|
2015-05-19 06:19:41 +00:00
|
|
|
GstGLDisplay *display;
|
2015-01-20 11:01:39 +00:00
|
|
|
CGLContextObj external_context = NULL;
|
|
|
|
CGLError ret;
|
2015-05-19 06:19:41 +00:00
|
|
|
GError *error = NULL;
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
if (self->gst_gl_context)
|
2017-11-27 03:44:58 +00:00
|
|
|
external_context = (CGLContextObj) gst_gl_context_get_gl_context (self->gst_gl_context);
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
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));
|
2015-05-19 06:19:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->draw_context)
|
|
|
|
gst_object_unref (self->draw_context);
|
|
|
|
|
2015-05-22 15:00:18 +00:00
|
|
|
if (kCGLNoError != CGLSetCurrentContext (self->gl_context)) {
|
|
|
|
GST_ERROR ("failed set cgl context %p current", self->gl_context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-27 03:44:58 +00:00
|
|
|
display = gst_gl_context_get_display (self->gst_gl_context);
|
2015-05-19 06:19:41 +00:00
|
|
|
self->draw_context = gst_gl_context_new_wrapped (display,
|
|
|
|
(guintptr) self->gl_context, GST_GL_PLATFORM_CGL,
|
2015-07-18 07:19:18 +00:00
|
|
|
gst_gl_context_get_current_gl_api (GST_GL_PLATFORM_CGL, NULL, NULL));
|
2015-05-19 06:19:41 +00:00
|
|
|
gst_object_unref (display);
|
|
|
|
|
|
|
|
if (!self->draw_context) {
|
|
|
|
GST_ERROR ("failed to create wrapped context");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_gl_context_activate (self->draw_context, TRUE);
|
2017-11-27 03:44:58 +00:00
|
|
|
gst_gl_context_set_shared_with (self->draw_context, self->gst_gl_context);
|
2015-05-19 06:19:41 +00:00
|
|
|
if (!gst_gl_context_fill_info (self->draw_context, &error)) {
|
|
|
|
GST_ERROR ("failed to fill wrapped context information: %s", error->message);
|
|
|
|
return NULL;
|
2015-01-20 11:01:39 +00:00
|
|
|
}
|
2015-05-19 06:19:41 +00:00
|
|
|
gst_gl_context_activate (self->draw_context, FALSE);
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
return self->gl_context;
|
|
|
|
}
|
|
|
|
|
2015-09-17 12:17:24 +00:00
|
|
|
- (void)queueResize {
|
|
|
|
self->queue_resize = TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:16:14 +00:00
|
|
|
- (void)releaseCGLContext:(CGLContextObj)glContext {
|
|
|
|
CGLReleaseContext (glContext);
|
|
|
|
}
|
|
|
|
|
2015-01-29 06:31:09 +00:00
|
|
|
- (void)setDrawCallback:(GstGLWindowCB)cb data:(gpointer)data
|
|
|
|
notify:(GDestroyNotify)notify {
|
2015-01-28 06:16:14 +00:00
|
|
|
g_return_if_fail (cb);
|
|
|
|
|
|
|
|
if (self->draw_notify)
|
|
|
|
self->draw_notify (self->draw_data);
|
|
|
|
|
|
|
|
self->draw_cb = cb;
|
|
|
|
self->draw_data = data;
|
|
|
|
self->draw_notify = notify;
|
|
|
|
}
|
|
|
|
|
2015-01-29 06:31:09 +00:00
|
|
|
- (void)setResizeCallback:(GstGLWindowResizeCB)cb data:(gpointer)data
|
|
|
|
notify:(GDestroyNotify)notify {
|
|
|
|
if (self->resize_notify)
|
2017-02-03 13:46:39 +00:00
|
|
|
self->resize_notify (self->resize_data);
|
2015-01-29 04:26:21 +00:00
|
|
|
|
2015-01-29 06:31:09 +00:00
|
|
|
self->resize_cb = cb;
|
|
|
|
self->resize_data = data;
|
|
|
|
self->resize_notify = notify;
|
2015-01-20 11:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 05:41:17 +00:00
|
|
|
- (BOOL)canDrawInCGLContext:(CGLContextObj)glContext
|
|
|
|
pixelFormat:(CGLPixelFormatObj)pixelFormat
|
|
|
|
forLayerTime:(CFTimeInterval)interval
|
|
|
|
displayTime:(const CVTimeStamp *)timeStamp {
|
|
|
|
return g_atomic_int_get (&self->can_draw);
|
|
|
|
}
|
|
|
|
|
2015-01-20 11:01:39 +00:00
|
|
|
- (void)drawInCGLContext:(CGLContextObj)glContext
|
|
|
|
pixelFormat:(CGLPixelFormatObj)pixelFormat
|
|
|
|
forLayerTime:(CFTimeInterval)interval
|
|
|
|
displayTime:(const CVTimeStamp *)timeStamp {
|
|
|
|
const GstGLFuncs *gl = ((GstGLContext *)self->gst_gl_context)->gl_vtable;
|
2015-01-29 06:31:09 +00:00
|
|
|
GstVideoRectangle src, dst, result;
|
2015-01-20 11:01:39 +00:00
|
|
|
gint ca_viewport[4];
|
|
|
|
|
2015-01-28 06:16:14 +00:00
|
|
|
GST_LOG ("CAOpenGLLayer drawing with cgl context %p", glContext);
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2019-03-05 05:13:15 +00:00
|
|
|
GST_TRACE ("retrieved viewport from CA %u,%u %ux%u", self->expected_dims[0],
|
|
|
|
self->expected_dims[1], self->expected_dims[2], self->expected_dims[3]);
|
2015-05-19 06:19:41 +00:00
|
|
|
gst_gl_context_activate (self->draw_context, TRUE);
|
2015-09-17 07:06:37 +00:00
|
|
|
if (self->queue_resize || self->last_bounds.size.width != self.bounds.size.width
|
2015-01-29 06:31:09 +00:00
|
|
|
|| self->last_bounds.size.height != self.bounds.size.height) {
|
|
|
|
if (self->resize_cb) {
|
2019-03-05 05:13:15 +00:00
|
|
|
self->resize_cb (self->resize_data,
|
2016-04-17 14:43:20 +00:00
|
|
|
self.bounds.size.width*self.contentsScale,
|
|
|
|
self.bounds.size.height*self.contentsScale);
|
2015-01-29 06:31:09 +00:00
|
|
|
|
|
|
|
gl->GetIntegerv (GL_VIEWPORT, self->expected_dims);
|
2019-03-05 05:13:15 +00:00
|
|
|
|
|
|
|
GST_LOG ("resize callback wants viewport %u,%u %ux%u",
|
|
|
|
self->expected_dims[0], self->expected_dims[1],
|
|
|
|
self->expected_dims[2], self->expected_dims[3]);
|
2015-01-29 06:31:09 +00:00
|
|
|
} else {
|
|
|
|
/* default to whatever ca gives us */
|
|
|
|
self->expected_dims[0] = ca_viewport[0];
|
|
|
|
self->expected_dims[1] = ca_viewport[1];
|
|
|
|
self->expected_dims[2] = ca_viewport[2];
|
|
|
|
self->expected_dims[3] = ca_viewport[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
self->last_bounds = self.bounds;
|
2015-09-17 07:06:37 +00:00
|
|
|
self->queue_resize = FALSE;
|
2015-01-29 06:31:09 +00:00
|
|
|
}
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-03-05 05:13:15 +00:00
|
|
|
GST_TRACE ("Using viewport %u,%u %ux%u", result.x, result.y, result.w,
|
|
|
|
result.h);
|
2015-01-20 11:01:39 +00:00
|
|
|
gl->Viewport (result.x, result.y, result.w, result.h);
|
|
|
|
|
2015-01-28 06:16:14 +00:00
|
|
|
if (self->draw_cb)
|
|
|
|
self->draw_cb (self->draw_data);
|
2015-05-19 06:19:41 +00:00
|
|
|
gst_gl_context_activate (self->draw_context, FALSE);
|
2015-01-20 11:01:39 +00:00
|
|
|
|
|
|
|
/* flushes the buffer */
|
|
|
|
[super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:interval displayTime:timeStamp];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|