blob: 507507bd0adab1cb6cb4f638b4e298e99e7434f9 [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
mtkleinf3723212014-06-25 14:08:00 -070010#include "Benchmark.h"
11#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070012#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070013#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070014#include "ResultsWriter.h"
mtklein92007582014-08-01 07:46:52 -070015#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "Stats.h"
17#include "Timer.h"
18
mtklein92007582014-08-01 07:46:52 -070019#include "SkOSFile.h"
mtkleinf3723212014-06-25 14:08:00 -070020#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070021#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070022#include "SkForceLinking.h"
23#include "SkGraphics.h"
24#include "SkString.h"
25#include "SkSurface.h"
26
mtkleinbb6a0282014-07-01 08:43:42 -070027#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070028 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070029 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070030 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070031#endif
32
mtkleinf3723212014-06-25 14:08:00 -070033__SK_FORCE_IMAGE_DECODER_LINKING;
34
bsalomon6eb03cc2014-08-07 14:28:50 -070035static const int kAutoTuneLoops = -1;
36
mtkleinb5110422014-08-07 15:20:02 -070037static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070038#ifdef SK_DEBUG
39 1;
mtkleina189ccd2014-07-14 12:28:47 -070040#else
bsalomon6eb03cc2014-08-07 14:28:50 -070041 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070042#endif
43
bsalomon6eb03cc2014-08-07 14:28:50 -070044static SkString loops_help_txt() {
45 SkString help;
46 help.printf("Number of times to run each bench. Set this to %d to auto-"
47 "tune for each bench. Timings are only reported when auto-tuning.",
48 kAutoTuneLoops);
49 return help;
50}
51
52DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
53
mtkleinf3723212014-06-25 14:08:00 -070054DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
55DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
56DEFINE_double(overheadGoal, 0.0001,
57 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070058DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
59DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070060DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
61 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070062
mtklein60317d0f2014-07-14 11:30:37 -070063DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070064DEFINE_int32(maxCalibrationAttempts, 3,
65 "Try up to this many times to guess loops for a bench, or skip the bench.");
66DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein94e51562014-08-19 12:41:53 -070067DEFINE_string(key, "",
68 "Space-separated key/value pairs to add to JSON identifying this bench config.");
69DEFINE_string(options, "",
70 "Space-separated option/value pairs to add to JSON, logging extra info.");
jcgregoriobf5e5232014-07-17 13:14:16 -070071DEFINE_string(gitHash, "", "Git hash to add to JSON.");
mtklein60317d0f2014-07-14 11:30:37 -070072
mtklein92007582014-08-01 07:46:52 -070073DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
74DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
75
mtkleinf3723212014-06-25 14:08:00 -070076static SkString humanize(double ms) {
77 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
78 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070079#ifdef SK_BUILD_FOR_WIN
80 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
81#else
mtkleinf3723212014-06-25 14:08:00 -070082 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070083#endif
mtkleinf3723212014-06-25 14:08:00 -070084 return SkStringPrintf("%.3gms", ms);
85}
mtklein55b0ffc2014-07-17 08:38:23 -070086#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070087
mtkleinbb6a0282014-07-01 08:43:42 -070088static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070089 if (canvas) {
90 canvas->clear(SK_ColorWHITE);
91 }
mtkleinbb6a0282014-07-01 08:43:42 -070092 WallTimer timer;
93 timer.start();
94 if (bench) {
95 bench->draw(loops, canvas);
96 }
97 if (canvas) {
98 canvas->flush();
99 }
100#if SK_SUPPORT_GPU
101 if (gl) {
102 SK_GL(*gl, Flush());
103 gl->swapBuffers();
104 }
105#endif
106 timer.end();
107 return timer.fWall;
108}
109
mtkleinf3723212014-06-25 14:08:00 -0700110static double estimate_timer_overhead() {
111 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700112 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700113 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700114 }
115 return overhead / FLAGS_overheadLoops;
116}
117
mtklein55b0ffc2014-07-17 08:38:23 -0700118static int clamp_loops(int loops) {
119 if (loops < 1) {
120 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
121 return 1;
122 }
123 if (loops > FLAGS_maxLoops) {
124 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
125 return FLAGS_maxLoops;
126 }
127 return loops;
128}
129
bsalomon6eb03cc2014-08-07 14:28:50 -0700130static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
131 if (filename.isEmpty()) {
132 return false;
133 }
134 if (kUnknown_SkColorType == canvas->imageInfo().fColorType) {
135 return false;
136 }
137 SkBitmap bmp;
138 bmp.setInfo(canvas->imageInfo());
139 if (!canvas->readPixels(&bmp, 0, 0)) {
140 SkDebugf("Can't read canvas pixels.\n");
141 return false;
142 }
143 SkString dir = SkOSPath::Dirname(filename.c_str());
144 if (!sk_mkdir(dir.c_str())) {
145 SkDebugf("Can't make dir %s.\n", dir.c_str());
146 return false;
147 }
148 SkFILEWStream stream(filename.c_str());
149 if (!stream.isValid()) {
150 SkDebugf("Can't write %s.\n", filename.c_str());
151 return false;
152 }
153 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
154 SkDebugf("Can't encode a PNG.\n");
155 return false;
156 }
157 return true;
158}
159
160static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700161static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
162 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700163 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700164 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700165 if (kAutoTuneLoops == FLAGS_loops) {
166 while (bench_plus_overhead < overhead) {
167 if (round++ == FLAGS_maxCalibrationAttempts) {
168 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
169 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
170 return kFailedLoops;
171 }
172 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700173 }
mtklein2069e222014-08-04 13:57:39 -0700174 }
mtkleinf3723212014-06-25 14:08:00 -0700175
mtkleinbb6a0282014-07-01 08:43:42 -0700176 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700177 // We'll pick N to make timer overhead negligible:
178 //
mtkleinbb6a0282014-07-01 08:43:42 -0700179 // overhead
180 // ------------------------- < FLAGS_overheadGoal
181 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700182 //
mtkleinbb6a0282014-07-01 08:43:42 -0700183 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700184 //
185 // Doing some math, we get:
186 //
mtkleinbb6a0282014-07-01 08:43:42 -0700187 // (overhead / FLAGS_overheadGoal) - overhead
188 // ------------------------------------------ < N
189 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700190 //
191 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700192 int loops = FLAGS_loops;
193 if (kAutoTuneLoops == loops) {
194 const double numer = overhead / FLAGS_overheadGoal - overhead;
195 const double denom = bench_plus_overhead - overhead;
196 loops = (int)ceil(numer / denom);
197 }
198 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700199
200 for (int i = 0; i < FLAGS_samples; i++) {
201 samples[i] = time(loops, bench, canvas, NULL) / loops;
202 }
203 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700204}
205
mtkleinbb6a0282014-07-01 08:43:42 -0700206#if SK_SUPPORT_GPU
207static int gpu_bench(SkGLContextHelper* gl,
208 Benchmark* bench,
209 SkCanvas* canvas,
210 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700211 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700212 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700213 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700214
215 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700216 int loops = FLAGS_loops;
217 if (kAutoTuneLoops == loops) {
218 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700219 double elapsed = 0;
220 do {
221 loops *= 2;
222 // If the GPU lets frames lag at all, we need to make sure we're timing
223 // _this_ round, not still timing last round. We force this by looping
224 // more times than any reasonable GPU will allow frames to lag.
225 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
226 elapsed = time(loops, bench, canvas, gl);
227 }
228 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700229
mtkleina189ccd2014-07-14 12:28:47 -0700230 // We've overshot at least a little. Scale back linearly.
231 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700232
mtkleina189ccd2014-07-14 12:28:47 -0700233 // Might as well make sure we're not still timing our calibration.
234 SK_GL(*gl, Finish());
235 }
mtklein55b0ffc2014-07-17 08:38:23 -0700236 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700237
238 // Pretty much the same deal as the calibration: do some warmup to make
239 // sure we're timing steady-state pipelined frames.
240 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
241 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700242 }
mtkleinbb6a0282014-07-01 08:43:42 -0700243
244 // Now, actually do the timing!
245 for (int i = 0; i < FLAGS_samples; i++) {
246 samples[i] = time(loops, bench, canvas, gl) / loops;
247 }
248 return loops;
249}
250#endif
251
252static SkString to_lower(const char* str) {
253 SkString lower(str);
254 for (size_t i = 0; i < lower.size(); i++) {
255 lower[i] = tolower(lower[i]);
256 }
257 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700258}
259
bsalomonc2553372014-07-22 13:09:05 -0700260struct Config {
261 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700262 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700263 SkColorType color;
264 SkAlphaType alpha;
265 int samples;
266#if SK_SUPPORT_GPU
267 GrContextFactory::GLContextType ctxType;
268#else
269 int bogusInt;
270#endif
271};
272
273struct Target {
274 explicit Target(const Config& c) : config(c) {}
275 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700276 SkAutoTDelete<SkSurface> surface;
277#if SK_SUPPORT_GPU
278 SkGLContextHelper* gl;
279#endif
280};
mtkleinf3723212014-06-25 14:08:00 -0700281
bsalomonc2553372014-07-22 13:09:05 -0700282static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700283 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700284 if (to_lower(FLAGS_config[i]).equals(name)) {
285 return true;
mtkleinf3723212014-06-25 14:08:00 -0700286 }
287 }
bsalomonc2553372014-07-22 13:09:05 -0700288 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700289}
290
bsalomonc2553372014-07-22 13:09:05 -0700291#if SK_SUPPORT_GPU
292static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
293 int sampleCnt) {
294 if (!is_cpu_config_allowed(name)) {
295 return false;
296 }
krajcevski69a55602014-08-13 10:46:31 -0700297 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700298 return sampleCnt <= ctx->getMaxSampleCount();
299 }
300 return false;
301}
302#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700303
bsalomonc2553372014-07-22 13:09:05 -0700304#if SK_SUPPORT_GPU
305#define kBogusGLContextType GrContextFactory::kNative_GLContextType
306#else
307#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700308#endif
bsalomonc2553372014-07-22 13:09:05 -0700309
310// Append all configs that are enabled and supported.
311static void create_configs(SkTDArray<Config>* configs) {
312 #define CPU_CONFIG(name, backend, color, alpha) \
313 if (is_cpu_config_allowed(#name)) { \
314 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
315 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700316 }
mtkleine714e752014-07-31 12:13:48 -0700317
mtklein40b32be2014-07-09 08:46:49 -0700318 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700319 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
320 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
321 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700322 }
mtkleinbb6a0282014-07-01 08:43:42 -0700323
324#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700325 #define GPU_CONFIG(name, ctxType, samples) \
326 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
327 Config config = { \
328 #name, \
329 Benchmark::kGPU_Backend, \
330 kN32_SkColorType, \
331 kPremul_SkAlphaType, \
332 samples, \
333 GrContextFactory::ctxType }; \
334 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700335 }
mtkleine714e752014-07-31 12:13:48 -0700336
mtklein40b32be2014-07-09 08:46:49 -0700337 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700338 GPU_CONFIG(gpu, kNative_GLContextType, 0)
339 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
340 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
341 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
342 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
343 GPU_CONFIG(debug, kDebug_GLContextType, 0)
344 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700345#ifdef SK_ANGLE
346 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
347#endif
mtklein40b32be2014-07-09 08:46:49 -0700348 }
mtkleinbb6a0282014-07-01 08:43:42 -0700349#endif
mtkleinf3723212014-06-25 14:08:00 -0700350}
351
bsalomonc2553372014-07-22 13:09:05 -0700352// If bench is enabled for config, returns a Target* for it, otherwise NULL.
353static Target* is_enabled(Benchmark* bench, const Config& config) {
354 if (!bench->isSuitableFor(config.backend)) {
355 return NULL;
356 }
357
358 SkImageInfo info;
359 info.fAlphaType = config.alpha;
360 info.fColorType = config.color;
361 info.fWidth = bench->getSize().fX;
362 info.fHeight = bench->getSize().fY;
363
364 Target* target = new Target(config);
365
366 if (Benchmark::kRaster_Backend == config.backend) {
367 target->surface.reset(SkSurface::NewRaster(info));
368 }
369#if SK_SUPPORT_GPU
370 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700371 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700372 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700373 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700374 }
375#endif
376
377 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
378 delete target;
379 return NULL;
380 }
381 return target;
382}
383
384// Creates targets for a benchmark and a set of configs.
385static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
386 const SkTDArray<Config>& configs) {
387 for (int i = 0; i < configs.count(); ++i) {
388 if (Target* t = is_enabled(b, configs[i])) {
389 targets->push(t);
390 }
mtkleine714e752014-07-31 12:13:48 -0700391
bsalomonc2553372014-07-22 13:09:05 -0700392 }
393}
394
mtklein60317d0f2014-07-14 11:30:37 -0700395static void fill_static_options(ResultsWriter* log) {
396#if defined(SK_BUILD_FOR_WIN32)
397 log->option("system", "WIN32");
398#elif defined(SK_BUILD_FOR_MAC)
399 log->option("system", "MAC");
400#elif defined(SK_BUILD_FOR_ANDROID)
401 log->option("system", "ANDROID");
402#elif defined(SK_BUILD_FOR_UNIX)
403 log->option("system", "UNIX");
404#else
405 log->option("system", "other");
406#endif
mtklein60317d0f2014-07-14 11:30:37 -0700407}
408
jcgregoriobf5e5232014-07-17 13:14:16 -0700409#if SK_SUPPORT_GPU
410static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700411 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700412 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
413 log->configOption("GL_VERSION", (const char*)(version));
414
415 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
416 log->configOption("GL_RENDERER", (const char*) version);
417
418 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
419 log->configOption("GL_VENDOR", (const char*) version);
420
421 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
422 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
423}
424#endif
425
mtkleine714e752014-07-31 12:13:48 -0700426class BenchmarkStream {
427public:
mtklein92007582014-08-01 07:46:52 -0700428 BenchmarkStream() : fBenches(BenchRegistry::Head())
429 , fGMs(skiagm::GMRegistry::Head())
430 , fCurrentScale(0)
431 , fCurrentSKP(0) {
432 for (int i = 0; i < FLAGS_skps.count(); i++) {
433 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
434 fSKPs.push_back() = FLAGS_skps[i];
435 } else {
436 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
437 SkString path;
438 while (it.next(&path)) {
439 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
440 }
441 }
442 }
mtkleine714e752014-07-31 12:13:48 -0700443
mtklein92007582014-08-01 07:46:52 -0700444 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
445 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
446 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
447 exit(1);
448 }
449
450 for (int i = 0; i < FLAGS_scales.count(); i++) {
451 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
452 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
453 exit(1);
454 }
455 }
456 }
457
458 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700459 if (fBenches) {
460 Benchmark* bench = fBenches->factory()(NULL);
461 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700462 fSourceType = "bench";
mtkleine714e752014-07-31 12:13:48 -0700463 return bench;
464 }
mtklein92007582014-08-01 07:46:52 -0700465
mtkleine714e752014-07-31 12:13:48 -0700466 while (fGMs) {
467 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
468 fGMs = fGMs->next();
469 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700470 fSourceType = "gm";
mtkleine714e752014-07-31 12:13:48 -0700471 return SkNEW_ARGS(GMBench, (gm.detach()));
472 }
473 }
mtklein92007582014-08-01 07:46:52 -0700474
475 while (fCurrentScale < fScales.count()) {
476 while (fCurrentSKP < fSKPs.count()) {
477 const SkString& path = fSKPs[fCurrentSKP++];
478
479 // Not strictly necessary, as it will be checked again later,
480 // but helps to avoid a lot of pointless work if we're going to skip it.
481 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
482 continue;
483 }
484
485 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
486 if (stream.get() == NULL) {
487 SkDebugf("Could not read %s.\n", path.c_str());
488 exit(1);
489 }
490
491 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
492 if (pic.get() == NULL) {
493 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
494 exit(1);
495 }
496
497 SkString name = SkOSPath::Basename(path.c_str());
498
499 fSourceType = "skp";
500 return SkNEW_ARGS(SKPBench,
501 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
502 }
503 fCurrentSKP = 0;
504 fCurrentScale++;
505 }
506
mtkleine714e752014-07-31 12:13:48 -0700507 return NULL;
508 }
mtklein92007582014-08-01 07:46:52 -0700509
510 void fillCurrentOptions(ResultsWriter* log) const {
511 log->configOption("source_type", fSourceType);
512 if (0 == strcmp(fSourceType, "skp")) {
513 log->configOption("clip",
514 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
515 fClip.fRight, fClip.fBottom).c_str());
516 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
517 }
518 }
519
mtkleine714e752014-07-31 12:13:48 -0700520private:
521 const BenchRegistry* fBenches;
522 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700523 SkIRect fClip;
524 SkTArray<SkScalar> fScales;
525 SkTArray<SkString> fSKPs;
526
527 const char* fSourceType;
528 int fCurrentScale;
529 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700530};
531
caryclark17f0b6d2014-07-22 10:15:34 -0700532int nanobench_main();
533int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700534 SetupCrashHandler();
535 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700536
krajcevski69a55602014-08-13 10:46:31 -0700537#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700538 GrContext::Options grContextOpts;
539 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
540 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700541#endif
542
bsalomon6eb03cc2014-08-07 14:28:50 -0700543 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700544 FLAGS_samples = 1;
545 FLAGS_gpuFrameLag = 0;
546 }
547
bsalomon6eb03cc2014-08-07 14:28:50 -0700548 if (!FLAGS_writePath.isEmpty()) {
549 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
550 if (!sk_mkdir(FLAGS_writePath[0])) {
551 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
552 FLAGS_writePath.set(0, NULL);
553 }
554 }
555
mtklein60317d0f2014-07-14 11:30:37 -0700556 MultiResultsWriter log;
jcgregoriobf5e5232014-07-17 13:14:16 -0700557 SkAutoTDelete<NanoJSONResultsWriter> json;
mtklein60317d0f2014-07-14 11:30:37 -0700558 if (!FLAGS_outResultsFile.isEmpty()) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700559 const char* gitHash = FLAGS_gitHash.isEmpty() ? "unknown-revision" : FLAGS_gitHash[0];
560 json.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0], gitHash)));
mtklein60317d0f2014-07-14 11:30:37 -0700561 log.add(json.get());
562 }
563 CallEnd<MultiResultsWriter> ender(log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700564
565 if (1 == FLAGS_key.count() % 2) {
566 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
567 return 1;
568 }
569 for (int i = 1; i < FLAGS_key.count(); i += 2) {
570 log.key(FLAGS_key[i-1], FLAGS_key[i]);
571 }
mtklein94e51562014-08-19 12:41:53 -0700572
mtklein60317d0f2014-07-14 11:30:37 -0700573 fill_static_options(&log);
mtklein94e51562014-08-19 12:41:53 -0700574 if (1 == FLAGS_options.count() % 2) {
575 SkDebugf("ERROR: --options must be passed with an even number of arguments.\n");
576 return 1;
577 }
578 for (int i = 1; i < FLAGS_options.count(); i += 2) {
579 log.option(FLAGS_options[i-1], FLAGS_options[i]);
580 }
mtklein60317d0f2014-07-14 11:30:37 -0700581
mtkleinf3723212014-06-25 14:08:00 -0700582 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700583 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400584
mtkleinbb6a0282014-07-01 08:43:42 -0700585 SkAutoTMalloc<double> samples(FLAGS_samples);
586
bsalomon6eb03cc2014-08-07 14:28:50 -0700587 if (kAutoTuneLoops != FLAGS_loops) {
588 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700589 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700590 // No header.
591 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700592 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700593 } else {
mtkleinafb43792014-08-19 15:55:55 -0700594 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700595 }
596
bsalomonc2553372014-07-22 13:09:05 -0700597 SkTDArray<Config> configs;
598 create_configs(&configs);
599
mtklein92007582014-08-01 07:46:52 -0700600 BenchmarkStream benchStream;
601 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700602 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700603 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
604 continue;
605 }
606
mtkleinbb6a0282014-07-01 08:43:42 -0700607 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700608 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700609
jcgregoriobf5e5232014-07-17 13:14:16 -0700610 if (!targets.isEmpty()) {
611 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
612 bench->preDraw();
613 }
mtkleinbb6a0282014-07-01 08:43:42 -0700614 for (int j = 0; j < targets.count(); j++) {
615 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700616 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700617
mtklein92007582014-08-01 07:46:52 -0700618#if SK_DEBUG
619 // skia:2797 Some SKPs SkASSERT in debug mode. Skip them for now.
mtklein6e33e232014-08-01 08:23:39 -0700620 if (0 == strcmp("565", config) && SkStrContains(bench->getName(), ".skp")) {
621 SkDebugf("Skipping 565 %s. See skia:2797\n", bench->getName());
mtklein92007582014-08-01 07:46:52 -0700622 continue;
623 }
624#endif
625
mtkleinbb6a0282014-07-01 08:43:42 -0700626 const int loops =
627#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700628 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700629 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
630 :
631#endif
632 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700633
bsalomon6eb03cc2014-08-07 14:28:50 -0700634 if (canvas && !FLAGS_writePath.isEmpty() && NULL != FLAGS_writePath[0]) {
635 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
636 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
637 pngFilename.append(".png");
638 write_canvas_png(canvas, pngFilename);
639 }
640
641 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700642 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400643 continue;
644 }
645
mtkleinf3723212014-06-25 14:08:00 -0700646 Stats stats(samples.get(), FLAGS_samples);
mtklein60317d0f2014-07-14 11:30:37 -0700647 log.config(config);
mtklein92007582014-08-01 07:46:52 -0700648 benchStream.fillCurrentOptions(&log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700649#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700650 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700651 fill_gpu_options(&log, targets[j]->gl);
652 }
653#endif
mtklein60317d0f2014-07-14 11:30:37 -0700654 log.timer("min_ms", stats.min);
655 log.timer("median_ms", stats.median);
656 log.timer("mean_ms", stats.mean);
657 log.timer("max_ms", stats.max);
658 log.timer("stddev_ms", sqrt(stats.var));
659
bsalomon6eb03cc2014-08-07 14:28:50 -0700660 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700661 if (targets.count() == 1) {
662 config = ""; // Only print the config if we run the same bench on more than one.
663 }
664 SkDebugf("%s\t%s\n", bench->getName(), config);
665 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700666 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700667 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700668 }
669 SkDebugf("%s\n", bench->getName());
670 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700671 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700672 config = ""; // Only print the config if we run the same bench on more than one.
673 }
mtklein55b0ffc2014-07-17 08:38:23 -0700674 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700675 } else {
676 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700677 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
678 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700679 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700680 , HUMANIZE(stats.min)
681 , HUMANIZE(stats.median)
682 , HUMANIZE(stats.mean)
683 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700684 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700685 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700686 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700687 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700688 );
689 }
690 }
mtkleinbb6a0282014-07-01 08:43:42 -0700691 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400692
693 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700694 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700695 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700696 }
697 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700698 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400699 }
700 #endif
mtkleinf3723212014-06-25 14:08:00 -0700701 }
702
703 return 0;
704}
705
706#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700707int main(int argc, char** argv) {
708 SkCommandLineFlags::Parse(argc, argv);
709 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700710}
711#endif