blob: 355deb681724b3dbbce4c1d02e5e876b8eb8090d [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"
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
17const int DEFAULT_REPEATS = 100;
18const int DEFAULT_TILE_WIDTH = 256;
19const int DEFAULT_TILE_HEIGHT = 256;
20
21struct Options;
22static void run_simple_benchmark(SkPicture* picture, const SkBitmap&,
23 const Options&);
24
25struct 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
36static 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: directory to read the serialized SkPicture files."
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
57static 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
76struct TileInfo {
77 SkBitmap* fBitmap;
78 SkCanvas* fCanvas;
79};
80
81static 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
94static 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
107static 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
136static void run_benchmark(const char* inputDir,
137 const SkString& inputFilename,
138 const Options& options) {
139 SkFILEStream inputStream;
140
141 SkString inputPath;
142 sk_tools::make_filepath(&inputPath, inputDir, inputFilename);
143 inputStream.setPath(inputPath.c_str());
144 if (!inputStream.isValid()) {
145 SkDebugf("Could not open file %s\n", inputPath.c_str());
146 return;
147 }
148
149 SkPicture picture(&inputStream);
150 SkBitmap bitmap;
151 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
152
153 printf("running bench [%i %i] %s ", picture.width(), picture.height(),
154 inputFilename.c_str());
155 options.fBenchmark(&picture, bitmap, options);
156}
157
158static void parse_commandline(int argc, char* const argv[],
159 const char** inputDir, 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 if (NULL == *inputDir) {
209 *inputDir = *argv;
210 } else {
211 usage(argv0);
212 exit(-1);
213 }
214 }
215 }
216
217 if (NULL == *inputDir) {
218 usage(argv0);
219 exit(-1);
220 }
221
222}
223
224int main(int argc, char* const argv[]) {
225 const char* inputDir = NULL;
226 Options options;
227
228 parse_commandline(argc, argv, &inputDir, &options);
229
230 SkOSFile::Iter iter(inputDir, "skp");
231 SkString inputFilename;
232
233 while(iter.next(&inputFilename)) {
234 run_benchmark(inputDir, inputFilename, options);
235 }
236}