blob: 07600ef3a447445737dc50626dc6d35e90bd97bd [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) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700105 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700106 if (!canvas) {
107 return false;
108 }
109 bmp->setInfo(canvas->imageInfo());
110 if (!canvas->readPixels(bmp, 0, 0)) {
111 SkDebugf("Can't read canvas pixels.\n");
112 return false;
113 }
114 return true;
115}
116
117#if SK_SUPPORT_GPU
118struct GPUTarget : public Target {
119 explicit GPUTarget(const Config& c) : Target(c), gl(NULL) { }
120 SkGLContext* gl;
121
122 void setup() override {
123 this->gl->makeCurrent();
124 // Make sure we're done with whatever came before.
125 SK_GL(*this->gl, Finish());
126 }
127 void endTiming() override {
128 if (this->gl) {
129 SK_GL(*this->gl, Flush());
130 this->gl->swapBuffers();
131 }
132 }
133 void fence() override {
134 SK_GL(*this->gl, Finish());
135 }
136
137 bool needsFrameTiming() const override { return true; }
138 bool init(SkImageInfo info, Benchmark* bench) override {
139 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
140 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
141 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType),
142 SkSurface::kNo_Budgeted, info,
143 this->config.samples, &props));
144 this->gl = gGrFactory->getGLContext(this->config.ctxType);
145 if (!this->surface.get()) {
146 return false;
147 }
148 return true;
149 }
150 void fillOptions(ResultsWriter* log) override {
151 const GrGLubyte* version;
152 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
153 log->configOption("GL_VERSION", (const char*)(version));
154
155 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
156 log->configOption("GL_RENDERER", (const char*) version);
157
158 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
159 log->configOption("GL_VENDOR", (const char*) version);
160
161 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
162 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
163 }
164};
165
166#endif
167
tomhudson75a0ebb2015-03-27 12:11:44 -0700168static double time(int loops, Benchmark* bench, Target* target) {
169 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700170 if (canvas) {
171 canvas->clear(SK_ColorWHITE);
172 }
mtkleinbb6a0282014-07-01 08:43:42 -0700173 WallTimer timer;
174 timer.start();
tomhudson75a0ebb2015-03-27 12:11:44 -0700175 canvas = target->beginTiming(canvas);
176 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700177 if (canvas) {
178 canvas->flush();
179 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700180 target->endTiming();
mtkleinbb6a0282014-07-01 08:43:42 -0700181 timer.end();
182 return timer.fWall;
183}
184
mtkleinf3723212014-06-25 14:08:00 -0700185static double estimate_timer_overhead() {
186 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700187 for (int i = 0; i < FLAGS_overheadLoops; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700188 WallTimer timer;
189 timer.start();
190 timer.end();
191 overhead += timer.fWall;
mtkleinf3723212014-06-25 14:08:00 -0700192 }
193 return overhead / FLAGS_overheadLoops;
194}
195
reed53249782014-10-10 09:09:52 -0700196static int detect_forever_loops(int loops) {
197 // look for a magic run-forever value
198 if (loops < 0) {
199 loops = SK_MaxS32;
200 }
201 return loops;
202}
203
mtklein55b0ffc2014-07-17 08:38:23 -0700204static int clamp_loops(int loops) {
205 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800206 SkDebugf("ERROR: clamping loops from %d to 1. "
207 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700208 return 1;
209 }
210 if (loops > FLAGS_maxLoops) {
211 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
212 return FLAGS_maxLoops;
213 }
214 return loops;
215}
216
tomhudsond968a6f2015-03-26 11:28:06 -0700217static bool write_canvas_png(Target* target, const SkString& filename) {
218
bsalomon6eb03cc2014-08-07 14:28:50 -0700219 if (filename.isEmpty()) {
220 return false;
221 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700222 if (target->getCanvas() &&
223 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700224 return false;
225 }
tomhudsond968a6f2015-03-26 11:28:06 -0700226
bsalomon6eb03cc2014-08-07 14:28:50 -0700227 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700228
229 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700230 return false;
231 }
tomhudsond968a6f2015-03-26 11:28:06 -0700232
bsalomon6eb03cc2014-08-07 14:28:50 -0700233 SkString dir = SkOSPath::Dirname(filename.c_str());
234 if (!sk_mkdir(dir.c_str())) {
235 SkDebugf("Can't make dir %s.\n", dir.c_str());
236 return false;
237 }
238 SkFILEWStream stream(filename.c_str());
239 if (!stream.isValid()) {
240 SkDebugf("Can't write %s.\n", filename.c_str());
241 return false;
242 }
243 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
244 SkDebugf("Can't encode a PNG.\n");
245 return false;
246 }
247 return true;
248}
249
250static int kFailedLoops = -2;
tomhudson75a0ebb2015-03-27 12:11:44 -0700251static int cpu_bench(const double overhead, Target* target, Benchmark* bench, double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700252 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700253 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700254 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700255 if (kAutoTuneLoops == FLAGS_loops) {
256 while (bench_plus_overhead < overhead) {
257 if (round++ == FLAGS_maxCalibrationAttempts) {
258 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700259 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700260 return kFailedLoops;
261 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700262 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700263 }
mtklein2069e222014-08-04 13:57:39 -0700264 }
mtkleinf3723212014-06-25 14:08:00 -0700265
mtkleinbb6a0282014-07-01 08:43:42 -0700266 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700267 // We'll pick N to make timer overhead negligible:
268 //
mtkleinbb6a0282014-07-01 08:43:42 -0700269 // overhead
270 // ------------------------- < FLAGS_overheadGoal
271 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700272 //
mtkleinbb6a0282014-07-01 08:43:42 -0700273 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700274 //
275 // Doing some math, we get:
276 //
mtkleinbb6a0282014-07-01 08:43:42 -0700277 // (overhead / FLAGS_overheadGoal) - overhead
278 // ------------------------------------------ < N
279 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700280 //
281 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700282 int loops = FLAGS_loops;
283 if (kAutoTuneLoops == loops) {
284 const double numer = overhead / FLAGS_overheadGoal - overhead;
285 const double denom = bench_plus_overhead - overhead;
286 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700287 loops = clamp_loops(loops);
288 } else {
289 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700290 }
mtkleinbb6a0282014-07-01 08:43:42 -0700291
292 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700293 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700294 }
295 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700296}
297
tomhudsond968a6f2015-03-26 11:28:06 -0700298static int gpu_bench(Target* target,
mtkleinbb6a0282014-07-01 08:43:42 -0700299 Benchmark* bench,
mtkleinbb6a0282014-07-01 08:43:42 -0700300 double* samples) {
mtkleinbb6a0282014-07-01 08:43:42 -0700301 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700302 int loops = FLAGS_loops;
303 if (kAutoTuneLoops == loops) {
304 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700305 double elapsed = 0;
306 do {
mtklein527930f2014-11-06 08:04:34 -0800307 if (1<<30 == loops) {
308 // We're about to wrap. Something's wrong with the bench.
309 loops = 0;
310 break;
311 }
mtkleina189ccd2014-07-14 12:28:47 -0700312 loops *= 2;
313 // If the GPU lets frames lag at all, we need to make sure we're timing
314 // _this_ round, not still timing last round. We force this by looping
315 // more times than any reasonable GPU will allow frames to lag.
316 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700317 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700318 }
319 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700320
mtkleina189ccd2014-07-14 12:28:47 -0700321 // We've overshot at least a little. Scale back linearly.
322 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700323 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700324
tomhudsond968a6f2015-03-26 11:28:06 -0700325 // Make sure we're not still timing our calibration.
326 target->fence();
reed53249782014-10-10 09:09:52 -0700327 } else {
328 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700329 }
mtkleinbb6a0282014-07-01 08:43:42 -0700330
331 // Pretty much the same deal as the calibration: do some warmup to make
332 // sure we're timing steady-state pipelined frames.
333 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700334 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700335 }
mtkleinbb6a0282014-07-01 08:43:42 -0700336
337 // Now, actually do the timing!
338 for (int i = 0; i < FLAGS_samples; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700339 samples[i] = time(loops, bench, target) / loops;
mtkleinbb6a0282014-07-01 08:43:42 -0700340 }
tomhudsond968a6f2015-03-26 11:28:06 -0700341
mtkleinbb6a0282014-07-01 08:43:42 -0700342 return loops;
343}
mtkleinbb6a0282014-07-01 08:43:42 -0700344
345static SkString to_lower(const char* str) {
346 SkString lower(str);
347 for (size_t i = 0; i < lower.size(); i++) {
348 lower[i] = tolower(lower[i]);
349 }
350 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700351}
352
bsalomonc2553372014-07-22 13:09:05 -0700353static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700354 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700355 if (to_lower(FLAGS_config[i]).equals(name)) {
356 return true;
mtkleinf3723212014-06-25 14:08:00 -0700357 }
358 }
bsalomonc2553372014-07-22 13:09:05 -0700359 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700360}
361
bsalomonc2553372014-07-22 13:09:05 -0700362#if SK_SUPPORT_GPU
363static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
364 int sampleCnt) {
365 if (!is_cpu_config_allowed(name)) {
366 return false;
367 }
krajcevski69a55602014-08-13 10:46:31 -0700368 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700369 return sampleCnt <= ctx->getMaxSampleCount();
370 }
371 return false;
372}
373#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700374
bsalomonc2553372014-07-22 13:09:05 -0700375#if SK_SUPPORT_GPU
376#define kBogusGLContextType GrContextFactory::kNative_GLContextType
377#else
378#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700379#endif
bsalomonc2553372014-07-22 13:09:05 -0700380
381// Append all configs that are enabled and supported.
382static void create_configs(SkTDArray<Config>* configs) {
jvanverth4736e142014-11-07 07:12:46 -0800383 #define CPU_CONFIG(name, backend, color, alpha) \
384 if (is_cpu_config_allowed(#name)) { \
385 Config config = { #name, Benchmark::backend, color, alpha, 0, \
386 kBogusGLContextType, false }; \
387 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700388 }
mtkleine714e752014-07-31 12:13:48 -0700389
mtklein40b32be2014-07-09 08:46:49 -0700390 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700391 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
392 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
393 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700394 }
mtkleinbb6a0282014-07-01 08:43:42 -0700395
396#if SK_SUPPORT_GPU
jvanverth4736e142014-11-07 07:12:46 -0800397 #define GPU_CONFIG(name, ctxType, samples, useDFText) \
bsalomonc2553372014-07-22 13:09:05 -0700398 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
399 Config config = { \
400 #name, \
401 Benchmark::kGPU_Backend, \
402 kN32_SkColorType, \
403 kPremul_SkAlphaType, \
404 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800405 GrContextFactory::ctxType, \
406 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700407 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700408 }
mtkleine714e752014-07-31 12:13:48 -0700409
mtklein40b32be2014-07-09 08:46:49 -0700410 if (FLAGS_gpu) {
jvanverth4736e142014-11-07 07:12:46 -0800411 GPU_CONFIG(gpu, kNative_GLContextType, 0, false)
412 GPU_CONFIG(msaa4, kNative_GLContextType, 4, false)
413 GPU_CONFIG(msaa16, kNative_GLContextType, 16, false)
414 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4, false)
415 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16, false)
416 GPU_CONFIG(gpudft, kNative_GLContextType, 0, true)
417 GPU_CONFIG(debug, kDebug_GLContextType, 0, false)
418 GPU_CONFIG(nullgpu, kNull_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700419#ifdef SK_ANGLE
jvanverth4736e142014-11-07 07:12:46 -0800420 GPU_CONFIG(angle, kANGLE_GLContextType, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700421#endif
mtklein40b32be2014-07-09 08:46:49 -0700422 }
mtkleinbb6a0282014-07-01 08:43:42 -0700423#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700424
425#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
426 if (is_cpu_config_allowed("hwui")) {
427 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
428 kPremul_SkAlphaType, 0, kBogusGLContextType, false };
429 configs->push(config);
430 }
431#endif
mtkleinf3723212014-06-25 14:08:00 -0700432}
433
bsalomonc2553372014-07-22 13:09:05 -0700434// If bench is enabled for config, returns a Target* for it, otherwise NULL.
435static Target* is_enabled(Benchmark* bench, const Config& config) {
436 if (!bench->isSuitableFor(config.backend)) {
437 return NULL;
438 }
439
reede5ea5002014-09-03 11:54:58 -0700440 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
441 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700442
tomhudsond968a6f2015-03-26 11:28:06 -0700443 Target* target = NULL;
bsalomonc2553372014-07-22 13:09:05 -0700444
tomhudsond968a6f2015-03-26 11:28:06 -0700445 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700446#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700447 case Benchmark::kGPU_Backend:
448 target = new GPUTarget(config);
449 break;
bsalomonc2553372014-07-22 13:09:05 -0700450#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700451#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
452 case Benchmark::kHWUI_Backend:
453 target = new HWUITarget(config, bench);
454 break;
455#endif
456 default:
457 target = new Target(config);
458 break;
459 }
bsalomonc2553372014-07-22 13:09:05 -0700460
tomhudsond968a6f2015-03-26 11:28:06 -0700461 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700462 delete target;
463 return NULL;
464 }
465 return target;
466}
467
468// Creates targets for a benchmark and a set of configs.
469static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
470 const SkTDArray<Config>& configs) {
471 for (int i = 0; i < configs.count(); ++i) {
472 if (Target* t = is_enabled(b, configs[i])) {
473 targets->push(t);
474 }
mtkleine714e752014-07-31 12:13:48 -0700475
bsalomonc2553372014-07-22 13:09:05 -0700476 }
477}
478
jcgregoriobf5e5232014-07-17 13:14:16 -0700479
mtkleine714e752014-07-31 12:13:48 -0700480class BenchmarkStream {
481public:
mtklein92007582014-08-01 07:46:52 -0700482 BenchmarkStream() : fBenches(BenchRegistry::Head())
483 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700484 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700485 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800486 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800487 , fCurrentUseMPD(0)
488 , fCurrentImage(0)
489 , fCurrentSubsetImage(0)
490 , fCurrentColorType(0)
491 , fDivisor(2) {
mtklein92007582014-08-01 07:46:52 -0700492 for (int i = 0; i < FLAGS_skps.count(); i++) {
493 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
494 fSKPs.push_back() = FLAGS_skps[i];
495 } else {
496 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
497 SkString path;
498 while (it.next(&path)) {
499 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
500 }
501 }
502 }
mtkleine714e752014-07-31 12:13:48 -0700503
mtklein92007582014-08-01 07:46:52 -0700504 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
505 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
506 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
507 exit(1);
508 }
509
510 for (int i = 0; i < FLAGS_scales.count(); i++) {
511 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
512 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
513 exit(1);
514 }
515 }
robertphillips5b693772014-11-21 06:19:36 -0800516
517 fUseMPDs.push_back() = false;
518 if (FLAGS_mpd) {
519 fUseMPDs.push_back() = true;
520 }
mtklein95553d92015-03-12 08:24:21 -0700521
msarett95f192d2015-02-13 09:05:41 -0800522 // Prepare the images for decoding
523 for (int i = 0; i < FLAGS_images.count(); i++) {
524 const char* flag = FLAGS_images[i];
525 if (sk_isdir(flag)) {
526 // If the value passed in is a directory, add all the images
527 SkOSFile::Iter it(flag);
528 SkString file;
529 while (it.next(&file)) {
530 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
531 }
532 } else if (sk_exists(flag)) {
533 // Also add the value if it is a single image
534 fImages.push_back() = flag;
535 }
536 }
mtklein95553d92015-03-12 08:24:21 -0700537
msarett95f192d2015-02-13 09:05:41 -0800538 // Choose the candidate color types for image decoding
539 const SkColorType colorTypes[] =
540 { kN32_SkColorType, kRGB_565_SkColorType, kAlpha_8_SkColorType };
541 fColorTypes.push_back_n(SK_ARRAY_COUNT(colorTypes), colorTypes);
mtklein92007582014-08-01 07:46:52 -0700542 }
543
mtkleinfd731ce2014-09-10 12:19:30 -0700544 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
545 // Not strictly necessary, as it will be checked again later,
546 // but helps to avoid a lot of pointless work if we're going to skip it.
547 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
548 return false;
549 }
550
scroggoa1193e42015-01-21 12:09:53 -0800551 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
mtkleinfd731ce2014-09-10 12:19:30 -0700552 if (stream.get() == NULL) {
553 SkDebugf("Could not read %s.\n", path);
554 return false;
555 }
556
mtklein57f27bd2015-02-09 11:58:41 -0800557 pic->reset(SkPicture::CreateFromStream(stream.get()));
mtkleinfd731ce2014-09-10 12:19:30 -0700558 if (pic->get() == NULL) {
559 SkDebugf("Could not read %s as an SkPicture.\n", path);
560 return false;
561 }
562 return true;
563 }
564
mtklein92007582014-08-01 07:46:52 -0700565 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700566 if (fBenches) {
567 Benchmark* bench = fBenches->factory()(NULL);
568 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700569 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700570 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700571 return bench;
572 }
mtklein92007582014-08-01 07:46:52 -0700573
mtkleine714e752014-07-31 12:13:48 -0700574 while (fGMs) {
575 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
576 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800577 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700578 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700579 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700580 return SkNEW_ARGS(GMBench, (gm.detach()));
581 }
582 }
mtklein92007582014-08-01 07:46:52 -0700583
mtkleinfd731ce2014-09-10 12:19:30 -0700584 // First add all .skps as RecordingBenches.
585 while (fCurrentRecording < fSKPs.count()) {
586 const SkString& path = fSKPs[fCurrentRecording++];
587 SkAutoTUnref<SkPicture> pic;
588 if (!ReadPicture(path.c_str(), &pic)) {
589 continue;
590 }
591 SkString name = SkOSPath::Basename(path.c_str());
592 fSourceType = "skp";
593 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800594 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800595 fSKPOps = pic->approximateOpCount();
mtkleinfd731ce2014-09-10 12:19:30 -0700596 return SkNEW_ARGS(RecordingBench, (name.c_str(), pic.get(), FLAGS_bbh));
597 }
598
599 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700600 while (fCurrentScale < fScales.count()) {
601 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800602 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700603 SkAutoTUnref<SkPicture> pic;
604 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800605 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700606 continue;
607 }
robertphillips5b693772014-11-21 06:19:36 -0800608
609 while (fCurrentUseMPD < fUseMPDs.count()) {
610 if (FLAGS_bbh) {
611 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
612 SkRTreeFactory factory;
613 SkPictureRecorder recorder;
614 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
615 pic->playback(recorder.beginRecording(pic->cullRect().width(),
616 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800617 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800618 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800619 pic.reset(recorder.endRecording());
620 }
621 SkString name = SkOSPath::Basename(path.c_str());
622 fSourceType = "skp";
623 fBenchType = "playback";
624 return SkNEW_ARGS(SKPBench,
625 (name.c_str(), pic.get(), fClip,
626 fScales[fCurrentScale], fUseMPDs[fCurrentUseMPD++]));
mtklein20840502014-08-21 15:51:22 -0700627 }
robertphillips5b693772014-11-21 06:19:36 -0800628 fCurrentUseMPD = 0;
629 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700630 }
631 fCurrentSKP = 0;
632 fCurrentScale++;
633 }
634
msarett95f192d2015-02-13 09:05:41 -0800635 // Run the DecodingBenches
636 while (fCurrentImage < fImages.count()) {
637 while (fCurrentColorType < fColorTypes.count()) {
638 const SkString& path = fImages[fCurrentImage];
639 SkColorType colorType = fColorTypes[fCurrentColorType];
640 fCurrentColorType++;
641 // Check if the image decodes before creating the benchmark
642 SkBitmap bitmap;
643 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
644 colorType, SkImageDecoder::kDecodePixels_Mode)) {
645 return new DecodingBench(path, colorType);
646 }
647 }
648 fCurrentColorType = 0;
649 fCurrentImage++;
650 }
651
652 // Run the DecodingSubsetBenches
653 while (fCurrentSubsetImage < fImages.count()) {
654 while (fCurrentColorType < fColorTypes.count()) {
655 const SkString& path = fImages[fCurrentSubsetImage];
656 SkColorType colorType = fColorTypes[fCurrentColorType];
657 fCurrentColorType++;
658 // Check if the image decodes before creating the benchmark
659 SkAutoTUnref<SkData> encoded(
660 SkData::NewFromFileName(path.c_str()));
661 SkAutoTDelete<SkMemoryStream> stream(
662 new SkMemoryStream(encoded));
663 SkAutoTDelete<SkImageDecoder>
664 decoder(SkImageDecoder::Factory(stream.get()));
665 if (!decoder) {
666 SkDebugf("Cannot find decoder for %s\n", path.c_str());
667 } else {
668 stream->rewind();
669 int w, h;
670 bool success;
671 if (!decoder->buildTileIndex(stream.detach(), &w, &h)
672 || w*h == 1) {
673 // This is not an error, but in this case we still
674 // do not want to run the benchmark.
675 success = false;
676 } else if (fDivisor > w || fDivisor > h) {
677 SkDebugf("Divisor %d is too big for %s %dx%d\n",
678 fDivisor, path.c_str(), w, h);
679 success = false;
680 } else {
681 const int sW = w / fDivisor;
682 const int sH = h / fDivisor;
683 SkBitmap bitmap;
684 success = true;
685 for (int y = 0; y < h; y += sH) {
686 for (int x = 0; x < w; x += sW) {
687 SkIRect rect = SkIRect::MakeXYWH(x, y, sW, sH);
688 success &= decoder->decodeSubset(&bitmap, rect,
689 colorType);
690 }
691 }
692 }
693 // Create the benchmark if successful
694 if (success) {
695 return new DecodingSubsetBench(path, colorType,
696 fDivisor);
697 }
698 }
699 }
700 fCurrentColorType = 0;
701 fCurrentSubsetImage++;
702 }
703
mtkleine714e752014-07-31 12:13:48 -0700704 return NULL;
705 }
mtklein92007582014-08-01 07:46:52 -0700706
707 void fillCurrentOptions(ResultsWriter* log) const {
708 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700709 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700710 if (0 == strcmp(fSourceType, "skp")) {
711 log->configOption("clip",
712 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
713 fClip.fRight, fClip.fBottom).c_str());
714 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800715 if (fCurrentUseMPD > 0) {
716 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
717 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
718 }
mtklein92007582014-08-01 07:46:52 -0700719 }
mtklein051e56d2014-12-04 08:46:51 -0800720 if (0 == strcmp(fBenchType, "recording")) {
721 log->metric("bytes", fSKPBytes);
722 log->metric("ops", fSKPOps);
723 }
mtklein92007582014-08-01 07:46:52 -0700724 }
725
mtkleine714e752014-07-31 12:13:48 -0700726private:
727 const BenchRegistry* fBenches;
728 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700729 SkIRect fClip;
730 SkTArray<SkScalar> fScales;
731 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800732 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800733 SkTArray<SkString> fImages;
734 SkTArray<SkColorType> fColorTypes;
mtklein92007582014-08-01 07:46:52 -0700735
mtklein051e56d2014-12-04 08:46:51 -0800736 double fSKPBytes, fSKPOps;
737
mtkleinfd731ce2014-09-10 12:19:30 -0700738 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
739 const char* fBenchType; // How we bench it: micro, recording, playback, ...
740 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700741 int fCurrentScale;
742 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800743 int fCurrentUseMPD;
msarett95f192d2015-02-13 09:05:41 -0800744 int fCurrentImage;
745 int fCurrentSubsetImage;
746 int fCurrentColorType;
747 const int fDivisor;
mtkleine714e752014-07-31 12:13:48 -0700748};
749
jcgregorio3b27ade2014-11-13 08:06:40 -0800750int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700751int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800752 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -0700753 SkAutoGraphics ag;
mtklein4f108442014-12-03 13:07:39 -0800754 SkTaskGroup::Enabler enabled;
mtkleinf3723212014-06-25 14:08:00 -0700755
krajcevski69a55602014-08-13 10:46:31 -0700756#if SK_SUPPORT_GPU
krajcevski12b35442014-08-13 12:06:26 -0700757 GrContext::Options grContextOpts;
758 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
759 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (grContextOpts)));
krajcevski69a55602014-08-13 10:46:31 -0700760#endif
761
bsalomon06cddec2014-10-24 10:40:50 -0700762 if (FLAGS_veryVerbose) {
763 FLAGS_verbose = true;
764 }
765
bsalomon6eb03cc2014-08-07 14:28:50 -0700766 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700767 FLAGS_samples = 1;
768 FLAGS_gpuFrameLag = 0;
769 }
770
bsalomon6eb03cc2014-08-07 14:28:50 -0700771 if (!FLAGS_writePath.isEmpty()) {
772 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
773 if (!sk_mkdir(FLAGS_writePath[0])) {
774 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
775 FLAGS_writePath.set(0, NULL);
776 }
777 }
778
mtklein1915b622014-08-20 11:45:00 -0700779 SkAutoTDelete<ResultsWriter> log(SkNEW(ResultsWriter));
mtklein60317d0f2014-07-14 11:30:37 -0700780 if (!FLAGS_outResultsFile.isEmpty()) {
mtklein1915b622014-08-20 11:45:00 -0700781 log.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0])));
mtklein60317d0f2014-07-14 11:30:37 -0700782 }
mtklein1915b622014-08-20 11:45:00 -0700783
784 if (1 == FLAGS_properties.count() % 2) {
785 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
786 return 1;
787 }
788 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
789 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
790 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700791
792 if (1 == FLAGS_key.count() % 2) {
793 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
794 return 1;
795 }
796 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -0700797 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -0700798 }
mtklein60317d0f2014-07-14 11:30:37 -0700799
mtkleinf3723212014-06-25 14:08:00 -0700800 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700801 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400802
mtkleinbb6a0282014-07-01 08:43:42 -0700803 SkAutoTMalloc<double> samples(FLAGS_samples);
804
bsalomon6eb03cc2014-08-07 14:28:50 -0700805 if (kAutoTuneLoops != FLAGS_loops) {
806 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700807 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700808 // No header.
809 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700810 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700811 } else {
qiankun.miao8247ec32014-09-09 19:24:36 -0700812 SkDebugf("maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
813 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -0700814 }
815
bsalomonc2553372014-07-22 13:09:05 -0700816 SkTDArray<Config> configs;
817 create_configs(&configs);
818
mtkleine070c2b2014-10-14 08:40:43 -0700819 int runs = 0;
mtklein92007582014-08-01 07:46:52 -0700820 BenchmarkStream benchStream;
821 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700822 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -0700823 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -0700824 continue;
825 }
826
mtkleinbb6a0282014-07-01 08:43:42 -0700827 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700828 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700829
jcgregoriobf5e5232014-07-17 13:14:16 -0700830 if (!targets.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -0700831 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
jcgregoriobf5e5232014-07-17 13:14:16 -0700832 bench->preDraw();
833 }
mtkleinbb6a0282014-07-01 08:43:42 -0700834 for (int j = 0; j < targets.count(); j++) {
tomhudsond968a6f2015-03-26 11:28:06 -0700835 // During HWUI output this canvas may be NULL.
tomhudson75a0ebb2015-03-27 12:11:44 -0700836 SkCanvas* canvas = targets[j]->getCanvas();
bsalomonc2553372014-07-22 13:09:05 -0700837 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700838
tomhudsond968a6f2015-03-26 11:28:06 -0700839 targets[j]->setup();
robertphillips5b693772014-11-21 06:19:36 -0800840 bench->perCanvasPreDraw(canvas);
841
mtkleinbb6a0282014-07-01 08:43:42 -0700842 const int loops =
tomhudsond968a6f2015-03-26 11:28:06 -0700843 targets[j]->needsFrameTiming()
tomhudson75a0ebb2015-03-27 12:11:44 -0700844 ? gpu_bench(targets[j], bench.get(), samples.get())
845 : cpu_bench(overhead, targets[j], bench.get(), samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700846
robertphillips5b693772014-11-21 06:19:36 -0800847 bench->perCanvasPostDraw(canvas);
848
tomhudsond968a6f2015-03-26 11:28:06 -0700849 if (Benchmark::kNonRendering_Backend != targets[j]->config.backend &&
850 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700851 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -0700852 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -0700853 pngFilename.append(".png");
tomhudsond968a6f2015-03-26 11:28:06 -0700854 write_canvas_png(targets[j], pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -0700855 }
856
857 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700858 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400859 continue;
860 }
861
mtkleinf3723212014-06-25 14:08:00 -0700862 Stats stats(samples.get(), FLAGS_samples);
mtklein1915b622014-08-20 11:45:00 -0700863 log->config(config);
mtklein96289052014-09-10 12:05:59 -0700864 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -0700865 benchStream.fillCurrentOptions(log.get());
tomhudsond968a6f2015-03-26 11:28:06 -0700866 targets[j]->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -0800867 log->metric("min_ms", stats.min);
mtkleine070c2b2014-10-14 08:40:43 -0700868 if (runs++ % FLAGS_flushEvery == 0) {
869 log->flush();
870 }
mtklein60317d0f2014-07-14 11:30:37 -0700871
bsalomon6eb03cc2014-08-07 14:28:50 -0700872 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700873 if (targets.count() == 1) {
874 config = ""; // Only print the config if we run the same bench on more than one.
875 }
mtklein53d25622014-09-18 07:39:42 -0700876 SkDebugf("%4dM\t%s\t%s\n"
mtklein95553d92015-03-12 08:24:21 -0700877 , sk_tools::getBestResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -0700878 , bench->getUniqueName()
879 , config);
mtkleina189ccd2014-07-14 12:28:47 -0700880 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700881 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700882 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700883 }
mtklein96289052014-09-10 12:05:59 -0700884 SkDebugf("%s\n", bench->getUniqueName());
mtkleinf3723212014-06-25 14:08:00 -0700885 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700886 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700887 config = ""; // Only print the config if we run the same bench on more than one.
888 }
mtklein96289052014-09-10 12:05:59 -0700889 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700890 } else {
891 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleinafb43792014-08-19 15:55:55 -0700892 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 -0700893 , sk_tools::getBestResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -0700894 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700895 , HUMANIZE(stats.min)
896 , HUMANIZE(stats.median)
897 , HUMANIZE(stats.mean)
898 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700899 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700900 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700901 , config
mtklein96289052014-09-10 12:05:59 -0700902 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -0700903 );
904 }
bsalomonb12ea412015-02-02 21:19:50 -0800905#if SK_SUPPORT_GPU
906 if (FLAGS_gpuStats &&
bsalomon06cddec2014-10-24 10:40:50 -0700907 Benchmark::kGPU_Backend == targets[j]->config.backend) {
mtklein6838d852014-10-29 14:15:10 -0700908 gGrFactory->get(targets[j]->config.ctxType)->printCacheStats();
bsalomonb12ea412015-02-02 21:19:50 -0800909 gGrFactory->get(targets[j]->config.ctxType)->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -0700910 }
911#endif
mtkleinf3723212014-06-25 14:08:00 -0700912 }
mtkleinbb6a0282014-07-01 08:43:42 -0700913 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400914
bsalomon06cddec2014-10-24 10:40:50 -0700915#if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700916 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700917 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700918 }
919 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700920 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400921 }
bsalomon06cddec2014-10-24 10:40:50 -0700922#endif
mtkleinf3723212014-06-25 14:08:00 -0700923 }
924
mtkleine1091452014-12-04 10:47:02 -0800925 log->bench("memory_usage", 0,0);
926 log->config("meta");
927 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
928
joshualitte0b19d42015-03-26 10:41:02 -0700929#if SK_SUPPORT_GPU
930 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
931 // SkEventTracer destructor
932 gGrFactory.reset(NULL);
933#endif
934
mtkleinf3723212014-06-25 14:08:00 -0700935 return 0;
936}
937
jcgregorio3b27ade2014-11-13 08:06:40 -0800938#if !defined SK_BUILD_FOR_IOS
939int main(int argc, char** argv) {
940 SkCommandLineFlags::Parse(argc, argv);
941 return nanobench_main();
942}
943#endif