blob: 21aeef4b3c263f57616b58fd2b7b41ae783376b6 [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"
mtkleinfd731ce2014-09-10 12:19:30 -070015#include "RecordingBench.h"
mtklein92007582014-08-01 07:46:52 -070016#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070017#include "Stats.h"
18#include "Timer.h"
19
mtklein20840502014-08-21 15:51:22 -070020#include "SkBBHFactory.h"
mtkleinf3723212014-06-25 14:08:00 -070021#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070022#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070023#include "SkForceLinking.h"
24#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070025#include "SkOSFile.h"
26#include "SkPictureRecorder.h"
mtkleinf3723212014-06-25 14:08:00 -070027#include "SkString.h"
28#include "SkSurface.h"
29
mtkleinbb6a0282014-07-01 08:43:42 -070030#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070031 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070032 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070033 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070034#endif
35
mtkleinf3723212014-06-25 14:08:00 -070036__SK_FORCE_IMAGE_DECODER_LINKING;
37
bsalomon6eb03cc2014-08-07 14:28:50 -070038static const int kAutoTuneLoops = -1;
39
mtkleinb5110422014-08-07 15:20:02 -070040static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070041#ifdef SK_DEBUG
42 1;
mtkleina189ccd2014-07-14 12:28:47 -070043#else
bsalomon6eb03cc2014-08-07 14:28:50 -070044 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070045#endif
46
bsalomon6eb03cc2014-08-07 14:28:50 -070047static SkString loops_help_txt() {
48 SkString help;
49 help.printf("Number of times to run each bench. Set this to %d to auto-"
50 "tune for each bench. Timings are only reported when auto-tuning.",
51 kAutoTuneLoops);
52 return help;
53}
54
55DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
56
mtkleinf3723212014-06-25 14:08:00 -070057DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
58DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
59DEFINE_double(overheadGoal, 0.0001,
60 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070061DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
62DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070063DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
64 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070065
mtklein60317d0f2014-07-14 11:30:37 -070066DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070067DEFINE_int32(maxCalibrationAttempts, 3,
68 "Try up to this many times to guess loops for a bench, or skip the bench.");
69DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070070DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
71DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070072DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein92007582014-08-01 07:46:52 -070073
mtkleinf3723212014-06-25 14:08:00 -070074static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070075 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
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
mtkleindc5bbab2014-09-24 06:34:09 -070079 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070080#else
mtkleindc5bbab2014-09-24 06:34:09 -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 }
reede5ea5002014-09-03 11:54:58 -0700133 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700134 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",
mtklein96289052014-09-10 12:05:59 -0700168 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700169 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
reede5ea5002014-09-03 11:54:58 -0700357 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
358 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700359
360 Target* target = new Target(config);
361
362 if (Benchmark::kRaster_Backend == config.backend) {
363 target->surface.reset(SkSurface::NewRaster(info));
364 }
365#if SK_SUPPORT_GPU
366 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700367 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700368 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700369 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700370 }
371#endif
372
373 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
374 delete target;
375 return NULL;
376 }
377 return target;
378}
379
380// Creates targets for a benchmark and a set of configs.
381static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
382 const SkTDArray<Config>& configs) {
383 for (int i = 0; i < configs.count(); ++i) {
384 if (Target* t = is_enabled(b, configs[i])) {
385 targets->push(t);
386 }
mtkleine714e752014-07-31 12:13:48 -0700387
bsalomonc2553372014-07-22 13:09:05 -0700388 }
389}
390
jcgregoriobf5e5232014-07-17 13:14:16 -0700391#if SK_SUPPORT_GPU
392static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700393 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700394 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
395 log->configOption("GL_VERSION", (const char*)(version));
396
397 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
398 log->configOption("GL_RENDERER", (const char*) version);
399
400 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
401 log->configOption("GL_VENDOR", (const char*) version);
402
403 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
404 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
405}
406#endif
407
mtkleine714e752014-07-31 12:13:48 -0700408class BenchmarkStream {
409public:
mtklein92007582014-08-01 07:46:52 -0700410 BenchmarkStream() : fBenches(BenchRegistry::Head())
411 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700412 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700413 , fCurrentScale(0)
414 , fCurrentSKP(0) {
415 for (int i = 0; i < FLAGS_skps.count(); i++) {
416 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
417 fSKPs.push_back() = FLAGS_skps[i];
418 } else {
419 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
420 SkString path;
421 while (it.next(&path)) {
422 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
423 }
424 }
425 }
mtkleine714e752014-07-31 12:13:48 -0700426
mtklein92007582014-08-01 07:46:52 -0700427 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
428 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
429 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
430 exit(1);
431 }
432
433 for (int i = 0; i < FLAGS_scales.count(); i++) {
434 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
435 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
436 exit(1);
437 }
438 }
439 }
440
mtkleinfd731ce2014-09-10 12:19:30 -0700441 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
442 // Not strictly necessary, as it will be checked again later,
443 // but helps to avoid a lot of pointless work if we're going to skip it.
444 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
445 return false;
446 }
447
448 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
449 if (stream.get() == NULL) {
450 SkDebugf("Could not read %s.\n", path);
451 return false;
452 }
453
mtklein963504b2014-09-17 06:58:39 -0700454 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700455 if (pic->get() == NULL) {
456 SkDebugf("Could not read %s as an SkPicture.\n", path);
457 return false;
458 }
459 return true;
460 }
461
mtklein92007582014-08-01 07:46:52 -0700462 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700463 if (fBenches) {
464 Benchmark* bench = fBenches->factory()(NULL);
465 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700466 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700467 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700468 return bench;
469 }
mtklein92007582014-08-01 07:46:52 -0700470
mtkleine714e752014-07-31 12:13:48 -0700471 while (fGMs) {
472 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
473 fGMs = fGMs->next();
474 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700475 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700476 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700477 return SkNEW_ARGS(GMBench, (gm.detach()));
478 }
479 }
mtklein92007582014-08-01 07:46:52 -0700480
mtkleinfd731ce2014-09-10 12:19:30 -0700481 // First add all .skps as RecordingBenches.
482 while (fCurrentRecording < fSKPs.count()) {
483 const SkString& path = fSKPs[fCurrentRecording++];
484 SkAutoTUnref<SkPicture> pic;
485 if (!ReadPicture(path.c_str(), &pic)) {
486 continue;
487 }
488 SkString name = SkOSPath::Basename(path.c_str());
489 fSourceType = "skp";
490 fBenchType = "recording";
491 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
492 }
493
494 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700495 while (fCurrentScale < fScales.count()) {
496 while (fCurrentSKP < fSKPs.count()) {
497 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700498 SkAutoTUnref<SkPicture> pic;
499 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700500 continue;
501 }
mtklein20840502014-08-21 15:51:22 -0700502 if (FLAGS_bbh) {
503 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
504 // Here we use an SkTileGrid with parameters optimized for FLAGS_clip.
505 const SkTileGridFactory::TileGridInfo info = {
506 SkISize::Make(fClip.width(), fClip.height()), // tile interval
507 SkISize::Make(0,0), // margin
508 SkIPoint::Make(fClip.left(), fClip.top()), // offset
509 };
510 SkTileGridFactory factory(info);
511 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700512 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtkleinea65bfa2014-09-09 07:59:46 -0700513 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700514 &factory));
mtklein20840502014-08-21 15:51:22 -0700515 pic.reset(recorder.endRecording());
516 }
mtkleinfd731ce2014-09-10 12:19:30 -0700517 SkString name = SkOSPath::Basename(path.c_str());
mtklein92007582014-08-01 07:46:52 -0700518 fSourceType = "skp";
mtkleinfd731ce2014-09-10 12:19:30 -0700519 fBenchType = "playback";
mtklein92007582014-08-01 07:46:52 -0700520 return SkNEW_ARGS(SKPBench,
521 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
522 }
523 fCurrentSKP = 0;
524 fCurrentScale++;
525 }
526
mtkleine714e752014-07-31 12:13:48 -0700527 return NULL;
528 }
mtklein92007582014-08-01 07:46:52 -0700529
530 void fillCurrentOptions(ResultsWriter* log) const {
531 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700532 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700533 if (0 == strcmp(fSourceType, "skp")) {
534 log->configOption("clip",
535 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
536 fClip.fRight, fClip.fBottom).c_str());
537 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
538 }
539 }
540
mtkleine714e752014-07-31 12:13:48 -0700541private:
542 const BenchRegistry* fBenches;
543 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700544 SkIRect fClip;
545 SkTArray<SkScalar> fScales;
546 SkTArray<SkString> fSKPs;
547
mtkleinfd731ce2014-09-10 12:19:30 -0700548 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
549 const char* fBenchType; // How we bench it: micro, recording, playback, ...
550 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700551 int fCurrentScale;
552 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700553};
554
caryclark17f0b6d2014-07-22 10:15:34 -0700555int nanobench_main();
556int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700557 SetupCrashHandler();
558 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700559
krajcevski69a55602014-08-13 10:46:31 -0700560#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700561 GrContext::Options grContextOpts;
562 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
563 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700564#endif
565
bsalomon6eb03cc2014-08-07 14:28:50 -0700566 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700567 FLAGS_samples = 1;
568 FLAGS_gpuFrameLag = 0;
569 }
570
bsalomon6eb03cc2014-08-07 14:28:50 -0700571 if (!FLAGS_writePath.isEmpty()) {
572 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
573 if (!sk_mkdir(FLAGS_writePath[0])) {
574 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
575 FLAGS_writePath.set(0, NULL);
576 }
577 }
578
mtklein1915b622014-08-20 11:45:00 -0700579 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700580 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700581 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700582 }
mtklein1915b622014-08-20 11:45:00 -0700583
584 if (1 == FLAGS_properties.count() % 2) {
585 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
586 return 1;
587 }
588 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
589 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
590 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700591
592 if (1 == FLAGS_key.count() % 2) {
593 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
594 return 1;
595 }
596 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700597 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700598 }
mtklein60317d0f2014-07-14 11:30:37 -0700599
mtkleinf3723212014-06-25 14:08:00 -0700600 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700601 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400602
mtkleinbb6a0282014-07-01 08:43:42 -0700603 SkAutoTMalloc<double> samples(FLAGS_samples);
604
bsalomon6eb03cc2014-08-07 14:28:50 -0700605 if (kAutoTuneLoops != FLAGS_loops) {
606 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700607 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700608 // No header.
609 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700610 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700611 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700612 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
613 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700614 }
615
bsalomonc2553372014-07-22 13:09:05 -0700616 SkTDArray<Config> configs;
617 create_configs(&configs);
618
mtklein92007582014-08-01 07:46:52 -0700619 BenchmarkStream benchStream;
620 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700621 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700622 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700623 continue;
624 }
625
mtkleinbb6a0282014-07-01 08:43:42 -0700626 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700627 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700628
jcgregoriobf5e5232014-07-17 13:14:16 -0700629 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700630 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700631 bench->preDraw();
632 }
mtkleinbb6a0282014-07-01 08:43:42 -0700633 for (int j = 0; j < targets.count(); j++) {
634 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700635 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700636
mtkleinbb6a0282014-07-01 08:43:42 -0700637 const int loops =
638#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700639 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700640 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
641 :
642#endif
643 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700644
bsalomon49f085d2014-09-05 13:34:00 -0700645 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700646 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700647 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700648 pngFilename.append(".png");
649 write_canvas_png(canvas, pngFilename);
650 }
651
652 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700653 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400654 continue;
655 }
656
mtkleinf3723212014-06-25 14:08:00 -0700657 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700658 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700659 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700660 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700661#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700662 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700663 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700664 }
665#endif
mtklein1915b622014-08-20 11:45:00 -0700666 log->timer("min_ms", stats.min);
667 log->timer("median_ms", stats.median);
668 log->timer("mean_ms", stats.mean);
669 log->timer("max_ms", stats.max);
670 log->timer("stddev_ms", sqrt(stats.var));
mtklein60317d0f2014-07-14 11:30:37 -0700671
bsalomon6eb03cc2014-08-07 14:28:50 -0700672 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700673 if (targets.count() == 1) {
674 config = ""; // Only print the config if we run the same bench on more than one.
675 }
mtklein53d25622014-09-18 07:39:42 -0700676 SkDebugf("%4dM\t%s\t%s\n"
677 , sk_tools::getMaxResidentSetSizeMB()
678 , bench->getUniqueName()
679 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700680 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700681 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700682 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700683 }
mtklein96289052014-09-10 12:05:59 -0700684 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700685 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700686 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700687 config = ""; // Only print the config if we run the same bench on more than one.
688 }
mtklein96289052014-09-10 12:05:59 -0700689 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700690 } else {
691 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700692 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
693 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700694 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700695 , HUMANIZE(stats.min)
696 , HUMANIZE(stats.median)
697 , HUMANIZE(stats.mean)
698 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700699 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700700 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700701 , config
mtklein96289052014-09-10 12:05:59 -0700702 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700703 );
704 }
705 }
mtkleinbb6a0282014-07-01 08:43:42 -0700706 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400707
708 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700709 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700710 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700711 }
712 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700713 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400714 }
715 #endif
mtkleinf3723212014-06-25 14:08:00 -0700716 }
717
718 return 0;
719}
720
721#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700722int main(int argc, char** argv) {
723 SkCommandLineFlags::Parse(argc, argv);
724 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700725}
726#endif