blob: 67646c266770adccafd78800b98850dd8f19ff97 [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) {
75 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
76 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070077#ifdef SK_BUILD_FOR_WIN
78 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
79#else
mtkleinf3723212014-06-25 14:08:00 -070080 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070081#endif
mtkleinf3723212014-06-25 14:08:00 -070082 return SkStringPrintf("%.3gms", ms);
83}
mtklein55b0ffc2014-07-17 08:38:23 -070084#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070085
mtkleinbb6a0282014-07-01 08:43:42 -070086static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070087 if (canvas) {
88 canvas->clear(SK_ColorWHITE);
89 }
mtkleinbb6a0282014-07-01 08:43:42 -070090 WallTimer timer;
91 timer.start();
92 if (bench) {
93 bench->draw(loops, canvas);
94 }
95 if (canvas) {
96 canvas->flush();
97 }
98#if SK_SUPPORT_GPU
99 if (gl) {
100 SK_GL(*gl, Flush());
101 gl->swapBuffers();
102 }
103#endif
104 timer.end();
105 return timer.fWall;
106}
107
mtkleinf3723212014-06-25 14:08:00 -0700108static double estimate_timer_overhead() {
109 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700110 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700111 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700112 }
113 return overhead / FLAGS_overheadLoops;
114}
115
mtklein55b0ffc2014-07-17 08:38:23 -0700116static int clamp_loops(int loops) {
117 if (loops < 1) {
118 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
119 return 1;
120 }
121 if (loops > FLAGS_maxLoops) {
122 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
123 return FLAGS_maxLoops;
124 }
125 return loops;
126}
127
bsalomon6eb03cc2014-08-07 14:28:50 -0700128static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
129 if (filename.isEmpty()) {
130 return false;
131 }
reede5ea5002014-09-03 11:54:58 -0700132 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700133 return false;
134 }
135 SkBitmap bmp;
136 bmp.setInfo(canvas->imageInfo());
137 if (!canvas->readPixels(&bmp, 0, 0)) {
138 SkDebugf("Can't read canvas pixels.\n");
139 return false;
140 }
141 SkString dir = SkOSPath::Dirname(filename.c_str());
142 if (!sk_mkdir(dir.c_str())) {
143 SkDebugf("Can't make dir %s.\n", dir.c_str());
144 return false;
145 }
146 SkFILEWStream stream(filename.c_str());
147 if (!stream.isValid()) {
148 SkDebugf("Can't write %s.\n", filename.c_str());
149 return false;
150 }
151 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
152 SkDebugf("Can't encode a PNG.\n");
153 return false;
154 }
155 return true;
156}
157
158static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700159static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
160 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700161 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700162 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700163 if (kAutoTuneLoops == FLAGS_loops) {
164 while (bench_plus_overhead < overhead) {
165 if (round++ == FLAGS_maxCalibrationAttempts) {
166 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700167 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700168 return kFailedLoops;
169 }
170 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700171 }
mtklein2069e222014-08-04 13:57:39 -0700172 }
mtkleinf3723212014-06-25 14:08:00 -0700173
mtkleinbb6a0282014-07-01 08:43:42 -0700174 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700175 // We'll pick N to make timer overhead negligible:
176 //
mtkleinbb6a0282014-07-01 08:43:42 -0700177 // overhead
178 // ------------------------- < FLAGS_overheadGoal
179 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700180 //
mtkleinbb6a0282014-07-01 08:43:42 -0700181 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700182 //
183 // Doing some math, we get:
184 //
mtkleinbb6a0282014-07-01 08:43:42 -0700185 // (overhead / FLAGS_overheadGoal) - overhead
186 // ------------------------------------------ < N
187 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700188 //
189 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700190 int loops = FLAGS_loops;
191 if (kAutoTuneLoops == loops) {
192 const double numer = overhead / FLAGS_overheadGoal - overhead;
193 const double denom = bench_plus_overhead - overhead;
194 loops = (int)ceil(numer / denom);
195 }
196 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700197
198 for (int i = 0; i < FLAGS_samples; i++) {
199 samples[i] = time(loops, bench, canvas, NULL) / loops;
200 }
201 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700202}
203
mtkleinbb6a0282014-07-01 08:43:42 -0700204#if SK_SUPPORT_GPU
205static int gpu_bench(SkGLContextHelper* gl,
206 Benchmark* bench,
207 SkCanvas* canvas,
208 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700209 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700210 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700211 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700212
213 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700214 int loops = FLAGS_loops;
215 if (kAutoTuneLoops == loops) {
216 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700217 double elapsed = 0;
218 do {
219 loops *= 2;
220 // If the GPU lets frames lag at all, we need to make sure we're timing
221 // _this_ round, not still timing last round. We force this by looping
222 // more times than any reasonable GPU will allow frames to lag.
223 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
224 elapsed = time(loops, bench, canvas, gl);
225 }
226 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700227
mtkleina189ccd2014-07-14 12:28:47 -0700228 // We've overshot at least a little. Scale back linearly.
229 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700230
mtkleina189ccd2014-07-14 12:28:47 -0700231 // Might as well make sure we're not still timing our calibration.
232 SK_GL(*gl, Finish());
233 }
mtklein55b0ffc2014-07-17 08:38:23 -0700234 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700235
236 // Pretty much the same deal as the calibration: do some warmup to make
237 // sure we're timing steady-state pipelined frames.
238 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
239 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700240 }
mtkleinbb6a0282014-07-01 08:43:42 -0700241
242 // Now, actually do the timing!
243 for (int i = 0; i < FLAGS_samples; i++) {
244 samples[i] = time(loops, bench, canvas, gl) / loops;
245 }
246 return loops;
247}
248#endif
249
250static SkString to_lower(const char* str) {
251 SkString lower(str);
252 for (size_t i = 0; i < lower.size(); i++) {
253 lower[i] = tolower(lower[i]);
254 }
255 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700256}
257
bsalomonc2553372014-07-22 13:09:05 -0700258struct Config {
259 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700260 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700261 SkColorType color;
262 SkAlphaType alpha;
263 int samples;
264#if SK_SUPPORT_GPU
265 GrContextFactory::GLContextType ctxType;
266#else
267 int bogusInt;
268#endif
269};
270
271struct Target {
272 explicit Target(const Config& c) : config(c) {}
273 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700274 SkAutoTDelete<SkSurface> surface;
275#if SK_SUPPORT_GPU
276 SkGLContextHelper* gl;
277#endif
278};
mtkleinf3723212014-06-25 14:08:00 -0700279
bsalomonc2553372014-07-22 13:09:05 -0700280static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700281 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700282 if (to_lower(FLAGS_config[i]).equals(name)) {
283 return true;
mtkleinf3723212014-06-25 14:08:00 -0700284 }
285 }
bsalomonc2553372014-07-22 13:09:05 -0700286 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700287}
288
bsalomonc2553372014-07-22 13:09:05 -0700289#if SK_SUPPORT_GPU
290static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
291 int sampleCnt) {
292 if (!is_cpu_config_allowed(name)) {
293 return false;
294 }
krajcevski69a55602014-08-13 10:46:31 -0700295 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700296 return sampleCnt <= ctx->getMaxSampleCount();
297 }
298 return false;
299}
300#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700301
bsalomonc2553372014-07-22 13:09:05 -0700302#if SK_SUPPORT_GPU
303#define kBogusGLContextType GrContextFactory::kNative_GLContextType
304#else
305#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700306#endif
bsalomonc2553372014-07-22 13:09:05 -0700307
308// Append all configs that are enabled and supported.
309static void create_configs(SkTDArray<Config>* configs) {
310 #define CPU_CONFIG(name, backend, color, alpha) \
311 if (is_cpu_config_allowed(#name)) { \
312 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
313 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700314 }
mtkleine714e752014-07-31 12:13:48 -0700315
mtklein40b32be2014-07-09 08:46:49 -0700316 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700317 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
318 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
319 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700320 }
mtkleinbb6a0282014-07-01 08:43:42 -0700321
322#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700323 #define GPU_CONFIG(name, ctxType, samples) \
324 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
325 Config config = { \
326 #name, \
327 Benchmark::kGPU_Backend, \
328 kN32_SkColorType, \
329 kPremul_SkAlphaType, \
330 samples, \
331 GrContextFactory::ctxType }; \
332 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700333 }
mtkleine714e752014-07-31 12:13:48 -0700334
mtklein40b32be2014-07-09 08:46:49 -0700335 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700336 GPU_CONFIG(gpu, kNative_GLContextType, 0)
337 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
338 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
339 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
340 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
341 GPU_CONFIG(debug, kDebug_GLContextType, 0)
342 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700343#ifdef SK_ANGLE
344 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
345#endif
mtklein40b32be2014-07-09 08:46:49 -0700346 }
mtkleinbb6a0282014-07-01 08:43:42 -0700347#endif
mtkleinf3723212014-06-25 14:08:00 -0700348}
349
bsalomonc2553372014-07-22 13:09:05 -0700350// If bench is enabled for config, returns a Target* for it, otherwise NULL.
351static Target* is_enabled(Benchmark* bench, const Config& config) {
352 if (!bench->isSuitableFor(config.backend)) {
353 return NULL;
354 }
355
reede5ea5002014-09-03 11:54:58 -0700356 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
357 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700358
359 Target* target = new Target(config);
360
361 if (Benchmark::kRaster_Backend == config.backend) {
362 target->surface.reset(SkSurface::NewRaster(info));
363 }
364#if SK_SUPPORT_GPU
365 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700366 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700367 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700368 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700369 }
370#endif
371
372 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
373 delete target;
374 return NULL;
375 }
376 return target;
377}
378
379// Creates targets for a benchmark and a set of configs.
380static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
381 const SkTDArray<Config>& configs) {
382 for (int i = 0; i < configs.count(); ++i) {
383 if (Target* t = is_enabled(b, configs[i])) {
384 targets->push(t);
385 }
mtkleine714e752014-07-31 12:13:48 -0700386
bsalomonc2553372014-07-22 13:09:05 -0700387 }
388}
389
jcgregoriobf5e5232014-07-17 13:14:16 -0700390#if SK_SUPPORT_GPU
391static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700392 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700393 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
394 log->configOption("GL_VERSION", (const char*)(version));
395
396 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
397 log->configOption("GL_RENDERER", (const char*) version);
398
399 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
400 log->configOption("GL_VENDOR", (const char*) version);
401
402 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
403 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
404}
405#endif
406
mtkleine714e752014-07-31 12:13:48 -0700407class BenchmarkStream {
408public:
mtklein92007582014-08-01 07:46:52 -0700409 BenchmarkStream() : fBenches(BenchRegistry::Head())
410 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700411 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700412 , fCurrentScale(0)
413 , fCurrentSKP(0) {
414 for (int i = 0; i < FLAGS_skps.count(); i++) {
415 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
416 fSKPs.push_back() = FLAGS_skps[i];
417 } else {
418 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
419 SkString path;
420 while (it.next(&path)) {
421 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
422 }
423 }
424 }
mtkleine714e752014-07-31 12:13:48 -0700425
mtklein92007582014-08-01 07:46:52 -0700426 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
427 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
428 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
429 exit(1);
430 }
431
432 for (int i = 0; i < FLAGS_scales.count(); i++) {
433 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
434 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
435 exit(1);
436 }
437 }
438 }
439
mtkleinfd731ce2014-09-10 12:19:30 -0700440 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
441 // Not strictly necessary, as it will be checked again later,
442 // but helps to avoid a lot of pointless work if we're going to skip it.
443 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
444 return false;
445 }
446
447 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
448 if (stream.get() == NULL) {
449 SkDebugf("Could not read %s.\n", path);
450 return false;
451 }
452
453 pic->reset(SkPicture::CreateFromStream(stream.get()));
454 if (pic->get() == NULL) {
455 SkDebugf("Could not read %s as an SkPicture.\n", path);
456 return false;
457 }
458 return true;
459 }
460
mtklein92007582014-08-01 07:46:52 -0700461 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700462 if (fBenches) {
463 Benchmark* bench = fBenches->factory()(NULL);
464 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700465 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700466 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700467 return bench;
468 }
mtklein92007582014-08-01 07:46:52 -0700469
mtkleine714e752014-07-31 12:13:48 -0700470 while (fGMs) {
471 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
472 fGMs = fGMs->next();
473 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700474 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700475 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700476 return SkNEW_ARGS(GMBench, (gm.detach()));
477 }
478 }
mtklein92007582014-08-01 07:46:52 -0700479
mtkleinfd731ce2014-09-10 12:19:30 -0700480 // First add all .skps as RecordingBenches.
481 while (fCurrentRecording < fSKPs.count()) {
482 const SkString& path = fSKPs[fCurrentRecording++];
483 SkAutoTUnref<SkPicture> pic;
484 if (!ReadPicture(path.c_str(), &pic)) {
485 continue;
486 }
487 SkString name = SkOSPath::Basename(path.c_str());
488 fSourceType = "skp";
489 fBenchType = "recording";
490 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
491 }
492
493 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700494 while (fCurrentScale < fScales.count()) {
495 while (fCurrentSKP < fSKPs.count()) {
496 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700497 SkAutoTUnref<SkPicture> pic;
498 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700499 continue;
500 }
mtklein20840502014-08-21 15:51:22 -0700501 if (FLAGS_bbh) {
502 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
503 // Here we use an SkTileGrid with parameters optimized for FLAGS_clip.
504 const SkTileGridFactory::TileGridInfo info = {
505 SkISize::Make(fClip.width(), fClip.height()), // tile interval
506 SkISize::Make(0,0), // margin
507 SkIPoint::Make(fClip.left(), fClip.top()), // offset
508 };
509 SkTileGridFactory factory(info);
510 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700511 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtkleinea65bfa2014-09-09 07:59:46 -0700512 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700513 &factory));
mtklein20840502014-08-21 15:51:22 -0700514 pic.reset(recorder.endRecording());
515 }
mtkleinfd731ce2014-09-10 12:19:30 -0700516 SkString name = SkOSPath::Basename(path.c_str());
mtklein92007582014-08-01 07:46:52 -0700517 fSourceType = "skp";
mtkleinfd731ce2014-09-10 12:19:30 -0700518 fBenchType = "playback";
mtklein92007582014-08-01 07:46:52 -0700519 return SkNEW_ARGS(SKPBench,
520 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
521 }
522 fCurrentSKP = 0;
523 fCurrentScale++;
524 }
525
mtkleine714e752014-07-31 12:13:48 -0700526 return NULL;
527 }
mtklein92007582014-08-01 07:46:52 -0700528
529 void fillCurrentOptions(ResultsWriter* log) const {
530 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700531 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700532 if (0 == strcmp(fSourceType, "skp")) {
533 log->configOption("clip",
534 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
535 fClip.fRight, fClip.fBottom).c_str());
536 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
537 }
538 }
539
mtkleine714e752014-07-31 12:13:48 -0700540private:
541 const BenchRegistry* fBenches;
542 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700543 SkIRect fClip;
544 SkTArray<SkScalar> fScales;
545 SkTArray<SkString> fSKPs;
546
mtkleinfd731ce2014-09-10 12:19:30 -0700547 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
548 const char* fBenchType; // How we bench it: micro, recording, playback, ...
549 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700550 int fCurrentScale;
551 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700552};
553
caryclark17f0b6d2014-07-22 10:15:34 -0700554int nanobench_main();
555int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700556 SetupCrashHandler();
557 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700558
krajcevski69a55602014-08-13 10:46:31 -0700559#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700560 GrContext::Options grContextOpts;
561 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
562 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700563#endif
564
bsalomon6eb03cc2014-08-07 14:28:50 -0700565 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700566 FLAGS_samples = 1;
567 FLAGS_gpuFrameLag = 0;
568 }
569
bsalomon6eb03cc2014-08-07 14:28:50 -0700570 if (!FLAGS_writePath.isEmpty()) {
571 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
572 if (!sk_mkdir(FLAGS_writePath[0])) {
573 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
574 FLAGS_writePath.set(0, NULL);
575 }
576 }
577
mtklein1915b622014-08-20 11:45:00 -0700578 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700579 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700580 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700581 }
mtklein1915b622014-08-20 11:45:00 -0700582
583 if (1 == FLAGS_properties.count() % 2) {
584 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
585 return 1;
586 }
587 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
588 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
589 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700590
591 if (1 == FLAGS_key.count() % 2) {
592 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
593 return 1;
594 }
595 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700596 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700597 }
mtklein60317d0f2014-07-14 11:30:37 -0700598
mtkleinf3723212014-06-25 14:08:00 -0700599 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700600 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400601
mtkleinbb6a0282014-07-01 08:43:42 -0700602 SkAutoTMalloc<double> samples(FLAGS_samples);
603
bsalomon6eb03cc2014-08-07 14:28:50 -0700604 if (kAutoTuneLoops != FLAGS_loops) {
605 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700606 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700607 // No header.
608 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700609 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700610 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700611 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
612 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700613 }
614
bsalomonc2553372014-07-22 13:09:05 -0700615 SkTDArray<Config> configs;
616 create_configs(&configs);
617
mtklein92007582014-08-01 07:46:52 -0700618 BenchmarkStream benchStream;
619 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700620 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700621 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700622 continue;
623 }
624
mtkleinbb6a0282014-07-01 08:43:42 -0700625 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700626 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700627
jcgregoriobf5e5232014-07-17 13:14:16 -0700628 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700629 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700630 bench->preDraw();
631 }
mtkleinbb6a0282014-07-01 08:43:42 -0700632 for (int j = 0; j < targets.count(); j++) {
633 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700634 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700635
mtkleinbb6a0282014-07-01 08:43:42 -0700636 const int loops =
637#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700638 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700639 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
640 :
641#endif
642 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700643
bsalomon49f085d2014-09-05 13:34:00 -0700644 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700645 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700646 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700647 pngFilename.append(".png");
648 write_canvas_png(canvas, pngFilename);
649 }
650
651 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700652 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400653 continue;
654 }
655
mtkleinf3723212014-06-25 14:08:00 -0700656 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700657 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700658 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700659 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700660#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700661 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700662 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700663 }
664#endif
mtklein1915b622014-08-20 11:45:00 -0700665 log->timer("min_ms", stats.min);
666 log->timer("median_ms", stats.median);
667 log->timer("mean_ms", stats.mean);
668 log->timer("max_ms", stats.max);
669 log->timer("stddev_ms", sqrt(stats.var));
mtklein60317d0f2014-07-14 11:30:37 -0700670
bsalomon6eb03cc2014-08-07 14:28:50 -0700671 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700672 if (targets.count() == 1) {
673 config = ""; // Only print the config if we run the same bench on more than one.
674 }
mtklein96289052014-09-10 12:05:59 -0700675 SkDebugf("%s\t%s\n", bench->getUniqueName(), config);
mtkleina189ccd2014-07-14 12:28:47 -0700676 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700677 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700678 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700679 }
mtklein96289052014-09-10 12:05:59 -0700680 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700681 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700682 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700683 config = ""; // Only print the config if we run the same bench on more than one.
684 }
mtklein96289052014-09-10 12:05:59 -0700685 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700686 } else {
687 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700688 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
689 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700690 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700691 , HUMANIZE(stats.min)
692 , HUMANIZE(stats.median)
693 , HUMANIZE(stats.mean)
694 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700695 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700696 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700697 , config
mtklein96289052014-09-10 12:05:59 -0700698 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700699 );
700 }
701 }
mtkleinbb6a0282014-07-01 08:43:42 -0700702 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400703
704 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700705 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700706 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700707 }
708 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700709 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400710 }
711 #endif
mtkleinf3723212014-06-25 14:08:00 -0700712 }
713
714 return 0;
715}
716
717#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700718int main(int argc, char** argv) {
719 SkCommandLineFlags::Parse(argc, argv);
720 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700721}
722#endif