]> git.cworth.org Git - apitrace/blob - retrace/glws_cocoa.mm
8ec58b13903c8b9762eda4278e8a627e193260fc
[apitrace] / retrace / glws_cocoa.mm
1 /**************************************************************************
2  *
3  * Copyright 2011 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 /**
28  * Minimal Cocoa integration.
29  *
30  * See also:
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  */
37
38
39 #include "glproc.hpp"
40
41 #include <stdlib.h>
42 #include <iostream>
43
44 #include <dlfcn.h>
45
46 #include <Cocoa/Cocoa.h>
47
48 #include "glws.hpp"
49
50
51 namespace glws {
52
53
54 static __thread NSAutoreleasePool *
55 autoreleasePool = nil;
56
57
58 class CocoaVisual : public Visual
59 {
60 public:
61     NSOpenGLPixelFormat *pixelFormat;
62
63     CocoaVisual(NSOpenGLPixelFormat *pf) :
64         pixelFormat(pf)
65     {}
66
67     ~CocoaVisual() {
68         [pixelFormat release];
69     }
70 };
71  
72
73 class CocoaDrawable : public Drawable
74 {
75 public:
76     NSWindow *window;
77     NSOpenGLContext *currentContext;
78
79     CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) :
80         Drawable(vis, w, h, pbuffer),
81         currentContext(nil)
82     {
83         NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
84
85         NSRect winRect = NSMakeRect(0, 0, w, h);
86
87         window = [[NSWindow alloc]
88                          initWithContentRect:winRect
89                                    styleMask:NSTitledWindowMask |
90                                              NSClosableWindowMask |
91                                              NSMiniaturizableWindowMask
92                                      backing:NSBackingStoreRetained
93                                        defer:NO];
94         assert(window != nil);
95
96         NSOpenGLView *view = [[NSOpenGLView alloc]
97                               initWithFrame:winRect
98                                 pixelFormat:pixelFormat];
99         assert(view != nil);
100
101         [window setContentView:view];
102         [window setTitle:@"glretrace"];
103
104     }
105
106     ~CocoaDrawable() {
107         [window release];
108     }
109
110     void
111     resize(int w, int h) {
112         if (w == width && h == height) {
113             return;
114         }
115
116         [window setContentSize:NSMakeSize(w, h)];
117
118         if (currentContext != nil) {
119             [currentContext update];
120             [window makeKeyAndOrderFront:nil];
121             [currentContext setView:[window contentView]];
122             [currentContext makeCurrentContext];
123         }
124
125         Drawable::resize(w, h);
126     }
127
128     void show(void) {
129         if (visible) {
130             return;
131         }
132
133         // TODO
134
135         Drawable::show();
136     }
137
138     void swapBuffers(void) {
139         if (currentContext != nil) {
140             [currentContext flushBuffer];
141         }
142     }
143 };
144
145
146 class CocoaContext : public Context
147 {
148 public:
149     NSOpenGLContext *context;
150
151     CocoaContext(const Visual *vis, Profile prof, NSOpenGLContext *ctx) :
152         Context(vis, prof),
153         context(ctx)
154     {}
155
156     ~CocoaContext() {
157         [context release];
158     }
159 };
160
161
162 static inline void
163 initThread(void) {
164     if (autoreleasePool == nil) {
165         autoreleasePool = [[NSAutoreleasePool alloc] init];
166     }
167 }
168
169 void
170 init(void) {
171     // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
172     _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
173
174     initThread();
175
176     [NSApplication sharedApplication];
177
178     [NSApp finishLaunching];
179 }
180
181
182 void
183 cleanup(void) {
184     [autoreleasePool release];
185 }
186
187
188 Visual *
189 createVisual(bool doubleBuffer, Profile profile) {
190
191     initThread();
192
193     if (profile != PROFILE_COMPAT &&
194         profile != PROFILE_CORE) {
195         return nil;
196     }
197
198     Attributes<NSOpenGLPixelFormatAttribute> attribs;
199
200     attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
201     attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
202     if (doubleBuffer) {
203         attribs.add(NSOpenGLPFADoubleBuffer);
204     }
205     attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
206     attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
207     if (profile == PROFILE_CORE) {
208 #if CGL_VERSION_1_3
209         attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
210 #else
211         return NULL;
212 #endif
213     }
214     
215     // Use Apple software rendering for debugging purposes.
216     if (0) {
217         attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
218     }
219
220     attribs.end();
221
222     NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
223                                      initWithAttributes:attribs];
224
225     return new CocoaVisual(pixelFormat);
226 }
227
228 Drawable *
229 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
230 {
231     initThread();
232
233     return new CocoaDrawable(visual, width, height, pbuffer);
234 }
235
236 Context *
237 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
238 {
239     initThread();
240
241     NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
242     NSOpenGLContext *share_context = nil;
243     NSOpenGLContext *context;
244
245     if (profile != PROFILE_COMPAT &&
246         profile != PROFILE_CORE) {
247         return nil;
248     }
249
250     if (shareContext) {
251         share_context = static_cast<CocoaContext*>(shareContext)->context;
252     }
253
254     context = [[NSOpenGLContext alloc]
255                initWithFormat:pixelFormat
256                shareContext:share_context];
257     assert(context != nil);
258
259     return new CocoaContext(visual, profile, context);
260 }
261
262 bool
263 makeCurrent(Drawable *drawable, Context *context)
264 {
265     initThread();
266
267     if (!drawable || !context) {
268         [NSOpenGLContext clearCurrentContext];
269     } else {
270         CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
271         CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
272
273         [cocoaDrawable->window makeKeyAndOrderFront:nil];
274         [cocoaContext->context setView:[cocoaDrawable->window contentView]];
275         [cocoaContext->context makeCurrentContext];
276
277         cocoaDrawable->currentContext = cocoaContext->context;
278     }
279
280     return TRUE;
281 }
282
283 bool
284 processEvents(void) {
285     initThread();
286
287     NSEvent* event;
288     do {
289         event = [NSApp nextEventMatchingMask:NSAnyEventMask
290                                    untilDate:[NSDate distantPast]
291                                       inMode:NSDefaultRunLoopMode
292                                      dequeue:YES];
293         if (event)
294             [NSApp sendEvent:event];
295     } while (event);
296
297     return true;
298 }
299
300
301 } /* namespace glws */