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