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