blob: 4bc56c4b17965497f2a0889fa25fc8dc6601313b [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 {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000189 kNonRendering_Backend,
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000190 kRaster_Backend,
191 kGPU_Backend,
192 kPDF_Backend,
193};
194
195static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000196 Backend backend, int sampleCount, GrContext* context) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000197 SkDevice* device = NULL;
198 SkBitmap bitmap;
199 bitmap.setConfig(config, size.fX, size.fY);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000201 switch (backend) {
202 case kRaster_Backend:
203 bitmap.allocPixels();
204 erase(bitmap);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000205 device = SkNEW_ARGS(SkDevice, (bitmap));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000206 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000207#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000208 case kGPU_Backend: {
209 GrTextureDesc desc;
210 desc.fConfig = kSkia8888_GrPixelConfig;
211 desc.fFlags = kRenderTarget_GrTextureFlagBit;
212 desc.fWidth = size.fX;
213 desc.fHeight = size.fY;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000214 desc.fSampleCnt = sampleCount;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000215 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
216 if (!texture) {
217 return NULL;
218 }
219 device = SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000220 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000221 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000222#endif
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000223 case kPDF_Backend:
224 default:
225 SkASSERT(!"unsupported");
226 }
227 return device;
228}
229
bsalomon@google.comcb265352013-02-22 16:13:16 +0000230#if SK_SUPPORT_GPU
231GrContextFactory gContextFactory;
232typedef GrContextFactory::GLContextType GLContextType;
233static const GLContextType kDontCareGLCtxType = GrContextFactory::kNative_GLContextType;
234#else
235typedef int GLContextType;
236static const GLContextType kDontCareGLCtxType = 0;
237#endif
238
reed@android.com4bc19832009-01-19 20:08:35 +0000239static const struct {
240 SkBitmap::Config fConfig;
241 const char* fName;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000242 int fSampleCnt;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000243 Backend fBackend;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000244 GLContextType fContextType;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000245 bool fRunByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000246} gConfigs[] = {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000247 { SkBitmap::kNo_Config, "NONRENDERING", 0, kNonRendering_Backend, kDontCareGLCtxType, true },
248 { SkBitmap::kARGB_8888_Config, "8888", 0, kRaster_Backend, kDontCareGLCtxType, true },
249 { SkBitmap::kRGB_565_Config, "565", 0, kRaster_Backend, kDontCareGLCtxType, true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000250#if SK_SUPPORT_GPU
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000251 { SkBitmap::kARGB_8888_Config, "GPU", 0, kGPU_Backend, GrContextFactory::kNative_GLContextType, true },
252 { SkBitmap::kARGB_8888_Config, "MSAA4", 4, kGPU_Backend, GrContextFactory::kNative_GLContextType, false },
253 { SkBitmap::kARGB_8888_Config, "MSAA16", 16, kGPU_Backend, GrContextFactory::kNative_GLContextType, false },
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000254#if SK_ANGLE
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000255 { SkBitmap::kARGB_8888_Config, "ANGLE", 0, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000256#endif // SK_ANGLE
robertphillips@google.coma0b63b82012-03-30 12:23:56 +0000257#ifdef SK_DEBUG
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000258 { SkBitmap::kARGB_8888_Config, "Debug", 0, kGPU_Backend, GrContextFactory::kDebug_GLContextType, GR_DEBUG },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000259#endif // SK_DEBUG
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000260 { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, kGPU_Backend, GrContextFactory::kNull_GLContextType, true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000261#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000262};
263
reed@android.com4c7d3d62009-01-21 03:15:13 +0000264static int findConfig(const char config[]) {
265 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
266 if (!strcmp(config, gConfigs[i].fName)) {
267 return i;
268 }
269 }
270 return -1;
271}
272
reed@google.com1a7eabc2011-10-31 16:33:52 +0000273static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
274 if (0 == array.count()) {
275 // no names, so don't skip anything
276 return false;
277 }
278 for (int i = 0; i < array.count(); ++i) {
279 if (strstr(name, array[i])) {
280 // found the name, so don't skip
281 return false;
282 }
283 }
284 return true;
285}
286
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000287static void help() {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000288 SkString configsStr;
289 static const size_t kConfigCount = SK_ARRAY_COUNT(gConfigs);
290 for (size_t i = 0; i < kConfigCount; ++i) {
291 configsStr.appendf("%s%s", gConfigs[i].fName, ((i == kConfigCount - 1) ? "" : "|"));
292 }
293
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000294 SkDebugf("Usage: bench [-o outDir] [--repeat nr] [--logPerIter] "
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000295 "[--timers [wcgWC]*] [--rotate]\n"
296 " [--scale] [--clip] [--min] [--forceAA 1|0] [--forceFilter 1|0]\n"
bsalomon@google.comc732f252013-03-11 20:22:15 +0000297 " [--forceDither 1|0] [--forceBlend 1|0]"
298#if SK_SUPPORT_GPU
299 " [--gpuCacheSize <bytes> <count>]"
300#endif
301 "\n"
302 " [--strokeWidth width] [--match name]\n"
303 " [--mode normal|deferred|deferredSilent|record|picturerecord]\n"
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000304 " [--config ");
305 SkDebugf("%s]\n", configsStr.c_str());
306 SkDebugf(" [-Dfoo bar] [--logFile filename] [-h|--help]");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000307 SkDebugf("\n\n");
308 SkDebugf(" -o outDir : Image of each bench will be put in outDir.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000309 SkDebugf(" --repeat nr : Each bench repeats for nr times.\n");
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000310 SkDebugf(" --logPerIter : "
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000311 "Log each repeat timer instead of mean, default is disabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000312 SkDebugf(" --timers [wcgWC]* : "
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000313 "Display wall, cpu, gpu, truncated wall or truncated cpu time for each bench.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000314 SkDebugf(" --rotate : Rotate before each bench runs.\n");
315 SkDebugf(" --scale : Scale before each bench runs.\n");
316 SkDebugf(" --clip : Clip before each bench runs.\n");
317 SkDebugf(" --min : Print the minimum times (instead of average).\n");
318 SkDebugf(" --forceAA 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000319 "Enable/disable anti-aliased, default is enabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000320 SkDebugf(" --forceFilter 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000321 "Enable/disable bitmap filtering, default is disabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000322 SkDebugf(" --forceDither 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000323 "Enable/disable dithering, default is disabled.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000324 SkDebugf(" --forceBlend 1|0 : "
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000325 "Enable/disable dithering, default is disabled.\n");
bsalomon@google.comc732f252013-03-11 20:22:15 +0000326#if SK_SUPPORT_GPU
327 SkDebugf(" --gpuCacheSize <bytes> <count>: "
328 "limits gpu cache to bytes size or object count.\n");
329 SkDebugf(" -1 for either value means use the default. 0 for either disables the cache.\n");
330#endif
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000331 SkDebugf(" --strokeWidth width : The width for path stroke.\n");
332 SkDebugf(" --match name : Only run bench whose name is matched.\n");
junov@chromium.orgfb103892012-09-20 19:35:43 +0000333 SkDebugf(" --mode normal|deferred|deferredSilent|record|picturerecord :\n"
334 " Run in the corresponding mode\n"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000335 " normal, Use a normal canvas to draw to;\n"
336 " deferred, Use a deferrred canvas when drawing;\n"
junov@chromium.orgfb103892012-09-20 19:35:43 +0000337 " deferredSilent, deferred with silent playback;\n"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000338 " record, Benchmark the time to record to an SkPicture;\n"
339 " picturerecord, Benchmark the time to do record from a \n"
340 " SkPicture to a SkPicture.\n");
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000341 SkDebugf(" --logFile filename : destination for writing log output, in addition to stdout.\n");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000342 SkDebugf(" --config %s:\n", configsStr.c_str());
343 SkDebugf(" Run bench in corresponding config mode.\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000344 SkDebugf(" -Dfoo bar : Add extra definition to bench.\n");
345 SkDebugf(" -h|--help : Show this help message.\n");
346}
347
caryclark@google.com5987f582012-10-02 18:33:14 +0000348int tool_main(int argc, char** argv);
349int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000350#if SK_ENABLE_INST_COUNT
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000351 gPrintInstCount = true;
352#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000353 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000354
reed@android.come9d00602009-09-02 21:12:42 +0000355 SkTDict<const char*> defineDict(1024);
reed@android.com4bc19832009-01-19 20:08:35 +0000356 int repeatDraw = 1;
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000357 bool logPerIter = false;
reed@android.com4bc19832009-01-19 20:08:35 +0000358 int forceAlpha = 0xFF;
359 bool forceAA = true;
reed@android.com29348cb2009-08-04 18:17:15 +0000360 bool forceFilter = false;
reed@android.com4e635f92009-10-19 17:39:46 +0000361 SkTriState::State forceDither = SkTriState::kDefault;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000362 bool timerWall = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000363 bool truncatedTimerWall = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000364 bool timerCpu = true;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000365 bool truncatedTimerCpu = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000366 bool timerGpu = true;
reed@android.com387359e2009-08-04 01:51:09 +0000367 bool doScale = false;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000368 bool doRotate = false;
369 bool doClip = false;
robertphillips@google.com2abed832012-08-23 17:50:06 +0000370 bool printMin = false;
agl@chromium.org652807b2010-04-27 15:47:34 +0000371 bool hasStrokeWidth = false;
bsalomon@google.comc732f252013-03-11 20:22:15 +0000372
373#if SK_SUPPORT_GPU
374 struct {
bsalomon@google.come3613ce2013-03-11 20:33:45 +0000375 int fBytes;
bsalomon@google.comc732f252013-03-11 20:22:15 +0000376 int fCount;
377 } gpuCacheSize = { -1, -1 }; // -1s mean use the default
378#endif
379
agl@chromium.org652807b2010-04-27 15:47:34 +0000380 float strokeWidth;
reed@google.com1a7eabc2011-10-31 16:33:52 +0000381 SkTDArray<const char*> fMatches;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000382 benchModes benchMode = kNormal_benchModes;
383 SkString perIterTimeformat("%.2f");
384 SkString normalTimeFormat("%6.2f");
385
reed@android.comb398fe82009-01-07 11:47:57 +0000386 SkString outDir;
reed@android.com387359e2009-08-04 01:51:09 +0000387 SkBitmap::Config outConfig = SkBitmap::kNo_Config;
388 const char* configName = "";
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000389 Backend backend = kRaster_Backend; // for warning
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000390 int sampleCount = 0;
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000391 SkTDArray<int> configs;
392 bool userConfig = false;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000393
scroggo@google.com9a412522012-09-07 15:21:18 +0000394 SkBenchLogger logger;
395
reed@android.comb398fe82009-01-07 11:47:57 +0000396 char* const* stop = argv + argc;
397 for (++argv; argv < stop; ++argv) {
398 if (strcmp(*argv, "-o") == 0) {
399 argv++;
400 if (argv < stop && **argv) {
401 outDir.set(*argv);
402 if (outDir.c_str()[outDir.size() - 1] != '/') {
403 outDir.append("/");
404 }
405 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000406 } else if (strcmp(*argv, "--repeat") == 0) {
reed@android.com4bc19832009-01-19 20:08:35 +0000407 argv++;
408 if (argv < stop) {
409 repeatDraw = atoi(*argv);
410 if (repeatDraw < 1) {
411 repeatDraw = 1;
412 }
413 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000414 logger.logError("missing arg for --repeat\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000415 help();
reed@android.com4bc19832009-01-19 20:08:35 +0000416 return -1;
417 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000418 } else if (strcmp(*argv, "--logPerIter") == 0) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000419 logPerIter = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000420 } else if (strcmp(*argv, "--timers") == 0) {
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000421 argv++;
422 if (argv < stop) {
423 timerWall = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000424 truncatedTimerWall = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000425 timerCpu = false;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000426 truncatedTimerCpu = false;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000427 timerGpu = false;
428 for (char* t = *argv; *t; ++t) {
429 switch (*t) {
430 case 'w': timerWall = true; break;
431 case 'c': timerCpu = true; break;
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000432 case 'W': truncatedTimerWall = true; break;
433 case 'C': truncatedTimerCpu = true; break;
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000434 case 'g': timerGpu = true; break;
435 }
436 }
437 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000438 logger.logError("missing arg for --timers\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000439 help();
bungeman@google.combe9ad4e2011-06-07 19:16:02 +0000440 return -1;
441 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000442 } else if (!strcmp(*argv, "--rotate")) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000443 doRotate = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000444 } else if (!strcmp(*argv, "--scale")) {
reed@android.com387359e2009-08-04 01:51:09 +0000445 doScale = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000446 } else if (!strcmp(*argv, "--clip")) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000447 doClip = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000448 } else if (!strcmp(*argv, "--min")) {
robertphillips@google.com2abed832012-08-23 17:50:06 +0000449 printMin = true;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000450 } else if (strcmp(*argv, "--forceAA") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000451 if (!parse_bool_arg(++argv, stop, &forceAA)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000452 logger.logError("missing arg for --forceAA\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000453 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000454 return -1;
455 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000456 } else if (strcmp(*argv, "--forceFilter") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000457 if (!parse_bool_arg(++argv, stop, &forceFilter)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000458 logger.logError("missing arg for --forceFilter\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000459 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000460 return -1;
461 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000462 } else if (strcmp(*argv, "--forceDither") == 0) {
reed@android.com4e635f92009-10-19 17:39:46 +0000463 bool tmp;
464 if (!parse_bool_arg(++argv, stop, &tmp)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000465 logger.logError("missing arg for --forceDither\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000466 help();
reed@android.com4e635f92009-10-19 17:39:46 +0000467 return -1;
468 }
469 forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse;
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000470 } else if (strcmp(*argv, "--forceBlend") == 0) {
reed@android.com29348cb2009-08-04 18:17:15 +0000471 bool wantAlpha = false;
472 if (!parse_bool_arg(++argv, stop, &wantAlpha)) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000473 logger.logError("missing arg for --forceBlend\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000474 help();
reed@android.com29348cb2009-08-04 18:17:15 +0000475 return -1;
476 }
477 forceAlpha = wantAlpha ? 0x80 : 0xFF;
bsalomon@google.comc732f252013-03-11 20:22:15 +0000478#if SK_SUPPORT_GPU
479 } else if (strcmp(*argv, "--gpuCacheSize") == 0) {
480 if (stop - argv > 2) {
481 gpuCacheSize.fBytes = atoi(*++argv);
482 gpuCacheSize.fCount = atoi(*++argv);
483 } else {
484 SkDebugf("missing arg for --gpuCacheSize\n");
485 help();
486 return -1;
487 }
488#endif
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000489 } else if (strcmp(*argv, "--mode") == 0) {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000490 argv++;
491 if (argv < stop) {
492 if (strcmp(*argv, "normal") == 0) {
493 benchMode = kNormal_benchModes;
494 } else if (strcmp(*argv, "deferred") == 0) {
495 benchMode = kDeferred_benchModes;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000496 } else if (strcmp(*argv, "deferredSilent") == 0) {
497 benchMode = kDeferredSilent_benchModes;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000498 } else if (strcmp(*argv, "record") == 0) {
499 benchMode = kRecord_benchModes;
500 } else if (strcmp(*argv, "picturerecord") == 0) {
501 benchMode = kPictureRecord_benchModes;
502 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000503 logger.logError("bad arg for --mode\n");
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000504 help();
505 return -1;
506 }
507 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000508 logger.logError("missing arg for --mode\n");
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000509 help();
510 return -1;
511 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000512 } else if (strcmp(*argv, "--strokeWidth") == 0) {
agl@chromium.org652807b2010-04-27 15:47:34 +0000513 argv++;
514 if (argv < stop) {
515 const char *strokeWidthStr = *argv;
516 if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000517 logger.logError("bad arg for --strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000518 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000519 return -1;
520 }
521 hasStrokeWidth = true;
522 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000523 logger.logError("missing arg for --strokeWidth\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000524 help();
agl@chromium.org652807b2010-04-27 15:47:34 +0000525 return -1;
526 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000527 } else if (strcmp(*argv, "--match") == 0) {
reed@android.com387359e2009-08-04 01:51:09 +0000528 argv++;
529 if (argv < stop) {
reed@google.com1a7eabc2011-10-31 16:33:52 +0000530 *fMatches.append() = *argv;
reed@android.com387359e2009-08-04 01:51:09 +0000531 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000532 logger.logError("missing arg for --match\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000533 help();
reed@android.com387359e2009-08-04 01:51:09 +0000534 return -1;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000535 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000536 } else if (strcmp(*argv, "--config") == 0) {
reed@android.com387359e2009-08-04 01:51:09 +0000537 argv++;
538 if (argv < stop) {
539 int index = findConfig(*argv);
540 if (index >= 0) {
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000541 *configs.append() = index;
542 userConfig = true;
reed@android.com387359e2009-08-04 01:51:09 +0000543 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000544 SkString str;
545 str.printf("unrecognized config %s\n", *argv);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000546 logger.logError(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000547 help();
reed@android.com387359e2009-08-04 01:51:09 +0000548 return -1;
549 }
550 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000551 logger.logError("missing arg for --config\n");
borenet@google.coma2b32b82012-08-07 12:58:50 +0000552 help();
553 return -1;
554 }
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000555 } else if (strcmp(*argv, "--logFile") == 0) {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000556 argv++;
557 if (argv < stop) {
558 if (!logger.SetLogFile(*argv)) {
559 SkString str;
560 str.printf("Could not open %s for writing.", *argv);
scroggo@google.com9a412522012-09-07 15:21:18 +0000561 logger.logError(str);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000562 return -1;
563 }
564 } else {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000565 logger.logError("missing arg for --logFile\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000566 help();
reed@android.com387359e2009-08-04 01:51:09 +0000567 return -1;
568 }
reed@android.com0c9da392010-02-22 19:50:13 +0000569 } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) {
reed@android.come9d00602009-09-02 21:12:42 +0000570 argv++;
reed@android.com0c9da392010-02-22 19:50:13 +0000571 if (argv < stop) {
reed@android.come9d00602009-09-02 21:12:42 +0000572 defineDict.set(argv[-1] + 2, *argv);
573 } else {
borenet@google.coma2b32b82012-08-07 12:58:50 +0000574 logger.logError("incomplete '-Dfoo bar' definition\n");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000575 help();
reed@android.come9d00602009-09-02 21:12:42 +0000576 return -1;
577 }
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000578 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
579 help();
580 return 0;
reed@android.com387359e2009-08-04 01:51:09 +0000581 } else {
reed@android.com29348cb2009-08-04 18:17:15 +0000582 SkString str;
583 str.printf("unrecognized arg %s\n", *argv);
borenet@google.coma2b32b82012-08-07 12:58:50 +0000584 logger.logError(str);
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000585 help();
reed@android.com387359e2009-08-04 01:51:09 +0000586 return -1;
reed@android.comb398fe82009-01-07 11:47:57 +0000587 }
588 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000589 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)
590 && !outDir.isEmpty()) {
scroggo@google.com4cea9f42012-09-12 19:27:11 +0000591 logger.logError("'--mode record' and '--mode picturerecord' are not"
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000592 " compatible with -o.\n");
593 return -1;
594 }
595 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)) {
596 perIterTimeformat.set("%.4f");
597 normalTimeFormat.set("%6.4f");
598 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000599 if (!userConfig) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000600 // if no config is specified by user, add the default configs
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000601 for (unsigned int i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000602 if (gConfigs[i].fRunByDefault) {
603 *configs.append() = i;
604 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000605 }
606 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000607 if (kNormal_benchModes != benchMode) {
608 // Non-rendering configs only run in normal mode
609 for (int i = 0; i < configs.count(); ++i) {
610 int configIdx = configs[i];
611 if (kNonRendering_Backend == gConfigs[configIdx].fBackend) {
612 configs.remove(i, 1);
613 --i;
614 }
615 }
616 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000617
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000618#if SK_SUPPORT_GPU
619 for (int i = 0; i < configs.count(); ++i) {
620 int configIdx = configs[i];
621
622 if (kGPU_Backend == gConfigs[configIdx].fBackend && gConfigs[configIdx].fSampleCnt > 0) {
623 GrContext* context = gContextFactory.get(gConfigs[configIdx].fContextType);
624 if (NULL == context) {
625 SkString error;
626 error.printf("Error creating GrContext for config %s. Config will be skipped.\n",
627 gConfigs[configIdx].fName);
628 logger.logError(error.c_str());
629 configs.remove(i);
630 --i;
631 continue;
632 }
633 if (gConfigs[configIdx].fSampleCnt > context->getMaxSampleCount()){
634 SkString error;
635 error.printf("Sample count (%d) for config %s is unsupported. "
636 "Config will be skipped.\n",
637 gConfigs[configIdx].fSampleCnt, gConfigs[configIdx].fName);
638 logger.logError(error.c_str());
639 configs.remove(i);
640 --i;
641 continue;
642 }
643 }
644 }
645#endif
646
reed@google.com3b14dc12011-04-04 14:31:36 +0000647 // report our current settings
648 {
649 SkString str;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000650 const char* deferredMode = benchMode == kDeferred_benchModes ? "yes" :
651 (benchMode == kDeferredSilent_benchModes ? "silent" : "no");
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000652 str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d "
junov@chromium.orgfb103892012-09-20 19:35:43 +0000653 "deferred=%s logperiter=%d",
654 forceAlpha, forceAA, forceFilter, deferredMode,
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000655 logPerIter);
robertphillips@google.com2abed832012-08-23 17:50:06 +0000656 str.appendf(" rotate=%d scale=%d clip=%d min=%d",
657 doRotate, doScale, doClip, printMin);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000658 str.appendf(" record=%d picturerecord=%d",
659 benchMode == kRecord_benchModes,
660 benchMode == kPictureRecord_benchModes);
bungeman@google.coma5d48412011-06-15 17:25:46 +0000661 const char * ditherName;
662 switch (forceDither) {
663 case SkTriState::kDefault: ditherName = "default"; break;
664 case SkTriState::kTrue: ditherName = "true"; break;
665 case SkTriState::kFalse: ditherName = "false"; break;
666 default: ditherName = "<invalid>"; break;
667 }
668 str.appendf(" dither=%s", ditherName);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000669
bungeman@google.coma5d48412011-06-15 17:25:46 +0000670 if (hasStrokeWidth) {
671 str.appendf(" strokeWidth=%f", strokeWidth);
672 } else {
673 str.append(" strokeWidth=none");
674 }
borenet@google.coma2b32b82012-08-07 12:58:50 +0000675
bungeman@google.coma5d48412011-06-15 17:25:46 +0000676#if defined(SK_SCALAR_IS_FLOAT)
677 str.append(" scalar=float");
678#elif defined(SK_SCALAR_IS_FIXED)
679 str.append(" scalar=fixed");
680#endif
681
682#if defined(SK_BUILD_FOR_WIN32)
683 str.append(" system=WIN32");
684#elif defined(SK_BUILD_FOR_MAC)
685 str.append(" system=MAC");
686#elif defined(SK_BUILD_FOR_ANDROID)
687 str.append(" system=ANDROID");
688#elif defined(SK_BUILD_FOR_UNIX)
689 str.append(" system=UNIX");
690#else
691 str.append(" system=other");
692#endif
693
694#if defined(SK_DEBUG)
695 str.append(" DEBUG");
696#endif
697 str.append("\n");
borenet@google.coma2b32b82012-08-07 12:58:50 +0000698 logger.logProgress(str);
reed@google.com3b14dc12011-04-04 14:31:36 +0000699 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000700
bsalomon@google.comcb265352013-02-22 16:13:16 +0000701 SkTArray<BenchTimer*> timers(SK_ARRAY_COUNT(gConfigs));
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000702 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000703#if SK_SUPPORT_GPU
bsalomon@google.comc732f252013-03-11 20:22:15 +0000704 SkGLContextHelper* glCtx = NULL;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000705 if (kGPU_Backend == gConfigs[i].fBackend) {
bsalomon@google.comc732f252013-03-11 20:22:15 +0000706 GrContext* context = gContextFactory.get(gConfigs[i].fContextType);
707 if (NULL != context) {
708 // Set the user specified cache limits if non-default.
709 size_t bytes;
710 int count;
711 context->getTextureCacheLimits(&count, &bytes);
712 if (-1 != gpuCacheSize.fBytes) {
bsalomon@google.come3613ce2013-03-11 20:33:45 +0000713 bytes = static_cast<size_t>(gpuCacheSize.fBytes);
bsalomon@google.comc732f252013-03-11 20:22:15 +0000714 }
715 if (-1 != gpuCacheSize.fCount) {
716 count = gpuCacheSize.fCount;
717 }
718 context->setTextureCacheLimits(count, bytes);
719 }
720 glCtx = gContextFactory.getGLContext(gConfigs[i].fContextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000721 }
bsalomon@google.comc732f252013-03-11 20:22:15 +0000722 timers.push_back(SkNEW_ARGS(BenchTimer, (glCtx)));
bsalomon@google.comcb265352013-02-22 16:13:16 +0000723#else
724 timers.push_back(SkNEW(BenchTimer));
725#endif
726 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000727
reed@android.come9d00602009-09-02 21:12:42 +0000728 Iter iter(&defineDict);
reed@android.combd700c32009-01-05 03:34:50 +0000729 SkBenchmark* bench;
730 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000731 SkAutoTUnref<SkBenchmark> benchUnref(bench);
732
reed@android.comb398fe82009-01-07 11:47:57 +0000733 SkIPoint dim = bench->getSize();
734 if (dim.fX <= 0 || dim.fY <= 0) {
735 continue;
736 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000737
reed@android.com4bc19832009-01-19 20:08:35 +0000738 bench->setForceAlpha(forceAlpha);
739 bench->setForceAA(forceAA);
reed@android.com29348cb2009-08-04 18:17:15 +0000740 bench->setForceFilter(forceFilter);
reed@android.com4e635f92009-10-19 17:39:46 +0000741 bench->setDither(forceDither);
agl@chromium.org652807b2010-04-27 15:47:34 +0000742 if (hasStrokeWidth) {
743 bench->setStrokeWidth(strokeWidth);
744 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000745
reed@android.com387359e2009-08-04 01:51:09 +0000746 // only run benchmarks if their name contains matchStr
reed@google.com1a7eabc2011-10-31 16:33:52 +0000747 if (skip_name(fMatches, bench->getName())) {
reed@android.com387359e2009-08-04 01:51:09 +0000748 continue;
749 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000750
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000751 bool loggedBenchStart = false;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000752
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000753 AutoPrePostDraw appd(bench);
754
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000755 for (int x = 0; x < configs.count(); ++x) {
756 int configIndex = configs[x];
757
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000758 if (kNonRendering_Backend == gConfigs[configIndex].fBackend) {
759 if (bench->isRendering()) {
760 continue;
761 }
762 } else {
763 if (!bench->isRendering()) {
764 continue;
765 }
766 }
767
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000768 outConfig = gConfigs[configIndex].fConfig;
769 configName = gConfigs[configIndex].fName;
770 backend = gConfigs[configIndex].fBackend;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000771 sampleCount = gConfigs[configIndex].fSampleCnt;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000772 GrContext* context = NULL;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000773 BenchTimer* timer = timers[configIndex];
bsalomon@google.com508824b2011-12-13 16:49:49 +0000774
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000775#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000776 SkGLContextHelper* glContext = NULL;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000777 if (kGPU_Backend == backend) {
778 context = gContextFactory.get(gConfigs[configIndex].fContextType);
779 if (NULL == context) {
780 continue;
781 }
782 glContext = gContextFactory.getGLContext(gConfigs[configIndex].fContextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000783 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000784#endif
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000785 SkDevice* device = NULL;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000786 SkCanvas* canvas = NULL;
787 SkPicture pictureRecordFrom;
788 SkPicture pictureRecordTo;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000789
790 if (kNonRendering_Backend != backend) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000791 device = make_device(outConfig, dim, backend, sampleCount, context);
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000792
793 switch(benchMode) {
794 case kDeferredSilent_benchModes:
795 case kDeferred_benchModes:
796 canvas = new SkDeferredCanvas(device);
797 break;
798 case kRecord_benchModes:
799 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY,
800 SkPicture::kUsePathBoundsForClip_RecordingFlag);
801 canvas->ref();
802 break;
803 case kPictureRecord_benchModes: {
804 // This sets up picture-to-picture recording.
805 // The C++ drawing calls for the benchmark are recorded into
806 // pictureRecordFrom. As the benchmark, we will time how
807 // long it takes to playback pictureRecordFrom into
808 // pictureRecordTo.
809 SkCanvas* tempCanvas = pictureRecordFrom.beginRecording(dim.fX, dim.fY,
810 SkPicture::kUsePathBoundsForClip_RecordingFlag);
811 bench->draw(tempCanvas);
812 pictureRecordFrom.endRecording();
813 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY,
814 SkPicture::kUsePathBoundsForClip_RecordingFlag);
815 canvas->ref();
816 break;
817 }
818 case kNormal_benchModes:
819 canvas = new SkCanvas(device);
820 break;
821 default:
822 SkASSERT(0);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000823 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000824 device->unref();
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000825 }
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000826 SkAutoUnref canvasUnref(canvas);
827
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000828 if (NULL != canvas) {
829 if (doClip) {
830 performClip(canvas, dim.fX, dim.fY);
831 }
832 if (doScale) {
833 performScale(canvas, dim.fX, dim.fY);
834 }
835 if (doRotate) {
836 performRotate(canvas, dim.fX, dim.fY);
837 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000838 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000839
840 if (!loggedBenchStart) {
841 loggedBenchStart = true;
842 SkString str;
843 str.printf("running bench [%d %d] %28s", dim.fX, dim.fY, bench->getName());
844 logger.logProgress(str);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000845 }
bsalomon@google.com508824b2011-12-13 16:49:49 +0000846
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000847 // warm up caches if needed
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000848 if (repeatDraw > 1 && NULL != canvas) {
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000849#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000850 // purge the GPU resources to reduce variance
851 if (NULL != context) {
852 context->freeGpuResources();
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000853 }
854#endif
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +0000855 SkAutoCanvasRestore acr(canvas, true);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000856 if (benchMode == kPictureRecord_benchModes) {
857 pictureRecordFrom.draw(canvas);
858 } else {
859 bench->draw(canvas);
860 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000861
862 if (kDeferredSilent_benchModes == benchMode) {
863 static_cast<SkDeferredCanvas*>(canvas)->silentFlush();
864 } else {
865 canvas->flush();
866 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000867#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000868 if (NULL != context) {
869 context->flush();
870 SK_GL(*glContext, Finish());
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000871 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000872#endif
reed@android.comeca48362010-12-20 18:34:17 +0000873 }
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000874
875 // record timer values for each repeat, and their sum
scroggo@google.com9a412522012-09-07 15:21:18 +0000876 TimerData timerData(perIterTimeformat, normalTimeFormat);
reed@android.com4bc19832009-01-19 20:08:35 +0000877 for (int i = 0; i < repeatDraw; i++) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000878 if ((benchMode == kRecord_benchModes || benchMode == kPictureRecord_benchModes)) {
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000879 // This will clear the recorded commands so that they do not
bsalomon@google.comcb265352013-02-22 16:13:16 +0000880 // accumulate.
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000881 canvas = pictureRecordTo.beginRecording(dim.fX, dim.fY,
882 SkPicture::kUsePathBoundsForClip_RecordingFlag);
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000883 }
884
bsalomon@google.comcb265352013-02-22 16:13:16 +0000885 timer->start();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000886 if (NULL != canvas) {
887 canvas->save();
888 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000889 if (benchMode == kPictureRecord_benchModes) {
890 pictureRecordFrom.draw(canvas);
891 } else {
892 bench->draw(canvas);
893 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000894
895 if (kDeferredSilent_benchModes == benchMode) {
896 static_cast<SkDeferredCanvas*>(canvas)->silentFlush();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000897 } else if (NULL != canvas) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000898 canvas->flush();
899 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000900
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000901 if (NULL != canvas) {
902 canvas->restore();
903 }
904
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000905 // stop the truncated timer after the last canvas call but
906 // don't wait for all the GL calls to complete
bsalomon@google.comcb265352013-02-22 16:13:16 +0000907 timer->truncatedEnd();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000908#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000909 if (NULL != glContext) {
910 context->flush();
911 SK_GL(*glContext, Finish());
reed@google.com25df8882011-07-14 19:03:58 +0000912 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000913#endif
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000914 // stop the inclusive and gpu timers once all the GL calls
915 // have completed
bsalomon@google.comcb265352013-02-22 16:13:16 +0000916 timer->end();
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000917
bsalomon@google.comcb265352013-02-22 16:13:16 +0000918 timerData.appendTimes(timer, repeatDraw - 1 == i);
robertphillips@google.com2abed832012-08-23 17:50:06 +0000919
reed@google.com25df8882011-07-14 19:03:58 +0000920 }
reed@android.com4bc19832009-01-19 20:08:35 +0000921 if (repeatDraw > 1) {
scroggo@google.com9a412522012-09-07 15:21:18 +0000922 SkString result = timerData.getResult(logPerIter, printMin, repeatDraw, configName,
923 timerWall, truncatedTimerWall, timerCpu,
bsalomon@google.comcb265352013-02-22 16:13:16 +0000924 truncatedTimerCpu,
925 timerGpu && NULL != context);
scroggo@google.com9a412522012-09-07 15:21:18 +0000926 logger.logProgress(result);
reed@android.com4bc19832009-01-19 20:08:35 +0000927 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000928 if (outDir.size() > 0 && kNonRendering_Backend != backend) {
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000929 saveFile(bench->getName(), configName, outDir.c_str(),
930 device->accessBitmap(false));
jvanverth@google.coma543b602013-01-09 18:22:53 +0000931 canvas->clear(SK_ColorWHITE);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000932 }
reed@android.com4bc19832009-01-19 20:08:35 +0000933 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000934 if (loggedBenchStart) {
935 logger.logProgress(SkString("\n"));
936 }
reed@android.combd700c32009-01-05 03:34:50 +0000937 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000938#if SK_SUPPORT_GPU
robertphillips@google.com9d594202012-09-13 14:05:00 +0000939#if GR_CACHE_STATS
bsalomon@google.comcb265352013-02-22 16:13:16 +0000940 for (int i = 0; i <= GrContextFactory::kLastGLContextType; ++i) {
941 GrContextFactory::GLContextType ctxType = (GrContextFactory::GLContextType)i;
942 GrContext* context = gContextFactory.get(ctxType);
943 if (NULL != context) {
944 SkDebugf("Cache Stats for %s context:\n", GrContextFactory::GLContextTypeName(ctxType));
945 context->printCacheStats();
946 SkDebugf("\n");
947 }
948 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000949#endif
bsalomon@google.comcb265352013-02-22 16:13:16 +0000950 // Destroy the GrContext before the inst tracking printing at main() exit occurs.
951 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000952#endif
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000953 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000954 SkDELETE(timers[i]);
955 }
robertphillips@google.com9d594202012-09-13 14:05:00 +0000956
reed@android.combd700c32009-01-05 03:34:50 +0000957 return 0;
958}
caryclark@google.com5987f582012-10-02 18:33:14 +0000959
borenet@google.com7158e6a2012-11-01 17:43:44 +0000960#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000961int main(int argc, char * const argv[]) {
962 return tool_main(argc, (char**) argv);
963}
964#endif