2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "BenchTimer.h"
12 #include "SkPicture.h"
15 #include "picture_utils.h"
17 const int DEFAULT_REPEATS = 100;
18 const int DEFAULT_TILE_WIDTH = 256;
19 const int DEFAULT_TILE_HEIGHT = 256;
22 static void run_simple_benchmark(SkPicture* picture, const SkBitmap&,
27 void (*fBenchmark) (SkPicture*, const SkBitmap& bitmap,
28 const Options& options);
32 Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
33 fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT){}
36 static void usage(const char* argv0) {
37 SkDebugf("SkPicture benchmarking tool\n");
41 " [--repeat] [--tile width height]"
45 " inputDir: A list of directories and files to use as input.\n"
46 " Files are expected to have the .skp extension.\n\n");
49 "Set the number of times to repeat each test."
50 " Default is %i.\n", DEFAULT_REPEATS);
52 " --tile width height: "
53 "Set to use the tiling size and specify the dimensions of each tile."
54 " Default is to not use tiling\n");
57 static void run_simple_benchmark(SkPicture* picture,
58 const SkBitmap& bitmap,
59 const Options& options) {
60 SkCanvas canvas(bitmap);
62 // We throw this away to remove first time effects (such as paging in this
64 canvas.drawPicture(*picture);
66 BenchTimer timer = BenchTimer(NULL);
68 for (int i = 0; i < options.fRepeats; ++i) {
69 canvas.drawPicture(*picture);
73 printf("simple: cmsecs = %6.2f\n", timer.fWall / options.fRepeats);
81 static void setup_single_tile(const SkBitmap& bitmap, const Options& options,
82 SkTArray<TileInfo>* tiles,
83 int tile_x_start, int tile_y_start) {
84 TileInfo& tile = tiles->push_back();
85 tile.fBitmap = new SkBitmap();
86 SkIRect rect = SkIRect::MakeXYWH(tile_x_start, tile_y_start,
89 bitmap.extractSubset(tile.fBitmap, rect);
90 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
91 tile.fCanvas->translate(-tile_x_start, -tile_y_start);
94 static void setup_tiles(SkPicture* picture, const SkBitmap& bitmap,
95 const Options& options, SkTArray<TileInfo>* tiles) {
96 for (int tile_y_start = 0; tile_y_start < picture->height();
97 tile_y_start += options.fTileHeight) {
98 for (int tile_x_start = 0; tile_x_start < picture->width();
99 tile_x_start += options.fTileWidth) {
100 setup_single_tile(bitmap, options, tiles, tile_x_start,
107 static void run_tile_benchmark(SkPicture* picture, const SkBitmap& bitmap,
108 const Options& options) {
109 SkTArray<TileInfo> tiles;
110 setup_tiles(picture, bitmap, options, &tiles);
112 // We throw this away to remove first time effects (such as paging in this
114 for (int j = 0; j < tiles.count(); ++j) {
115 tiles[j].fCanvas->drawPicture(*picture);
118 BenchTimer timer = BenchTimer(NULL);
120 for (int i = 0; i < options.fRepeats; ++i) {
121 for (int j = 0; j < tiles.count(); ++j) {
122 tiles[j].fCanvas->drawPicture(*picture);
127 for (int i = 0; i < tiles.count(); ++i) {
128 delete tiles[i].fCanvas;
129 delete tiles[i].fBitmap;
132 printf("tile%ix%i: cmsecs = %6.2f\n", options.fTileWidth,
133 options.fTileHeight, timer.fWall / options.fRepeats);
136 static void run_single_benchmark(const SkString& inputPath,
137 const Options& options) {
138 SkFILEStream inputStream;
140 inputStream.setPath(inputPath.c_str());
141 if (!inputStream.isValid()) {
142 SkDebugf("Could not open file %s\n", inputPath.c_str());
146 SkPicture picture(&inputStream);
148 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
151 sk_tools::get_basename(&filename, inputPath);
152 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
155 options.fBenchmark(&picture, bitmap, options);
158 static void parse_commandline(int argc, char* const argv[],
159 SkTArray<SkString>* inputs, Options* options) {
160 const char* argv0 = argv[0];
161 char* const* stop = argv + argc;
163 for (++argv; argv < stop; ++argv) {
164 if (0 == strcmp(*argv, "--repeat")) {
167 options->fRepeats = atoi(*argv);
168 if (options->fRepeats < 1) {
169 SkDebugf("--repeat must be given a value > 0\n");
173 SkDebugf("Missing arg for --repeat\n");
177 } else if (0 == strcmp(*argv, "--tile")) {
178 options->fBenchmark = run_tile_benchmark;
181 options->fTileWidth = atoi(*argv);
182 if (options->fTileWidth < 1) {
183 SkDebugf("--tile must be given a width with a value > 0\n");
187 SkDebugf("Missing width for --tile\n");
193 options->fTileHeight = atoi(*argv);
194 if (options->fTileHeight < 1) {
195 SkDebugf("--tile must be given a height with a value > 0"
200 SkDebugf("Missing height for --tile\n");
204 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
208 inputs->push_back(SkString(*argv));
212 if (inputs->count() < 1) {
218 static void process_input(const SkString& input, const Options& options) {
219 SkOSFile::Iter iter(input.c_str(), "skp");
220 SkString inputFilename;
222 if (iter.next(&inputFilename)) {
225 sk_tools::make_filepath(&inputPath, input.c_str(),
227 run_single_benchmark(inputPath, options);
228 } while(iter.next(&inputFilename));
230 run_single_benchmark(input, options);
234 int main(int argc, char* const argv[]) {
235 SkTArray<SkString> inputs;
238 parse_commandline(argc, argv, &inputs, &options);
240 for (int i = 0; i < inputs.count(); ++i) {
241 process_input(inputs[i], options);