]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Workaround weird issue with gdb and dlopen'ed libpthread.so
[apitrace] / glretrace_main.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <string.h>
28
29 #include "os_string.hpp"
30 #include "image.hpp"
31 #include "retrace.hpp"
32 #include "glproc.hpp"
33 #include "glstate.hpp"
34 #include "glretrace.hpp"
35
36
37 namespace glretrace {
38
39 bool double_buffer = true;
40 bool insideGlBeginEnd = false;
41 trace::Parser parser;
42 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
43 glws::Visual *visual = NULL;
44 glws::Drawable *drawable = NULL;
45 glws::Context *context = NULL;
46
47 unsigned frame = 0;
48 long long startTime = 0;
49 bool wait = false;
50
51 bool benchmark = false;
52 const char *compare_prefix = NULL;
53 const char *snapshot_prefix = NULL;
54 enum frequency snapshot_frequency = FREQUENCY_NEVER;
55
56 unsigned dump_state = ~0;
57
58 void
59 checkGlError(trace::Call &call) {
60     GLenum error = glGetError();
61     if (error == GL_NO_ERROR) {
62         return;
63     }
64
65     std::ostream & os = retrace::warning(call);
66
67     os << "glGetError(";
68     os << call.name();
69     os << ") = ";
70
71     switch (error) {
72     case GL_INVALID_ENUM:
73         os << "GL_INVALID_ENUM";
74         break;
75     case GL_INVALID_VALUE:
76         os << "GL_INVALID_VALUE";
77         break;
78     case GL_INVALID_OPERATION:
79         os << "GL_INVALID_OPERATION";
80         break;
81     case GL_STACK_OVERFLOW:
82         os << "GL_STACK_OVERFLOW";
83         break;
84     case GL_STACK_UNDERFLOW:
85         os << "GL_STACK_UNDERFLOW";
86         break;
87     case GL_OUT_OF_MEMORY:
88         os << "GL_OUT_OF_MEMORY";
89         break;
90     case GL_INVALID_FRAMEBUFFER_OPERATION:
91         os << "GL_INVALID_FRAMEBUFFER_OPERATION";
92         break;
93     case GL_TABLE_TOO_LARGE:
94         os << "GL_TABLE_TOO_LARGE";
95         break;
96     default:
97         os << error;
98         break;
99     }
100     os << "\n";
101 }
102
103 /**
104  * Grow the current drawble.
105  *
106  * We need to infer the drawable size from GL calls because the drawable sizes
107  * are specified by OS specific calls which we do not trace.
108  */
109 void
110 updateDrawable(int width, int height) {
111     if (!drawable) {
112         return;
113     }
114
115     if (drawable->visible &&
116         width  <= drawable->width &&
117         height <= drawable->height) {
118         return;
119     }
120
121     // Check for bound framebuffer last, as this may have a performance impact.
122     GLint draw_framebuffer = 0;
123     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
124     if (draw_framebuffer != 0) {
125         return;
126     }
127
128     drawable->resize(width, height);
129     drawable->show();
130
131     glScissor(0, 0, width, height);
132 }
133
134
135 void snapshot(unsigned call_no) {
136     if (!drawable ||
137         (!snapshot_prefix && !compare_prefix)) {
138         return;
139     }
140
141     image::Image *ref = NULL;
142
143     if (compare_prefix) {
144         os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
145         ref = image::readPNG(filename);
146         if (!ref) {
147             return;
148         }
149         if (retrace::verbosity >= 0) {
150             std::cout << "Read " << filename << "\n";
151         }
152     }
153
154     image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
155     if (!src) {
156         return;
157     }
158
159     if (snapshot_prefix) {
160         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
161             char comment[21];
162             snprintf(comment, sizeof comment, "%u", call_no);
163             src->writePNM(std::cout, comment);
164         } else {
165             os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
166             if (src->writePNG(filename) && retrace::verbosity >= 0) {
167                 std::cout << "Wrote " << filename << "\n";
168             }
169         }
170     }
171
172     if (ref) {
173         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
174         delete ref;
175     }
176
177     delete src;
178 }
179
180
181 void frame_complete(trace::Call &call) {
182     ++frame;
183
184     if (!drawable) {
185         return;
186     }
187
188     if (!drawable->visible) {
189         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
190     }
191
192     if (snapshot_frequency == FREQUENCY_FRAME ||
193         snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
194         snapshot(call.no);
195     }
196 }
197
198
199 static void display(void) {
200     retrace::Retracer retracer;
201
202     retracer.addCallbacks(gl_callbacks);
203     retracer.addCallbacks(glx_callbacks);
204     retracer.addCallbacks(wgl_callbacks);
205     retracer.addCallbacks(cgl_callbacks);
206     retracer.addCallbacks(egl_callbacks);
207
208     startTime = os::getTime();
209     trace::Call *call;
210
211     while ((call = parser.parse_call())) {
212         retracer.retrace(*call);
213
214         if (!insideGlBeginEnd &&
215             drawable && context &&
216             call->no >= dump_state) {
217             glstate::dumpCurrentContext(std::cout);
218             exit(0);
219         }
220
221         delete call;
222     }
223
224     // Reached the end of trace
225     glFlush();
226
227     long long endTime = os::getTime();
228     float timeInterval = (endTime - startTime) * 1.0E-6;
229
230     if (retrace::verbosity >= -1) { 
231         std::cout << 
232             "Rendered " << frame << " frames"
233             " in " <<  timeInterval << " secs,"
234             " average of " << (frame/timeInterval) << " fps\n";
235     }
236
237     if (wait) {
238         while (glws::processEvents()) {}
239     } else {
240         exit(0);
241     }
242 }
243
244
245 static void usage(void) {
246     std::cout << 
247         "Usage: glretrace [OPTION] TRACE\n"
248         "Replay TRACE.\n"
249         "\n"
250         "  -b           benchmark mode (no error checking or warning messages)\n"
251         "  -c PREFIX    compare against snapshots\n"
252         "  -core        use core profile\n"
253         "  -db          use a double buffer visual (default)\n"
254         "  -sb          use a single buffer visual\n"
255         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
256         "  -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
257         "  -v           verbose output\n"
258         "  -D CALLNO    dump state at specific call no\n"
259         "  -w           wait on final frame\n";
260 }
261
262 extern "C"
263 int main(int argc, char **argv)
264 {
265
266     int i;
267     for (i = 1; i < argc; ++i) {
268         const char *arg = argv[i];
269
270         if (arg[0] != '-') {
271             break;
272         }
273
274         if (!strcmp(arg, "--")) {
275             break;
276         } else if (!strcmp(arg, "-b")) {
277             benchmark = true;
278             retrace::verbosity = -1;
279             glws::debug = false;
280         } else if (!strcmp(arg, "-c")) {
281             compare_prefix = argv[++i];
282             if (snapshot_frequency == FREQUENCY_NEVER) {
283                 snapshot_frequency = FREQUENCY_FRAME;
284             }
285         } else if (!strcmp(arg, "-D")) {
286             dump_state = atoi(argv[++i]);
287             retrace::verbosity = -2;
288         } else if (!strcmp(arg, "-core")) {
289             defaultProfile = glws::PROFILE_CORE;
290         } else if (!strcmp(arg, "-db")) {
291             double_buffer = true;
292         } else if (!strcmp(arg, "-sb")) {
293             double_buffer = false;
294         } else if (!strcmp(arg, "--help")) {
295             usage();
296             return 0;
297         } else if (!strcmp(arg, "-s")) {
298             snapshot_prefix = argv[++i];
299             if (snapshot_frequency == FREQUENCY_NEVER) {
300                 snapshot_frequency = FREQUENCY_FRAME;
301             }
302             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
303                 retrace::verbosity = -2;
304             }
305         } else if (!strcmp(arg, "-S")) {
306             arg = argv[++i];
307             if (!strcmp(arg, "frame")) {
308                 snapshot_frequency = FREQUENCY_FRAME;
309             } else if (!strcmp(arg, "framebuffer")) {
310                 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
311             } else if (!strcmp(arg, "draw")) {
312                 snapshot_frequency = FREQUENCY_DRAW;
313             } else {
314                 std::cerr << "error: unknown frequency " << arg << "\n";
315                 usage();
316                 return 1;
317             }
318             if (snapshot_prefix == NULL) {
319                 snapshot_prefix = "";
320             }
321         } else if (!strcmp(arg, "-v")) {
322             ++retrace::verbosity;
323         } else if (!strcmp(arg, "-w")) {
324             wait = true;
325         } else {
326             std::cerr << "error: unknown option " << arg << "\n";
327             usage();
328             return 1;
329         }
330     }
331
332     glws::init();
333     visual = glws::createVisual(double_buffer);
334
335     for ( ; i < argc; ++i) {
336         if (!parser.open(argv[i])) {
337             std::cerr << "error: failed to open " << argv[i] << "\n";
338             return 1;
339         }
340
341         display();
342
343         parser.close();
344     }
345     
346     delete visual;
347     glws::cleanup();
348
349     return 0;
350 }
351
352 } /* namespace glretrace */