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
78 NSOpenGLContext *currentContext;
80 CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
81 Drawable(vis, w, h, pbuffer),
84 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
86 NSRect winRect = NSMakeRect(0, 0, w, h);
88 window = [[NSWindow alloc]
89 initWithContentRect:winRect
90 styleMask:NSTitledWindowMask |
91 NSClosableWindowMask |
92 NSMiniaturizableWindowMask
93 backing:NSBackingStoreRetained
95 assert(window != nil);
97 view = [[NSOpenGLView alloc]
99 pixelFormat:pixelFormat];
102 [window setContentView:view];
103 [window setTitle:@"glretrace"];
112 resize(int w, int h) {
113 if (w == width && h == height) {
117 [window setContentSize:NSMakeSize(w, h)];
119 if (currentContext != nil) {
120 [currentContext update];
121 [window makeKeyAndOrderFront:nil];
122 [currentContext setView:view];
123 [currentContext makeCurrentContext];
126 Drawable::resize(w, h);
139 void swapBuffers(void) {
140 if (currentContext != nil) {
141 [currentContext flushBuffer];
147 class CocoaContext : public Context
150 NSOpenGLContext *context;
152 CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
165 if (autoreleasePool == nil) {
166 autoreleasePool = [[NSAutoreleasePool alloc] init];
172 // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
173 _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
177 [NSApplication sharedApplication];
179 [NSApp finishLaunching];
185 [autoreleasePool release];
190 createVisual(bool doubleBuffer, Profile profile) {
194 if (profile != PROFILE_COMPAT &&
195 profile != PROFILE_CORE) {
199 Attributes<NSOpenGLPixelFormatAttribute> attribs;
201 attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
202 attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
204 attribs.add(NSOpenGLPFADoubleBuffer);
206 attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
207 attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
208 if (profile == PROFILE_CORE) {
210 attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
216 // Use Apple software rendering for debugging purposes.
218 attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
223 NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
224 initWithAttributes:attribs];
226 return new CocoaVisual(pixelFormat);
230 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
234 return new CocoaDrawable(visual, width, height, pbuffer);
238 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
242 NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
243 NSOpenGLContext *share_context = nil;
244 NSOpenGLContext *context;
246 if (profile != PROFILE_COMPAT &&
247 profile != PROFILE_CORE) {
252 share_context = static_cast<CocoaContext*>(shareContext)->context;
255 context = [[NSOpenGLContext alloc]
256 initWithFormat:pixelFormat
257 shareContext:share_context];
258 assert(context != nil);
260 return new CocoaContext(visual, profile, context);
264 makeCurrent(Drawable *drawable, Context *context)
268 if (!drawable || !context) {
269 [NSOpenGLContext clearCurrentContext];
271 CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
272 CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
274 [cocoaDrawable->window makeKeyAndOrderFront:nil];
275 [cocoaContext->context setView:[cocoaDrawable->window contentView]];
276 [cocoaContext->context makeCurrentContext];
278 cocoaDrawable->currentContext = cocoaContext->context;
285 processEvents(void) {
290 event = [NSApp nextEventMatchingMask:NSAnyEventMask
291 untilDate:[NSDate distantPast]
292 inMode:NSDefaultRunLoopMode
295 [NSApp sendEvent:event];
302 } /* namespace glws */