blob: 38724c1f7ab85dfba980d653e8e530c475b74fa5 [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"
mike@reedtribe.orga9015f82011-05-17 02:25:05 +00009#include "GrContext.h"
10#include "SkGpuDevice.h"
11#include "SkEGLContext.h"
reed@android.combd700c32009-01-05 03:34:50 +000012
13#include "SkBenchmark.h"
14
reed@android.com29348cb2009-08-04 18:17:15 +000015#ifdef ANDROID
16static void log_error(const char msg[]) { SkDebugf("%s", msg); }
17static void log_progress(const char msg[]) { SkDebugf("%s", msg); }
18#else
19static void log_error(const char msg[]) { fprintf(stderr, "%s", msg); }
20static void log_progress(const char msg[]) { printf("%s", msg); }
21#endif
22
23static void log_error(const SkString& str) { log_error(str.c_str()); }
24static void log_progress(const SkString& str) { log_progress(str.c_str()); }
25
26///////////////////////////////////////////////////////////////////////////////
27
reed@android.com6c924ad2009-03-31 03:48:49 +000028static void erase(SkBitmap& bm) {
29 if (bm.config() == SkBitmap::kA8_Config) {
30 bm.eraseColor(0);
31 } else {
32 bm.eraseColor(SK_ColorWHITE);
33 }
34}
35
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000036#if 0
reed@android.com6c924ad2009-03-31 03:48:49 +000037static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
38 if (bm1.width() != bm2.width() ||
39 bm1.height() != bm2.height() ||
40 bm1.config() != bm2.config()) {
41 return false;
42 }
43
44 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
45 for (int y = 0; y < bm1.height(); y++) {
46 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
47 return false;
48 }
49 }
reed@android.com6c924ad2009-03-31 03:48:49 +000050 return true;
51}
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000052#endif
reed@android.com6c924ad2009-03-31 03:48:49 +000053
reed@android.combd700c32009-01-05 03:34:50 +000054class Iter {
55public:
reed@android.come9d00602009-09-02 21:12:42 +000056 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +000057 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +000058 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +000059 }
60
61 SkBenchmark* next() {
62 if (fBench) {
63 BenchRegistry::Factory f = fBench->factory();
64 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +000065 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +000066 }
67 return NULL;
68 }
69
70private:
71 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +000072 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +000073};
74
75static void make_filename(const char name[], SkString* path) {
76 path->set(name);
77 for (int i = 0; name[i]; i++) {
78 switch (name[i]) {
79 case '/':
80 case '\\':
81 case ' ':
82 case ':':
83 path->writable_str()[i] = '-';
84 break;
85 default:
86 break;
87 }
88 }
89}
90
reed@android.com4c7d3d62009-01-21 03:15:13 +000091static void saveFile(const char name[], const char config[], const char dir[],
92 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +000093 SkBitmap copy;
94 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
95 return;
96 }
reed@android.comf523e252009-01-26 23:15:37 +000097
98 if (bm.config() == SkBitmap::kA8_Config) {
99 // turn alpha into gray-scale
100 size_t size = copy.getSize() >> 2;
101 SkPMColor* p = copy.getAddr32(0, 0);
102 for (size_t i = 0; i < size; i++) {
103 int c = (*p >> SK_A32_SHIFT) & 0xFF;
104 c = 255 - c;
105 c |= (c << 24) | (c << 16) | (c << 8);
106 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
107 }
108 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000109
110 SkString str;
111 make_filename(name, &str);
112 str.appendf("_%s.png", config);
113 str.prepend(dir);
114 ::remove(str.c_str());
115 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
116 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000117}
118
119static void performClip(SkCanvas* canvas, int w, int h) {
120 SkRect r;
121
122 r.set(SkIntToScalar(10), SkIntToScalar(10),
123 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
124 canvas->clipRect(r, SkRegion::kIntersect_Op);
125
126 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
127 SkIntToScalar(w-10), SkIntToScalar(h-10));
128 canvas->clipRect(r, SkRegion::kXOR_Op);
129}
130
131static void performRotate(SkCanvas* canvas, int w, int h) {
132 const SkScalar x = SkIntToScalar(w) / 2;
133 const SkScalar y = SkIntToScalar(h) / 2;
134
135 canvas->translate(x, y);
136 canvas->rotate(SkIntToScalar(35));
137 canvas->translate(-x, -y);
138}
139
reed@android.com387359e2009-08-04 01:51:09 +0000140static void performScale(SkCanvas* canvas, int w, int h) {
141 const SkScalar x = SkIntToScalar(w) / 2;
142 const SkScalar y = SkIntToScalar(h) / 2;
143
144 canvas->translate(x, y);
145 // just enough so we can't take the sprite case
146 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
147 canvas->translate(-x, -y);
148}
149
reed@android.com29348cb2009-08-04 18:17:15 +0000150static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
151 if (argv < stop) {
152 *var = atoi(*argv) != 0;
153 return true;
154 }
155 return false;
156}
157
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000158enum Backend {
159 kRaster_Backend,
160 kGPU_Backend,
161 kPDF_Backend,
162};
163
164static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
165 Backend backend, GrContext* context) {
166 SkDevice* device = NULL;
167 SkBitmap bitmap;
168 bitmap.setConfig(config, size.fX, size.fY);
169
170 switch (backend) {
171 case kRaster_Backend:
172 bitmap.allocPixels();
173 erase(bitmap);
174 device = new SkDevice(NULL, bitmap, true);
175 break;
176 case kGPU_Backend:
177 device = new SkGpuDevice(context, bitmap, SkGpuDevice::Current3DApiRenderTarget());
178// device->clear(0xFFFFFFFF);
179 break;
180 case kPDF_Backend:
181 default:
182 SkASSERT(!"unsupported");
183 }
184 return device;
185}
186
reed@android.com4bc19832009-01-19 20:08:35 +0000187static const struct {
188 SkBitmap::Config fConfig;
189 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000190 Backend fBackend;
reed@android.com4bc19832009-01-19 20:08:35 +0000191} gConfigs[] = {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000192 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend },
193 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend },
194 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend },
reed@android.com4bc19832009-01-19 20:08:35 +0000195};
196
reed@android.com4c7d3d62009-01-21 03:15:13 +0000197static int findConfig(const char config[]) {
198 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
199 if (!strcmp(config, gConfigs[i].fName)) {
200 return i;
201 }
202 }
203 return -1;
204}
205
reed@android.combd700c32009-01-05 03:34:50 +0000206int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000207 SkAutoGraphics ag;
208
reed@android.come9d00602009-09-02 21:12:42 +0000209 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000210 int repeatDraw = 1;
211 int forceAlpha = 0xFF;
212 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000213 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000214 SkTriState::State forceDither = SkTriState::kDefault;
reed@android.com387359e2009-08-04 01:51:09 +0000215 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000216 bool doRotate = false;
217 bool doClip = false;
reed@android.com387359e2009-08-04 01:51:09 +0000218 const char* matchStr = NULL;
agl@chromium.org652807b2010-04-27 15:47:34 +0000219 bool hasStrokeWidth = false;
220 float strokeWidth;
reed@android.com4bc19832009-01-19 20:08:35 +0000221
reed@android.comb398fe82009-01-07 11:47:57 +0000222 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000223 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
224 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000225 Backend backend = kRaster_Backend; // for warning
reed@android.com387359e2009-08-04 01:51:09 +0000226 int configCount = SK_ARRAY_COUNT(gConfigs);
reed@android.combd700c32009-01-05 03:34:50 +0000227
reed@android.comb398fe82009-01-07 11:47:57 +0000228 char* const* stop = argv + argc;
229 for (++argv; argv < stop; ++argv) {
230 if (strcmp(*argv, "-o") == 0) {
231 argv++;
232 if (argv < stop && **argv) {
233 outDir.set(*argv);
234 if (outDir.c_str()[outDir.size() - 1] != '/') {
235 outDir.append("/");
236 }
237 }
reed@android.com4bc19832009-01-19 20:08:35 +0000238 } else if (strcmp(*argv, "-repeat") == 0) {
239 argv++;
240 if (argv < stop) {
241 repeatDraw = atoi(*argv);
242 if (repeatDraw < 1) {
243 repeatDraw = 1;
244 }
245 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000246 log_error("missing arg for -repeat\n");
reed@android.com4bc19832009-01-19 20:08:35 +0000247 return -1;
248 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000249 } else if (!strcmp(*argv, "-rotate")) {
250 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000251 } else if (!strcmp(*argv, "-scale")) {
252 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000253 } else if (!strcmp(*argv, "-clip")) {
254 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000255 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000256 if (!parse_bool_arg(++argv, stop, &forceAA)) {
257 log_error("missing arg for -forceAA\n");
258 return -1;
259 }
260 } else if (strcmp(*argv, "-forceFilter") == 0) {
261 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
262 log_error("missing arg for -forceFilter\n");
263 return -1;
264 }
reed@android.com4e635f92009-10-19 17:39:46 +0000265 } else if (strcmp(*argv, "-forceDither") == 0) {
266 bool tmp;
267 if (!parse_bool_arg(++argv, stop, &tmp)) {
268 log_error("missing arg for -forceDither\n");
269 return -1;
270 }
271 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000272 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000273 bool wantAlpha = false;
274 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
275 log_error("missing arg for -forceBlend\n");
276 return -1;
277 }
278 forceAlpha = wantAlpha ? 0x80 : 0xFF;
agl@chromium.org652807b2010-04-27 15:47:34 +0000279 } else if (strcmp(*argv, "-strokeWidth") == 0) {
280 argv++;
281 if (argv < stop) {
282 const char *strokeWidthStr = *argv;
283 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
284 log_error("bad arg for -strokeWidth\n");
285 return -1;
286 }
287 hasStrokeWidth = true;
288 } else {
289 log_error("missing arg for -strokeWidth\n");
290 return -1;
291 }
reed@android.com387359e2009-08-04 01:51:09 +0000292 } else if (strcmp(*argv, "-match") == 0) {
293 argv++;
294 if (argv < stop) {
295 matchStr = *argv;
296 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000297 log_error("missing arg for -match\n");
reed@android.com387359e2009-08-04 01:51:09 +0000298 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000299 }
reed@android.com387359e2009-08-04 01:51:09 +0000300 } else if (strcmp(*argv, "-config") == 0) {
301 argv++;
302 if (argv < stop) {
303 int index = findConfig(*argv);
304 if (index >= 0) {
305 outConfig = gConfigs[index].fConfig;
306 configName = gConfigs[index].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000307 backend = gConfigs[index].fBackend;
reed@android.com387359e2009-08-04 01:51:09 +0000308 configCount = 1;
309 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000310 SkString str;
311 str.printf("unrecognized config %s\n", *argv);
312 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000313 return -1;
314 }
315 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000316 log_error("missing arg for -config\n");
reed@android.com387359e2009-08-04 01:51:09 +0000317 return -1;
318 }
reed@android.com0c9da392010-02-22 19:50:13 +0000319 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000320 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000321 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000322 defineDict.set(argv[-1] + 2, *argv);
323 } else {
324 log_error("incomplete '-Dfoo bar' definition\n");
325 return -1;
326 }
reed@android.com387359e2009-08-04 01:51:09 +0000327 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000328 SkString str;
329 str.printf("unrecognized arg %s\n", *argv);
330 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000331 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000332 }
333 }
reed@android.com387359e2009-08-04 01:51:09 +0000334
reed@google.com3b14dc12011-04-04 14:31:36 +0000335 // report our current settings
336 {
337 SkString str;
338 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d\n",
339 forceAlpha, forceAA, forceFilter);
340 log_progress(str);
341 }
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000342
343 GrContext* context = NULL;
344 SkEGLContext eglContext;
345 if (eglContext.init(1024, 1024)) {
346 context = GrContext::CreateGLShaderContext();
347 }
348
reed@android.come9d00602009-09-02 21:12:42 +0000349 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000350 SkBenchmark* bench;
351 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000352 SkIPoint dim = bench->getSize();
353 if (dim.fX <= 0 || dim.fY <= 0) {
354 continue;
355 }
reed@android.comb398fe82009-01-07 11:47:57 +0000356
reed@android.com4bc19832009-01-19 20:08:35 +0000357 bench->setForceAlpha(forceAlpha);
358 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000359 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000360 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000361 if (hasStrokeWidth) {
362 bench->setStrokeWidth(strokeWidth);
363 }
reed@android.com4bc19832009-01-19 20:08:35 +0000364
reed@android.com387359e2009-08-04 01:51:09 +0000365 // only run benchmarks if their name contains matchStr
366 if (matchStr && strstr(bench->getName(), matchStr) == NULL) {
367 continue;
368 }
369
reed@android.com29348cb2009-08-04 18:17:15 +0000370 {
371 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000372 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000373 bench->getName());
reed@android.com29348cb2009-08-04 18:17:15 +0000374 log_progress(str);
375 }
reed@android.com4bc19832009-01-19 20:08:35 +0000376
377 for (int configIndex = 0; configIndex < configCount; configIndex++) {
378 if (configCount > 1) {
379 outConfig = gConfigs[configIndex].fConfig;
380 configName = gConfigs[configIndex].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000381 backend = gConfigs[configIndex].fBackend;
reed@android.com4bc19832009-01-19 20:08:35 +0000382 }
reed@android.comf523e252009-01-26 23:15:37 +0000383
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000384 if (kGPU_Backend == backend && NULL == context) {
385 continue;
386 }
387
388 SkDevice* device = make_device(outConfig, dim, backend, context);
389 SkCanvas canvas(device);
390 device->unref();
reed@android.comf523e252009-01-26 23:15:37 +0000391
reed@android.com4c7d3d62009-01-21 03:15:13 +0000392 if (doClip) {
393 performClip(&canvas, dim.fX, dim.fY);
394 }
reed@android.com387359e2009-08-04 01:51:09 +0000395 if (doScale) {
396 performScale(&canvas, dim.fX, dim.fY);
397 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000398 if (doRotate) {
399 performRotate(&canvas, dim.fX, dim.fY);
400 }
401
reed@android.comeca48362010-12-20 18:34:17 +0000402 if (repeatDraw > 1) {
403 SkAutoCanvasRestore acr(&canvas, true);
404 bench->draw(&canvas);
405 }
406
reed@android.com4bc19832009-01-19 20:08:35 +0000407 SkMSec now = SkTime::GetMSecs();
408 for (int i = 0; i < repeatDraw; i++) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000409 SkAutoCanvasRestore acr(&canvas, true);
410 bench->draw(&canvas);
reed@android.com4bc19832009-01-19 20:08:35 +0000411 }
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000412 if (context) {
413 context->flush();
414 }
415
reed@android.com4bc19832009-01-19 20:08:35 +0000416 if (repeatDraw > 1) {
reed@android.com831f6c62010-02-22 22:03:06 +0000417 double duration = SkTime::GetMSecs() - now;
reed@android.com29348cb2009-08-04 18:17:15 +0000418 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000419 str.printf(" %4s: msecs = %5.2f", configName, duration / repeatDraw);
reed@android.com29348cb2009-08-04 18:17:15 +0000420 log_progress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000421 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000422 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000423 saveFile(bench->getName(), configName, outDir.c_str(),
424 device->accessBitmap(false));
reed@android.com4c7d3d62009-01-21 03:15:13 +0000425 }
reed@android.com4bc19832009-01-19 20:08:35 +0000426 }
reed@android.com29348cb2009-08-04 18:17:15 +0000427 log_progress("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000428 }
429
430 return 0;
431}