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 trace::Value *attribs = &call.arg(0);
130 int profile = glretrace::parseAttrib(attribs, kCGLPFAOpenGLProfile, kCGLOGLPVersion_Legacy);
132 if (profile == kCGLOGLPVersion_3_2_Core) {
133 // TODO: Do this on a per visual basis
134 retrace::coreProfile = true;
139 static void retrace_CGLCreateContext(trace::Call &call) {
140 unsigned long long share = call.arg(1).toUIntPtr();
141 Context *sharedContext = getContext(share);
143 const trace::Array *ctx_ptr = dynamic_cast<const trace::Array *>(&call.arg(2));
144 unsigned long long ctx = ctx_ptr->values[0]->toUIntPtr();
146 Context *context = glretrace::createContext(sharedContext);
147 context_map[ctx] = context;
151 static void retrace_CGLDestroyContext(trace::Call &call) {
152 unsigned long long ctx = call.arg(0).toUIntPtr();
154 ContextMap::iterator it;
155 it = context_map.find(ctx);
156 if (it == context_map.end()) {
162 context_map.erase(it);
166 static void retrace_CGLSetCurrentContext(trace::Call &call) {
167 unsigned long long ctx = call.arg(0).toUIntPtr();
169 glws::Drawable *new_drawable = getDrawable(ctx);
170 Context *new_context = getContext(ctx);
172 glretrace::makeCurrent(call, new_drawable, new_context);
176 static void retrace_CGLFlushDrawable(trace::Call &call) {
177 if (currentDrawable && currentContext) {
178 if (retrace::doubleBuffer) {
179 currentDrawable->swapBuffers();
184 frame_complete(call);
190 * We can't fully reimplement CGLTexImageIOSurface2D, as external IOSurface are
191 * no longer present. Simply emit a glTexImage2D to ensure the texture storage
195 * - /System/Library/Frameworks/OpenGL.framework/Headers/CGLIOSurface.h
197 static void retrace_CGLTexImageIOSurface2D(trace::Call &call) {
198 if (retrace::debug) {
199 retrace::warning(call) << "external IOSurface not supported\n";
202 unsigned long long ctx = call.arg(0).toUIntPtr();
203 Context *context = getContext(ctx);
206 target = static_cast<GLenum>((call.arg(1)).toSInt());
210 GLint internalformat;
211 internalformat = static_cast<GLenum>((call.arg(2)).toSInt());
214 width = (call.arg(3)).toSInt();
217 height = (call.arg(4)).toSInt();
222 format = static_cast<GLenum>((call.arg(5)).toSInt());
225 type = static_cast<GLenum>((call.arg(6)).toSInt());
227 GLvoid * pixels = NULL;
229 if (glretrace::currentContext != context) {
230 if (retrace::debug) {
231 retrace::warning(call) << "current context mismatch\n";
235 glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
237 if (retrace::debug && !glretrace::insideGlBeginEnd) {
238 glretrace::checkGlError(call);
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},