blob: ef0dbf0207356317a06d1578bf2a6ad02bc9b39b [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"
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000025" [--mode pipe | record | simple | tile width[%] height[%] | unflatten]\n"
26" [--device bitmap"
27#if SK_SUPPORT_GPU
28" | gpu"
29#endif
30"]"
reed@google.com006db0f2012-06-27 19:33:29 +000031, argv0);
32 SkDebugf("\n\n");
33 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000034" inputDir: A list of directories and files to use as input. Files are\n"
35" expected to have the .skp extension.\n\n");
reed@google.com006db0f2012-06-27 19:33:29 +000036 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000037" --mode pipe | record | simple | tile width[%] height[%] | unflatten: Run\n"
38" in the corresponding mode. Default is simple.\n");
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000039 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000040" pipe, Benchmark SkGPipe rendering.\n");
keyar@chromium.org21e3ed22012-07-16 19:20:14 +000041 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000042" record, Benchmark picture to picture recording.\n");
43 SkDebugf(
44" simple, Benchmark a simple rendering.\n");
45 SkDebugf(
46" tile width[%] height[%], Benchmark simple rendering using\n"
47" tiles with the given dimensions.\n");
48 SkDebugf(
49" unflatten, Benchmark picture unflattening.\n");
50 SkDebugf("\n");
51 SkDebugf(
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000052" --device bitmap"
53#if SK_SUPPORT_GPU
54" | gpu"
55#endif
56": Use the corresponding device. Default is bitmap.\n");
57 SkDebugf(
58" bitmap, Render to a bitmap.\n");
59 SkDebugf(
60#if SK_SUPPORT_GPU
61" gpu, Render to the GPU.\n");
62#endif
63 SkDebugf("\n");
64 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000065" --repeat: "
reed@google.com006db0f2012-06-27 19:33:29 +000066"Set the number of times to repeat each test."
67" Default is %i.\n", DEFAULT_REPEATS);
reed@google.com006db0f2012-06-27 19:33:29 +000068}
69
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000070static void run_single_benchmark(const SkString& inputPath,
keyar@chromium.org163b5672012-08-01 17:53:29 +000071 sk_tools::PictureBenchmark& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000072 SkFILEStream inputStream;
73
reed@google.com006db0f2012-06-27 19:33:29 +000074 inputStream.setPath(inputPath.c_str());
75 if (!inputStream.isValid()) {
76 SkDebugf("Could not open file %s\n", inputPath.c_str());
77 return;
78 }
79
80 SkPicture picture(&inputStream);
reed@google.com006db0f2012-06-27 19:33:29 +000081
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000082 SkString filename;
83 sk_tools::get_basename(&filename, inputPath);
keyar@chromium.org163b5672012-08-01 17:53:29 +000084 SkDebugf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000085 filename.c_str());
86
keyar@chromium.org163b5672012-08-01 17:53:29 +000087 benchmark.run(&picture);
keyar@chromium.org0665f252012-07-10 18:30:18 +000088}
89
keyar@chromium.org163b5672012-08-01 17:53:29 +000090static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
91 sk_tools::PictureBenchmark*& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000092 const char* argv0 = argv[0];
93 char* const* stop = argv + argc;
94
keyar@chromium.org163b5672012-08-01 17:53:29 +000095 int repeats = DEFAULT_REPEATS;
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000096 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
97 sk_tools::PictureRenderer::kBitmap_DeviceType;
keyar@chromium.org163b5672012-08-01 17:53:29 +000098
reed@google.com006db0f2012-06-27 19:33:29 +000099 for (++argv; argv < stop; ++argv) {
100 if (0 == strcmp(*argv, "--repeat")) {
101 ++argv;
102 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000103 repeats = atoi(*argv);
104 if (repeats < 1) {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000105 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000106 SkDebugf("--repeat must be given a value > 0\n");
107 exit(-1);
108 }
109 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000110 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000111 SkDebugf("Missing arg for --repeat\n");
112 usage(argv0);
113 exit(-1);
114 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000115 } else if (0 == strcmp(*argv, "--mode")) {
116 SkDELETE(benchmark);
117
reed@google.com006db0f2012-06-27 19:33:29 +0000118 ++argv;
keyar@chromium.org795cd472012-08-02 18:57:53 +0000119 if (argv >= stop) {
120 SkDebugf("Missing mode for --mode\n");
121 usage(argv0);
122 exit(-1);
123 }
124
125 if (0 == strcmp(*argv, "pipe")) {
126 benchmark = SkNEW(sk_tools::PipePictureBenchmark);
127 } else if (0 == strcmp(*argv, "record")) {
128 benchmark = SkNEW(sk_tools::RecordPictureBenchmark);
129 } else if (0 == strcmp(*argv, "simple")) {
130 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
131 } else if (0 == strcmp(*argv, "tile")) {
132 sk_tools::TiledPictureBenchmark* tileBenchmark =
133 SkNEW(sk_tools::TiledPictureBenchmark);
134 ++argv;
135 if (argv >= stop) {
136 SkDELETE(tileBenchmark);
137 SkDebugf("Missing width for --mode tile\n");
138 usage(argv0);
139 exit(-1);
140 }
141
keyar@chromium.org163b5672012-08-01 17:53:29 +0000142 if (sk_tools::is_percentage(*argv)) {
143 tileBenchmark->setTileWidthPercentage(atof(*argv));
144 if (!(tileBenchmark->getTileWidthPercentage() > 0)) {
145 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000146 SkDebugf("--mode tile must be given a width percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000147 exit(-1);
148 }
149 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000150 tileBenchmark->setTileWidth(atoi(*argv));
151 if (!(tileBenchmark->getTileWidth() > 0)) {
152 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000153 SkDebugf("--mode tile must be given a width > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000154 exit(-1);
155 }
reed@google.com006db0f2012-06-27 19:33:29 +0000156 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000157
158 ++argv;
159 if (argv >= stop) {
160 SkDELETE(tileBenchmark);
161 SkDebugf("Missing height for --mode tile\n");
162 usage(argv0);
163 exit(-1);
164 }
165
keyar@chromium.org163b5672012-08-01 17:53:29 +0000166 if (sk_tools::is_percentage(*argv)) {
167 tileBenchmark->setTileHeightPercentage(atof(*argv));
168 if (!(tileBenchmark->getTileHeightPercentage() > 0)) {
169 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000170 SkDebugf("--mode tile must be given a height percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000171 exit(-1);
172 }
173 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000174 tileBenchmark->setTileHeight(atoi(*argv));
175 if (!(tileBenchmark->getTileHeight() > 0)) {
176 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000177 SkDebugf("--mode tile must be given a height > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000178 exit(-1);
179 }
reed@google.com006db0f2012-06-27 19:33:29 +0000180 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000181
182 benchmark = tileBenchmark;
183 } else if (0 == strcmp(*argv, "unflatten")) {
184 benchmark = SkNEW(sk_tools::UnflattenPictureBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000185 } else {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000186 SkDebugf("%s is not a valid mode for --mode\n", *argv);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000187 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000188 exit(-1);
189 }
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000190 } else if (0 == strcmp(*argv, "--device")) {
191 ++argv;
192 if (argv >= stop) {
193 SkDebugf("Missing mode for --deivce\n");
194 usage(argv0);
195 exit(-1);
196 }
197
198 if (0 == strcmp(*argv, "bitmap")) {
199 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
200 }
201#if SK_SUPPORT_GPU
202 else if (0 == strcmp(*argv, "gpu")) {
203 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
204 }
205#endif
206 else {
207 SkDebugf("%s is not a valid mode for --device\n", *argv);
208 usage(argv0);
209 exit(-1);
210 }
211
reed@google.com006db0f2012-06-27 19:33:29 +0000212 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000213 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000214 usage(argv0);
215 exit(0);
216 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000217 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000218 }
219 }
220
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000221 if (inputs->count() < 1) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000222 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000223 usage(argv0);
224 exit(-1);
225 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000226
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000227 if (NULL == benchmark) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000228 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
229 }
230
231 benchmark->setRepeats(repeats);
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000232 benchmark->setDeviceType(deviceType);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000233}
reed@google.com006db0f2012-06-27 19:33:29 +0000234
keyar@chromium.org163b5672012-08-01 17:53:29 +0000235static void process_input(const SkString& input, sk_tools::PictureBenchmark& benchmark) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000236 SkOSFile::Iter iter(input.c_str(), "skp");
237 SkString inputFilename;
238
239 if (iter.next(&inputFilename)) {
240 do {
241 SkString inputPath;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000242 sk_tools::make_filepath(&inputPath, input, inputFilename);
243 run_single_benchmark(inputPath, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000244 } while(iter.next(&inputFilename));
245 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000246 run_single_benchmark(input, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000247 }
reed@google.com006db0f2012-06-27 19:33:29 +0000248}
249
250int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000251 SkTArray<SkString> inputs;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000252 sk_tools::PictureBenchmark* benchmark = NULL;
reed@google.com006db0f2012-06-27 19:33:29 +0000253
keyar@chromium.org163b5672012-08-01 17:53:29 +0000254 parse_commandline(argc, argv, &inputs, benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000255
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000256 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000257 process_input(inputs[i], *benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000258 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000259
260 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000261}