blob: 88b54227e2864410520dc36cf3c8cc676cf24d6b [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");
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000059#if SK_SUPPORT_GPU
keyar@chromium.orga40c20d2012-08-20 15:04:12 +000060 SkDebugf(
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000061" 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.orgdb9a5fb2012-08-21 17:57:59 +000084
85 SkString result;
86 result.printf("running bench [%i %i] %s ", picture.width(), picture.height(),
87 filename.c_str());
88 sk_tools::print_msg(result.c_str());
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000089
keyar@chromium.org163b5672012-08-01 17:53:29 +000090 benchmark.run(&picture);
keyar@chromium.org0665f252012-07-10 18:30:18 +000091}
92
keyar@chromium.org163b5672012-08-01 17:53:29 +000093static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
94 sk_tools::PictureBenchmark*& benchmark) {
reed@google.com006db0f2012-06-27 19:33:29 +000095 const char* argv0 = argv[0];
96 char* const* stop = argv + argc;
97
keyar@chromium.org163b5672012-08-01 17:53:29 +000098 int repeats = DEFAULT_REPEATS;
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000099 sk_tools::PictureRenderer::SkDeviceTypes deviceType =
100 sk_tools::PictureRenderer::kBitmap_DeviceType;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000101
reed@google.com006db0f2012-06-27 19:33:29 +0000102 for (++argv; argv < stop; ++argv) {
103 if (0 == strcmp(*argv, "--repeat")) {
104 ++argv;
105 if (argv < stop) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000106 repeats = atoi(*argv);
107 if (repeats < 1) {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000108 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000109 SkDebugf("--repeat must be given a value > 0\n");
110 exit(-1);
111 }
112 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000113 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000114 SkDebugf("Missing arg for --repeat\n");
115 usage(argv0);
116 exit(-1);
117 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000118 } else if (0 == strcmp(*argv, "--mode")) {
119 SkDELETE(benchmark);
120
reed@google.com006db0f2012-06-27 19:33:29 +0000121 ++argv;
keyar@chromium.org795cd472012-08-02 18:57:53 +0000122 if (argv >= stop) {
123 SkDebugf("Missing mode for --mode\n");
124 usage(argv0);
125 exit(-1);
126 }
127
128 if (0 == strcmp(*argv, "pipe")) {
129 benchmark = SkNEW(sk_tools::PipePictureBenchmark);
130 } else if (0 == strcmp(*argv, "record")) {
131 benchmark = SkNEW(sk_tools::RecordPictureBenchmark);
132 } else if (0 == strcmp(*argv, "simple")) {
133 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
134 } else if (0 == strcmp(*argv, "tile")) {
135 sk_tools::TiledPictureBenchmark* tileBenchmark =
136 SkNEW(sk_tools::TiledPictureBenchmark);
137 ++argv;
138 if (argv >= stop) {
139 SkDELETE(tileBenchmark);
140 SkDebugf("Missing width for --mode tile\n");
141 usage(argv0);
142 exit(-1);
143 }
144
keyar@chromium.org163b5672012-08-01 17:53:29 +0000145 if (sk_tools::is_percentage(*argv)) {
146 tileBenchmark->setTileWidthPercentage(atof(*argv));
147 if (!(tileBenchmark->getTileWidthPercentage() > 0)) {
148 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000149 SkDebugf("--mode tile must be given a width percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000150 exit(-1);
151 }
152 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000153 tileBenchmark->setTileWidth(atoi(*argv));
154 if (!(tileBenchmark->getTileWidth() > 0)) {
155 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000156 SkDebugf("--mode tile must be given a width > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000157 exit(-1);
158 }
reed@google.com006db0f2012-06-27 19:33:29 +0000159 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000160
161 ++argv;
162 if (argv >= stop) {
163 SkDELETE(tileBenchmark);
164 SkDebugf("Missing height for --mode tile\n");
165 usage(argv0);
166 exit(-1);
167 }
168
keyar@chromium.org163b5672012-08-01 17:53:29 +0000169 if (sk_tools::is_percentage(*argv)) {
170 tileBenchmark->setTileHeightPercentage(atof(*argv));
171 if (!(tileBenchmark->getTileHeightPercentage() > 0)) {
172 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000173 SkDebugf("--mode tile must be given a height percentage > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000174 exit(-1);
175 }
176 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000177 tileBenchmark->setTileHeight(atoi(*argv));
178 if (!(tileBenchmark->getTileHeight() > 0)) {
179 SkDELETE(tileBenchmark);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000180 SkDebugf("--mode tile must be given a height > 0\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000181 exit(-1);
182 }
reed@google.com006db0f2012-06-27 19:33:29 +0000183 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000184
185 benchmark = tileBenchmark;
186 } else if (0 == strcmp(*argv, "unflatten")) {
187 benchmark = SkNEW(sk_tools::UnflattenPictureBenchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000188 } else {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000189 SkDebugf("%s is not a valid mode for --mode\n", *argv);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000190 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000191 exit(-1);
192 }
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000193 } else if (0 == strcmp(*argv, "--device")) {
194 ++argv;
195 if (argv >= stop) {
196 SkDebugf("Missing mode for --deivce\n");
197 usage(argv0);
198 exit(-1);
199 }
200
201 if (0 == strcmp(*argv, "bitmap")) {
202 deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
203 }
204#if SK_SUPPORT_GPU
205 else if (0 == strcmp(*argv, "gpu")) {
206 deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
207 }
208#endif
209 else {
210 SkDebugf("%s is not a valid mode for --device\n", *argv);
211 usage(argv0);
212 exit(-1);
213 }
214
reed@google.com006db0f2012-06-27 19:33:29 +0000215 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000216 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000217 usage(argv0);
218 exit(0);
219 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000220 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000221 }
222 }
223
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000224 if (inputs->count() < 1) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000225 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000226 usage(argv0);
227 exit(-1);
228 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000229
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000230 if (NULL == benchmark) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000231 benchmark = SkNEW(sk_tools::SimplePictureBenchmark);
232 }
233
234 benchmark->setRepeats(repeats);
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000235 benchmark->setDeviceType(deviceType);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000236}
reed@google.com006db0f2012-06-27 19:33:29 +0000237
keyar@chromium.org163b5672012-08-01 17:53:29 +0000238static void process_input(const SkString& input, sk_tools::PictureBenchmark& benchmark) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000239 SkOSFile::Iter iter(input.c_str(), "skp");
240 SkString inputFilename;
241
242 if (iter.next(&inputFilename)) {
243 do {
244 SkString inputPath;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000245 sk_tools::make_filepath(&inputPath, input, inputFilename);
246 run_single_benchmark(inputPath, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000247 } while(iter.next(&inputFilename));
248 } else {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000249 run_single_benchmark(input, benchmark);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000250 }
reed@google.com006db0f2012-06-27 19:33:29 +0000251}
252
253int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000254 SkTArray<SkString> inputs;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000255 sk_tools::PictureBenchmark* benchmark = NULL;
reed@google.com006db0f2012-06-27 19:33:29 +0000256
keyar@chromium.org163b5672012-08-01 17:53:29 +0000257 parse_commandline(argc, argv, &inputs, benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000258
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000259 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000260 process_input(inputs[i], *benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000261 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000262
263 SkDELETE(benchmark);
reed@google.com006db0f2012-06-27 19:33:29 +0000264}