2 * MACDRV Cocoa OpenGL code
4 * Copyright 2012, 2013 Ken Thomases for CodeWeavers Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #import "cocoa_opengl.h"
23 #include "macdrv_cocoa.h"
24 #include "cocoa_event.h"
27 @interface WineOpenGLContext ()
28 @property (retain, nonatomic) NSView* latentView;
32 @implementation WineOpenGLContext
33 @synthesize latentView, needsUpdate;
41 /* On at least some versions of Mac OS X, -[NSOpenGLContext clearDrawable] has the
42 undesirable side effect of ordering the view's GL surface off-screen. This isn't
43 done when just changing the context's view to a different view (which I would
44 think would be analogous, since the old view and surface end up without a
45 context attached). So, we finesse things by first setting the context's view to
46 a different view (the content view of an off-screen window) and then letting the
47 original implementation proceed. */
48 - (void) clearDrawable
50 static NSWindow* dummyWindow;
51 static dispatch_once_t once;
53 dispatch_once(&once, ^{
55 dummyWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
56 styleMask:NSBorderlessWindowMask
57 backing:NSBackingStoreBuffered
62 [self setView:[dummyWindow contentView]];
63 [super clearDrawable];
69 /***********************************************************************
70 * macdrv_create_opengl_context
72 * Returns a Cocoa OpenGL context created from a CoreGL context. The
73 * caller is responsible for calling macdrv_dispose_opengl_context()
74 * when done with the context object.
76 macdrv_opengl_context macdrv_create_opengl_context(void* cglctx)
78 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
79 WineOpenGLContext *context;
81 context = [[WineOpenGLContext alloc] initWithCGLContextObj:cglctx];
84 return (macdrv_opengl_context)context;
87 /***********************************************************************
88 * macdrv_dispose_opengl_context
90 * Destroys a Cocoa OpenGL context previously created by
91 * macdrv_create_opengl_context();
93 void macdrv_dispose_opengl_context(macdrv_opengl_context c)
95 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
96 WineOpenGLContext *context = (WineOpenGLContext*)c;
99 macdrv_remove_view_opengl_context((macdrv_view)[context view], c);
100 if ([context latentView])
101 macdrv_remove_view_opengl_context((macdrv_view)[context latentView], c);
102 [context clearDrawable];
108 /***********************************************************************
109 * macdrv_make_context_current
111 void macdrv_make_context_current(macdrv_opengl_context c, macdrv_view v)
113 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
114 WineOpenGLContext *context = (WineOpenGLContext*)c;
115 NSView* view = (NSView*)v;
120 macdrv_remove_view_opengl_context((macdrv_view)[context view], c);
121 if ([context latentView])
122 macdrv_remove_view_opengl_context((macdrv_view)[context latentView], c);
123 context.needsUpdate = FALSE;
126 __block BOOL windowHasDevice;
128 macdrv_add_view_opengl_context(v, c);
131 windowHasDevice = [[view window] windowNumber] > 0;
135 [context setView:view];
136 [context setLatentView:nil];
139 [context setLatentView:view];
141 [context makeCurrentContext];
145 [WineOpenGLContext clearCurrentContext];
146 [context clearDrawable];
150 [WineOpenGLContext clearCurrentContext];
155 /***********************************************************************
156 * macdrv_update_opengl_context
158 void macdrv_update_opengl_context(macdrv_opengl_context c)
160 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
161 WineOpenGLContext *context = (WineOpenGLContext*)c;
163 if (context.needsUpdate)
165 context.needsUpdate = FALSE;
166 if (context.latentView)
168 [context setView:context.latentView];
169 context.latentView = nil;
178 /***********************************************************************
179 * macdrv_flush_opengl_context
181 * Performs an implicit glFlush() and then swaps the back buffer to the
182 * front (if the context is double-buffered).
184 void macdrv_flush_opengl_context(macdrv_opengl_context c)
186 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
187 WineOpenGLContext *context = (WineOpenGLContext*)c;
189 macdrv_update_opengl_context(c);
190 [context flushBuffer];