]> git.cworth.org Git - apitrace/blob - cli/cli_dump_images.cpp
dump-images: Execute glretrace from source dir when running uninstalled
[apitrace] / cli / cli_dump_images.cpp
1 /*********************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2012 Intel Corporation
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy,
11  * modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  *********************************************************************/
28
29 #include <string.h>
30 #include <limits.h> // for CHAR_MAX
31 #include <getopt.h>
32 #include <iostream>
33
34 #include "cli.hpp"
35
36 #include "os_string.hpp"
37 #include "os_process.hpp"
38
39 #include "trace_resource.hpp"
40
41 static const char *synopsis = "Dump frame images obtained from a trace.";
42
43 static void
44 usage(void)
45 {
46     std::cout << "usage apitrace dump-images [OPTIONS] TRACE_FILE\n"
47               << synopsis << "\n"
48         "\n"
49         "    -h, --help           show this help message and exit\n"
50         "        --calls=CALLSET  dump images only for specified calls\n"
51         "                         (default value is \"*/frame\" which\n"
52         "                          which dumps an image for each frame)\n"
53         "    -o, --output=PREFIX  prefix to use in naming output files\n"
54         "                         (default is trace filename without extension)\n"
55         "\n";
56 }
57
58 enum {
59     CALLS_OPT = CHAR_MAX + 1,
60 };
61
62 const static char *
63 shortOptions = "ho:";
64
65 const static struct option
66 longOptions[] = {
67     {"help", no_argument, 0, 'h'},
68     {"calls", required_argument, 0, CALLS_OPT},
69     {"output", required_argument, 0, 'o'},
70     {0, 0, 0, 0}
71 };
72
73 static int
74 command(int argc, char *argv[])
75 {
76     os::String prefix;
77     const char *calls, *filename, *output = NULL;
78
79     int opt;
80     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
81         switch (opt) {
82         case 'h':
83             usage();
84             return 0;
85         case CALLS_OPT:
86             calls = optarg;
87             break;
88         case 'o':
89             output = optarg;
90             break;
91         default:
92             std::cerr << "error: unexpected option `" << opt << "`\n";
93             usage();
94             return 1;
95         }
96     }
97
98     if (optind >= argc) {
99         std::cerr << "error: apitrace dump-images requires a trace file as an argument.\n";
100         usage();
101         return 1;
102     }
103
104     if (optind < argc - 1) { 
105         std::cerr << "error: apitrace dump-images can accept only a single trace file argument.\n";
106         usage();
107         return 1;
108     }
109
110     filename = argv[optind];
111
112     if (output == NULL) {
113         prefix = filename;
114         prefix.trimDirectory();
115         prefix.trimExtension();
116         output = prefix.str();
117     }
118
119     /* FIXME: It would be cleaner to pull the replaying of the trace
120      * in-process here and generate the images directly. But that
121      * pulls in a non-trivial amount of the existing 'retrace' code,
122      * along with dependencies on GL, etc.
123      *
124      * It will definitely make sense to do that once all that code has
125      * already been pulled in for the "apitrace retrace" (or "apitrace
126      * replay") command. */
127     std::vector<const char *> command;
128
129     os::String glretracePath = trace::findProgram("glretrace");
130     command.push_back(glretracePath);
131     command.push_back("-s");
132     command.push_back(output);
133     command.push_back("-S");
134     if (calls)
135         command.push_back(calls);
136     else
137         command.push_back("*/frame");
138     command.push_back(filename);
139     command.push_back(NULL);
140
141     return os::execute((char * const *)&command[0]);
142 }
143
144 const Command dump_images_command = {
145     "dump-images",
146     synopsis,
147     usage,
148     command
149 };