1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
30 #include "retrace.hpp"
31 #include "glretrace.hpp"
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
73 #define kCGLOGLPVersion_Legacy 0x1000
74 #define kCGLOGLPVersion_3_2_Core 0x3200
77 using namespace glretrace;
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;
87 static glws::Drawable *
88 getDrawable(unsigned long drawable_id) {
89 if (drawable_id == 0) {
93 /* XXX: Support multiple drawables. */
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());
107 getContext(unsigned long long ctx) {
112 ContextMap::const_iterator it;
113 it = context_map.find(ctx);
114 if (it == context_map.end()) {
116 context_map[ctx] = context = glretrace::createContext(sharedContext);
117 if (!sharedContext) {
118 sharedContext = context;
127 static void retrace_CGLChoosePixelFormat(trace::Call &call) {
128 int profile = kCGLOGLPVersion_Legacy;
130 const trace::Array * attribs = dynamic_cast<const trace::Array *>(&call.arg(0));
133 while (i < attribs->values.size()) {
134 int param = attribs->values[i++]->toSInt();
140 case kCGLPFAAllRenderers:
141 case kCGLPFADoubleBuffer:
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:
158 case kCGLPFABackingStore:
161 case kCGLPFAMultiScreen:
162 case kCGLPFACompliant:
164 case kCGLPFARemotePBuffer:
165 case kCGLPFAAllowOfflineRenderers:
166 case kCGLPFAAcceleratedCompute:
169 case kCGLPFAColorSize:
170 case kCGLPFAAlphaSize:
171 case kCGLPFADepthSize:
172 case kCGLPFAStencilSize:
173 case kCGLPFAAccumSize:
174 case kCGLPFASampleBuffers:
176 case kCGLPFARendererID:
177 case kCGLPFADisplayMask:
178 case kCGLPFAVirtualScreenCount:
182 case kCGLPFAOpenGLProfile:
183 profile = attribs->values[i++]->toSInt();
187 retrace::warning(call) << "unexpected attribute " << param << "\n";
193 if (profile == kCGLOGLPVersion_3_2_Core) {
194 // TODO: Do this on a per visual basis
195 retrace::coreProfile = true;
200 static void retrace_CGLCreateContext(trace::Call &call) {
201 unsigned long long share = call.arg(1).toUIntPtr();
202 Context *sharedContext = getContext(share);
204 const trace::Array *ctx_ptr = dynamic_cast<const trace::Array *>(&call.arg(2));
205 unsigned long long ctx = ctx_ptr->values[0]->toUIntPtr();
207 Context *context = glretrace::createContext(sharedContext);
208 context_map[ctx] = context;
212 static void retrace_CGLDestroyContext(trace::Call &call) {
213 unsigned long long ctx = call.arg(0).toUIntPtr();
215 ContextMap::iterator it;
216 it = context_map.find(ctx);
217 if (it == context_map.end()) {
223 context_map.erase(it);
227 static void retrace_CGLSetCurrentContext(trace::Call &call) {
228 unsigned long long ctx = call.arg(0).toUIntPtr();
230 glws::Drawable *new_drawable = getDrawable(ctx);
231 Context *new_context = getContext(ctx);
233 glretrace::makeCurrent(call, new_drawable, new_context);
237 static void retrace_CGLFlushDrawable(trace::Call &call) {
238 unsigned long long ctx = call.arg(0).toUIntPtr();
239 Context *context = getContext(ctx);
242 if (context->drawable) {
243 if (retrace::doubleBuffer) {
244 context->drawable->swapBuffers();
248 frame_complete(call);
250 if (retrace::debug) {
251 retrace::warning(call) << "context has no drawable\n";
259 * We can't fully reimplement CGLTexImageIOSurface2D, as external IOSurface are
260 * no longer present. Simply emit a glTexImage2D to ensure the texture storage
264 * - /System/Library/Frameworks/OpenGL.framework/Headers/CGLIOSurface.h
266 static void retrace_CGLTexImageIOSurface2D(trace::Call &call) {
267 if (retrace::debug) {
268 retrace::warning(call) << "external IOSurface not supported\n";
271 unsigned long long ctx = call.arg(0).toUIntPtr();
272 Context *context = getContext(ctx);
275 target = static_cast<GLenum>((call.arg(1)).toSInt());
279 GLint internalformat;
280 internalformat = static_cast<GLenum>((call.arg(2)).toSInt());
283 width = (call.arg(3)).toSInt();
286 height = (call.arg(4)).toSInt();
291 format = static_cast<GLenum>((call.arg(5)).toSInt());
294 type = static_cast<GLenum>((call.arg(6)).toSInt());
296 GLvoid * pixels = NULL;
298 if (glretrace::getCurrentContext() != context) {
299 if (retrace::debug) {
300 retrace::warning(call) << "current context mismatch\n";
304 glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
306 if (retrace::debug && !glretrace::insideGlBeginEnd) {
307 glretrace::checkGlError(call);
312 const retrace::Entry glretrace::cgl_callbacks[] = {
313 {"CGLChoosePixelFormat", &retrace_CGLChoosePixelFormat},
314 {"CGLDestroyPixelFormat", &retrace::ignore},
315 {"CGLCreateContext", &retrace_CGLCreateContext},
316 {"CGLDestroyContext", &retrace_CGLDestroyContext},
317 {"CGLSetCurrentContext", &retrace_CGLSetCurrentContext},
318 {"CGLGetCurrentContext", &retrace::ignore},
319 {"CGLEnable", &retrace::ignore},
320 {"CGLDisable", &retrace::ignore},
321 {"CGLSetParameter", &retrace::ignore},
322 {"CGLGetParameter", &retrace::ignore},
323 {"CGLFlushDrawable", &retrace_CGLFlushDrawable},
324 {"CGLUpdateContext", &retrace::ignore},
325 {"CGLTexImageIOSurface2D", &retrace_CGLTexImageIOSurface2D},