blob: 4f6827dd07b0d51514005fc79d24e1fc9dba8d67 [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"
reed@android.comf523e252009-01-26 23:15:37 +000017#include "SkColorPriv.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000018#include "SkGpuDevice.h"
reed@android.com3a859a02009-01-28 00:56:29 +000019#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000020#include "SkImageEncoder.h"
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000021#if SK_ANGLE
22#include "gl/SkANGLEGLContext.h"
23#endif
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000024#include "gl/SkNativeGLContext.h"
25#include "gl/SkNullGLContext.h"
robertphillips@google.com0da37192012-03-19 14:42:13 +000026#include "gl/SkDebugGLContext.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000027#include "SkNWayCanvas.h"
28#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +000029#include "SkString.h"
reed@android.combd700c32009-01-05 03:34:50 +000030
djsollen@google.com56c69772011-11-08 19:00:26 +000031#ifdef SK_BUILD_FOR_ANDROID
reed@android.com29348cb2009-08-04 18:17:15 +000032static void log_error(const char msg[]) { SkDebugf("%s", msg); }
33static void log_progress(const char msg[]) { SkDebugf("%s", msg); }
34#else
35static void log_error(const char msg[]) { fprintf(stderr, "%s", msg); }
36static void log_progress(const char msg[]) { printf("%s", msg); }
37#endif
38
39static void log_error(const SkString& str) { log_error(str.c_str()); }
40static void log_progress(const SkString& str) { log_progress(str.c_str()); }
41
42///////////////////////////////////////////////////////////////////////////////
43
reed@android.com6c924ad2009-03-31 03:48:49 +000044static void erase(SkBitmap& bm) {
45 if (bm.config() == SkBitmap::kA8_Config) {
46 bm.eraseColor(0);
47 } else {
48 bm.eraseColor(SK_ColorWHITE);
49 }
50}
51
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000052#if 0
reed@android.com6c924ad2009-03-31 03:48:49 +000053static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
54 if (bm1.width() != bm2.width() ||
55 bm1.height() != bm2.height() ||
56 bm1.config() != bm2.config()) {
57 return false;
58 }
59
60 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
61 for (int y = 0; y < bm1.height(); y++) {
62 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
63 return false;
64 }
65 }
reed@android.com6c924ad2009-03-31 03:48:49 +000066 return true;
67}
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000068#endif
reed@android.com6c924ad2009-03-31 03:48:49 +000069
reed@android.combd700c32009-01-05 03:34:50 +000070class Iter {
71public:
reed@android.come9d00602009-09-02 21:12:42 +000072 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +000073 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +000074 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +000075 }
76
77 SkBenchmark* next() {
78 if (fBench) {
79 BenchRegistry::Factory f = fBench->factory();
80 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +000081 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +000082 }
83 return NULL;
84 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000085
reed@android.combd700c32009-01-05 03:34:50 +000086private:
87 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +000088 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +000089};
90
91static void make_filename(const char name[], SkString* path) {
92 path->set(name);
93 for (int i = 0; name[i]; i++) {
94 switch (name[i]) {
95 case '/':
96 case '\\':
97 case ' ':
98 case ':':
99 path->writable_str()[i] = '-';
100 break;
101 default:
102 break;
103 }
104 }
105}
106
reed@android.com4c7d3d62009-01-21 03:15:13 +0000107static void saveFile(const char name[], const char config[], const char dir[],
108 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000109 SkBitmap copy;
110 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
111 return;
112 }
reed@android.comf523e252009-01-26 23:15:37 +0000113
114 if (bm.config() == SkBitmap::kA8_Config) {
115 // turn alpha into gray-scale
116 size_t size = copy.getSize() >> 2;
117 SkPMColor* p = copy.getAddr32(0, 0);
118 for (size_t i = 0; i < size; i++) {
119 int c = (*p >> SK_A32_SHIFT) & 0xFF;
120 c = 255 - c;
121 c |= (c << 24) | (c << 16) | (c << 8);
122 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
123 }
124 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000125
reed@android.com4c7d3d62009-01-21 03:15:13 +0000126 SkString str;
127 make_filename(name, &str);
128 str.appendf("_%s.png", config);
129 str.prepend(dir);
130 ::remove(str.c_str());
131 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
132 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000133}
134
135static void performClip(SkCanvas* canvas, int w, int h) {
136 SkRect r;
137
138 r.set(SkIntToScalar(10), SkIntToScalar(10),
139 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
140 canvas->clipRect(r, SkRegion::kIntersect_Op);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000141
reed@android.com4c7d3d62009-01-21 03:15:13 +0000142 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
143 SkIntToScalar(w-10), SkIntToScalar(h-10));
144 canvas->clipRect(r, SkRegion::kXOR_Op);
145}
146
147static void performRotate(SkCanvas* canvas, int w, int h) {
148 const SkScalar x = SkIntToScalar(w) / 2;
149 const SkScalar y = SkIntToScalar(h) / 2;
150
151 canvas->translate(x, y);
152 canvas->rotate(SkIntToScalar(35));
153 canvas->translate(-x, -y);
154}
155
reed@android.com387359e2009-08-04 01:51:09 +0000156static void performScale(SkCanvas* canvas, int w, int h) {
157 const SkScalar x = SkIntToScalar(w) / 2;
158 const SkScalar y = SkIntToScalar(h) / 2;
159
160 canvas->translate(x, y);
161 // just enough so we can't take the sprite case
162 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
163 canvas->translate(-x, -y);
164}
165
reed@android.com29348cb2009-08-04 18:17:15 +0000166static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
167 if (argv < stop) {
168 *var = atoi(*argv) != 0;
169 return true;
170 }
171 return false;
172}
173
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000174enum Backend {
175 kRaster_Backend,
176 kGPU_Backend,
177 kPDF_Backend,
178};
179
bsalomon@google.com508824b2011-12-13 16:49:49 +0000180class GLHelper {
181public:
182 GLHelper() {
183 }
184
185 bool init(SkGLContext* glCtx, int width, int height) {
186 GrContext* grCtx;
187 GrRenderTarget* rt;
188 if (glCtx->init(width, height)) {
189 GrPlatform3DContext ctx =
190 reinterpret_cast<GrPlatform3DContext>(glCtx->gl());
191 grCtx = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
192 if (NULL != grCtx) {
193 GrPlatformRenderTargetDesc desc;
194 desc.fConfig = kSkia8888_PM_GrPixelConfig;
195 desc.fWidth = width;
196 desc.fHeight = height;
197 desc.fStencilBits = 8;
198 desc.fRenderTargetHandle = glCtx->getFBOID();
199 rt = grCtx->createPlatformRenderTarget(desc);
200 if (NULL == rt) {
201 grCtx->unref();
202 return false;
203 }
204 }
205 } else {
206 return false;
207 }
208 glCtx->ref();
209 fGLContext.reset(glCtx);
210 fGrContext.reset(grCtx);
211 fRenderTarget.reset(rt);
212 return true;
213 }
214
robertphillips@google.com0da37192012-03-19 14:42:13 +0000215 void cleanup() {
216 fGLContext.reset(NULL);
217 fGrContext.reset(NULL);
218 fRenderTarget.reset(NULL);
219 }
220
bsalomon@google.com508824b2011-12-13 16:49:49 +0000221 bool isValid() {
222 return NULL != fGLContext.get();
223 }
224
225 SkGLContext* glContext() {
226 return fGLContext.get();
227 }
228
229 GrRenderTarget* renderTarget() {
230 return fRenderTarget.get();
231 }
232
233 GrContext* grContext() {
234 return fGrContext.get();
235 }
236private:
237 SkAutoTUnref<SkGLContext> fGLContext;
238 SkAutoTUnref<GrContext> fGrContext;
239 SkAutoTUnref<GrRenderTarget> fRenderTarget;
240};
241
242static GLHelper gRealGLHelper;
243static GLHelper gNullGLHelper;
robertphillips@google.com0da37192012-03-19 14:42:13 +0000244static GLHelper gDebugGLHelper;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000245#if SK_ANGLE
246static GLHelper gANGLEGLHelper;
247#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000248
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000249static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.com508824b2011-12-13 16:49:49 +0000250 Backend backend, GLHelper* glHelper) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000251 SkDevice* device = NULL;
252 SkBitmap bitmap;
253 bitmap.setConfig(config, size.fX, size.fY);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000254
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000255 switch (backend) {
256 case kRaster_Backend:
257 bitmap.allocPixels();
258 erase(bitmap);
reed@google.comaf951c92011-06-16 19:10:39 +0000259 device = new SkDevice(bitmap);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000260 break;
261 case kGPU_Backend:
bsalomon@google.com508824b2011-12-13 16:49:49 +0000262 device = new SkGpuDevice(glHelper->grContext(),
263 glHelper->renderTarget());
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000264 break;
265 case kPDF_Backend:
266 default:
267 SkASSERT(!"unsupported");
268 }
269 return device;
270}
271
reed@android.com4bc19832009-01-19 20:08:35 +0000272static const struct {
273 SkBitmap::Config fConfig;
274 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000275 Backend fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000276 GLHelper* fGLHelper;
reed@android.com4bc19832009-01-19 20:08:35 +0000277} gConfigs[] = {
bsalomon@google.com508824b2011-12-13 16:49:49 +0000278 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend, NULL },
279 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend, NULL },
280 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend, &gRealGLHelper },
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000281#if SK_ANGLE
282 { SkBitmap::kARGB_8888_Config, "ANGLE", kGPU_Backend, &gANGLEGLHelper },
283#endif
robertphillips@google.coma0b63b82012-03-30 12:23:56 +0000284#ifdef SK_DEBUG
285 { SkBitmap::kARGB_8888_Config, "Debug", kGPU_Backend, &gDebugGLHelper },
286#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000287 { SkBitmap::kARGB_8888_Config, "NULLGPU", kGPU_Backend, &gNullGLHelper },
reed@android.com4bc19832009-01-19 20:08:35 +0000288};
289
reed@android.com4c7d3d62009-01-21 03:15:13 +0000290static int findConfig(const char config[]) {
291 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
292 if (!strcmp(config, gConfigs[i].fName)) {
293 return i;
294 }
295 }
296 return -1;
297}
298
tomhudson@google.com83823942011-10-28 14:59:07 +0000299static void determine_gpu_context_size(SkTDict<const char*>& defineDict,
300 int* contextWidth,
301 int* contextHeight) {
302 Iter iter(&defineDict);
303 SkBenchmark* bench;
304 while ((bench = iter.next()) != NULL) {
305 SkIPoint dim = bench->getSize();
306 if (*contextWidth < dim.fX) {
307 *contextWidth = dim.fX;
308 }
309 if (*contextHeight < dim.fY) {
310 *contextHeight = dim.fY;
311 }
312 }
313}
314
reed@google.com1a7eabc2011-10-31 16:33:52 +0000315static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
316 if (0 == array.count()) {
317 // no names, so don't skip anything
318 return false;
319 }
320 for (int i = 0; i < array.count(); ++i) {
321 if (strstr(name, array[i])) {
322 // found the name, so don't skip
323 return false;
324 }
325 }
326 return true;
327}
328
reed@android.combd700c32009-01-05 03:34:50 +0000329int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000330 SkAutoGraphics ag;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000331
reed@android.come9d00602009-09-02 21:12:42 +0000332 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000333 int repeatDraw = 1;
334 int forceAlpha = 0xFF;
335 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000336 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000337 SkTriState::State forceDither = SkTriState::kDefault;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000338 bool timerWall = false;
339 bool timerCpu = true;
340 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000341 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000342 bool doRotate = false;
343 bool doClip = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000344 bool hasStrokeWidth = false;
345 float strokeWidth;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000346 SkTDArray<const char*> fMatches;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000347
reed@android.comb398fe82009-01-07 11:47:57 +0000348 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000349 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000350 GLHelper* glHelper = NULL;
reed@android.com387359e2009-08-04 01:51:09 +0000351 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000352 Backend backend = kRaster_Backend; // for warning
reed@android.com387359e2009-08-04 01:51:09 +0000353 int configCount = SK_ARRAY_COUNT(gConfigs);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000354
reed@android.comb398fe82009-01-07 11:47:57 +0000355 char* const* stop = argv + argc;
356 for (++argv; argv < stop; ++argv) {
357 if (strcmp(*argv, "-o") == 0) {
358 argv++;
359 if (argv < stop && **argv) {
360 outDir.set(*argv);
361 if (outDir.c_str()[outDir.size() - 1] != '/') {
362 outDir.append("/");
363 }
364 }
reed@android.com4bc19832009-01-19 20:08:35 +0000365 } else if (strcmp(*argv, "-repeat") == 0) {
366 argv++;
367 if (argv < stop) {
368 repeatDraw = atoi(*argv);
369 if (repeatDraw < 1) {
370 repeatDraw = 1;
371 }
372 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000373 log_error("missing arg for -repeat\n");
reed@android.com4bc19832009-01-19 20:08:35 +0000374 return -1;
375 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000376 } else if (strcmp(*argv, "-timers") == 0) {
377 argv++;
378 if (argv < stop) {
379 timerWall = false;
380 timerCpu = false;
381 timerGpu = false;
382 for (char* t = *argv; *t; ++t) {
383 switch (*t) {
384 case 'w': timerWall = true; break;
385 case 'c': timerCpu = true; break;
386 case 'g': timerGpu = true; break;
387 }
388 }
389 } else {
390 log_error("missing arg for -timers\n");
391 return -1;
392 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000393 } else if (!strcmp(*argv, "-rotate")) {
394 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000395 } else if (!strcmp(*argv, "-scale")) {
396 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000397 } else if (!strcmp(*argv, "-clip")) {
398 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000399 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000400 if (!parse_bool_arg(++argv, stop, &forceAA)) {
401 log_error("missing arg for -forceAA\n");
402 return -1;
403 }
404 } else if (strcmp(*argv, "-forceFilter") == 0) {
405 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
406 log_error("missing arg for -forceFilter\n");
407 return -1;
408 }
reed@android.com4e635f92009-10-19 17:39:46 +0000409 } else if (strcmp(*argv, "-forceDither") == 0) {
410 bool tmp;
411 if (!parse_bool_arg(++argv, stop, &tmp)) {
412 log_error("missing arg for -forceDither\n");
413 return -1;
414 }
415 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000416 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000417 bool wantAlpha = false;
418 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
419 log_error("missing arg for -forceBlend\n");
420 return -1;
421 }
422 forceAlpha = wantAlpha ? 0x80 : 0xFF;
agl@chromium.org652807b2010-04-27 15:47:34 +0000423 } else if (strcmp(*argv, "-strokeWidth") == 0) {
424 argv++;
425 if (argv < stop) {
426 const char *strokeWidthStr = *argv;
427 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
428 log_error("bad arg for -strokeWidth\n");
429 return -1;
430 }
431 hasStrokeWidth = true;
432 } else {
433 log_error("missing arg for -strokeWidth\n");
434 return -1;
435 }
reed@android.com387359e2009-08-04 01:51:09 +0000436 } else if (strcmp(*argv, "-match") == 0) {
437 argv++;
438 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000439 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000440 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000441 log_error("missing arg for -match\n");
reed@android.com387359e2009-08-04 01:51:09 +0000442 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000443 }
reed@android.com387359e2009-08-04 01:51:09 +0000444 } else if (strcmp(*argv, "-config") == 0) {
445 argv++;
446 if (argv < stop) {
447 int index = findConfig(*argv);
448 if (index >= 0) {
449 outConfig = gConfigs[index].fConfig;
450 configName = gConfigs[index].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000451 backend = gConfigs[index].fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000452 glHelper = gConfigs[index].fGLHelper;
reed@android.com387359e2009-08-04 01:51:09 +0000453 configCount = 1;
454 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000455 SkString str;
456 str.printf("unrecognized config %s\n", *argv);
457 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000458 return -1;
459 }
460 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000461 log_error("missing arg for -config\n");
reed@android.com387359e2009-08-04 01:51:09 +0000462 return -1;
463 }
reed@android.com0c9da392010-02-22 19:50:13 +0000464 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000465 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000466 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000467 defineDict.set(argv[-1] + 2, *argv);
468 } else {
469 log_error("incomplete '-Dfoo bar' definition\n");
470 return -1;
471 }
reed@android.com387359e2009-08-04 01:51:09 +0000472 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000473 SkString str;
474 str.printf("unrecognized arg %s\n", *argv);
475 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000476 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000477 }
478 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000479
reed@google.com3b14dc12011-04-04 14:31:36 +0000480 // report our current settings
481 {
482 SkString str;
bungeman@google.coma5d48412011-06-15 17:25:46 +0000483 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d",
reed@google.com3b14dc12011-04-04 14:31:36 +0000484 forceAlpha, forceAA, forceFilter);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000485 str.appendf(" rotate=%d scale=%d clip=%d",
486 doRotate, doScale, doClip);
487
488 const char * ditherName;
489 switch (forceDither) {
490 case SkTriState::kDefault: ditherName = "default"; break;
491 case SkTriState::kTrue: ditherName = "true"; break;
492 case SkTriState::kFalse: ditherName = "false"; break;
493 default: ditherName = "<invalid>"; break;
494 }
495 str.appendf(" dither=%s", ditherName);
496
497 if (hasStrokeWidth) {
498 str.appendf(" strokeWidth=%f", strokeWidth);
499 } else {
500 str.append(" strokeWidth=none");
501 }
502
503#if defined(SK_SCALAR_IS_FLOAT)
504 str.append(" scalar=float");
505#elif defined(SK_SCALAR_IS_FIXED)
506 str.append(" scalar=fixed");
507#endif
508
509#if defined(SK_BUILD_FOR_WIN32)
510 str.append(" system=WIN32");
511#elif defined(SK_BUILD_FOR_MAC)
512 str.append(" system=MAC");
513#elif defined(SK_BUILD_FOR_ANDROID)
514 str.append(" system=ANDROID");
515#elif defined(SK_BUILD_FOR_UNIX)
516 str.append(" system=UNIX");
517#else
518 str.append(" system=other");
519#endif
520
521#if defined(SK_DEBUG)
522 str.append(" DEBUG");
523#endif
524 str.append("\n");
reed@google.com3b14dc12011-04-04 14:31:36 +0000525 log_progress(str);
526 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000527
bungeman@google.coma5d48412011-06-15 17:25:46 +0000528 //Don't do GL when fixed.
529#if !defined(SK_SCALAR_IS_FIXED)
tomhudson@google.com83823942011-10-28 14:59:07 +0000530 int contextWidth = 1024;
531 int contextHeight = 1024;
532 determine_gpu_context_size(defineDict, &contextWidth, &contextHeight);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000533 SkAutoTUnref<SkGLContext> realGLCtx(new SkNativeGLContext);
534 SkAutoTUnref<SkGLContext> nullGLCtx(new SkNullGLContext);
robertphillips@google.com0da37192012-03-19 14:42:13 +0000535 SkAutoTUnref<SkGLContext> debugGLCtx(new SkDebugGLContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000536#if SK_ANGLE
537 SkAutoTUnref<SkGLContext> angleGLCtx(new SkANGLEGLContext);
538#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000539 gRealGLHelper.init(realGLCtx.get(), contextWidth, contextHeight);
540 gNullGLHelper.init(nullGLCtx.get(), contextWidth, contextHeight);
robertphillips@google.com0da37192012-03-19 14:42:13 +0000541 gDebugGLHelper.init(debugGLCtx.get(), contextWidth, contextHeight);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000542#if SK_ANGLE
543 gANGLEGLHelper.init(angleGLCtx.get(), contextWidth, contextHeight);
544#endif
bungeman@google.coma5d48412011-06-15 17:25:46 +0000545#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000546 BenchTimer timer = BenchTimer(gRealGLHelper.glContext());
bsalomon@google.com74913722011-10-27 20:44:19 +0000547
reed@android.come9d00602009-09-02 21:12:42 +0000548 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000549 SkBenchmark* bench;
550 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000551 SkIPoint dim = bench->getSize();
552 if (dim.fX <= 0 || dim.fY <= 0) {
553 continue;
554 }
reed@android.comb398fe82009-01-07 11:47:57 +0000555
reed@android.com4bc19832009-01-19 20:08:35 +0000556 bench->setForceAlpha(forceAlpha);
557 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000558 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000559 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000560 if (hasStrokeWidth) {
561 bench->setStrokeWidth(strokeWidth);
562 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000563
reed@android.com387359e2009-08-04 01:51:09 +0000564 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000565 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000566 continue;
567 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000568
reed@android.com29348cb2009-08-04 18:17:15 +0000569 {
570 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000571 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000572 bench->getName());
reed@android.com29348cb2009-08-04 18:17:15 +0000573 log_progress(str);
574 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000575
reed@android.com4bc19832009-01-19 20:08:35 +0000576 for (int configIndex = 0; configIndex < configCount; configIndex++) {
577 if (configCount > 1) {
578 outConfig = gConfigs[configIndex].fConfig;
579 configName = gConfigs[configIndex].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000580 backend = gConfigs[configIndex].fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000581 glHelper = gConfigs[configIndex].fGLHelper;
reed@android.com4bc19832009-01-19 20:08:35 +0000582 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000583
584 if (kGPU_Backend == backend &&
585 (NULL == glHelper || !glHelper->isValid())) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000586 continue;
587 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000588
bsalomon@google.com508824b2011-12-13 16:49:49 +0000589 SkDevice* device = make_device(outConfig, dim, backend, glHelper);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000590 SkCanvas canvas(device);
591 device->unref();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000592
reed@android.com4c7d3d62009-01-21 03:15:13 +0000593 if (doClip) {
594 performClip(&canvas, dim.fX, dim.fY);
595 }
reed@android.com387359e2009-08-04 01:51:09 +0000596 if (doScale) {
597 performScale(&canvas, dim.fX, dim.fY);
598 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000599 if (doRotate) {
600 performRotate(&canvas, dim.fX, dim.fY);
601 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000602
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000603 //warm up caches if needed
reed@android.comeca48362010-12-20 18:34:17 +0000604 if (repeatDraw > 1) {
605 SkAutoCanvasRestore acr(&canvas, true);
606 bench->draw(&canvas);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000607 if (glHelper) {
608 glHelper->grContext()->flush();
609 SK_GL(*glHelper->glContext(), Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000610 }
reed@android.comeca48362010-12-20 18:34:17 +0000611 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000612
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000613 timer.start();
reed@android.com4bc19832009-01-19 20:08:35 +0000614 for (int i = 0; i < repeatDraw; i++) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000615 SkAutoCanvasRestore acr(&canvas, true);
616 bench->draw(&canvas);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000617 if (glHelper) {
618 glHelper->grContext()->flush();
reed@google.com25df8882011-07-14 19:03:58 +0000619 }
620 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000621 if (glHelper) {
622 SK_GL(*glHelper->glContext(), Finish());
bsalomon@google.com788e2472011-10-19 20:56:30 +0000623 }
bsalomon@google.com788e2472011-10-19 20:56:30 +0000624 timer.end();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000625
reed@android.com4bc19832009-01-19 20:08:35 +0000626 if (repeatDraw > 1) {
reed@android.com29348cb2009-08-04 18:17:15 +0000627 SkString str;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000628 str.printf(" %4s:", configName);
629 if (timerWall) {
630 str.appendf(" msecs = %6.2f", timer.fWall / repeatDraw);
631 }
632 if (timerCpu) {
633 str.appendf(" cmsecs = %6.2f", timer.fCpu / repeatDraw);
634 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000635 if (timerGpu && glHelper && timer.fGpu > 0) {
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000636 str.appendf(" gmsecs = %6.2f", timer.fGpu / repeatDraw);
637 }
reed@android.com29348cb2009-08-04 18:17:15 +0000638 log_progress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000639 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000640 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000641 saveFile(bench->getName(), configName, outDir.c_str(),
642 device->accessBitmap(false));
reed@android.com4c7d3d62009-01-21 03:15:13 +0000643 }
reed@android.com4bc19832009-01-19 20:08:35 +0000644 }
reed@android.com29348cb2009-08-04 18:17:15 +0000645 log_progress("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000646 }
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000647
robertphillips@google.com0da37192012-03-19 14:42:13 +0000648 // need to clean up here rather than post-main to allow leak detection to work
649 gDebugGLHelper.cleanup();
650
reed@android.combd700c32009-01-05 03:34:50 +0000651 return 0;
652}