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