blob: 024ad0ff1c0193cac438cfb38742a402287eaf76 [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
djsollen@google.com56c69772011-11-08 19:00:26 +000027#ifdef SK_BUILD_FOR_ANDROID
reed@android.com29348cb2009-08-04 18:17:15 +000028static 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
bsalomon@google.com508824b2011-12-13 16:49:49 +0000176class GLHelper {
177public:
178 GLHelper() {
179 }
180
181 bool init(SkGLContext* glCtx, int width, int height) {
182 GrContext* grCtx;
183 GrRenderTarget* rt;
184 if (glCtx->init(width, height)) {
185 GrPlatform3DContext ctx =
186 reinterpret_cast<GrPlatform3DContext>(glCtx->gl());
187 grCtx = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
188 if (NULL != grCtx) {
189 GrPlatformRenderTargetDesc desc;
190 desc.fConfig = kSkia8888_PM_GrPixelConfig;
191 desc.fWidth = width;
192 desc.fHeight = height;
193 desc.fStencilBits = 8;
194 desc.fRenderTargetHandle = glCtx->getFBOID();
195 rt = grCtx->createPlatformRenderTarget(desc);
196 if (NULL == rt) {
197 grCtx->unref();
198 return false;
199 }
200 }
201 } else {
202 return false;
203 }
204 glCtx->ref();
205 fGLContext.reset(glCtx);
206 fGrContext.reset(grCtx);
207 fRenderTarget.reset(rt);
208 return true;
209 }
210
211 bool isValid() {
212 return NULL != fGLContext.get();
213 }
214
215 SkGLContext* glContext() {
216 return fGLContext.get();
217 }
218
219 GrRenderTarget* renderTarget() {
220 return fRenderTarget.get();
221 }
222
223 GrContext* grContext() {
224 return fGrContext.get();
225 }
226private:
227 SkAutoTUnref<SkGLContext> fGLContext;
228 SkAutoTUnref<GrContext> fGrContext;
229 SkAutoTUnref<GrRenderTarget> fRenderTarget;
230};
231
232static GLHelper gRealGLHelper;
233static GLHelper gNullGLHelper;
234
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000235static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.com508824b2011-12-13 16:49:49 +0000236 Backend backend, GLHelper* glHelper) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000237 SkDevice* device = NULL;
238 SkBitmap bitmap;
239 bitmap.setConfig(config, size.fX, size.fY);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000240
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000241 switch (backend) {
242 case kRaster_Backend:
243 bitmap.allocPixels();
244 erase(bitmap);
reed@google.comaf951c92011-06-16 19:10:39 +0000245 device = new SkDevice(bitmap);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000246 break;
247 case kGPU_Backend:
bsalomon@google.com508824b2011-12-13 16:49:49 +0000248 device = new SkGpuDevice(glHelper->grContext(),
249 glHelper->renderTarget());
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000250 break;
251 case kPDF_Backend:
252 default:
253 SkASSERT(!"unsupported");
254 }
255 return device;
256}
257
reed@android.com4bc19832009-01-19 20:08:35 +0000258static const struct {
259 SkBitmap::Config fConfig;
260 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000261 Backend fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000262 GLHelper* fGLHelper;
reed@android.com4bc19832009-01-19 20:08:35 +0000263} gConfigs[] = {
bsalomon@google.com508824b2011-12-13 16:49:49 +0000264 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend, NULL },
265 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend, NULL },
266 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend, &gRealGLHelper },
267 { SkBitmap::kARGB_8888_Config, "NULLGPU", kGPU_Backend, &gNullGLHelper },
reed@android.com4bc19832009-01-19 20:08:35 +0000268};
269
reed@android.com4c7d3d62009-01-21 03:15:13 +0000270static int findConfig(const char config[]) {
271 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
272 if (!strcmp(config, gConfigs[i].fName)) {
273 return i;
274 }
275 }
276 return -1;
277}
278
tomhudson@google.com83823942011-10-28 14:59:07 +0000279static void determine_gpu_context_size(SkTDict<const char*>& defineDict,
280 int* contextWidth,
281 int* contextHeight) {
282 Iter iter(&defineDict);
283 SkBenchmark* bench;
284 while ((bench = iter.next()) != NULL) {
285 SkIPoint dim = bench->getSize();
286 if (*contextWidth < dim.fX) {
287 *contextWidth = dim.fX;
288 }
289 if (*contextHeight < dim.fY) {
290 *contextHeight = dim.fY;
291 }
292 }
293}
294
reed@google.com1a7eabc2011-10-31 16:33:52 +0000295static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
296 if (0 == array.count()) {
297 // no names, so don't skip anything
298 return false;
299 }
300 for (int i = 0; i < array.count(); ++i) {
301 if (strstr(name, array[i])) {
302 // found the name, so don't skip
303 return false;
304 }
305 }
306 return true;
307}
308
reed@android.combd700c32009-01-05 03:34:50 +0000309int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000310 SkAutoGraphics ag;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000311
reed@android.come9d00602009-09-02 21:12:42 +0000312 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000313 int repeatDraw = 1;
314 int forceAlpha = 0xFF;
315 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000316 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000317 SkTriState::State forceDither = SkTriState::kDefault;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000318 bool timerWall = false;
319 bool timerCpu = true;
320 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000321 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000322 bool doRotate = false;
323 bool doClip = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000324 bool hasStrokeWidth = false;
325 float strokeWidth;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000326 SkTDArray<const char*> fMatches;
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000327
reed@android.comb398fe82009-01-07 11:47:57 +0000328 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000329 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000330 GLHelper* glHelper = NULL;
reed@android.com387359e2009-08-04 01:51:09 +0000331 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000332 Backend backend = kRaster_Backend; // for warning
reed@android.com387359e2009-08-04 01:51:09 +0000333 int configCount = SK_ARRAY_COUNT(gConfigs);
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000334
reed@android.comb398fe82009-01-07 11:47:57 +0000335 char* const* stop = argv + argc;
336 for (++argv; argv < stop; ++argv) {
337 if (strcmp(*argv, "-o") == 0) {
338 argv++;
339 if (argv < stop && **argv) {
340 outDir.set(*argv);
341 if (outDir.c_str()[outDir.size() - 1] != '/') {
342 outDir.append("/");
343 }
344 }
reed@android.com4bc19832009-01-19 20:08:35 +0000345 } else if (strcmp(*argv, "-repeat") == 0) {
346 argv++;
347 if (argv < stop) {
348 repeatDraw = atoi(*argv);
349 if (repeatDraw < 1) {
350 repeatDraw = 1;
351 }
352 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000353 log_error("missing arg for -repeat\n");
reed@android.com4bc19832009-01-19 20:08:35 +0000354 return -1;
355 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000356 } else if (strcmp(*argv, "-timers") == 0) {
357 argv++;
358 if (argv < stop) {
359 timerWall = false;
360 timerCpu = false;
361 timerGpu = false;
362 for (char* t = *argv; *t; ++t) {
363 switch (*t) {
364 case 'w': timerWall = true; break;
365 case 'c': timerCpu = true; break;
366 case 'g': timerGpu = true; break;
367 }
368 }
369 } else {
370 log_error("missing arg for -timers\n");
371 return -1;
372 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000373 } else if (!strcmp(*argv, "-rotate")) {
374 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000375 } else if (!strcmp(*argv, "-scale")) {
376 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000377 } else if (!strcmp(*argv, "-clip")) {
378 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000379 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000380 if (!parse_bool_arg(++argv, stop, &forceAA)) {
381 log_error("missing arg for -forceAA\n");
382 return -1;
383 }
384 } else if (strcmp(*argv, "-forceFilter") == 0) {
385 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
386 log_error("missing arg for -forceFilter\n");
387 return -1;
388 }
reed@android.com4e635f92009-10-19 17:39:46 +0000389 } else if (strcmp(*argv, "-forceDither") == 0) {
390 bool tmp;
391 if (!parse_bool_arg(++argv, stop, &tmp)) {
392 log_error("missing arg for -forceDither\n");
393 return -1;
394 }
395 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000396 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000397 bool wantAlpha = false;
398 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
399 log_error("missing arg for -forceBlend\n");
400 return -1;
401 }
402 forceAlpha = wantAlpha ? 0x80 : 0xFF;
agl@chromium.org652807b2010-04-27 15:47:34 +0000403 } else if (strcmp(*argv, "-strokeWidth") == 0) {
404 argv++;
405 if (argv < stop) {
406 const char *strokeWidthStr = *argv;
407 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
408 log_error("bad arg for -strokeWidth\n");
409 return -1;
410 }
411 hasStrokeWidth = true;
412 } else {
413 log_error("missing arg for -strokeWidth\n");
414 return -1;
415 }
reed@android.com387359e2009-08-04 01:51:09 +0000416 } else if (strcmp(*argv, "-match") == 0) {
417 argv++;
418 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000419 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000420 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000421 log_error("missing arg for -match\n");
reed@android.com387359e2009-08-04 01:51:09 +0000422 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000423 }
reed@android.com387359e2009-08-04 01:51:09 +0000424 } else if (strcmp(*argv, "-config") == 0) {
425 argv++;
426 if (argv < stop) {
427 int index = findConfig(*argv);
428 if (index >= 0) {
429 outConfig = gConfigs[index].fConfig;
430 configName = gConfigs[index].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000431 backend = gConfigs[index].fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000432 glHelper = gConfigs[index].fGLHelper;
reed@android.com387359e2009-08-04 01:51:09 +0000433 configCount = 1;
434 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000435 SkString str;
436 str.printf("unrecognized config %s\n", *argv);
437 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000438 return -1;
439 }
440 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000441 log_error("missing arg for -config\n");
reed@android.com387359e2009-08-04 01:51:09 +0000442 return -1;
443 }
reed@android.com0c9da392010-02-22 19:50:13 +0000444 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000445 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000446 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000447 defineDict.set(argv[-1] + 2, *argv);
448 } else {
449 log_error("incomplete '-Dfoo bar' definition\n");
450 return -1;
451 }
reed@android.com387359e2009-08-04 01:51:09 +0000452 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000453 SkString str;
454 str.printf("unrecognized arg %s\n", *argv);
455 log_error(str);
reed@android.com387359e2009-08-04 01:51:09 +0000456 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000457 }
458 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000459
reed@google.com3b14dc12011-04-04 14:31:36 +0000460 // report our current settings
461 {
462 SkString str;
bungeman@google.coma5d48412011-06-15 17:25:46 +0000463 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d",
reed@google.com3b14dc12011-04-04 14:31:36 +0000464 forceAlpha, forceAA, forceFilter);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000465 str.appendf(" rotate=%d scale=%d clip=%d",
466 doRotate, doScale, doClip);
467
468 const char * ditherName;
469 switch (forceDither) {
470 case SkTriState::kDefault: ditherName = "default"; break;
471 case SkTriState::kTrue: ditherName = "true"; break;
472 case SkTriState::kFalse: ditherName = "false"; break;
473 default: ditherName = "<invalid>"; break;
474 }
475 str.appendf(" dither=%s", ditherName);
476
477 if (hasStrokeWidth) {
478 str.appendf(" strokeWidth=%f", strokeWidth);
479 } else {
480 str.append(" strokeWidth=none");
481 }
482
483#if defined(SK_SCALAR_IS_FLOAT)
484 str.append(" scalar=float");
485#elif defined(SK_SCALAR_IS_FIXED)
486 str.append(" scalar=fixed");
487#endif
488
489#if defined(SK_BUILD_FOR_WIN32)
490 str.append(" system=WIN32");
491#elif defined(SK_BUILD_FOR_MAC)
492 str.append(" system=MAC");
493#elif defined(SK_BUILD_FOR_ANDROID)
494 str.append(" system=ANDROID");
495#elif defined(SK_BUILD_FOR_UNIX)
496 str.append(" system=UNIX");
497#else
498 str.append(" system=other");
499#endif
500
501#if defined(SK_DEBUG)
502 str.append(" DEBUG");
503#endif
504 str.append("\n");
reed@google.com3b14dc12011-04-04 14:31:36 +0000505 log_progress(str);
506 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000507
bungeman@google.coma5d48412011-06-15 17:25:46 +0000508 //Don't do GL when fixed.
509#if !defined(SK_SCALAR_IS_FIXED)
tomhudson@google.com83823942011-10-28 14:59:07 +0000510 int contextWidth = 1024;
511 int contextHeight = 1024;
512 determine_gpu_context_size(defineDict, &contextWidth, &contextHeight);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000513 SkAutoTUnref<SkGLContext> realGLCtx(new SkNativeGLContext);
514 SkAutoTUnref<SkGLContext> nullGLCtx(new SkNullGLContext);
515 gRealGLHelper.init(realGLCtx.get(), contextWidth, contextHeight);
516 gNullGLHelper.init(nullGLCtx.get(), contextWidth, contextHeight);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000517#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000518 BenchTimer timer = BenchTimer(gRealGLHelper.glContext());
bsalomon@google.com74913722011-10-27 20:44:19 +0000519
reed@android.come9d00602009-09-02 21:12:42 +0000520 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000521 SkBenchmark* bench;
522 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000523 SkIPoint dim = bench->getSize();
524 if (dim.fX <= 0 || dim.fY <= 0) {
525 continue;
526 }
reed@android.comb398fe82009-01-07 11:47:57 +0000527
reed@android.com4bc19832009-01-19 20:08:35 +0000528 bench->setForceAlpha(forceAlpha);
529 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000530 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000531 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000532 if (hasStrokeWidth) {
533 bench->setStrokeWidth(strokeWidth);
534 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000535
reed@android.com387359e2009-08-04 01:51:09 +0000536 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000537 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000538 continue;
539 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000540
reed@android.com29348cb2009-08-04 18:17:15 +0000541 {
542 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000543 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000544 bench->getName());
reed@android.com29348cb2009-08-04 18:17:15 +0000545 log_progress(str);
546 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000547
reed@android.com4bc19832009-01-19 20:08:35 +0000548 for (int configIndex = 0; configIndex < configCount; configIndex++) {
549 if (configCount > 1) {
550 outConfig = gConfigs[configIndex].fConfig;
551 configName = gConfigs[configIndex].fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000552 backend = gConfigs[configIndex].fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000553 glHelper = gConfigs[configIndex].fGLHelper;
reed@android.com4bc19832009-01-19 20:08:35 +0000554 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000555
556 if (kGPU_Backend == backend &&
557 (NULL == glHelper || !glHelper->isValid())) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000558 continue;
559 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000560
bsalomon@google.com508824b2011-12-13 16:49:49 +0000561 SkDevice* device = make_device(outConfig, dim, backend, glHelper);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000562 SkCanvas canvas(device);
563 device->unref();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000564
reed@android.com4c7d3d62009-01-21 03:15:13 +0000565 if (doClip) {
566 performClip(&canvas, dim.fX, dim.fY);
567 }
reed@android.com387359e2009-08-04 01:51:09 +0000568 if (doScale) {
569 performScale(&canvas, dim.fX, dim.fY);
570 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000571 if (doRotate) {
572 performRotate(&canvas, dim.fX, dim.fY);
573 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000574
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000575 //warm up caches if needed
reed@android.comeca48362010-12-20 18:34:17 +0000576 if (repeatDraw > 1) {
577 SkAutoCanvasRestore acr(&canvas, true);
578 bench->draw(&canvas);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000579 if (glHelper) {
580 glHelper->grContext()->flush();
581 SK_GL(*glHelper->glContext(), Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000582 }
reed@android.comeca48362010-12-20 18:34:17 +0000583 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000584
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000585 timer.start();
reed@android.com4bc19832009-01-19 20:08:35 +0000586 for (int i = 0; i < repeatDraw; i++) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000587 SkAutoCanvasRestore acr(&canvas, true);
588 bench->draw(&canvas);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000589 if (glHelper) {
590 glHelper->grContext()->flush();
reed@google.com25df8882011-07-14 19:03:58 +0000591 }
592 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000593 if (glHelper) {
594 SK_GL(*glHelper->glContext(), Finish());
bsalomon@google.com788e2472011-10-19 20:56:30 +0000595 }
bsalomon@google.com788e2472011-10-19 20:56:30 +0000596 timer.end();
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000597
reed@android.com4bc19832009-01-19 20:08:35 +0000598 if (repeatDraw > 1) {
reed@android.com29348cb2009-08-04 18:17:15 +0000599 SkString str;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000600 str.printf(" %4s:", configName);
601 if (timerWall) {
602 str.appendf(" msecs = %6.2f", timer.fWall / repeatDraw);
603 }
604 if (timerCpu) {
605 str.appendf(" cmsecs = %6.2f", timer.fCpu / repeatDraw);
606 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000607 if (timerGpu && glHelper && timer.fGpu > 0) {
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000608 str.appendf(" gmsecs = %6.2f", timer.fGpu / repeatDraw);
609 }
reed@android.com29348cb2009-08-04 18:17:15 +0000610 log_progress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000611 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000612 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000613 saveFile(bench->getName(), configName, outDir.c_str(),
614 device->accessBitmap(false));
reed@android.com4c7d3d62009-01-21 03:15:13 +0000615 }
reed@android.com4bc19832009-01-19 20:08:35 +0000616 }
reed@android.com29348cb2009-08-04 18:17:15 +0000617 log_progress("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000618 }
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000619
reed@android.combd700c32009-01-05 03:34:50 +0000620 return 0;
621}