]> git.cworth.org Git - apitrace/blob - retrace/glretrace_cgl.cpp
glretrace: Ignore a few more CGL calls.
[apitrace] / retrace / glretrace_cgl.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
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 #include <string.h>
28
29 #include "glproc.hpp"
30 #include "retrace.hpp"
31 #include "glretrace.hpp"
32
33
34 #define kCGLPFAAllRenderers            1
35 #define kCGLPFADoubleBuffer            5
36 #define kCGLPFAStereo                  6
37 #define kCGLPFAAuxBuffers              7
38 #define kCGLPFAColorSize               8
39 #define kCGLPFAAlphaSize              11
40 #define kCGLPFADepthSize              12
41 #define kCGLPFAStencilSize            13
42 #define kCGLPFAAccumSize              14
43 #define kCGLPFAMinimumPolicy          51
44 #define kCGLPFAMaximumPolicy          52
45 #define kCGLPFAOffScreen              53
46 #define kCGLPFAFullScreen             54
47 #define kCGLPFASampleBuffers          55
48 #define kCGLPFASamples                56
49 #define kCGLPFAAuxDepthStencil        57
50 #define kCGLPFAColorFloat             58
51 #define kCGLPFAMultisample            59
52 #define kCGLPFASupersample            60
53 #define kCGLPFASampleAlpha            61
54 #define kCGLPFARendererID             70
55 #define kCGLPFASingleRenderer         71
56 #define kCGLPFANoRecovery             72
57 #define kCGLPFAAccelerated            73
58 #define kCGLPFAClosestPolicy          74
59 #define kCGLPFARobust                 75
60 #define kCGLPFABackingStore           76
61 #define kCGLPFAMPSafe                 78
62 #define kCGLPFAWindow                 80
63 #define kCGLPFAMultiScreen            81
64 #define kCGLPFACompliant              83
65 #define kCGLPFADisplayMask            84
66 #define kCGLPFAPBuffer                90
67 #define kCGLPFARemotePBuffer          91
68 #define kCGLPFAAllowOfflineRenderers  96
69 #define kCGLPFAAcceleratedCompute     97
70 #define kCGLPFAOpenGLProfile          99
71 #define kCGLPFAVirtualScreenCount    128
72
73 #define kCGLOGLPVersion_Legacy   0x1000
74 #define kCGLOGLPVersion_3_2_Core 0x3200
75
76
77 using namespace glretrace;
78
79
80 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
81 typedef std::map<unsigned long long, Context *> ContextMap;
82 static DrawableMap drawable_map;
83 static ContextMap context_map;
84 static Context *sharedContext = NULL;
85
86
87 static glws::Drawable *
88 getDrawable(unsigned long drawable_id) {
89     if (drawable_id == 0) {
90         return NULL;
91     }
92
93     /* XXX: Support multiple drawables. */
94     drawable_id = 1;
95
96     DrawableMap::const_iterator it;
97     it = drawable_map.find(drawable_id);
98     if (it == drawable_map.end()) {
99         return (drawable_map[drawable_id] = glretrace::createDrawable());
100     }
101
102     return it->second;
103 }
104
105
106 static Context *
107 getContext(unsigned long long ctx) {
108     if (ctx == 0) {
109         return NULL;
110     }
111
112     ContextMap::const_iterator it;
113     it = context_map.find(ctx);
114     if (it == context_map.end()) {
115         Context *context;
116         context_map[ctx] = context = glretrace::createContext(sharedContext);
117         if (!sharedContext) {
118             sharedContext = context;
119         }
120         return context;
121     }
122
123     return it->second;
124 }
125
126
127 static void retrace_CGLChoosePixelFormat(trace::Call &call) {
128     int profile = kCGLOGLPVersion_Legacy;
129
130     const trace::Array * attribs = dynamic_cast<const trace::Array *>(&call.arg(0));
131     if (attribs) {
132         size_t i = 0;
133         while (i < attribs->values.size()) {
134             int param = attribs->values[i++]->toSInt();
135             if (param == 0) {
136                 break;
137             }
138
139             switch (param) {
140             case kCGLPFAAllRenderers:
141             case kCGLPFADoubleBuffer:
142             case kCGLPFAStereo:
143             case kCGLPFAAuxBuffers:
144             case kCGLPFAMinimumPolicy:
145             case kCGLPFAMaximumPolicy:
146             case kCGLPFAOffScreen:
147             case kCGLPFAFullScreen:
148             case kCGLPFAAuxDepthStencil:
149             case kCGLPFAColorFloat:
150             case kCGLPFAMultisample:
151             case kCGLPFASupersample:
152             case kCGLPFASampleAlpha:
153             case kCGLPFASingleRenderer:
154             case kCGLPFANoRecovery:
155             case kCGLPFAAccelerated:
156             case kCGLPFAClosestPolicy:
157             case kCGLPFARobust:
158             case kCGLPFABackingStore:
159             case kCGLPFAMPSafe:
160             case kCGLPFAWindow:
161             case kCGLPFAMultiScreen:
162             case kCGLPFACompliant:
163             case kCGLPFAPBuffer:
164             case kCGLPFARemotePBuffer:
165             case kCGLPFAAllowOfflineRenderers:
166             case kCGLPFAAcceleratedCompute:
167                 break;
168
169             case kCGLPFAColorSize:
170             case kCGLPFAAlphaSize:
171             case kCGLPFADepthSize:
172             case kCGLPFAStencilSize:
173             case kCGLPFAAccumSize:
174             case kCGLPFASampleBuffers:
175             case kCGLPFASamples:
176             case kCGLPFARendererID:
177             case kCGLPFADisplayMask:
178             case kCGLPFAVirtualScreenCount:
179                 ++i;
180                 break;
181
182             case kCGLPFAOpenGLProfile:
183                 profile = attribs->values[i++]->toSInt();
184                 break;
185
186             default:
187                 retrace::warning(call) << "unexpected attribute " << param << "\n";
188                 break;
189             }
190         }
191     }
192
193     if (profile == kCGLOGLPVersion_3_2_Core) {
194         // TODO: Do this on a per visual basis
195         retrace::coreProfile = true;
196     }
197 }
198
199
200 static void retrace_CGLCreateContext(trace::Call &call) {
201     unsigned long long share = call.arg(1).toUIntPtr();
202     Context *sharedContext = getContext(share);
203
204     const trace::Array *ctx_ptr = dynamic_cast<const trace::Array *>(&call.arg(2));
205     unsigned long long ctx = ctx_ptr->values[0]->toUIntPtr();
206
207     Context *context = glretrace::createContext(sharedContext);
208     context_map[ctx] = context;
209 }
210
211
212 static void retrace_CGLDestroyContext(trace::Call &call) {
213     unsigned long long ctx = call.arg(0).toUIntPtr();
214
215     ContextMap::iterator it;
216     it = context_map.find(ctx);
217     if (it == context_map.end()) {
218         return;
219     }
220
221     delete it->second;
222
223     context_map.erase(it);
224 }
225
226
227 static void retrace_CGLSetCurrentContext(trace::Call &call) {
228     unsigned long long ctx = call.arg(0).toUIntPtr();
229
230     glws::Drawable *new_drawable = getDrawable(ctx);
231     Context *new_context = getContext(ctx);
232
233     glretrace::makeCurrent(call, new_drawable, new_context);
234 }
235
236
237 static void retrace_CGLFlushDrawable(trace::Call &call) {
238     unsigned long long ctx = call.arg(0).toUIntPtr();
239     Context *context = getContext(ctx);
240
241     if (context) {
242         if (context->drawable) {
243             if (retrace::doubleBuffer) {
244                 context->drawable->swapBuffers();
245             } else {
246                 glFlush();
247             }
248             frame_complete(call);
249         } else {
250             if (retrace::debug) {
251                 retrace::warning(call) << "context has no drawable\n";
252             }
253         }
254     }
255 }
256
257
258 /**
259  * We can't fully reimplement CGLTexImageIOSurface2D, as external IOSurface are
260  * no longer present.  Simply emit a glTexImage2D to ensure the texture storage
261  * is present.
262  *
263  * See also:
264  * - /System/Library/Frameworks/OpenGL.framework/Headers/CGLIOSurface.h
265  */
266 static void retrace_CGLTexImageIOSurface2D(trace::Call &call) {
267     if (retrace::debug) {
268         retrace::warning(call) << "external IOSurface not supported\n";
269     }
270
271     unsigned long long ctx = call.arg(0).toUIntPtr();
272     Context *context = getContext(ctx);
273
274     GLenum target;
275     target = static_cast<GLenum>((call.arg(1)).toSInt());
276
277     GLint level = 0;
278
279     GLint internalformat;
280     internalformat = static_cast<GLenum>((call.arg(2)).toSInt());
281
282     GLsizei width;
283     width = (call.arg(3)).toSInt();
284
285     GLsizei height;
286     height = (call.arg(4)).toSInt();
287
288     GLint border = 0;
289
290     GLenum format;
291     format = static_cast<GLenum>((call.arg(5)).toSInt());
292
293     GLenum type;
294     type = static_cast<GLenum>((call.arg(6)).toSInt());
295
296     GLvoid * pixels = NULL;
297
298     if (glretrace::getCurrentContext() != context) {
299         if (retrace::debug) {
300             retrace::warning(call) << "current context mismatch\n";
301         }
302     }
303
304     glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
305
306     if (retrace::debug && !glretrace::insideGlBeginEnd) {
307         glretrace::checkGlError(call);
308     }
309 }
310
311
312 const retrace::Entry glretrace::cgl_callbacks[] = {
313     {"CGLChoosePixelFormat", &retrace_CGLChoosePixelFormat},
314     {"CGLCreateContext", &retrace_CGLCreateContext},
315     {"CGLDestroyContext", &retrace_CGLDestroyContext},
316     {"CGLDestroyPixelFormat", &retrace::ignore},
317     {"CGLDisable", &retrace::ignore},
318     {"CGLEnable", &retrace::ignore},
319     {"CGLErrorString", &retrace::ignore},
320     {"CGLFlushDrawable", &retrace_CGLFlushDrawable},
321     {"CGLGetCurrentContext", &retrace::ignore},
322     {"CGLGetOption", &retrace::ignore},
323     {"CGLGetParameter", &retrace::ignore},
324     {"CGLGetVersion", &retrace::ignore},
325     {"CGLGetVirtualScreen", &retrace::ignore},
326     {"CGLIsEnabled", &retrace::ignore},
327     {"CGLSetCurrentContext", &retrace_CGLSetCurrentContext},
328     {"CGLSetParameter", &retrace::ignore},
329     {"CGLTexImageIOSurface2D", &retrace_CGLTexImageIOSurface2D},
330     {"CGLUpdateContext", &retrace::ignore},
331     {NULL, NULL},
332 };
333