1 /*
2 * Copyright © 2011-2012 Linaro Limited
3 *
4 * This file is part of glcompbench.
5 *
6 * glcompbench is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * glcompbench is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with glcompbench. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Authors:
20 * Alexandros Frantzis <alexandros.frantzis@linaro.org>
21 * Jesse Barker <jesse.barker@linaro.org>
22 */
23
24 #include <cstring>
25 #include <cstdio>
26 #include <getopt.h>
27
28 #include "options.h"
29 #include "util.h"
30
31 std::vector<std::string> Options::benchmarks;
32 std::vector<std::string> Options::benchmark_files;
33 bool Options::validate = false;
34 std::string Options::data_path = std::string(GLMARK_DATA_PATH);
35 Options::FrameEnd Options::frame_end = Options::FrameEndDefault;
36 std::pair<int,int> Options::size(800, 600);
37 bool Options::list_scenes = false;
38 bool Options::show_all_options = false;
39 bool Options::show_debug = false;
40 bool Options::show_help = false;
41 bool Options::reuse_context = false;
42 bool Options::run_forever = false;
43 bool Options::annotate = false;
44 bool Options::offscreen = false;
45 GLVisualConfig Options::visual_config;
46
47 static struct option long_options[] = {
48 {"annotate", 0, 0, 0},
49 {"benchmark", 1, 0, 0},
50 {"benchmark-file", 1, 0, 0},
51 {"validate", 0, 0, 0},
52 {"data-path", 1, 0, 0},
53 {"frame-end", 1, 0, 0},
54 {"off-screen", 0, 0, 0},
55 {"visual-config", 1, 0, 0},
56 {"reuse-context", 0, 0, 0},
57 {"run-forever", 0, 0, 0},
58 {"size", 1, 0, 0},
59 {"fullscreen", 0, 0, 0},
60 {"list-scenes", 0, 0, 0},
61 {"show-all-options", 0, 0, 0},
62 {"debug", 0, 0, 0},
63 {"help", 0, 0, 0},
64 {0, 0, 0, 0}
65 };
66
67 /**
68 * Parses a size string of the form WxH
69 *
70 * @param str the string to parse
71 * @param size the parsed size (width, height)
72 */
73 static void
parse_size(const std::string & str,std::pair<int,int> & size)74 parse_size(const std::string &str, std::pair<int,int> &size)
75 {
76 std::vector<std::string> d;
77 Util::split(str, 'x', d, Util::SplitModeNormal);
78
79 size.first = Util::fromString<int>(d[0]);
80
81 /*
82 * Parse the second element (height). If there is none, use the value
83 * of the first element for the second (width = height)
84 */
85 if (d.size() > 1)
86 size.second = Util::fromString<int>(d[1]);
87 else
88 size.second = size.first;
89 }
90
91 /**
92 * Parses a frame-end method string
93 *
94 * @param str the string to parse
95 *
96 * @return the parsed frame end method
97 */
98 static Options::FrameEnd
frame_end_from_str(const std::string & str)99 frame_end_from_str(const std::string &str)
100 {
101 Options::FrameEnd m = Options::FrameEndDefault;
102
103 if (str == "swap")
104 m = Options::FrameEndSwap;
105 else if (str == "finish")
106 m = Options::FrameEndFinish;
107 else if (str == "readpixels")
108 m = Options::FrameEndReadPixels;
109 else if (str == "none")
110 m = Options::FrameEndNone;
111
112 return m;
113 }
114
115 void
print_help()116 Options::print_help()
117 {
118 printf("A benchmark for Open GL (ES) 2.0\n"
119 "\n"
120 "Options:\n"
121 " -b, --benchmark BENCH A benchmark or options to run: '(scene)?(:opt1=val1)*'\n"
122 " (the option can be used multiple times)\n"
123 " -f, --benchmark-file F Load benchmarks to run from a file containing a\n"
124 " list of benchmark descriptions (one per line)\n"
125 " (the option can be used multiple times)\n"
126 " --validate Run a quick output validation test instead of \n"
127 " running the benchmarks\n"
128 " --data-path PATH Path to glmark2 models, shaders and textures\n"
129 " Default: " GLMARK_DATA_PATH "\n"
130 " --frame-end METHOD How to end a frame [default,none,swap,finish,readpixels]\n"
131 " --off-screen Render to an off-screen surface\n"
132 " --visual-config C The visual configuration to use for the rendering\n"
133 " target: 'red=R:green=G:blue=B:alpha=A:buffer=BUF'.\n"
134 " The parameters may be defined in any order, and any\n"
135 " omitted parameters assume a default value of '1'\n"
136 " --reuse-context Use a single context for all scenes\n"
137 " (by default, each scene gets its own context)\n"
138 " -s, --size WxH Size of the output window (default: 800x600)\n"
139 " --fullscreen Run in fullscreen mode (equivalent to --size -1x-1)\n"
140 " -l, --list-scenes Display information about the available scenes\n"
141 " and their options\n"
142 " --show-all-options Show all scene option values used for benchmarks\n"
143 " (only explicitly set options are shown by default)\n"
144 " --run-forever Run indefinitely, looping from the last benchmark\n"
145 " back to the first\n"
146 " --annotate Annotate the benchmarks with on-screen information\n"
147 " (same as -b :show-fps=true:title=#info#)\n"
148 " -d, --debug Display debug messages\n"
149 " -h, --help Display help\n");
150 }
151
152 bool
parse_args(int argc,char ** argv)153 Options::parse_args(int argc, char **argv)
154 {
155 while (1) {
156 int option_index = -1;
157 int c;
158 const char *optname = "";
159
160 c = getopt_long(argc, argv, "b:f:s:ldh",
161 long_options, &option_index);
162 if (c == -1)
163 break;
164 if (c == ':' || c == '?')
165 return false;
166
167 if (option_index != -1)
168 optname = long_options[option_index].name;
169
170 if (!strcmp(optname, "annotate"))
171 Options::annotate = true;
172 if (c == 'b' || !strcmp(optname, "benchmark"))
173 Options::benchmarks.push_back(optarg);
174 else if (c == 'f' || !strcmp(optname, "benchmark-file"))
175 Options::benchmark_files.push_back(optarg);
176 else if (!strcmp(optname, "validate"))
177 Options::validate = true;
178 else if (!strcmp(optname, "data-path"))
179 Options::data_path = std::string(optarg);
180 else if (!strcmp(optname, "frame-end"))
181 Options::frame_end = frame_end_from_str(optarg);
182 else if (!strcmp(optname, "off-screen"))
183 Options::offscreen = true;
184 else if (!strcmp(optname, "visual-config"))
185 Options::visual_config = GLVisualConfig(optarg);
186 else if (!strcmp(optname, "reuse-context"))
187 Options::reuse_context = true;
188 else if (c == 's' || !strcmp(optname, "size"))
189 parse_size(optarg, Options::size);
190 else if (!strcmp(optname, "fullscreen"))
191 Options::size = std::pair<int,int>(-1, -1);
192 else if (c == 'l' || !strcmp(optname, "list-scenes"))
193 Options::list_scenes = true;
194 else if (!strcmp(optname, "show-all-options"))
195 Options::show_all_options = true;
196 else if (!strcmp(optname, "run-forever"))
197 Options::run_forever = true;
198 else if (c == 'd' || !strcmp(optname, "debug"))
199 Options::show_debug = true;
200 else if (c == 'h' || !strcmp(optname, "help"))
201 Options::show_help = true;
202 }
203
204 return true;
205 }
206