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;
31 + (NSView*) dummyView;
32 - (void) wine_updateBackingSize:(const CGSize*)size;
37 @implementation WineOpenGLContext
38 @synthesize latentView, needsUpdate, shouldClearToBlack;
42 [[self view] release];
49 static NSWindow* dummyWindow;
50 static dispatch_once_t once;
52 dispatch_once(&once, ^{
54 dummyWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
55 styleMask:NSBorderlessWindowMask
56 backing:NSBackingStoreBuffered
61 return dummyWindow.contentView;
64 // Normally, we take care that disconnecting a context from a view doesn't
65 // destroy that view's GL surface (see -clearDrawableLeavingSurfaceOnScreen).
66 // However, if we're using a surface backing size and that size changes, we
67 // need to destroy and recreate the surface or we get weird behavior.
68 - (void) resetSurfaceIfBackingSizeChanged
74 if (macdrv_get_view_backing_size((macdrv_view)self.view, view_backing) &&
75 (view_backing[0] != backing_size[0] || view_backing[1] != backing_size[1]))
77 view_backing[0] = backing_size[0];
78 view_backing[1] = backing_size[1];
79 macdrv_set_view_backing_size((macdrv_view)self.view, view_backing);
81 NSView* save = self.view;
82 [super clearDrawable];
84 shouldClearToBlack = TRUE;
88 - (void) wine_updateBackingSize:(const CGSize*)size
97 if (CGLIsEnabled(self.CGLContextObj, kCGLCESurfaceBackingSize, &enabled) != kCGLNoError)
100 if (!enabled || backing_size[0] != size->width || backing_size[1] != size->height)
102 backing_size[0] = size->width;
103 backing_size[1] = size->height;
104 CGLSetParameter(self.CGLContextObj, kCGLCPSurfaceBackingSize, backing_size);
108 CGLEnable(self.CGLContextObj, kCGLCESurfaceBackingSize);
110 [self resetSurfaceIfBackingSizeChanged];
117 if (CGLIsEnabled(self.CGLContextObj, kCGLCESurfaceBackingSize, &enabled) == kCGLNoError && enabled)
118 CGLDisable(self.CGLContextObj, kCGLCESurfaceBackingSize);
122 - (void) setView:(NSView*)newView
124 NSView* oldView = [self view];
125 [super setView:newView];
130 - (void) clearDrawable
132 NSView* oldView = [self view];
133 [super clearDrawable];
136 [self wine_updateBackingSize:NULL];
139 /* On at least some versions of Mac OS X, -[NSOpenGLContext clearDrawable] has the
140 undesirable side effect of ordering the view's GL surface off-screen. This isn't
141 done when just changing the context's view to a different view (which I would
142 think would be analogous, since the old view and surface end up without a
143 context attached). So, we finesse things by first setting the context's view to
144 a different view (the content view of an off-screen window) and then letting the
145 original implementation proceed. */
146 - (void) clearDrawableLeavingSurfaceOnScreen
148 [self setView:[[self class] dummyView]];
149 [self clearDrawable];
152 - (void) clearToBlackIfNeeded
154 if (shouldClearToBlack)
156 NSOpenGLContext* origContext = [NSOpenGLContext currentContext];
157 const char *gl_version;
159 GLint draw_framebuffer_binding, draw_buffer;
160 GLboolean scissor_test, color_mask[4];
161 GLfloat clear_color[4];
163 [self makeCurrentContext];
165 gl_version = (const char *)glGetString(GL_VERSION);
166 major = gl_version[0] - '0';
167 /* FIXME: Should check for GL_ARB_framebuffer_object and GL_EXT_framebuffer_object
168 * for older GL versions. */
171 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer_binding);
172 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
174 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
175 scissor_test = glIsEnabled(GL_SCISSOR_TEST);
176 glGetBooleanv(GL_COLOR_WRITEMASK, color_mask);
177 glGetFloatv(GL_COLOR_CLEAR_VALUE, clear_color);
178 glDrawBuffer(GL_FRONT_AND_BACK);
179 glDisable(GL_SCISSOR_TEST);
180 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
181 glClearColor(0, 0, 0, gl_surface_mode == GL_SURFACE_IN_FRONT_TRANSPARENT ? 0 : 1);
183 glClear(GL_COLOR_BUFFER_BIT);
185 glClearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
186 glColorMask(color_mask[0], color_mask[1], color_mask[2], color_mask[3]);
188 glEnable(GL_SCISSOR_TEST);
189 glDrawBuffer(draw_buffer);
191 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw_framebuffer_binding);
195 [origContext makeCurrentContext];
197 [NSOpenGLContext clearCurrentContext];
199 shouldClearToBlack = FALSE;
203 - (void) removeFromViews:(BOOL)removeViews
207 macdrv_remove_view_opengl_context((macdrv_view)[self view], (macdrv_opengl_context)self);
209 [self clearDrawableLeavingSurfaceOnScreen];
211 if ([self latentView])
213 macdrv_remove_view_opengl_context((macdrv_view)[self latentView], (macdrv_opengl_context)self);
215 [self setLatentView:nil];
223 /***********************************************************************
224 * macdrv_create_opengl_context
226 * Returns a Cocoa OpenGL context created from a CoreGL context. The
227 * caller is responsible for calling macdrv_dispose_opengl_context()
228 * when done with the context object.
230 macdrv_opengl_context macdrv_create_opengl_context(void* cglctx)
232 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
233 WineOpenGLContext *context;
235 context = [[WineOpenGLContext alloc] initWithCGLContextObj:cglctx];
238 return (macdrv_opengl_context)context;
241 /***********************************************************************
242 * macdrv_dispose_opengl_context
244 * Destroys a Cocoa OpenGL context previously created by
245 * macdrv_create_opengl_context();
247 void macdrv_dispose_opengl_context(macdrv_opengl_context c)
249 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
250 WineOpenGLContext *context = (WineOpenGLContext*)c;
252 [context removeFromViews:YES];
258 /***********************************************************************
259 * macdrv_make_context_current
261 void macdrv_make_context_current(macdrv_opengl_context c, macdrv_view v, CGRect r)
263 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
264 WineOpenGLContext *context = (WineOpenGLContext*)c;
265 NSView* view = (NSView*)v;
269 if (view == [context view] || view == [context latentView])
271 [context wine_updateBackingSize:&r.size];
272 macdrv_update_opengl_context(c);
276 [context removeFromViews:NO];
277 macdrv_add_view_opengl_context(v, c);
279 if (context.needsUpdate)
281 context.needsUpdate = FALSE;
283 [context setView:[[context class] dummyView]];
284 [context wine_updateBackingSize:&r.size];
285 [context setView:view];
286 [context setLatentView:nil];
287 [context resetSurfaceIfBackingSizeChanged];
292 [context clearDrawableLeavingSurfaceOnScreen];
293 [context wine_updateBackingSize:&r.size];
294 [context setLatentView:view];
298 [context makeCurrentContext];
301 [context clearToBlackIfNeeded];
305 WineOpenGLContext* currentContext = (WineOpenGLContext*)[WineOpenGLContext currentContext];
307 if ([currentContext isKindOfClass:[WineOpenGLContext class]])
309 [WineOpenGLContext clearCurrentContext];
310 if (currentContext != context)
311 [currentContext removeFromViews:YES];
315 [context removeFromViews:YES];
321 /***********************************************************************
322 * macdrv_update_opengl_context
324 void macdrv_update_opengl_context(macdrv_opengl_context c)
326 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
327 WineOpenGLContext *context = (WineOpenGLContext*)c;
329 if (context.needsUpdate)
331 context.needsUpdate = FALSE;
332 if (context.latentView)
334 [context setView:context.latentView];
335 context.latentView = nil;
337 [context resetSurfaceIfBackingSizeChanged];
338 [context clearToBlackIfNeeded];
343 [context resetSurfaceIfBackingSizeChanged];
350 /***********************************************************************
351 * macdrv_flush_opengl_context
353 * Performs an implicit glFlush() and then swaps the back buffer to the
354 * front (if the context is double-buffered).
356 void macdrv_flush_opengl_context(macdrv_opengl_context c)
358 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
359 WineOpenGLContext *context = (WineOpenGLContext*)c;
361 macdrv_update_opengl_context(c);
362 [context flushBuffer];