blob: aca98e43006082ff319ec7969e6e8926b5494496 [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.orgcf6c44c2012-07-09 19:37:40 +00009#include "SamplePipeControllers.h"
reed@google.com006db0f2012-06-27 19:33:29 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000012#include "SkGPipe.h"
reed@google.com006db0f2012-06-27 19:33:29 +000013#include "SkOSFile.h"
14#include "SkPicture.h"
15#include "SkStream.h"
16#include "SkTArray.h"
17#include "picture_utils.h"
18
19const int DEFAULT_REPEATS = 100;
20const int DEFAULT_TILE_WIDTH = 256;
21const int DEFAULT_TILE_HEIGHT = 256;
22
23struct Options;
keyar@chromium.orga4091ba2012-07-10 19:53:59 +000024static void run_simple_benchmark(SkPicture* picture, const Options&);
reed@google.com006db0f2012-06-27 19:33:29 +000025
26struct Options {
27 int fRepeats;
keyar@chromium.orga4091ba2012-07-10 19:53:59 +000028 void (*fBenchmark) (SkPicture*, const Options& options);
reed@google.com006db0f2012-06-27 19:33:29 +000029 int fTileWidth;
30 int fTileHeight;
keyar@chromium.org0665f252012-07-10 18:30:18 +000031 double fTileWidthPercentage;
32 double fTileHeightPercentage;
reed@google.com006db0f2012-06-27 19:33:29 +000033
34 Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
keyar@chromium.org0665f252012-07-10 18:30:18 +000035 fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT),
36 fTileWidthPercentage(0), fTileHeightPercentage(0){}
reed@google.com006db0f2012-06-27 19:33:29 +000037};
38
39static void usage(const char* argv0) {
40 SkDebugf("SkPicture benchmarking tool\n");
41 SkDebugf("\n"
42"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000043" %s <inputDir>...\n"
reed@google.com006db0f2012-06-27 19:33:29 +000044" [--repeat] [--tile width height]"
45, argv0);
46 SkDebugf("\n\n");
47 SkDebugf(
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000048" inputDir: A list of directories and files to use as input.\n"
49" Files are expected to have the .skp extension.\n\n");
reed@google.com006db0f2012-06-27 19:33:29 +000050 SkDebugf(
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000051" --pipe : "
52"Set to use piping."
53" Default is to not use piping.\n");
54 SkDebugf(
reed@google.com006db0f2012-06-27 19:33:29 +000055" --repeat : "
56"Set the number of times to repeat each test."
57" Default is %i.\n", DEFAULT_REPEATS);
58 SkDebugf(
keyar@chromium.org0665f252012-07-10 18:30:18 +000059" --tile width[%] height[%]: "
60"Set to use the tiling size and specify the dimensions of each tile.\n"
61" Default is to not use tiling\n");
keyar@chromium.org49169712012-07-12 19:19:55 +000062 SkDebugf(
63" --unflatten: "
64"Set to do a picture unflattening benchmark. Default is not to do this.\n");
reed@google.com006db0f2012-06-27 19:33:29 +000065}
66
keyar@chromium.orga4091ba2012-07-10 19:53:59 +000067static void run_simple_benchmark(SkPicture* picture, const Options& options) {
68 SkBitmap bitmap;
69 sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());
70
reed@google.com006db0f2012-06-27 19:33:29 +000071 SkCanvas canvas(bitmap);
72
73 // We throw this away to remove first time effects (such as paging in this
74 // program)
75 canvas.drawPicture(*picture);
76
77 BenchTimer timer = BenchTimer(NULL);
78 timer.start();
79 for (int i = 0; i < options.fRepeats; ++i) {
80 canvas.drawPicture(*picture);
81 }
82 timer.end();
83
keyar@chromium.orgb5e30ab2012-07-12 21:16:49 +000084 printf("simple: msecs = %6.2f\n", timer.fWall / options.fRepeats);
reed@google.com006db0f2012-06-27 19:33:29 +000085}
86
87struct TileInfo {
88 SkBitmap* fBitmap;
89 SkCanvas* fCanvas;
90};
91
keyar@chromium.org70b42222012-07-09 19:51:05 +000092static void clip_tile(SkPicture* picture, const TileInfo& tile) {
keyar@chromium.org3e8483e2012-07-13 18:15:04 +000093 SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()),
robertphillips@google.com59f46b82012-07-10 17:30:58 +000094 SkIntToScalar(picture->height()));
keyar@chromium.org70b42222012-07-09 19:51:05 +000095 tile.fCanvas->clipRect(clip);
96}
97
keyar@chromium.org3e8483e2012-07-13 18:15:04 +000098static void setup_single_tile(SkPicture* picture, const Options& options,
99 SkTArray<TileInfo>* tiles, int tile_x_start, int tile_y_start) {
reed@google.com006db0f2012-06-27 19:33:29 +0000100 TileInfo& tile = tiles->push_back();
keyar@chromium.org70b42222012-07-09 19:51:05 +0000101
keyar@chromium.org3e8483e2012-07-13 18:15:04 +0000102 tile.fBitmap = new SkBitmap();
103 sk_tools::setup_bitmap(tile.fBitmap, options.fTileWidth, options.fTileHeight);
104
105 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
106 tile.fCanvas->translate(SkIntToScalar(-tile_x_start), SkIntToScalar(-tile_y_start));
keyar@chromium.org70b42222012-07-09 19:51:05 +0000107 clip_tile(picture, tile);
reed@google.com006db0f2012-06-27 19:33:29 +0000108}
109
keyar@chromium.org3e8483e2012-07-13 18:15:04 +0000110static void setup_tiles(SkPicture* picture, const Options& options, SkTArray<TileInfo>* tiles) {
reed@google.com006db0f2012-06-27 19:33:29 +0000111 for (int tile_y_start = 0; tile_y_start < picture->height();
112 tile_y_start += options.fTileHeight) {
113 for (int tile_x_start = 0; tile_x_start < picture->width();
114 tile_x_start += options.fTileWidth) {
keyar@chromium.org3e8483e2012-07-13 18:15:04 +0000115 setup_single_tile(picture, options, tiles, tile_x_start, tile_y_start);
reed@google.com006db0f2012-06-27 19:33:29 +0000116 }
117 }
118
119}
120
keyar@chromium.orga4091ba2012-07-10 19:53:59 +0000121static void run_tile_benchmark(SkPicture* picture, const Options& options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000122 SkTArray<TileInfo> tiles;
keyar@chromium.org3e8483e2012-07-13 18:15:04 +0000123 setup_tiles(picture, options, &tiles);
reed@google.com006db0f2012-06-27 19:33:29 +0000124
125 // We throw this away to remove first time effects (such as paging in this
126 // program)
127 for (int j = 0; j < tiles.count(); ++j) {
128 tiles[j].fCanvas->drawPicture(*picture);
129 }
130
131 BenchTimer timer = BenchTimer(NULL);
132 timer.start();
133 for (int i = 0; i < options.fRepeats; ++i) {
134 for (int j = 0; j < tiles.count(); ++j) {
135 tiles[j].fCanvas->drawPicture(*picture);
136 }
137 }
138 timer.end();
139
140 for (int i = 0; i < tiles.count(); ++i) {
141 delete tiles[i].fCanvas;
142 delete tiles[i].fBitmap;
143 }
144
keyar@chromium.orgb5e30ab2012-07-12 21:16:49 +0000145 printf("%i_tiles_%ix%i: msecs = %6.2f\n", tiles.count(), options.fTileWidth,
reed@google.com006db0f2012-06-27 19:33:29 +0000146 options.fTileHeight, timer.fWall / options.fRepeats);
147}
148
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000149static void pipe_run(SkPicture* picture, SkCanvas* canvas) {
150 PipeController pipeController(canvas);
151 SkGPipeWriter writer;
152 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
153 pipeCanvas->drawPicture(*picture);
154 writer.endRecording();
155}
156
keyar@chromium.orga4091ba2012-07-10 19:53:59 +0000157static void run_pipe_benchmark(SkPicture* picture, const Options& options) {
158 SkBitmap bitmap;
159 sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height());
160
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000161 SkCanvas canvas(bitmap);
162
163 // We throw this away to remove first time effects (such as paging in this
164 // program)
165 pipe_run(picture, &canvas);
166
167 BenchTimer timer = BenchTimer(NULL);
168 timer.start();
169 for (int i = 0; i < options.fRepeats; ++i) {
170 pipe_run(picture, &canvas);
171 }
172 timer.end();
173
keyar@chromium.orgb5e30ab2012-07-12 21:16:49 +0000174 printf("pipe: msecs = %6.2f\n", timer.fWall / options.fRepeats);
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000175}
176
keyar@chromium.org49169712012-07-12 19:19:55 +0000177static void run_unflatten_benchmark(SkPicture* commands, const Options& options) {
178 BenchTimer timer = BenchTimer(NULL);
179 double wall_time = 0;
180
181 for (int i = 0; i < options.fRepeats + 1; ++i) {
182 SkPicture replayer;
183 SkCanvas* recorder = replayer.beginRecording(commands->width(), commands->height());
184
185 recorder->drawPicture(*commands);
186
187 timer.start();
188 replayer.endRecording();
189 timer.end();
190
191 // We want to ignore first time effects
192 if (i > 0) {
193 wall_time += timer.fWall;
194 }
195 }
196
keyar@chromium.orgb5e30ab2012-07-12 21:16:49 +0000197 printf("unflatten: msecs = %6.4f\n", wall_time / options.fRepeats);
keyar@chromium.org49169712012-07-12 19:19:55 +0000198}
199
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000200static void run_single_benchmark(const SkString& inputPath,
keyar@chromium.org0665f252012-07-10 18:30:18 +0000201 Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000202 SkFILEStream inputStream;
203
reed@google.com006db0f2012-06-27 19:33:29 +0000204 inputStream.setPath(inputPath.c_str());
205 if (!inputStream.isValid()) {
206 SkDebugf("Could not open file %s\n", inputPath.c_str());
207 return;
208 }
209
210 SkPicture picture(&inputStream);
reed@google.com006db0f2012-06-27 19:33:29 +0000211
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000212 SkString filename;
213 sk_tools::get_basename(&filename, inputPath);
reed@google.com006db0f2012-06-27 19:33:29 +0000214 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000215 filename.c_str());
216
keyar@chromium.org0665f252012-07-10 18:30:18 +0000217 if (options->fTileWidthPercentage > 0) {
218 options->fTileWidth = sk_float_ceil2int(options->fTileWidthPercentage * picture.width()
219 / 100);
220 }
221 if (options->fTileHeightPercentage > 0) {
222 options->fTileHeight = sk_float_ceil2int(options->fTileHeightPercentage * picture.height()
223 / 100);
224 }
225
keyar@chromium.orga4091ba2012-07-10 19:53:59 +0000226 options->fBenchmark(&picture, *options);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000227}
228
229static bool is_percentage(char* const string) {
230 SkString skString(string);
231 return skString.endsWith("%");
reed@google.com006db0f2012-06-27 19:33:29 +0000232}
233
234static void parse_commandline(int argc, char* const argv[],
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000235 SkTArray<SkString>* inputs, Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000236 const char* argv0 = argv[0];
237 char* const* stop = argv + argc;
238
239 for (++argv; argv < stop; ++argv) {
240 if (0 == strcmp(*argv, "--repeat")) {
241 ++argv;
242 if (argv < stop) {
243 options->fRepeats = atoi(*argv);
244 if (options->fRepeats < 1) {
245 SkDebugf("--repeat must be given a value > 0\n");
246 exit(-1);
247 }
248 } else {
249 SkDebugf("Missing arg for --repeat\n");
250 usage(argv0);
251 exit(-1);
252 }
253 } else if (0 == strcmp(*argv, "--tile")) {
254 options->fBenchmark = run_tile_benchmark;
255 ++argv;
256 if (argv < stop) {
keyar@chromium.org0665f252012-07-10 18:30:18 +0000257 if (is_percentage(*argv)) {
258 options->fTileWidthPercentage = atof(*argv);
259 if (!(options->fTileWidthPercentage > 0)) {
260 SkDebugf("--tile must be given a width percentage > 0\n");
261 exit(-1);
262 }
263 } else {
264 options->fTileWidth = atoi(*argv);
265 if (!(options->fTileWidth > 0)) {
266 SkDebugf("--tile must be given a width > 0\n");
267 exit(-1);
268 }
reed@google.com006db0f2012-06-27 19:33:29 +0000269 }
270 } else {
271 SkDebugf("Missing width for --tile\n");
272 usage(argv0);
273 exit(-1);
274 }
275 ++argv;
276 if (argv < stop) {
keyar@chromium.org0665f252012-07-10 18:30:18 +0000277 if (is_percentage(*argv)) {
278 options->fTileHeightPercentage = atof(*argv);
279 if (!(options->fTileHeightPercentage > 0)) {
280 SkDebugf(
281 "--tile must be given a height percentage > 0\n");
282 exit(-1);
283 }
284 } else {
285 options->fTileHeight = atoi(*argv);
286 if (!(options->fTileHeight > 0)) {
287 SkDebugf("--tile must be given a height > 0\n");
288 exit(-1);
289 }
reed@google.com006db0f2012-06-27 19:33:29 +0000290 }
291 } else {
292 SkDebugf("Missing height for --tile\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000293 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000294 exit(-1);
295 }
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000296 } else if (0 == strcmp(*argv, "--pipe")) {
297 options->fBenchmark = run_pipe_benchmark;
keyar@chromium.org49169712012-07-12 19:19:55 +0000298 } else if (0 == strcmp(*argv, "--unflatten")) {
299 options->fBenchmark = run_unflatten_benchmark;
reed@google.com006db0f2012-06-27 19:33:29 +0000300 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
301 usage(argv0);
302 exit(0);
303 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000304 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000305 }
306 }
307
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000308 if (inputs->count() < 1) {
reed@google.com006db0f2012-06-27 19:33:29 +0000309 usage(argv0);
310 exit(-1);
311 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000312}
reed@google.com006db0f2012-06-27 19:33:29 +0000313
keyar@chromium.org0665f252012-07-10 18:30:18 +0000314static void process_input(const SkString& input, Options* options) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000315 SkOSFile::Iter iter(input.c_str(), "skp");
316 SkString inputFilename;
317
318 if (iter.next(&inputFilename)) {
319 do {
320 SkString inputPath;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000321 sk_tools::make_filepath(&inputPath, input,
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000322 inputFilename);
323 run_single_benchmark(inputPath, options);
324 } while(iter.next(&inputFilename));
325 } else {
326 run_single_benchmark(input, options);
327 }
reed@google.com006db0f2012-06-27 19:33:29 +0000328}
329
330int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000331 SkTArray<SkString> inputs;
reed@google.com006db0f2012-06-27 19:33:29 +0000332 Options options;
333
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000334 parse_commandline(argc, argv, &inputs, &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000335
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000336 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org0665f252012-07-10 18:30:18 +0000337 process_input(inputs[i], &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000338 }
339}