]> git.cworth.org Git - apitrace/blob - retrace/glretrace_egl.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / retrace / 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 #include "eglsize.hpp"
36
37 #ifndef EGL_OPENGL_ES_API
38 #define EGL_OPENGL_ES_API               0x30A0
39 #define EGL_OPENVG_API                  0x30A1
40 #define EGL_OPENGL_API                  0x30A2
41 #define EGL_CONTEXT_CLIENT_VERSION      0x3098
42 #endif
43
44
45 using namespace glretrace;
46
47
48 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
49 typedef std::map<unsigned long long, Context *> ContextMap;
50 typedef std::map<unsigned long long, glws::Profile> ProfileMap;
51 static DrawableMap drawable_map;
52 static ContextMap context_map;
53 static ProfileMap profile_map;
54
55 static unsigned int current_api = EGL_OPENGL_ES_API;
56 static glws::Profile last_profile = glws::PROFILE_COMPAT;
57
58 static void
59 createDrawable(unsigned long long orig_config, unsigned long long orig_surface);
60
61 static glws::Drawable *
62 getDrawable(unsigned long long surface_ptr) {
63     if (surface_ptr == 0) {
64         return NULL;
65     }
66
67     DrawableMap::const_iterator it;
68     it = drawable_map.find(surface_ptr);
69     if (it == drawable_map.end()) {
70         // In Fennec we get the egl window surface from Java which isn't
71         // traced, so just create a drawable if it doesn't exist in here
72         createDrawable(0, surface_ptr);
73         it = drawable_map.find(surface_ptr);
74         assert(it != drawable_map.end());
75     }
76
77     return (it != drawable_map.end()) ? it->second : NULL;
78 }
79
80 static Context *
81 getContext(unsigned long long context_ptr) {
82     if (context_ptr == 0) {
83         return NULL;
84     }
85
86     ContextMap::const_iterator it;
87     it = context_map.find(context_ptr);
88
89     return (it != context_map.end()) ? it->second : NULL;
90 }
91
92 static void createDrawable(unsigned long long orig_config, unsigned long long orig_surface)
93 {
94     ProfileMap::iterator it = profile_map.find(orig_config);
95     glws::Profile profile;
96
97     // If the requested config is associated with a profile, use that
98     // profile. Otherwise, assume that the last used profile is what
99     // the user wants.
100     if (it != profile_map.end()) {
101         profile = it->second;
102     } else {
103         profile = last_profile;
104     }
105
106     glws::Drawable *drawable = glretrace::createDrawable(profile);
107     drawable_map[orig_surface] = drawable;
108 }
109
110 static void retrace_eglCreateWindowSurface(trace::Call &call) {
111     unsigned long long orig_config = call.arg(1).toUIntPtr();
112     unsigned long long orig_surface = call.ret->toUIntPtr();
113     createDrawable(orig_config, orig_surface);
114 }
115
116 static void retrace_eglCreatePbufferSurface(trace::Call &call) {
117     unsigned long long orig_config = call.arg(1).toUIntPtr();
118     unsigned long long orig_surface = call.ret->toUIntPtr();
119     createDrawable(orig_config, orig_surface);
120     // TODO: Respect the pbuffer dimensions too
121 }
122
123 static void retrace_eglDestroySurface(trace::Call &call) {
124     unsigned long long orig_surface = call.arg(1).toUIntPtr();
125
126     DrawableMap::iterator it;
127     it = drawable_map.find(orig_surface);
128
129     if (it != drawable_map.end()) {
130         glretrace::Context *currentContext = glretrace::getCurrentContext();
131         if (!currentContext || it->second != currentContext->drawable) {
132             // TODO: reference count
133             delete it->second;
134         }
135         drawable_map.erase(it);
136     }
137 }
138
139 static void retrace_eglBindAPI(trace::Call &call) {
140     current_api = call.arg(0).toUInt();
141 }
142
143 static void retrace_eglCreateContext(trace::Call &call) {
144     unsigned long long orig_context = call.ret->toUIntPtr();
145     unsigned long long orig_config = call.arg(1).toUIntPtr();
146     Context *share_context = getContext(call.arg(2).toUIntPtr());
147     trace::Array *attrib_array = dynamic_cast<trace::Array *>(&call.arg(3));
148     glws::Profile profile;
149
150     switch (current_api) {
151     case EGL_OPENGL_API:
152         profile = glws::PROFILE_COMPAT;
153         break;
154     case EGL_OPENGL_ES_API:
155     default:
156         profile = glws::PROFILE_ES1;
157         if (attrib_array) {
158             for (int i = 0; i < attrib_array->values.size(); i += 2) {
159                 int v = attrib_array->values[i]->toSInt();
160                 if (v == EGL_CONTEXT_CLIENT_VERSION) {
161                     v = attrib_array->values[i + 1]->toSInt();
162                     if (v == 2)
163                         profile = glws::PROFILE_ES2;
164                     break;
165                 }
166             }
167         }
168         break;
169     }
170
171
172     Context *context = glretrace::createContext(share_context, profile);
173     if (!context) {
174         const char *name;
175         switch (profile) {
176         case glws::PROFILE_COMPAT:
177             name = "OpenGL";
178             break;
179         case glws::PROFILE_ES1:
180             name = "OpenGL ES 1.1";
181             break;
182         case glws::PROFILE_ES2:
183             name = "OpenGL ES 2.0";
184             break;
185         default:
186             name = "unknown";
187             break;
188         }
189
190         retrace::warning(call) << "Failed to create " << name << " context.\n";
191         os::abort();
192     }
193
194     context_map[orig_context] = context;
195     profile_map[orig_config] = profile;
196     last_profile = profile;
197 }
198
199 static void retrace_eglDestroyContext(trace::Call &call) {
200     unsigned long long orig_context = call.arg(1).toUIntPtr();
201
202     ContextMap::iterator it;
203     it = context_map.find(orig_context);
204
205     if (it != context_map.end()) {
206         glretrace::Context *currentContext = glretrace::getCurrentContext();
207         if (it->second != currentContext) {
208             // TODO: reference count
209             delete it->second;
210         }
211         context_map.erase(it);
212     }
213 }
214
215 static void retrace_eglMakeCurrent(trace::Call &call) {
216     glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
217     Context *new_context = getContext(call.arg(3).toUIntPtr());
218
219     glretrace::makeCurrent(call, new_drawable, new_context);
220 }
221
222
223 static void retrace_eglSwapBuffers(trace::Call &call) {
224     glws::Drawable *drawable = getDrawable(call.arg(1).toUIntPtr());
225
226     frame_complete(call);
227
228     if (retrace::doubleBuffer) {
229         if (drawable) {
230             drawable->swapBuffers();
231         }
232     } else {
233         glFlush();
234     }
235 }
236
237 const retrace::Entry glretrace::egl_callbacks[] = {
238     {"eglGetError", &retrace::ignore},
239     {"eglGetDisplay", &retrace::ignore},
240     {"eglInitialize", &retrace::ignore},
241     {"eglTerminate", &retrace::ignore},
242     {"eglQueryString", &retrace::ignore},
243     {"eglGetConfigs", &retrace::ignore},
244     {"eglChooseConfig", &retrace::ignore},
245     {"eglGetConfigAttrib", &retrace::ignore},
246     {"eglCreateWindowSurface", &retrace_eglCreateWindowSurface},
247     {"eglCreatePbufferSurface", &retrace_eglCreatePbufferSurface},
248     //{"eglCreatePixmapSurface", &retrace::ignore},
249     {"eglDestroySurface", &retrace_eglDestroySurface},
250     {"eglQuerySurface", &retrace::ignore},
251     {"eglBindAPI", &retrace_eglBindAPI},
252     {"eglQueryAPI", &retrace::ignore},
253     //{"eglWaitClient", &retrace::ignore},
254     //{"eglReleaseThread", &retrace::ignore},
255     //{"eglCreatePbufferFromClientBuffer", &retrace::ignore},
256     //{"eglSurfaceAttrib", &retrace::ignore},
257     //{"eglBindTexImage", &retrace::ignore},
258     //{"eglReleaseTexImage", &retrace::ignore},
259     {"eglSwapInterval", &retrace::ignore},
260     {"eglCreateContext", &retrace_eglCreateContext},
261     {"eglDestroyContext", &retrace_eglDestroyContext},
262     {"eglMakeCurrent", &retrace_eglMakeCurrent},
263     {"eglGetCurrentContext", &retrace::ignore},
264     {"eglGetCurrentSurface", &retrace::ignore},
265     {"eglGetCurrentDisplay", &retrace::ignore},
266     {"eglQueryContext", &retrace::ignore},
267     {"eglWaitGL", &retrace::ignore},
268     {"eglWaitNative", &retrace::ignore},
269     {"eglSwapBuffers", &retrace_eglSwapBuffers},
270     //{"eglCopyBuffers", &retrace::ignore},
271     {"eglGetProcAddress", &retrace::ignore},
272     {"eglCreateImageKHR", &retrace::ignore},
273     {"eglDestroyImageKHR", &retrace::ignore},
274     {"glEGLImageTargetTexture2DOES", &retrace::ignore},
275     {NULL, NULL},
276 };