blob: a1fea22a85334feac58641407360c315de105772 [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"
mtklein60317d0f2014-07-14 11:30:37 -070013#include "ResultsWriter.h"
mtklein92007582014-08-01 07:46:52 -070014#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070015#include "Stats.h"
16#include "Timer.h"
17
mtklein92007582014-08-01 07:46:52 -070018#include "SkOSFile.h"
mtkleinf3723212014-06-25 14:08:00 -070019#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070020#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070021#include "SkForceLinking.h"
22#include "SkGraphics.h"
23#include "SkString.h"
24#include "SkSurface.h"
25
mtkleinbb6a0282014-07-01 08:43:42 -070026#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070027 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070028 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070029 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070030#endif
31
mtkleinf3723212014-06-25 14:08:00 -070032__SK_FORCE_IMAGE_DECODER_LINKING;
33
bsalomon6eb03cc2014-08-07 14:28:50 -070034static const int kAutoTuneLoops = -1;
35
mtkleinb5110422014-08-07 15:20:02 -070036static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070037#ifdef SK_DEBUG
38 1;
mtkleina189ccd2014-07-14 12:28:47 -070039#else
bsalomon6eb03cc2014-08-07 14:28:50 -070040 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070041#endif
42
bsalomon6eb03cc2014-08-07 14:28:50 -070043static SkString loops_help_txt() {
44 SkString help;
45 help.printf("Number of times to run each bench. Set this to %d to auto-"
46 "tune for each bench. Timings are only reported when auto-tuning.",
47 kAutoTuneLoops);
48 return help;
49}
50
51DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
52
mtkleinf3723212014-06-25 14:08:00 -070053DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
54DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
55DEFINE_double(overheadGoal, 0.0001,
56 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070057DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
58DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070059DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
60 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070061
mtklein60317d0f2014-07-14 11:30:37 -070062DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070063DEFINE_int32(maxCalibrationAttempts, 3,
64 "Try up to this many times to guess loops for a bench, or skip the bench.");
65DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein94e51562014-08-19 12:41:53 -070066DEFINE_string(key, "",
67 "Space-separated key/value pairs to add to JSON identifying this bench config.");
68DEFINE_string(options, "",
69 "Space-separated option/value pairs to add to JSON, logging extra info.");
jcgregoriobf5e5232014-07-17 13:14:16 -070070DEFINE_string(gitHash, "", "Git hash to add to JSON.");
mtklein60317d0f2014-07-14 11:30:37 -070071
mtklein92007582014-08-01 07:46:52 -070072DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
73DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
74
mtkleinf3723212014-06-25 14:08:00 -070075static SkString humanize(double ms) {
76 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
77 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070078#ifdef SK_BUILD_FOR_WIN
79 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
80#else
mtkleinf3723212014-06-25 14:08:00 -070081 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070082#endif
mtkleinf3723212014-06-25 14:08:00 -070083 return SkStringPrintf("%.3gms", ms);
84}
mtklein55b0ffc2014-07-17 08:38:23 -070085#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070086
mtkleinbb6a0282014-07-01 08:43:42 -070087static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070088 if (canvas) {
89 canvas->clear(SK_ColorWHITE);
90 }
mtkleinbb6a0282014-07-01 08:43:42 -070091 WallTimer timer;
92 timer.start();
93 if (bench) {
94 bench->draw(loops, canvas);
95 }
96 if (canvas) {
97 canvas->flush();
98 }
99#if SK_SUPPORT_GPU
100 if (gl) {
101 SK_GL(*gl, Flush());
102 gl->swapBuffers();
103 }
104#endif
105 timer.end();
106 return timer.fWall;
107}
108
mtkleinf3723212014-06-25 14:08:00 -0700109static double estimate_timer_overhead() {
110 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700111 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700112 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700113 }
114 return overhead / FLAGS_overheadLoops;
115}
116
mtklein55b0ffc2014-07-17 08:38:23 -0700117static int clamp_loops(int loops) {
118 if (loops < 1) {
119 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
120 return 1;
121 }
122 if (loops > FLAGS_maxLoops) {
123 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
124 return FLAGS_maxLoops;
125 }
126 return loops;
127}
128
bsalomon6eb03cc2014-08-07 14:28:50 -0700129static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
130 if (filename.isEmpty()) {
131 return false;
132 }
133 if (kUnknown_SkColorType == canvas->imageInfo().fColorType) {
134 return false;
135 }
136 SkBitmap bmp;
137 bmp.setInfo(canvas->imageInfo());
138 if (!canvas->readPixels(&bmp, 0, 0)) {
139 SkDebugf("Can't read canvas pixels.\n");
140 return false;
141 }
142 SkString dir = SkOSPath::Dirname(filename.c_str());
143 if (!sk_mkdir(dir.c_str())) {
144 SkDebugf("Can't make dir %s.\n", dir.c_str());
145 return false;
146 }
147 SkFILEWStream stream(filename.c_str());
148 if (!stream.isValid()) {
149 SkDebugf("Can't write %s.\n", filename.c_str());
150 return false;
151 }
152 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
153 SkDebugf("Can't encode a PNG.\n");
154 return false;
155 }
156 return true;
157}
158
159static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700160static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
161 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700162 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700163 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700164 if (kAutoTuneLoops == FLAGS_loops) {
165 while (bench_plus_overhead < overhead) {
166 if (round++ == FLAGS_maxCalibrationAttempts) {
167 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
168 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
169 return kFailedLoops;
170 }
171 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700172 }
mtklein2069e222014-08-04 13:57:39 -0700173 }
mtkleinf3723212014-06-25 14:08:00 -0700174
mtkleinbb6a0282014-07-01 08:43:42 -0700175 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700176 // We'll pick N to make timer overhead negligible:
177 //
mtkleinbb6a0282014-07-01 08:43:42 -0700178 // overhead
179 // ------------------------- < FLAGS_overheadGoal
180 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700181 //
mtkleinbb6a0282014-07-01 08:43:42 -0700182 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700183 //
184 // Doing some math, we get:
185 //
mtkleinbb6a0282014-07-01 08:43:42 -0700186 // (overhead / FLAGS_overheadGoal) - overhead
187 // ------------------------------------------ < N
188 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700189 //
190 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700191 int loops = FLAGS_loops;
192 if (kAutoTuneLoops == loops) {
193 const double numer = overhead / FLAGS_overheadGoal - overhead;
194 const double denom = bench_plus_overhead - overhead;
195 loops = (int)ceil(numer / denom);
196 }
197 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700198
199 for (int i = 0; i < FLAGS_samples; i++) {
200 samples[i] = time(loops, bench, canvas, NULL) / loops;
201 }
202 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700203}
204
mtkleinbb6a0282014-07-01 08:43:42 -0700205#if SK_SUPPORT_GPU
206static int gpu_bench(SkGLContextHelper* gl,
207 Benchmark* bench,
208 SkCanvas* canvas,
209 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700210 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700211 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700212 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700213
214 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700215 int loops = FLAGS_loops;
216 if (kAutoTuneLoops == loops) {
217 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700218 double elapsed = 0;
219 do {
220 loops *= 2;
221 // If the GPU lets frames lag at all, we need to make sure we're timing
222 // _this_ round, not still timing last round. We force this by looping
223 // more times than any reasonable GPU will allow frames to lag.
224 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
225 elapsed = time(loops, bench, canvas, gl);
226 }
227 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700228
mtkleina189ccd2014-07-14 12:28:47 -0700229 // We've overshot at least a little. Scale back linearly.
230 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700231
mtkleina189ccd2014-07-14 12:28:47 -0700232 // Might as well make sure we're not still timing our calibration.
233 SK_GL(*gl, Finish());
234 }
mtklein55b0ffc2014-07-17 08:38:23 -0700235 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700236
237 // Pretty much the same deal as the calibration: do some warmup to make
238 // sure we're timing steady-state pipelined frames.
239 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
240 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700241 }
mtkleinbb6a0282014-07-01 08:43:42 -0700242
243 // Now, actually do the timing!
244 for (int i = 0; i < FLAGS_samples; i++) {
245 samples[i] = time(loops, bench, canvas, gl) / loops;
246 }
247 return loops;
248}
249#endif
250
251static SkString to_lower(const char* str) {
252 SkString lower(str);
253 for (size_t i = 0; i < lower.size(); i++) {
254 lower[i] = tolower(lower[i]);
255 }
256 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700257}
258
bsalomonc2553372014-07-22 13:09:05 -0700259struct Config {
260 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700261 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700262 SkColorType color;
263 SkAlphaType alpha;
264 int samples;
265#if SK_SUPPORT_GPU
266 GrContextFactory::GLContextType ctxType;
267#else
268 int bogusInt;
269#endif
270};
271
272struct Target {
273 explicit Target(const Config& c) : config(c) {}
274 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700275 SkAutoTDelete<SkSurface> surface;
276#if SK_SUPPORT_GPU
277 SkGLContextHelper* gl;
278#endif
279};
mtkleinf3723212014-06-25 14:08:00 -0700280
bsalomonc2553372014-07-22 13:09:05 -0700281static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700282 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700283 if (to_lower(FLAGS_config[i]).equals(name)) {
284 return true;
mtkleinf3723212014-06-25 14:08:00 -0700285 }
286 }
bsalomonc2553372014-07-22 13:09:05 -0700287 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700288}
289
bsalomonc2553372014-07-22 13:09:05 -0700290#if SK_SUPPORT_GPU
291static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
292 int sampleCnt) {
293 if (!is_cpu_config_allowed(name)) {
294 return false;
295 }
krajcevski69a55602014-08-13 10:46:31 -0700296 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700297 return sampleCnt <= ctx->getMaxSampleCount();
298 }
299 return false;
300}
301#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700302
bsalomonc2553372014-07-22 13:09:05 -0700303#if SK_SUPPORT_GPU
304#define kBogusGLContextType GrContextFactory::kNative_GLContextType
305#else
306#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700307#endif
bsalomonc2553372014-07-22 13:09:05 -0700308
309// Append all configs that are enabled and supported.
310static void create_configs(SkTDArray<Config>* configs) {
311 #define CPU_CONFIG(name, backend, color, alpha) \
312 if (is_cpu_config_allowed(#name)) { \
313 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
314 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700315 }
mtkleine714e752014-07-31 12:13:48 -0700316
mtklein40b32be2014-07-09 08:46:49 -0700317 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700318 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
319 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
320 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700321 }
mtkleinbb6a0282014-07-01 08:43:42 -0700322
323#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700324 #define GPU_CONFIG(name, ctxType, samples) \
325 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
326 Config config = { \
327 #name, \
328 Benchmark::kGPU_Backend, \
329 kN32_SkColorType, \
330 kPremul_SkAlphaType, \
331 samples, \
332 GrContextFactory::ctxType }; \
333 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700334 }
mtkleine714e752014-07-31 12:13:48 -0700335
mtklein40b32be2014-07-09 08:46:49 -0700336 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700337 GPU_CONFIG(gpu, kNative_GLContextType, 0)
338 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
339 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
340 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
341 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
342 GPU_CONFIG(debug, kDebug_GLContextType, 0)
343 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700344#ifdef SK_ANGLE
345 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
346#endif
mtklein40b32be2014-07-09 08:46:49 -0700347 }
mtkleinbb6a0282014-07-01 08:43:42 -0700348#endif
mtkleinf3723212014-06-25 14:08:00 -0700349}
350
bsalomonc2553372014-07-22 13:09:05 -0700351// If bench is enabled for config, returns a Target* for it, otherwise NULL.
352static Target* is_enabled(Benchmark* bench, const Config& config) {
353 if (!bench->isSuitableFor(config.backend)) {
354 return NULL;
355 }
356
357 SkImageInfo info;
358 info.fAlphaType = config.alpha;
359 info.fColorType = config.color;
360 info.fWidth = bench->getSize().fX;
361 info.fHeight = bench->getSize().fY;
362
363 Target* target = new Target(config);
364
365 if (Benchmark::kRaster_Backend == config.backend) {
366 target->surface.reset(SkSurface::NewRaster(info));
367 }
368#if SK_SUPPORT_GPU
369 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700370 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700371 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700372 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700373 }
374#endif
375
376 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
377 delete target;
378 return NULL;
379 }
380 return target;
381}
382
383// Creates targets for a benchmark and a set of configs.
384static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
385 const SkTDArray<Config>& configs) {
386 for (int i = 0; i < configs.count(); ++i) {
387 if (Target* t = is_enabled(b, configs[i])) {
388 targets->push(t);
389 }
mtkleine714e752014-07-31 12:13:48 -0700390
bsalomonc2553372014-07-22 13:09:05 -0700391 }
392}
393
mtklein60317d0f2014-07-14 11:30:37 -0700394static void fill_static_options(ResultsWriter* log) {
395#if defined(SK_BUILD_FOR_WIN32)
396 log->option("system", "WIN32");
397#elif defined(SK_BUILD_FOR_MAC)
398 log->option("system", "MAC");
399#elif defined(SK_BUILD_FOR_ANDROID)
400 log->option("system", "ANDROID");
401#elif defined(SK_BUILD_FOR_UNIX)
402 log->option("system", "UNIX");
403#else
404 log->option("system", "other");
405#endif
mtklein60317d0f2014-07-14 11:30:37 -0700406}
407
jcgregoriobf5e5232014-07-17 13:14:16 -0700408#if SK_SUPPORT_GPU
409static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700410 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700411 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
412 log->configOption("GL_VERSION", (const char*)(version));
413
414 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
415 log->configOption("GL_RENDERER", (const char*) version);
416
417 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
418 log->configOption("GL_VENDOR", (const char*) version);
419
420 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
421 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
422}
423#endif
424
mtkleine714e752014-07-31 12:13:48 -0700425class BenchmarkStream {
426public:
mtklein92007582014-08-01 07:46:52 -0700427 BenchmarkStream() : fBenches(BenchRegistry::Head())
428 , fGMs(skiagm::GMRegistry::Head())
429 , fCurrentScale(0)
430 , fCurrentSKP(0) {
431 for (int i = 0; i < FLAGS_skps.count(); i++) {
432 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
433 fSKPs.push_back() = FLAGS_skps[i];
434 } else {
435 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
436 SkString path;
437 while (it.next(&path)) {
438 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
439 }
440 }
441 }
mtkleine714e752014-07-31 12:13:48 -0700442
mtklein92007582014-08-01 07:46:52 -0700443 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
444 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
445 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
446 exit(1);
447 }
448
449 for (int i = 0; i < FLAGS_scales.count(); i++) {
450 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
451 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
452 exit(1);
453 }
454 }
455 }
456
457 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700458 if (fBenches) {
459 Benchmark* bench = fBenches->factory()(NULL);
460 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700461 fSourceType = "bench";
mtkleine714e752014-07-31 12:13:48 -0700462 return bench;
463 }
mtklein92007582014-08-01 07:46:52 -0700464
mtkleine714e752014-07-31 12:13:48 -0700465 while (fGMs) {
466 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
467 fGMs = fGMs->next();
468 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700469 fSourceType = "gm";
mtkleine714e752014-07-31 12:13:48 -0700470 return SkNEW_ARGS(GMBench, (gm.detach()));
471 }
472 }
mtklein92007582014-08-01 07:46:52 -0700473
474 while (fCurrentScale < fScales.count()) {
475 while (fCurrentSKP < fSKPs.count()) {
476 const SkString& path = fSKPs[fCurrentSKP++];
477
478 // Not strictly necessary, as it will be checked again later,
479 // but helps to avoid a lot of pointless work if we're going to skip it.
480 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
481 continue;
482 }
483
484 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
485 if (stream.get() == NULL) {
486 SkDebugf("Could not read %s.\n", path.c_str());
487 exit(1);
488 }
489
490 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
491 if (pic.get() == NULL) {
492 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
493 exit(1);
494 }
495
496 SkString name = SkOSPath::Basename(path.c_str());
497
498 fSourceType = "skp";
499 return SkNEW_ARGS(SKPBench,
500 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
501 }
502 fCurrentSKP = 0;
503 fCurrentScale++;
504 }
505
mtkleine714e752014-07-31 12:13:48 -0700506 return NULL;
507 }
mtklein92007582014-08-01 07:46:52 -0700508
509 void fillCurrentOptions(ResultsWriter* log) const {
510 log->configOption("source_type", fSourceType);
511 if (0 == strcmp(fSourceType, "skp")) {
512 log->configOption("clip",
513 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
514 fClip.fRight, fClip.fBottom).c_str());
515 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
516 }
517 }
518
mtkleine714e752014-07-31 12:13:48 -0700519private:
520 const BenchRegistry* fBenches;
521 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700522 SkIRect fClip;
523 SkTArray<SkScalar> fScales;
524 SkTArray<SkString> fSKPs;
525
526 const char* fSourceType;
527 int fCurrentScale;
528 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700529};
530
caryclark17f0b6d2014-07-22 10:15:34 -0700531int nanobench_main();
532int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700533 SetupCrashHandler();
534 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700535
krajcevski69a55602014-08-13 10:46:31 -0700536#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700537 GrContext::Options grContextOpts;
538 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
539 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700540#endif
541
bsalomon6eb03cc2014-08-07 14:28:50 -0700542 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700543 FLAGS_samples = 1;
544 FLAGS_gpuFrameLag = 0;
545 }
546
bsalomon6eb03cc2014-08-07 14:28:50 -0700547 if (!FLAGS_writePath.isEmpty()) {
548 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
549 if (!sk_mkdir(FLAGS_writePath[0])) {
550 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
551 FLAGS_writePath.set(0, NULL);
552 }
553 }
554
mtklein60317d0f2014-07-14 11:30:37 -0700555 MultiResultsWriter log;
jcgregoriobf5e5232014-07-17 13:14:16 -0700556 SkAutoTDelete<NanoJSONResultsWriter> json;
mtklein60317d0f2014-07-14 11:30:37 -0700557 if (!FLAGS_outResultsFile.isEmpty()) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700558 const char* gitHash = FLAGS_gitHash.isEmpty() ? "unknown-revision" : FLAGS_gitHash[0];
559 json.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0], gitHash)));
mtklein60317d0f2014-07-14 11:30:37 -0700560 log.add(json.get());
561 }
562 CallEnd<MultiResultsWriter> ender(log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700563
564 if (1 == FLAGS_key.count() % 2) {
565 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
566 return 1;
567 }
568 for (int i = 1; i < FLAGS_key.count(); i += 2) {
569 log.key(FLAGS_key[i-1], FLAGS_key[i]);
570 }
mtklein94e51562014-08-19 12:41:53 -0700571
mtklein60317d0f2014-07-14 11:30:37 -0700572 fill_static_options(&log);
mtklein94e51562014-08-19 12:41:53 -0700573 if (1 == FLAGS_options.count() % 2) {
574 SkDebugf("ERROR: --options must be passed with an even number of arguments.\n");
575 return 1;
576 }
577 for (int i = 1; i < FLAGS_options.count(); i += 2) {
578 log.option(FLAGS_options[i-1], FLAGS_options[i]);
579 }
mtklein60317d0f2014-07-14 11:30:37 -0700580
mtkleinf3723212014-06-25 14:08:00 -0700581 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700582 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400583
mtkleinbb6a0282014-07-01 08:43:42 -0700584 SkAutoTMalloc<double> samples(FLAGS_samples);
585
bsalomon6eb03cc2014-08-07 14:28:50 -0700586 if (kAutoTuneLoops != FLAGS_loops) {
587 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700588 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700589 // No header.
590 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700591 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700592 } else {
mtklein5d9d10e2014-07-11 11:57:07 -0700593 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700594 }
595
bsalomonc2553372014-07-22 13:09:05 -0700596 SkTDArray<Config> configs;
597 create_configs(&configs);
598
mtklein92007582014-08-01 07:46:52 -0700599 BenchmarkStream benchStream;
600 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700601 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700602 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
603 continue;
604 }
605
mtkleinbb6a0282014-07-01 08:43:42 -0700606 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700607 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700608
jcgregoriobf5e5232014-07-17 13:14:16 -0700609 if (!targets.isEmpty()) {
610 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
611 bench->preDraw();
612 }
mtkleinbb6a0282014-07-01 08:43:42 -0700613 for (int j = 0; j < targets.count(); j++) {
614 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700615 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700616
mtklein92007582014-08-01 07:46:52 -0700617#if SK_DEBUG
618 // skia:2797 Some SKPs SkASSERT in debug mode. Skip them for now.
mtklein6e33e232014-08-01 08:23:39 -0700619 if (0 == strcmp("565", config) && SkStrContains(bench->getName(), ".skp")) {
620 SkDebugf("Skipping 565 %s. See skia:2797\n", bench->getName());
mtklein92007582014-08-01 07:46:52 -0700621 continue;
622 }
623#endif
624
mtkleinbb6a0282014-07-01 08:43:42 -0700625 const int loops =
626#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700627 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700628 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
629 :
630#endif
631 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700632
bsalomon6eb03cc2014-08-07 14:28:50 -0700633 if (canvas && !FLAGS_writePath.isEmpty() && NULL != FLAGS_writePath[0]) {
634 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
635 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
636 pngFilename.append(".png");
637 write_canvas_png(canvas, pngFilename);
638 }
639
640 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700641 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400642 continue;
643 }
644
mtkleinf3723212014-06-25 14:08:00 -0700645 Stats stats(samples.get(), FLAGS_samples);
mtklein60317d0f2014-07-14 11:30:37 -0700646 log.config(config);
mtklein92007582014-08-01 07:46:52 -0700647 benchStream.fillCurrentOptions(&log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700648#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700649 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700650 fill_gpu_options(&log, targets[j]->gl);
651 }
652#endif
mtklein60317d0f2014-07-14 11:30:37 -0700653 log.timer("min_ms", stats.min);
654 log.timer("median_ms", stats.median);
655 log.timer("mean_ms", stats.mean);
656 log.timer("max_ms", stats.max);
657 log.timer("stddev_ms", sqrt(stats.var));
658
bsalomon6eb03cc2014-08-07 14:28:50 -0700659 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700660 if (targets.count() == 1) {
661 config = ""; // Only print the config if we run the same bench on more than one.
662 }
663 SkDebugf("%s\t%s\n", bench->getName(), config);
664 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700665 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700666 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700667 }
668 SkDebugf("%s\n", bench->getName());
669 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700670 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700671 config = ""; // Only print the config if we run the same bench on more than one.
672 }
mtklein55b0ffc2014-07-17 08:38:23 -0700673 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700674 } else {
675 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein5d9d10e2014-07-11 11:57:07 -0700676 SkDebugf("%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
mtkleinf3723212014-06-25 14:08:00 -0700677 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700678 , HUMANIZE(stats.min)
679 , HUMANIZE(stats.median)
680 , HUMANIZE(stats.mean)
681 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700682 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700683 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700684 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700685 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700686 );
687 }
688 }
mtkleinbb6a0282014-07-01 08:43:42 -0700689 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400690
691 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700692 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700693 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700694 }
695 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700696 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400697 }
698 #endif
mtkleinf3723212014-06-25 14:08:00 -0700699 }
700
701 return 0;
702}
703
704#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700705int main(int argc, char** argv) {
706 SkCommandLineFlags::Parse(argc, argv);
707 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700708}
709#endif