]> git.cworth.org Git - apitrace/blob - glretrace_egl.cpp
Encode format as a member and not part of the label.
[apitrace] / 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 static DrawableMap drawable_map;
50 static ContextMap context_map;
51
52 static unsigned int current_api = EGL_OPENGL_ES_API;
53
54 static glws::Drawable *
55 getDrawable(unsigned long long surface_ptr) {
56     if (surface_ptr == 0) {
57         return NULL;
58     }
59
60     DrawableMap::const_iterator it;
61     it = drawable_map.find(surface_ptr);
62
63     return (it != drawable_map.end()) ? it->second : NULL;
64 }
65
66 static glws::Context *
67 getContext(unsigned long long context_ptr) {
68     if (context_ptr == 0) {
69         return NULL;
70     }
71
72     ContextMap::const_iterator it;
73     it = context_map.find(context_ptr);
74
75     return (it != context_map.end()) ? it->second : NULL;
76 }
77
78 static void retrace_eglCreateWindowSurface(trace::Call &call) {
79     unsigned long long orig_surface = call.ret->toUIntPtr();
80
81     glws::Drawable *drawable = glws::createDrawable(glretrace::visual);
82     drawable_map[orig_surface] = drawable;
83 }
84
85 static void retrace_eglDestroySurface(trace::Call &call) {
86     unsigned long long orig_surface = call.arg(1).toUIntPtr();
87
88     DrawableMap::iterator it;
89     it = drawable_map.find(orig_surface);
90
91     if (it != drawable_map.end()) {
92         delete it->second;
93         drawable_map.erase(it);
94     }
95 }
96
97 static void retrace_eglBindAPI(trace::Call &call) {
98     current_api = call.arg(0).toUInt();
99 }
100
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;
106
107     switch (current_api) {
108     case EGL_OPENGL_API:
109         profile = glws::PROFILE_COMPAT;
110         break;
111     case EGL_OPENGL_ES_API:
112     default:
113         profile = glws::PROFILE_ES1;
114         if (attrib_array) {
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();
119                     if (v == 2)
120                         profile = glws::PROFILE_ES2;
121                     break;
122                 }
123             }
124         }
125         break;
126     }
127
128
129     glws::Context *context = glws::createContext(glretrace::visual, share_context, profile);
130     if (!context) {
131         const char *name;
132         switch (profile) {
133         case glws::PROFILE_COMPAT:
134             name = "OpenGL";
135             break;
136         case glws::PROFILE_ES1:
137             name = "OpenGL ES 1.1";
138             break;
139         case glws::PROFILE_ES2:
140             name = "OpenGL ES 2.0";
141             break;
142         default:
143             name = "unknown";
144             break;
145         }
146
147         retrace::warning(call) << "Failed to create " << name << " context.\n";
148         os::abort();
149     }
150
151     context_map[orig_context] = context;
152 }
153
154 static void retrace_eglDestroyContext(trace::Call &call) {
155     unsigned long long orig_context = call.arg(1).toUIntPtr();
156
157     ContextMap::iterator it;
158     it = context_map.find(orig_context);
159
160     if (it != context_map.end()) {
161         delete it->second;
162         context_map.erase(it);
163     }
164 }
165
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());
169
170     if (new_drawable == drawable && new_context == context) {
171         return;
172     }
173
174     if (drawable && context) {
175         glFlush();
176         if (!double_buffer) {
177             frame_complete(call);
178         }
179     }
180
181     bool result = glws::makeCurrent(new_drawable, new_context);
182
183     if (new_drawable && new_context && result) {
184         drawable = new_drawable;
185         context = new_context;
186     } else {
187         drawable = NULL;
188         context = NULL;
189     }
190 }
191
192
193 static void retrace_eglSwapBuffers(trace::Call &call) {
194     frame_complete(call);
195
196     if (double_buffer) {
197         drawable->swapBuffers();
198     } else {
199         glFlush();
200     }
201 }
202
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},
238     {NULL, NULL},
239 };