blob: 0045d53f40ea6ad41b8d8ccf163b3a20492409ea [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[] =
scroggo21027992015-04-02 13:22:38 -0700543 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType, kIndex_8_SkColorType };
msarett95f192d2015-02-13 09:05:41 -0800544 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));
scroggo60869a42015-04-01 12:09:17 -0700642 if (!codec) {
643 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700644 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700645 continue;
646 }
scroggo21027992015-04-02 13:22:38 -0700647
scroggo60869a42015-04-01 12:09:17 -0700648 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700649 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700650 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700651
scroggo60869a42015-04-01 12:09:17 -0700652 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700653 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700654 SkAlphaType alphaType;
655 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
656 &alphaType)) {
657 continue;
658 }
659 if (alphaType != info.alphaType()) {
660 info = info.makeAlphaType(alphaType);
661 }
662
663 const size_t rowBytes = info.minRowBytes();
664 SkAutoMalloc storage(info.getSafeSize(rowBytes));
665
666 // Used if fCurrentColorType is kIndex_8_SkColorType
667 int colorCount = 256;
668 SkPMColor colors[256];
669
scroggo60869a42015-04-01 12:09:17 -0700670 const SkImageGenerator::Result result = codec->getPixels(
scroggo21027992015-04-02 13:22:38 -0700671 info, storage.get(), rowBytes, NULL, colors,
672 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700673 switch (result) {
674 case SkImageGenerator::kSuccess:
675 case SkImageGenerator::kIncompleteInput:
676 return new CodecBench(SkOSPath::Basename(path.c_str()),
677 encoded, colorType);
678 case SkImageGenerator::kInvalidConversion:
679 // This is okay. Not all conversions are valid.
680 break;
scroggo60869a42015-04-01 12:09:17 -0700681 default:
682 // This represents some sort of failure.
683 SkASSERT(false);
684 break;
685 }
686 }
687 fCurrentColorType = 0;
688 }
689
msarett95f192d2015-02-13 09:05:41 -0800690 // Run the DecodingBenches
691 while (fCurrentImage < fImages.count()) {
692 while (fCurrentColorType < fColorTypes.count()) {
693 const SkString& path = fImages[fCurrentImage];
694 SkColorType colorType = fColorTypes[fCurrentColorType];
695 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700696 // Check if the image decodes to the right color type
697 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800698 SkBitmap bitmap;
699 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700700 colorType, SkImageDecoder::kDecodePixels_Mode)
701 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800702 return new DecodingBench(path, colorType);
703 }
704 }
705 fCurrentColorType = 0;
706 fCurrentImage++;
707 }
708
709 // Run the DecodingSubsetBenches
710 while (fCurrentSubsetImage < fImages.count()) {
711 while (fCurrentColorType < fColorTypes.count()) {
712 const SkString& path = fImages[fCurrentSubsetImage];
713 SkColorType colorType = fColorTypes[fCurrentColorType];
714 fCurrentColorType++;
715 // Check if the image decodes before creating the benchmark
716 SkAutoTUnref<SkData> encoded(
717 SkData::NewFromFileName(path.c_str()));
718 SkAutoTDelete<SkMemoryStream> stream(
719 new SkMemoryStream(encoded));
720 SkAutoTDelete<SkImageDecoder>
721 decoder(SkImageDecoder::Factory(stream.get()));
722 if (!decoder) {
723 SkDebugf("Cannot find decoder for %s\n", path.c_str());
724 } else {
725 stream->rewind();
726 int w, h;
727 bool success;
728 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
729 || w*h == 1) {
730 // This is not an error, but in this case we still
731 // do not want to run the benchmark.
732 success = false;
733 } else if (fDivisor > w || fDivisor > h) {
734 SkDebugf("Divisor %d is too big for %s %dx%d\n",
735 fDivisor, path.c_str(), w, h);
736 success = false;
737 } else {
738 const int sW = w / fDivisor;
739 const int sH = h / fDivisor;
740 SkBitmap bitmap;
741 success = true;
742 for (int y = 0; y < h; y += sH) {
743 for (int x = 0; x < w; x += sW) {
744 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
745 success &= decoder->decodeSubset(&bitmap, rect,
746 colorType);
747 }
748 }
749 }
750 // Create the benchmark if successful
751 if (success) {
752 return new DecodingSubsetBench(path, colorType,
753 fDivisor);
754 }
755 }
756 }
757 fCurrentColorType = 0;
758 fCurrentSubsetImage++;
759 }
760
mtkleine714e752014-07-31 12:13:48 -0700761 return NULL;
762 }
mtklein92007582014-08-01 07:46:52 -0700763
764 void fillCurrentOptions(ResultsWriter* log) const {
765 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700766 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700767 if (0 == strcmp(fSourceType, "skp")) {
768 log->configOption("clip",
769 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
770 fClip.fRight, fClip.fBottom).c_str());
771 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800772 if (fCurrentUseMPD > 0) {
773 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
774 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
775 }
mtklein92007582014-08-01 07:46:52 -0700776 }
mtklein051e56d2014-12-04 08:46:51 -0800777 if (0 == strcmp(fBenchType, "recording")) {
778 log->metric("bytes", fSKPBytes);
779 log->metric("ops", fSKPOps);
780 }
mtklein92007582014-08-01 07:46:52 -0700781 }
782
mtkleine714e752014-07-31 12:13:48 -0700783private:
784 const BenchRegistry* fBenches;
785 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700786 SkIRect fClip;
787 SkTArray<SkScalar> fScales;
788 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800789 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800790 SkTArray<SkString> fImages;
791 SkTArray<SkColorType> fColorTypes;
mtklein92007582014-08-01 07:46:52 -0700792
mtklein051e56d2014-12-04 08:46:51 -0800793 double fSKPBytes, fSKPOps;
794
mtkleinfd731ce2014-09-10 12:19:30 -0700795 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
796 const char* fBenchType; // How we bench it: micro, recording, playback, ...
797 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700798 int fCurrentScale;
799 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800800 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700801 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800802 int fCurrentImage;
803 int fCurrentSubsetImage;
804 int fCurrentColorType;
805 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700806};
807
jcgregorio3b27ade2014-11-13 08:06:40 -0800808int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700809int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800810 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700811 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800812 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700813
krajcevski69a55602014-08-13 10:46:31 -0700814#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700815 GrContext::Options grContextOpts;
816 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
817 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700818#endif
819
bsalomon06cddec2014-10-24 10:40:50 -0700820 if (FLAGS_veryVerbose) {
821 FLAGS_verbose = true;
822 }
823
bsalomon6eb03cc2014-08-07 14:28:50 -0700824 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700825 FLAGS_samples = 1;
826 FLAGS_gpuFrameLag = 0;
827 }
828
bsalomon6eb03cc2014-08-07 14:28:50 -0700829 if (!FLAGS_writePath.isEmpty()) {
830 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
831 if (!sk_mkdir(FLAGS_writePath[0])) {
832 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
833 FLAGS_writePath.set(0, NULL);
834 }
835 }
836
mtklein1915b622014-08-20 11:45:00 -0700837 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700838 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700839 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700840 }
mtklein1915b622014-08-20 11:45:00 -0700841
842 if (1 == FLAGS_properties.count() % 2) {
843 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
844 return 1;
845 }
846 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
847 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
848 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700849
850 if (1 == FLAGS_key.count() % 2) {
851 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
852 return 1;
853 }
854 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700855 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700856 }
mtklein60317d0f2014-07-14 11:30:37 -0700857
mtkleinf3723212014-06-25 14:08:00 -0700858 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700859 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400860
mtkleinbb6a0282014-07-01 08:43:42 -0700861 SkAutoTMalloc<double> samples(FLAGS_samples);
862
bsalomon6eb03cc2014-08-07 14:28:50 -0700863 if (kAutoTuneLoops != FLAGS_loops) {
864 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700865 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700866 // No header.
867 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700868 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700869 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700870 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
871 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700872 }
873
bsalomonc2553372014-07-22 13:09:05 -0700874 SkTDArray<Config> configs;
875 create_configs(&configs);
876
mtkleine070c2b2014-10-14 08:40:43 -0700877 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700878 BenchmarkStream benchStream;
879 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700880 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700881 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700882 continue;
883 }
884
mtkleinbb6a0282014-07-01 08:43:42 -0700885 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700886 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700887
jcgregoriobf5e5232014-07-17 13:14:16 -0700888 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700889 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700890 bench->preDraw();
891 }
mtkleinbb6a0282014-07-01 08:43:42 -0700892 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700893 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -0700894 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -0700895 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700896
tomhudsond968a6f2015-03-26 11:28:06 -0700897 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800898 bench->perCanvasPreDraw(canvas);
899
mtkleinbb6a0282014-07-01 08:43:42 -0700900 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700901 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -0700902 ? gpu_bench(targets[j], bench.get(), samples.get())
903 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700904
robertphillips5b693772014-11-21 06:19:36 -0800905 bench->perCanvasPostDraw(canvas);
906
tomhudsond968a6f2015-03-26 11:28:06 -0700907 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
908 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700909 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700910 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700911 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700912 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700913 }
914
915 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700916 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400917 continue;
918 }
919
mtkleinf3723212014-06-25 14:08:00 -0700920 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700921 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700922 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700923 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700924 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800925 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700926 if (runs++ % FLAGS_flushEvery == 0) {
927 log->flush();
928 }
mtklein60317d0f2014-07-14 11:30:37 -0700929
bsalomon6eb03cc2014-08-07 14:28:50 -0700930 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700931 if (targets.count() == 1) {
932 config = ""; // Only print the config if we run the same bench on more than one.
933 }
mtklein53d25622014-09-18 07:39:42 -0700934 SkDebugf("%4dM\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700935 , sk_tools::getBestResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700936 , bench->getUniqueName()
937 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700938 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700939 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700940 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700941 }
mtklein96289052014-09-10 12:05:59 -0700942 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700943 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700944 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700945 config = ""; // Only print the config if we run the same bench on more than one.
946 }
mtklein96289052014-09-10 12:05:59 -0700947 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700948 } else {
949 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700950 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 -0700951 , sk_tools::getBestResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700952 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700953 , HUMANIZE(stats.min)
954 , HUMANIZE(stats.median)
955 , HUMANIZE(stats.mean)
956 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700957 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700958 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700959 , config
mtklein96289052014-09-10 12:05:59 -0700960 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700961 );
962 }
bsalomonb12ea412015-02-02 21:19:50 -0800963#if SK_SUPPORT_GPU
964 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -0700965 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700966 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -0800967 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -0700968 }
969#endif
mtkleinf3723212014-06-25 14:08:00 -0700970 }
mtkleinbb6a0282014-07-01 08:43:42 -0700971 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400972
bsalomon06cddec2014-10-24 10:40:50 -0700973#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700974 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700975 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700976 }
977 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700978 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400979 }
bsalomon06cddec2014-10-24 10:40:50 -0700980#endif
mtkleinf3723212014-06-25 14:08:00 -0700981 }
982
mtkleine1091452014-12-04 10:47:02 -0800983 log->bench("memory_usage", 0,0);
984 log->config("meta");
985 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
986
joshualitte0b19d42015-03-26 10:41:02 -0700987#if SK_SUPPORT_GPU
988 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
989 // SkEventTracer destructor
990 gGrFactory.reset(NULL);
991#endif
992
mtkleinf3723212014-06-25 14:08:00 -0700993 return 0;
994}
995
jcgregorio3b27ade2014-11-13 08:06:40 -0800996#if !defined SK_BUILD_FOR_IOS
997int main(int argc, char** argv) {
998 SkCommandLineFlags::Parse(argc, argv);
999 return nanobench_main();
1000}
1001#endif