| 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; |
| 24 | static void run_simple_benchmark(SkPicture* picture, const SkBitmap&, |
| 25 | const Options&); |
| 26 | |
| 27 | struct Options { |
| 28 | int fRepeats; |
| 29 | void (*fBenchmark) (SkPicture*, const SkBitmap& bitmap, |
| 30 | const Options& options); |
| 31 | int fTileWidth; |
| 32 | int fTileHeight; |
| 33 | |
| 34 | Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark), |
| 35 | fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT){} |
| 36 | }; |
| 37 | |
| 38 | static void usage(const char* argv0) { |
| 39 | SkDebugf("SkPicture benchmarking tool\n"); |
| 40 | SkDebugf("\n" |
| 41 | "Usage: \n" |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 42 | " %s <inputDir>...\n" |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 43 | " [--repeat] [--tile width height]" |
| 44 | , argv0); |
| 45 | SkDebugf("\n\n"); |
| 46 | SkDebugf( |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 47 | " inputDir: A list of directories and files to use as input.\n" |
| 48 | " Files are expected to have the .skp extension.\n\n"); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 49 | SkDebugf( |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 50 | " --pipe : " |
| 51 | "Set to use piping." |
| 52 | " Default is to not use piping.\n"); |
| 53 | SkDebugf( |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 54 | " --repeat : " |
| 55 | "Set the number of times to repeat each test." |
| 56 | " Default is %i.\n", DEFAULT_REPEATS); |
| 57 | SkDebugf( |
| 58 | " --tile width height: " |
| 59 | "Set to use the tiling size and specify the dimensions of each tile." |
| 60 | " Default is to not use tiling\n"); |
| 61 | } |
| 62 | |
| 63 | static void run_simple_benchmark(SkPicture* picture, |
| 64 | const SkBitmap& bitmap, |
| 65 | const Options& options) { |
| 66 | SkCanvas canvas(bitmap); |
| 67 | |
| 68 | // We throw this away to remove first time effects (such as paging in this |
| 69 | // program) |
| 70 | canvas.drawPicture(*picture); |
| 71 | |
| 72 | BenchTimer timer = BenchTimer(NULL); |
| 73 | timer.start(); |
| 74 | for (int i = 0; i < options.fRepeats; ++i) { |
| 75 | canvas.drawPicture(*picture); |
| 76 | } |
| 77 | timer.end(); |
| 78 | |
| 79 | printf("simple: cmsecs = %6.2f\n", timer.fWall / options.fRepeats); |
| 80 | } |
| 81 | |
| 82 | struct TileInfo { |
| 83 | SkBitmap* fBitmap; |
| 84 | SkCanvas* fCanvas; |
| 85 | }; |
| 86 | |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 87 | static void clip_tile(SkPicture* picture, const TileInfo& tile) { |
| 88 | SkRect clip = SkRect::MakeWH(picture->width(), picture->height()); |
| 89 | tile.fCanvas->clipRect(clip); |
| 90 | } |
| 91 | |
| 92 | static void setup_single_tile(SkPicture* picture, const SkBitmap& bitmap, |
| 93 | const Options& options, SkTArray<TileInfo>* tiles, |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 94 | int tile_x_start, int tile_y_start) { |
| 95 | TileInfo& tile = tiles->push_back(); |
| 96 | tile.fBitmap = new SkBitmap(); |
| 97 | SkIRect rect = SkIRect::MakeXYWH(tile_x_start, tile_y_start, |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 98 | options.fTileWidth, options.fTileHeight); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 99 | bitmap.extractSubset(tile.fBitmap, rect); |
| 100 | tile.fCanvas = new SkCanvas(*(tile.fBitmap)); |
| 101 | tile.fCanvas->translate(-tile_x_start, -tile_y_start); |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 102 | |
| 103 | clip_tile(picture, tile); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void setup_tiles(SkPicture* picture, const SkBitmap& bitmap, |
| 107 | const Options& options, SkTArray<TileInfo>* tiles) { |
| 108 | for (int tile_y_start = 0; tile_y_start < picture->height(); |
| 109 | tile_y_start += options.fTileHeight) { |
| 110 | for (int tile_x_start = 0; tile_x_start < picture->width(); |
| 111 | tile_x_start += options.fTileWidth) { |
| keyar@chromium.org | 70b4222 | 2012-07-09 19:51:05 +0000 | [diff] [blame] | 112 | setup_single_tile(picture, bitmap, options, tiles, tile_x_start, |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 113 | tile_y_start); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | static void run_tile_benchmark(SkPicture* picture, const SkBitmap& bitmap, |
| 120 | const Options& options) { |
| 121 | SkTArray<TileInfo> tiles; |
| 122 | setup_tiles(picture, bitmap, options, &tiles); |
| 123 | |
| 124 | // We throw this away to remove first time effects (such as paging in this |
| 125 | // program) |
| 126 | for (int j = 0; j < tiles.count(); ++j) { |
| 127 | tiles[j].fCanvas->drawPicture(*picture); |
| 128 | } |
| 129 | |
| 130 | BenchTimer timer = BenchTimer(NULL); |
| 131 | timer.start(); |
| 132 | for (int i = 0; i < options.fRepeats; ++i) { |
| 133 | for (int j = 0; j < tiles.count(); ++j) { |
| 134 | tiles[j].fCanvas->drawPicture(*picture); |
| 135 | } |
| 136 | } |
| 137 | timer.end(); |
| 138 | |
| 139 | for (int i = 0; i < tiles.count(); ++i) { |
| 140 | delete tiles[i].fCanvas; |
| 141 | delete tiles[i].fBitmap; |
| 142 | } |
| 143 | |
| keyar@chromium.org | 2724ae0 | 2012-07-10 15:13:21 +0000 | [diff] [blame^] | 144 | 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] | 145 | options.fTileHeight, timer.fWall / options.fRepeats); |
| 146 | } |
| 147 | |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 148 | static void pipe_run(SkPicture* picture, SkCanvas* canvas) { |
| 149 | PipeController pipeController(canvas); |
| 150 | SkGPipeWriter writer; |
| 151 | SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
| 152 | pipeCanvas->drawPicture(*picture); |
| 153 | writer.endRecording(); |
| 154 | } |
| 155 | |
| 156 | static void run_pipe_benchmark(SkPicture* picture, const SkBitmap& bitmap, |
| 157 | const Options& options) { |
| 158 | SkCanvas canvas(bitmap); |
| 159 | |
| 160 | // We throw this away to remove first time effects (such as paging in this |
| 161 | // program) |
| 162 | pipe_run(picture, &canvas); |
| 163 | |
| 164 | BenchTimer timer = BenchTimer(NULL); |
| 165 | timer.start(); |
| 166 | for (int i = 0; i < options.fRepeats; ++i) { |
| 167 | pipe_run(picture, &canvas); |
| 168 | } |
| 169 | timer.end(); |
| 170 | |
| 171 | printf("pipe: cmsecs = %6.2f\n", timer.fWall / options.fRepeats); |
| 172 | } |
| 173 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 174 | static void run_single_benchmark(const SkString& inputPath, |
| 175 | const Options& options) { |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 176 | SkFILEStream inputStream; |
| 177 | |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 178 | inputStream.setPath(inputPath.c_str()); |
| 179 | if (!inputStream.isValid()) { |
| 180 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | SkPicture picture(&inputStream); |
| 185 | SkBitmap bitmap; |
| 186 | sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height()); |
| 187 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 188 | SkString filename; |
| 189 | sk_tools::get_basename(&filename, inputPath); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 190 | printf("running bench [%i %i] %s ", picture.width(), picture.height(), |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 191 | filename.c_str()); |
| 192 | |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 193 | options.fBenchmark(&picture, bitmap, options); |
| 194 | } |
| 195 | |
| 196 | static void parse_commandline(int argc, char* const argv[], |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 197 | SkTArray<SkString>* inputs, Options* options) { |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 198 | const char* argv0 = argv[0]; |
| 199 | char* const* stop = argv + argc; |
| 200 | |
| 201 | for (++argv; argv < stop; ++argv) { |
| 202 | if (0 == strcmp(*argv, "--repeat")) { |
| 203 | ++argv; |
| 204 | if (argv < stop) { |
| 205 | options->fRepeats = atoi(*argv); |
| 206 | if (options->fRepeats < 1) { |
| 207 | SkDebugf("--repeat must be given a value > 0\n"); |
| 208 | exit(-1); |
| 209 | } |
| 210 | } else { |
| 211 | SkDebugf("Missing arg for --repeat\n"); |
| 212 | usage(argv0); |
| 213 | exit(-1); |
| 214 | } |
| 215 | } else if (0 == strcmp(*argv, "--tile")) { |
| 216 | options->fBenchmark = run_tile_benchmark; |
| 217 | ++argv; |
| 218 | if (argv < stop) { |
| 219 | options->fTileWidth = atoi(*argv); |
| 220 | if (options->fTileWidth < 1) { |
| 221 | SkDebugf("--tile must be given a width with a value > 0\n"); |
| 222 | exit(-1); |
| 223 | } |
| 224 | } else { |
| 225 | SkDebugf("Missing width for --tile\n"); |
| 226 | usage(argv0); |
| 227 | exit(-1); |
| 228 | } |
| 229 | ++argv; |
| 230 | if (argv < stop) { |
| 231 | options->fTileHeight = atoi(*argv); |
| 232 | if (options->fTileHeight < 1) { |
| 233 | SkDebugf("--tile must be given a height with a value > 0" |
| 234 | "\n"); |
| 235 | exit(-1); |
| 236 | } |
| 237 | } else { |
| 238 | SkDebugf("Missing height for --tile\n"); |
| 239 | usage(argv0);\ |
| 240 | exit(-1); |
| 241 | } |
| keyar@chromium.org | cf6c44c | 2012-07-09 19:37:40 +0000 | [diff] [blame] | 242 | } else if (0 == strcmp(*argv, "--pipe")) { |
| 243 | options->fBenchmark = run_pipe_benchmark; |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 244 | } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) { |
| 245 | usage(argv0); |
| 246 | exit(0); |
| 247 | } else { |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 248 | inputs->push_back(SkString(*argv)); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 252 | if (inputs->count() < 1) { |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 253 | usage(argv0); |
| 254 | exit(-1); |
| 255 | } |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 256 | } |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 257 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 258 | static void process_input(const SkString& input, const Options& options) { |
| 259 | SkOSFile::Iter iter(input.c_str(), "skp"); |
| 260 | SkString inputFilename; |
| 261 | |
| 262 | if (iter.next(&inputFilename)) { |
| 263 | do { |
| 264 | SkString inputPath; |
| 265 | sk_tools::make_filepath(&inputPath, input.c_str(), |
| 266 | inputFilename); |
| 267 | run_single_benchmark(inputPath, options); |
| 268 | } while(iter.next(&inputFilename)); |
| 269 | } else { |
| 270 | run_single_benchmark(input, options); |
| 271 | } |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int main(int argc, char* const argv[]) { |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 275 | SkTArray<SkString> inputs; |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 276 | Options options; |
| 277 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 278 | parse_commandline(argc, argv, &inputs, &options); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 279 | |
| keyar@chromium.org | d1dc920 | 2012-07-09 18:32:08 +0000 | [diff] [blame] | 280 | for (int i = 0; i < inputs.count(); ++i) { |
| 281 | process_input(inputs[i], options); |
| reed@google.com | 006db0f | 2012-06-27 19:33:29 +0000 | [diff] [blame] | 282 | } |
| 283 | } |