blob: 5e059328adae5e9fae805da2074a035debac9a2a [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"
msarett95f192d2015-02-13 09:05:41 -080012#include "DecodingBench.h"
13#include "DecodingSubsetBench.h"
mtkleine714e752014-07-31 12:13:48 -070014#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070015#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070016#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070017#include "RecordingBench.h"
mtklein92007582014-08-01 07:46:52 -070018#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070019#include "Stats.h"
20#include "Timer.h"
21
mtklein6838d852014-10-29 14:15:10 -070022#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070023#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070024#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080025#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "SkForceLinking.h"
27#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070028#include "SkOSFile.h"
29#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080030#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070031#include "SkString.h"
32#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080033#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070034
mtkleinbb6a0282014-07-01 08:43:42 -070035#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070036 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070037 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070038 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070039#endif
40
mtkleinf3723212014-06-25 14:08:00 -070041__SK_FORCE_IMAGE_DECODER_LINKING;
42
reed53249782014-10-10 09:09:52 -070043static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070044
mtkleinb5110422014-08-07 15:20:02 -070045static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070046#ifdef SK_DEBUG
47 1;
mtkleina189ccd2014-07-14 12:28:47 -070048#else
bsalomon6eb03cc2014-08-07 14:28:50 -070049 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070050#endif
51
bsalomon6eb03cc2014-08-07 14:28:50 -070052static SkString loops_help_txt() {
53 SkString help;
54 help.printf("Number of times to run each bench. Set this to %d to auto-"
55 "tune for each bench. Timings are only reported when auto-tuning.",
56 kAutoTuneLoops);
57 return help;
58}
59
60DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
61
mtkleinf3723212014-06-25 14:08:00 -070062DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
63DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
64DEFINE_double(overheadGoal, 0.0001,
65 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070066DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
67DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070068DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
69 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070070
mtklein60317d0f2014-07-14 11:30:37 -070071DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070072DEFINE_int32(maxCalibrationAttempts, 3,
73 "Try up to this many times to guess loops for a bench, or skip the bench.");
74DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070075DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
76DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070077DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080078DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070079DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080080DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080081DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070082
mtkleinf3723212014-06-25 14:08:00 -070083static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070084 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -080085 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -070086}
mtklein55b0ffc2014-07-17 08:38:23 -070087#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070088
kkinnunen9e61bb72014-10-09 05:24:15 -070089static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContext* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070090 if (canvas) {
91 canvas->clear(SK_ColorWHITE);
92 }
mtkleinbb6a0282014-07-01 08:43:42 -070093 WallTimer timer;
94 timer.start();
95 if (bench) {
96 bench->draw(loops, canvas);
97 }
98 if (canvas) {
99 canvas->flush();
100 }
101#if SK_SUPPORT_GPU
102 if (gl) {
103 SK_GL(*gl, Flush());
104 gl->swapBuffers();
105 }
106#endif
107 timer.end();
108 return timer.fWall;
109}
110
mtkleinf3723212014-06-25 14:08:00 -0700111static double estimate_timer_overhead() {
112 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700113 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700114 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700115 }
116 return overhead / FLAGS_overheadLoops;
117}
118
reed53249782014-10-10 09:09:52 -0700119static int detect_forever_loops(int loops) {
120 // look for a magic run-forever value
121 if (loops < 0) {
122 loops = SK_MaxS32;
123 }
124 return loops;
125}
126
mtklein55b0ffc2014-07-17 08:38:23 -0700127static int clamp_loops(int loops) {
128 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800129 SkDebugf("ERROR: clamping loops from %d to 1. "
130 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700131 return 1;
132 }
133 if (loops > FLAGS_maxLoops) {
134 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
135 return FLAGS_maxLoops;
136 }
137 return loops;
138}
139
bsalomon6eb03cc2014-08-07 14:28:50 -0700140static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
141 if (filename.isEmpty()) {
142 return false;
143 }
reede5ea5002014-09-03 11:54:58 -0700144 if (kUnknown_SkColorType == canvas->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700145 return false;
146 }
147 SkBitmap bmp;
148 bmp.setInfo(canvas->imageInfo());
149 if (!canvas->readPixels(&bmp, 0, 0)) {
150 SkDebugf("Can't read canvas pixels.\n");
151 return false;
152 }
153 SkString dir = SkOSPath::Dirname(filename.c_str());
154 if (!sk_mkdir(dir.c_str())) {
155 SkDebugf("Can't make dir %s.\n", dir.c_str());
156 return false;
157 }
158 SkFILEWStream stream(filename.c_str());
159 if (!stream.isValid()) {
160 SkDebugf("Can't write %s.\n", filename.c_str());
161 return false;
162 }
163 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
164 SkDebugf("Can't encode a PNG.\n");
165 return false;
166 }
167 return true;
168}
169
170static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700171static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
172 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700173 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700174 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700175 if (kAutoTuneLoops == FLAGS_loops) {
176 while (bench_plus_overhead < overhead) {
177 if (round++ == FLAGS_maxCalibrationAttempts) {
178 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700179 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700180 return kFailedLoops;
181 }
182 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700183 }
mtklein2069e222014-08-04 13:57:39 -0700184 }
mtkleinf3723212014-06-25 14:08:00 -0700185
mtkleinbb6a0282014-07-01 08:43:42 -0700186 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700187 // We'll pick N to make timer overhead negligible:
188 //
mtkleinbb6a0282014-07-01 08:43:42 -0700189 // overhead
190 // ------------------------- < FLAGS_overheadGoal
191 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700192 //
mtkleinbb6a0282014-07-01 08:43:42 -0700193 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700194 //
195 // Doing some math, we get:
196 //
mtkleinbb6a0282014-07-01 08:43:42 -0700197 // (overhead / FLAGS_overheadGoal) - overhead
198 // ------------------------------------------ < N
199 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700200 //
201 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700202 int loops = FLAGS_loops;
203 if (kAutoTuneLoops == loops) {
204 const double numer = overhead / FLAGS_overheadGoal - overhead;
205 const double denom = bench_plus_overhead - overhead;
206 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700207 loops = clamp_loops(loops);
208 } else {
209 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700210 }
mtkleinbb6a0282014-07-01 08:43:42 -0700211
212 for (int i = 0; i < FLAGS_samples; i++) {
213 samples[i] = time(loops, bench, canvas, NULL) / loops;
214 }
215 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700216}
217
mtkleinbb6a0282014-07-01 08:43:42 -0700218#if SK_SUPPORT_GPU
robertphillips5b693772014-11-21 06:19:36 -0800219static void setup_gl(SkGLContext* gl) {
220 gl->makeCurrent();
221 // Make sure we're done with whatever came before.
222 SK_GL(*gl, Finish());
223}
224
kkinnunen9e61bb72014-10-09 05:24:15 -0700225static int gpu_bench(SkGLContext* gl,
mtkleinbb6a0282014-07-01 08:43:42 -0700226 Benchmark* bench,
227 SkCanvas* canvas,
228 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700229 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700230 int loops = FLAGS_loops;
231 if (kAutoTuneLoops == loops) {
232 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700233 double elapsed = 0;
234 do {
mtklein527930f2014-11-06 08:04:34 -0800235 if (1<<30 == loops) {
236 // We're about to wrap. Something's wrong with the bench.
237 loops = 0;
238 break;
239 }
mtkleina189ccd2014-07-14 12:28:47 -0700240 loops *= 2;
241 // If the GPU lets frames lag at all, we need to make sure we're timing
242 // _this_ round, not still timing last round. We force this by looping
243 // more times than any reasonable GPU will allow frames to lag.
244 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
245 elapsed = time(loops, bench, canvas, gl);
246 }
247 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700248
mtkleina189ccd2014-07-14 12:28:47 -0700249 // We've overshot at least a little. Scale back linearly.
250 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700251 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700252
mtkleina189ccd2014-07-14 12:28:47 -0700253 // Might as well make sure we're not still timing our calibration.
254 SK_GL(*gl, Finish());
reed53249782014-10-10 09:09:52 -0700255 } else {
256 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700257 }
mtkleinbb6a0282014-07-01 08:43:42 -0700258
259 // Pretty much the same deal as the calibration: do some warmup to make
260 // sure we're timing steady-state pipelined frames.
261 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
262 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700263 }
mtkleinbb6a0282014-07-01 08:43:42 -0700264
265 // Now, actually do the timing!
266 for (int i = 0; i < FLAGS_samples; i++) {
267 samples[i] = time(loops, bench, canvas, gl) / loops;
268 }
269 return loops;
270}
271#endif
272
273static SkString to_lower(const char* str) {
274 SkString lower(str);
275 for (size_t i = 0; i < lower.size(); i++) {
276 lower[i] = tolower(lower[i]);
277 }
278 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700279}
280
bsalomonc2553372014-07-22 13:09:05 -0700281struct Config {
282 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700283 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700284 SkColorType color;
285 SkAlphaType alpha;
286 int samples;
287#if SK_SUPPORT_GPU
288 GrContextFactory::GLContextType ctxType;
jvanverth4736e142014-11-07 07:12:46 -0800289 bool useDFText;
bsalomonc2553372014-07-22 13:09:05 -0700290#else
291 int bogusInt;
jvanverth4736e142014-11-07 07:12:46 -0800292 bool bogusBool;
bsalomonc2553372014-07-22 13:09:05 -0700293#endif
294};
295
296struct Target {
297 explicit Target(const Config& c) : config(c) {}
298 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700299 SkAutoTDelete<SkSurface> surface;
300#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700301 SkGLContext* gl;
mtkleinbb6a0282014-07-01 08:43:42 -0700302#endif
303};
mtkleinf3723212014-06-25 14:08:00 -0700304
bsalomonc2553372014-07-22 13:09:05 -0700305static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700306 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700307 if (to_lower(FLAGS_config[i]).equals(name)) {
308 return true;
mtkleinf3723212014-06-25 14:08:00 -0700309 }
310 }
bsalomonc2553372014-07-22 13:09:05 -0700311 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700312}
313
bsalomonc2553372014-07-22 13:09:05 -0700314#if SK_SUPPORT_GPU
315static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
316 int sampleCnt) {
317 if (!is_cpu_config_allowed(name)) {
318 return false;
319 }
krajcevski69a55602014-08-13 10:46:31 -0700320 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700321 return sampleCnt <= ctx->getMaxSampleCount();
322 }
323 return false;
324}
325#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700326
bsalomonc2553372014-07-22 13:09:05 -0700327#if SK_SUPPORT_GPU
328#define kBogusGLContextType GrContextFactory::kNative_GLContextType
329#else
330#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700331#endif
bsalomonc2553372014-07-22 13:09:05 -0700332
333// Append all configs that are enabled and supported.
334static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800335 #define CPU_CONFIG(name, backend, color, alpha) \
336 if (is_cpu_config_allowed(#name)) { \
337 Config config = { #name, Benchmark::backend, color, alpha, 0, \
338 kBogusGLContextType, false }; \
339 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700340 }
mtkleine714e752014-07-31 12:13:48 -0700341
mtklein40b32be2014-07-09 08:46:49 -0700342 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700343 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
344 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
345 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700346 }
mtkleinbb6a0282014-07-01 08:43:42 -0700347
348#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800349 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700350 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
351 Config config = { \
352 #name, \
353 Benchmark::kGPU_Backend, \
354 kN32_SkColorType, \
355 kPremul_SkAlphaType, \
356 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800357 GrContextFactory::ctxType, \
358 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700359 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700360 }
mtkleine714e752014-07-31 12:13:48 -0700361
mtklein40b32be2014-07-09 08:46:49 -0700362 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800363 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
364 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
365 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
366 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
367 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
368 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
369 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
370 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700371#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800372 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700373#endif
mtklein40b32be2014-07-09 08:46:49 -0700374 }
mtkleinbb6a0282014-07-01 08:43:42 -0700375#endif
mtkleinf3723212014-06-25 14:08:00 -0700376}
377
bsalomonc2553372014-07-22 13:09:05 -0700378// If bench is enabled for config, returns a Target* for it, otherwise NULL.
379static Target* is_enabled(Benchmark* bench, const Config& config) {
380 if (!bench->isSuitableFor(config.backend)) {
381 return NULL;
382 }
383
reede5ea5002014-09-03 11:54:58 -0700384 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
385 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700386
387 Target* target = new Target(config);
388
389 if (Benchmark::kRaster_Backend == config.backend) {
390 target->surface.reset(SkSurface::NewRaster(info));
391 }
392#if SK_SUPPORT_GPU
393 else if (Benchmark::kGPU_Backend == config.backend) {
jvanverth4736e142014-11-07 07:12:46 -0800394 uint32_t flags = config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
395 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
bsalomonafe30052015-01-16 07:32:33 -0800396 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType),
397 SkSurface::kNo_Budgeted, info,
jvanverth4736e142014-11-07 07:12:46 -0800398 config.samples, &props));
krajcevski69a55602014-08-13 10:46:31 -0700399 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700400 }
401#endif
402
403 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
404 delete target;
405 return NULL;
406 }
407 return target;
408}
409
410// Creates targets for a benchmark and a set of configs.
411static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
412 const SkTDArray<Config>& configs) {
413 for (int i = 0; i < configs.count(); ++i) {
414 if (Target* t = is_enabled(b, configs[i])) {
415 targets->push(t);
416 }
mtkleine714e752014-07-31 12:13:48 -0700417
bsalomonc2553372014-07-22 13:09:05 -0700418 }
419}
420
jcgregoriobf5e5232014-07-17 13:14:16 -0700421#if SK_SUPPORT_GPU
kkinnunen9e61bb72014-10-09 05:24:15 -0700422static void fill_gpu_options(ResultsWriter* log, SkGLContext* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700423 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700424 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
425 log->configOption("GL_VERSION", (const char*)(version));
426
427 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
428 log->configOption("GL_RENDERER", (const char*) version);
429
430 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
431 log->configOption("GL_VENDOR", (const char*) version);
432
433 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
434 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
435}
436#endif
437
mtkleine714e752014-07-31 12:13:48 -0700438class BenchmarkStream {
439public:
mtklein92007582014-08-01 07:46:52 -0700440 BenchmarkStream() : fBenches(BenchRegistry::Head())
441 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700442 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700443 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800444 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800445 , fCurrentUseMPD(0)
446 , fCurrentImage(0)
447 , fCurrentSubsetImage(0)
448 , fCurrentColorType(0)
449 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700450 for (int i = 0; i < FLAGS_skps.count(); i++) {
451 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
452 fSKPs.push_back() = FLAGS_skps[i];
453 } else {
454 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
455 SkString path;
456 while (it.next(&path)) {
457 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
458 }
459 }
460 }
mtkleine714e752014-07-31 12:13:48 -0700461
mtklein92007582014-08-01 07:46:52 -0700462 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
463 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
464 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
465 exit(1);
466 }
467
468 for (int i = 0; i < FLAGS_scales.count(); i++) {
469 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
470 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
471 exit(1);
472 }
473 }
robertphillips5b693772014-11-21 06:19:36 -0800474
475 fUseMPDs.push_back() = false;
476 if (FLAGS_mpd) {
477 fUseMPDs.push_back() = true;
478 }
mtklein95553d92015-03-12 08:24:21 -0700479
msarett95f192d2015-02-13 09:05:41 -0800480 // Prepare the images for decoding
481 for (int i = 0; i < FLAGS_images.count(); i++) {
482 const char* flag = FLAGS_images[i];
483 if (sk_isdir(flag)) {
484 // If the value passed in is a directory, add all the images
485 SkOSFile::Iter it(flag);
486 SkString file;
487 while (it.next(&file)) {
488 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
489 }
490 } else if (sk_exists(flag)) {
491 // Also add the value if it is a single image
492 fImages.push_back() = flag;
493 }
494 }
mtklein95553d92015-03-12 08:24:21 -0700495
msarett95f192d2015-02-13 09:05:41 -0800496 // Choose the candidate color types for image decoding
497 const SkColorType colorTypes[] =
498 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType };
499 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700500 }
501
mtkleinfd731ce2014-09-10 12:19:30 -0700502 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
503 // Not strictly necessary, as it will be checked again later,
504 // but helps to avoid a lot of pointless work if we're going to skip it.
505 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
506 return false;
507 }
508
scroggoa1193e42015-01-21 12:09:53 -0800509 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700510 if (stream.get() == NULL) {
511 SkDebugf("Could not read %s.\n", path);
512 return false;
513 }
514
mtklein57f27bd2015-02-09 11:58:41 -0800515 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700516 if (pic->get() == NULL) {
517 SkDebugf("Could not read %s as an SkPicture.\n", path);
518 return false;
519 }
520 return true;
521 }
522
mtklein92007582014-08-01 07:46:52 -0700523 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700524 if (fBenches) {
525 Benchmark* bench = fBenches->factory()(NULL);
526 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700527 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700528 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700529 return bench;
530 }
mtklein92007582014-08-01 07:46:52 -0700531
mtkleine714e752014-07-31 12:13:48 -0700532 while (fGMs) {
533 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
534 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800535 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700536 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700537 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700538 return SkNEW_ARGS(GMBench, (gm.detach()));
539 }
540 }
mtklein92007582014-08-01 07:46:52 -0700541
mtkleinfd731ce2014-09-10 12:19:30 -0700542 // First add all .skps as RecordingBenches.
543 while (fCurrentRecording < fSKPs.count()) {
544 const SkString& path = fSKPs[fCurrentRecording++];
545 SkAutoTUnref<SkPicture> pic;
546 if (!ReadPicture(path.c_str(), &pic)) {
547 continue;
548 }
549 SkString name = SkOSPath::Basename(path.c_str());
550 fSourceType = "skp";
551 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800552 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800553 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700554 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
555 }
556
557 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700558 while (fCurrentScale < fScales.count()) {
559 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800560 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700561 SkAutoTUnref<SkPicture> pic;
562 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800563 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700564 continue;
565 }
robertphillips5b693772014-11-21 06:19:36 -0800566
567 while (fCurrentUseMPD < fUseMPDs.count()) {
568 if (FLAGS_bbh) {
569 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
570 SkRTreeFactory factory;
571 SkPictureRecorder recorder;
572 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
573 pic->playback(recorder.beginRecording(pic->cullRect().width(),
574 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800575 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800576 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800577 pic.reset(recorder.endRecording());
578 }
579 SkString name = SkOSPath::Basename(path.c_str());
580 fSourceType = "skp";
581 fBenchType = "playback";
582 return SkNEW_ARGS(SKPBench,
583 (name.c_str(), pic.get(), fClip,
584 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
mtklein20840502014-08-21 15:51:22 -0700585 }
robertphillips5b693772014-11-21 06:19:36 -0800586 fCurrentUseMPD = 0;
587 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700588 }
589 fCurrentSKP = 0;
590 fCurrentScale++;
591 }
592
msarett95f192d2015-02-13 09:05:41 -0800593 // Run the DecodingBenches
594 while (fCurrentImage < fImages.count()) {
595 while (fCurrentColorType < fColorTypes.count()) {
596 const SkString& path = fImages[fCurrentImage];
597 SkColorType colorType = fColorTypes[fCurrentColorType];
598 fCurrentColorType++;
599 // Check if the image decodes before creating the benchmark
600 SkBitmap bitmap;
601 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
602 colorType, SkImageDecoder::kDecodePixels_Mode)) {
603 return new DecodingBench(path, colorType);
604 }
605 }
606 fCurrentColorType = 0;
607 fCurrentImage++;
608 }
609
610 // Run the DecodingSubsetBenches
611 while (fCurrentSubsetImage < fImages.count()) {
612 while (fCurrentColorType < fColorTypes.count()) {
613 const SkString& path = fImages[fCurrentSubsetImage];
614 SkColorType colorType = fColorTypes[fCurrentColorType];
615 fCurrentColorType++;
616 // Check if the image decodes before creating the benchmark
617 SkAutoTUnref<SkData> encoded(
618 SkData::NewFromFileName(path.c_str()));
619 SkAutoTDelete<SkMemoryStream> stream(
620 new SkMemoryStream(encoded));
621 SkAutoTDelete<SkImageDecoder>
622 decoder(SkImageDecoder::Factory(stream.get()));
623 if (!decoder) {
624 SkDebugf("Cannot find decoder for %s\n", path.c_str());
625 } else {
626 stream->rewind();
627 int w, h;
628 bool success;
629 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
630 || w*h == 1) {
631 // This is not an error, but in this case we still
632 // do not want to run the benchmark.
633 success = false;
634 } else if (fDivisor > w || fDivisor > h) {
635 SkDebugf("Divisor %d is too big for %s %dx%d\n",
636 fDivisor, path.c_str(), w, h);
637 success = false;
638 } else {
639 const int sW = w / fDivisor;
640 const int sH = h / fDivisor;
641 SkBitmap bitmap;
642 success = true;
643 for (int y = 0; y < h; y += sH) {
644 for (int x = 0; x < w; x += sW) {
645 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
646 success &= decoder->decodeSubset(&bitmap, rect,
647 colorType);
648 }
649 }
650 }
651 // Create the benchmark if successful
652 if (success) {
653 return new DecodingSubsetBench(path, colorType,
654 fDivisor);
655 }
656 }
657 }
658 fCurrentColorType = 0;
659 fCurrentSubsetImage++;
660 }
661
mtkleine714e752014-07-31 12:13:48 -0700662 return NULL;
663 }
mtklein92007582014-08-01 07:46:52 -0700664
665 void fillCurrentOptions(ResultsWriter* log) const {
666 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700667 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700668 if (0 == strcmp(fSourceType, "skp")) {
669 log->configOption("clip",
670 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
671 fClip.fRight, fClip.fBottom).c_str());
672 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800673 if (fCurrentUseMPD > 0) {
674 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
675 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
676 }
mtklein92007582014-08-01 07:46:52 -0700677 }
mtklein051e56d2014-12-04 08:46:51 -0800678 if (0 == strcmp(fBenchType, "recording")) {
679 log->metric("bytes", fSKPBytes);
680 log->metric("ops", fSKPOps);
681 }
mtklein92007582014-08-01 07:46:52 -0700682 }
683
mtkleine714e752014-07-31 12:13:48 -0700684private:
685 const BenchRegistry* fBenches;
686 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700687 SkIRect fClip;
688 SkTArray<SkScalar> fScales;
689 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800690 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800691 SkTArray<SkString> fImages;
692 SkTArray<SkColorType> fColorTypes;
mtklein92007582014-08-01 07:46:52 -0700693
mtklein051e56d2014-12-04 08:46:51 -0800694 double fSKPBytes, fSKPOps;
695
mtkleinfd731ce2014-09-10 12:19:30 -0700696 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
697 const char* fBenchType; // How we bench it: micro, recording, playback, ...
698 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700699 int fCurrentScale;
700 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800701 int fCurrentUseMPD;
msarett95f192d2015-02-13 09:05:41 -0800702 int fCurrentImage;
703 int fCurrentSubsetImage;
704 int fCurrentColorType;
705 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700706};
707
jcgregorio3b27ade2014-11-13 08:06:40 -0800708int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700709int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800710 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700711 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800712 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700713
krajcevski69a55602014-08-13 10:46:31 -0700714#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700715 GrContext::Options grContextOpts;
716 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
717 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700718#endif
719
bsalomon06cddec2014-10-24 10:40:50 -0700720 if (FLAGS_veryVerbose) {
721 FLAGS_verbose = true;
722 }
723
bsalomon6eb03cc2014-08-07 14:28:50 -0700724 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700725 FLAGS_samples = 1;
726 FLAGS_gpuFrameLag = 0;
727 }
728
bsalomon6eb03cc2014-08-07 14:28:50 -0700729 if (!FLAGS_writePath.isEmpty()) {
730 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
731 if (!sk_mkdir(FLAGS_writePath[0])) {
732 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
733 FLAGS_writePath.set(0, NULL);
734 }
735 }
736
mtklein1915b622014-08-20 11:45:00 -0700737 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700738 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700739 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700740 }
mtklein1915b622014-08-20 11:45:00 -0700741
742 if (1 == FLAGS_properties.count() % 2) {
743 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
744 return 1;
745 }
746 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
747 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
748 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700749
750 if (1 == FLAGS_key.count() % 2) {
751 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
752 return 1;
753 }
754 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700755 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700756 }
mtklein60317d0f2014-07-14 11:30:37 -0700757
mtkleinf3723212014-06-25 14:08:00 -0700758 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700759 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400760
mtkleinbb6a0282014-07-01 08:43:42 -0700761 SkAutoTMalloc<double> samples(FLAGS_samples);
762
bsalomon6eb03cc2014-08-07 14:28:50 -0700763 if (kAutoTuneLoops != FLAGS_loops) {
764 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700765 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700766 // No header.
767 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700768 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700769 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700770 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
771 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700772 }
773
bsalomonc2553372014-07-22 13:09:05 -0700774 SkTDArray<Config> configs;
775 create_configs(&configs);
776
mtkleine070c2b2014-10-14 08:40:43 -0700777 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700778 BenchmarkStream benchStream;
779 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700780 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700781 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700782 continue;
783 }
784
mtkleinbb6a0282014-07-01 08:43:42 -0700785 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700786 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700787
jcgregoriobf5e5232014-07-17 13:14:16 -0700788 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700789 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700790 bench->preDraw();
791 }
mtkleinbb6a0282014-07-01 08:43:42 -0700792 for (int j = 0; j < targets.count(); j++) {
793 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700794 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700795
robertphillips5b693772014-11-21 06:19:36 -0800796#if SK_SUPPORT_GPU
797 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
798 setup_gl(targets[j]->gl);
799 }
800#endif
801
802 bench->perCanvasPreDraw(canvas);
803
mtkleinbb6a0282014-07-01 08:43:42 -0700804 const int loops =
805#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700806 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700807 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
808 :
809#endif
810 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700811
robertphillips5b693772014-11-21 06:19:36 -0800812 bench->perCanvasPostDraw(canvas);
813
bsalomon49f085d2014-09-05 13:34:00 -0700814 if (canvas && !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700815 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700816 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700817 pngFilename.append(".png");
818 write_canvas_png(canvas, pngFilename);
819 }
820
821 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700822 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400823 continue;
824 }
825
mtkleinf3723212014-06-25 14:08:00 -0700826 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700827 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700828 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700829 benchStream.fillCurrentOptions(log.get());
jcgregoriobf5e5232014-07-17 13:14:16 -0700830#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700831 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein1915b622014-08-20 11:45:00 -0700832 fill_gpu_options(log.get(), targets[j]->gl);
jcgregoriobf5e5232014-07-17 13:14:16 -0700833 }
834#endif
mtklein051e56d2014-12-04 08:46:51 -0800835 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700836 if (runs++ % FLAGS_flushEvery == 0) {
837 log->flush();
838 }
mtklein60317d0f2014-07-14 11:30:37 -0700839
bsalomon6eb03cc2014-08-07 14:28:50 -0700840 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700841 if (targets.count() == 1) {
842 config = ""; // Only print the config if we run the same bench on more than one.
843 }
mtklein53d25622014-09-18 07:39:42 -0700844 SkDebugf("%4dM\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700845 , sk_tools::getBestResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700846 , bench->getUniqueName()
847 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700848 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700849 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700850 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700851 }
mtklein96289052014-09-10 12:05:59 -0700852 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700853 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700854 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700855 config = ""; // Only print the config if we run the same bench on more than one.
856 }
mtklein96289052014-09-10 12:05:59 -0700857 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700858 } else {
859 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700860 SkDebugf("%4dM\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700861 , sk_tools::getBestResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700862 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700863 , HUMANIZE(stats.min)
864 , HUMANIZE(stats.median)
865 , HUMANIZE(stats.mean)
866 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700867 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700868 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700869 , config
mtklein96289052014-09-10 12:05:59 -0700870 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700871 );
872 }
bsalomonb12ea412015-02-02 21:19:50 -0800873#if SK_SUPPORT_GPU
874 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -0700875 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700876 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -0800877 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -0700878 }
879#endif
mtkleinf3723212014-06-25 14:08:00 -0700880 }
mtkleinbb6a0282014-07-01 08:43:42 -0700881 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400882
bsalomon06cddec2014-10-24 10:40:50 -0700883#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700884 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700885 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700886 }
887 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700888 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400889 }
bsalomon06cddec2014-10-24 10:40:50 -0700890#endif
mtkleinf3723212014-06-25 14:08:00 -0700891 }
892
mtkleine1091452014-12-04 10:47:02 -0800893 log->bench("memory_usage", 0,0);
894 log->config("meta");
895 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
896
joshualitte0b19d42015-03-26 10:41:02 -0700897#if SK_SUPPORT_GPU
898 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
899 // SkEventTracer destructor
900 gGrFactory.reset(NULL);
901#endif
902
mtkleinf3723212014-06-25 14:08:00 -0700903 return 0;
904}
905
jcgregorio3b27ade2014-11-13 08:06:40 -0800906#if !defined SK_BUILD_FOR_IOS
907int main(int argc, char** argv) {
908 SkCommandLineFlags::Parse(argc, argv);
909 return nanobench_main();
910}
911#endif