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 #include <OpenGL/gl.h>
22 #import "cocoa_opengl.h"
24 #include "macdrv_cocoa.h"
25 #include "cocoa_event.h"
28 @interface WineOpenGLContext ()
29 @property (retain, nonatomic) NSView* latentView;
33 @implementation WineOpenGLContext
34 @synthesize latentView, needsUpdate, shouldClearToBlack;
38 [[self view] release];
43 - (void) setView:(NSView*)newView
45 NSView* oldView = [self view];
46 [super setView:newView];
51 - (void) clearDrawable
53 NSView* oldView = [self view];
54 [super clearDrawable];
58 /* On at least some versions of Mac OS X, -[NSOpenGLContext clearDrawable] has the
59 undesirable side effect of ordering the view's GL surface off-screen. This isn't
60 done when just changing the context's view to a different view (which I would
61 think would be analogous, since the old view and surface end up without a
62 context attached). So, we finesse things by first setting the context's view to
63 a different view (the content view of an off-screen window) and then letting the
64 original implementation proceed. */
65 - (void) clearDrawableLeavingSurfaceOnScreen
67 static NSWindow* dummyWindow;
68 static dispatch_once_t once;
70 dispatch_once(&once, ^{
72 dummyWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
73 styleMask:NSBorderlessWindowMask
74 backing:NSBackingStoreBuffered
79 [self setView:[dummyWindow contentView]];
83 - (void) clearToBlackIfNeeded
85 if (shouldClearToBlack)
87 NSOpenGLContext* origContext = [NSOpenGLContext currentContext];
89 [self makeCurrentContext];
91 glPushAttrib(GL_COLOR_BUFFER_BIT | GL_SCISSOR_BIT);
92 glDrawBuffer(GL_FRONT_AND_BACK);
93 glDisable(GL_SCISSOR_TEST);
94 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
95 glClearColor(0, 0, 0, 1);
96 glClear(GL_COLOR_BUFFER_BIT);
101 [origContext makeCurrentContext];
103 [NSOpenGLContext clearCurrentContext];
105 shouldClearToBlack = FALSE;
109 - (void) removeFromViews:(BOOL)removeViews
113 macdrv_remove_view_opengl_context((macdrv_view)[self view], (macdrv_opengl_context)self);
115 [self clearDrawableLeavingSurfaceOnScreen];
117 if ([self latentView])
119 macdrv_remove_view_opengl_context((macdrv_view)[self latentView], (macdrv_opengl_context)self);
121 [self setLatentView:nil];
129 /***********************************************************************
130 * macdrv_create_opengl_context
132 * Returns a Cocoa OpenGL context created from a CoreGL context. The
133 * caller is responsible for calling macdrv_dispose_opengl_context()
134 * when done with the context object.
136 macdrv_opengl_context macdrv_create_opengl_context(void* cglctx)
138 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
139 WineOpenGLContext *context;
141 context = [[WineOpenGLContext alloc] initWithCGLContextObj:cglctx];
144 return (macdrv_opengl_context)context;
147 /***********************************************************************
148 * macdrv_dispose_opengl_context
150 * Destroys a Cocoa OpenGL context previously created by
151 * macdrv_create_opengl_context();
153 void macdrv_dispose_opengl_context(macdrv_opengl_context c)
155 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
156 WineOpenGLContext *context = (WineOpenGLContext*)c;
158 [context removeFromViews:YES];
164 /***********************************************************************
165 * macdrv_make_context_current
167 void macdrv_make_context_current(macdrv_opengl_context c, macdrv_view v)
169 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
170 WineOpenGLContext *context = (WineOpenGLContext*)c;
171 NSView* view = (NSView*)v;
175 if (view == [context view] || view == [context latentView])
176 macdrv_update_opengl_context(c);
179 [context removeFromViews:NO];
180 macdrv_add_view_opengl_context(v, c);
182 if (context.needsUpdate)
184 context.needsUpdate = FALSE;
185 [context setView:view];
186 [context setLatentView:nil];
191 [context clearDrawableLeavingSurfaceOnScreen];
192 [context setLatentView:view];
196 [context makeCurrentContext];
199 [context clearToBlackIfNeeded];
203 WineOpenGLContext* currentContext = (WineOpenGLContext*)[WineOpenGLContext currentContext];
205 if ([currentContext isKindOfClass:[WineOpenGLContext class]])
207 [WineOpenGLContext clearCurrentContext];
208 if (currentContext != context)
209 [currentContext removeFromViews:YES];
213 [context removeFromViews:YES];
219 /***********************************************************************
220 * macdrv_update_opengl_context
222 void macdrv_update_opengl_context(macdrv_opengl_context c)
224 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
225 WineOpenGLContext *context = (WineOpenGLContext*)c;
227 if (context.needsUpdate)
229 context.needsUpdate = FALSE;
230 if (context.latentView)
232 [context setView:context.latentView];
233 context.latentView = nil;
235 [context clearToBlackIfNeeded];
244 /***********************************************************************
245 * macdrv_flush_opengl_context
247 * Performs an implicit glFlush() and then swaps the back buffer to the
248 * front (if the context is double-buffered).
250 void macdrv_flush_opengl_context(macdrv_opengl_context c)
252 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
253 WineOpenGLContext *context = (WineOpenGLContext*)c;
255 macdrv_update_opengl_context(c);
256 [context flushBuffer];