blob: 20afdeb32633b6fff7a32f00adc2c3cbe17c66c0 [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
mtkleinf3723212014-06-25 14:08:00 -070010#include "Benchmark.h"
11#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070012#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070013#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070014#include "ResultsWriter.h"
mtklein92007582014-08-01 07:46:52 -070015#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "Stats.h"
17#include "Timer.h"
18
mtklein20840502014-08-21 15:51:22 -070019#include "SkBBHFactory.h"
mtkleinf3723212014-06-25 14:08:00 -070020#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070021#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070022#include "SkForceLinking.h"
23#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070024#include "SkOSFile.h"
25#include "SkPictureRecorder.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "SkString.h"
27#include "SkSurface.h"
28
mtkleinbb6a0282014-07-01 08:43:42 -070029#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070030 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070031 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070032 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070033#endif
34
mtkleinf3723212014-06-25 14:08:00 -070035__SK_FORCE_IMAGE_DECODER_LINKING;
36
bsalomon6eb03cc2014-08-07 14:28:50 -070037static const int kAutoTuneLoops = -1;
38
mtkleinb5110422014-08-07 15:20:02 -070039static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070040#ifdef SK_DEBUG
41 1;
mtkleina189ccd2014-07-14 12:28:47 -070042#else
bsalomon6eb03cc2014-08-07 14:28:50 -070043 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070044#endif
45
bsalomon6eb03cc2014-08-07 14:28:50 -070046static SkString loops_help_txt() {
47 SkString help;
48 help.printf("Number of times to run each bench. Set this to %d to auto-"
49 "tune for each bench. Timings are only reported when auto-tuning.",
50 kAutoTuneLoops);
51 return help;
52}
53
54DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
55
mtkleinf3723212014-06-25 14:08:00 -070056DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
57DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
58DEFINE_double(overheadGoal, 0.0001,
59 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070060DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
61DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070062DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
63 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070064
mtklein60317d0f2014-07-14 11:30:37 -070065DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070066DEFINE_int32(maxCalibrationAttempts, 3,
67 "Try up to this many times to guess loops for a bench, or skip the bench.");
68DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070069DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
70DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070071DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
mtklein92007582014-08-01 07:46:52 -070072
mtkleinf3723212014-06-25 14:08:00 -070073static SkString humanize(double ms) {
74 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
75 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070076#ifdef SK_BUILD_FOR_WIN
77 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
78#else
mtkleinf3723212014-06-25 14:08:00 -070079 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070080#endif
mtkleinf3723212014-06-25 14:08:00 -070081 return SkStringPrintf("%.3gms", ms);
82}
mtklein55b0ffc2014-07-17 08:38:23 -070083#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070084
mtkleinbb6a0282014-07-01 08:43:42 -070085static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070086 if (canvas) {
87 canvas->clear(SK_ColorWHITE);
88 }
mtkleinbb6a0282014-07-01 08:43:42 -070089 WallTimer timer;
90 timer.start();
91 if (bench) {
92 bench->draw(loops, canvas);
93 }
94 if (canvas) {
95 canvas->flush();
96 }
97#if SK_SUPPORT_GPU
98 if (gl) {
99 SK_GL(*gl, Flush());
100 gl->swapBuffers();
101 }
102#endif
103 timer.end();
104 return timer.fWall;
105}
106
mtkleinf3723212014-06-25 14:08:00 -0700107static double estimate_timer_overhead() {
108 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700109 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700110 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700111 }
112 return overhead / FLAGS_overheadLoops;
113}
114
mtklein55b0ffc2014-07-17 08:38:23 -0700115static int clamp_loops(int loops) {
116 if (loops < 1) {
117 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
118 return 1;
119 }
120 if (loops > FLAGS_maxLoops) {
121 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
122 return FLAGS_maxLoops;
123 }
124 return loops;
125}
126
bsalomon6eb03cc2014-08-07 14:28:50 -0700127static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
128 if (filename.isEmpty()) {
129 return false;
130 }
reede5ea5002014-09-03 11:54:58 -0700131 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700132 return false;
133 }
134 SkBitmap bmp;
135 bmp.setInfo(canvas->imageInfo());
136 if (!canvas->readPixels(&bmp, 0, 0)) {
137 SkDebugf("Can't read canvas pixels.\n");
138 return false;
139 }
140 SkString dir = SkOSPath::Dirname(filename.c_str());
141 if (!sk_mkdir(dir.c_str())) {
142 SkDebugf("Can't make dir %s.\n", dir.c_str());
143 return false;
144 }
145 SkFILEWStream stream(filename.c_str());
146 if (!stream.isValid()) {
147 SkDebugf("Can't write %s.\n", filename.c_str());
148 return false;
149 }
150 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
151 SkDebugf("Can't encode a PNG.\n");
152 return false;
153 }
154 return true;
155}
156
157static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700158static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
159 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700160 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700161 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700162 if (kAutoTuneLoops == FLAGS_loops) {
163 while (bench_plus_overhead < overhead) {
164 if (round++ == FLAGS_maxCalibrationAttempts) {
165 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
166 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
167 return kFailedLoops;
168 }
169 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700170 }
mtklein2069e222014-08-04 13:57:39 -0700171 }
mtkleinf3723212014-06-25 14:08:00 -0700172
mtkleinbb6a0282014-07-01 08:43:42 -0700173 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700174 // We'll pick N to make timer overhead negligible:
175 //
mtkleinbb6a0282014-07-01 08:43:42 -0700176 // overhead
177 // ------------------------- < FLAGS_overheadGoal
178 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700179 //
mtkleinbb6a0282014-07-01 08:43:42 -0700180 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700181 //
182 // Doing some math, we get:
183 //
mtkleinbb6a0282014-07-01 08:43:42 -0700184 // (overhead / FLAGS_overheadGoal) - overhead
185 // ------------------------------------------ < N
186 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700187 //
188 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700189 int loops = FLAGS_loops;
190 if (kAutoTuneLoops == loops) {
191 const double numer = overhead / FLAGS_overheadGoal - overhead;
192 const double denom = bench_plus_overhead - overhead;
193 loops = (int)ceil(numer / denom);
194 }
195 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700196
197 for (int i = 0; i < FLAGS_samples; i++) {
198 samples[i] = time(loops, bench, canvas, NULL) / loops;
199 }
200 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700201}
202
mtkleinbb6a0282014-07-01 08:43:42 -0700203#if SK_SUPPORT_GPU
204static int gpu_bench(SkGLContextHelper* gl,
205 Benchmark* bench,
206 SkCanvas* canvas,
207 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700208 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700209 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700210 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700211
212 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700213 int loops = FLAGS_loops;
214 if (kAutoTuneLoops == loops) {
215 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700216 double elapsed = 0;
217 do {
218 loops *= 2;
219 // If the GPU lets frames lag at all, we need to make sure we're timing
220 // _this_ round, not still timing last round. We force this by looping
221 // more times than any reasonable GPU will allow frames to lag.
222 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
223 elapsed = time(loops, bench, canvas, gl);
224 }
225 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700226
mtkleina189ccd2014-07-14 12:28:47 -0700227 // We've overshot at least a little. Scale back linearly.
228 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700229
mtkleina189ccd2014-07-14 12:28:47 -0700230 // Might as well make sure we're not still timing our calibration.
231 SK_GL(*gl, Finish());
232 }
mtklein55b0ffc2014-07-17 08:38:23 -0700233 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700234
235 // Pretty much the same deal as the calibration: do some warmup to make
236 // sure we're timing steady-state pipelined frames.
237 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
238 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700239 }
mtkleinbb6a0282014-07-01 08:43:42 -0700240
241 // Now, actually do the timing!
242 for (int i = 0; i < FLAGS_samples; i++) {
243 samples[i] = time(loops, bench, canvas, gl) / loops;
244 }
245 return loops;
246}
247#endif
248
249static SkString to_lower(const char* str) {
250 SkString lower(str);
251 for (size_t i = 0; i < lower.size(); i++) {
252 lower[i] = tolower(lower[i]);
253 }
254 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700255}
256
bsalomonc2553372014-07-22 13:09:05 -0700257struct Config {
258 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700259 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700260 SkColorType color;
261 SkAlphaType alpha;
262 int samples;
263#if SK_SUPPORT_GPU
264 GrContextFactory::GLContextType ctxType;
265#else
266 int bogusInt;
267#endif
268};
269
270struct Target {
271 explicit Target(const Config& c) : config(c) {}
272 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700273 SkAutoTDelete<SkSurface> surface;
274#if SK_SUPPORT_GPU
275 SkGLContextHelper* gl;
276#endif
277};
mtkleinf3723212014-06-25 14:08:00 -0700278
bsalomonc2553372014-07-22 13:09:05 -0700279static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700280 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700281 if (to_lower(FLAGS_config[i]).equals(name)) {
282 return true;
mtkleinf3723212014-06-25 14:08:00 -0700283 }
284 }
bsalomonc2553372014-07-22 13:09:05 -0700285 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700286}
287
bsalomonc2553372014-07-22 13:09:05 -0700288#if SK_SUPPORT_GPU
289static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
290 int sampleCnt) {
291 if (!is_cpu_config_allowed(name)) {
292 return false;
293 }
krajcevski69a55602014-08-13 10:46:31 -0700294 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700295 return sampleCnt <= ctx->getMaxSampleCount();
296 }
297 return false;
298}
299#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700300
bsalomonc2553372014-07-22 13:09:05 -0700301#if SK_SUPPORT_GPU
302#define kBogusGLContextType GrContextFactory::kNative_GLContextType
303#else
304#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700305#endif
bsalomonc2553372014-07-22 13:09:05 -0700306
307// Append all configs that are enabled and supported.
308static void create_configs(SkTDArray<Config>* configs) {
309 #define CPU_CONFIG(name, backend, color, alpha) \
310 if (is_cpu_config_allowed(#name)) { \
311 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
312 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700313 }
mtkleine714e752014-07-31 12:13:48 -0700314
mtklein40b32be2014-07-09 08:46:49 -0700315 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700316 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
317 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
318 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700319 }
mtkleinbb6a0282014-07-01 08:43:42 -0700320
321#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700322 #define GPU_CONFIG(name, ctxType, samples) \
323 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
324 Config config = { \
325 #name, \
326 Benchmark::kGPU_Backend, \
327 kN32_SkColorType, \
328 kPremul_SkAlphaType, \
329 samples, \
330 GrContextFactory::ctxType }; \
331 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700332 }
mtkleine714e752014-07-31 12:13:48 -0700333
mtklein40b32be2014-07-09 08:46:49 -0700334 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700335 GPU_CONFIG(gpu, kNative_GLContextType, 0)
336 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
337 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
338 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
339 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
340 GPU_CONFIG(debug, kDebug_GLContextType, 0)
341 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700342#ifdef SK_ANGLE
343 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
344#endif
mtklein40b32be2014-07-09 08:46:49 -0700345 }
mtkleinbb6a0282014-07-01 08:43:42 -0700346#endif
mtkleinf3723212014-06-25 14:08:00 -0700347}
348
bsalomonc2553372014-07-22 13:09:05 -0700349// If bench is enabled for config, returns a Target* for it, otherwise NULL.
350static Target* is_enabled(Benchmark* bench, const Config& config) {
351 if (!bench->isSuitableFor(config.backend)) {
352 return NULL;
353 }
354
reede5ea5002014-09-03 11:54:58 -0700355 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
356 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700357
358 Target* target = new Target(config);
359
360 if (Benchmark::kRaster_Backend == config.backend) {
361 target->surface.reset(SkSurface::NewRaster(info));
362 }
363#if SK_SUPPORT_GPU
364 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700365 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700366 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700367 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700368 }
369#endif
370
371 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
372 delete target;
373 return NULL;
374 }
375 return target;
376}
377
378// Creates targets for a benchmark and a set of configs.
379static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
380 const SkTDArray<Config>& configs) {
381 for (int i = 0; i < configs.count(); ++i) {
382 if (Target* t = is_enabled(b, configs[i])) {
383 targets->push(t);
384 }
mtkleine714e752014-07-31 12:13:48 -0700385
bsalomonc2553372014-07-22 13:09:05 -0700386 }
387}
388
jcgregoriobf5e5232014-07-17 13:14:16 -0700389#if SK_SUPPORT_GPU
390static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700391 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700392 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
393 log->configOption("GL_VERSION", (const char*)(version));
394
395 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
396 log->configOption("GL_RENDERER", (const char*) version);
397
398 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
399 log->configOption("GL_VENDOR", (const char*) version);
400
401 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
402 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
403}
404#endif
405
mtkleine714e752014-07-31 12:13:48 -0700406class BenchmarkStream {
407public:
mtklein92007582014-08-01 07:46:52 -0700408 BenchmarkStream() : fBenches(BenchRegistry::Head())
409 , fGMs(skiagm::GMRegistry::Head())
410 , fCurrentScale(0)
411 , fCurrentSKP(0) {
412 for (int i = 0; i < FLAGS_skps.count(); i++) {
413 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
414 fSKPs.push_back() = FLAGS_skps[i];
415 } else {
416 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
417 SkString path;
418 while (it.next(&path)) {
419 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
420 }
421 }
422 }
mtkleine714e752014-07-31 12:13:48 -0700423
mtklein92007582014-08-01 07:46:52 -0700424 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
425 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
426 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
427 exit(1);
428 }
429
430 for (int i = 0; i < FLAGS_scales.count(); i++) {
431 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
432 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
433 exit(1);
434 }
435 }
436 }
437
438 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700439 if (fBenches) {
440 Benchmark* bench = fBenches->factory()(NULL);
441 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700442 fSourceType = "bench";
mtkleine714e752014-07-31 12:13:48 -0700443 return bench;
444 }
mtklein92007582014-08-01 07:46:52 -0700445
mtkleine714e752014-07-31 12:13:48 -0700446 while (fGMs) {
447 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
448 fGMs = fGMs->next();
449 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700450 fSourceType = "gm";
mtkleine714e752014-07-31 12:13:48 -0700451 return SkNEW_ARGS(GMBench, (gm.detach()));
452 }
453 }
mtklein92007582014-08-01 07:46:52 -0700454
455 while (fCurrentScale < fScales.count()) {
456 while (fCurrentSKP < fSKPs.count()) {
457 const SkString& path = fSKPs[fCurrentSKP++];
458
459 // Not strictly necessary, as it will be checked again later,
460 // but helps to avoid a lot of pointless work if we're going to skip it.
461 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
462 continue;
463 }
464
465 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
466 if (stream.get() == NULL) {
467 SkDebugf("Could not read %s.\n", path.c_str());
468 exit(1);
469 }
470
471 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
472 if (pic.get() == NULL) {
473 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
474 exit(1);
475 }
476
477 SkString name = SkOSPath::Basename(path.c_str());
478
mtklein20840502014-08-21 15:51:22 -0700479 if (FLAGS_bbh) {
480 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
481 // Here we use an SkTileGrid with parameters optimized for FLAGS_clip.
482 const SkTileGridFactory::TileGridInfo info = {
483 SkISize::Make(fClip.width(), fClip.height()), // tile interval
484 SkISize::Make(0,0), // margin
485 SkIPoint::Make(fClip.left(), fClip.top()), // offset
486 };
487 SkTileGridFactory factory(info);
488 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700489 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtkleinea65bfa2014-09-09 07:59:46 -0700490 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700491 &factory));
mtklein20840502014-08-21 15:51:22 -0700492 pic.reset(recorder.endRecording());
493 }
494
mtklein92007582014-08-01 07:46:52 -0700495 fSourceType = "skp";
496 return SkNEW_ARGS(SKPBench,
497 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
498 }
499 fCurrentSKP = 0;
500 fCurrentScale++;
501 }
502
mtkleine714e752014-07-31 12:13:48 -0700503 return NULL;
504 }
mtklein92007582014-08-01 07:46:52 -0700505
506 void fillCurrentOptions(ResultsWriter* log) const {
507 log->configOption("source_type", fSourceType);
508 if (0 == strcmp(fSourceType, "skp")) {
509 log->configOption("clip",
510 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
511 fClip.fRight, fClip.fBottom).c_str());
512 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
513 }
514 }
515
mtkleine714e752014-07-31 12:13:48 -0700516private:
517 const BenchRegistry* fBenches;
518 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700519 SkIRect fClip;
520 SkTArray<SkScalar> fScales;
521 SkTArray<SkString> fSKPs;
522
523 const char* fSourceType;
524 int fCurrentScale;
525 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700526};
527
caryclark17f0b6d2014-07-22 10:15:34 -0700528int nanobench_main();
529int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700530 SetupCrashHandler();
531 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700532
krajcevski69a55602014-08-13 10:46:31 -0700533#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700534 GrContext::Options grContextOpts;
535 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
536 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700537#endif
538
bsalomon6eb03cc2014-08-07 14:28:50 -0700539 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700540 FLAGS_samples = 1;
541 FLAGS_gpuFrameLag = 0;
542 }
543
bsalomon6eb03cc2014-08-07 14:28:50 -0700544 if (!FLAGS_writePath.isEmpty()) {
545 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
546 if (!sk_mkdir(FLAGS_writePath[0])) {
547 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
548 FLAGS_writePath.set(0, NULL);
549 }
550 }
551
mtklein1915b622014-08-20 11:45:00 -0700552 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700553 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700554 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700555 }
mtklein1915b622014-08-20 11:45:00 -0700556
557 if (1 == FLAGS_properties.count() % 2) {
558 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
559 return 1;
560 }
561 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
562 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
563 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700564
565 if (1 == FLAGS_key.count() % 2) {
566 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
567 return 1;
568 }
569 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700570 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700571 }
mtklein60317d0f2014-07-14 11:30:37 -0700572
mtkleinf3723212014-06-25 14:08:00 -0700573 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700574 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400575
mtkleinbb6a0282014-07-01 08:43:42 -0700576 SkAutoTMalloc<double> samples(FLAGS_samples);
577
bsalomon6eb03cc2014-08-07 14:28:50 -0700578 if (kAutoTuneLoops != FLAGS_loops) {
579 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700580 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700581 // No header.
582 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700583 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700584 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700585 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
586 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700587 }
588
bsalomonc2553372014-07-22 13:09:05 -0700589 SkTDArray<Config> configs;
590 create_configs(&configs);
591
mtklein92007582014-08-01 07:46:52 -0700592 BenchmarkStream benchStream;
593 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700594 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700595 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
596 continue;
597 }
598
mtkleinbb6a0282014-07-01 08:43:42 -0700599 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700600 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700601
jcgregoriobf5e5232014-07-17 13:14:16 -0700602 if (!targets.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700603 log->bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700604 bench->preDraw();
605 }
mtkleinbb6a0282014-07-01 08:43:42 -0700606 for (int j = 0; j < targets.count(); j++) {
607 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700608 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700609
mtkleinbb6a0282014-07-01 08:43:42 -0700610 const int loops =
611#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700612 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700613 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
614 :
615#endif
616 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700617
bsalomon49f085d2014-09-05 13:34:00 -0700618 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700619 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
620 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
621 pngFilename.append(".png");
622 write_canvas_png(canvas, pngFilename);
623 }
624
625 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700626 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400627 continue;
628 }
629
mtkleinf3723212014-06-25 14:08:00 -0700630 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700631 log->config(config);
632 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700633#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700634 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700635 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700636 }
637#endif
mtklein1915b622014-08-20 11:45:00 -0700638 log->timer("min_ms", stats.min);
639 log->timer("median_ms", stats.median);
640 log->timer("mean_ms", stats.mean);
641 log->timer("max_ms", stats.max);
642 log->timer("stddev_ms", sqrt(stats.var));
mtklein60317d0f2014-07-14 11:30:37 -0700643
bsalomon6eb03cc2014-08-07 14:28:50 -0700644 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700645 if (targets.count() == 1) {
646 config = ""; // Only print the config if we run the same bench on more than one.
647 }
648 SkDebugf("%s\t%s\n", bench->getName(), config);
649 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700650 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700651 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700652 }
653 SkDebugf("%s\n", bench->getName());
654 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700655 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700656 config = ""; // Only print the config if we run the same bench on more than one.
657 }
mtklein55b0ffc2014-07-17 08:38:23 -0700658 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700659 } else {
660 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700661 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
662 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700663 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700664 , HUMANIZE(stats.min)
665 , HUMANIZE(stats.median)
666 , HUMANIZE(stats.mean)
667 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700668 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700669 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700670 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700671 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700672 );
673 }
674 }
mtkleinbb6a0282014-07-01 08:43:42 -0700675 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400676
677 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700678 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700679 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700680 }
681 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700682 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400683 }
684 #endif
mtkleinf3723212014-06-25 14:08:00 -0700685 }
686
687 return 0;
688}
689
690#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700691int main(int argc, char** argv) {
692 SkCommandLineFlags::Parse(argc, argv);
693 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700694}
695#endif