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