]> git.cworth.org Git - apitrace/blob - retrace/retrace_main.cpp
Prevent hangs in XCloseDisplay.
[apitrace] / retrace / retrace_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 #include <iostream>
29
30 #include "os_binary.hpp"
31 #include "os_time.hpp"
32 #include "image.hpp"
33 #include "trace_callset.hpp"
34 #include "trace_dump.hpp"
35 #include "retrace.hpp"
36
37
38 static bool waitOnFinish = false;
39
40 static const char *comparePrefix = NULL;
41 static const char *snapshotPrefix = NULL;
42 static trace::CallSet snapshotFrequency;
43 static trace::CallSet compareFrequency;
44
45 static unsigned dumpStateCallNo = ~0;
46
47
48 namespace retrace {
49
50
51 trace::Parser parser;
52
53
54 int verbosity = 0;
55 bool debug = true;
56 bool profiling = false;
57
58
59 bool doubleBuffer = true;
60 bool coreProfile = false;
61
62
63 static unsigned frameNo = 0;
64
65
66 void
67 frameComplete(trace::Call &call) {
68     ++frameNo;
69 }
70
71
72 static void
73 takeSnapshot(unsigned call_no) {
74     assert(snapshotPrefix || comparePrefix);
75
76     image::Image *ref = NULL;
77
78     if (comparePrefix) {
79         os::String filename = os::String::format("%s%010u.png", comparePrefix, call_no);
80         ref = image::readPNG(filename);
81         if (!ref) {
82             return;
83         }
84         if (retrace::verbosity >= 0) {
85             std::cout << "Read " << filename << "\n";
86         }
87     }
88
89     image::Image *src = getSnapshot();
90     if (!src) {
91         return;
92     }
93
94     if (snapshotPrefix) {
95         if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
96             char comment[21];
97             snprintf(comment, sizeof comment, "%u", call_no);
98             src->writePNM(std::cout, comment);
99         } else {
100             os::String filename = os::String::format("%s%010u.png", snapshotPrefix, call_no);
101             if (src->writePNG(filename) && retrace::verbosity >= 0) {
102                 std::cout << "Wrote " << filename << "\n";
103             }
104         }
105     }
106
107     if (ref) {
108         std::cout << "Snapshot " << call_no << " average precision of " << src->compare(*ref) << " bits\n";
109         delete ref;
110     }
111
112     delete src;
113
114     return;
115 }
116
117
118 static void
119 mainLoop() {
120     retrace::Retracer retracer;
121
122     addCallbacks(retracer);
123
124     long long startTime = 0; 
125     frameNo = 0;
126
127     startTime = os::getTime();
128     trace::Call *call;
129
130     while ((call = retrace::parser.parse_call())) {
131         bool swapRenderTarget = call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET;
132         bool doSnapshot =
133             snapshotFrequency.contains(*call) ||
134             compareFrequency.contains(*call)
135         ;
136
137         // For calls which cause rendertargets to be swaped, we take the
138         // snapshot _before_ swapping the rendertargets.
139         if (doSnapshot && swapRenderTarget) {
140             if (call->flags & trace::CALL_FLAG_END_FRAME) {
141                 // For swapbuffers/presents we still use this call number,
142                 // spite not have been executed yet.
143                 takeSnapshot(call->no);
144             } else {
145                 // Whereas for ordinate fbo/rendertarget changes we use the
146                 // previous call's number.
147                 takeSnapshot(call->no - 1);
148             }
149         }
150
151         retracer.retrace(*call);
152
153         if (doSnapshot && !swapRenderTarget) {
154             takeSnapshot(call->no);
155         }
156
157         if (call->no >= dumpStateCallNo &&
158             dumpState(std::cout)) {
159             exit(0);
160         }
161
162         delete call;
163     }
164
165     // Reached the end of trace
166     flushRendering();
167
168     long long endTime = os::getTime();
169     float timeInterval = (endTime - startTime) * (1.0 / os::timeFrequency);
170
171     if ((retrace::verbosity >= -1) || (retrace::profiling)) {
172         std::cout << 
173             "Rendered " << frameNo << " frames"
174             " in " <<  timeInterval << " secs,"
175             " average of " << (frameNo/timeInterval) << " fps\n";
176     }
177
178     if (waitOnFinish) {
179         waitForInput();
180     } else {
181         return;
182     }
183 }
184
185
186 } /* namespace retrace */
187
188
189 static void
190 usage(const char *argv0) {
191     std::cout << 
192         "Usage: " << argv0 << " [OPTION] TRACE [...]\n"
193         "Replay TRACE.\n"
194         "\n"
195         "  -b           benchmark mode (no error checking or warning messages)\n"
196         "  -p           profiling mode (run whole trace, dump profiling info)\n"
197         "  -c PREFIX    compare against snapshots\n"
198         "  -C CALLSET   calls to compare (default is every frame)\n"
199         "  -core        use core profile\n"
200         "  -db          use a double buffer visual (default)\n"
201         "  -sb          use a single buffer visual\n"
202         "  -s PREFIX    take snapshots; `-` for PNM stdout output\n"
203         "  -S CALLSET   calls to snapshot (default is every frame)\n"
204         "  -v           increase output verbosity\n"
205         "  -D CALLNO    dump state at specific call no\n"
206         "  -w           waitOnFinish on final frame\n";
207 }
208
209
210 extern "C"
211 int main(int argc, char **argv)
212 {
213     assert(compareFrequency.empty());
214     assert(snapshotFrequency.empty());
215
216     int i;
217     for (i = 1; i < argc; ++i) {
218         const char *arg = argv[i];
219
220         if (arg[0] != '-') {
221             break;
222         }
223
224         if (!strcmp(arg, "--")) {
225             break;
226         } else if (!strcmp(arg, "-b")) {
227             retrace::debug = false;
228             retrace::verbosity = -1;
229         } else if (!strcmp(arg, "-p")) {
230             retrace::debug = false;
231             retrace::profiling = true;
232             retrace::verbosity = -1;
233         } else if (!strcmp(arg, "-c")) {
234             comparePrefix = argv[++i];
235             if (compareFrequency.empty()) {
236                 compareFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
237             }
238         } else if (!strcmp(arg, "-C")) {
239             compareFrequency = trace::CallSet(argv[++i]);
240             if (comparePrefix == NULL) {
241                 comparePrefix = "";
242             }
243         } else if (!strcmp(arg, "-D")) {
244             dumpStateCallNo = atoi(argv[++i]);
245             retrace::verbosity = -2;
246         } else if (!strcmp(arg, "-core")) {
247             retrace::coreProfile = true;
248         } else if (!strcmp(arg, "-db")) {
249             retrace::doubleBuffer = true;
250         } else if (!strcmp(arg, "-sb")) {
251             retrace::doubleBuffer = false;
252         } else if (!strcmp(arg, "--help")) {
253             usage(argv[0]);
254             return 0;
255         } else if (!strcmp(arg, "-s")) {
256             snapshotPrefix = argv[++i];
257             if (snapshotFrequency.empty()) {
258                 snapshotFrequency = trace::CallSet(trace::FREQUENCY_FRAME);
259             }
260             if (snapshotPrefix[0] == '-' && snapshotPrefix[1] == 0) {
261                 os::setBinaryMode(stdout);
262                 retrace::verbosity = -2;
263             }
264         } else if (!strcmp(arg, "-S")) {
265             snapshotFrequency = trace::CallSet(argv[++i]);
266             if (snapshotPrefix == NULL) {
267                 snapshotPrefix = "";
268             }
269         } else if (!strcmp(arg, "-v")) {
270             ++retrace::verbosity;
271         } else if (!strcmp(arg, "-w")) {
272             waitOnFinish = true;
273         } else {
274             std::cerr << "error: unknown option " << arg << "\n";
275             usage(argv[0]);
276             return 1;
277         }
278     }
279
280     retrace::setUp();
281
282     for ( ; i < argc; ++i) {
283         if (!retrace::parser.open(argv[i])) {
284             std::cerr << "error: failed to open " << argv[i] << "\n";
285             return 1;
286         }
287
288         retrace::mainLoop();
289
290         retrace::parser.close();
291     }
292
293     // XXX: X often hangs on XCloseDisplay
294     //retrace::cleanUp();
295
296     return 0;
297 }
298