]> git.cworth.org Git - apitrace/blob - glretrace_main.cpp
Make os::getTime() inline and make time frequency OS-dependent variable.
[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 "os_time.hpp"
31 #include "image.hpp"
32 #include "retrace.hpp"
33 #include "glproc.hpp"
34 #include "glstate.hpp"
35 #include "glretrace.hpp"
36
37
38 namespace glretrace {
39
40 bool double_buffer = true;
41 bool insideGlBeginEnd = false;
42 trace::Parser parser;
43 glws::Profile defaultProfile = glws::PROFILE_COMPAT;
44 glws::Visual *visual[glws::PROFILE_MAX];
45 glws::Drawable *drawable = NULL;
46 glws::Context *context = NULL;
47
48 unsigned frame = 0;
49 long long startTime = 0;
50 bool wait = false;
51
52 bool benchmark = false;
53 const char *compare_prefix = NULL;
54 const char *snapshot_prefix = NULL;
55 enum frequency snapshot_frequency = FREQUENCY_NEVER;
56
57 unsigned dump_state = ~0;
58
59 void
60 checkGlError(trace::Call &call) {
61     GLenum error = glGetError();
62     if (error == GL_NO_ERROR) {
63         return;
64     }
65
66     std::ostream & os = retrace::warning(call);
67
68     os << "glGetError(";
69     os << call.name();
70     os << ") = ";
71
72     switch (error) {
73     case GL_INVALID_ENUM:
74         os << "GL_INVALID_ENUM";
75         break;
76     case GL_INVALID_VALUE:
77         os << "GL_INVALID_VALUE";
78         break;
79     case GL_INVALID_OPERATION:
80         os << "GL_INVALID_OPERATION";
81         break;
82     case GL_STACK_OVERFLOW:
83         os << "GL_STACK_OVERFLOW";
84         break;
85     case GL_STACK_UNDERFLOW:
86         os << "GL_STACK_UNDERFLOW";
87         break;
88     case GL_OUT_OF_MEMORY:
89         os << "GL_OUT_OF_MEMORY";
90         break;
91     case GL_INVALID_FRAMEBUFFER_OPERATION:
92         os << "GL_INVALID_FRAMEBUFFER_OPERATION";
93         break;
94     case GL_TABLE_TOO_LARGE:
95         os << "GL_TABLE_TOO_LARGE";
96         break;
97     default:
98         os << error;
99         break;
100     }
101     os << "\n";
102 }
103
104 /**
105  * Grow the current drawble.
106  *
107  * We need to infer the drawable size from GL calls because the drawable sizes
108  * are specified by OS specific calls which we do not trace.
109  */
110 void
111 updateDrawable(int width, int height) {
112     if (!drawable) {
113         return;
114     }
115
116     if (drawable->visible &&
117         width  <= drawable->width &&
118         height <= drawable->height) {
119         return;
120     }
121
122     // Ignore zero area viewports
123     if (width == 0 || height == 0) {
124         return;
125     }
126
127     // Check for bound framebuffer last, as this may have a performance impact.
128     GLint draw_framebuffer = 0;
129     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
130     if (draw_framebuffer != 0) {
131         return;
132     }
133
134     drawable->resize(width, height);
135     drawable->show();
136
137     glScissor(0, 0, width, height);
138 }
139
140
141 void snapshot(unsigned call_no) {
142     if (!drawable ||
143         (!snapshot_prefix && !compare_prefix)) {
144         return;
145     }
146
147     image::Image *ref = NULL;
148
149     if (compare_prefix) {
150         os::String filename = os::String::format("%s%010u.png", compare_prefix, call_no);
151         ref = image::readPNG(filename);
152         if (!ref) {
153             return;
154         }
155         if (retrace::verbosity >= 0) {
156             std::cout << "Read " << filename << "\n";
157         }
158     }
159
160     image::Image *src = glstate::getDrawBufferImage(GL_RGBA);
161     if (!src) {
162         return;
163     }
164
165     if (snapshot_prefix) {
166         if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
167             char comment[21];
168             snprintf(comment, sizeof comment, "%u", call_no);
169             src->writePNM(std::cout, comment);
170         } else {
171             os::String filename = os::String::format("%s%010u.png", snapshot_prefix, call_no);
172             if (src->writePNG(filename) && retrace::verbosity >= 0) {
173                 std::cout << "Wrote " << filename << "\n";
174             }
175         }
176     }
177
178     if (ref) {
179         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
180         delete ref;
181     }
182
183     delete src;
184 }
185
186
187 void frame_complete(trace::Call &call) {
188     ++frame;
189
190     if (!drawable) {
191         return;
192     }
193
194     if (!drawable->visible) {
195         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
196     }
197
198     if (snapshot_frequency == FREQUENCY_FRAME ||
199         snapshot_frequency == FREQUENCY_FRAMEBUFFER) {
200         snapshot(call.no);
201     }
202 }
203
204
205 static void display(void) {
206     retrace::Retracer retracer;
207
208     retracer.addCallbacks(gl_callbacks);
209     retracer.addCallbacks(glx_callbacks);
210     retracer.addCallbacks(wgl_callbacks);
211     retracer.addCallbacks(cgl_callbacks);
212     retracer.addCallbacks(egl_callbacks);
213
214     startTime = os::getTime();
215     trace::Call *call;
216
217     while ((call = parser.parse_call())) {
218         retracer.retrace(*call);
219
220         if (!insideGlBeginEnd &&
221             drawable && context &&
222             call->no >= dump_state) {
223             glstate::dumpCurrentContext(std::cout);
224             exit(0);
225         }
226
227         delete call;
228     }
229
230     // Reached the end of trace
231     glFlush();
232
233     long long endTime = os::getTime();
234     float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
235
236     if (retrace::verbosity >= -1) { 
237         std::cout << 
238             "Rendered " << frame << " frames"
239             " in " <<  timeInterval << " secs,"
240             " average of " << (frame/timeInterval) << " fps\n";
241     }
242
243     if (wait) {
244         while (glws::processEvents()) {}
245     } else {
246         exit(0);
247     }
248 }
249
250
251 static void usage(void) {
252     std::cout << 
253         "Usage: glretrace [OPTION] TRACE\n"
254         "Replay TRACE.\n"
255         "\n"
256         "  -b           benchmark mode (no error checking or warning messages)\n"
257         "  -c PREFIX    compare against snapshots\n"
258         "  -core        use core profile\n"
259         "  -db          use a double buffer visual (default)\n"
260         "  -sb          use a single buffer visual\n"
261         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
262         "  -S FREQUENCY snapshot frequency: frame (default), framebuffer, or draw\n"
263         "  -v           increase output verbosity\n"
264         "  -D CALLNO    dump state at specific call no\n"
265         "  -w           wait on final frame\n";
266 }
267
268 extern "C"
269 int main(int argc, char **argv)
270 {
271
272     int i;
273     for (i = 1; i < argc; ++i) {
274         const char *arg = argv[i];
275
276         if (arg[0] != '-') {
277             break;
278         }
279
280         if (!strcmp(arg, "--")) {
281             break;
282         } else if (!strcmp(arg, "-b")) {
283             benchmark = true;
284             retrace::verbosity = -1;
285             glws::debug = false;
286         } else if (!strcmp(arg, "-c")) {
287             compare_prefix = argv[++i];
288             if (snapshot_frequency == FREQUENCY_NEVER) {
289                 snapshot_frequency = FREQUENCY_FRAME;
290             }
291         } else if (!strcmp(arg, "-D")) {
292             dump_state = atoi(argv[++i]);
293             retrace::verbosity = -2;
294         } else if (!strcmp(arg, "-core")) {
295             defaultProfile = glws::PROFILE_CORE;
296         } else if (!strcmp(arg, "-db")) {
297             double_buffer = true;
298         } else if (!strcmp(arg, "-sb")) {
299             double_buffer = false;
300         } else if (!strcmp(arg, "--help")) {
301             usage();
302             return 0;
303         } else if (!strcmp(arg, "-s")) {
304             snapshot_prefix = argv[++i];
305             if (snapshot_frequency == FREQUENCY_NEVER) {
306                 snapshot_frequency = FREQUENCY_FRAME;
307             }
308             if (snapshot_prefix[0] == '-' && snapshot_prefix[1] == 0) {
309                 retrace::verbosity = -2;
310             }
311         } else if (!strcmp(arg, "-S")) {
312             arg = argv[++i];
313             if (!strcmp(arg, "frame")) {
314                 snapshot_frequency = FREQUENCY_FRAME;
315             } else if (!strcmp(arg, "framebuffer")) {
316                 snapshot_frequency = FREQUENCY_FRAMEBUFFER;
317             } else if (!strcmp(arg, "draw")) {
318                 snapshot_frequency = FREQUENCY_DRAW;
319             } else {
320                 std::cerr << "error: unknown frequency " << arg << "\n";
321                 usage();
322                 return 1;
323             }
324             if (snapshot_prefix == NULL) {
325                 snapshot_prefix = "";
326             }
327         } else if (!strcmp(arg, "-v")) {
328             ++retrace::verbosity;
329         } else if (!strcmp(arg, "-w")) {
330             wait = true;
331         } else {
332             std::cerr << "error: unknown option " << arg << "\n";
333             usage();
334             return 1;
335         }
336     }
337
338     glws::init();
339     visual[glws::PROFILE_COMPAT] = glws::createVisual(double_buffer, glws::PROFILE_COMPAT);
340     visual[glws::PROFILE_CORE] = glws::createVisual(double_buffer, glws::PROFILE_CORE);
341     visual[glws::PROFILE_ES1] = glws::createVisual(double_buffer, glws::PROFILE_ES1);
342     visual[glws::PROFILE_ES2] = glws::createVisual(double_buffer, glws::PROFILE_ES2);
343
344     for ( ; i < argc; ++i) {
345         if (!parser.open(argv[i])) {
346             std::cerr << "error: failed to open " << argv[i] << "\n";
347             return 1;
348         }
349
350         display();
351
352         parser.close();
353     }
354
355     for (int n = 0; n < glws::PROFILE_MAX; n++) {
356         delete visual[n];
357     }
358
359     glws::cleanup();
360
361     return 0;
362 }
363
364 } /* namespace glretrace */