blob: 4bc1484da0251134d79255d6a6e891d8bfa72673 [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"
13#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080014#include "DecodingBench.h"
15#include "DecodingSubsetBench.h"
mtkleine714e752014-07-31 12:13:48 -070016#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070017#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070018#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070019#include "RecordingBench.h"
mtklein92007582014-08-01 07:46:52 -070020#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070021#include "Stats.h"
22#include "Timer.h"
23
mtklein6838d852014-10-29 14:15:10 -070024#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070025#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070026#include "SkCommonFlags.h"
msarett95f192d2015-02-13 09:05:41 -080027#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070028#include "SkForceLinking.h"
29#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070030#include "SkOSFile.h"
31#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080032#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070033#include "SkString.h"
34#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080035#include "SkTaskGroup.h"
mtkleinf3723212014-06-25 14:08:00 -070036
tomhudsond968a6f2015-03-26 11:28:06 -070037#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
38 #include "nanobenchAndroid.h"
39#endif
40
mtkleinbb6a0282014-07-01 08:43:42 -070041#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070042 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070043 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070044 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070045#endif
46
mtkleinf3723212014-06-25 14:08:00 -070047__SK_FORCE_IMAGE_DECODER_LINKING;
48
reed53249782014-10-10 09:09:52 -070049static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070050
mtkleinb5110422014-08-07 15:20:02 -070051static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070052#ifdef SK_DEBUG
53 1;
mtkleina189ccd2014-07-14 12:28:47 -070054#else
bsalomon6eb03cc2014-08-07 14:28:50 -070055 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070056#endif
57
bsalomon6eb03cc2014-08-07 14:28:50 -070058static SkString loops_help_txt() {
59 SkString help;
60 help.printf("Number of times to run each bench. Set this to %d to auto-"
61 "tune for each bench. Timings are only reported when auto-tuning.",
62 kAutoTuneLoops);
63 return help;
64}
65
66DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
67
mtkleinf3723212014-06-25 14:08:00 -070068DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
69DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
70DEFINE_double(overheadGoal, 0.0001,
71 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070072DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
73DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070074DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
75 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070076
mtklein60317d0f2014-07-14 11:30:37 -070077DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070078DEFINE_int32(maxCalibrationAttempts, 3,
79 "Try up to this many times to guess loops for a bench, or skip the bench.");
80DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070081DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
82DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
mtklein20840502014-08-21 15:51:22 -070083DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -080084DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
mtkleine070c2b2014-10-14 08:40:43 -070085DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -080086DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -080087DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
mtklein92007582014-08-01 07:46:52 -070088
mtkleinf3723212014-06-25 14:08:00 -070089static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -070090 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -080091 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -070092}
mtklein55b0ffc2014-07-17 08:38:23 -070093#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070094
tomhudsond968a6f2015-03-26 11:28:06 -070095bool Target::init(SkImageInfo info, Benchmark* bench) {
96 if (Benchmark::kRaster_Backend == config.backend) {
97 this->surface.reset(SkSurface::NewRaster(info));
98 if (!this->surface.get()) {
99 return false;
100 }
101 }
102 return true;
103}
104bool Target::capturePixels(SkBitmap* bmp) {
105 if (!this->surface.get()) {
106 return false;
107 }
108 SkCanvas* canvas = this->surface->getCanvas();
109 if (!canvas) {
110 return false;
111 }
112 bmp->setInfo(canvas->imageInfo());
113 if (!canvas->readPixels(bmp, 0, 0)) {
114 SkDebugf("Can't read canvas pixels.\n");
115 return false;
116 }
117 return true;
118}
119
120#if SK_SUPPORT_GPU
121struct GPUTarget : public Target {
122 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
123 SkGLContext* gl;
124
125 void setup() override {
126 this->gl->makeCurrent();
127 // Make sure we're done with whatever came before.
128 SK_GL(*this->gl, Finish());
129 }
130 void endTiming() override {
131 if (this->gl) {
132 SK_GL(*this->gl, Flush());
133 this->gl->swapBuffers();
134 }
135 }
136 void fence() override {
137 SK_GL(*this->gl, Finish());
138 }
139
140 bool needsFrameTiming() const override { return true; }
141 bool init(SkImageInfo info, Benchmark* bench) override {
142 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
143 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
144 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
145 SkSurface::kNo_Budgeted, info,
146 this->config.samples, &props));
147 this->gl = gGrFactory->getGLContext(this->config.ctxType);
148 if (!this->surface.get()) {
149 return false;
150 }
151 return true;
152 }
153 void fillOptions(ResultsWriter* log) override {
154 const GrGLubyte* version;
155 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
156 log->configOption("GL_VERSION", (const char*)(version));
157
158 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
159 log->configOption("GL_RENDERER", (const char*) version);
160
161 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
162 log->configOption("GL_VENDOR", (const char*) version);
163
164 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
165 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
166 }
167};
168
169#endif
170
171static double time(int loops, Benchmark* bench, SkCanvas* canvas, Target* target) {
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();
tomhudsond968a6f2015-03-26 11:28:06 -0700177 if (target) {
178 canvas = target->beginTiming(canvas);
179 }
mtkleinbb6a0282014-07-01 08:43:42 -0700180 if (bench) {
181 bench->draw(loops, canvas);
182 }
183 if (canvas) {
184 canvas->flush();
185 }
tomhudsond968a6f2015-03-26 11:28:06 -0700186 if (target) {
187 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700188 }
mtkleinbb6a0282014-07-01 08:43:42 -0700189 timer.end();
190 return timer.fWall;
191}
192
mtkleinf3723212014-06-25 14:08:00 -0700193static double estimate_timer_overhead() {
194 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700195 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700196 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700197 }
198 return overhead / FLAGS_overheadLoops;
199}
200
reed53249782014-10-10 09:09:52 -0700201static int detect_forever_loops(int loops) {
202 // look for a magic run-forever value
203 if (loops < 0) {
204 loops = SK_MaxS32;
205 }
206 return loops;
207}
208
mtklein55b0ffc2014-07-17 08:38:23 -0700209static int clamp_loops(int loops) {
210 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800211 SkDebugf("ERROR: clamping loops from %d to 1. "
212 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700213 return 1;
214 }
215 if (loops > FLAGS_maxLoops) {
216 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
217 return FLAGS_maxLoops;
218 }
219 return loops;
220}
221
tomhudsond968a6f2015-03-26 11:28:06 -0700222static bool write_canvas_png(Target* target, const SkString& filename) {
223
bsalomon6eb03cc2014-08-07 14:28:50 -0700224 if (filename.isEmpty()) {
225 return false;
226 }
tomhudsond968a6f2015-03-26 11:28:06 -0700227 if (target->surface.get() && target->surface->getCanvas() &&
228 kUnknown_SkColorType == target->surface->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700229 return false;
230 }
tomhudsond968a6f2015-03-26 11:28:06 -0700231
bsalomon6eb03cc2014-08-07 14:28:50 -0700232 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700233
234 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700235 return false;
236 }
tomhudsond968a6f2015-03-26 11:28:06 -0700237
bsalomon6eb03cc2014-08-07 14:28:50 -0700238 SkString dir = SkOSPath::Dirname(filename.c_str());
239 if (!sk_mkdir(dir.c_str())) {
240 SkDebugf("Can't make dir %s.\n", dir.c_str());
241 return false;
242 }
243 SkFILEWStream stream(filename.c_str());
244 if (!stream.isValid()) {
245 SkDebugf("Can't write %s.\n", filename.c_str());
246 return false;
247 }
248 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
249 SkDebugf("Can't encode a PNG.\n");
250 return false;
251 }
252 return true;
253}
254
255static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700256static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
257 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700258 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700259 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700260 if (kAutoTuneLoops == FLAGS_loops) {
261 while (bench_plus_overhead < overhead) {
262 if (round++ == FLAGS_maxCalibrationAttempts) {
263 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700264 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700265 return kFailedLoops;
266 }
267 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700268 }
mtklein2069e222014-08-04 13:57:39 -0700269 }
mtkleinf3723212014-06-25 14:08:00 -0700270
mtkleinbb6a0282014-07-01 08:43:42 -0700271 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700272 // We'll pick N to make timer overhead negligible:
273 //
mtkleinbb6a0282014-07-01 08:43:42 -0700274 // overhead
275 // ------------------------- < FLAGS_overheadGoal
276 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700277 //
mtkleinbb6a0282014-07-01 08:43:42 -0700278 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700279 //
280 // Doing some math, we get:
281 //
mtkleinbb6a0282014-07-01 08:43:42 -0700282 // (overhead / FLAGS_overheadGoal) - overhead
283 // ------------------------------------------ < N
284 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700285 //
286 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700287 int loops = FLAGS_loops;
288 if (kAutoTuneLoops == loops) {
289 const double numer = overhead / FLAGS_overheadGoal - overhead;
290 const double denom = bench_plus_overhead - overhead;
291 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700292 loops = clamp_loops(loops);
293 } else {
294 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700295 }
mtkleinbb6a0282014-07-01 08:43:42 -0700296
297 for (int i = 0; i < FLAGS_samples; i++) {
298 samples[i] = time(loops, bench, canvas, NULL) / loops;
299 }
300 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700301}
302
tomhudsond968a6f2015-03-26 11:28:06 -0700303static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700304 Benchmark* bench,
305 SkCanvas* canvas,
306 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700307 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700308 int loops = FLAGS_loops;
309 if (kAutoTuneLoops == loops) {
310 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700311 double elapsed = 0;
312 do {
mtklein527930f2014-11-06 08:04:34 -0800313 if (1<<30 == loops) {
314 // We're about to wrap. Something's wrong with the bench.
315 loops = 0;
316 break;
317 }
mtkleina189ccd2014-07-14 12:28:47 -0700318 loops *= 2;
319 // If the GPU lets frames lag at all, we need to make sure we're timing
320 // _this_ round, not still timing last round. We force this by looping
321 // more times than any reasonable GPU will allow frames to lag.
322 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700323 elapsed = time(loops, bench, canvas, target);
mtkleina189ccd2014-07-14 12:28:47 -0700324 }
325 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700326
mtkleina189ccd2014-07-14 12:28:47 -0700327 // We've overshot at least a little. Scale back linearly.
328 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700329 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700330
tomhudsond968a6f2015-03-26 11:28:06 -0700331 // Make sure we're not still timing our calibration.
332 target->fence();
reed53249782014-10-10 09:09:52 -0700333 } else {
334 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700335 }
mtkleinbb6a0282014-07-01 08:43:42 -0700336
337 // Pretty much the same deal as the calibration: do some warmup to make
338 // sure we're timing steady-state pipelined frames.
339 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700340 time(loops, bench, canvas, target);
mtkleinf3723212014-06-25 14:08:00 -0700341 }
mtkleinbb6a0282014-07-01 08:43:42 -0700342
343 // Now, actually do the timing!
344 for (int i = 0; i < FLAGS_samples; i++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700345 samples[i] = time(loops, bench, canvas, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700346 }
tomhudsond968a6f2015-03-26 11:28:06 -0700347
mtkleinbb6a0282014-07-01 08:43:42 -0700348 return loops;
349}
mtkleinbb6a0282014-07-01 08:43:42 -0700350
351static SkString to_lower(const char* str) {
352 SkString lower(str);
353 for (size_t i = 0; i < lower.size(); i++) {
354 lower[i] = tolower(lower[i]);
355 }
356 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700357}
358
bsalomonc2553372014-07-22 13:09:05 -0700359static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700360 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700361 if (to_lower(FLAGS_config[i]).equals(name)) {
362 return true;
mtkleinf3723212014-06-25 14:08:00 -0700363 }
364 }
bsalomonc2553372014-07-22 13:09:05 -0700365 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700366}
367
bsalomonc2553372014-07-22 13:09:05 -0700368#if SK_SUPPORT_GPU
369static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
370 int sampleCnt) {
371 if (!is_cpu_config_allowed(name)) {
372 return false;
373 }
krajcevski69a55602014-08-13 10:46:31 -0700374 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700375 return sampleCnt <= ctx->getMaxSampleCount();
376 }
377 return false;
378}
379#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700380
bsalomonc2553372014-07-22 13:09:05 -0700381#if SK_SUPPORT_GPU
382#define kBogusGLContextType GrContextFactory::kNative_GLContextType
383#else
384#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700385#endif
bsalomonc2553372014-07-22 13:09:05 -0700386
387// Append all configs that are enabled and supported.
388static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800389 #define CPU_CONFIG(name, backend, color, alpha) \
390 if (is_cpu_config_allowed(#name)) { \
391 Config config = { #name, Benchmark::backend, color, alpha, 0, \
392 kBogusGLContextType, false }; \
393 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700394 }
mtkleine714e752014-07-31 12:13:48 -0700395
mtklein40b32be2014-07-09 08:46:49 -0700396 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700397 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
398 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
399 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700400 }
mtkleinbb6a0282014-07-01 08:43:42 -0700401
402#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800403 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700404 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
405 Config config = { \
406 #name, \
407 Benchmark::kGPU_Backend, \
408 kN32_SkColorType, \
409 kPremul_SkAlphaType, \
410 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800411 GrContextFactory::ctxType, \
412 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700413 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700414 }
mtkleine714e752014-07-31 12:13:48 -0700415
mtklein40b32be2014-07-09 08:46:49 -0700416 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800417 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
418 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
419 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
420 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
421 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
422 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
423 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
424 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700425#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800426 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700427#endif
mtklein40b32be2014-07-09 08:46:49 -0700428 }
mtkleinbb6a0282014-07-01 08:43:42 -0700429#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700430
431#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
432 if (is_cpu_config_allowed("hwui")) {
433 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
434 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
435 configs->push(config);
436 }
437#endif
mtkleinf3723212014-06-25 14:08:00 -0700438}
439
bsalomonc2553372014-07-22 13:09:05 -0700440// If bench is enabled for config, returns a Target* for it, otherwise NULL.
441static Target* is_enabled(Benchmark* bench, const Config& config) {
442 if (!bench->isSuitableFor(config.backend)) {
443 return NULL;
444 }
445
reede5ea5002014-09-03 11:54:58 -0700446 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
447 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700448
tomhudsond968a6f2015-03-26 11:28:06 -0700449 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700450
tomhudsond968a6f2015-03-26 11:28:06 -0700451 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700452#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700453 case Benchmark::kGPU_Backend:
454 target = new GPUTarget(config);
455 break;
bsalomonc2553372014-07-22 13:09:05 -0700456#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700457#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
458 case Benchmark::kHWUI_Backend:
459 target = new HWUITarget(config, bench);
460 break;
461#endif
462 default:
463 target = new Target(config);
464 break;
465 }
bsalomonc2553372014-07-22 13:09:05 -0700466
tomhudsond968a6f2015-03-26 11:28:06 -0700467 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700468 delete target;
469 return NULL;
470 }
471 return target;
472}
473
474// Creates targets for a benchmark and a set of configs.
475static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
476 const SkTDArray<Config>& configs) {
477 for (int i = 0; i < configs.count(); ++i) {
478 if (Target* t = is_enabled(b, configs[i])) {
479 targets->push(t);
480 }
mtkleine714e752014-07-31 12:13:48 -0700481
bsalomonc2553372014-07-22 13:09:05 -0700482 }
483}
484
jcgregoriobf5e5232014-07-17 13:14:16 -0700485
mtkleine714e752014-07-31 12:13:48 -0700486class BenchmarkStream {
487public:
mtklein92007582014-08-01 07:46:52 -0700488 BenchmarkStream() : fBenches(BenchRegistry::Head())
489 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700490 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700491 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800492 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800493 , fCurrentUseMPD(0)
494 , fCurrentImage(0)
495 , fCurrentSubsetImage(0)
496 , fCurrentColorType(0)
497 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700498 for (int i = 0; i < FLAGS_skps.count(); i++) {
499 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
500 fSKPs.push_back() = FLAGS_skps[i];
501 } else {
502 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
503 SkString path;
504 while (it.next(&path)) {
505 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
506 }
507 }
508 }
mtkleine714e752014-07-31 12:13:48 -0700509
mtklein92007582014-08-01 07:46:52 -0700510 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
511 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
512 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
513 exit(1);
514 }
515
516 for (int i = 0; i < FLAGS_scales.count(); i++) {
517 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
518 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
519 exit(1);
520 }
521 }
robertphillips5b693772014-11-21 06:19:36 -0800522
523 fUseMPDs.push_back() = false;
524 if (FLAGS_mpd) {
525 fUseMPDs.push_back() = true;
526 }
mtklein95553d92015-03-12 08:24:21 -0700527
msarett95f192d2015-02-13 09:05:41 -0800528 // Prepare the images for decoding
529 for (int i = 0; i < FLAGS_images.count(); i++) {
530 const char* flag = FLAGS_images[i];
531 if (sk_isdir(flag)) {
532 // If the value passed in is a directory, add all the images
533 SkOSFile::Iter it(flag);
534 SkString file;
535 while (it.next(&file)) {
536 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
537 }
538 } else if (sk_exists(flag)) {
539 // Also add the value if it is a single image
540 fImages.push_back() = flag;
541 }
542 }
mtklein95553d92015-03-12 08:24:21 -0700543
msarett95f192d2015-02-13 09:05:41 -0800544 // Choose the candidate color types for image decoding
545 const SkColorType colorTypes[] =
546 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType };
547 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700548 }
549
mtkleinfd731ce2014-09-10 12:19:30 -0700550 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
551 // Not strictly necessary, as it will be checked again later,
552 // but helps to avoid a lot of pointless work if we're going to skip it.
553 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
554 return false;
555 }
556
scroggoa1193e42015-01-21 12:09:53 -0800557 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700558 if (stream.get() == NULL) {
559 SkDebugf("Could not read %s.\n", path);
560 return false;
561 }
562
mtklein57f27bd2015-02-09 11:58:41 -0800563 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700564 if (pic->get() == NULL) {
565 SkDebugf("Could not read %s as an SkPicture.\n", path);
566 return false;
567 }
568 return true;
569 }
570
mtklein92007582014-08-01 07:46:52 -0700571 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700572 if (fBenches) {
573 Benchmark* bench = fBenches->factory()(NULL);
574 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700575 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700576 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700577 return bench;
578 }
mtklein92007582014-08-01 07:46:52 -0700579
mtkleine714e752014-07-31 12:13:48 -0700580 while (fGMs) {
581 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
582 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800583 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700584 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700585 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700586 return SkNEW_ARGS(GMBench, (gm.detach()));
587 }
588 }
mtklein92007582014-08-01 07:46:52 -0700589
mtkleinfd731ce2014-09-10 12:19:30 -0700590 // First add all .skps as RecordingBenches.
591 while (fCurrentRecording < fSKPs.count()) {
592 const SkString& path = fSKPs[fCurrentRecording++];
593 SkAutoTUnref<SkPicture> pic;
594 if (!ReadPicture(path.c_str(), &pic)) {
595 continue;
596 }
597 SkString name = SkOSPath::Basename(path.c_str());
598 fSourceType = "skp";
599 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800600 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800601 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700602 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
603 }
604
605 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700606 while (fCurrentScale < fScales.count()) {
607 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800608 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700609 SkAutoTUnref<SkPicture> pic;
610 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800611 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700612 continue;
613 }
robertphillips5b693772014-11-21 06:19:36 -0800614
615 while (fCurrentUseMPD < fUseMPDs.count()) {
616 if (FLAGS_bbh) {
617 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
618 SkRTreeFactory factory;
619 SkPictureRecorder recorder;
620 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
621 pic->playback(recorder.beginRecording(pic->cullRect().width(),
622 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800623 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800624 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800625 pic.reset(recorder.endRecording());
626 }
627 SkString name = SkOSPath::Basename(path.c_str());
628 fSourceType = "skp";
629 fBenchType = "playback";
630 return SkNEW_ARGS(SKPBench,
631 (name.c_str(), pic.get(), fClip,
632 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
mtklein20840502014-08-21 15:51:22 -0700633 }
robertphillips5b693772014-11-21 06:19:36 -0800634 fCurrentUseMPD = 0;
635 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700636 }
637 fCurrentSKP = 0;
638 fCurrentScale++;
639 }
640
msarett95f192d2015-02-13 09:05:41 -0800641 // Run the DecodingBenches
642 while (fCurrentImage < fImages.count()) {
643 while (fCurrentColorType < fColorTypes.count()) {
644 const SkString& path = fImages[fCurrentImage];
645 SkColorType colorType = fColorTypes[fCurrentColorType];
646 fCurrentColorType++;
647 // Check if the image decodes before creating the benchmark
648 SkBitmap bitmap;
649 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
650 colorType, SkImageDecoder::kDecodePixels_Mode)) {
651 return new DecodingBench(path, colorType);
652 }
653 }
654 fCurrentColorType = 0;
655 fCurrentImage++;
656 }
657
658 // Run the DecodingSubsetBenches
659 while (fCurrentSubsetImage < fImages.count()) {
660 while (fCurrentColorType < fColorTypes.count()) {
661 const SkString& path = fImages[fCurrentSubsetImage];
662 SkColorType colorType = fColorTypes[fCurrentColorType];
663 fCurrentColorType++;
664 // Check if the image decodes before creating the benchmark
665 SkAutoTUnref<SkData> encoded(
666 SkData::NewFromFileName(path.c_str()));
667 SkAutoTDelete<SkMemoryStream> stream(
668 new SkMemoryStream(encoded));
669 SkAutoTDelete<SkImageDecoder>
670 decoder(SkImageDecoder::Factory(stream.get()));
671 if (!decoder) {
672 SkDebugf("Cannot find decoder for %s\n", path.c_str());
673 } else {
674 stream->rewind();
675 int w, h;
676 bool success;
677 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
678 || w*h == 1) {
679 // This is not an error, but in this case we still
680 // do not want to run the benchmark.
681 success = false;
682 } else if (fDivisor > w || fDivisor > h) {
683 SkDebugf("Divisor %d is too big for %s %dx%d\n",
684 fDivisor, path.c_str(), w, h);
685 success = false;
686 } else {
687 const int sW = w / fDivisor;
688 const int sH = h / fDivisor;
689 SkBitmap bitmap;
690 success = true;
691 for (int y = 0; y < h; y += sH) {
692 for (int x = 0; x < w; x += sW) {
693 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
694 success &= decoder->decodeSubset(&bitmap, rect,
695 colorType);
696 }
697 }
698 }
699 // Create the benchmark if successful
700 if (success) {
701 return new DecodingSubsetBench(path, colorType,
702 fDivisor);
703 }
704 }
705 }
706 fCurrentColorType = 0;
707 fCurrentSubsetImage++;
708 }
709
mtkleine714e752014-07-31 12:13:48 -0700710 return NULL;
711 }
mtklein92007582014-08-01 07:46:52 -0700712
713 void fillCurrentOptions(ResultsWriter* log) const {
714 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700715 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700716 if (0 == strcmp(fSourceType, "skp")) {
717 log->configOption("clip",
718 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
719 fClip.fRight, fClip.fBottom).c_str());
720 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800721 if (fCurrentUseMPD > 0) {
722 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
723 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
724 }
mtklein92007582014-08-01 07:46:52 -0700725 }
mtklein051e56d2014-12-04 08:46:51 -0800726 if (0 == strcmp(fBenchType, "recording")) {
727 log->metric("bytes", fSKPBytes);
728 log->metric("ops", fSKPOps);
729 }
mtklein92007582014-08-01 07:46:52 -0700730 }
731
mtkleine714e752014-07-31 12:13:48 -0700732private:
733 const BenchRegistry* fBenches;
734 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700735 SkIRect fClip;
736 SkTArray<SkScalar> fScales;
737 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800738 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800739 SkTArray<SkString> fImages;
740 SkTArray<SkColorType> fColorTypes;
mtklein92007582014-08-01 07:46:52 -0700741
mtklein051e56d2014-12-04 08:46:51 -0800742 double fSKPBytes, fSKPOps;
743
mtkleinfd731ce2014-09-10 12:19:30 -0700744 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
745 const char* fBenchType; // How we bench it: micro, recording, playback, ...
746 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700747 int fCurrentScale;
748 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800749 int fCurrentUseMPD;
msarett95f192d2015-02-13 09:05:41 -0800750 int fCurrentImage;
751 int fCurrentSubsetImage;
752 int fCurrentColorType;
753 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700754};
755
jcgregorio3b27ade2014-11-13 08:06:40 -0800756int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700757int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800758 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700759 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800760 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700761
krajcevski69a55602014-08-13 10:46:31 -0700762#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700763 GrContext::Options grContextOpts;
764 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
765 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700766#endif
767
bsalomon06cddec2014-10-24 10:40:50 -0700768 if (FLAGS_veryVerbose) {
769 FLAGS_verbose = true;
770 }
771
bsalomon6eb03cc2014-08-07 14:28:50 -0700772 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700773 FLAGS_samples = 1;
774 FLAGS_gpuFrameLag = 0;
775 }
776
bsalomon6eb03cc2014-08-07 14:28:50 -0700777 if (!FLAGS_writePath.isEmpty()) {
778 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
779 if (!sk_mkdir(FLAGS_writePath[0])) {
780 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
781 FLAGS_writePath.set(0, NULL);
782 }
783 }
784
mtklein1915b622014-08-20 11:45:00 -0700785 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700786 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700787 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700788 }
mtklein1915b622014-08-20 11:45:00 -0700789
790 if (1 == FLAGS_properties.count() % 2) {
791 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
792 return 1;
793 }
794 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
795 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
796 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700797
798 if (1 == FLAGS_key.count() % 2) {
799 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
800 return 1;
801 }
802 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700803 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700804 }
mtklein60317d0f2014-07-14 11:30:37 -0700805
mtkleinf3723212014-06-25 14:08:00 -0700806 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700807 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400808
mtkleinbb6a0282014-07-01 08:43:42 -0700809 SkAutoTMalloc<double> samples(FLAGS_samples);
810
bsalomon6eb03cc2014-08-07 14:28:50 -0700811 if (kAutoTuneLoops != FLAGS_loops) {
812 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700813 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700814 // No header.
815 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700816 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700817 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700818 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
819 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700820 }
821
bsalomonc2553372014-07-22 13:09:05 -0700822 SkTDArray<Config> configs;
823 create_configs(&configs);
824
mtkleine070c2b2014-10-14 08:40:43 -0700825 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700826 BenchmarkStream benchStream;
827 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700828 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700829 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700830 continue;
831 }
832
mtkleinbb6a0282014-07-01 08:43:42 -0700833 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700834 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700835
jcgregoriobf5e5232014-07-17 13:14:16 -0700836 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700837 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700838 bench->preDraw();
839 }
mtkleinbb6a0282014-07-01 08:43:42 -0700840 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700841 // During HWUI output this canvas may be NULL.
mtkleinbb6a0282014-07-01 08:43:42 -0700842 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700843 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700844
tomhudsond968a6f2015-03-26 11:28:06 -0700845 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800846 bench->perCanvasPreDraw(canvas);
847
mtkleinbb6a0282014-07-01 08:43:42 -0700848 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700849 targets[j]->needsFrameTiming()
850 ? gpu_bench(targets[j], bench.get(), canvas, samples.get())
851 : cpu_bench(overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700852
robertphillips5b693772014-11-21 06:19:36 -0800853 bench->perCanvasPostDraw(canvas);
854
tomhudsond968a6f2015-03-26 11:28:06 -0700855 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
856 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700857 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700858 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700859 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700860 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700861 }
862
863 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700864 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400865 continue;
866 }
867
mtkleinf3723212014-06-25 14:08:00 -0700868 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700869 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700870 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700871 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700872 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800873 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700874 if (runs++ % FLAGS_flushEvery == 0) {
875 log->flush();
876 }
mtklein60317d0f2014-07-14 11:30:37 -0700877
bsalomon6eb03cc2014-08-07 14:28:50 -0700878 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700879 if (targets.count() == 1) {
880 config = ""; // Only print the config if we run the same bench on more than one.
881 }
mtklein53d25622014-09-18 07:39:42 -0700882 SkDebugf("%4dM\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700883 , sk_tools::getBestResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700884 , bench->getUniqueName()
885 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700886 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700887 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700888 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700889 }
mtklein96289052014-09-10 12:05:59 -0700890 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700891 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700892 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700893 config = ""; // Only print the config if we run the same bench on more than one.
894 }
mtklein96289052014-09-10 12:05:59 -0700895 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700896 } else {
897 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700898 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 -0700899 , sk_tools::getBestResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700900 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700901 , HUMANIZE(stats.min)
902 , HUMANIZE(stats.median)
903 , HUMANIZE(stats.mean)
904 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700905 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700906 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700907 , config
mtklein96289052014-09-10 12:05:59 -0700908 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700909 );
910 }
bsalomonb12ea412015-02-02 21:19:50 -0800911#if SK_SUPPORT_GPU
912 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -0700913 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700914 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -0800915 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -0700916 }
917#endif
mtkleinf3723212014-06-25 14:08:00 -0700918 }
mtkleinbb6a0282014-07-01 08:43:42 -0700919 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400920
bsalomon06cddec2014-10-24 10:40:50 -0700921#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700922 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700923 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700924 }
925 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700926 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400927 }
bsalomon06cddec2014-10-24 10:40:50 -0700928#endif
mtkleinf3723212014-06-25 14:08:00 -0700929 }
930
mtkleine1091452014-12-04 10:47:02 -0800931 log->bench("memory_usage", 0,0);
932 log->config("meta");
933 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
934
joshualitte0b19d42015-03-26 10:41:02 -0700935#if SK_SUPPORT_GPU
936 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
937 // SkEventTracer destructor
938 gGrFactory.reset(NULL);
939#endif
940
mtkleinf3723212014-06-25 14:08:00 -0700941 return 0;
942}
943
jcgregorio3b27ade2014-11-13 08:06:40 -0800944#if !defined SK_BUILD_FOR_IOS
945int main(int argc, char** argv) {
946 SkCommandLineFlags::Parse(argc, argv);
947 return nanobench_main();
948}
949#endif