blob: d06d75358d0a56831065f4ca1f22099549929b40 [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
reed53249782014-10-10 09:09:52 -070038static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070039
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
kkinnunen9e61bb72014-10-09 05:24:15 -070087static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContext* 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
reed53249782014-10-10 09:09:52 -0700117static int detect_forever_loops(int loops) {
118 // look for a magic run-forever value
119 if (loops < 0) {
120 loops = SK_MaxS32;
121 }
122 return loops;
123}
124
mtklein55b0ffc2014-07-17 08:38:23 -0700125static int clamp_loops(int loops) {
126 if (loops < 1) {
127 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
128 return 1;
129 }
130 if (loops > FLAGS_maxLoops) {
131 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
132 return FLAGS_maxLoops;
133 }
134 return loops;
135}
136
bsalomon6eb03cc2014-08-07 14:28:50 -0700137static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
138 if (filename.isEmpty()) {
139 return false;
140 }
reede5ea5002014-09-03 11:54:58 -0700141 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700142 return false;
143 }
144 SkBitmap bmp;
145 bmp.setInfo(canvas->imageInfo());
146 if (!canvas->readPixels(&bmp, 0, 0)) {
147 SkDebugf("Can't read canvas pixels.\n");
148 return false;
149 }
150 SkString dir = SkOSPath::Dirname(filename.c_str());
151 if (!sk_mkdir(dir.c_str())) {
152 SkDebugf("Can't make dir %s.\n", dir.c_str());
153 return false;
154 }
155 SkFILEWStream stream(filename.c_str());
156 if (!stream.isValid()) {
157 SkDebugf("Can't write %s.\n", filename.c_str());
158 return false;
159 }
160 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
161 SkDebugf("Can't encode a PNG.\n");
162 return false;
163 }
164 return true;
165}
166
167static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700168static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
169 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700170 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700171 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700172 if (kAutoTuneLoops == FLAGS_loops) {
173 while (bench_plus_overhead < overhead) {
174 if (round++ == FLAGS_maxCalibrationAttempts) {
175 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700176 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700177 return kFailedLoops;
178 }
179 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700180 }
mtklein2069e222014-08-04 13:57:39 -0700181 }
mtkleinf3723212014-06-25 14:08:00 -0700182
mtkleinbb6a0282014-07-01 08:43:42 -0700183 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700184 // We'll pick N to make timer overhead negligible:
185 //
mtkleinbb6a0282014-07-01 08:43:42 -0700186 // overhead
187 // ------------------------- < FLAGS_overheadGoal
188 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700189 //
mtkleinbb6a0282014-07-01 08:43:42 -0700190 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700191 //
192 // Doing some math, we get:
193 //
mtkleinbb6a0282014-07-01 08:43:42 -0700194 // (overhead / FLAGS_overheadGoal) - overhead
195 // ------------------------------------------ < N
196 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700197 //
198 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700199 int loops = FLAGS_loops;
200 if (kAutoTuneLoops == loops) {
201 const double numer = overhead / FLAGS_overheadGoal - overhead;
202 const double denom = bench_plus_overhead - overhead;
203 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700204 loops = clamp_loops(loops);
205 } else {
206 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700207 }
mtkleinbb6a0282014-07-01 08:43:42 -0700208
209 for (int i = 0; i < FLAGS_samples; i++) {
210 samples[i] = time(loops, bench, canvas, NULL) / loops;
211 }
212 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700213}
214
mtkleinbb6a0282014-07-01 08:43:42 -0700215#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700216static int gpu_bench(SkGLContext* gl,
mtkleinbb6a0282014-07-01 08:43:42 -0700217 Benchmark* bench,
218 SkCanvas* canvas,
219 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700220 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700221 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700222 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700223
224 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700225 int loops = FLAGS_loops;
226 if (kAutoTuneLoops == loops) {
227 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700228 double elapsed = 0;
229 do {
230 loops *= 2;
231 // If the GPU lets frames lag at all, we need to make sure we're timing
232 // _this_ round, not still timing last round. We force this by looping
233 // more times than any reasonable GPU will allow frames to lag.
234 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
235 elapsed = time(loops, bench, canvas, gl);
236 }
237 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700238
mtkleina189ccd2014-07-14 12:28:47 -0700239 // We've overshot at least a little. Scale back linearly.
240 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700241 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700242
mtkleina189ccd2014-07-14 12:28:47 -0700243 // Might as well make sure we're not still timing our calibration.
244 SK_GL(*gl, Finish());
reed53249782014-10-10 09:09:52 -0700245 } else {
246 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700247 }
mtkleinbb6a0282014-07-01 08:43:42 -0700248
249 // Pretty much the same deal as the calibration: do some warmup to make
250 // sure we're timing steady-state pipelined frames.
251 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
252 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700253 }
mtkleinbb6a0282014-07-01 08:43:42 -0700254
255 // Now, actually do the timing!
256 for (int i = 0; i < FLAGS_samples; i++) {
257 samples[i] = time(loops, bench, canvas, gl) / loops;
258 }
259 return loops;
260}
261#endif
262
263static SkString to_lower(const char* str) {
264 SkString lower(str);
265 for (size_t i = 0; i < lower.size(); i++) {
266 lower[i] = tolower(lower[i]);
267 }
268 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700269}
270
bsalomonc2553372014-07-22 13:09:05 -0700271struct Config {
272 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700273 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700274 SkColorType color;
275 SkAlphaType alpha;
276 int samples;
277#if SK_SUPPORT_GPU
278 GrContextFactory::GLContextType ctxType;
279#else
280 int bogusInt;
281#endif
282};
283
284struct Target {
285 explicit Target(const Config& c) : config(c) {}
286 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700287 SkAutoTDelete<SkSurface> surface;
288#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700289 SkGLContext* gl;
mtkleinbb6a0282014-07-01 08:43:42 -0700290#endif
291};
mtkleinf3723212014-06-25 14:08:00 -0700292
bsalomonc2553372014-07-22 13:09:05 -0700293static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700294 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700295 if (to_lower(FLAGS_config[i]).equals(name)) {
296 return true;
mtkleinf3723212014-06-25 14:08:00 -0700297 }
298 }
bsalomonc2553372014-07-22 13:09:05 -0700299 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700300}
301
bsalomonc2553372014-07-22 13:09:05 -0700302#if SK_SUPPORT_GPU
303static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
304 int sampleCnt) {
305 if (!is_cpu_config_allowed(name)) {
306 return false;
307 }
krajcevski69a55602014-08-13 10:46:31 -0700308 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700309 return sampleCnt <= ctx->getMaxSampleCount();
310 }
311 return false;
312}
313#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700314
bsalomonc2553372014-07-22 13:09:05 -0700315#if SK_SUPPORT_GPU
316#define kBogusGLContextType GrContextFactory::kNative_GLContextType
317#else
318#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700319#endif
bsalomonc2553372014-07-22 13:09:05 -0700320
321// Append all configs that are enabled and supported.
322static void create_configs(SkTDArray<Config>* configs) {
323 #define CPU_CONFIG(name, backend, color, alpha) \
324 if (is_cpu_config_allowed(#name)) { \
325 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
326 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700327 }
mtkleine714e752014-07-31 12:13:48 -0700328
mtklein40b32be2014-07-09 08:46:49 -0700329 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700330 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
331 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
332 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700333 }
mtkleinbb6a0282014-07-01 08:43:42 -0700334
335#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700336 #define GPU_CONFIG(name, ctxType, samples) \
337 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
338 Config config = { \
339 #name, \
340 Benchmark::kGPU_Backend, \
341 kN32_SkColorType, \
342 kPremul_SkAlphaType, \
343 samples, \
344 GrContextFactory::ctxType }; \
345 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700346 }
mtkleine714e752014-07-31 12:13:48 -0700347
mtklein40b32be2014-07-09 08:46:49 -0700348 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700349 GPU_CONFIG(gpu, kNative_GLContextType, 0)
350 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
351 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
352 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
353 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
354 GPU_CONFIG(debug, kDebug_GLContextType, 0)
355 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700356#ifdef SK_ANGLE
357 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
358#endif
mtklein40b32be2014-07-09 08:46:49 -0700359 }
mtkleinbb6a0282014-07-01 08:43:42 -0700360#endif
mtkleinf3723212014-06-25 14:08:00 -0700361}
362
bsalomonc2553372014-07-22 13:09:05 -0700363// If bench is enabled for config, returns a Target* for it, otherwise NULL.
364static Target* is_enabled(Benchmark* bench, const Config& config) {
365 if (!bench->isSuitableFor(config.backend)) {
366 return NULL;
367 }
368
reede5ea5002014-09-03 11:54:58 -0700369 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
370 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700371
372 Target* target = new Target(config);
373
374 if (Benchmark::kRaster_Backend == config.backend) {
375 target->surface.reset(SkSurface::NewRaster(info));
376 }
377#if SK_SUPPORT_GPU
378 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700379 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700380 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700381 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700382 }
383#endif
384
385 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
386 delete target;
387 return NULL;
388 }
389 return target;
390}
391
392// Creates targets for a benchmark and a set of configs.
393static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
394 const SkTDArray<Config>& configs) {
395 for (int i = 0; i < configs.count(); ++i) {
396 if (Target* t = is_enabled(b, configs[i])) {
397 targets->push(t);
398 }
mtkleine714e752014-07-31 12:13:48 -0700399
bsalomonc2553372014-07-22 13:09:05 -0700400 }
401}
402
jcgregoriobf5e5232014-07-17 13:14:16 -0700403#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700404static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700405 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700406 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
407 log->configOption("GL_VERSION", (const char*)(version));
408
409 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
410 log->configOption("GL_RENDERER", (const char*) version);
411
412 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
413 log->configOption("GL_VENDOR", (const char*) version);
414
415 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
416 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
417}
418#endif
419
mtkleine714e752014-07-31 12:13:48 -0700420class BenchmarkStream {
421public:
mtklein92007582014-08-01 07:46:52 -0700422 BenchmarkStream() : fBenches(BenchRegistry::Head())
423 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700424 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700425 , fCurrentScale(0)
426 , fCurrentSKP(0) {
427 for (int i = 0; i < FLAGS_skps.count(); i++) {
428 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
429 fSKPs.push_back() = FLAGS_skps[i];
430 } else {
431 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
432 SkString path;
433 while (it.next(&path)) {
434 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
435 }
436 }
437 }
mtkleine714e752014-07-31 12:13:48 -0700438
mtklein92007582014-08-01 07:46:52 -0700439 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
440 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
441 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
442 exit(1);
443 }
444
445 for (int i = 0; i < FLAGS_scales.count(); i++) {
446 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
447 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
448 exit(1);
449 }
450 }
451 }
452
mtkleinfd731ce2014-09-10 12:19:30 -0700453 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
454 // Not strictly necessary, as it will be checked again later,
455 // but helps to avoid a lot of pointless work if we're going to skip it.
456 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
457 return false;
458 }
459
460 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
461 if (stream.get() == NULL) {
462 SkDebugf("Could not read %s.\n", path);
463 return false;
464 }
465
mtklein963504b2014-09-17 06:58:39 -0700466 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700467 if (pic->get() == NULL) {
468 SkDebugf("Could not read %s as an SkPicture.\n", path);
469 return false;
470 }
471 return true;
472 }
473
mtklein92007582014-08-01 07:46:52 -0700474 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700475 if (fBenches) {
476 Benchmark* bench = fBenches->factory()(NULL);
477 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700478 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700479 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700480 return bench;
481 }
mtklein92007582014-08-01 07:46:52 -0700482
mtkleine714e752014-07-31 12:13:48 -0700483 while (fGMs) {
484 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
485 fGMs = fGMs->next();
486 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700487 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700488 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700489 return SkNEW_ARGS(GMBench, (gm.detach()));
490 }
491 }
mtklein92007582014-08-01 07:46:52 -0700492
mtkleinfd731ce2014-09-10 12:19:30 -0700493 // First add all .skps as RecordingBenches.
494 while (fCurrentRecording < fSKPs.count()) {
495 const SkString& path = fSKPs[fCurrentRecording++];
496 SkAutoTUnref<SkPicture> pic;
497 if (!ReadPicture(path.c_str(), &pic)) {
498 continue;
499 }
500 SkString name = SkOSPath::Basename(path.c_str());
501 fSourceType = "skp";
502 fBenchType = "recording";
503 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
504 }
505
506 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700507 while (fCurrentScale < fScales.count()) {
508 while (fCurrentSKP < fSKPs.count()) {
509 const SkString& path = fSKPs[fCurrentSKP++];
mtkleinfd731ce2014-09-10 12:19:30 -0700510 SkAutoTUnref<SkPicture> pic;
511 if (!ReadPicture(path.c_str(), &pic)) {
mtklein92007582014-08-01 07:46:52 -0700512 continue;
513 }
mtklein20840502014-08-21 15:51:22 -0700514 if (FLAGS_bbh) {
515 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
516 // Here we use an SkTileGrid with parameters optimized for FLAGS_clip.
517 const SkTileGridFactory::TileGridInfo info = {
518 SkISize::Make(fClip.width(), fClip.height()), // tile interval
519 SkISize::Make(0,0), // margin
520 SkIPoint::Make(fClip.left(), fClip.top()), // offset
521 };
522 SkTileGridFactory factory(info);
523 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700524 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtkleinea65bfa2014-09-09 07:59:46 -0700525 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700526 &factory));
mtklein20840502014-08-21 15:51:22 -0700527 pic.reset(recorder.endRecording());
528 }
mtkleinfd731ce2014-09-10 12:19:30 -0700529 SkString name = SkOSPath::Basename(path.c_str());
mtklein92007582014-08-01 07:46:52 -0700530 fSourceType = "skp";
mtkleinfd731ce2014-09-10 12:19:30 -0700531 fBenchType = "playback";
mtklein92007582014-08-01 07:46:52 -0700532 return SkNEW_ARGS(SKPBench,
533 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
534 }
535 fCurrentSKP = 0;
536 fCurrentScale++;
537 }
538
mtkleine714e752014-07-31 12:13:48 -0700539 return NULL;
540 }
mtklein92007582014-08-01 07:46:52 -0700541
542 void fillCurrentOptions(ResultsWriter* log) const {
543 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700544 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700545 if (0 == strcmp(fSourceType, "skp")) {
546 log->configOption("clip",
547 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
548 fClip.fRight, fClip.fBottom).c_str());
549 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
550 }
551 }
552
mtkleine714e752014-07-31 12:13:48 -0700553private:
554 const BenchRegistry* fBenches;
555 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700556 SkIRect fClip;
557 SkTArray<SkScalar> fScales;
558 SkTArray<SkString> fSKPs;
559
mtkleinfd731ce2014-09-10 12:19:30 -0700560 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
561 const char* fBenchType; // How we bench it: micro, recording, playback, ...
562 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700563 int fCurrentScale;
564 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700565};
566
caryclark17f0b6d2014-07-22 10:15:34 -0700567int nanobench_main();
568int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700569 SetupCrashHandler();
570 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700571
krajcevski69a55602014-08-13 10:46:31 -0700572#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700573 GrContext::Options grContextOpts;
574 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
575 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700576#endif
577
bsalomon6eb03cc2014-08-07 14:28:50 -0700578 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700579 FLAGS_samples = 1;
580 FLAGS_gpuFrameLag = 0;
581 }
582
bsalomon6eb03cc2014-08-07 14:28:50 -0700583 if (!FLAGS_writePath.isEmpty()) {
584 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
585 if (!sk_mkdir(FLAGS_writePath[0])) {
586 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
587 FLAGS_writePath.set(0, NULL);
588 }
589 }
590
mtklein1915b622014-08-20 11:45:00 -0700591 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700592 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700593 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700594 }
mtklein1915b622014-08-20 11:45:00 -0700595
596 if (1 == FLAGS_properties.count() % 2) {
597 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
598 return 1;
599 }
600 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
601 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
602 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700603
604 if (1 == FLAGS_key.count() % 2) {
605 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
606 return 1;
607 }
608 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700609 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700610 }
mtklein60317d0f2014-07-14 11:30:37 -0700611
mtkleinf3723212014-06-25 14:08:00 -0700612 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700613 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400614
mtkleinbb6a0282014-07-01 08:43:42 -0700615 SkAutoTMalloc<double> samples(FLAGS_samples);
616
bsalomon6eb03cc2014-08-07 14:28:50 -0700617 if (kAutoTuneLoops != FLAGS_loops) {
618 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700619 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700620 // No header.
621 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700622 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700623 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700624 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
625 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700626 }
627
bsalomonc2553372014-07-22 13:09:05 -0700628 SkTDArray<Config> configs;
629 create_configs(&configs);
630
mtklein92007582014-08-01 07:46:52 -0700631 BenchmarkStream benchStream;
632 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700633 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700634 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700635 continue;
636 }
637
mtkleinbb6a0282014-07-01 08:43:42 -0700638 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700639 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700640
jcgregoriobf5e5232014-07-17 13:14:16 -0700641 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700642 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700643 bench->preDraw();
644 }
mtkleinbb6a0282014-07-01 08:43:42 -0700645 for (int j = 0; j < targets.count(); j++) {
646 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700647 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700648
mtkleinbb6a0282014-07-01 08:43:42 -0700649 const int loops =
650#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700651 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700652 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
653 :
654#endif
655 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700656
bsalomon49f085d2014-09-05 13:34:00 -0700657 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700658 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700659 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700660 pngFilename.append(".png");
661 write_canvas_png(canvas, pngFilename);
662 }
663
664 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700665 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400666 continue;
667 }
668
mtkleinf3723212014-06-25 14:08:00 -0700669 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700670 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700671 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700672 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700673#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700674 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700675 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700676 }
677#endif
mtklein1915b622014-08-20 11:45:00 -0700678 log->timer("min_ms", stats.min);
679 log->timer("median_ms", stats.median);
680 log->timer("mean_ms", stats.mean);
681 log->timer("max_ms", stats.max);
682 log->timer("stddev_ms", sqrt(stats.var));
mtklein60317d0f2014-07-14 11:30:37 -0700683
bsalomon6eb03cc2014-08-07 14:28:50 -0700684 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700685 if (targets.count() == 1) {
686 config = ""; // Only print the config if we run the same bench on more than one.
687 }
mtklein53d25622014-09-18 07:39:42 -0700688 SkDebugf("%4dM\t%s\t%s\n"
689 , sk_tools::getMaxResidentSetSizeMB()
690 , bench->getUniqueName()
691 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700692 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700693 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700694 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700695 }
mtklein96289052014-09-10 12:05:59 -0700696 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700697 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700698 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700699 config = ""; // Only print the config if we run the same bench on more than one.
700 }
mtklein96289052014-09-10 12:05:59 -0700701 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700702 } else {
703 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700704 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
705 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700706 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700707 , HUMANIZE(stats.min)
708 , HUMANIZE(stats.median)
709 , HUMANIZE(stats.mean)
710 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700711 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700712 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700713 , config
mtklein96289052014-09-10 12:05:59 -0700714 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700715 );
716 }
717 }
mtkleinbb6a0282014-07-01 08:43:42 -0700718 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400719
720 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700721 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700722 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700723 }
724 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700725 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400726 }
727 #endif
mtkleinf3723212014-06-25 14:08:00 -0700728 }
729
730 return 0;
731}
732
733#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700734int main(int argc, char** argv) {
735 SkCommandLineFlags::Parse(argc, argv);
736 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700737}
738#endif