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"
35 #include "eglsize.hpp"
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
45 using namespace glretrace;
48 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
49 typedef std::map<unsigned long long, glws::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;
55 static unsigned int current_api = EGL_OPENGL_ES_API;
56 static glws::Profile last_profile = glws::PROFILE_COMPAT;
59 createDrawable(unsigned long long orig_config, unsigned long long orig_surface);
61 static glws::Drawable *
62 getDrawable(unsigned long long surface_ptr) {
63 if (surface_ptr == 0) {
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());
77 return (it != drawable_map.end()) ? it->second : NULL;
80 static glws::Context *
81 getContext(unsigned long long context_ptr) {
82 if (context_ptr == 0) {
86 ContextMap::const_iterator it;
87 it = context_map.find(context_ptr);
89 return (it != context_map.end()) ? it->second : NULL;
92 static void createDrawable(unsigned long long orig_config, unsigned long long orig_surface)
94 ProfileMap::iterator it = profile_map.find(orig_config);
95 glws::Profile profile;
97 // If the requested config is associated with a profile, use that
98 // profile. Otherwise, assume that the last used profile is what
100 if (it != profile_map.end()) {
101 profile = it->second;
103 profile = last_profile;
106 glws::Drawable *drawable = glretrace::createDrawable(profile);
107 drawable_map[orig_surface] = drawable;
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);
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
123 static void retrace_eglDestroySurface(trace::Call &call) {
124 unsigned long long orig_surface = call.arg(1).toUIntPtr();
126 DrawableMap::iterator it;
127 it = drawable_map.find(orig_surface);
129 if (it != drawable_map.end()) {
130 if (it->second != currentDrawable) {
131 // TODO: reference count
134 drawable_map.erase(it);
138 static void retrace_eglBindAPI(trace::Call &call) {
139 current_api = call.arg(0).toUInt();
142 static void retrace_eglCreateContext(trace::Call &call) {
143 unsigned long long orig_context = call.ret->toUIntPtr();
144 unsigned long long orig_config = call.arg(1).toUIntPtr();
145 glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
146 trace::Array *attrib_array = dynamic_cast<trace::Array *>(&call.arg(3));
147 glws::Profile profile;
149 switch (current_api) {
151 profile = glws::PROFILE_COMPAT;
153 case EGL_OPENGL_ES_API:
155 profile = glws::PROFILE_ES1;
157 for (int i = 0; i < attrib_array->values.size(); i += 2) {
158 int v = attrib_array->values[i]->toSInt();
159 if (v == EGL_CONTEXT_CLIENT_VERSION) {
160 v = attrib_array->values[i + 1]->toSInt();
162 profile = glws::PROFILE_ES2;
171 glws::Context *context = glretrace::createContext(share_context, profile);
175 case glws::PROFILE_COMPAT:
178 case glws::PROFILE_ES1:
179 name = "OpenGL ES 1.1";
181 case glws::PROFILE_ES2:
182 name = "OpenGL ES 2.0";
189 retrace::warning(call) << "Failed to create " << name << " context.\n";
193 context_map[orig_context] = context;
194 profile_map[orig_config] = profile;
195 last_profile = profile;
198 static void retrace_eglDestroyContext(trace::Call &call) {
199 unsigned long long orig_context = call.arg(1).toUIntPtr();
201 ContextMap::iterator it;
202 it = context_map.find(orig_context);
204 if (it != context_map.end()) {
206 context_map.erase(it);
210 static void retrace_eglMakeCurrent(trace::Call &call) {
211 glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
212 glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
214 glretrace::makeCurrent(call, new_drawable, new_context);
218 static void retrace_eglSwapBuffers(trace::Call &call) {
219 frame_complete(call);
221 if (retrace::doubleBuffer && currentDrawable) {
222 currentDrawable->swapBuffers();
228 static void retrace_glEGLImageTargetTexture2DOES(trace::Call &call)
230 EGLenum target = call.arg(0).toUInt();
231 struct image_blob *iblob;
232 struct image_info *info;
234 if (target != GL_TEXTURE_2D) {
235 os::log("%s: target %d not supported\n", __func__, target);
238 iblob = static_cast<struct image_blob *>((call.arg(1)).toPointer());
241 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, info->width, info->height, 0,
242 GL_RGBA, GL_UNSIGNED_BYTE, iblob->data);
245 const retrace::Entry glretrace::egl_callbacks[] = {
246 {"eglGetError", &retrace::ignore},
247 {"eglGetDisplay", &retrace::ignore},
248 {"eglInitialize", &retrace::ignore},
249 {"eglTerminate", &retrace::ignore},
250 {"eglQueryString", &retrace::ignore},
251 {"eglGetConfigs", &retrace::ignore},
252 {"eglChooseConfig", &retrace::ignore},
253 {"eglGetConfigAttrib", &retrace::ignore},
254 {"eglCreateWindowSurface", &retrace_eglCreateWindowSurface},
255 {"eglCreatePbufferSurface", &retrace_eglCreatePbufferSurface},
256 //{"eglCreatePixmapSurface", &retrace::ignore},
257 {"eglDestroySurface", &retrace_eglDestroySurface},
258 {"eglQuerySurface", &retrace::ignore},
259 {"eglBindAPI", &retrace_eglBindAPI},
260 {"eglQueryAPI", &retrace::ignore},
261 //{"eglWaitClient", &retrace::ignore},
262 //{"eglReleaseThread", &retrace::ignore},
263 //{"eglCreatePbufferFromClientBuffer", &retrace::ignore},
264 //{"eglSurfaceAttrib", &retrace::ignore},
265 //{"eglBindTexImage", &retrace::ignore},
266 //{"eglReleaseTexImage", &retrace::ignore},
267 {"eglSwapInterval", &retrace::ignore},
268 {"eglCreateContext", &retrace_eglCreateContext},
269 {"eglDestroyContext", &retrace_eglDestroyContext},
270 {"eglMakeCurrent", &retrace_eglMakeCurrent},
271 {"eglGetCurrentContext", &retrace::ignore},
272 {"eglGetCurrentSurface", &retrace::ignore},
273 {"eglGetCurrentDisplay", &retrace::ignore},
274 {"eglQueryContext", &retrace::ignore},
275 {"eglWaitGL", &retrace::ignore},
276 {"eglWaitNative", &retrace::ignore},
277 {"eglSwapBuffers", &retrace_eglSwapBuffers},
278 //{"eglCopyBuffers", &retrace::ignore},
279 {"eglGetProcAddress", &retrace::ignore},
280 {"eglCreateImageKHR", &retrace::ignore},
281 {"glEGLImageTargetTexture2DOES", &retrace_glEGLImageTargetTexture2DOES},