]> git.cworth.org Git - apitrace/blob - retrace/glws_cocoa.mm
Disassemble D3D10.1 shaders too.
[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     NSOpenGLView *view;
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         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:view];
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 void
163 init(void) {
164     // Prevent glproc to load system's OpenGL, so that we can trace glretrace.
165     _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
166
167     [NSApplication sharedApplication];
168
169     autoreleasePool = [[NSAutoreleasePool alloc] init];
170
171     [NSApp finishLaunching];
172 }
173
174
175 void
176 cleanup(void) {
177     [autoreleasePool release];
178 }
179
180
181 Visual *
182 createVisual(bool doubleBuffer, Profile profile) {
183     if (profile != PROFILE_COMPAT &&
184         profile != PROFILE_CORE) {
185         return nil;
186     }
187
188     Attributes<NSOpenGLPixelFormatAttribute> attribs;
189
190     attribs.add(NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)1);
191     attribs.add(NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24);
192     if (doubleBuffer) {
193         attribs.add(NSOpenGLPFADoubleBuffer);
194     }
195     attribs.add(NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)1);
196     attribs.add(NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)1);
197     if (profile == PROFILE_CORE) {
198 #if CGL_VERSION_1_3
199         attribs.add(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
200 #else
201         return NULL;
202 #endif
203     }
204     
205     // Use Apple software rendering for debugging purposes.
206     if (0) {
207         attribs.add(NSOpenGLPFARendererID, 0x00020200); // kCGLRendererGenericID
208     }
209
210     attribs.end();
211
212     NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
213                                      initWithAttributes:attribs];
214
215     return new CocoaVisual(pixelFormat);
216 }
217
218 Drawable *
219 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
220 {
221     return new CocoaDrawable(visual, width, height, pbuffer);
222 }
223
224 Context *
225 createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug)
226 {
227     NSOpenGLPixelFormat *pixelFormat = static_cast<const CocoaVisual *>(visual)->pixelFormat;
228     NSOpenGLContext *share_context = nil;
229     NSOpenGLContext *context;
230
231     if (profile != PROFILE_COMPAT &&
232         profile != PROFILE_CORE) {
233         return nil;
234     }
235
236     if (shareContext) {
237         share_context = static_cast<CocoaContext*>(shareContext)->context;
238     }
239
240     context = [[NSOpenGLContext alloc]
241                initWithFormat:pixelFormat
242                shareContext:share_context];
243     assert(context != nil);
244
245     return new CocoaContext(visual, profile, context);
246 }
247
248 bool
249 makeCurrent(Drawable *drawable, Context *context)
250 {
251     if (!drawable || !context) {
252         [NSOpenGLContext clearCurrentContext];
253     } else {
254         CocoaDrawable *cocoaDrawable = static_cast<CocoaDrawable *>(drawable);
255         CocoaContext *cocoaContext = static_cast<CocoaContext *>(context);
256
257         [cocoaDrawable->window makeKeyAndOrderFront:nil];
258         [cocoaContext->context setView:[cocoaDrawable->window contentView]];
259         [cocoaContext->context makeCurrentContext];
260
261         cocoaDrawable->currentContext = cocoaContext->context;
262     }
263
264     return TRUE;
265 }
266
267 bool
268 processEvents(void) {
269    NSEvent* event;
270
271     do {
272         event = [NSApp nextEventMatchingMask:NSAnyEventMask
273                                    untilDate:[NSDate distantPast]
274                                       inMode:NSDefaultRunLoopMode
275                                      dequeue:YES];
276         if (event)
277             [NSApp sendEvent:event];
278     } while (event);
279
280     return true;
281 }
282
283
284 } /* namespace glws */