gl/cocoa: small refactor of layer/view creation into the window

This commit is contained in:
Matthew Waters 2015-02-17 16:39:56 +11:00 committed by Tim-Philipp Müller
parent c4e75844a8
commit 742e4a10a2
3 changed files with 17 additions and 40 deletions

View file

@ -57,7 +57,7 @@ struct _GstGLContextCocoaPrivate
- (id) initWithFrameLayer:(GstGLWindowCocoa *)window rect:(NSRect)contentRect layer:(CALayer *)layerContent; - (id) initWithFrameLayer:(GstGLWindowCocoa *)window rect:(NSRect)contentRect layer:(CALayer *)layerContent;
@end @end
gboolean gst_gl_window_cocoa_create_window (GstGLWindowCocoa *window_cocoa, NSRect rect); gboolean gst_gl_window_cocoa_create_window (GstGLWindowCocoa *window_cocoa);
G_END_DECLS G_END_DECLS

View file

@ -304,8 +304,6 @@ gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
dispatch_sync (dispatch_get_main_queue (), ^{ dispatch_sync (dispatch_get_main_queue (), ^{
NSAutoreleasePool *pool; NSAutoreleasePool *pool;
CGLPixelFormatObj fmt = NULL; CGLPixelFormatObj fmt = NULL;
GstGLNSView *glView = NULL;
GstGLCAOpenGLLayer *layer;
CGLContextObj glContext; CGLContextObj glContext;
CGLPixelFormatAttribute attribs[] = { CGLPixelFormatAttribute attribs[] = {
kCGLPFADoubleBuffer, kCGLPFADoubleBuffer,
@ -313,20 +311,10 @@ gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
0 0
}; };
CGLError ret; CGLError ret;
NSRect rect;
NSWindow *window_handle;
gint npix; gint npix;
pool = [[NSAutoreleasePool alloc] init]; pool = [[NSAutoreleasePool alloc] init];
rect.origin.x = 0;
rect.origin.y = 0;
rect.size.width = 320;
rect.size.height = 240;
gst_gl_window_cocoa_create_window (window_cocoa, rect);
window_handle = (NSWindow *) gst_gl_window_get_window_handle (window);
if (priv->external_gl_context) { if (priv->external_gl_context) {
fmt = CGLGetPixelFormat (priv->external_gl_context); fmt = CGLGetPixelFormat (priv->external_gl_context);
} }
@ -354,10 +342,7 @@ gst_gl_context_cocoa_create_context (GstGLContext *context, GstGLAPI gl_api,
context_cocoa->priv->pixel_format = fmt; context_cocoa->priv->pixel_format = fmt;
context_cocoa->priv->gl_context = glContext; context_cocoa->priv->gl_context = glContext;
layer = [[GstGLCAOpenGLLayer alloc] initWithGstGLContext:context_cocoa]; gst_gl_window_cocoa_create_window (window_cocoa);
glView = [[GstGLNSView alloc] initWithFrameLayer:window_cocoa rect:rect layer:layer];
[window_handle setContentView:glView];
[pool release]; [pool release];
}); });

View file

@ -139,9 +139,21 @@ gst_gl_window_cocoa_new (void)
/* Must be called from the main thread */ /* Must be called from the main thread */
gboolean gboolean
gst_gl_window_cocoa_create_window (GstGLWindowCocoa *window_cocoa, NSRect rect) gst_gl_window_cocoa_create_window (GstGLWindowCocoa *window_cocoa)
{ {
GstGLWindowCocoaPrivate *priv = window_cocoa->priv; GstGLWindowCocoaPrivate *priv = window_cocoa->priv;
GstGLWindow *window = GST_GL_WINDOW (window_cocoa);
NSRect mainRect = [[NSScreen mainScreen] visibleFrame];
gint h = priv->preferred_height;
gint y = mainRect.size.height > h ? (mainRect.size.height - h) * 0.5 : 0;
NSRect rect = NSMakeRect (0, y, priv->preferred_width, priv->preferred_height);
NSRect windowRect = NSMakeRect (0, y, priv->preferred_width, priv->preferred_height);
GstGLContext *context = gst_gl_window_get_context (window);
GstGLContextCocoa *context_cocoa = GST_GL_CONTEXT_COCOA (context);
GstGLCAOpenGLLayer *layer = [[GstGLCAOpenGLLayer alloc] initWithGstGLContext:context_cocoa];
GstGLNSView *glView = [[GstGLNSView alloc] initWithFrameLayer:window_cocoa rect:windowRect layer:layer];
gst_object_unref (context);
priv->internal_win_id = [[GstGLNSWindow alloc] initWithContentRect:rect styleMask: priv->internal_win_id = [[GstGLNSWindow alloc] initWithContentRect:rect styleMask:
(NSTitledWindowMask | NSClosableWindowMask | (NSTitledWindowMask | NSClosableWindowMask |
@ -150,6 +162,8 @@ gst_gl_window_cocoa_create_window (GstGLWindowCocoa *window_cocoa, NSRect rect)
GST_DEBUG ("NSWindow id: %"G_GUINTPTR_FORMAT, (guintptr) priv->internal_win_id); GST_DEBUG ("NSWindow id: %"G_GUINTPTR_FORMAT, (guintptr) priv->internal_win_id);
[priv->internal_win_id setContentView:glView];
return TRUE; return TRUE;
} }
@ -240,30 +254,8 @@ gst_gl_window_cocoa_show (GstGLWindow * window)
dispatch_sync (dispatch_get_main_queue(), ^{ dispatch_sync (dispatch_get_main_queue(), ^{
if (!priv->external_view && !priv->visible) { if (!priv->external_view && !priv->visible) {
NSRect mainRect = [[NSScreen mainScreen] visibleFrame];
NSRect windowRect = [priv->internal_win_id frame];
gint x = 0;
gint y = 0;
GST_DEBUG_OBJECT (window_cocoa, "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 = window_cocoa->priv->preferred_width;
windowRect.size.height = window_cocoa->priv->preferred_height;
GST_DEBUG_OBJECT (window_cocoa, "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;
[priv->internal_win_id setFrame:windowRect display:NO];
GST_DEBUG_OBJECT (window_cocoa, "make the window available\n"); GST_DEBUG_OBJECT (window_cocoa, "make the window available\n");
[priv->internal_win_id makeMainWindow]; [priv->internal_win_id makeMainWindow];
[priv->internal_win_id orderFrontRegardless]; [priv->internal_win_id orderFrontRegardless];
[priv->internal_win_id setViewsNeedDisplay:YES]; [priv->internal_win_id setViewsNeedDisplay:YES];