blob: 1579f692f40b63ff5eca34fcdfa419b3fe3e1906 [file] [log] [blame]
reed@google.com006db0f2012-06-27 19:33:29 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "BenchTimer.h"
keyar@chromium.org163b5672012-08-01 17:53:29 +00009#include "PictureBenchmark.h"
reed@google.com006db0f2012-06-27 19:33:29 +000010#include "SkCanvas.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000011#include "SkMath.h"
reed@google.com006db0f2012-06-27 19:33:29 +000012#include "SkOSFile.h"
13#include "SkPicture.h"
14#include "SkStream.h"
15#include "SkTArray.h"
16#include "picture_utils.h"
17
18const int DEFAULT_REPEATS = 100;
reed@google.com006db0f2012-06-27 19:33:29 +000019
20static void usage(const char* argv0) {
21 SkDebugf("SkPicture benchmarking tool\n");
22 SkDebugf("\n"
23"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000024" %s <inputDir>...\n"
keyar@chromium.org795cd472012-08-02 18:57:53 +000025" [--repeat] \n"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000026" [--mode pipe | pow2tile minWidth height[%] | record | simple\n"
27" | tile width[%] height[%] | unflatten]\n"
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000028" [--device bitmap"
29#if SK_SUPPORT_GPU
30" | gpu"
31#endif
32"]"
reed@google.com006db0f2012-06-27 19:33:29 +000033, argv0);
34 SkDebugf("\n\n");
35 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000036" inputDir: A list of directories and files to use as input. Files are\n"
37" expected to have the .skp extension.\n\n");
reed@google.com006db0f2012-06-27 19:33:29 +000038 SkDebugf(
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000039" --mode pipe | pow2tile minWidht height[%] | record | simple\n"
40" | tile width[%] height[%] | unflatten: Run in the corresponding mode.\n"
41" Default is simple.\n");
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000042 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000043" pipe, Benchmark SkGPipe rendering.\n");
keyar@chromium.org21e3ed22012-07-16 19:20:14 +000044 SkDebugf(
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000045" pow2tile minWidth height[%], Creates tiles with widths\n"
46" that are all a power of two\n"
47" such that they minimize the\n"
48" amount of wasted tile space.\n"
49" minWidth is the minimum width\n"
50" of these tiles and must be a\n"
51" power of two. Simple\n"
52" rendering using these tiles\n"
53" is benchmarked.\n");
54 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000055" record, Benchmark picture to picture recording.\n");
56 SkDebugf(
57" simple, Benchmark a simple rendering.\n");
58 SkDebugf(
59" tile width[%] height[%], Benchmark simple rendering using\n"
60" tiles with the given dimensions.\n");
61 SkDebugf(
62" unflatten, Benchmark picture unflattening.\n");
63 SkDebugf("\n");
64 SkDebugf(
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000065" --device bitmap"
66#if SK_SUPPORT_GPU
67" | gpu"
68#endif
69": Use the corresponding device. Default is bitmap.\n");
70 SkDebugf(
71" bitmap, Render to a bitmap.\n");
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000072#if SK_SUPPORT_GPU
keyar@chromium.orga40c20d2012-08-20 15:04:12 +000073 SkDebugf(
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000074" gpu, Render to the GPU.\n");
75#endif
76 SkDebugf("\n");
77 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000078" --repeat: "
reed@google.com006db0f2012-06-27 19:33:29 +000079"Set the number of times to repeat each test."
80" Default is %i.\n", DEFAULT_REPEATS);
reed@google.com006db0f2012-06-27 19:33:29 +000081}
82
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000083static void run_single_benchmark(const SkString& inputPath,
keyar@chromium.org163b5672012-08-01 17:53:29 +000084 sk_tools::PictureBenchmark& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000085 SkFILEStream inputStream;
86
reed@google.com006db0f2012-06-27 19:33:29 +000087 inputStream.setPath(inputPath.c_str());
88 if (!inputStream.isValid()) {
89 SkDebugf("Could not open file %s\n", inputPath.c_str());
90 return;
91 }
92
93 SkPicture picture(&inputStream);
reed@google.com006db0f2012-06-27 19:33:29 +000094
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000095 SkString filename;
96 sk_tools::get_basename(&filename, inputPath);
keyar@chromium.orgdb9a5fb2012-08-21 17:57:59 +000097
98 SkString result;
99 result.printf("running bench [%i %i] %s ", picture.width(), picture.height(),
100 filename.c_str());
101 sk_tools::print_msg(result.c_str());
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000102
keyar@chromium.org163b5672012-08-01 17:53:29 +0000103 benchmark.run(&picture);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000104}
105
keyar@chromium.org163b5672012-08-01 17:53:29 +0000106static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
107 sk_tools::PictureBenchmark*& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +0000108 const char* argv0 = argv[0];
109 char* const* stop = argv + argc;
110
keyar@chromium.org163b5672012-08-01 17:53:29 +0000111 int repeats = DEFAULT_REPEATS;
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000112 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
113 sk_tools::PictureRenderer::kBitmap_DeviceType;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000114
reed@google.com006db0f2012-06-27 19:33:29 +0000115 for (++argv; argv < stop; ++argv) {
116 if (0 == strcmp(*argv, "--repeat")) {
117 ++argv;
118 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000119 repeats = atoi(*argv);
120 if (repeats < 1) {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000121 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000122 SkDebugf("--repeat must be given a value > 0\n");
123 exit(-1);
124 }
125 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000126 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000127 SkDebugf("Missing arg for --repeat\n");
128 usage(argv0);
129 exit(-1);
130 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000131 } else if (0 == strcmp(*argv, "--mode")) {
132 SkDELETE(benchmark);
133
reed@google.com006db0f2012-06-27 19:33:29 +0000134 ++argv;
keyar@chromium.org795cd472012-08-02 18:57:53 +0000135 if (argv >= stop) {
136 SkDebugf("Missing mode for --mode\n");
137 usage(argv0);
138 exit(-1);
139 }
140
141 if (0 == strcmp(*argv, "pipe")) {
142 benchmark = SkNEW(sk_tools::PipePictureBenchmark);
143 } else if (0 == strcmp(*argv, "record")) {
144 benchmark = SkNEW(sk_tools::RecordPictureBenchmark);
145 } else if (0 == strcmp(*argv, "simple")) {
146 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000147 } else if ((0 == strcmp(*argv, "tile")) || (0 == strcmp(*argv, "pow2tile"))) {
148 char* mode = *argv;
149 bool isPowerOf2Mode = false;
150
151 if (0 == strcmp(*argv, "pow2tile")) {
152 isPowerOf2Mode = true;
153 }
154
keyar@chromium.org795cd472012-08-02 18:57:53 +0000155 sk_tools::TiledPictureBenchmark* tileBenchmark =
156 SkNEW(sk_tools::TiledPictureBenchmark);
157 ++argv;
158 if (argv >= stop) {
159 SkDELETE(tileBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000160 SkDebugf("Missing width for --mode %s\n", mode);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000161 usage(argv0);
162 exit(-1);
163 }
164
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000165 if (isPowerOf2Mode) {
166 int minWidth = atoi(*argv);
167
168 if (!SkIsPow2(minWidth) || minWidth <= 0) {
169 SkDELETE(tileBenchmark);
170 SkDebugf("--mode %s must be given a width"
171 " value that is a power of two\n", mode);
172 exit(-1);
173 }
174
175 tileBenchmark->setTileMinPowerOf2Width(minWidth);
176 } else if (sk_tools::is_percentage(*argv)) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000177 tileBenchmark->setTileWidthPercentage(atof(*argv));
178 if (!(tileBenchmark->getTileWidthPercentage() > 0)) {
179 SkDELETE(tileBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000180 SkDebugf("--mode %s must be given a width percentage > 0\n", mode);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000181 exit(-1);
182 }
183 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000184 tileBenchmark->setTileWidth(atoi(*argv));
185 if (!(tileBenchmark->getTileWidth() > 0)) {
186 SkDELETE(tileBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000187 SkDebugf("--mode %s must be given a width > 0\n", mode);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000188 exit(-1);
189 }
reed@google.com006db0f2012-06-27 19:33:29 +0000190 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000191
192 ++argv;
193 if (argv >= stop) {
194 SkDELETE(tileBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000195 SkDebugf("Missing height for --mode %s\n", mode);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000196 usage(argv0);
197 exit(-1);
198 }
199
keyar@chromium.org163b5672012-08-01 17:53:29 +0000200 if (sk_tools::is_percentage(*argv)) {
201 tileBenchmark->setTileHeightPercentage(atof(*argv));
202 if (!(tileBenchmark->getTileHeightPercentage() > 0)) {
203 SkDELETE(tileBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000204 SkDebugf("--mode %s must be given a height percentage > 0\n", mode);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000205 exit(-1);
206 }
207 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000208 tileBenchmark->setTileHeight(atoi(*argv));
209 if (!(tileBenchmark->getTileHeight() > 0)) {
210 SkDELETE(tileBenchmark);
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000211 SkDebugf("--mode %s must be given a height > 0\n", mode);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000212 exit(-1);
213 }
reed@google.com006db0f2012-06-27 19:33:29 +0000214 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000215
216 benchmark = tileBenchmark;
217 } else if (0 == strcmp(*argv, "unflatten")) {
218 benchmark = SkNEW(sk_tools::UnflattenPictureBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000219 } else {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000220 SkDebugf("%s is not a valid mode for --mode\n", *argv);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000221 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000222 exit(-1);
223 }
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000224 } else if (0 == strcmp(*argv, "--device")) {
225 ++argv;
226 if (argv >= stop) {
227 SkDebugf("Missing mode for --deivce\n");
228 usage(argv0);
229 exit(-1);
230 }
231
232 if (0 == strcmp(*argv, "bitmap")) {
233 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
234 }
235#if SK_SUPPORT_GPU
236 else if (0 == strcmp(*argv, "gpu")) {
237 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
238 }
239#endif
240 else {
241 SkDebugf("%s is not a valid mode for --device\n", *argv);
242 usage(argv0);
243 exit(-1);
244 }
245
reed@google.com006db0f2012-06-27 19:33:29 +0000246 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000247 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000248 usage(argv0);
249 exit(0);
250 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000251 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000252 }
253 }
254
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000255 if (inputs->count() < 1) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000256 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000257 usage(argv0);
258 exit(-1);
259 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000260
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000261 if (NULL == benchmark) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000262 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
263 }
264
265 benchmark->setRepeats(repeats);
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000266 benchmark->setDeviceType(deviceType);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000267}
reed@google.com006db0f2012-06-27 19:33:29 +0000268
keyar@chromium.org163b5672012-08-01 17:53:29 +0000269static void process_input(const SkString& input, sk_tools::PictureBenchmark& benchmark) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000270 SkOSFile::Iter iter(input.c_str(), "skp");
271 SkString inputFilename;
272
273 if (iter.next(&inputFilename)) {
274 do {
275 SkString inputPath;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000276 sk_tools::make_filepath(&inputPath, input, inputFilename);
277 run_single_benchmark(inputPath, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000278 } while(iter.next(&inputFilename));
279 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000280 run_single_benchmark(input, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000281 }
reed@google.com006db0f2012-06-27 19:33:29 +0000282}
283
284int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000285 SkTArray<SkString> inputs;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000286 sk_tools::PictureBenchmark* benchmark = NULL;
reed@google.com006db0f2012-06-27 19:33:29 +0000287
keyar@chromium.org163b5672012-08-01 17:53:29 +0000288 parse_commandline(argc, argv, &inputs, benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000289
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000290 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000291 process_input(inputs[i], *benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000292 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000293
294 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000295}