blob: f90d9639e793144886d8479f880c75d580c504f0 [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"
bsalomon@google.comcb265352013-02-22 16:13:16 +000014#include "GrContextFactory.h"
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000015#include "gl/GrGLDefines.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000016#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#include "SkGpuDevice.h"
bsalomon@google.com41809932013-02-22 16:25:28 +000018#else
19class GrContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000020#endif // SK_SUPPORT_GPU
bsalomon@google.com971d0c82011-08-19 17:22:05 +000021
scroggo@google.com9a412522012-09-07 15:21:18 +000022#include "SkBenchLogger.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000023#include "SkBenchmark.h"
reed@android.combd700c32009-01-05 03:34:50 +000024#include "SkCanvas.h"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +000025#include "SkDeferredCanvas.h"
junov@chromium.org44b67b22012-08-07 18:00:31 +000026#include "SkDevice.h"
reed@android.comf523e252009-01-26 23:15:37 +000027#include "SkColorPriv.h"
reed@android.com3a859a02009-01-28 00:56:29 +000028#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000029#include "SkImageEncoder.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000030#include "SkNWayCanvas.h"
31#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +000032#include "SkString.h"
bsalomon@google.com41809932013-02-22 16:25:28 +000033#include "SkTArray.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000034#include "TimerData.h"
reed@android.com29348cb2009-08-04 18:17:15 +000035
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000036enum benchModes {
37 kNormal_benchModes,
38 kDeferred_benchModes,
junov@chromium.orgfb103892012-09-20 19:35:43 +000039 kDeferredSilent_benchModes,
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000040 kRecord_benchModes,
41 kPictureRecord_benchModes
42};
43
44///////////////////////////////////////////////////////////////////////////////
45
reed@android.com6c924ad2009-03-31 03:48:49 +000046static void erase(SkBitmap& bm) {
47 if (bm.config() == SkBitmap::kA8_Config) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000048 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com6c924ad2009-03-31 03:48:49 +000049 } else {
50 bm.eraseColor(SK_ColorWHITE);
51 }
52}
53
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000054#if 0
reed@android.com6c924ad2009-03-31 03:48:49 +000055static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
56 if (bm1.width() != bm2.width() ||
57 bm1.height() != bm2.height() ||
58 bm1.config() != bm2.config()) {
59 return false;
60 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061
reed@android.com6c924ad2009-03-31 03:48:49 +000062 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
63 for (int y = 0; y < bm1.height(); y++) {
64 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
65 return false;
66 }
67 }
reed@android.com6c924ad2009-03-31 03:48:49 +000068 return true;
69}
mike@reedtribe.orga9015f82011-05-17 02:25:05 +000070#endif
reed@android.com6c924ad2009-03-31 03:48:49 +000071
reed@android.combd700c32009-01-05 03:34:50 +000072class Iter {
73public:
reed@android.come9d00602009-09-02 21:12:42 +000074 Iter(void* param) {
reed@android.combd700c32009-01-05 03:34:50 +000075 fBench = BenchRegistry::Head();
reed@android.come9d00602009-09-02 21:12:42 +000076 fParam = param;
reed@android.combd700c32009-01-05 03:34:50 +000077 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078
reed@android.combd700c32009-01-05 03:34:50 +000079 SkBenchmark* next() {
80 if (fBench) {
81 BenchRegistry::Factory f = fBench->factory();
82 fBench = fBench->next();
reed@android.come9d00602009-09-02 21:12:42 +000083 return f(fParam);
reed@android.combd700c32009-01-05 03:34:50 +000084 }
85 return NULL;
86 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000087
reed@android.combd700c32009-01-05 03:34:50 +000088private:
89 const BenchRegistry* fBench;
reed@android.come9d00602009-09-02 21:12:42 +000090 void* fParam;
reed@android.combd700c32009-01-05 03:34:50 +000091};
92
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000093class AutoPrePostDraw {
94public:
95 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
96 fBench->preDraw();
97 }
98 ~AutoPrePostDraw() {
99 fBench->postDraw();
100 }
101private:
102 SkBenchmark* fBench;
103};
104
reed@android.combd700c32009-01-05 03:34:50 +0000105static void make_filename(const char name[], SkString* path) {
106 path->set(name);
107 for (int i = 0; name[i]; i++) {
108 switch (name[i]) {
109 case '/':
110 case '\\':
111 case ' ':
112 case ':':
113 path->writable_str()[i] = '-';
114 break;
115 default:
116 break;
117 }
118 }
119}
120
reed@android.com4c7d3d62009-01-21 03:15:13 +0000121static void saveFile(const char name[], const char config[], const char dir[],
122 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000123 SkBitmap copy;
124 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
125 return;
126 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000127
reed@android.comf523e252009-01-26 23:15:37 +0000128 if (bm.config() == SkBitmap::kA8_Config) {
129 // turn alpha into gray-scale
130 size_t size = copy.getSize() >> 2;
131 SkPMColor* p = copy.getAddr32(0, 0);
132 for (size_t i = 0; i < size; i++) {
133 int c = (*p >> SK_A32_SHIFT) & 0xFF;
134 c = 255 - c;
135 c |= (c << 24) | (c << 16) | (c << 8);
136 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
137 }
138 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000139
reed@android.com4c7d3d62009-01-21 03:15:13 +0000140 SkString str;
141 make_filename(name, &str);
142 str.appendf("_%s.png", config);
143 str.prepend(dir);
144 ::remove(str.c_str());
145 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
146 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000147}
148
149static void performClip(SkCanvas* canvas, int w, int h) {
150 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000151
reed@android.com4c7d3d62009-01-21 03:15:13 +0000152 r.set(SkIntToScalar(10), SkIntToScalar(10),
153 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
154 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155
reed@android.com4c7d3d62009-01-21 03:15:13 +0000156 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
157 SkIntToScalar(w-10), SkIntToScalar(h-10));
158 canvas->clipRect(r, SkRegion::kXOR_Op);
159}
160
161static void performRotate(SkCanvas* canvas, int w, int h) {
162 const SkScalar x = SkIntToScalar(w) / 2;
163 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000164
reed@android.com4c7d3d62009-01-21 03:15:13 +0000165 canvas->translate(x, y);
166 canvas->rotate(SkIntToScalar(35));
167 canvas->translate(-x, -y);
168}
169
reed@android.com387359e2009-08-04 01:51:09 +0000170static void performScale(SkCanvas* canvas, int w, int h) {
171 const SkScalar x = SkIntToScalar(w) / 2;
172 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000173
reed@android.com387359e2009-08-04 01:51:09 +0000174 canvas->translate(x, y);
175 // just enough so we can't take the sprite case
176 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
177 canvas->translate(-x, -y);
178}
179
reed@android.com29348cb2009-08-04 18:17:15 +0000180static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
181 if (argv < stop) {
182 *var = atoi(*argv) != 0;
183 return true;
184 }
185 return false;
186}
187
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000188enum Backend {
189 kRaster_Backend,
190 kGPU_Backend,
191 kPDF_Backend,
192};
193
194static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.comcb265352013-02-22 16:13:16 +0000195 Backend backend, GrContext* context) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000196 SkDevice* device = NULL;
197 SkBitmap bitmap;
198 bitmap.setConfig(config, size.fX, size.fY);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000200 switch (backend) {
201 case kRaster_Backend:
202 bitmap.allocPixels();
203 erase(bitmap);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000204 device = SkNEW_ARGS(SkDevice, (bitmap));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000205 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000206#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000207 case kGPU_Backend: {
208 GrTextureDesc desc;
209 desc.fConfig = kSkia8888_GrPixelConfig;
210 desc.fFlags = kRenderTarget_GrTextureFlagBit;
211 desc.fWidth = size.fX;
212 desc.fHeight = size.fY;
213 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
214 if (!texture) {
215 return NULL;
216 }
217 device = SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000218 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000219 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000220#endif
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000221 case kPDF_Backend:
222 default:
223 SkASSERT(!"unsupported");
224 }
225 return device;
226}
227
bsalomon@google.comcb265352013-02-22 16:13:16 +0000228#if SK_SUPPORT_GPU
229GrContextFactory gContextFactory;
230typedef GrContextFactory::GLContextType GLContextType;
231static const GLContextType kDontCareGLCtxType = GrContextFactory::kNative_GLContextType;
232#else
233typedef int GLContextType;
234static const GLContextType kDontCareGLCtxType = 0;
235#endif
236
reed@android.com4bc19832009-01-19 20:08:35 +0000237static const struct {
238 SkBitmap::Config fConfig;
239 const char* fName;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000240 Backend fBackend;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000241 GLContextType fContextType;
reed@android.com4bc19832009-01-19 20:08:35 +0000242} gConfigs[] = {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000243 { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend, kDontCareGLCtxType },
244 { SkBitmap::kRGB_565_Config, "565", kRaster_Backend, kDontCareGLCtxType },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000245#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000246 { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend, GrContextFactory::kNative_GLContextType },
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000247#if SK_ANGLE
bsalomon@google.comcb265352013-02-22 16:13:16 +0000248 { SkBitmap::kARGB_8888_Config, "ANGLE", kGPU_Backend, GrContextFactory::kANGLE_GLContextType },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000249#endif // SK_ANGLE
robertphillips@google.coma0b63b82012-03-30 12:23:56 +0000250#ifdef SK_DEBUG
bsalomon@google.comcb265352013-02-22 16:13:16 +0000251 { SkBitmap::kARGB_8888_Config, "Debug", kGPU_Backend, GrContextFactory::kDebug_GLContextType },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000252#endif // SK_DEBUG
bsalomon@google.comcb265352013-02-22 16:13:16 +0000253 { SkBitmap::kARGB_8888_Config, "NULLGPU", kGPU_Backend, GrContextFactory::kNull_GLContextType },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000254#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000255};
256
reed@android.com4c7d3d62009-01-21 03:15:13 +0000257static int findConfig(const char config[]) {
258 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
259 if (!strcmp(config, gConfigs[i].fName)) {
260 return i;
261 }
262 }
263 return -1;
264}
265
reed@google.com1a7eabc2011-10-31 16:33:52 +0000266static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
267 if (0 == array.count()) {
268 // no names, so don't skip anything
269 return false;
270 }
271 for (int i = 0; i < array.count(); ++i) {
272 if (strstr(name, array[i])) {
273 // found the name, so don't skip
274 return false;
275 }
276 }
277 return true;
278}
279
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000280static void help() {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000281 SkDebugf("Usage: bench [-o outDir] [--repeat nr] [--logPerIter] "
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000282 "[--timers [wcgWC]*] [--rotate]\n"
283 " [--scale] [--clip] [--min] [--forceAA 1|0] [--forceFilter 1|0]\n"
bsalomon@google.comc732f252013-03-11 20:22:15 +0000284 " [--forceDither 1|0] [--forceBlend 1|0]"
285#if SK_SUPPORT_GPU
286 " [--gpuCacheSize <bytes> <count>]"
287#endif
288 "\n"
289 " [--strokeWidth width] [--match name]\n"
290 " [--mode normal|deferred|deferredSilent|record|picturerecord]\n"
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000291 " [--config 8888|565|GPU|ANGLE|NULLGPU] [-Dfoo bar] [--logFile filename]\n"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000292 " [-h|--help]");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000293 SkDebugf("\n\n");
294 SkDebugf(" -o outDir : Image of each bench will be put in outDir.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000295 SkDebugf(" --repeat nr : Each bench repeats for nr times.\n");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000296 SkDebugf(" --logPerIter : "
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000297 "Log each repeat timer instead of mean, default is disabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000298 SkDebugf(" --timers [wcgWC]* : "
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000299 "Display wall, cpu, gpu, truncated wall or truncated cpu time for each bench.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000300 SkDebugf(" --rotate : Rotate before each bench runs.\n");
301 SkDebugf(" --scale : Scale before each bench runs.\n");
302 SkDebugf(" --clip : Clip before each bench runs.\n");
303 SkDebugf(" --min : Print the minimum times (instead of average).\n");
304 SkDebugf(" --forceAA 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000305 "Enable/disable anti-aliased, default is enabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000306 SkDebugf(" --forceFilter 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000307 "Enable/disable bitmap filtering, default is disabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000308 SkDebugf(" --forceDither 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000309 "Enable/disable dithering, default is disabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000310 SkDebugf(" --forceBlend 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000311 "Enable/disable dithering, default is disabled.\n");
bsalomon@google.comc732f252013-03-11 20:22:15 +0000312#if SK_SUPPORT_GPU
313 SkDebugf(" --gpuCacheSize <bytes> <count>: "
314 "limits gpu cache to bytes size or object count.\n");
315 SkDebugf(" -1 for either value means use the default. 0 for either disables the cache.\n");
316#endif
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000317 SkDebugf(" --strokeWidth width : The width for path stroke.\n");
318 SkDebugf(" --match name : Only run bench whose name is matched.\n");
junov@chromium.orgfb103892012-09-20 19:35:43 +0000319 SkDebugf(" --mode normal|deferred|deferredSilent|record|picturerecord :\n"
320 " Run in the corresponding mode\n"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000321 " normal, Use a normal canvas to draw to;\n"
322 " deferred, Use a deferrred canvas when drawing;\n"
junov@chromium.orgfb103892012-09-20 19:35:43 +0000323 " deferredSilent, deferred with silent playback;\n"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000324 " record, Benchmark the time to record to an SkPicture;\n"
325 " picturerecord, Benchmark the time to do record from a \n"
326 " SkPicture to a SkPicture.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000327 SkDebugf(" --logFile filename : destination for writing log output, in addition to stdout.\n");
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000328#if SK_SUPPORT_GPU
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000329 SkDebugf(" --config 8888|565|GPU|ANGLE|NULLGPU : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000330 "Run bench in corresponding config mode.\n");
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000331#else
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000332 SkDebugf(" --config 8888|565: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000333 "Run bench in corresponding config mode.\n");
334#endif
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000335 SkDebugf(" -Dfoo bar : Add extra definition to bench.\n");
336 SkDebugf(" -h|--help : Show this help message.\n");
337}
338
caryclark@google.com5987f582012-10-02 18:33:14 +0000339int tool_main(int argc, char** argv);
340int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000341#if SK_ENABLE_INST_COUNT
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000342 gPrintInstCount = true;
343#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000344 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000345
reed@android.come9d00602009-09-02 21:12:42 +0000346 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000347 int repeatDraw = 1;
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000348 bool logPerIter = false;
reed@android.com4bc19832009-01-19 20:08:35 +0000349 int forceAlpha = 0xFF;
350 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000351 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000352 SkTriState::State forceDither = SkTriState::kDefault;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000353 bool timerWall = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000354 bool truncatedTimerWall = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000355 bool timerCpu = true;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000356 bool truncatedTimerCpu = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000357 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000358 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000359 bool doRotate = false;
360 bool doClip = false;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000361 bool printMin = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000362 bool hasStrokeWidth = false;
bsalomon@google.comc732f252013-03-11 20:22:15 +0000363
364#if SK_SUPPORT_GPU
365 struct {
bsalomon@google.come3613ce2013-03-11 20:33:45 +0000366 int fBytes;
bsalomon@google.comc732f252013-03-11 20:22:15 +0000367 int fCount;
368 } gpuCacheSize = { -1, -1 }; // -1s mean use the default
369#endif
370
agl@chromium.org652807b2010-04-27 15:47:34 +0000371 float strokeWidth;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000372 SkTDArray<const char*> fMatches;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000373 benchModes benchMode = kNormal_benchModes;
374 SkString perIterTimeformat("%.2f");
375 SkString normalTimeFormat("%6.2f");
376
reed@android.comb398fe82009-01-07 11:47:57 +0000377 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000378 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
379 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000380 Backend backend = kRaster_Backend; // for warning
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000381 SkTDArray<int> configs;
382 bool userConfig = false;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000383
scroggo@google.com9a412522012-09-07 15:21:18 +0000384 SkBenchLogger logger;
385
reed@android.comb398fe82009-01-07 11:47:57 +0000386 char* const* stop = argv + argc;
387 for (++argv; argv < stop; ++argv) {
388 if (strcmp(*argv, "-o") == 0) {
389 argv++;
390 if (argv < stop && **argv) {
391 outDir.set(*argv);
392 if (outDir.c_str()[outDir.size() - 1] != '/') {
393 outDir.append("/");
394 }
395 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000396 } else if (strcmp(*argv, "--repeat") == 0) {
reed@android.com4bc19832009-01-19 20:08:35 +0000397 argv++;
398 if (argv < stop) {
399 repeatDraw = atoi(*argv);
400 if (repeatDraw < 1) {
401 repeatDraw = 1;
402 }
403 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000404 logger.logError("missing arg for --repeat\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000405 help();
reed@android.com4bc19832009-01-19 20:08:35 +0000406 return -1;
407 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000408 } else if (strcmp(*argv, "--logPerIter") == 0) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000409 logPerIter = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000410 } else if (strcmp(*argv, "--timers") == 0) {
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000411 argv++;
412 if (argv < stop) {
413 timerWall = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000414 truncatedTimerWall = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000415 timerCpu = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000416 truncatedTimerCpu = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000417 timerGpu = false;
418 for (char* t = *argv; *t; ++t) {
419 switch (*t) {
420 case 'w': timerWall = true; break;
421 case 'c': timerCpu = true; break;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000422 case 'W': truncatedTimerWall = true; break;
423 case 'C': truncatedTimerCpu = true; break;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000424 case 'g': timerGpu = true; break;
425 }
426 }
427 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000428 logger.logError("missing arg for --timers\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000429 help();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000430 return -1;
431 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000432 } else if (!strcmp(*argv, "--rotate")) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000433 doRotate = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000434 } else if (!strcmp(*argv, "--scale")) {
reed@android.com387359e2009-08-04 01:51:09 +0000435 doScale = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000436 } else if (!strcmp(*argv, "--clip")) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000437 doClip = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000438 } else if (!strcmp(*argv, "--min")) {
robertphillips@google.com2abed832012-08-23 17:50:06 +0000439 printMin = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000440 } else if (strcmp(*argv, "--forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000441 if (!parse_bool_arg(++argv, stop, &forceAA)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000442 logger.logError("missing arg for --forceAA\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000443 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000444 return -1;
445 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000446 } else if (strcmp(*argv, "--forceFilter") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000447 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000448 logger.logError("missing arg for --forceFilter\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000449 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000450 return -1;
451 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000452 } else if (strcmp(*argv, "--forceDither") == 0) {
reed@android.com4e635f92009-10-19 17:39:46 +0000453 bool tmp;
454 if (!parse_bool_arg(++argv, stop, &tmp)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000455 logger.logError("missing arg for --forceDither\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000456 help();
reed@android.com4e635f92009-10-19 17:39:46 +0000457 return -1;
458 }
459 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000460 } else if (strcmp(*argv, "--forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000461 bool wantAlpha = false;
462 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000463 logger.logError("missing arg for --forceBlend\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000464 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000465 return -1;
466 }
467 forceAlpha = wantAlpha ? 0x80 : 0xFF;
bsalomon@google.comc732f252013-03-11 20:22:15 +0000468#if SK_SUPPORT_GPU
469 } else if (strcmp(*argv, "--gpuCacheSize") == 0) {
470 if (stop - argv > 2) {
471 gpuCacheSize.fBytes = atoi(*++argv);
472 gpuCacheSize.fCount = atoi(*++argv);
473 } else {
474 SkDebugf("missing arg for --gpuCacheSize\n");
475 help();
476 return -1;
477 }
478#endif
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000479 } else if (strcmp(*argv, "--mode") == 0) {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000480 argv++;
481 if (argv < stop) {
482 if (strcmp(*argv, "normal") == 0) {
483 benchMode = kNormal_benchModes;
484 } else if (strcmp(*argv, "deferred") == 0) {
485 benchMode = kDeferred_benchModes;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000486 } else if (strcmp(*argv, "deferredSilent") == 0) {
487 benchMode = kDeferredSilent_benchModes;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000488 } else if (strcmp(*argv, "record") == 0) {
489 benchMode = kRecord_benchModes;
490 } else if (strcmp(*argv, "picturerecord") == 0) {
491 benchMode = kPictureRecord_benchModes;
492 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000493 logger.logError("bad arg for --mode\n");
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000494 help();
495 return -1;
496 }
497 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000498 logger.logError("missing arg for --mode\n");
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000499 help();
500 return -1;
501 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000502 } else if (strcmp(*argv, "--strokeWidth") == 0) {
agl@chromium.org652807b2010-04-27 15:47:34 +0000503 argv++;
504 if (argv < stop) {
505 const char *strokeWidthStr = *argv;
506 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000507 logger.logError("bad arg for --strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000508 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000509 return -1;
510 }
511 hasStrokeWidth = true;
512 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000513 logger.logError("missing arg for --strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000514 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000515 return -1;
516 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000517 } else if (strcmp(*argv, "--match") == 0) {
reed@android.com387359e2009-08-04 01:51:09 +0000518 argv++;
519 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000520 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000521 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000522 logger.logError("missing arg for --match\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000523 help();
reed@android.com387359e2009-08-04 01:51:09 +0000524 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000525 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000526 } else if (strcmp(*argv, "--config") == 0) {
reed@android.com387359e2009-08-04 01:51:09 +0000527 argv++;
528 if (argv < stop) {
529 int index = findConfig(*argv);
530 if (index >= 0) {
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000531 *configs.append() = index;
532 userConfig = true;
reed@android.com387359e2009-08-04 01:51:09 +0000533 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000534 SkString str;
535 str.printf("unrecognized config %s\n", *argv);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000536 logger.logError(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000537 help();
reed@android.com387359e2009-08-04 01:51:09 +0000538 return -1;
539 }
540 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000541 logger.logError("missing arg for --config\n");
borenet@google.coma2b32b82012-08-07 12:58:50 +0000542 help();
543 return -1;
544 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000545 } else if (strcmp(*argv, "--logFile") == 0) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000546 argv++;
547 if (argv < stop) {
548 if (!logger.SetLogFile(*argv)) {
549 SkString str;
550 str.printf("Could not open %s for writing.", *argv);
scroggo@google.com9a412522012-09-07 15:21:18 +0000551 logger.logError(str);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000552 return -1;
553 }
554 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000555 logger.logError("missing arg for --logFile\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000556 help();
reed@android.com387359e2009-08-04 01:51:09 +0000557 return -1;
558 }
reed@android.com0c9da392010-02-22 19:50:13 +0000559 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000560 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000561 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000562 defineDict.set(argv[-1] + 2, *argv);
563 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000564 logger.logError("incomplete '-Dfoo bar' definition\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000565 help();
reed@android.come9d00602009-09-02 21:12:42 +0000566 return -1;
567 }
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000568 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
569 help();
570 return 0;
reed@android.com387359e2009-08-04 01:51:09 +0000571 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000572 SkString str;
573 str.printf("unrecognized arg %s\n", *argv);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000574 logger.logError(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000575 help();
reed@android.com387359e2009-08-04 01:51:09 +0000576 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000577 }
578 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000579 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)
580 && !outDir.isEmpty()) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000581 logger.logError("'--mode record' and '--mode picturerecord' are not"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000582 " compatible with -o.\n");
583 return -1;
584 }
585 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)) {
586 perIterTimeformat.set("%.4f");
587 normalTimeFormat.set("%6.4f");
588 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000589 if (!userConfig) {
590 // if no config is specified by user, we add them all.
591 for (unsigned int i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
592 *configs.append() = i;
593 }
594 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000595
reed@google.com3b14dc12011-04-04 14:31:36 +0000596 // report our current settings
597 {
598 SkString str;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000599 const char* deferredMode = benchMode == kDeferred_benchModes ? "yes" :
600 (benchMode == kDeferredSilent_benchModes ? "silent" : "no");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000601 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d "
junov@chromium.orgfb103892012-09-20 19:35:43 +0000602 "deferred=%s logperiter=%d",
603 forceAlpha, forceAA, forceFilter, deferredMode,
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000604 logPerIter);
robertphillips@google.com2abed832012-08-23 17:50:06 +0000605 str.appendf(" rotate=%d scale=%d clip=%d min=%d",
606 doRotate, doScale, doClip, printMin);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000607 str.appendf(" record=%d picturerecord=%d",
608 benchMode == kRecord_benchModes,
609 benchMode == kPictureRecord_benchModes);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000610 const char * ditherName;
611 switch (forceDither) {
612 case SkTriState::kDefault: ditherName = "default"; break;
613 case SkTriState::kTrue: ditherName = "true"; break;
614 case SkTriState::kFalse: ditherName = "false"; break;
615 default: ditherName = "<invalid>"; break;
616 }
617 str.appendf(" dither=%s", ditherName);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000618
bungeman@google.coma5d48412011-06-15 17:25:46 +0000619 if (hasStrokeWidth) {
620 str.appendf(" strokeWidth=%f", strokeWidth);
621 } else {
622 str.append(" strokeWidth=none");
623 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000624
bungeman@google.coma5d48412011-06-15 17:25:46 +0000625#if defined(SK_SCALAR_IS_FLOAT)
626 str.append(" scalar=float");
627#elif defined(SK_SCALAR_IS_FIXED)
628 str.append(" scalar=fixed");
629#endif
630
631#if defined(SK_BUILD_FOR_WIN32)
632 str.append(" system=WIN32");
633#elif defined(SK_BUILD_FOR_MAC)
634 str.append(" system=MAC");
635#elif defined(SK_BUILD_FOR_ANDROID)
636 str.append(" system=ANDROID");
637#elif defined(SK_BUILD_FOR_UNIX)
638 str.append(" system=UNIX");
639#else
640 str.append(" system=other");
641#endif
642
643#if defined(SK_DEBUG)
644 str.append(" DEBUG");
645#endif
646 str.append("\n");
borenet@google.coma2b32b82012-08-07 12:58:50 +0000647 logger.logProgress(str);
reed@google.com3b14dc12011-04-04 14:31:36 +0000648 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000649
bsalomon@google.comcb265352013-02-22 16:13:16 +0000650 SkTArray<BenchTimer*> timers(SK_ARRAY_COUNT(gConfigs));
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000651 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000652#if SK_SUPPORT_GPU
bsalomon@google.comc732f252013-03-11 20:22:15 +0000653 SkGLContextHelper* glCtx = NULL;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000654 if (kGPU_Backend == gConfigs[i].fBackend) {
bsalomon@google.comc732f252013-03-11 20:22:15 +0000655 GrContext* context = gContextFactory.get(gConfigs[i].fContextType);
656 if (NULL != context) {
657 // Set the user specified cache limits if non-default.
658 size_t bytes;
659 int count;
660 context->getTextureCacheLimits(&count, &bytes);
661 if (-1 != gpuCacheSize.fBytes) {
bsalomon@google.come3613ce2013-03-11 20:33:45 +0000662 bytes = static_cast<size_t>(gpuCacheSize.fBytes);
bsalomon@google.comc732f252013-03-11 20:22:15 +0000663 }
664 if (-1 != gpuCacheSize.fCount) {
665 count = gpuCacheSize.fCount;
666 }
667 context->setTextureCacheLimits(count, bytes);
668 }
669 glCtx = gContextFactory.getGLContext(gConfigs[i].fContextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000670 }
bsalomon@google.comc732f252013-03-11 20:22:15 +0000671 timers.push_back(SkNEW_ARGS(BenchTimer, (glCtx)));
bsalomon@google.comcb265352013-02-22 16:13:16 +0000672#else
673 timers.push_back(SkNEW(BenchTimer));
674#endif
675 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000676
reed@android.come9d00602009-09-02 21:12:42 +0000677 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000678 SkBenchmark* bench;
679 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000680 SkAutoTUnref<SkBenchmark> benchUnref(bench);
681
reed@android.comb398fe82009-01-07 11:47:57 +0000682 SkIPoint dim = bench->getSize();
683 if (dim.fX <= 0 || dim.fY <= 0) {
684 continue;
685 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686
reed@android.com4bc19832009-01-19 20:08:35 +0000687 bench->setForceAlpha(forceAlpha);
688 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000689 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000690 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000691 if (hasStrokeWidth) {
692 bench->setStrokeWidth(strokeWidth);
693 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000694
reed@android.com387359e2009-08-04 01:51:09 +0000695 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000696 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000697 continue;
698 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000699
reed@android.com29348cb2009-08-04 18:17:15 +0000700 {
701 SkString str;
reed@google.comd34658a2011-04-11 13:12:51 +0000702 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY,
reed@android.com0c9da392010-02-22 19:50:13 +0000703 bench->getName());
borenet@google.coma2b32b82012-08-07 12:58:50 +0000704 logger.logProgress(str);
reed@android.com29348cb2009-08-04 18:17:15 +0000705 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000706
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000707 AutoPrePostDraw appd(bench);
708
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000709 bool runOnce = false;
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000710 for (int x = 0; x < configs.count(); ++x) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000711 if (!bench->isRendering() && runOnce) {
712 continue;
713 }
714 runOnce = true;
715
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000716 int configIndex = configs[x];
717
718 outConfig = gConfigs[configIndex].fConfig;
719 configName = gConfigs[configIndex].fName;
720 backend = gConfigs[configIndex].fBackend;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000721 GrContext* context = NULL;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000722 BenchTimer* timer = timers[configIndex];
bsalomon@google.com508824b2011-12-13 16:49:49 +0000723
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000724#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000725 SkGLContextHelper* glContext = NULL;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000726 if (kGPU_Backend == backend) {
727 context = gContextFactory.get(gConfigs[configIndex].fContextType);
728 if (NULL == context) {
729 continue;
730 }
731 glContext = gContextFactory.getGLContext(gConfigs[configIndex].fContextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000732 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000733#endif
bsalomon@google.comcb265352013-02-22 16:13:16 +0000734 SkDevice* device = make_device(outConfig, dim, backend, context);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000735 SkCanvas* canvas = NULL;
736 SkPicture pictureRecordFrom;
737 SkPicture pictureRecordTo;
738 switch(benchMode) {
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000739 case kDeferredSilent_benchModes:
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000740 case kDeferred_benchModes:
741 canvas = new SkDeferredCanvas(device);
742 break;
743 case kRecord_benchModes:
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000744 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY,
745 SkPicture::kUsePathBoundsForClip_RecordingFlag);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000746 canvas->ref();
747 break;
748 case kPictureRecord_benchModes: {
749 // This sets up picture-to-picture recording.
750 // The C++ drawing calls for the benchmark are recorded into
751 // pictureRecordFrom. As the benchmark, we will time how
752 // long it takes to playback pictureRecordFrom into
753 // pictureRecordTo.
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000754 SkCanvas* tempCanvas = pictureRecordFrom.beginRecording(dim.fX, dim.fY,
755 SkPicture::kUsePathBoundsForClip_RecordingFlag);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000756 bench->draw(tempCanvas);
757 pictureRecordFrom.endRecording();
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000758 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY,
759 SkPicture::kUsePathBoundsForClip_RecordingFlag);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000760 canvas->ref();
761 break;
762 }
763 case kNormal_benchModes:
764 canvas = new SkCanvas(device);
765 break;
766 default:
767 SkASSERT(0);
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000768 }
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000769 device->unref();
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000770 SkAutoUnref canvasUnref(canvas);
771
reed@android.com4c7d3d62009-01-21 03:15:13 +0000772 if (doClip) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000773 performClip(canvas, dim.fX, dim.fY);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000774 }
reed@android.com387359e2009-08-04 01:51:09 +0000775 if (doScale) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000776 performScale(canvas, dim.fX, dim.fY);
reed@android.com387359e2009-08-04 01:51:09 +0000777 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000778 if (doRotate) {
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000779 performRotate(canvas, dim.fX, dim.fY);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000780 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000781
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000782 // warm up caches if needed
reed@android.comeca48362010-12-20 18:34:17 +0000783 if (repeatDraw > 1) {
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000784#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000785 // purge the GPU resources to reduce variance
786 if (NULL != context) {
787 context->freeGpuResources();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000788 }
789#endif
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000790 SkAutoCanvasRestore acr(canvas, true);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000791 if (benchMode == kPictureRecord_benchModes) {
792 pictureRecordFrom.draw(canvas);
793 } else {
794 bench->draw(canvas);
795 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000796
797 if (kDeferredSilent_benchModes == benchMode) {
798 static_cast<SkDeferredCanvas*>(canvas)->silentFlush();
799 } else {
800 canvas->flush();
801 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000802#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000803 if (NULL != context) {
804 context->flush();
805 SK_GL(*glContext, Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000806 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000807#endif
reed@android.comeca48362010-12-20 18:34:17 +0000808 }
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000809
810 // record timer values for each repeat, and their sum
scroggo@google.com9a412522012-09-07 15:21:18 +0000811 TimerData timerData(perIterTimeformat, normalTimeFormat);
reed@android.com4bc19832009-01-19 20:08:35 +0000812 for (int i = 0; i < repeatDraw; i++) {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000813 if ((benchMode == kRecord_benchModes
814 || benchMode == kPictureRecord_benchModes)) {
815 // This will clear the recorded commands so that they do not
bsalomon@google.comcb265352013-02-22 16:13:16 +0000816 // accumulate.
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000817 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY,
818 SkPicture::kUsePathBoundsForClip_RecordingFlag);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000819 }
820
bsalomon@google.comcb265352013-02-22 16:13:16 +0000821 timer->start();
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000822 SkAutoCanvasRestore acr(canvas, true);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000823 if (benchMode == kPictureRecord_benchModes) {
824 pictureRecordFrom.draw(canvas);
825 } else {
826 bench->draw(canvas);
827 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000828
829 if (kDeferredSilent_benchModes == benchMode) {
830 static_cast<SkDeferredCanvas*>(canvas)->silentFlush();
831 } else {
832 canvas->flush();
833 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000834
835 // stop the truncated timer after the last canvas call but
836 // don't wait for all the GL calls to complete
bsalomon@google.comcb265352013-02-22 16:13:16 +0000837 timer->truncatedEnd();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000838#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000839 if (NULL != glContext) {
840 context->flush();
841 SK_GL(*glContext, Finish());
reed@google.com25df8882011-07-14 19:03:58 +0000842 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000843#endif
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000844 // stop the inclusive and gpu timers once all the GL calls
845 // have completed
bsalomon@google.comcb265352013-02-22 16:13:16 +0000846 timer->end();
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000847
bsalomon@google.comcb265352013-02-22 16:13:16 +0000848 timerData.appendTimes(timer, repeatDraw - 1 == i);
robertphillips@google.com2abed832012-08-23 17:50:06 +0000849
reed@google.com25df8882011-07-14 19:03:58 +0000850 }
reed@android.com4bc19832009-01-19 20:08:35 +0000851 if (repeatDraw > 1) {
scroggo@google.com9a412522012-09-07 15:21:18 +0000852 SkString result = timerData.getResult(logPerIter, printMin, repeatDraw, configName,
853 timerWall, truncatedTimerWall, timerCpu,
bsalomon@google.comcb265352013-02-22 16:13:16 +0000854 truncatedTimerCpu,
855 timerGpu && NULL != context);
scroggo@google.com9a412522012-09-07 15:21:18 +0000856 logger.logProgress(result);
reed@android.com4bc19832009-01-19 20:08:35 +0000857 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000858 if (outDir.size() > 0) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000859 saveFile(bench->getName(), configName, outDir.c_str(),
860 device->accessBitmap(false));
jvanverth@google.coma543b602013-01-09 18:22:53 +0000861 canvas->clear(SK_ColorWHITE);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000862 }
reed@android.com4bc19832009-01-19 20:08:35 +0000863 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000864 logger.logProgress(SkString("\n"));
reed@android.combd700c32009-01-05 03:34:50 +0000865 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000866#if SK_SUPPORT_GPU
robertphillips@google.com9d594202012-09-13 14:05:00 +0000867#if GR_CACHE_STATS
bsalomon@google.comcb265352013-02-22 16:13:16 +0000868 for (int i = 0; i <= GrContextFactory::kLastGLContextType; ++i) {
869 GrContextFactory::GLContextType ctxType = (GrContextFactory::GLContextType)i;
870 GrContext* context = gContextFactory.get(ctxType);
871 if (NULL != context) {
872 SkDebugf("Cache Stats for %s context:\n", GrContextFactory::GLContextTypeName(ctxType));
873 context->printCacheStats();
874 SkDebugf("\n");
875 }
876 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000877#endif
bsalomon@google.comcb265352013-02-22 16:13:16 +0000878 // Destroy the GrContext before the inst tracking printing at main() exit occurs.
879 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000880#endif
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000881 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000882 SkDELETE(timers[i]);
883 }
robertphillips@google.com9d594202012-09-13 14:05:00 +0000884
reed@android.combd700c32009-01-05 03:34:50 +0000885 return 0;
886}
caryclark@google.com5987f582012-10-02 18:33:14 +0000887
borenet@google.com7158e6a2012-11-01 17:43:44 +0000888#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000889int main(int argc, char * const argv[]) {
890 return tool_main(argc, (char**) argv);
891}
892#endif