1 /**************************************************************************
3 * Copyright 2011 LunarG, Inc.
6 * Based on glretrace_glx.cpp, which has
8 * Copyright 2011 Jose Fonseca
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
28 **************************************************************************/
32 #include "retrace.hpp"
33 #include "glretrace.hpp"
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
44 using namespace glretrace;
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;
54 static unsigned int current_api = EGL_OPENGL_ES_API;
55 static glws::Profile last_profile = glws::PROFILE_COMPAT;
58 createDrawable(unsigned long long orig_config, unsigned long long orig_surface);
60 static glws::Drawable *
61 getDrawable(unsigned long long surface_ptr) {
62 if (surface_ptr == 0) {
66 DrawableMap::const_iterator it;
67 it = drawable_map.find(surface_ptr);
68 if (it == drawable_map.end()) {
69 // In Fennec we get the egl window surface from Java which isn't
70 // traced, so just create a drawable if it doesn't exist in here
71 createDrawable(0, surface_ptr);
72 it = drawable_map.find(surface_ptr);
73 assert(it != drawable_map.end());
76 return (it != drawable_map.end()) ? it->second : NULL;
79 static glws::Context *
80 getContext(unsigned long long context_ptr) {
81 if (context_ptr == 0) {
85 ContextMap::const_iterator it;
86 it = context_map.find(context_ptr);
88 return (it != context_map.end()) ? it->second : NULL;
91 static void createDrawable(unsigned long long orig_config, unsigned long long orig_surface)
93 ProfileMap::iterator it = profile_map.find(orig_config);
94 glws::Profile profile;
96 // If the requested config is associated with a profile, use that
97 // profile. Otherwise, assume that the last used profile is what
99 if (it != profile_map.end()) {
100 profile = it->second;
102 profile = last_profile;
105 glws::Visual *visual = glretrace::visual[profile];
107 glws::Drawable *drawable = glws::createDrawable(visual);
108 drawable_map[orig_surface] = drawable;
111 static void retrace_eglCreateWindowSurface(trace::Call &call) {
112 unsigned long long orig_config = call.arg(1).toUIntPtr();
113 unsigned long long orig_surface = call.ret->toUIntPtr();
114 createDrawable(orig_config, orig_surface);
117 static void retrace_eglCreatePbufferSurface(trace::Call &call) {
118 unsigned long long orig_config = call.arg(1).toUIntPtr();
119 unsigned long long orig_surface = call.ret->toUIntPtr();
120 createDrawable(orig_config, orig_surface);
121 // TODO: Respect the pbuffer dimensions too
124 static void retrace_eglDestroySurface(trace::Call &call) {
125 unsigned long long orig_surface = call.arg(1).toUIntPtr();
127 DrawableMap::iterator it;
128 it = drawable_map.find(orig_surface);
130 if (it != drawable_map.end()) {
131 if (it->second != drawable) {
132 // TODO: reference count
135 drawable_map.erase(it);
139 static void retrace_eglBindAPI(trace::Call &call) {
140 current_api = call.arg(0).toUInt();
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 glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
147 trace::Array *attrib_array = dynamic_cast<trace::Array *>(&call.arg(3));
148 glws::Profile profile;
150 switch (current_api) {
152 profile = glws::PROFILE_COMPAT;
154 case EGL_OPENGL_ES_API:
156 profile = glws::PROFILE_ES1;
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();
163 profile = glws::PROFILE_ES2;
172 glws::Context *context = glws::createContext(glretrace::visual[profile], share_context, profile);
176 case glws::PROFILE_COMPAT:
179 case glws::PROFILE_ES1:
180 name = "OpenGL ES 1.1";
182 case glws::PROFILE_ES2:
183 name = "OpenGL ES 2.0";
190 retrace::warning(call) << "Failed to create " << name << " context.\n";
194 context_map[orig_context] = context;
195 profile_map[orig_config] = profile;
196 last_profile = profile;
199 static void retrace_eglDestroyContext(trace::Call &call) {
200 unsigned long long orig_context = call.arg(1).toUIntPtr();
202 ContextMap::iterator it;
203 it = context_map.find(orig_context);
205 if (it != context_map.end()) {
207 context_map.erase(it);
211 static void retrace_eglMakeCurrent(trace::Call &call) {
212 glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
213 glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
215 if (new_drawable == drawable && new_context == context) {
219 if (drawable && context) {
221 if (!double_buffer) {
222 frame_complete(call);
226 bool result = glws::makeCurrent(new_drawable, new_context);
228 if (new_drawable && new_context && result) {
229 drawable = new_drawable;
230 context = new_context;
238 static void retrace_eglSwapBuffers(trace::Call &call) {
239 frame_complete(call);
241 if (double_buffer && drawable) {
242 drawable->swapBuffers();
248 const retrace::Entry glretrace::egl_callbacks[] = {
249 {"eglGetError", &retrace::ignore},
250 {"eglGetDisplay", &retrace::ignore},
251 {"eglInitialize", &retrace::ignore},
252 {"eglTerminate", &retrace::ignore},
253 {"eglQueryString", &retrace::ignore},
254 {"eglGetConfigs", &retrace::ignore},
255 {"eglChooseConfig", &retrace::ignore},
256 {"eglGetConfigAttrib", &retrace::ignore},
257 {"eglCreateWindowSurface", &retrace_eglCreateWindowSurface},
258 {"eglCreatePbufferSurface", &retrace_eglCreatePbufferSurface},
259 //{"eglCreatePixmapSurface", &retrace::ignore},
260 {"eglDestroySurface", &retrace_eglDestroySurface},
261 {"eglQuerySurface", &retrace::ignore},
262 {"eglBindAPI", &retrace_eglBindAPI},
263 {"eglQueryAPI", &retrace::ignore},
264 //{"eglWaitClient", &retrace::ignore},
265 //{"eglReleaseThread", &retrace::ignore},
266 //{"eglCreatePbufferFromClientBuffer", &retrace::ignore},
267 //{"eglSurfaceAttrib", &retrace::ignore},
268 //{"eglBindTexImage", &retrace::ignore},
269 //{"eglReleaseTexImage", &retrace::ignore},
270 {"eglSwapInterval", &retrace::ignore},
271 {"eglCreateContext", &retrace_eglCreateContext},
272 {"eglDestroyContext", &retrace_eglDestroyContext},
273 {"eglMakeCurrent", &retrace_eglMakeCurrent},
274 {"eglGetCurrentContext", &retrace::ignore},
275 {"eglGetCurrentSurface", &retrace::ignore},
276 {"eglGetCurrentDisplay", &retrace::ignore},
277 {"eglQueryContext", &retrace::ignore},
278 {"eglWaitGL", &retrace::ignore},
279 {"eglWaitNative", &retrace::ignore},
280 {"eglSwapBuffers", &retrace_eglSwapBuffers},
281 //{"eglCopyBuffers", &retrace::ignore},
282 {"eglGetProcAddress", &retrace::ignore},