blob: dfde94a396cc6a15228c4b657140205c8ac1d9a3 [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012#if SK_SUPPORT_GPU
bsalomon@google.com971d0c82011-08-19 17:22:05 +000013#include "GrContext.h"
14#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#if SK_ANGLE
16#include "gl/SkANGLEGLContext.h"
17#endif // SK_ANGLE
18#include "gl/SkNativeGLContext.h"
19#include "gl/SkNullGLContext.h"
20#include "gl/SkDebugGLContext.h"
21#include "SkGpuDevice.h"
22#endif // SK_SUPPORT_GPU
bsalomon@google.com971d0c82011-08-19 17:22:05 +000023
24#include "SkBenchmark.h"
reed@android.combd700c32009-01-05 03:34:50 +000025#include "SkCanvas.h"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +000026#include "SkDeferredCanvas.h"
junov@chromium.org44b67b22012-08-07 18:00:31 +000027#include "SkDevice.h"
reed@android.comf523e252009-01-26 23:15:37 +000028#include "SkColorPriv.h"
reed@android.com3a859a02009-01-28 00:56:29 +000029#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000030#include "SkImageEncoder.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000031#include "SkNWayCanvas.h"
32#include "SkPicture.h"
borenet@google.coma2b32b82012-08-07 12:58:50 +000033#include "SkStream.h"
reed@android.combd700c32009-01-05 03:34:50 +000034#include "SkString.h"
reed@android.combd700c32009-01-05 03:34:50 +000035
robertphillips@google.com2abed832012-08-23 17:50:06 +000036template <typename T> const T& Min(const T& a, const T& b) {
37 return (a < b) ? a : b;
38}
39
borenet@google.coma2b32b82012-08-07 12:58:50 +000040class SkBenchLogger {
41public:
42 SkBenchLogger() : fFileStream(NULL) {}
43 ~SkBenchLogger() {
44 if (fFileStream)
45 SkDELETE(fFileStream);
46 }
47
48 bool SetLogFile(const char file[]) {
49 fFileStream = SkNEW_ARGS(SkFILEWStream, (file));
50 return fFileStream->isValid();
51 }
52
53 void logError(const char msg[]) { nativeLogError(msg); }
54 void logError(const SkString& str) { nativeLogError(str.c_str()); }
55
56 void logProgress(const char msg[]) {
57 nativeLogProgress(msg);
58 fileWrite(msg, strlen(msg));
59 }
60 void logProgress(const SkString& str) {
61 nativeLogProgress(str.c_str());
62 fileWrite(str.c_str(), str.size());
63 }
64
65private:
djsollen@google.com56c69772011-11-08 19:00:26 +000066#ifdef SK_BUILD_FOR_ANDROID
borenet@google.coma2b32b82012-08-07 12:58:50 +000067 void nativeLogError(const char msg[]) { SkDebugf("%s", msg); }
68 void nativeLogProgress(const char msg[]) { SkDebugf("%s", msg); }
reed@android.com29348cb2009-08-04 18:17:15 +000069#else
borenet@google.coma2b32b82012-08-07 12:58:50 +000070 void nativeLogError(const char msg[]) { fprintf(stderr, "%s", msg); }
71 void nativeLogProgress(const char msg[]) { printf("%s", msg); }
reed@android.com29348cb2009-08-04 18:17:15 +000072#endif
73
borenet@google.coma2b32b82012-08-07 12:58:50 +000074 void fileWrite(const char msg[], size_t size) {
75 if (fFileStream && fFileStream->isValid())
76 fFileStream->write(msg, size);
77 }
78 SkFILEWStream* fFileStream;
79
80} logger;
reed@android.com29348cb2009-08-04 18:17:15 +000081
82///////////////////////////////////////////////////////////////////////////////
83
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000084enum benchModes {
85 kNormal_benchModes,
86 kDeferred_benchModes,
87 kRecord_benchModes,
88 kPictureRecord_benchModes
89};
90
91///////////////////////////////////////////////////////////////////////////////
92
reed@android.com6c924ad2009-03-31 03:48:49 +000093static void erase(SkBitmap& bm) {
94 if (bm.config() == SkBitmap::kA8_Config) {
95 bm.eraseColor(0);
96 } else {
97 bm.eraseColor(SK_ColorWHITE);
98 }
99}
100
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000101#if 0
reed@android.com6c924ad2009-03-31 03:48:49 +0000102static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
103 if (bm1.width() != bm2.width() ||
104 bm1.height() != bm2.height() ||
105 bm1.config() != bm2.config()) {
106 return false;
107 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000108
reed@android.com6c924ad2009-03-31 03:48:49 +0000109 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
110 for (int y = 0; y < bm1.height(); y++) {
111 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
112 return false;
113 }
114 }
reed@android.com6c924ad2009-03-31 03:48:49 +0000115 return true;
116}
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000117#endif
reed@android.com6c924ad2009-03-31 03:48:49 +0000118
reed@android.combd700c32009-01-05 03:34:50 +0000119class Iter {
120public:
reed@android.come9d00602009-09-02 21:12:42 +0000121 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +0000122 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +0000123 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +0000124 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000125
reed@android.combd700c32009-01-05 03:34:50 +0000126 SkBenchmark* next() {
127 if (fBench) {
128 BenchRegistry::Factory f = fBench->factory();
129 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +0000130 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +0000131 }
132 return NULL;
133 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000134
reed@android.combd700c32009-01-05 03:34:50 +0000135private:
136 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +0000137 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +0000138};
139
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000140class AutoPrePostDraw {
141public:
142 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
143 fBench->preDraw();
144 }
145 ~AutoPrePostDraw() {
146 fBench->postDraw();
147 }
148private:
149 SkBenchmark* fBench;
150};
151
reed@android.combd700c32009-01-05 03:34:50 +0000152static void make_filename(const char name[], SkString* path) {
153 path->set(name);
154 for (int i = 0; name[i]; i++) {
155 switch (name[i]) {
156 case '/':
157 case '\\':
158 case ' ':
159 case ':':
160 path->writable_str()[i] = '-';
161 break;
162 default:
163 break;
164 }
165 }
166}
167
reed@android.com4c7d3d62009-01-21 03:15:13 +0000168static void saveFile(const char name[], const char config[], const char dir[],
169 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000170 SkBitmap copy;
171 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
172 return;
173 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000174
reed@android.comf523e252009-01-26 23:15:37 +0000175 if (bm.config() == SkBitmap::kA8_Config) {
176 // turn alpha into gray-scale
177 size_t size = copy.getSize() >> 2;
178 SkPMColor* p = copy.getAddr32(0, 0);
179 for (size_t i = 0; i < size; i++) {
180 int c = (*p >> SK_A32_SHIFT) & 0xFF;
181 c = 255 - c;
182 c |= (c << 24) | (c << 16) | (c << 8);
183 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
184 }
185 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000186
reed@android.com4c7d3d62009-01-21 03:15:13 +0000187 SkString str;
188 make_filename(name, &str);
189 str.appendf("_%s.png", config);
190 str.prepend(dir);
191 ::remove(str.c_str());
192 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
193 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000194}
195
196static void performClip(SkCanvas* canvas, int w, int h) {
197 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000198
reed@android.com4c7d3d62009-01-21 03:15:13 +0000199 r.set(SkIntToScalar(10), SkIntToScalar(10),
200 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
201 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000202
reed@android.com4c7d3d62009-01-21 03:15:13 +0000203 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
204 SkIntToScalar(w-10), SkIntToScalar(h-10));
205 canvas->clipRect(r, SkRegion::kXOR_Op);
206}
207
208static void performRotate(SkCanvas* canvas, int w, int h) {
209 const SkScalar x = SkIntToScalar(w) / 2;
210 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211
reed@android.com4c7d3d62009-01-21 03:15:13 +0000212 canvas->translate(x, y);
213 canvas->rotate(SkIntToScalar(35));
214 canvas->translate(-x, -y);
215}
216
reed@android.com387359e2009-08-04 01:51:09 +0000217static void performScale(SkCanvas* canvas, int w, int h) {
218 const SkScalar x = SkIntToScalar(w) / 2;
219 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000220
reed@android.com387359e2009-08-04 01:51:09 +0000221 canvas->translate(x, y);
222 // just enough so we can't take the sprite case
223 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
224 canvas->translate(-x, -y);
225}
226
reed@android.com29348cb2009-08-04 18:17:15 +0000227static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
228 if (argv < stop) {
229 *var = atoi(*argv) != 0;
230 return true;
231 }
232 return false;
233}
234
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000235enum Backend {
236 kRaster_Backend,
237 kGPU_Backend,
238 kPDF_Backend,
239};
240
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000241#if SK_SUPPORT_GPU
bsalomon@google.com508824b2011-12-13 16:49:49 +0000242class GLHelper {
243public:
244 GLHelper() {
245 }
246
247 bool init(SkGLContext* glCtx, int width, int height) {
248 GrContext* grCtx;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000249 if (!glCtx->init(width, height)) {
bsalomon@google.com508824b2011-12-13 16:49:49 +0000250 return false;
251 }
tomhudson@google.com8afae612012-08-14 15:03:35 +0000252 GrPlatform3DContext ctx =
253 reinterpret_cast<GrPlatform3DContext>(glCtx->gl());
254 grCtx = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
255 if (NULL != grCtx) {
256 GrPlatformRenderTargetDesc desc;
257 desc.fConfig = kSkia8888_PM_GrPixelConfig;
258 desc.fWidth = width;
259 desc.fHeight = height;
260 desc.fStencilBits = 8;
261 desc.fRenderTargetHandle = glCtx->getFBOID();
262 GrRenderTarget* rt = grCtx->createPlatformRenderTarget(desc);
263 if (NULL == rt) {
264 grCtx->unref();
265 return false;
266 }
267 glCtx->ref();
268 fGLContext.reset(glCtx);
269 fGrContext.reset(grCtx);
270 fRenderTarget.reset(rt);
271 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000272 return true;
273 }
274
robertphillips@google.com0da37192012-03-19 14:42:13 +0000275 void cleanup() {
276 fGLContext.reset(NULL);
277 fGrContext.reset(NULL);
278 fRenderTarget.reset(NULL);
279 }
280
bsalomon@google.com508824b2011-12-13 16:49:49 +0000281 bool isValid() {
282 return NULL != fGLContext.get();
283 }
284
285 SkGLContext* glContext() {
286 return fGLContext.get();
287 }
288
289 GrRenderTarget* renderTarget() {
290 return fRenderTarget.get();
291 }
292
293 GrContext* grContext() {
294 return fGrContext.get();
295 }
296private:
297 SkAutoTUnref<SkGLContext> fGLContext;
298 SkAutoTUnref<GrContext> fGrContext;
299 SkAutoTUnref<GrRenderTarget> fRenderTarget;
300};
301
302static GLHelper gRealGLHelper;
303static GLHelper gNullGLHelper;
robertphillips@google.com0da37192012-03-19 14:42:13 +0000304static GLHelper gDebugGLHelper;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000305#if SK_ANGLE
306static GLHelper gANGLEGLHelper;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000307#endif // SK_ANGLE
308#else // !SK_SUPPORT_GPU
309class GLHelper;
310class SkGLContext;
311#endif // !SK_SUPPORT_GPU
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000312static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.com508824b2011-12-13 16:49:49 +0000313 Backend backend, GLHelper* glHelper) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000314 SkDevice* device = NULL;
315 SkBitmap bitmap;
316 bitmap.setConfig(config, size.fX, size.fY);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000317
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000318 switch (backend) {
319 case kRaster_Backend:
320 bitmap.allocPixels();
321 erase(bitmap);
reed@google.comaf951c92011-06-16 19:10:39 +0000322 device = new SkDevice(bitmap);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000323 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000324#if SK_SUPPORT_GPU
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000325 case kGPU_Backend:
bsalomon@google.com508824b2011-12-13 16:49:49 +0000326 device = new SkGpuDevice(glHelper->grContext(),
327 glHelper->renderTarget());
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000328 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000329#endif
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000330 case kPDF_Backend:
331 default:
332 SkASSERT(!"unsupported");
333 }
334 return device;
335}
336
reed@android.com4bc19832009-01-19 20:08:35 +0000337static const struct {
338 SkBitmap::Config fConfig;
339 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000340 Backend fBackend;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000341 GLHelper* fGLHelper;
reed@android.com4bc19832009-01-19 20:08:35 +0000342} gConfigs[] = {
bsalomon@google.com508824b2011-12-13 16:49:49 +0000343 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend, NULL },
344 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend, NULL },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000345#if SK_SUPPORT_GPU
bsalomon@google.com508824b2011-12-13 16:49:49 +0000346 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend, &gRealGLHelper },
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000347#if SK_ANGLE
348 { SkBitmap::kARGB_8888_Config, "ANGLE", kGPU_Backend, &gANGLEGLHelper },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000349#endif // SK_ANGLE
robertphillips@google.coma0b63b82012-03-30 12:23:56 +0000350#ifdef SK_DEBUG
351 { SkBitmap::kARGB_8888_Config, "Debug", kGPU_Backend, &gDebugGLHelper },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000352#endif // SK_DEBUG
bsalomon@google.com508824b2011-12-13 16:49:49 +0000353 { SkBitmap::kARGB_8888_Config, "NULLGPU", kGPU_Backend, &gNullGLHelper },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000354#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000355};
356
reed@android.com4c7d3d62009-01-21 03:15:13 +0000357static int findConfig(const char config[]) {
358 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
359 if (!strcmp(config, gConfigs[i].fName)) {
360 return i;
361 }
362 }
363 return -1;
364}
365
tomhudson@google.com83823942011-10-28 14:59:07 +0000366static void determine_gpu_context_size(SkTDict<const char*>& defineDict,
367 int* contextWidth,
368 int* contextHeight) {
369 Iter iter(&defineDict);
370 SkBenchmark* bench;
371 while ((bench = iter.next()) != NULL) {
372 SkIPoint dim = bench->getSize();
373 if (*contextWidth < dim.fX) {
374 *contextWidth = dim.fX;
375 }
376 if (*contextHeight < dim.fY) {
377 *contextHeight = dim.fY;
378 }
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000379 bench->unref();
tomhudson@google.com83823942011-10-28 14:59:07 +0000380 }
381}
382
reed@google.com1a7eabc2011-10-31 16:33:52 +0000383static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
384 if (0 == array.count()) {
385 // no names, so don't skip anything
386 return false;
387 }
388 for (int i = 0; i < array.count(); ++i) {
389 if (strstr(name, array[i])) {
390 // found the name, so don't skip
391 return false;
392 }
393 }
394 return true;
395}
396
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000397static void help() {
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000398 SkDebugf("Usage: bench [-o outDir] [-repeat nr] [-logPerIter 1|0] "
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000399 "[-timers [wcgWC]*] [-rotate]\n"
robertphillips@google.com2abed832012-08-23 17:50:06 +0000400 " [-scale] [-clip] [-min] [-forceAA 1|0] [-forceFilter 1|0]\n"
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000401 " [-forceDither 1|0] [-forceBlend 1|0] [-strokeWidth width]\n"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000402 " [-match name] [-mode normal|deferred|record|picturerecord]\n"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000403 " [-config 8888|565|GPU|ANGLE|NULLGPU] [-Dfoo bar]\n"
404 " [-h|--help]");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000405 SkDebugf("\n\n");
406 SkDebugf(" -o outDir : Image of each bench will be put in outDir.\n");
407 SkDebugf(" -repeat nr : Each bench repeats for nr times.\n");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000408 SkDebugf(" -logPerIter 1|0 : "
409 "Log each repeat timer instead of mean, default is disabled.\n");
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000410 SkDebugf(" -timers [wcgWC]* : "
411 "Display wall, cpu, gpu, truncated wall or truncated cpu time for each bench.\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000412 SkDebugf(" -rotate : Rotate before each bench runs.\n");
413 SkDebugf(" -scale : Scale before each bench runs.\n");
414 SkDebugf(" -clip : Clip before each bench runs.\n");
robertphillips@google.com2abed832012-08-23 17:50:06 +0000415 SkDebugf(" -min : Print the minimum times (instead of average).\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000416 SkDebugf(" -forceAA 1|0 : "
417 "Enable/disable anti-aliased, default is enabled.\n");
418 SkDebugf(" -forceFilter 1|0 : "
419 "Enable/disable bitmap filtering, default is disabled.\n");
420 SkDebugf(" -forceDither 1|0 : "
421 "Enable/disable dithering, default is disabled.\n");
422 SkDebugf(" -forceBlend 1|0 : "
423 "Enable/disable dithering, default is disabled.\n");
424 SkDebugf(" -strokeWidth width : The width for path stroke.\n");
425 SkDebugf(" -match name : Only run bench whose name is matched.\n");
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000426 SkDebugf(" -mode normal|deferred|record|picturerecord : Run in the corresponding mode\n"
427 " normal, Use a normal canvas to draw to;\n"
428 " deferred, Use a deferrred canvas when drawing;\n"
429 " record, Benchmark the time to record to an SkPicture;\n"
430 " picturerecord, Benchmark the time to do record from a \n"
431 " SkPicture to a SkPicture.\n");
borenet@google.coma2b32b82012-08-07 12:58:50 +0000432 SkDebugf(" -logFile : destination for writing log output, in addition to stdout.\n");
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000433#if SK_SUPPORT_GPU
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000434 SkDebugf(" -config 8888|565|GPU|ANGLE|NULLGPU : "
435 "Run bench in corresponding config mode.\n");
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000436#else
437 SkDebugf(" -config 8888|565: "
438 "Run bench in corresponding config mode.\n");
439#endif
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000440 SkDebugf(" -Dfoo bar : Add extra definition to bench.\n");
441 SkDebugf(" -h|--help : Show this help message.\n");
442}
443
reed@android.combd700c32009-01-05 03:34:50 +0000444int main (int argc, char * const argv[]) {
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000445#ifdef SK_ENABLE_INST_COUNT
446 gPrintInstCount = true;
447#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000448 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000449
reed@android.come9d00602009-09-02 21:12:42 +0000450 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000451 int repeatDraw = 1;
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000452 bool logPerIter = false;
reed@android.com4bc19832009-01-19 20:08:35 +0000453 int forceAlpha = 0xFF;
454 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000455 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000456 SkTriState::State forceDither = SkTriState::kDefault;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000457 bool timerWall = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000458 bool truncatedTimerWall = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000459 bool timerCpu = true;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000460 bool truncatedTimerCpu = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000461 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000462 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000463 bool doRotate = false;
464 bool doClip = false;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000465 bool printMin = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000466 bool hasStrokeWidth = false;
467 float strokeWidth;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000468 SkTDArray<const char*> fMatches;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000469 benchModes benchMode = kNormal_benchModes;
470 SkString perIterTimeformat("%.2f");
471 SkString normalTimeFormat("%6.2f");
472
reed@android.comb398fe82009-01-07 11:47:57 +0000473 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000474 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000475 GLHelper* glHelper = NULL;
reed@android.com387359e2009-08-04 01:51:09 +0000476 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000477 Backend backend = kRaster_Backend; // for warning
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000478 SkTDArray<int> configs;
479 bool userConfig = false;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000480
reed@android.comb398fe82009-01-07 11:47:57 +0000481 char* const* stop = argv + argc;
482 for (++argv; argv < stop; ++argv) {
483 if (strcmp(*argv, "-o") == 0) {
484 argv++;
485 if (argv < stop && **argv) {
486 outDir.set(*argv);
487 if (outDir.c_str()[outDir.size() - 1] != '/') {
488 outDir.append("/");
489 }
490 }
reed@android.com4bc19832009-01-19 20:08:35 +0000491 } else if (strcmp(*argv, "-repeat") == 0) {
492 argv++;
493 if (argv < stop) {
494 repeatDraw = atoi(*argv);
495 if (repeatDraw < 1) {
496 repeatDraw = 1;
497 }
498 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000499 logger.logError("missing arg for -repeat\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000500 help();
reed@android.com4bc19832009-01-19 20:08:35 +0000501 return -1;
502 }
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000503 } else if (strcmp(*argv, "-logPerIter") == 0) {
504 if (!parse_bool_arg(++argv, stop, &logPerIter)) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000505 logger.logError("missing arg for -logPerIter\n");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000506 help();
507 return -1;
508 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000509 } else if (strcmp(*argv, "-timers") == 0) {
510 argv++;
511 if (argv < stop) {
512 timerWall = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000513 truncatedTimerWall = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000514 timerCpu = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000515 truncatedTimerCpu = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000516 timerGpu = false;
517 for (char* t = *argv; *t; ++t) {
518 switch (*t) {
519 case 'w': timerWall = true; break;
520 case 'c': timerCpu = true; break;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000521 case 'W': truncatedTimerWall = true; break;
522 case 'C': truncatedTimerCpu = true; break;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000523 case 'g': timerGpu = true; break;
524 }
525 }
526 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000527 logger.logError("missing arg for -timers\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000528 help();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000529 return -1;
530 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000531 } else if (!strcmp(*argv, "-rotate")) {
532 doRotate = true;
reed@android.com387359e2009-08-04 01:51:09 +0000533 } else if (!strcmp(*argv, "-scale")) {
534 doScale = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000535 } else if (!strcmp(*argv, "-clip")) {
536 doClip = true;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000537 } else if (!strcmp(*argv, "-min")) {
538 printMin = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000539 } else if (strcmp(*argv, "-forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000540 if (!parse_bool_arg(++argv, stop, &forceAA)) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000541 logger.logError("missing arg for -forceAA\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000542 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000543 return -1;
544 }
545 } else if (strcmp(*argv, "-forceFilter") == 0) {
546 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000547 logger.logError("missing arg for -forceFilter\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000548 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000549 return -1;
550 }
reed@android.com4e635f92009-10-19 17:39:46 +0000551 } else if (strcmp(*argv, "-forceDither") == 0) {
552 bool tmp;
553 if (!parse_bool_arg(++argv, stop, &tmp)) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000554 logger.logError("missing arg for -forceDither\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000555 help();
reed@android.com4e635f92009-10-19 17:39:46 +0000556 return -1;
557 }
558 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
reed@android.com4bc19832009-01-19 20:08:35 +0000559 } else if (strcmp(*argv, "-forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000560 bool wantAlpha = false;
561 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000562 logger.logError("missing arg for -forceBlend\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000563 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000564 return -1;
565 }
566 forceAlpha = wantAlpha ? 0x80 : 0xFF;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000567 } else if (strcmp(*argv, "-mode") == 0) {
568 argv++;
569 if (argv < stop) {
570 if (strcmp(*argv, "normal") == 0) {
571 benchMode = kNormal_benchModes;
572 } else if (strcmp(*argv, "deferred") == 0) {
573 benchMode = kDeferred_benchModes;
574 } else if (strcmp(*argv, "record") == 0) {
575 benchMode = kRecord_benchModes;
576 } else if (strcmp(*argv, "picturerecord") == 0) {
577 benchMode = kPictureRecord_benchModes;
578 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000579 logger.logError("bad arg for -mode\n");
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000580 help();
581 return -1;
582 }
583 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000584 logger.logError("missing arg for -mode\n");
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000585 help();
586 return -1;
587 }
agl@chromium.org652807b2010-04-27 15:47:34 +0000588 } else if (strcmp(*argv, "-strokeWidth") == 0) {
589 argv++;
590 if (argv < stop) {
591 const char *strokeWidthStr = *argv;
592 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000593 logger.logError("bad arg for -strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000594 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000595 return -1;
596 }
597 hasStrokeWidth = true;
598 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000599 logger.logError("missing arg for -strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000600 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000601 return -1;
602 }
reed@android.com387359e2009-08-04 01:51:09 +0000603 } else if (strcmp(*argv, "-match") == 0) {
604 argv++;
605 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000606 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000607 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000608 logger.logError("missing arg for -match\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000609 help();
reed@android.com387359e2009-08-04 01:51:09 +0000610 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000611 }
reed@android.com387359e2009-08-04 01:51:09 +0000612 } else if (strcmp(*argv, "-config") == 0) {
613 argv++;
614 if (argv < stop) {
615 int index = findConfig(*argv);
616 if (index >= 0) {
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000617 *configs.append() = index;
618 userConfig = true;
reed@android.com387359e2009-08-04 01:51:09 +0000619 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000620 SkString str;
621 str.printf("unrecognized config %s\n", *argv);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000622 logger.logError(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000623 help();
reed@android.com387359e2009-08-04 01:51:09 +0000624 return -1;
625 }
626 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000627 logger.logError("missing arg for -config\n");
628 help();
629 return -1;
630 }
631 } else if (strcmp(*argv, "-logFile") == 0) {
632 argv++;
633 if (argv < stop) {
634 if (!logger.SetLogFile(*argv)) {
635 SkString str;
636 str.printf("Could not open %s for writing.", *argv);
637 return -1;
638 }
639 } else {
640 logger.logError("missing arg for -logFile\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000641 help();
reed@android.com387359e2009-08-04 01:51:09 +0000642 return -1;
643 }
reed@android.com0c9da392010-02-22 19:50:13 +0000644 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000645 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000646 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000647 defineDict.set(argv[-1] + 2, *argv);
648 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000649 logger.logError("incomplete '-Dfoo bar' definition\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000650 help();
reed@android.come9d00602009-09-02 21:12:42 +0000651 return -1;
652 }
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000653 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
654 help();
655 return 0;
reed@android.com387359e2009-08-04 01:51:09 +0000656 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000657 SkString str;
658 str.printf("unrecognized arg %s\n", *argv);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000659 logger.logError(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000660 help();
reed@android.com387359e2009-08-04 01:51:09 +0000661 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000662 }
663 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000664 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)
665 && !outDir.isEmpty()) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000666 logger.logError("'-mode record' and '-mode picturerecord' are not"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000667 " compatible with -o.\n");
668 return -1;
669 }
670 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)) {
671 perIterTimeformat.set("%.4f");
672 normalTimeFormat.set("%6.4f");
673 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000674 if (!userConfig) {
675 // if no config is specified by user, we add them all.
676 for (unsigned int i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
677 *configs.append() = i;
678 }
679 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000680
reed@google.com3b14dc12011-04-04 14:31:36 +0000681 // report our current settings
682 {
683 SkString str;
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000684 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d "
685 "deferred=%d logperiter=%d",
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000686 forceAlpha, forceAA, forceFilter, benchMode == kDeferred_benchModes,
687 logPerIter);
robertphillips@google.com2abed832012-08-23 17:50:06 +0000688 str.appendf(" rotate=%d scale=%d clip=%d min=%d",
689 doRotate, doScale, doClip, printMin);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000690 str.appendf(" record=%d picturerecord=%d",
691 benchMode == kRecord_benchModes,
692 benchMode == kPictureRecord_benchModes);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000693 const char * ditherName;
694 switch (forceDither) {
695 case SkTriState::kDefault: ditherName = "default"; break;
696 case SkTriState::kTrue: ditherName = "true"; break;
697 case SkTriState::kFalse: ditherName = "false"; break;
698 default: ditherName = "<invalid>"; break;
699 }
700 str.appendf(" dither=%s", ditherName);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000701
bungeman@google.coma5d48412011-06-15 17:25:46 +0000702 if (hasStrokeWidth) {
703 str.appendf(" strokeWidth=%f", strokeWidth);
704 } else {
705 str.append(" strokeWidth=none");
706 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000707
bungeman@google.coma5d48412011-06-15 17:25:46 +0000708#if defined(SK_SCALAR_IS_FLOAT)
709 str.append(" scalar=float");
710#elif defined(SK_SCALAR_IS_FIXED)
711 str.append(" scalar=fixed");
712#endif
713
714#if defined(SK_BUILD_FOR_WIN32)
715 str.append(" system=WIN32");
716#elif defined(SK_BUILD_FOR_MAC)
717 str.append(" system=MAC");
718#elif defined(SK_BUILD_FOR_ANDROID)
719 str.append(" system=ANDROID");
720#elif defined(SK_BUILD_FOR_UNIX)
721 str.append(" system=UNIX");
722#else
723 str.append(" system=other");
724#endif
725
726#if defined(SK_DEBUG)
727 str.append(" DEBUG");
728#endif
729 str.append("\n");
borenet@google.coma2b32b82012-08-07 12:58:50 +0000730 logger.logProgress(str);
reed@google.com3b14dc12011-04-04 14:31:36 +0000731 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000732
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000733 SkGLContext* timerCtx = NULL;
bungeman@google.coma5d48412011-06-15 17:25:46 +0000734 //Don't do GL when fixed.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000735#if !defined(SK_SCALAR_IS_FIXED) && SK_SUPPORT_GPU
tomhudson@google.com83823942011-10-28 14:59:07 +0000736 int contextWidth = 1024;
737 int contextHeight = 1024;
738 determine_gpu_context_size(defineDict, &contextWidth, &contextHeight);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000739 SkAutoTUnref<SkGLContext> realGLCtx(new SkNativeGLContext);
740 SkAutoTUnref<SkGLContext> nullGLCtx(new SkNullGLContext);
robertphillips@google.com0da37192012-03-19 14:42:13 +0000741 SkAutoTUnref<SkGLContext> debugGLCtx(new SkDebugGLContext);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000742 gRealGLHelper.init(realGLCtx.get(), contextWidth, contextHeight);
743 gNullGLHelper.init(nullGLCtx.get(), contextWidth, contextHeight);
robertphillips@google.com0da37192012-03-19 14:42:13 +0000744 gDebugGLHelper.init(debugGLCtx.get(), contextWidth, contextHeight);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000745#if SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000746 SkAutoTUnref<SkGLContext> angleGLCtx(new SkANGLEGLContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000747 gANGLEGLHelper.init(angleGLCtx.get(), contextWidth, contextHeight);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000748#endif // SK_ANGLE
749 timerCtx = gRealGLHelper.glContext();
750#endif // !defined(SK_SCALAR_IS_FIXED) && SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +0000751
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000752 BenchTimer timer = BenchTimer(timerCtx);
reed@android.come9d00602009-09-02 21:12:42 +0000753 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000754 SkBenchmark* bench;
755 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000756 SkAutoTUnref<SkBenchmark> benchUnref(bench);
757
reed@android.comb398fe82009-01-07 11:47:57 +0000758 SkIPoint dim = bench->getSize();
759 if (dim.fX <= 0 || dim.fY <= 0) {
760 continue;
761 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000762
reed@android.com4bc19832009-01-19 20:08:35 +0000763 bench->setForceAlpha(forceAlpha);
764 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000765 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000766 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000767 if (hasStrokeWidth) {
768 bench->setStrokeWidth(strokeWidth);
769 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000770
reed@android.com387359e2009-08-04 01:51:09 +0000771 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000772 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000773 continue;
774 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775
reed@android.com29348cb2009-08-04 18:17:15 +0000776 {
777 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000778 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000779 bench->getName());
borenet@google.coma2b32b82012-08-07 12:58:50 +0000780 logger.logProgress(str);
reed@android.com29348cb2009-08-04 18:17:15 +0000781 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000782
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000783 AutoPrePostDraw appd(bench);
784
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000785 for (int x = 0; x < configs.count(); ++x) {
786 int configIndex = configs[x];
787
788 outConfig = gConfigs[configIndex].fConfig;
789 configName = gConfigs[configIndex].fName;
790 backend = gConfigs[configIndex].fBackend;
791 glHelper = gConfigs[configIndex].fGLHelper;
bsalomon@google.com508824b2011-12-13 16:49:49 +0000792
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000793#if SK_SUPPORT_GPU
bsalomon@google.com508824b2011-12-13 16:49:49 +0000794 if (kGPU_Backend == backend &&
795 (NULL == glHelper || !glHelper->isValid())) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000796 continue;
797 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000798#endif
bsalomon@google.com508824b2011-12-13 16:49:49 +0000799 SkDevice* device = make_device(outConfig, dim, backend, glHelper);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000800 SkCanvas* canvas = NULL;
801 SkPicture pictureRecordFrom;
802 SkPicture pictureRecordTo;
803 switch(benchMode) {
804 case kDeferred_benchModes:
805 canvas = new SkDeferredCanvas(device);
806 break;
807 case kRecord_benchModes:
808 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY);
809 canvas->ref();
810 break;
811 case kPictureRecord_benchModes: {
812 // This sets up picture-to-picture recording.
813 // The C++ drawing calls for the benchmark are recorded into
814 // pictureRecordFrom. As the benchmark, we will time how
815 // long it takes to playback pictureRecordFrom into
816 // pictureRecordTo.
817 SkCanvas* tempCanvas = pictureRecordFrom.beginRecording(dim.fX, dim.fY);
818 bench->draw(tempCanvas);
819 pictureRecordFrom.endRecording();
820 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY);
821 canvas->ref();
822 break;
823 }
824 case kNormal_benchModes:
825 canvas = new SkCanvas(device);
826 break;
827 default:
828 SkASSERT(0);
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000829 }
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000830 device->unref();
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000831 SkAutoUnref canvasUnref(canvas);
832
reed@android.com4c7d3d62009-01-21 03:15:13 +0000833 if (doClip) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000834 performClip(canvas, dim.fX, dim.fY);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000835 }
reed@android.com387359e2009-08-04 01:51:09 +0000836 if (doScale) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000837 performScale(canvas, dim.fX, dim.fY);
reed@android.com387359e2009-08-04 01:51:09 +0000838 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000839 if (doRotate) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000840 performRotate(canvas, dim.fX, dim.fY);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000841 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000842
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000843 // warm up caches if needed
reed@android.comeca48362010-12-20 18:34:17 +0000844 if (repeatDraw > 1) {
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000845#if SK_SUPPORT_GPU
846 if (glHelper) {
847 // purge the GPU resources to reduce variance
848 glHelper->grContext()->freeGpuResources();
849 }
850#endif
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000851 SkAutoCanvasRestore acr(canvas, true);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000852 if (benchMode == kPictureRecord_benchModes) {
853 pictureRecordFrom.draw(canvas);
854 } else {
855 bench->draw(canvas);
856 }
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000857 canvas->flush();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000858#if SK_SUPPORT_GPU
bsalomon@google.com508824b2011-12-13 16:49:49 +0000859 if (glHelper) {
860 glHelper->grContext()->flush();
861 SK_GL(*glHelper->glContext(), Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000862 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000863#endif
reed@android.comeca48362010-12-20 18:34:17 +0000864 }
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000865
866 // record timer values for each repeat, and their sum
867 SkString fWallStr(" msecs = ");
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000868 SkString fTruncatedWallStr(" Wmsecs = ");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000869 SkString fCpuStr(" cmsecs = ");
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000870 SkString fTruncatedCpuStr(" Cmsecs = ");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000871 SkString fGpuStr(" gmsecs = ");
robertphillips@google.com2abed832012-08-23 17:50:06 +0000872 double fWallSum = 0.0, fWallMin;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000873 double fTruncatedWallSum = 0.0, fTruncatedWallMin;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000874 double fCpuSum = 0.0, fCpuMin;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000875 double fTruncatedCpuSum = 0.0, fTruncatedCpuMin;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000876 double fGpuSum = 0.0, fGpuMin;
reed@android.com4bc19832009-01-19 20:08:35 +0000877 for (int i = 0; i < repeatDraw; i++) {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000878 if ((benchMode == kRecord_benchModes
879 || benchMode == kPictureRecord_benchModes)) {
880 // This will clear the recorded commands so that they do not
881 // acculmulate.
882 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY);
883 }
884
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000885 timer.start();
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000886 SkAutoCanvasRestore acr(canvas, true);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000887 if (benchMode == kPictureRecord_benchModes) {
888 pictureRecordFrom.draw(canvas);
889 } else {
890 bench->draw(canvas);
891 }
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000892 canvas->flush();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000893
894 // stop the truncated timer after the last canvas call but
895 // don't wait for all the GL calls to complete
896 timer.truncatedEnd();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000897#if SK_SUPPORT_GPU
bsalomon@google.com508824b2011-12-13 16:49:49 +0000898 if (glHelper) {
899 glHelper->grContext()->flush();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000900 SK_GL(*glHelper->glContext(), Finish());
reed@google.com25df8882011-07-14 19:03:58 +0000901 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000902#endif
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000903 // stop the inclusive and gpu timers once all the GL calls
904 // have completed
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000905 timer.end();
906
907 if (i == repeatDraw - 1) {
908 // no comma after the last value
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000909 fWallStr.appendf(perIterTimeformat.c_str(), timer.fWall);
910 fCpuStr.appendf(perIterTimeformat.c_str(), timer.fCpu);
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000911 fTruncatedWallStr.appendf(perIterTimeformat.c_str(), timer.fTruncatedWall);
912 fTruncatedCpuStr.appendf(perIterTimeformat.c_str(), timer.fTruncatedCpu);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000913 fGpuStr.appendf(perIterTimeformat.c_str(), timer.fGpu);
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000914 } else {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000915 fWallStr.appendf(perIterTimeformat.c_str(), timer.fWall);
916 fWallStr.appendf(",");
917 fCpuStr.appendf(perIterTimeformat.c_str(), timer.fCpu);
918 fCpuStr.appendf(",");
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000919 fTruncatedWallStr.appendf(perIterTimeformat.c_str(), timer.fTruncatedWall);
920 fTruncatedWallStr.appendf(",");
921 fTruncatedCpuStr.appendf(perIterTimeformat.c_str(), timer.fTruncatedCpu);
922 fTruncatedCpuStr.appendf(",");
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000923 fGpuStr.appendf(perIterTimeformat.c_str(), timer.fGpu);
924 fGpuStr.appendf(",");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000925 }
robertphillips@google.com2abed832012-08-23 17:50:06 +0000926
927 if (0 == i) {
928 fWallMin = timer.fWall;
929 fCpuMin = timer.fCpu;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000930 fTruncatedWallMin = timer.fTruncatedWall;
931 fTruncatedCpuMin = timer.fTruncatedCpu;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000932 fGpuMin = timer.fGpu;
933 } else {
934 fWallMin = Min(fWallMin, timer.fWall);
935 fCpuMin = Min(fCpuMin, timer.fCpu);
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000936 fTruncatedWallMin = Min(fTruncatedWallMin, timer.fTruncatedWall);
937 fTruncatedCpuMin = Min(fTruncatedCpuMin, timer.fTruncatedCpu);
robertphillips@google.com2abed832012-08-23 17:50:06 +0000938 fGpuMin = Min(fGpuMin, timer.fGpu);
939 }
940
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000941 fWallSum += timer.fWall;
942 fCpuSum += timer.fCpu;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000943 fTruncatedWallSum += timer.fTruncatedWall;
944 fTruncatedCpuSum += timer.fTruncatedCpu;
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000945 fGpuSum += timer.fGpu;
reed@google.com25df8882011-07-14 19:03:58 +0000946 }
reed@android.com4bc19832009-01-19 20:08:35 +0000947 if (repeatDraw > 1) {
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000948 // output each repeat (no average) if logPerIter is set,
949 // otherwise output only the average
950 if (!logPerIter) {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000951 fWallStr.set(" msecs = ");
robertphillips@google.com2abed832012-08-23 17:50:06 +0000952 fWallStr.appendf(normalTimeFormat.c_str(),
953 printMin ? fWallMin : fWallSum / repeatDraw);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000954 fCpuStr.set(" cmsecs = ");
robertphillips@google.com2abed832012-08-23 17:50:06 +0000955 fCpuStr.appendf(normalTimeFormat.c_str(),
956 printMin ? fCpuMin : fCpuSum / repeatDraw);
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000957 fTruncatedWallStr.set(" Wmsecs = ");
958 fTruncatedWallStr.appendf(normalTimeFormat.c_str(),
959 printMin ? fTruncatedWallMin : fTruncatedWallSum / repeatDraw);
960 fTruncatedCpuStr.set(" Cmsecs = ");
961 fTruncatedCpuStr.appendf(normalTimeFormat.c_str(),
962 printMin ? fTruncatedCpuMin : fTruncatedCpuSum / repeatDraw);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000963 fGpuStr.set(" gmsecs = ");
robertphillips@google.com2abed832012-08-23 17:50:06 +0000964 fGpuStr.appendf(normalTimeFormat.c_str(),
965 printMin ? fGpuMin : fGpuSum / repeatDraw);
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000966 }
reed@android.com29348cb2009-08-04 18:17:15 +0000967 SkString str;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000968 str.printf(" %4s:", configName);
969 if (timerWall) {
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000970 str += fWallStr;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000971 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000972 if (truncatedTimerWall) {
973 str += fTruncatedWallStr;
974 }
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000975 if (timerCpu) {
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000976 str += fCpuStr;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000977 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000978 if (truncatedTimerCpu) {
979 str += fTruncatedCpuStr;
980 }
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000981 if (timerGpu && glHelper && fGpuSum > 0) {
982 str += fGpuStr;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000983 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000984 logger.logProgress(str);
reed@android.com4bc19832009-01-19 20:08:35 +0000985 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000986 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000987 saveFile(bench->getName(), configName, outDir.c_str(),
988 device->accessBitmap(false));
reed@android.com4c7d3d62009-01-21 03:15:13 +0000989 }
reed@android.com4bc19832009-01-19 20:08:35 +0000990 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000991 logger.logProgress(SkString("\n"));
reed@android.combd700c32009-01-05 03:34:50 +0000992 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000993#if SK_SUPPORT_GPU
robertphillips@google.com0da37192012-03-19 14:42:13 +0000994 // need to clean up here rather than post-main to allow leak detection to work
995 gDebugGLHelper.cleanup();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000996#endif
reed@android.combd700c32009-01-05 03:34:50 +0000997 return 0;
998}