]> git.cworth.org Git - apitrace/blob - retrace/glretrace_cgl.cpp
Ignore CGLChoose/DestroyPixelFormat 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 using namespace glretrace;
35
36
37 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
38 typedef std::map<unsigned long long, Context *> ContextMap;
39 static DrawableMap drawable_map;
40 static ContextMap context_map;
41 static Context *sharedContext = NULL;
42
43
44 static glws::Drawable *
45 getDrawable(unsigned long drawable_id) {
46     if (drawable_id == 0) {
47         return NULL;
48     }
49
50     /* XXX: Support multiple drawables. */
51     drawable_id = 1;
52
53     DrawableMap::const_iterator it;
54     it = drawable_map.find(drawable_id);
55     if (it == drawable_map.end()) {
56         return (drawable_map[drawable_id] = glretrace::createDrawable());
57     }
58
59     return it->second;
60 }
61
62
63 static Context *
64 getContext(unsigned long long ctx) {
65     if (ctx == 0) {
66         return NULL;
67     }
68
69     ContextMap::const_iterator it;
70     it = context_map.find(ctx);
71     if (it == context_map.end()) {
72         Context *context;
73         context_map[ctx] = context = glretrace::createContext(sharedContext);
74         if (!sharedContext) {
75             sharedContext = context;
76         }
77         return context;
78     }
79
80     return it->second;
81 }
82
83 static void retrace_CGLCreateContext(trace::Call &call) {
84     unsigned long long share = call.arg(1).toUIntPtr();
85     Context *sharedContext = getContext(share);
86
87     const trace::Array *ctx_ptr = dynamic_cast<const trace::Array *>(&call.arg(2));
88     unsigned long long ctx = ctx_ptr->values[0]->toUIntPtr();
89
90     Context *context = glretrace::createContext(sharedContext);
91     context_map[ctx] = context;
92 }
93
94
95 static void retrace_CGLDestroyContext(trace::Call &call) {
96     unsigned long long ctx = call.arg(0).toUIntPtr();
97
98     ContextMap::iterator it;
99     it = context_map.find(ctx);
100     if (it == context_map.end()) {
101         return;
102     }
103
104     delete it->second;
105
106     context_map.erase(it);
107 }
108
109
110 static void retrace_CGLSetCurrentContext(trace::Call &call) {
111     unsigned long long ctx = call.arg(0).toUIntPtr();
112
113     glws::Drawable *new_drawable = getDrawable(ctx);
114     Context *new_context = getContext(ctx);
115
116     glretrace::makeCurrent(call, new_drawable, new_context);
117 }
118
119
120 static void retrace_CGLFlushDrawable(trace::Call &call) {
121     if (currentDrawable && currentContext) {
122         if (retrace::doubleBuffer) {
123             currentDrawable->swapBuffers();
124         } else {
125             glFlush();
126         }
127
128         frame_complete(call);
129     }
130 }
131
132
133 /**
134  * We can't fully reimplement CGLTexImageIOSurface2D, as external IOSurface are
135  * no longer present.  Simply emit a glTexImage2D to ensure the texture storage
136  * is present.
137  *
138  * See also:
139  * - /System/Library/Frameworks/OpenGL.framework/Headers/CGLIOSurface.h
140  */
141 static void retrace_CGLTexImageIOSurface2D(trace::Call &call) {
142     if (retrace::debug) {
143         retrace::warning(call) << "external IOSurface not supported\n";
144     }
145
146     unsigned long long ctx = call.arg(0).toUIntPtr();
147     Context *context = getContext(ctx);
148
149     GLenum target;
150     target = static_cast<GLenum>((call.arg(1)).toSInt());
151
152     GLint level = 0;
153
154     GLint internalformat;
155     internalformat = static_cast<GLenum>((call.arg(2)).toSInt());
156
157     GLsizei width;
158     width = (call.arg(3)).toSInt();
159
160     GLsizei height;
161     height = (call.arg(4)).toSInt();
162
163     GLint border = 0;
164
165     GLenum format;
166     format = static_cast<GLenum>((call.arg(5)).toSInt());
167
168     GLenum type;
169     type = static_cast<GLenum>((call.arg(6)).toSInt());
170
171     GLvoid * pixels = NULL;
172
173     if (glretrace::currentContext != context) {
174         if (retrace::debug) {
175             retrace::warning(call) << "current context mismatch\n";
176         }
177     }
178
179     glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
180
181     if (retrace::debug && !glretrace::insideGlBeginEnd) {
182         glretrace::checkGlError(call);
183     }
184 }
185
186
187 const retrace::Entry glretrace::cgl_callbacks[] = {
188     {"CGLChoosePixelFormat", &retrace::ignore},
189     {"CGLDestroyPixelFormat", &retrace::ignore},
190     {"CGLCreateContext", &retrace_CGLCreateContext},
191     {"CGLDestroyContext", &retrace_CGLDestroyContext},
192     {"CGLSetCurrentContext", &retrace_CGLSetCurrentContext},
193     {"CGLGetCurrentContext", &retrace::ignore},
194     {"CGLEnable", &retrace::ignore},
195     {"CGLDisable", &retrace::ignore},
196     {"CGLSetParameter", &retrace::ignore},
197     {"CGLGetParameter", &retrace::ignore},
198     {"CGLFlushDrawable", &retrace_CGLFlushDrawable},
199     {"CGLTexImageIOSurface2D", &retrace_CGLTexImageIOSurface2D},
200     {NULL, NULL},
201 };
202