blob: 7443604dae398ab3a893eec80d1a6af8ca00a301 [file] [log] [blame]
reed@android.combd700c32009-01-05 03:34:50 +00001#include "SkCanvas.h"
reed@android.comf523e252009-01-26 23:15:37 +00002#include "SkColorPriv.h"
reed@android.com3a859a02009-01-28 00:56:29 +00003#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +00004#include "SkImageEncoder.h"
reed@android.com6c924ad2009-03-31 03:48:49 +00005#include "SkNWayCanvas.h"
6#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +00007#include "SkString.h"
reed@android.com4bc19832009-01-19 20:08:35 +00008#include "SkTime.h"
reed@android.combd700c32009-01-05 03:34:50 +00009
10#include "SkBenchmark.h"
11
reed@android.com29348cb2009-08-04 18:17:15 +000012#ifdef ANDROID
13static void log_error(const char msg[]) { SkDebugf("%s", msg); }
14static void log_progress(const char msg[]) { SkDebugf("%s", msg); }
15#else
16static void log_error(const char msg[]) { fprintf(stderr, "%s", msg); }
17static void log_progress(const char msg[]) { printf("%s", msg); }
18#endif
19
20static void log_error(const SkString& str) { log_error(str.c_str()); }
21static void log_progress(const SkString& str) { log_progress(str.c_str()); }
22
23///////////////////////////////////////////////////////////////////////////////
24
reed@android.com6c924ad2009-03-31 03:48:49 +000025static void erase(SkBitmap& bm) {
26 if (bm.config() == SkBitmap::kA8_Config) {
27 bm.eraseColor(0);
28 } else {
29 bm.eraseColor(SK_ColorWHITE);
30 }
31}
32
33static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
34 if (bm1.width() != bm2.width() ||
35 bm1.height() != bm2.height() ||
36 bm1.config() != bm2.config()) {
37 return false;
38 }
39
40 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
41 for (int y = 0; y < bm1.height(); y++) {
42 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
43 return false;
44 }
45 }
46
47 return true;
48}
49
reed@android.combd700c32009-01-05 03:34:50 +000050class Iter {
51public:
reed@android.come9d00602009-09-02 21:12:42 +000052 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +000053 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +000054 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +000055 }
56
57 SkBenchmark* next() {
58 if (fBench) {
59 BenchRegistry::Factory f = fBench->factory();
60 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +000061 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +000062 }
63 return NULL;
64 }
65
66private:
67 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +000068 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +000069};
70
71static void make_filename(const char name[], SkString* path) {
72 path->set(name);
73 for (int i = 0; name[i]; i++) {
74 switch (name[i]) {
75 case '/':
76 case '\\':
77 case ' ':
78 case ':':
79 path->writable_str()[i] = '-';
80 break;
81 default:
82 break;
83 }
84 }
85}
86
reed@android.com4c7d3d62009-01-21 03:15:13 +000087static void saveFile(const char name[], const char config[], const char dir[],
88 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +000089 SkBitmap copy;
90 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
91 return;
92 }
reed@android.comf523e252009-01-26 23:15:37 +000093
94 if (bm.config() == SkBitmap::kA8_Config) {
95 // turn alpha into gray-scale
96 size_t size = copy.getSize() >> 2;
97 SkPMColor* p = copy.getAddr32(0, 0);
98 for (size_t i = 0; i < size; i++) {
99 int c = (*p >> SK_A32_SHIFT) & 0xFF;
100 c = 255 - c;
101 c |= (c << 24) | (c << 16) | (c << 8);
102 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
103 }
104 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000105
106 SkString str;
107 make_filename(name, &str);
108 str.appendf("_%s.png", config);
109 str.prepend(dir);
110 ::remove(str.c_str());
111 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
112 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000113}
114
115static void performClip(SkCanvas* canvas, int w, int h) {
116 SkRect r;
117
118 r.set(SkIntToScalar(10), SkIntToScalar(10),
119 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
120 canvas->clipRect(r, SkRegion::kIntersect_Op);
121
122 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
123 SkIntToScalar(w-10), SkIntToScalar(h-10));
124 canvas->clipRect(r, SkRegion::kXOR_Op);
125}
126
127static void performRotate(SkCanvas* canvas, int w, int h) {
128 const SkScalar x = SkIntToScalar(w) / 2;
129 const SkScalar y = SkIntToScalar(h) / 2;
130
131 canvas->translate(x, y);
132 canvas->rotate(SkIntToScalar(35));
133 canvas->translate(-x, -y);
134}
135
reed@android.com387359e2009-08-04 01:51:09 +0000136static void performScale(SkCanvas* canvas, int w, int h) {
137 const SkScalar x = SkIntToScalar(w) / 2;
138 const SkScalar y = SkIntToScalar(h) / 2;
139
140 canvas->translate(x, y);
141 // just enough so we can't take the sprite case
142 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
143 canvas->translate(-x, -y);
144}
145
reed@android.com6c924ad2009-03-31 03:48:49 +0000146static void compare_pict_to_bitmap(SkPicture* pict, const SkBitmap& bm) {
147 SkBitmap bm2;
148
149 bm2.setConfig(bm.config(), bm.width(), bm.height());
150 bm2.allocPixels();
151 erase(bm2);
152
153 SkCanvas canvas(bm2);
154 canvas.drawPicture(*pict);
155
156 if (!equal(bm, bm2)) {
157 SkDebugf("----- compare_pict_to_bitmap failed\n");
158 }
159}
160
reed@android.com29348cb2009-08-04 18:17:15 +0000161static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
162 if (argv < stop) {
163 *var = atoi(*argv) != 0;
164 return true;
165 }
166 return false;
167}
168
reed@android.com4bc19832009-01-19 20:08:35 +0000169static const struct {
170 SkBitmap::Config fConfig;
171 const char* fName;
172} gConfigs[] = {
173 { SkBitmap::kARGB_8888_Config, "8888" },
174 { SkBitmap::kRGB_565_Config, "565", },
reed@android.com831f6c62010-02-22 22:03:06 +0000175#if 0
reed@android.com4bc19832009-01-19 20:08:35 +0000176 { SkBitmap::kARGB_4444_Config, "4444", },
177 { SkBitmap::kA8_Config, "A8", }
reed@android.com831f6c62010-02-22 22:03:06 +0000178#endif
reed@android.com4bc19832009-01-19 20:08:35 +0000179};
180
reed@android.com4c7d3d62009-01-21 03:15:13 +0000181static int findConfig(const char config[]) {
182 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
183 if (!strcmp(config, gConfigs[i].fName)) {
184 return i;
185 }
186 }
187 return -1;
188}
189
reed@android.combd700c32009-01-05 03:34:50 +0000190int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000191 SkAutoGraphics ag;
192
reed@android.come9d00602009-09-02 21:12:42 +0000193 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000194 int repeatDraw = 1;
195 int forceAlpha = 0xFF;
196 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000197 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000198 SkTriState::State forceDither = SkTriState::kDefault;
reed@android.com387359e2009-08-04 01:51:09 +0000199 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000200 bool doRotate = false;
201 bool doClip = false;
reed@android.com6c924ad2009-03-31 03:48:49 +0000202 bool doPict = false;
reed@android.com387359e2009-08-04 01:51:09 +0000203 const char* matchStr = NULL;
reed@android.com4bc19832009-01-19 20:08:35 +0000204
reed@android.comb398fe82009-01-07 11:47:57 +0000205 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000206 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
207 const char* configName = "";
208 int configCount = SK_ARRAY_COUNT(gConfigs);
reed@android.combd700c32009-01-05 03:34:50 +0000209
reed@android.comb398fe82009-01-07 11:47:57 +0000210 char* const* stop = argv + argc;
211 for (++argv; argv < stop; ++argv) {
212 if (strcmp(*argv, "-o") == 0) {
213 argv++;
214 if (argv < stop && **argv) {
215 outDir.set(*argv);
216 if (outDir.c_str()[outDir.size() - 1] != '/') {
217 outDir.append("/");
218 }
219 }
reed@android.com6c924ad2009-03-31 03:48:49 +0000220 } else if (strcmp(*argv, "-pict") == 0) {
221 doPict = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000222 } else if (strcmp(*argv, "-repeat") == 0) {
223 argv++;
224 if (argv < stop) {
225 repeatDraw = atoi(*argv);
226 if (repeatDraw < 1) {
227 repeatDraw = 1;
228 }
229 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000230 log_error("missing arg for -repeat\n");
reed@android.com4bc19832009-01-19 20:08:35 +0000231 return -1;
232 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000233 } else if (!strcmp(*argv, "-rotate")) {
234 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000235 } else if (!strcmp(*argv, "-scale")) {
236 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000237 } else if (!strcmp(*argv, "-clip")) {
238 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000239 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000240 if (!parse_bool_arg(++argv, stop, &forceAA)) {
241 log_error("missing arg for -forceAA\n");
242 return -1;
243 }
244 } else if (strcmp(*argv, "-forceFilter") == 0) {
245 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
246 log_error("missing arg for -forceFilter\n");
247 return -1;
248 }
reed@android.com4e635f92009-10-19 17:39:46 +0000249 } else if (strcmp(*argv, "-forceDither") == 0) {
250 bool tmp;
251 if (!parse_bool_arg(++argv, stop, &tmp)) {
252 log_error("missing arg for -forceDither\n");
253 return -1;
254 }
255 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000256 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000257 bool wantAlpha = false;
258 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
259 log_error("missing arg for -forceBlend\n");
260 return -1;
261 }
262 forceAlpha = wantAlpha ? 0x80 : 0xFF;
reed@android.com387359e2009-08-04 01:51:09 +0000263 } else if (strcmp(*argv, "-match") == 0) {
264 argv++;
265 if (argv < stop) {
266 matchStr = *argv;
267 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000268 log_error("missing arg for -match\n");
reed@android.com387359e2009-08-04 01:51:09 +0000269 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000270 }
reed@android.com387359e2009-08-04 01:51:09 +0000271 } else if (strcmp(*argv, "-config") == 0) {
272 argv++;
273 if (argv < stop) {
274 int index = findConfig(*argv);
275 if (index >= 0) {
276 outConfig = gConfigs[index].fConfig;
277 configName = gConfigs[index].fName;
278 configCount = 1;
279 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000280 SkString str;
281 str.printf("unrecognized config %s\n", *argv);
282 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000283 return -1;
284 }
285 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000286 log_error("missing arg for -config\n");
reed@android.com387359e2009-08-04 01:51:09 +0000287 return -1;
288 }
reed@android.com0c9da392010-02-22 19:50:13 +0000289 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000290 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000291 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000292 defineDict.set(argv[-1] + 2, *argv);
293 } else {
294 log_error("incomplete '-Dfoo bar' definition\n");
295 return -1;
296 }
reed@android.com387359e2009-08-04 01:51:09 +0000297 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000298 SkString str;
299 str.printf("unrecognized arg %s\n", *argv);
300 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000301 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000302 }
303 }
reed@android.com387359e2009-08-04 01:51:09 +0000304
reed@android.come9d00602009-09-02 21:12:42 +0000305 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000306 SkBenchmark* bench;
307 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000308 SkIPoint dim = bench->getSize();
309 if (dim.fX <= 0 || dim.fY <= 0) {
310 continue;
311 }
reed@android.comb398fe82009-01-07 11:47:57 +0000312
reed@android.com4bc19832009-01-19 20:08:35 +0000313 bench->setForceAlpha(forceAlpha);
314 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000315 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000316 bench->setDither(forceDither);
reed@android.com4bc19832009-01-19 20:08:35 +0000317
reed@android.com387359e2009-08-04 01:51:09 +0000318 // only run benchmarks if their name contains matchStr
319 if (matchStr && strstr(bench->getName(), matchStr) == NULL) {
320 continue;
321 }
322
reed@android.com29348cb2009-08-04 18:17:15 +0000323 {
324 SkString str;
reed@android.com0c9da392010-02-22 19:50:13 +0000325 str.printf("running bench [%d %d] %16s", dim.fX, dim.fY,
326 bench->getName());
reed@android.com29348cb2009-08-04 18:17:15 +0000327 log_progress(str);
328 }
reed@android.com4bc19832009-01-19 20:08:35 +0000329
330 for (int configIndex = 0; configIndex < configCount; configIndex++) {
331 if (configCount > 1) {
332 outConfig = gConfigs[configIndex].fConfig;
333 configName = gConfigs[configIndex].fName;
334 }
335
336 SkBitmap bm;
337 bm.setConfig(outConfig, dim.fX, dim.fY);
338 bm.allocPixels();
reed@android.com6c924ad2009-03-31 03:48:49 +0000339 erase(bm);
reed@android.comf523e252009-01-26 23:15:37 +0000340
reed@android.com4bc19832009-01-19 20:08:35 +0000341 SkCanvas canvas(bm);
reed@android.comf523e252009-01-26 23:15:37 +0000342
reed@android.com4c7d3d62009-01-21 03:15:13 +0000343 if (doClip) {
344 performClip(&canvas, dim.fX, dim.fY);
345 }
reed@android.com387359e2009-08-04 01:51:09 +0000346 if (doScale) {
347 performScale(&canvas, dim.fX, dim.fY);
348 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000349 if (doRotate) {
350 performRotate(&canvas, dim.fX, dim.fY);
351 }
352
reed@android.com4bc19832009-01-19 20:08:35 +0000353 SkMSec now = SkTime::GetMSecs();
354 for (int i = 0; i < repeatDraw; i++) {
reed@android.com6c924ad2009-03-31 03:48:49 +0000355 SkCanvas* c = &canvas;
356
357 SkNWayCanvas nway;
358 SkPicture* pict = NULL;
359 if (doPict) {
360 pict = new SkPicture;
361 nway.addCanvas(pict->beginRecording(bm.width(), bm.height()));
362 nway.addCanvas(&canvas);
363 c = &nway;
364 }
365
366 SkAutoCanvasRestore acr(c, true);
367 bench->draw(c);
368
369 if (pict) {
370 compare_pict_to_bitmap(pict, bm);
371 pict->unref();
372 }
reed@android.com4bc19832009-01-19 20:08:35 +0000373 }
374 if (repeatDraw > 1) {
reed@android.com831f6c62010-02-22 22:03:06 +0000375 double duration = SkTime::GetMSecs() - now;
reed@android.com29348cb2009-08-04 18:17:15 +0000376 SkString str;
reed@android.com831f6c62010-02-22 22:03:06 +0000377 str.printf(" %4s: msecs = %7.2f, fps = %7.2f", configName,
378 duration / repeatDraw, repeatDraw * 1000.0 / duration);
reed@android.com29348cb2009-08-04 18:17:15 +0000379 log_progress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000380 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000381 if (outDir.size() > 0) {
382 saveFile(bench->getName(), configName, outDir.c_str(), bm);
383 }
reed@android.com4bc19832009-01-19 20:08:35 +0000384 }
reed@android.com29348cb2009-08-04 18:17:15 +0000385 log_progress("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000386 }
387
388 return 0;
389}