blob: 23030397f347f490ae4e526fd804e987e87866dd [file] [log] [blame]
Alexandros Frantzis72483092011-06-08 14:24:18 +03001/*
Jesse Barkerbccca292012-01-26 09:36:24 -08002 * Copyright © 2011-2012 Linaro Limited
Alexandros Frantzis72483092011-06-08 14:24:18 +03003 *
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 <cstdlib>
26#include <cstdio>
27#include <getopt.h>
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030028#include <sstream>
Alexandros Frantzis72483092011-06-08 14:24:18 +030029
30#include "options.h"
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030031#include "util.h"
Alexandros Frantzis72483092011-06-08 14:24:18 +030032
Alexandros Frantzisd97db372011-06-09 17:24:50 +030033std::vector<std::string> Options::benchmarks;
Alexandros Frantzisfd3ed312011-09-20 15:52:21 +030034std::vector<std::string> Options::benchmark_files;
Alexandros Frantzis35a1fa12011-06-15 14:25:14 +030035bool Options::validate = false;
Alexandros Frantzis3f2913a2012-03-20 14:55:49 +020036Options::FrameEnd Options::frame_end = Options::FrameEndDefault;
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030037std::pair<int,int> Options::size(800, 600);
Alexandros Frantzisced8c582011-06-09 12:54:56 +030038bool Options::list_scenes = false;
Alexandros Frantzis15bb0512011-09-20 12:45:24 +030039bool Options::show_all_options = false;
Alexandros Frantzis72483092011-06-08 14:24:18 +030040bool Options::show_debug = false;
41bool Options::show_help = false;
Jesse Barkeraf9c7bb2012-01-04 09:15:29 -080042bool Options::reuse_context = false;
Alexandros Frantzisae8deb92012-02-15 19:09:14 +020043bool Options::run_forever = false;
Alexandros Frantzis1c377f12012-02-16 11:03:42 +020044bool Options::annotate = false;
Alexandros Frantzisc047bb92012-03-20 13:29:25 +020045bool Options::offscreen = false;
Alexandros Frantzis967efea2012-05-10 12:18:53 +030046GLVisualConfig Options::visual_config;
Alexandros Frantzis72483092011-06-08 14:24:18 +030047
48static struct option long_options[] = {
Alexandros Frantzis1c377f12012-02-16 11:03:42 +020049 {"annotate", 0, 0, 0},
Alexandros Frantzisd97db372011-06-09 17:24:50 +030050 {"benchmark", 1, 0, 0},
Alexandros Frantzisfd3ed312011-09-20 15:52:21 +030051 {"benchmark-file", 1, 0, 0},
Alexandros Frantzis35a1fa12011-06-15 14:25:14 +030052 {"validate", 0, 0, 0},
Alexandros Frantzis3f2913a2012-03-20 14:55:49 +020053 {"frame-end", 1, 0, 0},
Alexandros Frantzisc047bb92012-03-20 13:29:25 +020054 {"off-screen", 0, 0, 0},
Alexandros Frantzis967efea2012-05-10 12:18:53 +030055 {"visual-config", 1, 0, 0},
Jesse Barkeraf9c7bb2012-01-04 09:15:29 -080056 {"reuse-context", 0, 0, 0},
Alexandros Frantzisae8deb92012-02-15 19:09:14 +020057 {"run-forever", 0, 0, 0},
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030058 {"size", 1, 0, 0},
Alexandros Frantziscfffb572012-06-15 12:03:04 +030059 {"fullscreen", 0, 0, 0},
Alexandros Frantzisced8c582011-06-09 12:54:56 +030060 {"list-scenes", 0, 0, 0},
Alexandros Frantzis15bb0512011-09-20 12:45:24 +030061 {"show-all-options", 0, 0, 0},
Alexandros Frantzis72483092011-06-08 14:24:18 +030062 {"debug", 0, 0, 0},
63 {"help", 0, 0, 0},
64 {0, 0, 0, 0}
65};
66
Alexandros Frantzisebdfc722011-10-26 14:12:19 +030067/**
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030068 * Parses a size string of the form WxH
Alexandros Frantzisebdfc722011-10-26 14:12:19 +030069 *
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030070 * @param str the string to parse
Alexandros Frantzisebdfc722011-10-26 14:12:19 +030071 * @param size the parsed size (width, height)
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030072 */
73static void
74parse_size(const std::string &str, std::pair<int,int> &size)
75{
76 std::vector<std::string> d;
77 Util::split(str, 'x', d);
78
Alexandros Frantzis15832232011-11-01 18:46:08 +020079 size.first = Util::fromString<int>(d[0]);
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030080
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 */
Alexandros Frantzis15832232011-11-01 18:46:08 +020085 if (d.size() > 1)
86 size.second = Util::fromString<int>(d[1]);
Alexandros Frantzisebdfc722011-10-26 14:12:19 +030087 else
Alexandros Frantziscad7bef2011-08-09 14:13:43 +030088 size.second = size.first;
89}
90
Alexandros Frantzis3f2913a2012-03-20 14:55:49 +020091/**
92 * Parses a frame-end method string
93 *
94 * @param str the string to parse
95 *
96 * @return the parsed frame end method
97 */
98static Options::FrameEnd
99frame_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
Alexandros Frantzis72483092011-06-08 14:24:18 +0300115void
116Options::print_help()
117{
118 printf("A benchmark for Open GL (ES) 2.0\n"
119 "\n"
120 "Options:\n"
Alexandros Frantzis4e01b6b2011-06-09 17:54:24 +0300121 " -b, --benchmark BENCH A benchmark to run: 'scene(:opt1=val1)*'\n"
122 " (the option can be used multiple times)\n"
Alexandros Frantzisfd3ed312011-09-20 15:52:21 +0300123 " -f, --benchmark-file F Load benchmarks to run from a file containing a\n"
Alexandros Frantzisd03a01b2011-09-21 17:10:37 +0300124 " list of benchmark descriptions (one per line)\n"
125 " (the option can be used multiple times)\n"
Alexandros Frantzis35a1fa12011-06-15 14:25:14 +0300126 " --validate Run a quick output validation test instead of \n"
127 " running the benchmarks\n"
Alexandros Frantzis3f2913a2012-03-20 14:55:49 +0200128 " --frame-end METHOD How to end a frame [default,none,swap,finish,readpixels]\n"
Alexandros Frantzisc047bb92012-03-20 13:29:25 +0200129 " --off-screen Render to an off-screen surface\n"
Alexandros Frantzis967efea2012-05-10 12:18:53 +0300130 " --visual-config C The visual configuration to use for the rendering\n"
131 " target: 'red=R:green=G:blue=B:alpha=A:buffer=BUF'.\n"
132 " The parameters may be defined in any order, and any\n"
133 " omitted parameters assume a default value of '1'\n"
Jesse Barkeraf9c7bb2012-01-04 09:15:29 -0800134 " --reuse-context Use a single context for all scenes\n"
135 " (by default, each scene gets its own context)\n"
Alexandros Frantzisd03a01b2011-09-21 17:10:37 +0300136 " -s, --size WxH Size of the output window (default: 800x600)\n"
Alexandros Frantziscfffb572012-06-15 12:03:04 +0300137 " --fullscreen Run in fullscreen mode (equivalent to --size -1x-1)\n"
Alexandros Frantzis4e01b6b2011-06-09 17:54:24 +0300138 " -l, --list-scenes Display information about the available scenes\n"
139 " and their options\n"
Alexandros Frantzis15bb0512011-09-20 12:45:24 +0300140 " --show-all-options Show all scene option values used for benchmarks\n"
141 " (only explicitly set options are shown by default)\n"
Alexandros Frantzisae8deb92012-02-15 19:09:14 +0200142 " --run-forever Run indefinitely, looping from the last benchmark\n"
143 " back to the first\n"
Alexandros Frantzis1c377f12012-02-16 11:03:42 +0200144 " --annotate Annotate the benchmarks with on-screen information\n"
145 " (same as -b :show-fps=true:title=#info#)\n"
Alexandros Frantzis4e01b6b2011-06-09 17:54:24 +0300146 " -d, --debug Display debug messages\n"
147 " -h, --help Display help\n");
Alexandros Frantzis72483092011-06-08 14:24:18 +0300148}
149
Alexandros Frantziscabf0402011-06-09 14:34:18 +0300150bool
Alexandros Frantzis72483092011-06-08 14:24:18 +0300151Options::parse_args(int argc, char **argv)
152{
153 while (1) {
Alexandros Frantzis7af1c6f2011-06-09 17:49:13 +0300154 int option_index = -1;
Alexandros Frantzis72483092011-06-08 14:24:18 +0300155 int c;
Alexandros Frantzis7af1c6f2011-06-09 17:49:13 +0300156 const char *optname = "";
Alexandros Frantzis72483092011-06-08 14:24:18 +0300157
Alexandros Frantzisfd3ed312011-09-20 15:52:21 +0300158 c = getopt_long(argc, argv, "b:f:s:ldh",
Alexandros Frantzis72483092011-06-08 14:24:18 +0300159 long_options, &option_index);
160 if (c == -1)
161 break;
Alexandros Frantziscabf0402011-06-09 14:34:18 +0300162 if (c == ':' || c == '?')
163 return false;
Alexandros Frantzis72483092011-06-08 14:24:18 +0300164
Alexandros Frantzis7af1c6f2011-06-09 17:49:13 +0300165 if (option_index != -1)
166 optname = long_options[option_index].name;
Alexandros Frantzis72483092011-06-08 14:24:18 +0300167
Alexandros Frantzis1c377f12012-02-16 11:03:42 +0200168 if (!strcmp(optname, "annotate"))
169 Options::annotate = true;
Alexandros Frantzis5bd5d762011-06-09 17:48:20 +0300170 if (c == 'b' || !strcmp(optname, "benchmark"))
171 Options::benchmarks.push_back(optarg);
Alexandros Frantzisfd3ed312011-09-20 15:52:21 +0300172 else if (c == 'f' || !strcmp(optname, "benchmark-file"))
173 Options::benchmark_files.push_back(optarg);
Alexandros Frantzis35a1fa12011-06-15 14:25:14 +0300174 else if (!strcmp(optname, "validate"))
175 Options::validate = true;
Alexandros Frantzis3f2913a2012-03-20 14:55:49 +0200176 else if (!strcmp(optname, "frame-end"))
177 Options::frame_end = frame_end_from_str(optarg);
Alexandros Frantzisc047bb92012-03-20 13:29:25 +0200178 else if (!strcmp(optname, "off-screen"))
179 Options::offscreen = true;
Alexandros Frantzis967efea2012-05-10 12:18:53 +0300180 else if (!strcmp(optname, "visual-config"))
181 Options::visual_config = GLVisualConfig(optarg);
Jesse Barkeraf9c7bb2012-01-04 09:15:29 -0800182 else if (!strcmp(optname, "reuse-context"))
183 Options::reuse_context = true;
Alexandros Frantziscad7bef2011-08-09 14:13:43 +0300184 else if (c == 's' || !strcmp(optname, "size"))
185 parse_size(optarg, Options::size);
Alexandros Frantziscfffb572012-06-15 12:03:04 +0300186 else if (!strcmp(optname, "fullscreen"))
187 Options::size = std::pair<int,int>(-1, -1);
Alexandros Frantzis4e01b6b2011-06-09 17:54:24 +0300188 else if (c == 'l' || !strcmp(optname, "list-scenes"))
Alexandros Frantzis5bd5d762011-06-09 17:48:20 +0300189 Options::list_scenes = true;
Alexandros Frantzis15bb0512011-09-20 12:45:24 +0300190 else if (!strcmp(optname, "show-all-options"))
191 Options::show_all_options = true;
Alexandros Frantzisae8deb92012-02-15 19:09:14 +0200192 else if (!strcmp(optname, "run-forever"))
193 Options::run_forever = true;
Alexandros Frantzis4e01b6b2011-06-09 17:54:24 +0300194 else if (c == 'd' || !strcmp(optname, "debug"))
Alexandros Frantzis5bd5d762011-06-09 17:48:20 +0300195 Options::show_debug = true;
Alexandros Frantzis4e01b6b2011-06-09 17:54:24 +0300196 else if (c == 'h' || !strcmp(optname, "help"))
Alexandros Frantzis5bd5d762011-06-09 17:48:20 +0300197 Options::show_help = true;
Alexandros Frantzis72483092011-06-08 14:24:18 +0300198 }
Alexandros Frantziscabf0402011-06-09 14:34:18 +0300199
200 return true;
Alexandros Frantzis72483092011-06-08 14:24:18 +0300201}