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;
57 static glws::Drawable *
58 getDrawable(unsigned long long surface_ptr) {
59 if (surface_ptr == 0) {
63 DrawableMap::const_iterator it;
64 it = drawable_map.find(surface_ptr);
66 return (it != drawable_map.end()) ? it->second : NULL;
69 static glws::Context *
70 getContext(unsigned long long context_ptr) {
71 if (context_ptr == 0) {
75 ContextMap::const_iterator it;
76 it = context_map.find(context_ptr);
78 return (it != context_map.end()) ? it->second : NULL;
81 static void retrace_eglCreateWindowSurface(trace::Call &call) {
82 unsigned long long orig_surface = call.ret->toUIntPtr();
83 unsigned long long orig_config = call.arg(1).toUIntPtr();
84 ProfileMap::iterator it = profile_map.find(orig_config);
85 glws::Profile profile;
87 // If the requested config is associated with a profile, use that
88 // profile. Otherwise, assume that the last used profile is what
90 if (it != profile_map.end()) {
93 profile = last_profile;
96 glws::Drawable *drawable = glws::createDrawable(glretrace::visual[profile]);
97 drawable_map[orig_surface] = drawable;
100 static void retrace_eglDestroySurface(trace::Call &call) {
101 unsigned long long orig_surface = call.arg(1).toUIntPtr();
103 DrawableMap::iterator it;
104 it = drawable_map.find(orig_surface);
106 if (it != drawable_map.end()) {
108 drawable_map.erase(it);
112 static void retrace_eglBindAPI(trace::Call &call) {
113 current_api = call.arg(0).toUInt();
116 static void retrace_eglCreateContext(trace::Call &call) {
117 unsigned long long orig_context = call.ret->toUIntPtr();
118 unsigned long long orig_config = call.arg(1).toUIntPtr();
119 glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
120 trace::Array *attrib_array = dynamic_cast<trace::Array *>(&call.arg(3));
121 glws::Profile profile;
123 switch (current_api) {
125 profile = glws::PROFILE_COMPAT;
127 case EGL_OPENGL_ES_API:
129 profile = glws::PROFILE_ES1;
131 for (int i = 0; i < attrib_array->values.size(); i += 2) {
132 int v = attrib_array->values[i]->toSInt();
133 if (v == EGL_CONTEXT_CLIENT_VERSION) {
134 v = attrib_array->values[i + 1]->toSInt();
136 profile = glws::PROFILE_ES2;
145 glws::Context *context = glws::createContext(glretrace::visual[profile], share_context, profile);
149 case glws::PROFILE_COMPAT:
152 case glws::PROFILE_ES1:
153 name = "OpenGL ES 1.1";
155 case glws::PROFILE_ES2:
156 name = "OpenGL ES 2.0";
163 retrace::warning(call) << "Failed to create " << name << " context.\n";
167 context_map[orig_context] = context;
168 profile_map[orig_config] = profile;
169 last_profile = profile;
172 static void retrace_eglDestroyContext(trace::Call &call) {
173 unsigned long long orig_context = call.arg(1).toUIntPtr();
175 ContextMap::iterator it;
176 it = context_map.find(orig_context);
178 if (it != context_map.end()) {
180 context_map.erase(it);
184 static void retrace_eglMakeCurrent(trace::Call &call) {
185 glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
186 glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
188 if (new_drawable == drawable && new_context == context) {
192 if (drawable && context) {
194 if (!double_buffer) {
195 frame_complete(call);
199 bool result = glws::makeCurrent(new_drawable, new_context);
201 if (new_drawable && new_context && result) {
202 drawable = new_drawable;
203 context = new_context;
211 static void retrace_eglSwapBuffers(trace::Call &call) {
212 frame_complete(call);
215 drawable->swapBuffers();
221 const retrace::Entry glretrace::egl_callbacks[] = {
222 {"eglGetError", &retrace::ignore},
223 {"eglGetDisplay", &retrace::ignore},
224 {"eglInitialize", &retrace::ignore},
225 {"eglTerminate", &retrace::ignore},
226 {"eglQueryString", &retrace::ignore},
227 {"eglGetConfigs", &retrace::ignore},
228 {"eglChooseConfig", &retrace::ignore},
229 {"eglGetConfigAttrib", &retrace::ignore},
230 {"eglCreateWindowSurface", &retrace_eglCreateWindowSurface},
231 //{"eglCreatePbufferSurface", &retrace::ignore},
232 //{"eglCreatePixmapSurface", &retrace::ignore},
233 {"eglDestroySurface", &retrace_eglDestroySurface},
234 {"eglQuerySurface", &retrace::ignore},
235 {"eglBindAPI", &retrace_eglBindAPI},
236 {"eglQueryAPI", &retrace::ignore},
237 //{"eglWaitClient", &retrace::ignore},
238 //{"eglReleaseThread", &retrace::ignore},
239 //{"eglCreatePbufferFromClientBuffer", &retrace::ignore},
240 //{"eglSurfaceAttrib", &retrace::ignore},
241 //{"eglBindTexImage", &retrace::ignore},
242 //{"eglReleaseTexImage", &retrace::ignore},
243 {"eglSwapInterval", &retrace::ignore},
244 {"eglCreateContext", &retrace_eglCreateContext},
245 {"eglDestroyContext", &retrace_eglDestroyContext},
246 {"eglMakeCurrent", &retrace_eglMakeCurrent},
247 {"eglGetCurrentContext", &retrace::ignore},
248 {"eglGetCurrentSurface", &retrace::ignore},
249 {"eglGetCurrentDisplay", &retrace::ignore},
250 {"eglQueryContext", &retrace::ignore},
251 {"eglWaitGL", &retrace::ignore},
252 {"eglWaitNative", &retrace::ignore},
253 {"eglSwapBuffers", &retrace_eglSwapBuffers},
254 //{"eglCopyBuffers", &retrace::ignore},
255 {"eglGetProcAddress", &retrace::ignore},