]> git.cworth.org Git - apitrace/blob - glretrace_egl.cpp
Add "apitrace trim" command.
[apitrace] / glretrace_egl.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 LunarG, Inc.
4  * All Rights Reserved.
5  *
6  * Based on glretrace_glx.cpp, which has
7  *
8  *   Copyright 2011 Jose Fonseca
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  **************************************************************************/
29
30
31 #include "glproc.hpp"
32 #include "retrace.hpp"
33 #include "glretrace.hpp"
34 #include "os.hpp"
35
36 #ifndef EGL_OPENGL_ES_API
37 #define EGL_OPENGL_ES_API               0x30A0
38 #define EGL_OPENVG_API                  0x30A1
39 #define EGL_OPENGL_API                  0x30A2
40 #define EGL_CONTEXT_CLIENT_VERSION      0x3098
41 #endif
42
43
44 using namespace glretrace;
45
46
47 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
48 typedef std::map<unsigned long long, glws::Context *> ContextMap;
49 typedef std::map<unsigned long long, glws::Profile> ProfileMap;
50 static DrawableMap drawable_map;
51 static ContextMap context_map;
52 static ProfileMap profile_map;
53
54 static unsigned int current_api = EGL_OPENGL_ES_API;
55 static glws::Profile last_profile = glws::PROFILE_COMPAT;
56
57 static glws::Drawable *
58 getDrawable(unsigned long long surface_ptr) {
59     if (surface_ptr == 0) {
60         return NULL;
61     }
62
63     DrawableMap::const_iterator it;
64     it = drawable_map.find(surface_ptr);
65
66     return (it != drawable_map.end()) ? it->second : NULL;
67 }
68
69 static glws::Context *
70 getContext(unsigned long long context_ptr) {
71     if (context_ptr == 0) {
72         return NULL;
73     }
74
75     ContextMap::const_iterator it;
76     it = context_map.find(context_ptr);
77
78     return (it != context_map.end()) ? it->second : NULL;
79 }
80
81 static void createDrawable(unsigned long long orig_config, unsigned long long orig_surface)
82 {
83     ProfileMap::iterator it = profile_map.find(orig_config);
84     glws::Profile profile;
85
86     // If the requested config is associated with a profile, use that
87     // profile. Otherwise, assume that the last used profile is what
88     // the user wants.
89     if (it != profile_map.end()) {
90         profile = it->second;
91     } else {
92         profile = last_profile;
93     }
94
95     glws::Visual *visual = glretrace::visual[profile];
96
97     glws::Drawable *drawable = glws::createDrawable(visual);
98     drawable_map[orig_surface] = drawable;
99 }
100
101 static void retrace_eglCreateWindowSurface(trace::Call &call) {
102     unsigned long long orig_config = call.arg(1).toUIntPtr();
103     unsigned long long orig_surface = call.ret->toUIntPtr();
104     createDrawable(orig_config, orig_surface);
105 }
106
107 static void retrace_eglCreatePbufferSurface(trace::Call &call) {
108     unsigned long long orig_config = call.arg(1).toUIntPtr();
109     unsigned long long orig_surface = call.ret->toUIntPtr();
110     createDrawable(orig_config, orig_surface);
111     // TODO: Respect the pbuffer dimensions too
112 }
113
114 static void retrace_eglDestroySurface(trace::Call &call) {
115     unsigned long long orig_surface = call.arg(1).toUIntPtr();
116
117     DrawableMap::iterator it;
118     it = drawable_map.find(orig_surface);
119
120     if (it != drawable_map.end()) {
121         if (it->second != drawable) {
122             // TODO: reference count
123             delete it->second;
124         }
125         drawable_map.erase(it);
126     }
127 }
128
129 static void retrace_eglBindAPI(trace::Call &call) {
130     current_api = call.arg(0).toUInt();
131 }
132
133 static void retrace_eglCreateContext(trace::Call &call) {
134     unsigned long long orig_context = call.ret->toUIntPtr();
135     unsigned long long orig_config = call.arg(1).toUIntPtr();
136     glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
137     trace::Array *attrib_array = dynamic_cast<trace::Array *>(&call.arg(3));
138     glws::Profile profile;
139
140     switch (current_api) {
141     case EGL_OPENGL_API:
142         profile = glws::PROFILE_COMPAT;
143         break;
144     case EGL_OPENGL_ES_API:
145     default:
146         profile = glws::PROFILE_ES1;
147         if (attrib_array) {
148             for (int i = 0; i < attrib_array->values.size(); i += 2) {
149                 int v = attrib_array->values[i]->toSInt();
150                 if (v == EGL_CONTEXT_CLIENT_VERSION) {
151                     v = attrib_array->values[i + 1]->toSInt();
152                     if (v == 2)
153                         profile = glws::PROFILE_ES2;
154                     break;
155                 }
156             }
157         }
158         break;
159     }
160
161
162     glws::Context *context = glws::createContext(glretrace::visual[profile], share_context, profile);
163     if (!context) {
164         const char *name;
165         switch (profile) {
166         case glws::PROFILE_COMPAT:
167             name = "OpenGL";
168             break;
169         case glws::PROFILE_ES1:
170             name = "OpenGL ES 1.1";
171             break;
172         case glws::PROFILE_ES2:
173             name = "OpenGL ES 2.0";
174             break;
175         default:
176             name = "unknown";
177             break;
178         }
179
180         retrace::warning(call) << "Failed to create " << name << " context.\n";
181         os::abort();
182     }
183
184     context_map[orig_context] = context;
185     profile_map[orig_config] = profile;
186     last_profile = profile;
187 }
188
189 static void retrace_eglDestroyContext(trace::Call &call) {
190     unsigned long long orig_context = call.arg(1).toUIntPtr();
191
192     ContextMap::iterator it;
193     it = context_map.find(orig_context);
194
195     if (it != context_map.end()) {
196         delete it->second;
197         context_map.erase(it);
198     }
199 }
200
201 static void retrace_eglMakeCurrent(trace::Call &call) {
202     glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
203     glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
204
205     if (new_drawable == drawable && new_context == context) {
206         return;
207     }
208
209     if (drawable && context) {
210         glFlush();
211         if (!double_buffer) {
212             frame_complete(call);
213         }
214     }
215
216     bool result = glws::makeCurrent(new_drawable, new_context);
217
218     if (new_drawable && new_context && result) {
219         drawable = new_drawable;
220         context = new_context;
221     } else {
222         drawable = NULL;
223         context = NULL;
224     }
225 }
226
227
228 static void retrace_eglSwapBuffers(trace::Call &call) {
229     frame_complete(call);
230
231     if (double_buffer) {
232         drawable->swapBuffers();
233     } else {
234         glFlush();
235     }
236 }
237
238 const retrace::Entry glretrace::egl_callbacks[] = {
239     {"eglGetError", &retrace::ignore},
240     {"eglGetDisplay", &retrace::ignore},
241     {"eglInitialize", &retrace::ignore},
242     {"eglTerminate", &retrace::ignore},
243     {"eglQueryString", &retrace::ignore},
244     {"eglGetConfigs", &retrace::ignore},
245     {"eglChooseConfig", &retrace::ignore},
246     {"eglGetConfigAttrib", &retrace::ignore},
247     {"eglCreateWindowSurface", &retrace_eglCreateWindowSurface},
248     {"eglCreatePbufferSurface", &retrace_eglCreatePbufferSurface},
249     //{"eglCreatePixmapSurface", &retrace::ignore},
250     {"eglDestroySurface", &retrace_eglDestroySurface},
251     {"eglQuerySurface", &retrace::ignore},
252     {"eglBindAPI", &retrace_eglBindAPI},
253     {"eglQueryAPI", &retrace::ignore},
254     //{"eglWaitClient", &retrace::ignore},
255     //{"eglReleaseThread", &retrace::ignore},
256     //{"eglCreatePbufferFromClientBuffer", &retrace::ignore},
257     //{"eglSurfaceAttrib", &retrace::ignore},
258     //{"eglBindTexImage", &retrace::ignore},
259     //{"eglReleaseTexImage", &retrace::ignore},
260     {"eglSwapInterval", &retrace::ignore},
261     {"eglCreateContext", &retrace_eglCreateContext},
262     {"eglDestroyContext", &retrace_eglDestroyContext},
263     {"eglMakeCurrent", &retrace_eglMakeCurrent},
264     {"eglGetCurrentContext", &retrace::ignore},
265     {"eglGetCurrentSurface", &retrace::ignore},
266     {"eglGetCurrentDisplay", &retrace::ignore},
267     {"eglQueryContext", &retrace::ignore},
268     {"eglWaitGL", &retrace::ignore},
269     {"eglWaitNative", &retrace::ignore},
270     {"eglSwapBuffers", &retrace_eglSwapBuffers},
271     //{"eglCopyBuffers", &retrace::ignore},
272     {"eglGetProcAddress", &retrace::ignore},
273     {NULL, NULL},
274 };