blob: ae9e850b1a74aeb5a337f4dbb20ae403777f89c3 [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"
keyar@chromium.org795cd472012-08-02 18:57:53 +000024" [--repeat] \n"
25" [--mode pipe | record | simple | tile width[%] height[%] | unflatten]"
reed@google.com006db0f2012-06-27 19:33:29 +000026, argv0);
27 SkDebugf("\n\n");
28 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000029" inputDir: A list of directories and files to use as input. Files are\n"
30" expected to have the .skp extension.\n\n");
reed@google.com006db0f2012-06-27 19:33:29 +000031 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000032" --mode pipe | record | simple | tile width[%] height[%] | unflatten: Run\n"
33" in the corresponding mode. Default is simple.\n");
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000034 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000035" pipe, Benchmark SkGPipe rendering.\n");
keyar@chromium.org21e3ed22012-07-16 19:20:14 +000036 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000037" record, Benchmark picture to picture recording.\n");
38 SkDebugf(
39" simple, Benchmark a simple rendering.\n");
40 SkDebugf(
41" tile width[%] height[%], Benchmark simple rendering using\n"
42" tiles with the given dimensions.\n");
43 SkDebugf(
44" unflatten, Benchmark picture unflattening.\n");
45 SkDebugf("\n");
46 SkDebugf(
47" --repeat: "
reed@google.com006db0f2012-06-27 19:33:29 +000048"Set the number of times to repeat each test."
49" Default is %i.\n", DEFAULT_REPEATS);
reed@google.com006db0f2012-06-27 19:33:29 +000050}
51
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000052static void run_single_benchmark(const SkString& inputPath,
keyar@chromium.org163b5672012-08-01 17:53:29 +000053 sk_tools::PictureBenchmark& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000054 SkFILEStream inputStream;
55
reed@google.com006db0f2012-06-27 19:33:29 +000056 inputStream.setPath(inputPath.c_str());
57 if (!inputStream.isValid()) {
58 SkDebugf("Could not open file %s\n", inputPath.c_str());
59 return;
60 }
61
62 SkPicture picture(&inputStream);
reed@google.com006db0f2012-06-27 19:33:29 +000063
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000064 SkString filename;
65 sk_tools::get_basename(&filename, inputPath);
keyar@chromium.org163b5672012-08-01 17:53:29 +000066 SkDebugf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000067 filename.c_str());
68
keyar@chromium.org163b5672012-08-01 17:53:29 +000069 benchmark.run(&picture);
keyar@chromium.org0665f252012-07-10 18:30:18 +000070}
71
keyar@chromium.org163b5672012-08-01 17:53:29 +000072static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
73 sk_tools::PictureBenchmark*& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000074 const char* argv0 = argv[0];
75 char* const* stop = argv + argc;
76
keyar@chromium.org163b5672012-08-01 17:53:29 +000077 int repeats = DEFAULT_REPEATS;
78
reed@google.com006db0f2012-06-27 19:33:29 +000079 for (++argv; argv < stop; ++argv) {
80 if (0 == strcmp(*argv, "--repeat")) {
81 ++argv;
82 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +000083 repeats = atoi(*argv);
84 if (repeats < 1) {
keyar@chromium.org795cd472012-08-02 18:57:53 +000085 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +000086 SkDebugf("--repeat must be given a value > 0\n");
87 exit(-1);
88 }
89 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +000090 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +000091 SkDebugf("Missing arg for --repeat\n");
92 usage(argv0);
93 exit(-1);
94 }
keyar@chromium.org795cd472012-08-02 18:57:53 +000095 } else if (0 == strcmp(*argv, "--mode")) {
96 SkDELETE(benchmark);
97
reed@google.com006db0f2012-06-27 19:33:29 +000098 ++argv;
keyar@chromium.org795cd472012-08-02 18:57:53 +000099 if (argv >= stop) {
100 SkDebugf("Missing mode for --mode\n");
101 usage(argv0);
102 exit(-1);
103 }
104
105 if (0 == strcmp(*argv, "pipe")) {
106 benchmark = SkNEW(sk_tools::PipePictureBenchmark);
107 } else if (0 == strcmp(*argv, "record")) {
108 benchmark = SkNEW(sk_tools::RecordPictureBenchmark);
109 } else if (0 == strcmp(*argv, "simple")) {
110 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
111 } else if (0 == strcmp(*argv, "tile")) {
112 sk_tools::TiledPictureBenchmark* tileBenchmark =
113 SkNEW(sk_tools::TiledPictureBenchmark);
114 ++argv;
115 if (argv >= stop) {
116 SkDELETE(tileBenchmark);
117 SkDebugf("Missing width for --mode tile\n");
118 usage(argv0);
119 exit(-1);
120 }
121
keyar@chromium.org163b5672012-08-01 17:53:29 +0000122 if (sk_tools::is_percentage(*argv)) {
123 tileBenchmark->setTileWidthPercentage(atof(*argv));
124 if (!(tileBenchmark->getTileWidthPercentage() > 0)) {
125 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000126 SkDebugf("--mode tile must be given a width percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000127 exit(-1);
128 }
129 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000130 tileBenchmark->setTileWidth(atoi(*argv));
131 if (!(tileBenchmark->getTileWidth() > 0)) {
132 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000133 SkDebugf("--mode tile must be given a width > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000134 exit(-1);
135 }
reed@google.com006db0f2012-06-27 19:33:29 +0000136 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000137
138 ++argv;
139 if (argv >= stop) {
140 SkDELETE(tileBenchmark);
141 SkDebugf("Missing height for --mode tile\n");
142 usage(argv0);
143 exit(-1);
144 }
145
keyar@chromium.org163b5672012-08-01 17:53:29 +0000146 if (sk_tools::is_percentage(*argv)) {
147 tileBenchmark->setTileHeightPercentage(atof(*argv));
148 if (!(tileBenchmark->getTileHeightPercentage() > 0)) {
149 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000150 SkDebugf("--mode tile must be given a height percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000151 exit(-1);
152 }
153 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000154 tileBenchmark->setTileHeight(atoi(*argv));
155 if (!(tileBenchmark->getTileHeight() > 0)) {
156 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000157 SkDebugf("--mode tile must be given a height > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000158 exit(-1);
159 }
reed@google.com006db0f2012-06-27 19:33:29 +0000160 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000161
162 benchmark = tileBenchmark;
163 } else if (0 == strcmp(*argv, "unflatten")) {
164 benchmark = SkNEW(sk_tools::UnflattenPictureBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000165 } else {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000166 SkDebugf("%s is not a valid mode for --mode\n", *argv);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000167 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000168 exit(-1);
169 }
170 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000171 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000172 usage(argv0);
173 exit(0);
174 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000175 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000176 }
177 }
178
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000179 if (inputs->count() < 1) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000180 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000181 usage(argv0);
182 exit(-1);
183 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000184
185 if (benchmark == NULL) {
186 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
187 }
188
189 benchmark->setRepeats(repeats);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000190}
reed@google.com006db0f2012-06-27 19:33:29 +0000191
keyar@chromium.org163b5672012-08-01 17:53:29 +0000192static void process_input(const SkString& input, sk_tools::PictureBenchmark& benchmark) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000193 SkOSFile::Iter iter(input.c_str(), "skp");
194 SkString inputFilename;
195
196 if (iter.next(&inputFilename)) {
197 do {
198 SkString inputPath;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000199 sk_tools::make_filepath(&inputPath, input, inputFilename);
200 run_single_benchmark(inputPath, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000201 } while(iter.next(&inputFilename));
202 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000203 run_single_benchmark(input, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000204 }
reed@google.com006db0f2012-06-27 19:33:29 +0000205}
206
207int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000208 SkTArray<SkString> inputs;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000209 sk_tools::PictureBenchmark* benchmark = NULL;
reed@google.com006db0f2012-06-27 19:33:29 +0000210
keyar@chromium.org163b5672012-08-01 17:53:29 +0000211 parse_commandline(argc, argv, &inputs, benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000212
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000213 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000214 process_input(inputs[i], *benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000215 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000216
217 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000218}