blob: 88163110fb6748263ebe97791149630a4e912976 [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
87static void setup_single_tile(const SkBitmap& bitmap, const Options& options,
88 SkTArray<TileInfo>* tiles,
89 int tile_x_start, int tile_y_start) {
90 TileInfo& tile = tiles->push_back();
91 tile.fBitmap = new SkBitmap();
92 SkIRect rect = SkIRect::MakeXYWH(tile_x_start, tile_y_start,
93 options.fTileWidth,
94 options.fTileHeight);
95 bitmap.extractSubset(tile.fBitmap, rect);
96 tile.fCanvas = new SkCanvas(*(tile.fBitmap));
97 tile.fCanvas->translate(-tile_x_start, -tile_y_start);
98}
99
100static void setup_tiles(SkPicture* picture, const SkBitmap& bitmap,
101 const Options& options, SkTArray<TileInfo>* tiles) {
102 for (int tile_y_start = 0; tile_y_start < picture->height();
103 tile_y_start += options.fTileHeight) {
104 for (int tile_x_start = 0; tile_x_start < picture->width();
105 tile_x_start += options.fTileWidth) {
106 setup_single_tile(bitmap, options, tiles, tile_x_start,
107 tile_y_start);
108 }
109 }
110
111}
112
113static void run_tile_benchmark(SkPicture* picture, const SkBitmap& bitmap,
114 const Options& options) {
115 SkTArray<TileInfo> tiles;
116 setup_tiles(picture, bitmap, options, &tiles);
117
118 // We throw this away to remove first time effects (such as paging in this
119 // program)
120 for (int j = 0; j < tiles.count(); ++j) {
121 tiles[j].fCanvas->drawPicture(*picture);
122 }
123
124 BenchTimer timer = BenchTimer(NULL);
125 timer.start();
126 for (int i = 0; i < options.fRepeats; ++i) {
127 for (int j = 0; j < tiles.count(); ++j) {
128 tiles[j].fCanvas->drawPicture(*picture);
129 }
130 }
131 timer.end();
132
133 for (int i = 0; i < tiles.count(); ++i) {
134 delete tiles[i].fCanvas;
135 delete tiles[i].fBitmap;
136 }
137
138 printf("tile%ix%i: cmsecs = %6.2f\n", options.fTileWidth,
139 options.fTileHeight, timer.fWall / options.fRepeats);
140}
141
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000142static void pipe_run(SkPicture* picture, SkCanvas* canvas) {
143 PipeController pipeController(canvas);
144 SkGPipeWriter writer;
145 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
146 pipeCanvas->drawPicture(*picture);
147 writer.endRecording();
148}
149
150static void run_pipe_benchmark(SkPicture* picture, const SkBitmap& bitmap,
151 const Options& options) {
152 SkCanvas canvas(bitmap);
153
154 // We throw this away to remove first time effects (such as paging in this
155 // program)
156 pipe_run(picture, &canvas);
157
158 BenchTimer timer = BenchTimer(NULL);
159 timer.start();
160 for (int i = 0; i < options.fRepeats; ++i) {
161 pipe_run(picture, &canvas);
162 }
163 timer.end();
164
165 printf("pipe: cmsecs = %6.2f\n", timer.fWall / options.fRepeats);
166}
167
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000168static void run_single_benchmark(const SkString& inputPath,
169 const Options& options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000170 SkFILEStream inputStream;
171
reed@google.com006db0f2012-06-27 19:33:29 +0000172 inputStream.setPath(inputPath.c_str());
173 if (!inputStream.isValid()) {
174 SkDebugf("Could not open file %s\n", inputPath.c_str());
175 return;
176 }
177
178 SkPicture picture(&inputStream);
179 SkBitmap bitmap;
180 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
181
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000182 SkString filename;
183 sk_tools::get_basename(&filename, inputPath);
reed@google.com006db0f2012-06-27 19:33:29 +0000184 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000185 filename.c_str());
186
reed@google.com006db0f2012-06-27 19:33:29 +0000187 options.fBenchmark(&picture, bitmap, options);
188}
189
190static void parse_commandline(int argc, char* const argv[],
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000191 SkTArray<SkString>* inputs, Options* options) {
reed@google.com006db0f2012-06-27 19:33:29 +0000192 const char* argv0 = argv[0];
193 char* const* stop = argv + argc;
194
195 for (++argv; argv < stop; ++argv) {
196 if (0 == strcmp(*argv, "--repeat")) {
197 ++argv;
198 if (argv < stop) {
199 options->fRepeats = atoi(*argv);
200 if (options->fRepeats < 1) {
201 SkDebugf("--repeat must be given a value > 0\n");
202 exit(-1);
203 }
204 } else {
205 SkDebugf("Missing arg for --repeat\n");
206 usage(argv0);
207 exit(-1);
208 }
209 } else if (0 == strcmp(*argv, "--tile")) {
210 options->fBenchmark = run_tile_benchmark;
211 ++argv;
212 if (argv < stop) {
213 options->fTileWidth = atoi(*argv);
214 if (options->fTileWidth < 1) {
215 SkDebugf("--tile must be given a width with a value > 0\n");
216 exit(-1);
217 }
218 } else {
219 SkDebugf("Missing width for --tile\n");
220 usage(argv0);
221 exit(-1);
222 }
223 ++argv;
224 if (argv < stop) {
225 options->fTileHeight = atoi(*argv);
226 if (options->fTileHeight < 1) {
227 SkDebugf("--tile must be given a height with a value > 0"
228 "\n");
229 exit(-1);
230 }
231 } else {
232 SkDebugf("Missing height for --tile\n");
233 usage(argv0);\
234 exit(-1);
235 }
keyar@chromium.orgcf6c44c2012-07-09 19:37:40 +0000236 } else if (0 == strcmp(*argv, "--pipe")) {
237 options->fBenchmark = run_pipe_benchmark;
reed@google.com006db0f2012-06-27 19:33:29 +0000238 } else if (0 == strcmp(*argv, "--help") || 0 == strcmp(*argv, "-h")) {
239 usage(argv0);
240 exit(0);
241 } else {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000242 inputs->push_back(SkString(*argv));
reed@google.com006db0f2012-06-27 19:33:29 +0000243 }
244 }
245
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000246 if (inputs->count() < 1) {
reed@google.com006db0f2012-06-27 19:33:29 +0000247 usage(argv0);
248 exit(-1);
249 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000250}
reed@google.com006db0f2012-06-27 19:33:29 +0000251
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000252static void process_input(const SkString& input, const Options& options) {
253 SkOSFile::Iter iter(input.c_str(), "skp");
254 SkString inputFilename;
255
256 if (iter.next(&inputFilename)) {
257 do {
258 SkString inputPath;
259 sk_tools::make_filepath(&inputPath, input.c_str(),
260 inputFilename);
261 run_single_benchmark(inputPath, options);
262 } while(iter.next(&inputFilename));
263 } else {
264 run_single_benchmark(input, options);
265 }
reed@google.com006db0f2012-06-27 19:33:29 +0000266}
267
268int main(int argc, char* const argv[]) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000269 SkTArray<SkString> inputs;
reed@google.com006db0f2012-06-27 19:33:29 +0000270 Options options;
271
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000272 parse_commandline(argc, argv, &inputs, &options);
reed@google.com006db0f2012-06-27 19:33:29 +0000273
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000274 for (int i = 0; i < inputs.count(); ++i) {
275 process_input(inputs[i], options);
reed@google.com006db0f2012-06-27 19:33:29 +0000276 }
277}