blob: a627b50a3a221ed0523c18176711bf7bfaca9cca [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
tomhudsond968a6f2015-03-26 11:28:06 -070010#include "nanobench.h"
11
mtkleinf3723212014-06-25 14:08:00 -070012#include "Benchmark.h"
scroggo60869a42015-04-01 12:09:17 -070013#include "CodecBench.h"
mtkleinf3723212014-06-25 14:08:00 -070014#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080015#include "DecodingBench.h"
16#include "DecodingSubsetBench.h"
mtkleine714e752014-07-31 12:13:48 -070017#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070018#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070019#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070020#include "RecordingBench.h"
mtklein92007582014-08-01 07:46:52 -070021#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070022#include "Stats.h"
23#include "Timer.h"
24
mtklein6838d852014-10-29 14:15:10 -070025#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070026#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070027#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070028#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080029#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070030#include "SkForceLinking.h"
31#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070032#include "SkOSFile.h"
33#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080034#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070035#include "SkString.h"
36#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080037#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070038
tomhudsond968a6f2015-03-26 11:28:06 -070039#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
40 #include "nanobenchAndroid.h"
41#endif
42
mtkleinbb6a0282014-07-01 08:43:42 -070043#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070044 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070045 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070046 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070047#endif
48
mtkleinf3723212014-06-25 14:08:00 -070049__SK_FORCE_IMAGE_DECODER_LINKING;
50
reed53249782014-10-10 09:09:52 -070051static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070052
mtkleinb5110422014-08-07 15:20:02 -070053static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070054#ifdef SK_DEBUG
55 1;
mtkleina189ccd2014-07-14 12:28:47 -070056#else
bsalomon6eb03cc2014-08-07 14:28:50 -070057 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070058#endif
59
bsalomon6eb03cc2014-08-07 14:28:50 -070060static SkString loops_help_txt() {
61 SkString help;
62 help.printf("Number of times to run each bench. Set this to %d to auto-"
63 "tune for each bench. Timings are only reported when auto-tuning.",
64 kAutoTuneLoops);
65 return help;
66}
67
68DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
69
mtkleinf3723212014-06-25 14:08:00 -070070DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
71DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
72DEFINE_double(overheadGoal, 0.0001,
73 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070074DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
75DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070076DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
77 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070078
mtklein60317d0f2014-07-14 11:30:37 -070079DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070080DEFINE_int32(maxCalibrationAttempts, 3,
81 "Try up to this many times to guess loops for a bench, or skip the bench.");
82DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070083DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
84DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070085DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080086DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070087DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080088DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080089DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070090
mtkleinf3723212014-06-25 14:08:00 -070091static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070092 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -080093 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -070094}
mtklein55b0ffc2014-07-17 08:38:23 -070095#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070096
tomhudsond968a6f2015-03-26 11:28:06 -070097bool Target::init(SkImageInfo info, Benchmark* bench) {
98 if (Benchmark::kRaster_Backend == config.backend) {
99 this->surface.reset(SkSurface::NewRaster(info));
100 if (!this->surface.get()) {
101 return false;
102 }
103 }
104 return true;
105}
106bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700107 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700108 if (!canvas) {
109 return false;
110 }
111 bmp->setInfo(canvas->imageInfo());
112 if (!canvas->readPixels(bmp, 0, 0)) {
113 SkDebugf("Can't read canvas pixels.\n");
114 return false;
115 }
116 return true;
117}
118
119#if SK_SUPPORT_GPU
120struct GPUTarget : public Target {
121 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
122 SkGLContext* gl;
123
124 void setup() override {
125 this->gl->makeCurrent();
126 // Make sure we're done with whatever came before.
127 SK_GL(*this->gl, Finish());
128 }
129 void endTiming() override {
130 if (this->gl) {
131 SK_GL(*this->gl, Flush());
132 this->gl->swapBuffers();
133 }
134 }
135 void fence() override {
136 SK_GL(*this->gl, Finish());
137 }
138
139 bool needsFrameTiming() const override { return true; }
140 bool init(SkImageInfo info, Benchmark* bench) override {
141 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
142 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
143 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
144 SkSurface::kNo_Budgeted, info,
145 this->config.samples, &props));
146 this->gl = gGrFactory->getGLContext(this->config.ctxType);
147 if (!this->surface.get()) {
148 return false;
149 }
150 return true;
151 }
152 void fillOptions(ResultsWriter* log) override {
153 const GrGLubyte* version;
154 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
155 log->configOption("GL_VERSION", (const char*)(version));
156
157 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
158 log->configOption("GL_RENDERER", (const char*) version);
159
160 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
161 log->configOption("GL_VENDOR", (const char*) version);
162
163 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
164 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
165 }
166};
167
168#endif
169
tomhudson75a0ebb2015-03-27 12:11:44 -0700170static double time(int loops, Benchmark* bench, Target* target) {
171 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700172 if (canvas) {
173 canvas->clear(SK_ColorWHITE);
174 }
mtkleinbb6a0282014-07-01 08:43:42 -0700175 WallTimer timer;
176 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700177 canvas = target->beginTiming(canvas);
178 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700179 if (canvas) {
180 canvas->flush();
181 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700182 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700183 timer.end();
184 return timer.fWall;
185}
186
mtkleinf3723212014-06-25 14:08:00 -0700187static double estimate_timer_overhead() {
188 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700189 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700190 WallTimer timer;
191 timer.start();
192 timer.end();
193 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700194 }
195 return overhead / FLAGS_overheadLoops;
196}
197
reed53249782014-10-10 09:09:52 -0700198static int detect_forever_loops(int loops) {
199 // look for a magic run-forever value
200 if (loops < 0) {
201 loops = SK_MaxS32;
202 }
203 return loops;
204}
205
mtklein55b0ffc2014-07-17 08:38:23 -0700206static int clamp_loops(int loops) {
207 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800208 SkDebugf("ERROR: clamping loops from %d to 1. "
209 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700210 return 1;
211 }
212 if (loops > FLAGS_maxLoops) {
213 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
214 return FLAGS_maxLoops;
215 }
216 return loops;
217}
218
tomhudsond968a6f2015-03-26 11:28:06 -0700219static bool write_canvas_png(Target* target, const SkString& filename) {
220
bsalomon6eb03cc2014-08-07 14:28:50 -0700221 if (filename.isEmpty()) {
222 return false;
223 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700224 if (target->getCanvas() &&
225 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700226 return false;
227 }
tomhudsond968a6f2015-03-26 11:28:06 -0700228
bsalomon6eb03cc2014-08-07 14:28:50 -0700229 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700230
231 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700232 return false;
233 }
tomhudsond968a6f2015-03-26 11:28:06 -0700234
bsalomon6eb03cc2014-08-07 14:28:50 -0700235 SkString dir = SkOSPath::Dirname(filename.c_str());
236 if (!sk_mkdir(dir.c_str())) {
237 SkDebugf("Can't make dir %s.\n", dir.c_str());
238 return false;
239 }
240 SkFILEWStream stream(filename.c_str());
241 if (!stream.isValid()) {
242 SkDebugf("Can't write %s.\n", filename.c_str());
243 return false;
244 }
245 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
246 SkDebugf("Can't encode a PNG.\n");
247 return false;
248 }
249 return true;
250}
251
252static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700253static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700254 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700255 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700256 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700257 if (kAutoTuneLoops == FLAGS_loops) {
258 while (bench_plus_overhead < overhead) {
259 if (round++ == FLAGS_maxCalibrationAttempts) {
260 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700261 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700262 return kFailedLoops;
263 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700264 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700265 }
mtklein2069e222014-08-04 13:57:39 -0700266 }
mtkleinf3723212014-06-25 14:08:00 -0700267
mtkleinbb6a0282014-07-01 08:43:42 -0700268 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700269 // We'll pick N to make timer overhead negligible:
270 //
mtkleinbb6a0282014-07-01 08:43:42 -0700271 // overhead
272 // ------------------------- < FLAGS_overheadGoal
273 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700274 //
mtkleinbb6a0282014-07-01 08:43:42 -0700275 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700276 //
277 // Doing some math, we get:
278 //
mtkleinbb6a0282014-07-01 08:43:42 -0700279 // (overhead / FLAGS_overheadGoal) - overhead
280 // ------------------------------------------ < N
281 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700282 //
283 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700284 int loops = FLAGS_loops;
285 if (kAutoTuneLoops == loops) {
286 const double numer = overhead / FLAGS_overheadGoal - overhead;
287 const double denom = bench_plus_overhead - overhead;
288 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700289 loops = clamp_loops(loops);
290 } else {
291 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700292 }
mtkleinbb6a0282014-07-01 08:43:42 -0700293
294 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700295 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700296 }
297 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700298}
299
tomhudsond968a6f2015-03-26 11:28:06 -0700300static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700301 Benchmark* bench,
mtkleinbb6a0282014-07-01 08:43:42 -0700302 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700303 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700304 int loops = FLAGS_loops;
305 if (kAutoTuneLoops == loops) {
306 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700307 double elapsed = 0;
308 do {
mtklein527930f2014-11-06 08:04:34 -0800309 if (1<<30 == loops) {
310 // We're about to wrap. Something's wrong with the bench.
311 loops = 0;
312 break;
313 }
mtkleina189ccd2014-07-14 12:28:47 -0700314 loops *= 2;
315 // If the GPU lets frames lag at all, we need to make sure we're timing
316 // _this_ round, not still timing last round. We force this by looping
317 // more times than any reasonable GPU will allow frames to lag.
318 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700319 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700320 }
321 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700322
mtkleina189ccd2014-07-14 12:28:47 -0700323 // We've overshot at least a little. Scale back linearly.
324 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700325 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700326
tomhudsond968a6f2015-03-26 11:28:06 -0700327 // Make sure we're not still timing our calibration.
328 target->fence();
reed53249782014-10-10 09:09:52 -0700329 } else {
330 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700331 }
mtkleinbb6a0282014-07-01 08:43:42 -0700332
333 // Pretty much the same deal as the calibration: do some warmup to make
334 // sure we're timing steady-state pipelined frames.
335 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700336 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700337 }
mtkleinbb6a0282014-07-01 08:43:42 -0700338
339 // Now, actually do the timing!
340 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700341 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700342 }
tomhudsond968a6f2015-03-26 11:28:06 -0700343
mtkleinbb6a0282014-07-01 08:43:42 -0700344 return loops;
345}
mtkleinbb6a0282014-07-01 08:43:42 -0700346
347static SkString to_lower(const char* str) {
348 SkString lower(str);
349 for (size_t i = 0; i < lower.size(); i++) {
350 lower[i] = tolower(lower[i]);
351 }
352 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700353}
354
bsalomonc2553372014-07-22 13:09:05 -0700355static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700356 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700357 if (to_lower(FLAGS_config[i]).equals(name)) {
358 return true;
mtkleinf3723212014-06-25 14:08:00 -0700359 }
360 }
bsalomonc2553372014-07-22 13:09:05 -0700361 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700362}
363
bsalomonc2553372014-07-22 13:09:05 -0700364#if SK_SUPPORT_GPU
365static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
366 int sampleCnt) {
367 if (!is_cpu_config_allowed(name)) {
368 return false;
369 }
krajcevski69a55602014-08-13 10:46:31 -0700370 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700371 return sampleCnt <= ctx->getMaxSampleCount();
372 }
373 return false;
374}
375#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700376
bsalomonc2553372014-07-22 13:09:05 -0700377#if SK_SUPPORT_GPU
378#define kBogusGLContextType GrContextFactory::kNative_GLContextType
379#else
380#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700381#endif
bsalomonc2553372014-07-22 13:09:05 -0700382
383// Append all configs that are enabled and supported.
384static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800385 #define CPU_CONFIG(name, backend, color, alpha) \
386 if (is_cpu_config_allowed(#name)) { \
387 Config config = { #name, Benchmark::backend, color, alpha, 0, \
388 kBogusGLContextType, false }; \
389 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700390 }
mtkleine714e752014-07-31 12:13:48 -0700391
mtklein40b32be2014-07-09 08:46:49 -0700392 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700393 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
394 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
395 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700396 }
mtkleinbb6a0282014-07-01 08:43:42 -0700397
398#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800399 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700400 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
401 Config config = { \
402 #name, \
403 Benchmark::kGPU_Backend, \
404 kN32_SkColorType, \
405 kPremul_SkAlphaType, \
406 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800407 GrContextFactory::ctxType, \
408 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700409 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700410 }
mtkleine714e752014-07-31 12:13:48 -0700411
mtklein40b32be2014-07-09 08:46:49 -0700412 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800413 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
414 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
415 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
416 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
417 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
418 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
419 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
420 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700421#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800422 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700423#endif
mtklein40b32be2014-07-09 08:46:49 -0700424 }
mtkleinbb6a0282014-07-01 08:43:42 -0700425#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700426
427#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
428 if (is_cpu_config_allowed("hwui")) {
429 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
430 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
431 configs->push(config);
432 }
433#endif
mtkleinf3723212014-06-25 14:08:00 -0700434}
435
bsalomonc2553372014-07-22 13:09:05 -0700436// If bench is enabled for config, returns a Target* for it, otherwise NULL.
437static Target* is_enabled(Benchmark* bench, const Config& config) {
438 if (!bench->isSuitableFor(config.backend)) {
439 return NULL;
440 }
441
reede5ea5002014-09-03 11:54:58 -0700442 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
443 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700444
tomhudsond968a6f2015-03-26 11:28:06 -0700445 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700446
tomhudsond968a6f2015-03-26 11:28:06 -0700447 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700448#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700449 case Benchmark::kGPU_Backend:
450 target = new GPUTarget(config);
451 break;
bsalomonc2553372014-07-22 13:09:05 -0700452#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700453#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
454 case Benchmark::kHWUI_Backend:
455 target = new HWUITarget(config, bench);
456 break;
457#endif
458 default:
459 target = new Target(config);
460 break;
461 }
bsalomonc2553372014-07-22 13:09:05 -0700462
tomhudsond968a6f2015-03-26 11:28:06 -0700463 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700464 delete target;
465 return NULL;
466 }
467 return target;
468}
469
470// Creates targets for a benchmark and a set of configs.
471static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
472 const SkTDArray<Config>& configs) {
473 for (int i = 0; i < configs.count(); ++i) {
474 if (Target* t = is_enabled(b, configs[i])) {
475 targets->push(t);
476 }
mtkleine714e752014-07-31 12:13:48 -0700477
bsalomonc2553372014-07-22 13:09:05 -0700478 }
479}
480
jcgregoriobf5e5232014-07-17 13:14:16 -0700481
mtkleine714e752014-07-31 12:13:48 -0700482class BenchmarkStream {
483public:
mtklein92007582014-08-01 07:46:52 -0700484 BenchmarkStream() : fBenches(BenchRegistry::Head())
485 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700486 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700487 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800488 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800489 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700490 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800491 , fCurrentImage(0)
492 , fCurrentSubsetImage(0)
493 , fCurrentColorType(0)
494 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700495 for (int i = 0; i < FLAGS_skps.count(); i++) {
496 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
497 fSKPs.push_back() = FLAGS_skps[i];
498 } else {
499 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
500 SkString path;
501 while (it.next(&path)) {
502 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
503 }
504 }
505 }
mtkleine714e752014-07-31 12:13:48 -0700506
mtklein92007582014-08-01 07:46:52 -0700507 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
508 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
509 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
510 exit(1);
511 }
512
513 for (int i = 0; i < FLAGS_scales.count(); i++) {
514 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
515 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
516 exit(1);
517 }
518 }
robertphillips5b693772014-11-21 06:19:36 -0800519
520 fUseMPDs.push_back() = false;
521 if (FLAGS_mpd) {
522 fUseMPDs.push_back() = true;
523 }
mtklein95553d92015-03-12 08:24:21 -0700524
msarett95f192d2015-02-13 09:05:41 -0800525 // Prepare the images for decoding
526 for (int i = 0; i < FLAGS_images.count(); i++) {
527 const char* flag = FLAGS_images[i];
528 if (sk_isdir(flag)) {
529 // If the value passed in is a directory, add all the images
530 SkOSFile::Iter it(flag);
531 SkString file;
532 while (it.next(&file)) {
533 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
534 }
535 } else if (sk_exists(flag)) {
536 // Also add the value if it is a single image
537 fImages.push_back() = flag;
538 }
539 }
mtklein95553d92015-03-12 08:24:21 -0700540
msarett95f192d2015-02-13 09:05:41 -0800541 // Choose the candidate color types for image decoding
542 const SkColorType colorTypes[] =
543 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType };
544 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700545 }
546
mtkleinfd731ce2014-09-10 12:19:30 -0700547 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
548 // Not strictly necessary, as it will be checked again later,
549 // but helps to avoid a lot of pointless work if we're going to skip it.
550 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
551 return false;
552 }
553
scroggoa1193e42015-01-21 12:09:53 -0800554 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700555 if (stream.get() == NULL) {
556 SkDebugf("Could not read %s.\n", path);
557 return false;
558 }
559
mtklein57f27bd2015-02-09 11:58:41 -0800560 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700561 if (pic->get() == NULL) {
562 SkDebugf("Could not read %s as an SkPicture.\n", path);
563 return false;
564 }
565 return true;
566 }
567
mtklein92007582014-08-01 07:46:52 -0700568 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700569 if (fBenches) {
570 Benchmark* bench = fBenches->factory()(NULL);
571 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700572 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700573 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700574 return bench;
575 }
mtklein92007582014-08-01 07:46:52 -0700576
mtkleine714e752014-07-31 12:13:48 -0700577 while (fGMs) {
578 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
579 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800580 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700581 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700582 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700583 return SkNEW_ARGS(GMBench, (gm.detach()));
584 }
585 }
mtklein92007582014-08-01 07:46:52 -0700586
mtkleinfd731ce2014-09-10 12:19:30 -0700587 // First add all .skps as RecordingBenches.
588 while (fCurrentRecording < fSKPs.count()) {
589 const SkString& path = fSKPs[fCurrentRecording++];
590 SkAutoTUnref<SkPicture> pic;
591 if (!ReadPicture(path.c_str(), &pic)) {
592 continue;
593 }
594 SkString name = SkOSPath::Basename(path.c_str());
595 fSourceType = "skp";
596 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800597 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800598 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700599 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
600 }
601
602 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700603 while (fCurrentScale < fScales.count()) {
604 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800605 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700606 SkAutoTUnref<SkPicture> pic;
607 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800608 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700609 continue;
610 }
robertphillips5b693772014-11-21 06:19:36 -0800611
612 while (fCurrentUseMPD < fUseMPDs.count()) {
613 if (FLAGS_bbh) {
614 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
615 SkRTreeFactory factory;
616 SkPictureRecorder recorder;
617 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
618 pic->playback(recorder.beginRecording(pic->cullRect().width(),
619 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800620 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800621 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800622 pic.reset(recorder.endRecording());
623 }
624 SkString name = SkOSPath::Basename(path.c_str());
625 fSourceType = "skp";
626 fBenchType = "playback";
627 return SkNEW_ARGS(SKPBench,
628 (name.c_str(), pic.get(), fClip,
629 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
mtklein20840502014-08-21 15:51:22 -0700630 }
robertphillips5b693772014-11-21 06:19:36 -0800631 fCurrentUseMPD = 0;
632 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700633 }
634 fCurrentSKP = 0;
635 fCurrentScale++;
636 }
637
scroggo60869a42015-04-01 12:09:17 -0700638 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
639 const SkString& path = fImages[fCurrentCodec];
640 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
641 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
642 SkASSERT(codec);
643 if (!codec) {
644 // Nothing to time.
645 continue;
646 }
647 while (fCurrentColorType < fColorTypes.count()) {
648 SkColorType colorType = fColorTypes[fCurrentColorType];
649 fCurrentColorType++;
650 // Make sure we can decode to this color type.
651 SkBitmap bitmap;
652 SkImageInfo info = codec->getInfo().makeColorType(colorType);
653 bitmap.allocPixels(info);
654 const SkImageGenerator::Result result = codec->getPixels(
655 bitmap.info(), bitmap.getPixels(), bitmap.rowBytes());
656 switch (result) {
657 case SkImageGenerator::kSuccess:
658 case SkImageGenerator::kIncompleteInput:
659 return new CodecBench(SkOSPath::Basename(path.c_str()),
660 encoded, colorType);
661 case SkImageGenerator::kInvalidConversion:
662 // This is okay. Not all conversions are valid.
663 break;
664 case SkImageGenerator::kCouldNotRewind:
665 // FIXME: This is due to a bug in some implementations
666 // of SkCodec. All should support rewinding.
667 break;
668 default:
669 // This represents some sort of failure.
670 SkASSERT(false);
671 break;
672 }
673 }
674 fCurrentColorType = 0;
675 }
676
msarett95f192d2015-02-13 09:05:41 -0800677 // Run the DecodingBenches
678 while (fCurrentImage < fImages.count()) {
679 while (fCurrentColorType < fColorTypes.count()) {
680 const SkString& path = fImages[fCurrentImage];
681 SkColorType colorType = fColorTypes[fCurrentColorType];
682 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700683 // Check if the image decodes to the right color type
684 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800685 SkBitmap bitmap;
686 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700687 colorType, SkImageDecoder::kDecodePixels_Mode)
688 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800689 return new DecodingBench(path, colorType);
690 }
691 }
692 fCurrentColorType = 0;
693 fCurrentImage++;
694 }
695
696 // Run the DecodingSubsetBenches
697 while (fCurrentSubsetImage < fImages.count()) {
698 while (fCurrentColorType < fColorTypes.count()) {
699 const SkString& path = fImages[fCurrentSubsetImage];
700 SkColorType colorType = fColorTypes[fCurrentColorType];
701 fCurrentColorType++;
702 // Check if the image decodes before creating the benchmark
703 SkAutoTUnref<SkData> encoded(
704 SkData::NewFromFileName(path.c_str()));
705 SkAutoTDelete<SkMemoryStream> stream(
706 new SkMemoryStream(encoded));
707 SkAutoTDelete<SkImageDecoder>
708 decoder(SkImageDecoder::Factory(stream.get()));
709 if (!decoder) {
710 SkDebugf("Cannot find decoder for %s\n", path.c_str());
711 } else {
712 stream->rewind();
713 int w, h;
714 bool success;
715 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
716 || w*h == 1) {
717 // This is not an error, but in this case we still
718 // do not want to run the benchmark.
719 success = false;
720 } else if (fDivisor > w || fDivisor > h) {
721 SkDebugf("Divisor %d is too big for %s %dx%d\n",
722 fDivisor, path.c_str(), w, h);
723 success = false;
724 } else {
725 const int sW = w / fDivisor;
726 const int sH = h / fDivisor;
727 SkBitmap bitmap;
728 success = true;
729 for (int y = 0; y < h; y += sH) {
730 for (int x = 0; x < w; x += sW) {
731 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
732 success &= decoder->decodeSubset(&bitmap, rect,
733 colorType);
734 }
735 }
736 }
737 // Create the benchmark if successful
738 if (success) {
739 return new DecodingSubsetBench(path, colorType,
740 fDivisor);
741 }
742 }
743 }
744 fCurrentColorType = 0;
745 fCurrentSubsetImage++;
746 }
747
mtkleine714e752014-07-31 12:13:48 -0700748 return NULL;
749 }
mtklein92007582014-08-01 07:46:52 -0700750
751 void fillCurrentOptions(ResultsWriter* log) const {
752 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700753 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700754 if (0 == strcmp(fSourceType, "skp")) {
755 log->configOption("clip",
756 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
757 fClip.fRight, fClip.fBottom).c_str());
758 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800759 if (fCurrentUseMPD > 0) {
760 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
761 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
762 }
mtklein92007582014-08-01 07:46:52 -0700763 }
mtklein051e56d2014-12-04 08:46:51 -0800764 if (0 == strcmp(fBenchType, "recording")) {
765 log->metric("bytes", fSKPBytes);
766 log->metric("ops", fSKPOps);
767 }
mtklein92007582014-08-01 07:46:52 -0700768 }
769
mtkleine714e752014-07-31 12:13:48 -0700770private:
771 const BenchRegistry* fBenches;
772 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700773 SkIRect fClip;
774 SkTArray<SkScalar> fScales;
775 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800776 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800777 SkTArray<SkString> fImages;
778 SkTArray<SkColorType> fColorTypes;
mtklein92007582014-08-01 07:46:52 -0700779
mtklein051e56d2014-12-04 08:46:51 -0800780 double fSKPBytes, fSKPOps;
781
mtkleinfd731ce2014-09-10 12:19:30 -0700782 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
783 const char* fBenchType; // How we bench it: micro, recording, playback, ...
784 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700785 int fCurrentScale;
786 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800787 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700788 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800789 int fCurrentImage;
790 int fCurrentSubsetImage;
791 int fCurrentColorType;
792 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700793};
794
jcgregorio3b27ade2014-11-13 08:06:40 -0800795int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700796int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800797 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700798 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800799 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700800
krajcevski69a55602014-08-13 10:46:31 -0700801#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700802 GrContext::Options grContextOpts;
803 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
804 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700805#endif
806
bsalomon06cddec2014-10-24 10:40:50 -0700807 if (FLAGS_veryVerbose) {
808 FLAGS_verbose = true;
809 }
810
bsalomon6eb03cc2014-08-07 14:28:50 -0700811 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700812 FLAGS_samples = 1;
813 FLAGS_gpuFrameLag = 0;
814 }
815
bsalomon6eb03cc2014-08-07 14:28:50 -0700816 if (!FLAGS_writePath.isEmpty()) {
817 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
818 if (!sk_mkdir(FLAGS_writePath[0])) {
819 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
820 FLAGS_writePath.set(0, NULL);
821 }
822 }
823
mtklein1915b622014-08-20 11:45:00 -0700824 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700825 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700826 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700827 }
mtklein1915b622014-08-20 11:45:00 -0700828
829 if (1 == FLAGS_properties.count() % 2) {
830 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
831 return 1;
832 }
833 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
834 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
835 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700836
837 if (1 == FLAGS_key.count() % 2) {
838 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
839 return 1;
840 }
841 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700842 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700843 }
mtklein60317d0f2014-07-14 11:30:37 -0700844
mtkleinf3723212014-06-25 14:08:00 -0700845 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700846 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400847
mtkleinbb6a0282014-07-01 08:43:42 -0700848 SkAutoTMalloc<double> samples(FLAGS_samples);
849
bsalomon6eb03cc2014-08-07 14:28:50 -0700850 if (kAutoTuneLoops != FLAGS_loops) {
851 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700852 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700853 // No header.
854 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700855 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700856 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700857 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
858 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700859 }
860
bsalomonc2553372014-07-22 13:09:05 -0700861 SkTDArray<Config> configs;
862 create_configs(&configs);
863
mtkleine070c2b2014-10-14 08:40:43 -0700864 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700865 BenchmarkStream benchStream;
866 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700867 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700868 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700869 continue;
870 }
871
mtkleinbb6a0282014-07-01 08:43:42 -0700872 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700873 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700874
jcgregoriobf5e5232014-07-17 13:14:16 -0700875 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700876 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700877 bench->preDraw();
878 }
mtkleinbb6a0282014-07-01 08:43:42 -0700879 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700880 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -0700881 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -0700882 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700883
tomhudsond968a6f2015-03-26 11:28:06 -0700884 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800885 bench->perCanvasPreDraw(canvas);
886
mtkleinbb6a0282014-07-01 08:43:42 -0700887 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700888 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -0700889 ? gpu_bench(targets[j], bench.get(), samples.get())
890 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700891
robertphillips5b693772014-11-21 06:19:36 -0800892 bench->perCanvasPostDraw(canvas);
893
tomhudsond968a6f2015-03-26 11:28:06 -0700894 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
895 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700896 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700897 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700898 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700899 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700900 }
901
902 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700903 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400904 continue;
905 }
906
mtkleinf3723212014-06-25 14:08:00 -0700907 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700908 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700909 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700910 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700911 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800912 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700913 if (runs++ % FLAGS_flushEvery == 0) {
914 log->flush();
915 }
mtklein60317d0f2014-07-14 11:30:37 -0700916
bsalomon6eb03cc2014-08-07 14:28:50 -0700917 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700918 if (targets.count() == 1) {
919 config = ""; // Only print the config if we run the same bench on more than one.
920 }
mtklein53d25622014-09-18 07:39:42 -0700921 SkDebugf("%4dM\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700922 , sk_tools::getBestResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700923 , bench->getUniqueName()
924 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700925 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700926 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700927 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700928 }
mtklein96289052014-09-10 12:05:59 -0700929 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700930 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700931 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700932 config = ""; // Only print the config if we run the same bench on more than one.
933 }
mtklein96289052014-09-10 12:05:59 -0700934 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700935 } else {
936 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700937 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 -0700938 , sk_tools::getBestResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700939 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700940 , HUMANIZE(stats.min)
941 , HUMANIZE(stats.median)
942 , HUMANIZE(stats.mean)
943 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700944 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700945 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700946 , config
mtklein96289052014-09-10 12:05:59 -0700947 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700948 );
949 }
bsalomonb12ea412015-02-02 21:19:50 -0800950#if SK_SUPPORT_GPU
951 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -0700952 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700953 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -0800954 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -0700955 }
956#endif
mtkleinf3723212014-06-25 14:08:00 -0700957 }
mtkleinbb6a0282014-07-01 08:43:42 -0700958 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400959
bsalomon06cddec2014-10-24 10:40:50 -0700960#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700961 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700962 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700963 }
964 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700965 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400966 }
bsalomon06cddec2014-10-24 10:40:50 -0700967#endif
mtkleinf3723212014-06-25 14:08:00 -0700968 }
969
mtkleine1091452014-12-04 10:47:02 -0800970 log->bench("memory_usage", 0,0);
971 log->config("meta");
972 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
973
joshualitte0b19d42015-03-26 10:41:02 -0700974#if SK_SUPPORT_GPU
975 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
976 // SkEventTracer destructor
977 gGrFactory.reset(NULL);
978#endif
979
mtkleinf3723212014-06-25 14:08:00 -0700980 return 0;
981}
982
jcgregorio3b27ade2014-11-13 08:06:40 -0800983#if !defined SK_BUILD_FOR_IOS
984int main(int argc, char** argv) {
985 SkCommandLineFlags::Parse(argc, argv);
986 return nanobench_main();
987}
988#endif