blob: c595eb54f96d18ebbedd7d9820d9e1ffab0e1827 [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"
11#include "SkOSFile.h"
12#include "SkPicture.h"
13#include "SkStream.h"
14#include "SkTArray.h"
15#include "picture_utils.h"
16
17const int DEFAULT_REPEATS = 100;
reed@google.com006db0f2012-06-27 19:33:29 +000018
19static void usage(const char* argv0) {
20 SkDebugf("SkPicture benchmarking tool\n");
21 SkDebugf("\n"
22"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000023" %s <inputDir>...\n"
reed@google.com006db0f2012-06-27 19:33:29 +000024" [--repeat] [--tile width height]"
25, argv0);
26 SkDebugf("\n\n");
27 SkDebugf(
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000028" inputDir: A list of directories and files to use as input.\n"
29" Files are expected to have the .skp extension.\n\n");
reed@google.com006db0f2012-06-27 19:33:29 +000030 SkDebugf(
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000031" --pipe : "
32"Set to use piping."
33" Default is to not use piping.\n");
34 SkDebugf(
keyar@chromium.org163b5672012-08-01 17:53:29 +000035" --record : "
keyar@chromium.org21e3ed22012-07-16 19:20:14 +000036"Set to do a picture recording benchmark. Default is not to do this.\n");
37 SkDebugf(
reed@google.com006db0f2012-06-27 19:33:29 +000038" --repeat : "
39"Set the number of times to repeat each test."
40" Default is %i.\n", DEFAULT_REPEATS);
41 SkDebugf(
keyar@chromium.org0665f252012-07-10 18:30:18 +000042" --tile width[%] height[%]: "
43"Set to use the tiling size and specify the dimensions of each tile.\n"
44" Default is to not use tiling\n");
keyar@chromium.org49169712012-07-12 19:19:55 +000045 SkDebugf(
46" --unflatten: "
47"Set to do a picture unflattening benchmark. Default is not to do this.\n");
reed@google.com006db0f2012-06-27 19:33:29 +000048}
49
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000050static void run_single_benchmark(const SkString& inputPath,
keyar@chromium.org163b5672012-08-01 17:53:29 +000051 sk_tools::PictureBenchmark& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000052 SkFILEStream inputStream;
53
reed@google.com006db0f2012-06-27 19:33:29 +000054 inputStream.setPath(inputPath.c_str());
55 if (!inputStream.isValid()) {
56 SkDebugf("Could not open file %s\n", inputPath.c_str());
57 return;
58 }
59
60 SkPicture picture(&inputStream);
reed@google.com006db0f2012-06-27 19:33:29 +000061
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000062 SkString filename;
63 sk_tools::get_basename(&filename, inputPath);
keyar@chromium.org163b5672012-08-01 17:53:29 +000064 SkDebugf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000065 filename.c_str());
66
keyar@chromium.org163b5672012-08-01 17:53:29 +000067 benchmark.run(&picture);
keyar@chromium.org0665f252012-07-10 18:30:18 +000068}
69
keyar@chromium.org163b5672012-08-01 17:53:29 +000070static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
71 sk_tools::PictureBenchmark*& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000072 const char* argv0 = argv[0];
73 char* const* stop = argv + argc;
74
keyar@chromium.org163b5672012-08-01 17:53:29 +000075 int repeats = DEFAULT_REPEATS;
76
reed@google.com006db0f2012-06-27 19:33:29 +000077 for (++argv; argv < stop; ++argv) {
78 if (0 == strcmp(*argv, "--repeat")) {
79 ++argv;
80 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000081 repeats = atoi(*argv);
82 if (repeats < 1) {
83 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +000084 SkDebugf("--repeat must be given a value > 0\n");
85 exit(-1);
86 }
87 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +000088 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +000089 SkDebugf("Missing arg for --repeat\n");
90 usage(argv0);
91 exit(-1);
92 }
93 } else if (0 == strcmp(*argv, "--tile")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000094 sk_tools::TiledPictureBenchmark* tileBenchmark = SkNEW(sk_tools::TiledPictureBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +000095 ++argv;
96 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000097 if (sk_tools::is_percentage(*argv)) {
98 tileBenchmark->setTileWidthPercentage(atof(*argv));
99 if (!(tileBenchmark->getTileWidthPercentage() > 0)) {
100 SkDELETE(tileBenchmark);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000101 SkDebugf("--tile must be given a width percentage > 0\n");
102 exit(-1);
103 }
104 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000105 tileBenchmark->setTileWidth(atoi(*argv));
106 if (!(tileBenchmark->getTileWidth() > 0)) {
107 SkDELETE(tileBenchmark);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000108 SkDebugf("--tile must be given a width > 0\n");
109 exit(-1);
110 }
reed@google.com006db0f2012-06-27 19:33:29 +0000111 }
112 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000113 SkDELETE(tileBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000114 SkDebugf("Missing width for --tile\n");
115 usage(argv0);
116 exit(-1);
117 }
118 ++argv;
119 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000120 if (sk_tools::is_percentage(*argv)) {
121 tileBenchmark->setTileHeightPercentage(atof(*argv));
122 if (!(tileBenchmark->getTileHeightPercentage() > 0)) {
123 SkDELETE(tileBenchmark);
124 SkDebugf("--tile must be given a height percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000125 exit(-1);
126 }
127 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000128 tileBenchmark->setTileHeight(atoi(*argv));
129 if (!(tileBenchmark->getTileHeight() > 0)) {
130 SkDELETE(tileBenchmark);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000131 SkDebugf("--tile must be given a height > 0\n");
132 exit(-1);
133 }
reed@google.com006db0f2012-06-27 19:33:29 +0000134 }
135 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000136 SkDELETE(tileBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000137 SkDebugf("Missing height for --tile\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000138 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000139 exit(-1);
140 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000141 benchmark = tileBenchmark;
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000142 } else if (0 == strcmp(*argv, "--pipe")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000143 benchmark = SkNEW(sk_tools::PipePictureBenchmark);
keyar@chromium.org21e3ed22012-07-16 19:20:14 +0000144 } else if (0 == strcmp(*argv, "--record")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000145 benchmark = SkNEW(sk_tools::RecordPictureBenchmark);
keyar@chromium.org49169712012-07-12 19:19:55 +0000146 } else if (0 == strcmp(*argv, "--unflatten")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000147 benchmark = SkNEW(sk_tools::UnflattenPictureBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000148 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000149 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000150 usage(argv0);
151 exit(0);
152 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000153 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000154 }
155 }
156
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000157 if (inputs->count() < 1) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000158 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000159 usage(argv0);
160 exit(-1);
161 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000162
163 if (benchmark == NULL) {
164 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
165 }
166
167 benchmark->setRepeats(repeats);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000168}
reed@google.com006db0f2012-06-27 19:33:29 +0000169
keyar@chromium.org163b5672012-08-01 17:53:29 +0000170static void process_input(const SkString& input, sk_tools::PictureBenchmark& benchmark) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000171 SkOSFile::Iter iter(input.c_str(), "skp");
172 SkString inputFilename;
173
174 if (iter.next(&inputFilename)) {
175 do {
176 SkString inputPath;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000177 sk_tools::make_filepath(&inputPath, input, inputFilename);
178 run_single_benchmark(inputPath, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000179 } while(iter.next(&inputFilename));
180 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000181 run_single_benchmark(input, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000182 }
reed@google.com006db0f2012-06-27 19:33:29 +0000183}
184
185int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000186 SkTArray<SkString> inputs;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000187 sk_tools::PictureBenchmark* benchmark = NULL;
reed@google.com006db0f2012-06-27 19:33:29 +0000188
keyar@chromium.org163b5672012-08-01 17:53:29 +0000189 parse_commandline(argc, argv, &inputs, benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000190
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000191 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000192 process_input(inputs[i], *benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000193 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000194
195 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000196}