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) clearDrawableLeavingSurfaceOnScreen
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]];
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 clearDrawableLeavingSurfaceOnScreen];
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 viewIsValidDrawable;
128 macdrv_add_view_opengl_context(v, c);
131 viewIsValidDrawable = [[view window] windowNumber] > 0 &&
132 !NSIsEmptyRect([view visibleRect]);
134 if (viewIsValidDrawable)
136 [context setView:view];
137 [context setLatentView:nil];
140 [context setLatentView:view];
142 [context makeCurrentContext];
146 [WineOpenGLContext clearCurrentContext];
147 [context clearDrawableLeavingSurfaceOnScreen];
151 [WineOpenGLContext clearCurrentContext];
156 /***********************************************************************
157 * macdrv_update_opengl_context
159 void macdrv_update_opengl_context(macdrv_opengl_context c)
161 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
162 WineOpenGLContext *context = (WineOpenGLContext*)c;
164 if (context.needsUpdate)
166 context.needsUpdate = FALSE;
167 if (context.latentView)
169 [context setView:context.latentView];
170 context.latentView = nil;
179 /***********************************************************************
180 * macdrv_flush_opengl_context
182 * Performs an implicit glFlush() and then swaps the back buffer to the
183 * front (if the context is double-buffered).
185 void macdrv_flush_opengl_context(macdrv_opengl_context c)
187 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
188 WineOpenGLContext *context = (WineOpenGLContext*)c;
190 macdrv_update_opengl_context(c);
191 [context flushBuffer];