blob: 1a1dad179c3a5f956bfa314bbc12bf924c7d85c8 [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
84 printf("simple: cmsecs = %6.2f\n", timer.fWall / options.fRepeats);
85}
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) {
robertphillips@google.com59f46b82012-07-10 17:30:58 +000093 SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()),
94 SkIntToScalar(picture->height()));
keyar@chromium.org70b42222012-07-09 19:51:05 +000095 tile.fCanvas->clipRect(clip);
96}
97
98static void setup_single_tile(SkPicture* picture, const SkBitmap& bitmap,
99 const Options& options, SkTArray<TileInfo>* tiles,
reed@google.com006db0f2012-06-27 19:33:29 +0000100 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.org70b42222012-07-09 19:51:05 +0000104 options.fTileWidth, options.fTileHeight);
reed@google.com006db0f2012-06-27 19:33:29 +0000105 bitmap.extractSubset(tile.fBitmap, rect);
106 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000107 tile.fCanvas->translate(SkIntToScalar(-tile_x_start),
108 SkIntToScalar(-tile_y_start));
keyar@chromium.org70b42222012-07-09 19:51:05 +0000109
110 clip_tile(picture, tile);
reed@google.com006db0f2012-06-27 19:33:29 +0000111}
112
113static 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.org70b42222012-07-09 19:51:05 +0000119 setup_single_tile(picture, bitmap, options, tiles, tile_x_start,
reed@google.com006db0f2012-06-27 19:33:29 +0000120 tile_y_start);
121 }
122 }
123
124}
125
keyar@chromium.orga4091ba2012-07-10 19:53:59 +0000126static 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.com006db0f2012-06-27 19:33:29 +0000130 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.org2724ae02012-07-10 15:13:21 +0000153 printf("%i_tiles_%ix%i: cmsecs = %6.2f\n", tiles.count(), options.fTileWidth,
reed@google.com006db0f2012-06-27 19:33:29 +0000154 options.fTileHeight, timer.fWall / options.fRepeats);
155}
156
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000157static 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.orga4091ba2012-07-10 19:53:59 +0000165static 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.orgcf6c44c2012-07-09 19:37:40 +0000169 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.org49169712012-07-12 19:19:55 +0000185static 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.orgd1dc9202012-07-09 18:32:08 +0000208static void run_single_benchmark(const SkString& inputPath,
keyar@chromium.org0665f252012-07-10 18:30:18 +0000209 Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000210 SkFILEStream inputStream;
211
reed@google.com006db0f2012-06-27 19:33:29 +0000212 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.com006db0f2012-06-27 19:33:29 +0000219
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000220 SkString filename;
221 sk_tools::get_basename(&filename, inputPath);
reed@google.com006db0f2012-06-27 19:33:29 +0000222 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000223 filename.c_str());
224
keyar@chromium.org0665f252012-07-10 18:30:18 +0000225 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.orga4091ba2012-07-10 19:53:59 +0000234 options->fBenchmark(&picture, *options);
keyar@chromium.org0665f252012-07-10 18:30:18 +0000235}
236
237static bool is_percentage(char* const string) {
238 SkString skString(string);
239 return skString.endsWith("%");
reed@google.com006db0f2012-06-27 19:33:29 +0000240}
241
242static void parse_commandline(int argc, char* const argv[],
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000243 SkTArray<SkString>* inputs, Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000244 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.org0665f252012-07-10 18:30:18 +0000265 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.com006db0f2012-06-27 19:33:29 +0000277 }
278 } else {
279 SkDebugf("Missing width for --tile\n");
280 usage(argv0);
281 exit(-1);
282 }
283 ++argv;
284 if (argv < stop) {
keyar@chromium.org0665f252012-07-10 18:30:18 +0000285 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.com006db0f2012-06-27 19:33:29 +0000298 }
299 } else {
300 SkDebugf("Missing height for --tile\n");
keyar@chromium.org0665f252012-07-10 18:30:18 +0000301 usage(argv0);
reed@google.com006db0f2012-06-27 19:33:29 +0000302 exit(-1);
303 }
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000304 } else if (0 == strcmp(*argv, "--pipe")) {
305 options->fBenchmark = run_pipe_benchmark;
keyar@chromium.org49169712012-07-12 19:19:55 +0000306 } else if (0 == strcmp(*argv, "--unflatten")) {
307 options->fBenchmark = run_unflatten_benchmark;
reed@google.com006db0f2012-06-27 19:33:29 +0000308 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
309 usage(argv0);
310 exit(0);
311 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000312 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000313 }
314 }
315
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000316 if (inputs->count() < 1) {
reed@google.com006db0f2012-06-27 19:33:29 +0000317 usage(argv0);
318 exit(-1);
319 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000320}
reed@google.com006db0f2012-06-27 19:33:29 +0000321
keyar@chromium.org0665f252012-07-10 18:30:18 +0000322static void process_input(const SkString& input, Options* options) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000323 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.com006db0f2012-06-27 19:33:29 +0000336}
337
338int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000339 SkTArray<SkString> inputs;
reed@google.com006db0f2012-06-27 19:33:29 +0000340 Options options;
341
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000342 parse_commandline(argc, argv, &inputs, &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000343
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000344 for (int i = 0; i < inputs.count(); ++i) {
keyar@chromium.org0665f252012-07-10 18:30:18 +0000345 process_input(inputs[i], &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000346 }
347}