1 /**************************************************************************
3 * Copyright 2011 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
28 * Minimal Cocoa integration.
31 * - http://developer.apple.com/library/mac/#samplecode/CocoaGL/Introduction/Intro.html
32 * - http://developer.apple.com/library/mac/#samplecode/Cocoa_With_Carbon_or_CPP/Introduction/Intro.html
33 * - http://developer.apple.com/library/mac/#samplecode/glut/Introduction/Intro.html
34 * - http://developer.apple.com/library/mac/#samplecode/GLEssentials/Introduction/Intro.html
35 * - http://www.glfw.org/
36 * - http://cocoasamurai.blogspot.co.uk/2008/04/guide-to-threading-on-leopard.html
37 * - http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html
48 #include <Cocoa/Cocoa.h>
54 * Dummy thread to force Cocoa to enter multithreading mode.
56 @interface DummyThread : NSObject
57 + (void)enterMultiThreaded;
58 + (void)dummyThreadMethod:(id)unused;
61 @implementation DummyThread
62 + (void)dummyThreadMethod:(id)unused {
66 + (void)enterMultiThreaded {
67 [NSThread detachNewThreadSelector:@selector(dummyThreadMethod:)
77 static __thread NSAutoreleasePool *
78 autoreleasePool = nil;
81 class CocoaVisual : public Visual
84 NSOpenGLPixelFormat *pixelFormat;
86 CocoaVisual(NSOpenGLPixelFormat *pf) :
91 [pixelFormat release];
96 class CocoaDrawable : public Drawable
101 NSOpenGLContext *currentContext;
103 CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
104 Drawable(vis, w, h, pbuffer),
107 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
109 NSRect winRect = NSMakeRect(0, 0, w, h);
111 window = [[NSWindow alloc]
112 initWithContentRect:winRect
113 styleMask:NSTitledWindowMask |
114 NSClosableWindowMask |
115 NSMiniaturizableWindowMask
116 backing:NSBackingStoreRetained
118 assert(window != nil);
120 view = [[NSOpenGLView alloc]
121 initWithFrame:winRect
122 pixelFormat:pixelFormat];
125 [window setContentView:view];
126 [window setTitle:@"glretrace"];
135 resize(int w, int h) {
136 if (w == width && h == height) {
140 [window setContentSize:NSMakeSize(w, h)];
142 if (currentContext != nil) {
143 [currentContext update];
144 [window makeKeyAndOrderFront:nil];
145 [currentContext setView:view];
146 [currentContext makeCurrentContext];
149 Drawable::resize(w, h);
162 void swapBuffers(void) {
163 if (currentContext != nil) {
164 [currentContext flushBuffer];
170 class CocoaContext : public Context
173 NSOpenGLContext *context;
175 CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
188 if (autoreleasePool == nil) {
189 autoreleasePool = [[NSAutoreleasePool alloc] init];
195 // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
196 _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
200 [DummyThread enterMultiThreaded];
202 bool isMultiThreaded = [NSThread isMultiThreaded];
203 if (!isMultiThreaded) {
204 std::cerr << "error: failed to enable Cocoa multi-threading\n";
208 [NSApplication sharedApplication];
210 [NSApp finishLaunching];
216 [autoreleasePool release];
221 createVisual(bool doubleBuffer, Profile profile) {
225 if (profile != PROFILE_COMPAT &&
226 profile != PROFILE_CORE) {
230 Attributes<NSOpenGLPixelFormatAttribute> attribs;
232 attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
233 attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
235 attribs.add(NSOpenGLPFADoubleBuffer);
237 attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
238 attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
239 if (profile == PROFILE_CORE) {
241 attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
247 // Use Apple software rendering for debugging purposes.
249 attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
254 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
255 initWithAttributes:attribs];
257 return new CocoaVisual(pixelFormat);
261 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
265 return new CocoaDrawable(visual, width, height, pbuffer);
269 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
273 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
274 NSOpenGLContext *share_context = nil;
275 NSOpenGLContext *context;
277 if (profile != PROFILE_COMPAT &&
278 profile != PROFILE_CORE) {
283 share_context = static_cast<CocoaContext*>(shareContext)->context;
286 context = [[NSOpenGLContext alloc]
287 initWithFormat:pixelFormat
288 shareContext:share_context];
289 assert(context != nil);
291 return new CocoaContext(visual, profile, context);
295 makeCurrent(Drawable *drawable, Context *context)
299 if (!drawable || !context) {
300 [NSOpenGLContext clearCurrentContext];
302 CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
303 CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
305 [cocoaDrawable->window makeKeyAndOrderFront:nil];
306 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
307 [cocoaContext->context makeCurrentContext];
309 cocoaDrawable->currentContext = cocoaContext->context;
316 processEvents(void) {
321 event = [NSApp nextEventMatchingMask:NSAnyEventMask
322 untilDate:[NSDate distantPast]
323 inMode:NSDefaultRunLoopMode
326 [NSApp sendEvent:event];
333 } /* namespace glws */