blob: b2a7b7dea62bb9565d7de7d7a028669eb7829e55 [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;
24static void run_simple_benchmark(SkPicture* picture, const SkBitmap&,
25 const Options&);
26
27struct 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
38static void usage(const char* argv0) {
39 SkDebugf("SkPicture benchmarking tool\n");
40 SkDebugf("\n"
41"Usage: \n"
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000042" %s <inputDir>...\n"
reed@google.com006db0f2012-06-27 19:33:29 +000043" [--repeat] [--tile width height]"
44, argv0);
45 SkDebugf("\n\n");
46 SkDebugf(
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000047" 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.com006db0f2012-06-27 19:33:29 +000049 SkDebugf(
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +000050" --pipe : "
51"Set to use piping."
52" Default is to not use piping.\n");
53 SkDebugf(
reed@google.com006db0f2012-06-27 19:33:29 +000054" --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
63static 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
82struct TileInfo {
83 SkBitmap* fBitmap;
84 SkCanvas* fCanvas;
85};
86
keyar@chromium.org70b42222012-07-09 19:51:05 +000087static void clip_tile(SkPicture* picture, const TileInfo& tile) {
88 SkRect clip = SkRect::MakeWH(picture->width(), picture->height());
89 tile.fCanvas->clipRect(clip);
90}
91
92static void setup_single_tile(SkPicture* picture, const SkBitmap& bitmap,
93 const Options& options, SkTArray<TileInfo>* tiles,
reed@google.com006db0f2012-06-27 19:33:29 +000094 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.org70b42222012-07-09 19:51:05 +000098 options.fTileWidth, options.fTileHeight);
reed@google.com006db0f2012-06-27 19:33:29 +000099 bitmap.extractSubset(tile.fBitmap, rect);
100 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
101 tile.fCanvas->translate(-tile_x_start, -tile_y_start);
keyar@chromium.org70b42222012-07-09 19:51:05 +0000102
103 clip_tile(picture, tile);
reed@google.com006db0f2012-06-27 19:33:29 +0000104}
105
106static 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.org70b42222012-07-09 19:51:05 +0000112 setup_single_tile(picture, bitmap, options, tiles, tile_x_start,
reed@google.com006db0f2012-06-27 19:33:29 +0000113 tile_y_start);
114 }
115 }
116
117}
118
119static 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.org2724ae02012-07-10 15:13:21 +0000144 printf("%i_tiles_%ix%i: cmsecs = %6.2f\n", tiles.count(), options.fTileWidth,
reed@google.com006db0f2012-06-27 19:33:29 +0000145 options.fTileHeight, timer.fWall / options.fRepeats);
146}
147
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000148static 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
156static 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.orgd1dc9202012-07-09 18:32:08 +0000174static void run_single_benchmark(const SkString& inputPath,
175 const Options& options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000176 SkFILEStream inputStream;
177
reed@google.com006db0f2012-06-27 19:33:29 +0000178 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.orgd1dc9202012-07-09 18:32:08 +0000188 SkString filename;
189 sk_tools::get_basename(&filename, inputPath);
reed@google.com006db0f2012-06-27 19:33:29 +0000190 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000191 filename.c_str());
192
reed@google.com006db0f2012-06-27 19:33:29 +0000193 options.fBenchmark(&picture, bitmap, options);
194}
195
196static void parse_commandline(int argc, char* const argv[],
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000197 SkTArray<SkString>* inputs, Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000198 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.orgcf6c44c2012-07-09 19:37:40 +0000242 } else if (0 == strcmp(*argv, "--pipe")) {
243 options->fBenchmark = run_pipe_benchmark;
reed@google.com006db0f2012-06-27 19:33:29 +0000244 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
245 usage(argv0);
246 exit(0);
247 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000248 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000249 }
250 }
251
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000252 if (inputs->count() < 1) {
reed@google.com006db0f2012-06-27 19:33:29 +0000253 usage(argv0);
254 exit(-1);
255 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000256}
reed@google.com006db0f2012-06-27 19:33:29 +0000257
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000258static 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.com006db0f2012-06-27 19:33:29 +0000272}
273
274int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000275 SkTArray<SkString> inputs;
reed@google.com006db0f2012-06-27 19:33:29 +0000276 Options options;
277
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000278 parse_commandline(argc, argv, &inputs, &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000279
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000280 for (int i = 0; i < inputs.count(); ++i) {
281 process_input(inputs[i], options);
reed@google.com006db0f2012-06-27 19:33:29 +0000282 }
283}