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>
50 #include "os_thread.hpp"
55 * Dummy thread to force Cocoa to enter multithreading mode.
57 @interface DummyThread : NSObject
58 + (void)enterMultiThreaded;
59 + (void)dummyThreadMethod:(id)unused;
62 @implementation DummyThread
63 + (void)dummyThreadMethod:(id)unused {
67 + (void)enterMultiThreaded {
68 [NSThread detachNewThreadSelector:@selector(dummyThreadMethod:)
78 static OS_THREAD_SPECIFIC_PTR(NSAutoreleasePool)
82 class CocoaVisual : public Visual
85 NSOpenGLPixelFormat *pixelFormat;
87 CocoaVisual(NSOpenGLPixelFormat *pf) :
92 [pixelFormat release];
97 class CocoaDrawable : public Drawable
102 NSOpenGLContext *currentContext;
104 CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
105 Drawable(vis, w, h, pbuffer),
108 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
110 NSRect winRect = NSMakeRect(0, 0, w, h);
112 window = [[NSWindow alloc]
113 initWithContentRect:winRect
114 styleMask:NSTitledWindowMask |
115 NSClosableWindowMask |
116 NSMiniaturizableWindowMask
117 backing:NSBackingStoreRetained
119 assert(window != nil);
121 view = [[NSOpenGLView alloc]
122 initWithFrame:winRect
123 pixelFormat:pixelFormat];
126 [window setContentView:view];
127 [window setTitle:@"glretrace"];
136 resize(int w, int h) {
137 if (w == width && h == height) {
141 [window setContentSize:NSMakeSize(w, h)];
143 if (currentContext != nil) {
144 [currentContext update];
145 [window makeKeyAndOrderFront:nil];
146 [currentContext setView:view];
147 [currentContext makeCurrentContext];
150 Drawable::resize(w, h);
163 void swapBuffers(void) {
164 if (currentContext != nil) {
165 [currentContext flushBuffer];
171 class CocoaContext : public Context
174 NSOpenGLContext *context;
176 CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
189 if (autoreleasePool == nil) {
190 autoreleasePool = [[NSAutoreleasePool alloc] init];
196 // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
197 _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
201 [DummyThread enterMultiThreaded];
203 bool isMultiThreaded = [NSThread isMultiThreaded];
204 if (!isMultiThreaded) {
205 std::cerr << "error: failed to enable Cocoa multi-threading\n";
209 [NSApplication sharedApplication];
211 [NSApp finishLaunching];
217 [autoreleasePool release];
222 createVisual(bool doubleBuffer, Profile profile) {
226 if (profile != PROFILE_COMPAT &&
227 profile != PROFILE_CORE) {
231 Attributes<NSOpenGLPixelFormatAttribute> attribs;
233 attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
234 attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
236 attribs.add(NSOpenGLPFADoubleBuffer);
238 attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
239 attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
240 if (profile == PROFILE_CORE) {
242 attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
248 // Use Apple software rendering for debugging purposes.
250 attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
255 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
256 initWithAttributes:attribs];
258 return new CocoaVisual(pixelFormat);
262 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
266 return new CocoaDrawable(visual, width, height, pbuffer);
270 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
274 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
275 NSOpenGLContext *share_context = nil;
276 NSOpenGLContext *context;
278 if (profile != PROFILE_COMPAT &&
279 profile != PROFILE_CORE) {
284 share_context = static_cast<CocoaContext*>(shareContext)->context;
287 context = [[NSOpenGLContext alloc]
288 initWithFormat:pixelFormat
289 shareContext:share_context];
290 assert(context != nil);
292 return new CocoaContext(visual, profile, context);
296 makeCurrent(Drawable *drawable, Context *context)
300 if (!drawable || !context) {
301 [NSOpenGLContext clearCurrentContext];
303 CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
304 CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
306 [cocoaDrawable->window makeKeyAndOrderFront:nil];
307 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
308 [cocoaContext->context makeCurrentContext];
310 cocoaDrawable->currentContext = cocoaContext->context;
317 processEvents(void) {
322 event = [NSApp nextEventMatchingMask:NSAnyEventMask
323 untilDate:[NSDate distantPast]
324 inMode:NSDefaultRunLoopMode
327 [NSApp sendEvent:event];
334 } /* namespace glws */