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 static DrawableMap drawable_map;
50 static ContextMap context_map;
52 static unsigned int current_api = EGL_OPENGL_ES_API;
54 static glws::Drawable *
55 getDrawable(unsigned long long surface_ptr) {
56 if (surface_ptr == 0) {
60 DrawableMap::const_iterator it;
61 it = drawable_map.find(surface_ptr);
63 return (it != drawable_map.end()) ? it->second : NULL;
66 static glws::Context *
67 getContext(unsigned long long context_ptr) {
68 if (context_ptr == 0) {
72 ContextMap::const_iterator it;
73 it = context_map.find(context_ptr);
75 return (it != context_map.end()) ? it->second : NULL;
78 static void retrace_eglCreateWindowSurface(trace::Call &call) {
79 unsigned long long orig_surface = call.ret->toUIntPtr();
81 glws::Drawable *drawable = glws::createDrawable(glretrace::visual);
82 drawable_map[orig_surface] = drawable;
85 static void retrace_eglDestroySurface(trace::Call &call) {
86 unsigned long long orig_surface = call.arg(1).toUIntPtr();
88 DrawableMap::iterator it;
89 it = drawable_map.find(orig_surface);
91 if (it != drawable_map.end()) {
93 drawable_map.erase(it);
97 static void retrace_eglBindAPI(trace::Call &call) {
98 current_api = call.arg(0).toUInt();
101 static void retrace_eglCreateContext(trace::Call &call) {
102 unsigned long long orig_context = call.ret->toUIntPtr();
103 glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
104 trace::Array *attrib_array = dynamic_cast<trace::Array *>(&call.arg(3));
105 glws::Profile profile;
107 switch (current_api) {
109 profile = glws::PROFILE_COMPAT;
111 case EGL_OPENGL_ES_API:
113 profile = glws::PROFILE_ES1;
115 for (int i = 0; i < attrib_array->values.size(); i += 2) {
116 int v = attrib_array->values[i]->toSInt();
117 if (v == EGL_CONTEXT_CLIENT_VERSION) {
118 v = attrib_array->values[i + 1]->toSInt();
120 profile = glws::PROFILE_ES2;
129 glws::Context *context = glws::createContext(glretrace::visual, share_context, profile);
133 case glws::PROFILE_COMPAT:
136 case glws::PROFILE_ES1:
137 name = "OpenGL ES 1.1";
139 case glws::PROFILE_ES2:
140 name = "OpenGL ES 2.0";
147 retrace::warning(call) << "Failed to create " << name << " context.\n";
151 context_map[orig_context] = context;
154 static void retrace_eglDestroyContext(trace::Call &call) {
155 unsigned long long orig_context = call.arg(1).toUIntPtr();
157 ContextMap::iterator it;
158 it = context_map.find(orig_context);
160 if (it != context_map.end()) {
162 context_map.erase(it);
166 static void retrace_eglMakeCurrent(trace::Call &call) {
167 glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
168 glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
170 if (new_drawable == drawable && new_context == context) {
174 if (drawable && context) {
176 if (!double_buffer) {
177 frame_complete(call);
181 bool result = glws::makeCurrent(new_drawable, new_context);
183 if (new_drawable && new_context && result) {
184 drawable = new_drawable;
185 context = new_context;
193 static void retrace_eglSwapBuffers(trace::Call &call) {
194 frame_complete(call);
197 drawable->swapBuffers();
203 const retrace::Entry glretrace::egl_callbacks[] = {
204 {"eglGetError", &retrace::ignore},
205 {"eglGetDisplay", &retrace::ignore},
206 {"eglInitialize", &retrace::ignore},
207 {"eglTerminate", &retrace::ignore},
208 {"eglQueryString", &retrace::ignore},
209 {"eglGetConfigs", &retrace::ignore},
210 {"eglChooseConfig", &retrace::ignore},
211 {"eglGetConfigAttrib", &retrace::ignore},
212 {"eglCreateWindowSurface", &retrace_eglCreateWindowSurface},
213 //{"eglCreatePbufferSurface", &retrace::ignore},
214 //{"eglCreatePixmapSurface", &retrace::ignore},
215 {"eglDestroySurface", &retrace_eglDestroySurface},
216 {"eglQuerySurface", &retrace::ignore},
217 {"eglBindAPI", &retrace_eglBindAPI},
218 {"eglQueryAPI", &retrace::ignore},
219 //{"eglWaitClient", &retrace::ignore},
220 //{"eglReleaseThread", &retrace::ignore},
221 //{"eglCreatePbufferFromClientBuffer", &retrace::ignore},
222 //{"eglSurfaceAttrib", &retrace::ignore},
223 //{"eglBindTexImage", &retrace::ignore},
224 //{"eglReleaseTexImage", &retrace::ignore},
225 {"eglSwapInterval", &retrace::ignore},
226 {"eglCreateContext", &retrace_eglCreateContext},
227 {"eglDestroyContext", &retrace_eglDestroyContext},
228 {"eglMakeCurrent", &retrace_eglMakeCurrent},
229 {"eglGetCurrentContext", &retrace::ignore},
230 {"eglGetCurrentSurface", &retrace::ignore},
231 {"eglGetCurrentDisplay", &retrace::ignore},
232 {"eglQueryContext", &retrace::ignore},
233 {"eglWaitGL", &retrace::ignore},
234 {"eglWaitNative", &retrace::ignore},
235 {"eglSwapBuffers", &retrace_eglSwapBuffers},
236 //{"eglCopyBuffers", &retrace::ignore},
237 {"eglGetProcAddress", &retrace::ignore},