blob: c58d5b1e5f2e5fd3f07381ed354fae74a6c1a5f6 [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"
bsalomon@google.com74913722011-10-27 20:44:19 +000021#include "SkNativeGLContext.h"
22#include "SkNullGLContext.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000023#include "SkNWayCanvas.h"
24#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +000025#include "SkString.h"
reed@android.combd700c32009-01-05 03:34:50 +000026
reed@android.com29348cb2009-08-04 18:17:15 +000027#ifdef ANDROID
28static void log_error(const char msg[]) { SkDebugf("%s", msg); }
29static void log_progress(const char msg[]) { SkDebugf("%s", msg); }
30#else
31static void log_error(const char msg[]) { fprintf(stderr, "%s", msg); }
32static void log_progress(const char msg[]) { printf("%s", msg); }
33#endif
34
35static void log_error(const SkString& str) { log_error(str.c_str()); }
36static void log_progress(const SkString& str) { log_progress(str.c_str()); }
37
38///////////////////////////////////////////////////////////////////////////////
39
reed@android.com6c924ad2009-03-31 03:48:49 +000040static void erase(SkBitmap& bm) {
41 if (bm.config() == SkBitmap::kA8_Config) {
42 bm.eraseColor(0);
43 } else {
44 bm.eraseColor(SK_ColorWHITE);
45 }
46}
47
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000048#if 0
reed@android.com6c924ad2009-03-31 03:48:49 +000049static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
50 if (bm1.width() != bm2.width() ||
51 bm1.height() != bm2.height() ||
52 bm1.config() != bm2.config()) {
53 return false;
54 }
55
56 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
57 for (int y = 0; y < bm1.height(); y++) {
58 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
59 return false;
60 }
61 }
reed@android.com6c924ad2009-03-31 03:48:49 +000062 return true;
63}
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000064#endif
reed@android.com6c924ad2009-03-31 03:48:49 +000065
reed@android.combd700c32009-01-05 03:34:50 +000066class Iter {
67public:
reed@android.come9d00602009-09-02 21:12:42 +000068 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +000069 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +000070 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +000071 }
72
73 SkBenchmark* next() {
74 if (fBench) {
75 BenchRegistry::Factory f = fBench->factory();
76 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +000077 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +000078 }
79 return NULL;
80 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000081
reed@android.combd700c32009-01-05 03:34:50 +000082private:
83 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +000084 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +000085};
86
87static void make_filename(const char name[], SkString* path) {
88 path->set(name);
89 for (int i = 0; name[i]; i++) {
90 switch (name[i]) {
91 case '/':
92 case '\\':
93 case ' ':
94 case ':':
95 path->writable_str()[i] = '-';
96 break;
97 default:
98 break;
99 }
100 }
101}
102
reed@android.com4c7d3d62009-01-21 03:15:13 +0000103static void saveFile(const char name[], const char config[], const char dir[],
104 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000105 SkBitmap copy;
106 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
107 return;
108 }
reed@android.comf523e252009-01-26 23:15:37 +0000109
110 if (bm.config() == SkBitmap::kA8_Config) {
111 // turn alpha into gray-scale
112 size_t size = copy.getSize() >> 2;
113 SkPMColor* p = copy.getAddr32(0, 0);
114 for (size_t i = 0; i < size; i++) {
115 int c = (*p >> SK_A32_SHIFT) & 0xFF;
116 c = 255 - c;
117 c |= (c << 24) | (c << 16) | (c << 8);
118 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
119 }
120 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000121
reed@android.com4c7d3d62009-01-21 03:15:13 +0000122 SkString str;
123 make_filename(name, &str);
124 str.appendf("_%s.png", config);
125 str.prepend(dir);
126 ::remove(str.c_str());
127 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
128 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000129}
130
131static void performClip(SkCanvas* canvas, int w, int h) {
132 SkRect r;
133
134 r.set(SkIntToScalar(10), SkIntToScalar(10),
135 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
136 canvas->clipRect(r, SkRegion::kIntersect_Op);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000137
reed@android.com4c7d3d62009-01-21 03:15:13 +0000138 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
139 SkIntToScalar(w-10), SkIntToScalar(h-10));
140 canvas->clipRect(r, SkRegion::kXOR_Op);
141}
142
143static void performRotate(SkCanvas* canvas, int w, int h) {
144 const SkScalar x = SkIntToScalar(w) / 2;
145 const SkScalar y = SkIntToScalar(h) / 2;
146
147 canvas->translate(x, y);
148 canvas->rotate(SkIntToScalar(35));
149 canvas->translate(-x, -y);
150}
151
reed@android.com387359e2009-08-04 01:51:09 +0000152static void performScale(SkCanvas* canvas, int w, int h) {
153 const SkScalar x = SkIntToScalar(w) / 2;
154 const SkScalar y = SkIntToScalar(h) / 2;
155
156 canvas->translate(x, y);
157 // just enough so we can't take the sprite case
158 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
159 canvas->translate(-x, -y);
160}
161
reed@android.com29348cb2009-08-04 18:17:15 +0000162static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
163 if (argv < stop) {
164 *var = atoi(*argv) != 0;
165 return true;
166 }
167 return false;
168}
169
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000170enum Backend {
171 kRaster_Backend,
172 kGPU_Backend,
173 kPDF_Backend,
174};
175
176static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000177 Backend backend, GrContext* context,
178 GrRenderTarget* rt) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000179 SkDevice* device = NULL;
180 SkBitmap bitmap;
181 bitmap.setConfig(config, size.fX, size.fY);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000182
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000183 switch (backend) {
184 case kRaster_Backend:
185 bitmap.allocPixels();
186 erase(bitmap);
reed@google.comaf951c92011-06-16 19:10:39 +0000187 device = new SkDevice(bitmap);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000188 break;
189 case kGPU_Backend:
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000190 device = new SkGpuDevice(context, rt);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000191// device->clear(0xFFFFFFFF);
192 break;
193 case kPDF_Backend:
194 default:
195 SkASSERT(!"unsupported");
196 }
197 return device;
198}
199
reed@android.com4bc19832009-01-19 20:08:35 +0000200static const struct {
201 SkBitmap::Config fConfig;
202 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000203 Backend fBackend;
reed@android.com4bc19832009-01-19 20:08:35 +0000204} gConfigs[] = {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000205 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend },
206 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend },
207 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend },
reed@android.com4bc19832009-01-19 20:08:35 +0000208};
209
reed@android.com4c7d3d62009-01-21 03:15:13 +0000210static int findConfig(const char config[]) {
211 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
212 if (!strcmp(config, gConfigs[i].fName)) {
213 return i;
214 }
215 }
216 return -1;
217}
218
tomhudson@google.com83823942011-10-28 14:59:07 +0000219static void determine_gpu_context_size(SkTDict<const char*>& defineDict,
220 int* contextWidth,
221 int* contextHeight) {
222 Iter iter(&defineDict);
223 SkBenchmark* bench;
224 while ((bench = iter.next()) != NULL) {
225 SkIPoint dim = bench->getSize();
226 if (*contextWidth < dim.fX) {
227 *contextWidth = dim.fX;
228 }
229 if (*contextHeight < dim.fY) {
230 *contextHeight = dim.fY;
231 }
232 }
233}
234
reed@google.com1a7eabc2011-10-31 16:33:52 +0000235static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
236 if (0 == array.count()) {
237 // no names, so don't skip anything
238 return false;
239 }
240 for (int i = 0; i < array.count(); ++i) {
241 if (strstr(name, array[i])) {
242 // found the name, so don't skip
243 return false;
244 }
245 }
246 return true;
247}
248
reed@android.combd700c32009-01-05 03:34:50 +0000249int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000250 SkAutoGraphics ag;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000251
reed@android.come9d00602009-09-02 21:12:42 +0000252 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000253 int repeatDraw = 1;
254 int forceAlpha = 0xFF;
255 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000256 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000257 SkTriState::State forceDither = SkTriState::kDefault;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000258 bool timerWall = false;
259 bool timerCpu = true;
260 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000261 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000262 bool doRotate = false;
263 bool doClip = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000264 bool hasStrokeWidth = false;
265 float strokeWidth;
bsalomon@google.com74913722011-10-27 20:44:19 +0000266 bool useNullGL = false;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000267 SkTDArray<const char*> fMatches;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000268
reed@android.comb398fe82009-01-07 11:47:57 +0000269 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000270 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
271 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000272 Backend backend = kRaster_Backend; // for warning
reed@android.com387359e2009-08-04 01:51:09 +0000273 int configCount = SK_ARRAY_COUNT(gConfigs);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000274
reed@android.comb398fe82009-01-07 11:47:57 +0000275 char* const* stop = argv + argc;
276 for (++argv; argv < stop; ++argv) {
277 if (strcmp(*argv, "-o") == 0) {
278 argv++;
279 if (argv < stop && **argv) {
280 outDir.set(*argv);
281 if (outDir.c_str()[outDir.size() - 1] != '/') {
282 outDir.append("/");
283 }
284 }
reed@android.com4bc19832009-01-19 20:08:35 +0000285 } else if (strcmp(*argv, "-repeat") == 0) {
286 argv++;
287 if (argv < stop) {
288 repeatDraw = atoi(*argv);
289 if (repeatDraw < 1) {
290 repeatDraw = 1;
291 }
292 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000293 log_error("missing arg for -repeat\n");
reed@android.com4bc19832009-01-19 20:08:35 +0000294 return -1;
295 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000296 } else if (strcmp(*argv, "-timers") == 0) {
297 argv++;
298 if (argv < stop) {
299 timerWall = false;
300 timerCpu = false;
301 timerGpu = false;
302 for (char* t = *argv; *t; ++t) {
303 switch (*t) {
304 case 'w': timerWall = true; break;
305 case 'c': timerCpu = true; break;
306 case 'g': timerGpu = true; break;
307 }
308 }
309 } else {
310 log_error("missing arg for -timers\n");
311 return -1;
312 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000313 } else if (!strcmp(*argv, "-rotate")) {
314 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000315 } else if (!strcmp(*argv, "-scale")) {
316 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000317 } else if (!strcmp(*argv, "-clip")) {
318 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000319 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000320 if (!parse_bool_arg(++argv, stop, &forceAA)) {
321 log_error("missing arg for -forceAA\n");
322 return -1;
323 }
324 } else if (strcmp(*argv, "-forceFilter") == 0) {
325 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
326 log_error("missing arg for -forceFilter\n");
327 return -1;
328 }
reed@android.com4e635f92009-10-19 17:39:46 +0000329 } else if (strcmp(*argv, "-forceDither") == 0) {
330 bool tmp;
331 if (!parse_bool_arg(++argv, stop, &tmp)) {
332 log_error("missing arg for -forceDither\n");
333 return -1;
334 }
335 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000336 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000337 bool wantAlpha = false;
338 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
339 log_error("missing arg for -forceBlend\n");
340 return -1;
341 }
342 forceAlpha = wantAlpha ? 0x80 : 0xFF;
agl@chromium.org652807b2010-04-27 15:47:34 +0000343 } else if (strcmp(*argv, "-strokeWidth") == 0) {
344 argv++;
345 if (argv < stop) {
346 const char *strokeWidthStr = *argv;
347 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
348 log_error("bad arg for -strokeWidth\n");
349 return -1;
350 }
351 hasStrokeWidth = true;
352 } else {
353 log_error("missing arg for -strokeWidth\n");
354 return -1;
355 }
reed@android.com387359e2009-08-04 01:51:09 +0000356 } else if (strcmp(*argv, "-match") == 0) {
357 argv++;
358 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000359 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000360 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000361 log_error("missing arg for -match\n");
reed@android.com387359e2009-08-04 01:51:09 +0000362 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000363 }
reed@android.com387359e2009-08-04 01:51:09 +0000364 } else if (strcmp(*argv, "-config") == 0) {
365 argv++;
366 if (argv < stop) {
367 int index = findConfig(*argv);
368 if (index >= 0) {
369 outConfig = gConfigs[index].fConfig;
370 configName = gConfigs[index].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000371 backend = gConfigs[index].fBackend;
reed@android.com387359e2009-08-04 01:51:09 +0000372 configCount = 1;
373 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000374 SkString str;
375 str.printf("unrecognized config %s\n", *argv);
376 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000377 return -1;
378 }
379 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000380 log_error("missing arg for -config\n");
reed@android.com387359e2009-08-04 01:51:09 +0000381 return -1;
382 }
reed@android.com0c9da392010-02-22 19:50:13 +0000383 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000384 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000385 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000386 defineDict.set(argv[-1] + 2, *argv);
387 } else {
388 log_error("incomplete '-Dfoo bar' definition\n");
389 return -1;
390 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000391 } else if (strcmp(*argv, "-nullgl") == 0) {
392 useNullGL = true;
reed@android.com387359e2009-08-04 01:51:09 +0000393 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000394 SkString str;
395 str.printf("unrecognized arg %s\n", *argv);
396 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000397 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000398 }
399 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000400
reed@google.com3b14dc12011-04-04 14:31:36 +0000401 // report our current settings
402 {
403 SkString str;
bungeman@google.coma5d48412011-06-15 17:25:46 +0000404 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d",
reed@google.com3b14dc12011-04-04 14:31:36 +0000405 forceAlpha, forceAA, forceFilter);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000406 str.appendf(" rotate=%d scale=%d clip=%d",
407 doRotate, doScale, doClip);
408
409 const char * ditherName;
410 switch (forceDither) {
411 case SkTriState::kDefault: ditherName = "default"; break;
412 case SkTriState::kTrue: ditherName = "true"; break;
413 case SkTriState::kFalse: ditherName = "false"; break;
414 default: ditherName = "<invalid>"; break;
415 }
416 str.appendf(" dither=%s", ditherName);
417
418 if (hasStrokeWidth) {
419 str.appendf(" strokeWidth=%f", strokeWidth);
420 } else {
421 str.append(" strokeWidth=none");
422 }
423
424#if defined(SK_SCALAR_IS_FLOAT)
425 str.append(" scalar=float");
426#elif defined(SK_SCALAR_IS_FIXED)
427 str.append(" scalar=fixed");
428#endif
429
430#if defined(SK_BUILD_FOR_WIN32)
431 str.append(" system=WIN32");
432#elif defined(SK_BUILD_FOR_MAC)
433 str.append(" system=MAC");
434#elif defined(SK_BUILD_FOR_ANDROID)
435 str.append(" system=ANDROID");
436#elif defined(SK_BUILD_FOR_UNIX)
437 str.append(" system=UNIX");
438#else
439 str.append(" system=other");
440#endif
441
442#if defined(SK_DEBUG)
443 str.append(" DEBUG");
444#endif
445 str.append("\n");
reed@google.com3b14dc12011-04-04 14:31:36 +0000446 log_progress(str);
447 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000448
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000449 GrContext* context = NULL;
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000450 GrRenderTarget* rt = NULL;
bungeman@google.coma5d48412011-06-15 17:25:46 +0000451 //Don't do GL when fixed.
bsalomon@google.com74913722011-10-27 20:44:19 +0000452 SkAutoTUnref<SkGLContext> glctx;
bungeman@google.coma5d48412011-06-15 17:25:46 +0000453#if !defined(SK_SCALAR_IS_FIXED)
bsalomon@google.com74913722011-10-27 20:44:19 +0000454 if (useNullGL) {
455 glctx.reset(new SkNullGLContext);
456 } else {
457 glctx.reset(new SkNativeGLContext);
458 }
tomhudson@google.com83823942011-10-28 14:59:07 +0000459 int contextWidth = 1024;
460 int contextHeight = 1024;
461 determine_gpu_context_size(defineDict, &contextWidth, &contextHeight);
462 if (glctx.get()->init(contextWidth, contextHeight)) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000463 GrPlatform3DContext ctx =
bsalomon@google.com74913722011-10-27 20:44:19 +0000464 reinterpret_cast<GrPlatform3DContext>(glctx.get()->gl());
bsalomon@google.com373a6632011-10-19 20:43:20 +0000465 context = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000466 if (NULL != context) {
467 GrPlatformSurfaceDesc desc;
468 desc.reset();
469 desc.fConfig = kRGBA_8888_GrPixelConfig;
tomhudson@google.com83823942011-10-28 14:59:07 +0000470 desc.fWidth = contextWidth;
471 desc.fHeight = contextHeight;
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000472 desc.fStencilBits = 8;
bsalomon@google.com74913722011-10-27 20:44:19 +0000473 desc.fPlatformRenderTarget = glctx.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000474 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
475 rt = static_cast<GrRenderTarget*>(context->createPlatformSurface(desc));
476 if (NULL == rt) {
477 context->unref();
478 context = NULL;
479 }
480 }
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000481 }
bungeman@google.coma5d48412011-06-15 17:25:46 +0000482#endif
bsalomon@google.com74913722011-10-27 20:44:19 +0000483 BenchTimer timer = BenchTimer(context ? glctx.get() : NULL);
484
reed@android.come9d00602009-09-02 21:12:42 +0000485 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000486 SkBenchmark* bench;
487 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000488 SkIPoint dim = bench->getSize();
489 if (dim.fX <= 0 || dim.fY <= 0) {
490 continue;
491 }
reed@android.comb398fe82009-01-07 11:47:57 +0000492
reed@android.com4bc19832009-01-19 20:08:35 +0000493 bench->setForceAlpha(forceAlpha);
494 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000495 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000496 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000497 if (hasStrokeWidth) {
498 bench->setStrokeWidth(strokeWidth);
499 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000500
reed@android.com387359e2009-08-04 01:51:09 +0000501 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000502 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000503 continue;
504 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000505
reed@android.com29348cb2009-08-04 18:17:15 +0000506 {
507 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000508 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000509 bench->getName());
reed@android.com29348cb2009-08-04 18:17:15 +0000510 log_progress(str);
511 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000512
reed@android.com4bc19832009-01-19 20:08:35 +0000513 for (int configIndex = 0; configIndex < configCount; configIndex++) {
514 if (configCount > 1) {
515 outConfig = gConfigs[configIndex].fConfig;
516 configName = gConfigs[configIndex].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000517 backend = gConfigs[configIndex].fBackend;
reed@android.com4bc19832009-01-19 20:08:35 +0000518 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000519
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000520 if (kGPU_Backend == backend && NULL == context) {
521 continue;
522 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000523
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000524 SkDevice* device = make_device(outConfig, dim,
525 backend, context, rt);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000526 SkCanvas canvas(device);
527 device->unref();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000528
reed@android.com4c7d3d62009-01-21 03:15:13 +0000529 if (doClip) {
530 performClip(&canvas, dim.fX, dim.fY);
531 }
reed@android.com387359e2009-08-04 01:51:09 +0000532 if (doScale) {
533 performScale(&canvas, dim.fX, dim.fY);
534 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000535 if (doRotate) {
536 performRotate(&canvas, dim.fX, dim.fY);
537 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000538
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000539 bool gpu = kGPU_Backend == backend && context;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000540 //warm up caches if needed
reed@android.comeca48362010-12-20 18:34:17 +0000541 if (repeatDraw > 1) {
542 SkAutoCanvasRestore acr(&canvas, true);
543 bench->draw(&canvas);
bsalomon@google.com788e2472011-10-19 20:56:30 +0000544#if !defined(SK_SCALAR_IS_FIXED)
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000545 if (gpu) {
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000546 context->flush();
bsalomon@google.com74913722011-10-27 20:44:19 +0000547 SK_GL(*glctx.get(), Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000548 }
bsalomon@google.com788e2472011-10-19 20:56:30 +0000549#endif
reed@android.comeca48362010-12-20 18:34:17 +0000550 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000551
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000552 timer.start();
reed@android.com4bc19832009-01-19 20:08:35 +0000553 for (int i = 0; i < repeatDraw; i++) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000554 SkAutoCanvasRestore acr(&canvas, true);
555 bench->draw(&canvas);
reed@google.com25df8882011-07-14 19:03:58 +0000556 if (gpu) {
557 context->flush();
558 }
559 }
bsalomon@google.com788e2472011-10-19 20:56:30 +0000560 #if !defined(SK_SCALAR_IS_FIXED)
561 if (gpu) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000562 SK_GL(*glctx.get(), Finish());
bsalomon@google.com788e2472011-10-19 20:56:30 +0000563 }
564 #endif
565 timer.end();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000566
reed@android.com4bc19832009-01-19 20:08:35 +0000567 if (repeatDraw > 1) {
reed@android.com29348cb2009-08-04 18:17:15 +0000568 SkString str;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000569 str.printf(" %4s:", configName);
570 if (timerWall) {
571 str.appendf(" msecs = %6.2f", timer.fWall / repeatDraw);
572 }
573 if (timerCpu) {
574 str.appendf(" cmsecs = %6.2f", timer.fCpu / repeatDraw);
575 }
576 if (timerGpu && gpu && timer.fGpu > 0) {
577 str.appendf(" gmsecs = %6.2f", timer.fGpu / repeatDraw);
578 }
reed@android.com29348cb2009-08-04 18:17:15 +0000579 log_progress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000580 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000581 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000582 saveFile(bench->getName(), configName, outDir.c_str(),
583 device->accessBitmap(false));
reed@android.com4c7d3d62009-01-21 03:15:13 +0000584 }
reed@android.com4bc19832009-01-19 20:08:35 +0000585 }
reed@android.com29348cb2009-08-04 18:17:15 +0000586 log_progress("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000587 }
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000588
589 SkSafeUnref(context);
590 SkSafeUnref(rt);
591
reed@android.combd700c32009-01-05 03:34:50 +0000592 return 0;
593}