blob: 1c3dd4a016ddd938ecef79d49af0ef727ef487a9 [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) {
robertphillips@google.com59f46b82012-07-10 17:30:58 +000088 SkRect clip = SkRect::MakeWH(SkIntToScalar(picture->width()),
89 SkIntToScalar(picture->height()));
keyar@chromium.org70b42222012-07-09 19:51:05 +000090 tile.fCanvas->clipRect(clip);
91}
92
93static void setup_single_tile(SkPicture* picture, const SkBitmap& bitmap,
94 const Options& options, SkTArray<TileInfo>* tiles,
reed@google.com006db0f2012-06-27 19:33:29 +000095 int tile_x_start, int tile_y_start) {
96 TileInfo& tile = tiles->push_back();
97 tile.fBitmap = new SkBitmap();
98 SkIRect rect = SkIRect::MakeXYWH(tile_x_start, tile_y_start,
keyar@chromium.org70b42222012-07-09 19:51:05 +000099 options.fTileWidth, options.fTileHeight);
reed@google.com006db0f2012-06-27 19:33:29 +0000100 bitmap.extractSubset(tile.fBitmap, rect);
101 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000102 tile.fCanvas->translate(SkIntToScalar(-tile_x_start),
103 SkIntToScalar(-tile_y_start));
keyar@chromium.org70b42222012-07-09 19:51:05 +0000104
105 clip_tile(picture, tile);
reed@google.com006db0f2012-06-27 19:33:29 +0000106}
107
108static void setup_tiles(SkPicture* picture, const SkBitmap& bitmap,
109 const Options& options, SkTArray<TileInfo>* tiles) {
110 for (int tile_y_start = 0; tile_y_start < picture->height();
111 tile_y_start += options.fTileHeight) {
112 for (int tile_x_start = 0; tile_x_start < picture->width();
113 tile_x_start += options.fTileWidth) {
keyar@chromium.org70b42222012-07-09 19:51:05 +0000114 setup_single_tile(picture, bitmap, options, tiles, tile_x_start,
reed@google.com006db0f2012-06-27 19:33:29 +0000115 tile_y_start);
116 }
117 }
118
119}
120
121static void run_tile_benchmark(SkPicture* picture, const SkBitmap& bitmap,
122 const Options& options) {
123 SkTArray<TileInfo> tiles;
124 setup_tiles(picture, bitmap, options, &tiles);
125
126 // We throw this away to remove first time effects (such as paging in this
127 // program)
128 for (int j = 0; j < tiles.count(); ++j) {
129 tiles[j].fCanvas->drawPicture(*picture);
130 }
131
132 BenchTimer timer = BenchTimer(NULL);
133 timer.start();
134 for (int i = 0; i < options.fRepeats; ++i) {
135 for (int j = 0; j < tiles.count(); ++j) {
136 tiles[j].fCanvas->drawPicture(*picture);
137 }
138 }
139 timer.end();
140
141 for (int i = 0; i < tiles.count(); ++i) {
142 delete tiles[i].fCanvas;
143 delete tiles[i].fBitmap;
144 }
145
keyar@chromium.org2724ae02012-07-10 15:13:21 +0000146 printf("%i_tiles_%ix%i: cmsecs = %6.2f\n", tiles.count(), options.fTileWidth,
reed@google.com006db0f2012-06-27 19:33:29 +0000147 options.fTileHeight, timer.fWall / options.fRepeats);
148}
149
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000150static void pipe_run(SkPicture* picture, SkCanvas* canvas) {
151 PipeController pipeController(canvas);
152 SkGPipeWriter writer;
153 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
154 pipeCanvas->drawPicture(*picture);
155 writer.endRecording();
156}
157
158static void run_pipe_benchmark(SkPicture* picture, const SkBitmap& bitmap,
159 const Options& options) {
160 SkCanvas canvas(bitmap);
161
162 // We throw this away to remove first time effects (such as paging in this
163 // program)
164 pipe_run(picture, &canvas);
165
166 BenchTimer timer = BenchTimer(NULL);
167 timer.start();
168 for (int i = 0; i < options.fRepeats; ++i) {
169 pipe_run(picture, &canvas);
170 }
171 timer.end();
172
173 printf("pipe: cmsecs = %6.2f\n", timer.fWall / options.fRepeats);
174}
175
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000176static void run_single_benchmark(const SkString& inputPath,
177 const Options& options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000178 SkFILEStream inputStream;
179
reed@google.com006db0f2012-06-27 19:33:29 +0000180 inputStream.setPath(inputPath.c_str());
181 if (!inputStream.isValid()) {
182 SkDebugf("Could not open file %s\n", inputPath.c_str());
183 return;
184 }
185
186 SkPicture picture(&inputStream);
187 SkBitmap bitmap;
188 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
189
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000190 SkString filename;
191 sk_tools::get_basename(&filename, inputPath);
reed@google.com006db0f2012-06-27 19:33:29 +0000192 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000193 filename.c_str());
194
reed@google.com006db0f2012-06-27 19:33:29 +0000195 options.fBenchmark(&picture, bitmap, options);
196}
197
198static void parse_commandline(int argc, char* const argv[],
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000199 SkTArray<SkString>* inputs, Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000200 const char* argv0 = argv[0];
201 char* const* stop = argv + argc;
202
203 for (++argv; argv < stop; ++argv) {
204 if (0 == strcmp(*argv, "--repeat")) {
205 ++argv;
206 if (argv < stop) {
207 options->fRepeats = atoi(*argv);
208 if (options->fRepeats < 1) {
209 SkDebugf("--repeat must be given a value > 0\n");
210 exit(-1);
211 }
212 } else {
213 SkDebugf("Missing arg for --repeat\n");
214 usage(argv0);
215 exit(-1);
216 }
217 } else if (0 == strcmp(*argv, "--tile")) {
218 options->fBenchmark = run_tile_benchmark;
219 ++argv;
220 if (argv < stop) {
221 options->fTileWidth = atoi(*argv);
222 if (options->fTileWidth < 1) {
223 SkDebugf("--tile must be given a width with a value > 0\n");
224 exit(-1);
225 }
226 } else {
227 SkDebugf("Missing width for --tile\n");
228 usage(argv0);
229 exit(-1);
230 }
231 ++argv;
232 if (argv < stop) {
233 options->fTileHeight = atoi(*argv);
234 if (options->fTileHeight < 1) {
235 SkDebugf("--tile must be given a height with a value > 0"
236 "\n");
237 exit(-1);
238 }
239 } else {
240 SkDebugf("Missing height for --tile\n");
241 usage(argv0);\
242 exit(-1);
243 }
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000244 } else if (0 == strcmp(*argv, "--pipe")) {
245 options->fBenchmark = run_pipe_benchmark;
reed@google.com006db0f2012-06-27 19:33:29 +0000246 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
247 usage(argv0);
248 exit(0);
249 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000250 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000251 }
252 }
253
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000254 if (inputs->count() < 1) {
reed@google.com006db0f2012-06-27 19:33:29 +0000255 usage(argv0);
256 exit(-1);
257 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000258}
reed@google.com006db0f2012-06-27 19:33:29 +0000259
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000260static void process_input(const SkString& input, const Options& options) {
261 SkOSFile::Iter iter(input.c_str(), "skp");
262 SkString inputFilename;
263
264 if (iter.next(&inputFilename)) {
265 do {
266 SkString inputPath;
267 sk_tools::make_filepath(&inputPath, input.c_str(),
268 inputFilename);
269 run_single_benchmark(inputPath, options);
270 } while(iter.next(&inputFilename));
271 } else {
272 run_single_benchmark(input, options);
273 }
reed@google.com006db0f2012-06-27 19:33:29 +0000274}
275
276int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000277 SkTArray<SkString> inputs;
reed@google.com006db0f2012-06-27 19:33:29 +0000278 Options options;
279
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000280 parse_commandline(argc, argv, &inputs, &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000281
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000282 for (int i = 0; i < inputs.count(); ++i) {
283 process_input(inputs[i], options);
reed@google.com006db0f2012-06-27 19:33:29 +0000284 }
285}