1/* 2 * GStreamer 3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com> 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public 16 * License along with this library; if not, write to the 17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21#ifdef HAVE_CONFIG_H 22#include "config.h" 23#endif 24 25#if !defined(MAC_OS_X_VERSION_MAX_ALLOWED) || MAC_OS_X_VERSION_MAX_ALLOWED >= 1014 26# define GL_SILENCE_DEPRECATION 27#endif 28 29#include <Cocoa/Cocoa.h> 30 31#include "gstglcaopengllayer.h" 32#include "gstgl_cocoa_private.h" 33 34#define GST_CAT_DEFAULT gst_gl_ca_opengl_layer_debug 35GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); 36 37static void 38_init_debug (void) 39{ 40 static gsize _init = 0; 41 42 if (g_once_init_enter (&_init)) { 43 GST_DEBUG_CATEGORY_INIT(gst_gl_ca_opengl_layer_debug, "glcaopengllayer", 44 0, "CAOpenGLLayer"); 45 46 g_once_init_leave (&_init, 1); 47 } 48} 49 50@implementation GstGLCAOpenGLLayer 51- (void)dealloc { 52 if (self->draw_notify) 53 self->draw_notify (self->draw_data); 54 55 if (self->draw_context) 56 gst_object_unref (self->draw_context); 57 58 GST_TRACE ("dealloc GstGLCAOpenGLLayer %p context %p", self, self->gst_gl_context); 59} 60 61static void 62_context_ready (gpointer data) 63{ 64 GstGLCAOpenGLLayer *ca_layer = (__bridge GstGLCAOpenGLLayer *) data; 65 66 g_atomic_int_set (&ca_layer->can_draw, 1); 67} 68 69- (id)initWithGstGLContext:(GstGLContext *)parent_gl_context { 70 g_return_val_if_fail (GST_IS_GL_CONTEXT_COCOA (parent_gl_context), nil); 71 72 self = [super init]; 73 74 _init_debug(); 75 76 GST_LOG ("init CAOpenGLLayer"); 77 78 self->gst_gl_context = parent_gl_context; 79 self.needsDisplayOnBoundsChange = YES; 80 81 gst_gl_window_send_message_async (parent_gl_context->window, 82 (GstGLWindowCB) _context_ready, (__bridge_retained gpointer)self, (GDestroyNotify)CFRelease); 83 84 return self; 85} 86 87- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { 88 CGLPixelFormatObj fmt = NULL; 89 90 if (self->gst_gl_context) 91 fmt = gst_gl_context_cocoa_get_pixel_format (GST_GL_CONTEXT_COCOA (self->gst_gl_context)); 92 93 if (!fmt) { 94 CGLPixelFormatAttribute attribs[] = { 95 kCGLPFADoubleBuffer, 96 kCGLPFAAccumSize, 32, 97 0 98 }; 99 CGLError ret; 100 gint npix = 0; 101 102 GST_DEBUG ("creating new pixel format for CAOpenGLLayer %p", self); 103 104 ret = CGLChoosePixelFormat (attribs, &fmt, &npix); 105 if (ret != kCGLNoError) { 106 GST_ERROR ("CAOpenGLLayer cannot choose a pixel format: %s", CGLErrorString (ret)); 107 } 108 } 109 110 return fmt; 111} 112 113- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { 114 GstGLDisplay *display; 115 CGLContextObj external_context = NULL; 116 CGLError ret; 117 GError *error = NULL; 118 119 if (self->gst_gl_context) 120 external_context = (CGLContextObj) gst_gl_context_get_gl_context (self->gst_gl_context); 121 122 GST_INFO ("attempting to create CGLContext for CAOpenGLLayer with " 123 "share context %p", external_context); 124 125 ret = CGLCreateContext (pixelFormat, external_context, &self->gl_context); 126 if (ret != kCGLNoError) { 127 GST_ERROR ("failed to create CGL context in CAOpenGLLayer with share context %p: %s", external_context, CGLErrorString(ret)); 128 return NULL; 129 } 130 131 if (self->draw_context) 132 gst_object_unref (self->draw_context); 133 134 if (kCGLNoError != CGLSetCurrentContext (self->gl_context)) { 135 GST_ERROR ("failed set cgl context %p current", self->gl_context); 136 return NULL; 137 } 138 139 display = gst_gl_context_get_display (self->gst_gl_context); 140 self->draw_context = gst_gl_context_new_wrapped (display, 141 (guintptr) self->gl_context, GST_GL_PLATFORM_CGL, 142 gst_gl_context_get_current_gl_api (GST_GL_PLATFORM_CGL, NULL, NULL)); 143 gst_object_unref (display); 144 145 if (!self->draw_context) { 146 GST_ERROR ("failed to create wrapped context"); 147 return NULL; 148 } 149 150 gst_gl_context_activate (self->draw_context, TRUE); 151 gst_gl_context_set_shared_with (self->draw_context, self->gst_gl_context); 152 if (!gst_gl_context_fill_info (self->draw_context, &error)) { 153 GST_ERROR ("failed to fill wrapped context information: %s", error->message); 154 return NULL; 155 } 156 gst_gl_context_activate (self->draw_context, FALSE); 157 158 return self->gl_context; 159} 160 161- (void)queueResize { 162 self->queue_resize = TRUE; 163} 164 165- (void)releaseCGLContext:(CGLContextObj)glContext { 166 CGLReleaseContext (glContext); 167} 168 169- (void)setDrawCallback:(GstGLWindowCB)cb data:(gpointer)data 170 notify:(GDestroyNotify)notify { 171 g_return_if_fail (cb); 172 173 if (self->draw_notify) 174 self->draw_notify (self->draw_data); 175 176 self->draw_cb = cb; 177 self->draw_data = data; 178 self->draw_notify = notify; 179} 180 181- (void)setResizeCallback:(GstGLWindowResizeCB)cb data:(gpointer)data 182 notify:(GDestroyNotify)notify { 183 if (self->resize_notify) 184 self->resize_notify (self->resize_data); 185 186 self->resize_cb = cb; 187 self->resize_data = data; 188 self->resize_notify = notify; 189} 190 191- (BOOL)canDrawInCGLContext:(CGLContextObj)glContext 192 pixelFormat:(CGLPixelFormatObj)pixelFormat 193 forLayerTime:(CFTimeInterval)interval 194 displayTime:(const CVTimeStamp *)timeStamp { 195 return g_atomic_int_get (&self->can_draw); 196} 197 198- (void)drawInCGLContext:(CGLContextObj)glContext 199 pixelFormat:(CGLPixelFormatObj)pixelFormat 200 forLayerTime:(CFTimeInterval)interval 201 displayTime:(const CVTimeStamp *)timeStamp { 202 const GstGLFuncs *gl = ((GstGLContext *)self->gst_gl_context)->gl_vtable; 203 GstVideoRectangle src, dst, result; 204 gint ca_viewport[4]; 205 206 GST_LOG ("CAOpenGLLayer drawing with cgl context %p", glContext); 207 208 /* attempt to get the correct viewport back due to CA being too smart 209 * and messing around with it so center the expected viewport into 210 * the CA viewport set up on entry to this function */ 211 gl->GetIntegerv (GL_VIEWPORT, ca_viewport); 212 213 GST_TRACE ("retrieved viewport from CA %u,%u %ux%u", self->expected_dims[0], 214 self->expected_dims[1], self->expected_dims[2], self->expected_dims[3]); 215 gst_gl_context_activate (self->draw_context, TRUE); 216 if (self->queue_resize || self->last_bounds.size.width != self.bounds.size.width 217 || self->last_bounds.size.height != self.bounds.size.height) { 218 if (self->resize_cb) { 219 self->resize_cb (self->resize_data, 220 self.bounds.size.width*self.contentsScale, 221 self.bounds.size.height*self.contentsScale); 222 223 gl->GetIntegerv (GL_VIEWPORT, self->expected_dims); 224 225 GST_LOG ("resize callback wants viewport %u,%u %ux%u", 226 self->expected_dims[0], self->expected_dims[1], 227 self->expected_dims[2], self->expected_dims[3]); 228 } else { 229 /* default to whatever ca gives us */ 230 self->expected_dims[0] = ca_viewport[0]; 231 self->expected_dims[1] = ca_viewport[1]; 232 self->expected_dims[2] = ca_viewport[2]; 233 self->expected_dims[3] = ca_viewport[3]; 234 } 235 236 self->last_bounds = self.bounds; 237 self->queue_resize = FALSE; 238 } 239 240 src.x = self->expected_dims[0]; 241 src.y = self->expected_dims[1]; 242 src.w = self->expected_dims[2]; 243 src.h = self->expected_dims[3]; 244 245 dst.x = ca_viewport[0]; 246 dst.y = ca_viewport[1]; 247 dst.w = ca_viewport[2]; 248 dst.h = ca_viewport[3]; 249 250 gst_video_sink_center_rect (src, dst, &result, TRUE); 251 252 GST_TRACE ("Using viewport %u,%u %ux%u", result.x, result.y, result.w, 253 result.h); 254 gl->Viewport (result.x, result.y, result.w, result.h); 255 256 if (self->draw_cb) 257 self->draw_cb (self->draw_data); 258 gst_gl_context_activate (self->draw_context, FALSE); 259 260 /* flushes the buffer */ 261 [super drawInCGLContext:glContext pixelFormat:pixelFormat forLayerTime:interval displayTime:timeStamp]; 262} 263 264@end 265