| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 1 | /* |
| 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.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 9 | #include "SamplePipeControllers.h" |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 12 | #include "SkGPipe.h" |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| 14 | #include "SkPicture.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkTArray.h" |
| 17 | #include "picture_utils.h" |
| 18 | |
| 19 | const int DEFAULT_REPEATS = 100; |
| 20 | const int DEFAULT_TILE_WIDTH = 256; |
| 21 | const int DEFAULT_TILE_HEIGHT = 256; |
| 22 | |
| 23 | struct Options; |
| keyar@chromium.org | a4091ba | 2012-07-10 19:53:59 +0000 | [diff] [blame] | 24 | static void run_simple_benchmark(SkPicture* picture, const Options&); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 25 | |
| 26 | struct Options { |
| 27 | int fRepeats; |
| keyar@chromium.org | a4091ba | 2012-07-10 19:53:59 +0000 | [diff] [blame] | 28 | void (*fBenchmark) (SkPicture*, const Options& options); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 29 | int fTileWidth; |
| 30 | int fTileHeight; |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 31 | double fTileWidthPercentage; |
| 32 | double fTileHeightPercentage; |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 33 | |
| 34 | Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark), |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 35 | fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT), |
| 36 | fTileWidthPercentage(0), fTileHeightPercentage(0){} |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static void usage(const char* argv0) { |
| 40 | SkDebugf("SkPicture benchmarking tool\n"); |
| 41 | SkDebugf("\n" |
| 42 | "Usage: \n" |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 43 | " %s <inputDir>...\n" |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 44 | " [--repeat] [--tile width height]" |
| 45 | , argv0); |
| 46 | SkDebugf("\n\n"); |
| 47 | SkDebugf( |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 48 | " 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.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 50 | SkDebugf( |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 51 | " --pipe : " |
| 52 | "Set to use piping." |
| 53 | " Default is to not use piping.\n"); |
| 54 | SkDebugf( |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 55 | " --repeat : " |
| 56 | "Set the number of times to repeat each test." |
| 57 | " Default is %i.\n", DEFAULT_REPEATS); |
| 58 | SkDebugf( |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 59 | " --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.org | 4916971 | 2012-07-12 19:19:55 +0000 | [diff] [blame^] | 62 | SkDebugf( |
| 63 | " --unflatten: " |
| 64 | "Set to do a picture unflattening benchmark. Default is not to do this.\n"); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| keyar@chromium.org | a4091ba | 2012-07-10 19:53:59 +0000 | [diff] [blame] | 67 | static 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.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 71 | 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 | |
| 84 | printf("simple: cmsecs = %6.2f\n", timer.fWall / options.fRepeats); |
| 85 | } |
| 86 | |
| 87 | struct TileInfo { |
| 88 | SkBitmap* fBitmap; |
| 89 | SkCanvas* fCanvas; |
| 90 | }; |
| 91 | |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 92 | static void clip_tile(SkPicture* picture, const TileInfo& tile) { |
| robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 93 | SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()), |
| 94 | SkIntToScalar(picture->height())); |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 95 | tile.fCanvas->clipRect(clip); |
| 96 | } |
| 97 | |
| 98 | static void setup_single_tile(SkPicture* picture, const SkBitmap& bitmap, |
| 99 | const Options& options, SkTArray<TileInfo>* tiles, |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 100 | int tile_x_start, int tile_y_start) { |
| 101 | TileInfo& tile = tiles->push_back(); |
| 102 | tile.fBitmap = new SkBitmap(); |
| 103 | SkIRect rect = SkIRect::MakeXYWH(tile_x_start, tile_y_start, |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 104 | options.fTileWidth, options.fTileHeight); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 105 | bitmap.extractSubset(tile.fBitmap, rect); |
| 106 | tile.fCanvas = new SkCanvas(*(tile.fBitmap)); |
| robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 107 | tile.fCanvas->translate(SkIntToScalar(-tile_x_start), |
| 108 | SkIntToScalar(-tile_y_start)); |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 109 | |
| 110 | clip_tile(picture, tile); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void setup_tiles(SkPicture* picture, const SkBitmap& bitmap, |
| 114 | const Options& options, SkTArray<TileInfo>* tiles) { |
| 115 | for (int tile_y_start = 0; tile_y_start < picture->height(); |
| 116 | tile_y_start += options.fTileHeight) { |
| 117 | for (int tile_x_start = 0; tile_x_start < picture->width(); |
| 118 | tile_x_start += options.fTileWidth) { |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 119 | setup_single_tile(picture, bitmap, options, tiles, tile_x_start, |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 120 | tile_y_start); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
| keyar@chromium.org | a4091ba | 2012-07-10 19:53:59 +0000 | [diff] [blame] | 126 | static void run_tile_benchmark(SkPicture* picture, const Options& options) { |
| 127 | SkBitmap bitmap; |
| 128 | sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height()); |
| 129 | |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 130 | SkTArray<TileInfo> tiles; |
| 131 | setup_tiles(picture, bitmap, options, &tiles); |
| 132 | |
| 133 | // We throw this away to remove first time effects (such as paging in this |
| 134 | // program) |
| 135 | for (int j = 0; j < tiles.count(); ++j) { |
| 136 | tiles[j].fCanvas->drawPicture(*picture); |
| 137 | } |
| 138 | |
| 139 | BenchTimer timer = BenchTimer(NULL); |
| 140 | timer.start(); |
| 141 | for (int i = 0; i < options.fRepeats; ++i) { |
| 142 | for (int j = 0; j < tiles.count(); ++j) { |
| 143 | tiles[j].fCanvas->drawPicture(*picture); |
| 144 | } |
| 145 | } |
| 146 | timer.end(); |
| 147 | |
| 148 | for (int i = 0; i < tiles.count(); ++i) { |
| 149 | delete tiles[i].fCanvas; |
| 150 | delete tiles[i].fBitmap; |
| 151 | } |
| 152 | |
| keyar@chromium.org | 2724ae0 | 2012-07-10 15:13:21 +0000 | [diff] [blame] | 153 | printf("%i_tiles_%ix%i: cmsecs = %6.2f\n", tiles.count(), options.fTileWidth, |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 154 | options.fTileHeight, timer.fWall / options.fRepeats); |
| 155 | } |
| 156 | |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 157 | static void pipe_run(SkPicture* picture, SkCanvas* canvas) { |
| 158 | PipeController pipeController(canvas); |
| 159 | SkGPipeWriter writer; |
| 160 | SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
| 161 | pipeCanvas->drawPicture(*picture); |
| 162 | writer.endRecording(); |
| 163 | } |
| 164 | |
| keyar@chromium.org | a4091ba | 2012-07-10 19:53:59 +0000 | [diff] [blame] | 165 | static void run_pipe_benchmark(SkPicture* picture, const Options& options) { |
| 166 | SkBitmap bitmap; |
| 167 | sk_tools::setup_bitmap(&bitmap, picture->width(), picture->height()); |
| 168 | |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 169 | SkCanvas canvas(bitmap); |
| 170 | |
| 171 | // We throw this away to remove first time effects (such as paging in this |
| 172 | // program) |
| 173 | pipe_run(picture, &canvas); |
| 174 | |
| 175 | BenchTimer timer = BenchTimer(NULL); |
| 176 | timer.start(); |
| 177 | for (int i = 0; i < options.fRepeats; ++i) { |
| 178 | pipe_run(picture, &canvas); |
| 179 | } |
| 180 | timer.end(); |
| 181 | |
| 182 | printf("pipe: cmsecs = %6.2f\n", timer.fWall / options.fRepeats); |
| 183 | } |
| 184 | |
| keyar@chromium.org | 4916971 | 2012-07-12 19:19:55 +0000 | [diff] [blame^] | 185 | static void run_unflatten_benchmark(SkPicture* commands, const Options& options) { |
| 186 | BenchTimer timer = BenchTimer(NULL); |
| 187 | double wall_time = 0; |
| 188 | |
| 189 | for (int i = 0; i < options.fRepeats + 1; ++i) { |
| 190 | SkPicture replayer; |
| 191 | SkCanvas* recorder = replayer.beginRecording(commands->width(), commands->height()); |
| 192 | |
| 193 | recorder->drawPicture(*commands); |
| 194 | |
| 195 | timer.start(); |
| 196 | replayer.endRecording(); |
| 197 | timer.end(); |
| 198 | |
| 199 | // We want to ignore first time effects |
| 200 | if (i > 0) { |
| 201 | wall_time += timer.fWall; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | printf("unflatten: cmsecs = %6.4f\n", wall_time / options.fRepeats); |
| 206 | } |
| 207 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 208 | static void run_single_benchmark(const SkString& inputPath, |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 209 | Options* options) { |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 210 | SkFILEStream inputStream; |
| 211 | |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 212 | inputStream.setPath(inputPath.c_str()); |
| 213 | if (!inputStream.isValid()) { |
| 214 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | SkPicture picture(&inputStream); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 219 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 220 | SkString filename; |
| 221 | sk_tools::get_basename(&filename, inputPath); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 222 | printf("running bench [%i %i] %s ", picture.width(), picture.height(), |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 223 | filename.c_str()); |
| 224 | |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 225 | if (options->fTileWidthPercentage > 0) { |
| 226 | options->fTileWidth = sk_float_ceil2int(options->fTileWidthPercentage * picture.width() |
| 227 | / 100); |
| 228 | } |
| 229 | if (options->fTileHeightPercentage > 0) { |
| 230 | options->fTileHeight = sk_float_ceil2int(options->fTileHeightPercentage * picture.height() |
| 231 | / 100); |
| 232 | } |
| 233 | |
| keyar@chromium.org | a4091ba | 2012-07-10 19:53:59 +0000 | [diff] [blame] | 234 | options->fBenchmark(&picture, *options); |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static bool is_percentage(char* const string) { |
| 238 | SkString skString(string); |
| 239 | return skString.endsWith("%"); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void parse_commandline(int argc, char* const argv[], |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 243 | SkTArray<SkString>* inputs, Options* options) { |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 244 | const char* argv0 = argv[0]; |
| 245 | char* const* stop = argv + argc; |
| 246 | |
| 247 | for (++argv; argv < stop; ++argv) { |
| 248 | if (0 == strcmp(*argv, "--repeat")) { |
| 249 | ++argv; |
| 250 | if (argv < stop) { |
| 251 | options->fRepeats = atoi(*argv); |
| 252 | if (options->fRepeats < 1) { |
| 253 | SkDebugf("--repeat must be given a value > 0\n"); |
| 254 | exit(-1); |
| 255 | } |
| 256 | } else { |
| 257 | SkDebugf("Missing arg for --repeat\n"); |
| 258 | usage(argv0); |
| 259 | exit(-1); |
| 260 | } |
| 261 | } else if (0 == strcmp(*argv, "--tile")) { |
| 262 | options->fBenchmark = run_tile_benchmark; |
| 263 | ++argv; |
| 264 | if (argv < stop) { |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 265 | if (is_percentage(*argv)) { |
| 266 | options->fTileWidthPercentage = atof(*argv); |
| 267 | if (!(options->fTileWidthPercentage > 0)) { |
| 268 | SkDebugf("--tile must be given a width percentage > 0\n"); |
| 269 | exit(-1); |
| 270 | } |
| 271 | } else { |
| 272 | options->fTileWidth = atoi(*argv); |
| 273 | if (!(options->fTileWidth > 0)) { |
| 274 | SkDebugf("--tile must be given a width > 0\n"); |
| 275 | exit(-1); |
| 276 | } |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 277 | } |
| 278 | } else { |
| 279 | SkDebugf("Missing width for --tile\n"); |
| 280 | usage(argv0); |
| 281 | exit(-1); |
| 282 | } |
| 283 | ++argv; |
| 284 | if (argv < stop) { |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 285 | if (is_percentage(*argv)) { |
| 286 | options->fTileHeightPercentage = atof(*argv); |
| 287 | if (!(options->fTileHeightPercentage > 0)) { |
| 288 | SkDebugf( |
| 289 | "--tile must be given a height percentage > 0\n"); |
| 290 | exit(-1); |
| 291 | } |
| 292 | } else { |
| 293 | options->fTileHeight = atoi(*argv); |
| 294 | if (!(options->fTileHeight > 0)) { |
| 295 | SkDebugf("--tile must be given a height > 0\n"); |
| 296 | exit(-1); |
| 297 | } |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 298 | } |
| 299 | } else { |
| 300 | SkDebugf("Missing height for --tile\n"); |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 301 | usage(argv0); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 302 | exit(-1); |
| 303 | } |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 304 | } else if (0 == strcmp(*argv, "--pipe")) { |
| 305 | options->fBenchmark = run_pipe_benchmark; |
| keyar@chromium.org | 4916971 | 2012-07-12 19:19:55 +0000 | [diff] [blame^] | 306 | } else if (0 == strcmp(*argv, "--unflatten")) { |
| 307 | options->fBenchmark = run_unflatten_benchmark; |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 308 | } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) { |
| 309 | usage(argv0); |
| 310 | exit(0); |
| 311 | } else { |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 312 | inputs->push_back(SkString(*argv)); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 316 | if (inputs->count() < 1) { |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 317 | usage(argv0); |
| 318 | exit(-1); |
| 319 | } |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 320 | } |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 321 | |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 322 | static void process_input(const SkString& input, Options* options) { |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 323 | SkOSFile::Iter iter(input.c_str(), "skp"); |
| 324 | SkString inputFilename; |
| 325 | |
| 326 | if (iter.next(&inputFilename)) { |
| 327 | do { |
| 328 | SkString inputPath; |
| 329 | sk_tools::make_filepath(&inputPath, input.c_str(), |
| 330 | inputFilename); |
| 331 | run_single_benchmark(inputPath, options); |
| 332 | } while(iter.next(&inputFilename)); |
| 333 | } else { |
| 334 | run_single_benchmark(input, options); |
| 335 | } |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int main(int argc, char* const argv[]) { |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 339 | SkTArray<SkString> inputs; |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 340 | Options options; |
| 341 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 342 | parse_commandline(argc, argv, &inputs, &options); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 343 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 344 | for (int i = 0; i < inputs.count(); ++i) { |
| keyar@chromium.org | 0665f25 | 2012-07-10 18:30:18 +0000 | [diff] [blame] | 345 | process_input(inputs[i], &options); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 346 | } |
| 347 | } |