blob: ec4f3a295186eacfaa266bbb0e4077e3a4cf3f74 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00008
9
10#include "BenchTimer.h"
11
12#include "GrContext.h"
13#include "GrRenderTarget.h"
14
15#include "SkBenchmark.h"
reed@android.combd700c32009-01-05 03:34:50 +000016#include "SkCanvas.h"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +000017#include "SkDeferredCanvas.h"
reed@android.comf523e252009-01-26 23:15:37 +000018#include "SkColorPriv.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000019#include "SkGpuDevice.h"
reed@android.com3a859a02009-01-28 00:56:29 +000020#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000021#include "SkImageEncoder.h"
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000022#if SK_ANGLE
23#include "gl/SkANGLEGLContext.h"
24#endif
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000025#include "gl/SkNativeGLContext.h"
26#include "gl/SkNullGLContext.h"
robertphillips@google.com0da37192012-03-19 14:42:13 +000027#include "gl/SkDebugGLContext.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000028#include "SkNWayCanvas.h"
29#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +000030#include "SkString.h"
reed@android.combd700c32009-01-05 03:34:50 +000031
djsollen@google.com56c69772011-11-08 19:00:26 +000032#ifdef SK_BUILD_FOR_ANDROID
reed@android.com29348cb2009-08-04 18:17:15 +000033static void log_error(const char msg[]) { SkDebugf("%s", msg); }
34static void log_progress(const char msg[]) { SkDebugf("%s", msg); }
35#else
36static void log_error(const char msg[]) { fprintf(stderr, "%s", msg); }
37static void log_progress(const char msg[]) { printf("%s", msg); }
38#endif
39
40static void log_error(const SkString& str) { log_error(str.c_str()); }
41static void log_progress(const SkString& str) { log_progress(str.c_str()); }
42
43///////////////////////////////////////////////////////////////////////////////
44
reed@android.com6c924ad2009-03-31 03:48:49 +000045static void erase(SkBitmap& bm) {
46 if (bm.config() == SkBitmap::kA8_Config) {
47 bm.eraseColor(0);
48 } else {
49 bm.eraseColor(SK_ColorWHITE);
50 }
51}
52
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000053#if 0
reed@android.com6c924ad2009-03-31 03:48:49 +000054static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
55 if (bm1.width() != bm2.width() ||
56 bm1.height() != bm2.height() ||
57 bm1.config() != bm2.config()) {
58 return false;
59 }
60
61 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
62 for (int y = 0; y < bm1.height(); y++) {
63 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
64 return false;
65 }
66 }
reed@android.com6c924ad2009-03-31 03:48:49 +000067 return true;
68}
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000069#endif
reed@android.com6c924ad2009-03-31 03:48:49 +000070
reed@android.combd700c32009-01-05 03:34:50 +000071class Iter {
72public:
reed@android.come9d00602009-09-02 21:12:42 +000073 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +000074 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +000075 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +000076 }
77
78 SkBenchmark* next() {
79 if (fBench) {
80 BenchRegistry::Factory f = fBench->factory();
81 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +000082 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +000083 }
84 return NULL;
85 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000086
reed@android.combd700c32009-01-05 03:34:50 +000087private:
88 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +000089 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +000090};
91
92static void make_filename(const char name[], SkString* path) {
93 path->set(name);
94 for (int i = 0; name[i]; i++) {
95 switch (name[i]) {
96 case '/':
97 case '\\':
98 case ' ':
99 case ':':
100 path->writable_str()[i] = '-';
101 break;
102 default:
103 break;
104 }
105 }
106}
107
reed@android.com4c7d3d62009-01-21 03:15:13 +0000108static void saveFile(const char name[], const char config[], const char dir[],
109 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000110 SkBitmap copy;
111 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
112 return;
113 }
reed@android.comf523e252009-01-26 23:15:37 +0000114
115 if (bm.config() == SkBitmap::kA8_Config) {
116 // turn alpha into gray-scale
117 size_t size = copy.getSize() >> 2;
118 SkPMColor* p = copy.getAddr32(0, 0);
119 for (size_t i = 0; i < size; i++) {
120 int c = (*p >> SK_A32_SHIFT) & 0xFF;
121 c = 255 - c;
122 c |= (c << 24) | (c << 16) | (c << 8);
123 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
124 }
125 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000126
reed@android.com4c7d3d62009-01-21 03:15:13 +0000127 SkString str;
128 make_filename(name, &str);
129 str.appendf("_%s.png", config);
130 str.prepend(dir);
131 ::remove(str.c_str());
132 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
133 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000134}
135
136static void performClip(SkCanvas* canvas, int w, int h) {
137 SkRect r;
138
139 r.set(SkIntToScalar(10), SkIntToScalar(10),
140 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
141 canvas->clipRect(r, SkRegion::kIntersect_Op);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000142
reed@android.com4c7d3d62009-01-21 03:15:13 +0000143 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
144 SkIntToScalar(w-10), SkIntToScalar(h-10));
145 canvas->clipRect(r, SkRegion::kXOR_Op);
146}
147
148static void performRotate(SkCanvas* canvas, int w, int h) {
149 const SkScalar x = SkIntToScalar(w) / 2;
150 const SkScalar y = SkIntToScalar(h) / 2;
151
152 canvas->translate(x, y);
153 canvas->rotate(SkIntToScalar(35));
154 canvas->translate(-x, -y);
155}
156
reed@android.com387359e2009-08-04 01:51:09 +0000157static void performScale(SkCanvas* canvas, int w, int h) {
158 const SkScalar x = SkIntToScalar(w) / 2;
159 const SkScalar y = SkIntToScalar(h) / 2;
160
161 canvas->translate(x, y);
162 // just enough so we can't take the sprite case
163 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
164 canvas->translate(-x, -y);
165}
166
reed@android.com29348cb2009-08-04 18:17:15 +0000167static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
168 if (argv < stop) {
169 *var = atoi(*argv) != 0;
170 return true;
171 }
172 return false;
173}
174
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000175enum Backend {
176 kRaster_Backend,
177 kGPU_Backend,
178 kPDF_Backend,
179};
180
bsalomon@google.com508824b2011-12-13 16:49:49 +0000181class GLHelper {
182public:
183 GLHelper() {
184 }
185
186 bool init(SkGLContext* glCtx, int width, int height) {
187 GrContext* grCtx;
188 GrRenderTarget* rt;
189 if (glCtx->init(width, height)) {
190 GrPlatform3DContext ctx =
191 reinterpret_cast<GrPlatform3DContext>(glCtx->gl());
192 grCtx = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
193 if (NULL != grCtx) {
194 GrPlatformRenderTargetDesc desc;
195 desc.fConfig = kSkia8888_PM_GrPixelConfig;
196 desc.fWidth = width;
197 desc.fHeight = height;
198 desc.fStencilBits = 8;
199 desc.fRenderTargetHandle = glCtx->getFBOID();
200 rt = grCtx->createPlatformRenderTarget(desc);
201 if (NULL == rt) {
202 grCtx->unref();
203 return false;
204 }
205 }
206 } else {
207 return false;
208 }
209 glCtx->ref();
210 fGLContext.reset(glCtx);
211 fGrContext.reset(grCtx);
212 fRenderTarget.reset(rt);
213 return true;
214 }
215
robertphillips@google.com0da37192012-03-19 14:42:13 +0000216 void cleanup() {
217 fGLContext.reset(NULL);
218 fGrContext.reset(NULL);
219 fRenderTarget.reset(NULL);
220 }
221
bsalomon@google.com508824b2011-12-13 16:49:49 +0000222 bool isValid() {
223 return NULL != fGLContext.get();
224 }
225
226 SkGLContext* glContext() {
227 return fGLContext.get();
228 }
229
230 GrRenderTarget* renderTarget() {
231 return fRenderTarget.get();
232 }
233
234 GrContext* grContext() {
235 return fGrContext.get();
236 }
237private:
238 SkAutoTUnref<SkGLContext> fGLContext;
239 SkAutoTUnref<GrContext> fGrContext;
240 SkAutoTUnref<GrRenderTarget> fRenderTarget;
241};
242
243static GLHelper gRealGLHelper;
244static GLHelper gNullGLHelper;
robertphillips@google.com0da37192012-03-19 14:42:13 +0000245static GLHelper gDebugGLHelper;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000246#if SK_ANGLE
247static GLHelper gANGLEGLHelper;
248#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000249
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000250static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.com508824b2011-12-13 16:49:49 +0000251 Backend backend, GLHelper* glHelper) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000252 SkDevice* device = NULL;
253 SkBitmap bitmap;
254 bitmap.setConfig(config, size.fX, size.fY);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000255
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000256 switch (backend) {
257 case kRaster_Backend:
258 bitmap.allocPixels();
259 erase(bitmap);
reed@google.comaf951c92011-06-16 19:10:39 +0000260 device = new SkDevice(bitmap);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000261 break;
262 case kGPU_Backend:
bsalomon@google.com508824b2011-12-13 16:49:49 +0000263 device = new SkGpuDevice(glHelper->grContext(),
264 glHelper->renderTarget());
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000265 break;
266 case kPDF_Backend:
267 default:
268 SkASSERT(!"unsupported");
269 }
270 return device;
271}
272
reed@android.com4bc19832009-01-19 20:08:35 +0000273static const struct {
274 SkBitmap::Config fConfig;
275 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000276 Backend fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000277 GLHelper* fGLHelper;
reed@android.com4bc19832009-01-19 20:08:35 +0000278} gConfigs[] = {
bsalomon@google.com508824b2011-12-13 16:49:49 +0000279 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend, NULL },
280 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend, NULL },
281 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend, &gRealGLHelper },
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000282#if SK_ANGLE
283 { SkBitmap::kARGB_8888_Config, "ANGLE", kGPU_Backend, &gANGLEGLHelper },
284#endif
robertphillips@google.coma0b63b82012-03-30 12:23:56 +0000285#ifdef SK_DEBUG
286 { SkBitmap::kARGB_8888_Config, "Debug", kGPU_Backend, &gDebugGLHelper },
287#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000288 { SkBitmap::kARGB_8888_Config, "NULLGPU", kGPU_Backend, &gNullGLHelper },
reed@android.com4bc19832009-01-19 20:08:35 +0000289};
290
reed@android.com4c7d3d62009-01-21 03:15:13 +0000291static int findConfig(const char config[]) {
292 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
293 if (!strcmp(config, gConfigs[i].fName)) {
294 return i;
295 }
296 }
297 return -1;
298}
299
tomhudson@google.com83823942011-10-28 14:59:07 +0000300static void determine_gpu_context_size(SkTDict<const char*>& defineDict,
301 int* contextWidth,
302 int* contextHeight) {
303 Iter iter(&defineDict);
304 SkBenchmark* bench;
305 while ((bench = iter.next()) != NULL) {
306 SkIPoint dim = bench->getSize();
307 if (*contextWidth < dim.fX) {
308 *contextWidth = dim.fX;
309 }
310 if (*contextHeight < dim.fY) {
311 *contextHeight = dim.fY;
312 }
313 }
314}
315
reed@google.com1a7eabc2011-10-31 16:33:52 +0000316static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
317 if (0 == array.count()) {
318 // no names, so don't skip anything
319 return false;
320 }
321 for (int i = 0; i < array.count(); ++i) {
322 if (strstr(name, array[i])) {
323 // found the name, so don't skip
324 return false;
325 }
326 }
327 return true;
328}
329
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000330static void help() {
331 SkDebugf("Usage: bench [-o outDir] [-repeat nr] "
332 "[-timers [wcg]*] [-rotate]\n"
333 " [-scale] [-clip] [-forceAA 1|0] [-forceFilter 1|0]\n"
334 " [-forceDither 1|0] [-forceBlend 1|0] [-strokeWidth width]\n"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000335 " [-forceDeferred 1|0] [-match name]\n"
336 " [-config 8888|565|GPU|ANGLE|NULLGPU] [-Dfoo bar]\n"
337 " [-h|--help]");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000338 SkDebugf("\n\n");
339 SkDebugf(" -o outDir : Image of each bench will be put in outDir.\n");
340 SkDebugf(" -repeat nr : Each bench repeats for nr times.\n");
341 SkDebugf(" -timers [wcg]* : "
342 "Display wall time, cpu time or gpu time for each bench.\n");
343 SkDebugf(" -rotate : Rotate before each bench runs.\n");
344 SkDebugf(" -scale : Scale before each bench runs.\n");
345 SkDebugf(" -clip : Clip before each bench runs.\n");
346 SkDebugf(" -forceAA 1|0 : "
347 "Enable/disable anti-aliased, default is enabled.\n");
348 SkDebugf(" -forceFilter 1|0 : "
349 "Enable/disable bitmap filtering, default is disabled.\n");
350 SkDebugf(" -forceDither 1|0 : "
351 "Enable/disable dithering, default is disabled.\n");
352 SkDebugf(" -forceBlend 1|0 : "
353 "Enable/disable dithering, default is disabled.\n");
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000354 SkDebugf(" -forceDeferred 1|0 : "
355 "Enable/disable deferred canvas, default is disabled.\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000356 SkDebugf(" -strokeWidth width : The width for path stroke.\n");
357 SkDebugf(" -match name : Only run bench whose name is matched.\n");
358 SkDebugf(" -config 8888|565|GPU|ANGLE|NULLGPU : "
359 "Run bench in corresponding config mode.\n");
360 SkDebugf(" -Dfoo bar : Add extra definition to bench.\n");
361 SkDebugf(" -h|--help : Show this help message.\n");
362}
363
reed@android.combd700c32009-01-05 03:34:50 +0000364int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000365 SkAutoGraphics ag;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000366
reed@android.come9d00602009-09-02 21:12:42 +0000367 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000368 int repeatDraw = 1;
369 int forceAlpha = 0xFF;
370 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000371 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000372 SkTriState::State forceDither = SkTriState::kDefault;
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000373 bool forceDeferred = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000374 bool timerWall = false;
375 bool timerCpu = true;
376 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000377 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000378 bool doRotate = false;
379 bool doClip = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000380 bool hasStrokeWidth = false;
381 float strokeWidth;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000382 SkTDArray<const char*> fMatches;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000383
reed@android.comb398fe82009-01-07 11:47:57 +0000384 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000385 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000386 GLHelper* glHelper = NULL;
reed@android.com387359e2009-08-04 01:51:09 +0000387 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000388 Backend backend = kRaster_Backend; // for warning
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000389 SkTDArray<int> configs;
390 bool userConfig = false;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000391
reed@android.comb398fe82009-01-07 11:47:57 +0000392 char* const* stop = argv + argc;
393 for (++argv; argv < stop; ++argv) {
394 if (strcmp(*argv, "-o") == 0) {
395 argv++;
396 if (argv < stop && **argv) {
397 outDir.set(*argv);
398 if (outDir.c_str()[outDir.size() - 1] != '/') {
399 outDir.append("/");
400 }
401 }
reed@android.com4bc19832009-01-19 20:08:35 +0000402 } else if (strcmp(*argv, "-repeat") == 0) {
403 argv++;
404 if (argv < stop) {
405 repeatDraw = atoi(*argv);
406 if (repeatDraw < 1) {
407 repeatDraw = 1;
408 }
409 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000410 log_error("missing arg for -repeat\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000411 help();
reed@android.com4bc19832009-01-19 20:08:35 +0000412 return -1;
413 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000414 } else if (strcmp(*argv, "-timers") == 0) {
415 argv++;
416 if (argv < stop) {
417 timerWall = false;
418 timerCpu = false;
419 timerGpu = false;
420 for (char* t = *argv; *t; ++t) {
421 switch (*t) {
422 case 'w': timerWall = true; break;
423 case 'c': timerCpu = true; break;
424 case 'g': timerGpu = true; break;
425 }
426 }
427 } else {
428 log_error("missing arg for -timers\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000429 help();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000430 return -1;
431 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000432 } else if (!strcmp(*argv, "-rotate")) {
433 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000434 } else if (!strcmp(*argv, "-scale")) {
435 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000436 } else if (!strcmp(*argv, "-clip")) {
437 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000438 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000439 if (!parse_bool_arg(++argv, stop, &forceAA)) {
440 log_error("missing arg for -forceAA\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000441 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000442 return -1;
443 }
444 } else if (strcmp(*argv, "-forceFilter") == 0) {
445 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
446 log_error("missing arg for -forceFilter\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000447 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000448 return -1;
449 }
reed@android.com4e635f92009-10-19 17:39:46 +0000450 } else if (strcmp(*argv, "-forceDither") == 0) {
451 bool tmp;
452 if (!parse_bool_arg(++argv, stop, &tmp)) {
453 log_error("missing arg for -forceDither\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000454 help();
reed@android.com4e635f92009-10-19 17:39:46 +0000455 return -1;
456 }
457 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000458 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000459 bool wantAlpha = false;
460 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
461 log_error("missing arg for -forceBlend\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000462 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000463 return -1;
464 }
465 forceAlpha = wantAlpha ? 0x80 : 0xFF;
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000466 } else if (strcmp(*argv, "-forceDeferred") == 0) {
467 if (!parse_bool_arg(++argv, stop, &forceDeferred)) {
468 log_error("missing arg for -forceDeferred\n");
469 help();
470 return -1;
471 }
agl@chromium.org652807b2010-04-27 15:47:34 +0000472 } else if (strcmp(*argv, "-strokeWidth") == 0) {
473 argv++;
474 if (argv < stop) {
475 const char *strokeWidthStr = *argv;
476 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
477 log_error("bad arg for -strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000478 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000479 return -1;
480 }
481 hasStrokeWidth = true;
482 } else {
483 log_error("missing arg for -strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000484 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000485 return -1;
486 }
reed@android.com387359e2009-08-04 01:51:09 +0000487 } else if (strcmp(*argv, "-match") == 0) {
488 argv++;
489 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000490 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000491 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000492 log_error("missing arg for -match\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000493 help();
reed@android.com387359e2009-08-04 01:51:09 +0000494 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000495 }
reed@android.com387359e2009-08-04 01:51:09 +0000496 } else if (strcmp(*argv, "-config") == 0) {
497 argv++;
498 if (argv < stop) {
499 int index = findConfig(*argv);
500 if (index >= 0) {
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000501 *configs.append() = index;
502 userConfig = true;
reed@android.com387359e2009-08-04 01:51:09 +0000503 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000504 SkString str;
505 str.printf("unrecognized config %s\n", *argv);
506 log_error(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000507 help();
reed@android.com387359e2009-08-04 01:51:09 +0000508 return -1;
509 }
510 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000511 log_error("missing arg for -config\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000512 help();
reed@android.com387359e2009-08-04 01:51:09 +0000513 return -1;
514 }
reed@android.com0c9da392010-02-22 19:50:13 +0000515 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000516 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000517 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000518 defineDict.set(argv[-1] + 2, *argv);
519 } else {
520 log_error("incomplete '-Dfoo bar' definition\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000521 help();
reed@android.come9d00602009-09-02 21:12:42 +0000522 return -1;
523 }
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000524 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
525 help();
526 return 0;
reed@android.com387359e2009-08-04 01:51:09 +0000527 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000528 SkString str;
529 str.printf("unrecognized arg %s\n", *argv);
530 log_error(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000531 help();
reed@android.com387359e2009-08-04 01:51:09 +0000532 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000533 }
534 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000535 if (!userConfig) {
536 // if no config is specified by user, we add them all.
537 for (unsigned int i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
538 *configs.append() = i;
539 }
540 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000541
reed@google.com3b14dc12011-04-04 14:31:36 +0000542 // report our current settings
543 {
544 SkString str;
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000545 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d deferred=%d",
546 forceAlpha, forceAA, forceFilter, forceDeferred);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000547 str.appendf(" rotate=%d scale=%d clip=%d",
548 doRotate, doScale, doClip);
549
550 const char * ditherName;
551 switch (forceDither) {
552 case SkTriState::kDefault: ditherName = "default"; break;
553 case SkTriState::kTrue: ditherName = "true"; break;
554 case SkTriState::kFalse: ditherName = "false"; break;
555 default: ditherName = "<invalid>"; break;
556 }
557 str.appendf(" dither=%s", ditherName);
558
559 if (hasStrokeWidth) {
560 str.appendf(" strokeWidth=%f", strokeWidth);
561 } else {
562 str.append(" strokeWidth=none");
563 }
564
565#if defined(SK_SCALAR_IS_FLOAT)
566 str.append(" scalar=float");
567#elif defined(SK_SCALAR_IS_FIXED)
568 str.append(" scalar=fixed");
569#endif
570
571#if defined(SK_BUILD_FOR_WIN32)
572 str.append(" system=WIN32");
573#elif defined(SK_BUILD_FOR_MAC)
574 str.append(" system=MAC");
575#elif defined(SK_BUILD_FOR_ANDROID)
576 str.append(" system=ANDROID");
577#elif defined(SK_BUILD_FOR_UNIX)
578 str.append(" system=UNIX");
579#else
580 str.append(" system=other");
581#endif
582
583#if defined(SK_DEBUG)
584 str.append(" DEBUG");
585#endif
586 str.append("\n");
reed@google.com3b14dc12011-04-04 14:31:36 +0000587 log_progress(str);
588 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000589
bungeman@google.coma5d48412011-06-15 17:25:46 +0000590 //Don't do GL when fixed.
591#if !defined(SK_SCALAR_IS_FIXED)
tomhudson@google.com83823942011-10-28 14:59:07 +0000592 int contextWidth = 1024;
593 int contextHeight = 1024;
594 determine_gpu_context_size(defineDict, &contextWidth, &contextHeight);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000595 SkAutoTUnref<SkGLContext> realGLCtx(new SkNativeGLContext);
596 SkAutoTUnref<SkGLContext> nullGLCtx(new SkNullGLContext);
robertphillips@google.com0da37192012-03-19 14:42:13 +0000597 SkAutoTUnref<SkGLContext> debugGLCtx(new SkDebugGLContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000598#if SK_ANGLE
599 SkAutoTUnref<SkGLContext> angleGLCtx(new SkANGLEGLContext);
600#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000601 gRealGLHelper.init(realGLCtx.get(), contextWidth, contextHeight);
602 gNullGLHelper.init(nullGLCtx.get(), contextWidth, contextHeight);
robertphillips@google.com0da37192012-03-19 14:42:13 +0000603 gDebugGLHelper.init(debugGLCtx.get(), contextWidth, contextHeight);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000604#if SK_ANGLE
605 gANGLEGLHelper.init(angleGLCtx.get(), contextWidth, contextHeight);
606#endif
bungeman@google.coma5d48412011-06-15 17:25:46 +0000607#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000608 BenchTimer timer = BenchTimer(gRealGLHelper.glContext());
bsalomon@google.com74913722011-10-27 20:44:19 +0000609
reed@android.come9d00602009-09-02 21:12:42 +0000610 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000611 SkBenchmark* bench;
612 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000613 SkIPoint dim = bench->getSize();
614 if (dim.fX <= 0 || dim.fY <= 0) {
615 continue;
616 }
reed@android.comb398fe82009-01-07 11:47:57 +0000617
reed@android.com4bc19832009-01-19 20:08:35 +0000618 bench->setForceAlpha(forceAlpha);
619 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000620 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000621 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000622 if (hasStrokeWidth) {
623 bench->setStrokeWidth(strokeWidth);
624 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000625
reed@android.com387359e2009-08-04 01:51:09 +0000626 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000627 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000628 continue;
629 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000630
reed@android.com29348cb2009-08-04 18:17:15 +0000631 {
632 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000633 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000634 bench->getName());
reed@android.com29348cb2009-08-04 18:17:15 +0000635 log_progress(str);
636 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000637
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000638 for (int x = 0; x < configs.count(); ++x) {
639 int configIndex = configs[x];
640
641 outConfig = gConfigs[configIndex].fConfig;
642 configName = gConfigs[configIndex].fName;
643 backend = gConfigs[configIndex].fBackend;
644 glHelper = gConfigs[configIndex].fGLHelper;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000645
646 if (kGPU_Backend == backend &&
647 (NULL == glHelper || !glHelper->isValid())) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000648 continue;
649 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000650
bsalomon@google.com508824b2011-12-13 16:49:49 +0000651 SkDevice* device = make_device(outConfig, dim, backend, glHelper);
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000652 SkCanvas* canvas;
653 if (forceDeferred) {
654 canvas = new SkDeferredCanvas(device);
655 } else {
656 canvas = new SkCanvas(device);
657 }
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000658 device->unref();
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000659 SkAutoUnref canvasUnref(canvas);
660
reed@android.com4c7d3d62009-01-21 03:15:13 +0000661 if (doClip) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000662 performClip(canvas, dim.fX, dim.fY);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000663 }
reed@android.com387359e2009-08-04 01:51:09 +0000664 if (doScale) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000665 performScale(canvas, dim.fX, dim.fY);
reed@android.com387359e2009-08-04 01:51:09 +0000666 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000667 if (doRotate) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000668 performRotate(canvas, dim.fX, dim.fY);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000669 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000670
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000671 //warm up caches if needed
reed@android.comeca48362010-12-20 18:34:17 +0000672 if (repeatDraw > 1) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000673 SkAutoCanvasRestore acr(canvas, true);
674 bench->draw(canvas);
675 canvas->flush();
bsalomon@google.com508824b2011-12-13 16:49:49 +0000676 if (glHelper) {
677 glHelper->grContext()->flush();
678 SK_GL(*glHelper->glContext(), Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000679 }
reed@android.comeca48362010-12-20 18:34:17 +0000680 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000681
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000682 timer.start();
reed@android.com4bc19832009-01-19 20:08:35 +0000683 for (int i = 0; i < repeatDraw; i++) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000684 SkAutoCanvasRestore acr(canvas, true);
685 bench->draw(canvas);
686 canvas->flush();
bsalomon@google.com508824b2011-12-13 16:49:49 +0000687 if (glHelper) {
688 glHelper->grContext()->flush();
reed@google.com25df8882011-07-14 19:03:58 +0000689 }
690 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000691 if (glHelper) {
692 SK_GL(*glHelper->glContext(), Finish());
bsalomon@google.com788e2472011-10-19 20:56:30 +0000693 }
bsalomon@google.com788e2472011-10-19 20:56:30 +0000694 timer.end();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000695
reed@android.com4bc19832009-01-19 20:08:35 +0000696 if (repeatDraw > 1) {
reed@android.com29348cb2009-08-04 18:17:15 +0000697 SkString str;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000698 str.printf(" %4s:", configName);
699 if (timerWall) {
700 str.appendf(" msecs = %6.2f", timer.fWall / repeatDraw);
701 }
702 if (timerCpu) {
703 str.appendf(" cmsecs = %6.2f", timer.fCpu / repeatDraw);
704 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000705 if (timerGpu && glHelper && timer.fGpu > 0) {
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000706 str.appendf(" gmsecs = %6.2f", timer.fGpu / repeatDraw);
707 }
reed@android.com29348cb2009-08-04 18:17:15 +0000708 log_progress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000709 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000710 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000711 saveFile(bench->getName(), configName, outDir.c_str(),
712 device->accessBitmap(false));
reed@android.com4c7d3d62009-01-21 03:15:13 +0000713 }
reed@android.com4bc19832009-01-19 20:08:35 +0000714 }
reed@android.com29348cb2009-08-04 18:17:15 +0000715 log_progress("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000716 }
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000717
robertphillips@google.com0da37192012-03-19 14:42:13 +0000718 // need to clean up here rather than post-main to allow leak detection to work
719 gDebugGLHelper.cleanup();
720
reed@android.combd700c32009-01-05 03:34:50 +0000721 return 0;
722}