Allow specific files and multiple inputs for picture testing tools.
[platform/upstream/libSkiaSharp.git] / tools / bench_pictures_main.cpp
1 /*
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
17 const int DEFAULT_REPEATS = 100;
18 const int DEFAULT_TILE_WIDTH = 256;
19 const int DEFAULT_TILE_HEIGHT = 256;
20
21 struct Options;
22 static void run_simple_benchmark(SkPicture* picture, const SkBitmap&,
23                                  const Options&);
24
25 struct 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
36 static void usage(const char* argv0) {
37     SkDebugf("SkPicture benchmarking tool\n");
38     SkDebugf("\n"
39 "Usage: \n"
40 "     %s <inputDir>...\n"
41 "     [--repeat] [--tile width height]"
42 , argv0);
43     SkDebugf("\n\n");
44     SkDebugf(
45 "     inputDir:  A list of directories and files to use as input.\n"
46 "                    Files are expected to have the .skp extension.\n\n");
47     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
57 static 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
76 struct TileInfo {
77     SkBitmap* fBitmap;
78     SkCanvas* fCanvas;
79 };
80
81 static 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
94 static 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
107 static 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
136 static void run_single_benchmark(const SkString& inputPath,
137                                  const Options& options) {
138     SkFILEStream inputStream;
139
140     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
150     SkString filename;
151     sk_tools::get_basename(&filename, inputPath);
152     printf("running bench [%i %i] %s ", picture.width(), picture.height(),
153            filename.c_str());
154
155     options.fBenchmark(&picture, bitmap, options);
156 }
157
158 static void parse_commandline(int argc, char* const argv[],
159                               SkTArray<SkString>* inputs, Options* options) {
160     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 {
208             inputs->push_back(SkString(*argv));
209         }
210     }
211
212     if (inputs->count() < 1) {
213         usage(argv0);
214         exit(-1);
215     }
216 }
217
218 static 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     }
232 }
233
234 int main(int argc, char* const argv[]) {
235     SkTArray<SkString> inputs;
236     Options options;
237
238     parse_commandline(argc, argv, &inputs, &options);
239
240     for (int i = 0; i < inputs.count(); ++i) {
241         process_input(inputs[i], options);
242     }
243 }