blob: e06a63bdb18e98ee1da7127481872dbb5d56cf17 [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"
9#include "SkBitmap.h"
10#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;
18const int DEFAULT_TILE_WIDTH = 256;
19const int DEFAULT_TILE_HEIGHT = 256;
20
21struct Options;
22static void run_simple_benchmark(SkPicture* picture, const SkBitmap&,
23 const Options&);
24
25struct Options {
26 int fRepeats;
27 void (*fBenchmark) (SkPicture*, const SkBitmap& bitmap,
28 const Options& options);
29 int fTileWidth;
30 int fTileHeight;
31
32 Options() : fRepeats(DEFAULT_REPEATS), fBenchmark(run_simple_benchmark),
33 fTileWidth(DEFAULT_TILE_WIDTH), fTileHeight(DEFAULT_TILE_HEIGHT){}
34};
35
36static void usage(const char* argv0) {
37 SkDebugf("SkPicture benchmarking tool\n");
38 SkDebugf("\n"
39"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000040" %s <inputDir>...\n"
reed@google.com006db0f2012-06-27 19:33:29 +000041" [--repeat] [--tile width height]"
42, argv0);
43 SkDebugf("\n\n");
44 SkDebugf(
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000045" inputDir: A list of directories and files to use as input.\n"
46" Files are expected to have the .skp extension.\n\n");
reed@google.com006db0f2012-06-27 19:33:29 +000047 SkDebugf(
48" --repeat : "
49"Set the number of times to repeat each test."
50" Default is %i.\n", DEFAULT_REPEATS);
51 SkDebugf(
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");
55}
56
57static void run_simple_benchmark(SkPicture* picture,
58 const SkBitmap& bitmap,
59 const Options& options) {
60 SkCanvas canvas(bitmap);
61
62 // We throw this away to remove first time effects (such as paging in this
63 // program)
64 canvas.drawPicture(*picture);
65
66 BenchTimer timer = BenchTimer(NULL);
67 timer.start();
68 for (int i = 0; i < options.fRepeats; ++i) {
69 canvas.drawPicture(*picture);
70 }
71 timer.end();
72
73 printf("simple: cmsecs = %6.2f\n", timer.fWall / options.fRepeats);
74}
75
76struct TileInfo {
77 SkBitmap* fBitmap;
78 SkCanvas* fCanvas;
79};
80
81static 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,
87 options.fTileWidth,
88 options.fTileHeight);
89 bitmap.extractSubset(tile.fBitmap, rect);
90 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
91 tile.fCanvas->translate(-tile_x_start, -tile_y_start);
92}
93
94static 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,
101 tile_y_start);
102 }
103 }
104
105}
106
107static void run_tile_benchmark(SkPicture* picture, const SkBitmap& bitmap,
108 const Options& options) {
109 SkTArray<TileInfo> tiles;
110 setup_tiles(picture, bitmap, options, &tiles);
111
112 // We throw this away to remove first time effects (such as paging in this
113 // program)
114 for (int j = 0; j < tiles.count(); ++j) {
115 tiles[j].fCanvas->drawPicture(*picture);
116 }
117
118 BenchTimer timer = BenchTimer(NULL);
119 timer.start();
120 for (int i = 0; i < options.fRepeats; ++i) {
121 for (int j = 0; j < tiles.count(); ++j) {
122 tiles[j].fCanvas->drawPicture(*picture);
123 }
124 }
125 timer.end();
126
127 for (int i = 0; i < tiles.count(); ++i) {
128 delete tiles[i].fCanvas;
129 delete tiles[i].fBitmap;
130 }
131
132 printf("tile%ix%i: cmsecs = %6.2f\n", options.fTileWidth,
133 options.fTileHeight, timer.fWall / options.fRepeats);
134}
135
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000136static void run_single_benchmark(const SkString& inputPath,
137 const Options& options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000138 SkFILEStream inputStream;
139
reed@google.com006db0f2012-06-27 19:33:29 +0000140 inputStream.setPath(inputPath.c_str());
141 if (!inputStream.isValid()) {
142 SkDebugf("Could not open file %s\n", inputPath.c_str());
143 return;
144 }
145
146 SkPicture picture(&inputStream);
147 SkBitmap bitmap;
148 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
149
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000150 SkString filename;
151 sk_tools::get_basename(&filename, inputPath);
reed@google.com006db0f2012-06-27 19:33:29 +0000152 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000153 filename.c_str());
154
reed@google.com006db0f2012-06-27 19:33:29 +0000155 options.fBenchmark(&picture, bitmap, options);
156}
157
158static void parse_commandline(int argc, char* const argv[],
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000159 SkTArray<SkString>* inputs, Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000160 const char* argv0 = argv[0];
161 char* const* stop = argv + argc;
162
163 for (++argv; argv < stop; ++argv) {
164 if (0 == strcmp(*argv, "--repeat")) {
165 ++argv;
166 if (argv < stop) {
167 options->fRepeats = atoi(*argv);
168 if (options->fRepeats < 1) {
169 SkDebugf("--repeat must be given a value > 0\n");
170 exit(-1);
171 }
172 } else {
173 SkDebugf("Missing arg for --repeat\n");
174 usage(argv0);
175 exit(-1);
176 }
177 } else if (0 == strcmp(*argv, "--tile")) {
178 options->fBenchmark = run_tile_benchmark;
179 ++argv;
180 if (argv < stop) {
181 options->fTileWidth = atoi(*argv);
182 if (options->fTileWidth < 1) {
183 SkDebugf("--tile must be given a width with a value > 0\n");
184 exit(-1);
185 }
186 } else {
187 SkDebugf("Missing width for --tile\n");
188 usage(argv0);
189 exit(-1);
190 }
191 ++argv;
192 if (argv < stop) {
193 options->fTileHeight = atoi(*argv);
194 if (options->fTileHeight < 1) {
195 SkDebugf("--tile must be given a height with a value > 0"
196 "\n");
197 exit(-1);
198 }
199 } else {
200 SkDebugf("Missing height for --tile\n");
201 usage(argv0);\
202 exit(-1);
203 }
204 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
205 usage(argv0);
206 exit(0);
207 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000208 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000209 }
210 }
211
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000212 if (inputs->count() < 1) {
reed@google.com006db0f2012-06-27 19:33:29 +0000213 usage(argv0);
214 exit(-1);
215 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000216}
reed@google.com006db0f2012-06-27 19:33:29 +0000217
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000218static void process_input(const SkString& input, const Options& options) {
219 SkOSFile::Iter iter(input.c_str(), "skp");
220 SkString inputFilename;
221
222 if (iter.next(&inputFilename)) {
223 do {
224 SkString inputPath;
225 sk_tools::make_filepath(&inputPath, input.c_str(),
226 inputFilename);
227 run_single_benchmark(inputPath, options);
228 } while(iter.next(&inputFilename));
229 } else {
230 run_single_benchmark(input, options);
231 }
reed@google.com006db0f2012-06-27 19:33:29 +0000232}
233
234int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000235 SkTArray<SkString> inputs;
reed@google.com006db0f2012-06-27 19:33:29 +0000236 Options options;
237
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000238 parse_commandline(argc, argv, &inputs, &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000239
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000240 for (int i = 0; i < inputs.count(); ++i) {
241 process_input(inputs[i], options);
reed@google.com006db0f2012-06-27 19:33:29 +0000242 }
243}