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/
46 #include <Cocoa/Cocoa.h>
54 static __thread NSAutoreleasePool *
55 autoreleasePool = nil;
58 class CocoaVisual : public Visual
61 NSOpenGLPixelFormat *pixelFormat;
63 CocoaVisual(NSOpenGLPixelFormat *pf) :
68 [pixelFormat release];
73 class CocoaDrawable : public Drawable
77 NSOpenGLContext *currentContext;
79 CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
80 Drawable(vis, w, h, pbuffer),
83 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
85 NSRect winRect = NSMakeRect(0, 0, w, h);
87 window = [[NSWindow alloc]
88 initWithContentRect:winRect
89 styleMask:NSTitledWindowMask |
90 NSClosableWindowMask |
91 NSMiniaturizableWindowMask
92 backing:NSBackingStoreRetained
94 assert(window != nil);
96 NSOpenGLView *view = [[NSOpenGLView alloc]
98 pixelFormat:pixelFormat];
101 [window setContentView:view];
102 [window setTitle:@"glretrace"];
111 resize(int w, int h) {
112 if (w == width && h == height) {
116 [window setContentSize:NSMakeSize(w, h)];
118 if (currentContext != nil) {
119 [currentContext update];
120 [window makeKeyAndOrderFront:nil];
121 [currentContext setView:[window contentView]];
122 [currentContext makeCurrentContext];
125 Drawable::resize(w, h);
138 void swapBuffers(void) {
139 if (currentContext != nil) {
140 [currentContext flushBuffer];
146 class CocoaContext : public Context
149 NSOpenGLContext *context;
151 CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
164 if (autoreleasePool == nil) {
165 autoreleasePool = [[NSAutoreleasePool alloc] init];
171 // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
172 _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
176 [NSApplication sharedApplication];
178 [NSApp finishLaunching];
184 [autoreleasePool release];
189 createVisual(bool doubleBuffer, Profile profile) {
193 if (profile != PROFILE_COMPAT &&
194 profile != PROFILE_CORE) {
198 Attributes<NSOpenGLPixelFormatAttribute> attribs;
200 attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
201 attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
203 attribs.add(NSOpenGLPFADoubleBuffer);
205 attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
206 attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
207 if (profile == PROFILE_CORE) {
209 attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
215 // Use Apple software rendering for debugging purposes.
217 attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
222 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
223 initWithAttributes:attribs];
225 return new CocoaVisual(pixelFormat);
229 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
233 return new CocoaDrawable(visual, width, height, pbuffer);
237 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
241 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
242 NSOpenGLContext *share_context = nil;
243 NSOpenGLContext *context;
245 if (profile != PROFILE_COMPAT &&
246 profile != PROFILE_CORE) {
251 share_context = static_cast<CocoaContext*>(shareContext)->context;
254 context = [[NSOpenGLContext alloc]
255 initWithFormat:pixelFormat
256 shareContext:share_context];
257 assert(context != nil);
259 return new CocoaContext(visual, profile, context);
263 makeCurrent(Drawable *drawable, Context *context)
267 if (!drawable || !context) {
268 [NSOpenGLContext clearCurrentContext];
270 CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
271 CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
273 [cocoaDrawable->window makeKeyAndOrderFront:nil];
274 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
275 [cocoaContext->context makeCurrentContext];
277 cocoaDrawable->currentContext = cocoaContext->context;
284 processEvents(void) {
289 event = [NSApp nextEventMatchingMask:NSAnyEventMask
290 untilDate:[NSDate distantPast]
291 inMode:NSDefaultRunLoopMode
294 [NSApp sendEvent:event];
301 } /* namespace glws */