]> git.cworth.org Git - apitrace/blob - retrace/glretrace_cgl.cpp
Determine core32 from the traces.
[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     trace::Value *attribs = &call.arg(0);
129
130     int profile = glretrace::parseAttrib(attribs, kCGLPFAOpenGLProfile, kCGLOGLPVersion_Legacy);
131
132     if (profile == kCGLOGLPVersion_3_2_Core) {
133         // TODO: Do this on a per visual basis
134         retrace::coreProfile = true;
135     }
136 }
137
138
139 static void retrace_CGLCreateContext(trace::Call &call) {
140     unsigned long long share = call.arg(1).toUIntPtr();
141     Context *sharedContext = getContext(share);
142
143     const trace::Array *ctx_ptr = dynamic_cast<const trace::Array *>(&call.arg(2));
144     unsigned long long ctx = ctx_ptr->values[0]->toUIntPtr();
145
146     Context *context = glretrace::createContext(sharedContext);
147     context_map[ctx] = context;
148 }
149
150
151 static void retrace_CGLDestroyContext(trace::Call &call) {
152     unsigned long long ctx = call.arg(0).toUIntPtr();
153
154     ContextMap::iterator it;
155     it = context_map.find(ctx);
156     if (it == context_map.end()) {
157         return;
158     }
159
160     delete it->second;
161
162     context_map.erase(it);
163 }
164
165
166 static void retrace_CGLSetCurrentContext(trace::Call &call) {
167     unsigned long long ctx = call.arg(0).toUIntPtr();
168
169     glws::Drawable *new_drawable = getDrawable(ctx);
170     Context *new_context = getContext(ctx);
171
172     glretrace::makeCurrent(call, new_drawable, new_context);
173 }
174
175
176 static void retrace_CGLFlushDrawable(trace::Call &call) {
177     if (currentDrawable && currentContext) {
178         if (retrace::doubleBuffer) {
179             currentDrawable->swapBuffers();
180         } else {
181             glFlush();
182         }
183
184         frame_complete(call);
185     }
186 }
187
188
189 /**
190  * We can't fully reimplement CGLTexImageIOSurface2D, as external IOSurface are
191  * no longer present.  Simply emit a glTexImage2D to ensure the texture storage
192  * is present.
193  *
194  * See also:
195  * - /System/Library/Frameworks/OpenGL.framework/Headers/CGLIOSurface.h
196  */
197 static void retrace_CGLTexImageIOSurface2D(trace::Call &call) {
198     if (retrace::debug) {
199         retrace::warning(call) << "external IOSurface not supported\n";
200     }
201
202     unsigned long long ctx = call.arg(0).toUIntPtr();
203     Context *context = getContext(ctx);
204
205     GLenum target;
206     target = static_cast<GLenum>((call.arg(1)).toSInt());
207
208     GLint level = 0;
209
210     GLint internalformat;
211     internalformat = static_cast<GLenum>((call.arg(2)).toSInt());
212
213     GLsizei width;
214     width = (call.arg(3)).toSInt();
215
216     GLsizei height;
217     height = (call.arg(4)).toSInt();
218
219     GLint border = 0;
220
221     GLenum format;
222     format = static_cast<GLenum>((call.arg(5)).toSInt());
223
224     GLenum type;
225     type = static_cast<GLenum>((call.arg(6)).toSInt());
226
227     GLvoid * pixels = NULL;
228
229     if (glretrace::currentContext != context) {
230         if (retrace::debug) {
231             retrace::warning(call) << "current context mismatch\n";
232         }
233     }
234
235     glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
236
237     if (retrace::debug && !glretrace::insideGlBeginEnd) {
238         glretrace::checkGlError(call);
239     }
240 }
241
242
243 const retrace::Entry glretrace::cgl_callbacks[] = {
244     {"CGLChoosePixelFormat", &retrace_CGLChoosePixelFormat},
245     {"CGLDestroyPixelFormat", &retrace::ignore},
246     {"CGLCreateContext", &retrace_CGLCreateContext},
247     {"CGLDestroyContext", &retrace_CGLDestroyContext},
248     {"CGLSetCurrentContext", &retrace_CGLSetCurrentContext},
249     {"CGLGetCurrentContext", &retrace::ignore},
250     {"CGLEnable", &retrace::ignore},
251     {"CGLDisable", &retrace::ignore},
252     {"CGLSetParameter", &retrace::ignore},
253     {"CGLGetParameter", &retrace::ignore},
254     {"CGLFlushDrawable", &retrace_CGLFlushDrawable},
255     {"CGLTexImageIOSurface2D", &retrace_CGLTexImageIOSurface2D},
256     {NULL, NULL},
257 };
258