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