blob: b7858983f8ff6cf95bcc367059e6e5d07eb3d757 [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"
msarett7f691442015-09-22 11:56:16 -070013#include "BitmapRegionDecoderBench.h"
scroggo60869a42015-04-01 12:09:17 -070014#include "CodecBench.h"
msarett7f691442015-09-22 11:56:16 -070015#include "CodecBenchPriv.h"
mtkleinf3723212014-06-25 14:08:00 -070016#include "CrashHandler.h"
msarett95f192d2015-02-13 09:05:41 -080017#include "DecodingBench.h"
mtkleine714e752014-07-31 12:13:48 -070018#include "GMBench.h"
mtkleinafb43792014-08-19 15:55:55 -070019#include "ProcStats.h"
mtklein60317d0f2014-07-14 11:30:37 -070020#include "ResultsWriter.h"
mtkleinfd731ce2014-09-10 12:19:30 -070021#include "RecordingBench.h"
joshualitt261c3ad2015-04-27 09:16:57 -070022#include "SKPAnimationBench.h"
mtklein92007582014-08-01 07:46:52 -070023#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070024#include "Stats.h"
mtkleinf3723212014-06-25 14:08:00 -070025
msarett5cb48852015-11-06 08:56:32 -080026#include "SkBitmapRegionDecoder.h"
mtklein6838d852014-10-29 14:15:10 -070027#include "SkBBoxHierarchy.h"
mtkleinf3723212014-06-25 14:08:00 -070028#include "SkCanvas.h"
scroggo60869a42015-04-01 12:09:17 -070029#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070030#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080031#include "SkCommonFlagsConfig.h"
msarett95f192d2015-02-13 09:05:41 -080032#include "SkData.h"
mtkleinf3723212014-06-25 14:08:00 -070033#include "SkForceLinking.h"
34#include "SkGraphics.h"
mtklein20840502014-08-21 15:51:22 -070035#include "SkOSFile.h"
36#include "SkPictureRecorder.h"
mtklein051e56d2014-12-04 08:46:51 -080037#include "SkPictureUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070038#include "SkString.h"
39#include "SkSurface.h"
robertphillips5b693772014-11-21 06:19:36 -080040#include "SkTaskGroup.h"
msarettc149f0e2016-01-04 11:35:43 -080041#include "SkThreadUtils.h"
mtkleinf3723212014-06-25 14:08:00 -070042
bungeman60e0fee2015-08-26 05:15:46 -070043#include <stdlib.h>
44
tomhudsond968a6f2015-03-26 11:28:06 -070045#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
46 #include "nanobenchAndroid.h"
47#endif
48
mtkleinbb6a0282014-07-01 08:43:42 -070049#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070050 #include "gl/GrGLDefines.h"
bsalomon76228632015-05-29 08:02:10 -070051 #include "GrCaps.h"
mtkleinbb6a0282014-07-01 08:43:42 -070052 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070053 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070054#endif
55
bsalomon682c2692015-05-22 14:01:46 -070056 struct GrContextOptions;
57
mtkleinf3723212014-06-25 14:08:00 -070058__SK_FORCE_IMAGE_DECODER_LINKING;
59
reed53249782014-10-10 09:09:52 -070060static const int kAutoTuneLoops = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -070061
mtkleinb5110422014-08-07 15:20:02 -070062static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070063#ifdef SK_DEBUG
64 1;
mtkleina189ccd2014-07-14 12:28:47 -070065#else
bsalomon6eb03cc2014-08-07 14:28:50 -070066 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070067#endif
68
bsalomon6eb03cc2014-08-07 14:28:50 -070069static SkString loops_help_txt() {
70 SkString help;
71 help.printf("Number of times to run each bench. Set this to %d to auto-"
72 "tune for each bench. Timings are only reported when auto-tuning.",
73 kAutoTuneLoops);
74 return help;
75}
76
cdaltone1b89582015-06-25 19:17:08 -070077static SkString to_string(int n) {
78 SkString str;
79 str.appendS32(n);
80 return str;
81}
82
bsalomon6eb03cc2014-08-07 14:28:50 -070083DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
84
mtkleinf3723212014-06-25 14:08:00 -070085DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
mtkleinbbba1682015-10-28 11:36:30 -070086DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
mtkleinf3723212014-06-25 14:08:00 -070087DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
88DEFINE_double(overheadGoal, 0.0001,
89 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070090DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
cdaltond416a5b2015-06-23 13:23:44 -070091DEFINE_int32(gpuFrameLag, 5, "If unknown, estimated maximum number of frames GPU allows to lag.");
krajcevski12b35442014-08-13 12:06:26 -070092DEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
93 "software path rendering.");
mtkleinf3723212014-06-25 14:08:00 -070094
mtklein60317d0f2014-07-14 11:30:37 -070095DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070096DEFINE_int32(maxCalibrationAttempts, 3,
97 "Try up to this many times to guess loops for a bench, or skip the bench.");
98DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
mtklein92007582014-08-01 07:46:52 -070099DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
100DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
cdalton63a82852015-06-29 14:06:10 -0700101DEFINE_string(zoom, "1.0,0", "Comma-separated zoomMax,zoomPeriodMs factors for a periodic SKP zoom "
102 "function that ping-pongs between 1.0 and zoomMax.");
mtklein20840502014-08-21 15:51:22 -0700103DEFINE_bool(bbh, true, "Build a BBH for SKPs?");
robertphillips5b693772014-11-21 06:19:36 -0800104DEFINE_bool(mpd, true, "Use MultiPictureDraw for the SKPs?");
cdaltonb4022962015-06-25 10:51:56 -0700105DEFINE_bool(loopSKP, true, "Loop SKPs like we do for micro benches?");
mtkleine070c2b2014-10-14 08:40:43 -0700106DEFINE_int32(flushEvery, 10, "Flush --outResultsFile every Nth run.");
mtklein55e88b22015-01-21 15:50:13 -0800107DEFINE_bool(resetGpuContext, true, "Reset the GrContext before running each test.");
bsalomonb12ea412015-02-02 21:19:50 -0800108DEFINE_bool(gpuStats, false, "Print GPU stats after each gpu benchmark?");
joshualitte45c81c2015-12-02 09:05:37 -0800109DEFINE_bool(gpuStatsDump, false, "Dump GPU states after each benchmark to json");
msarettc149f0e2016-01-04 11:35:43 -0800110DEFINE_bool(keepAlive, false, "Print a message every so often so that we don't time out");
mtklein92007582014-08-01 07:46:52 -0700111
mtkleinbbba1682015-10-28 11:36:30 -0700112static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
113
mtkleinf3723212014-06-25 14:08:00 -0700114static SkString humanize(double ms) {
mtkleindc5bbab2014-09-24 06:34:09 -0700115 if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
mtklein748ca3b2015-01-15 10:56:12 -0800116 return HumanizeMs(ms);
mtkleinf3723212014-06-25 14:08:00 -0700117}
mtklein55b0ffc2014-07-17 08:38:23 -0700118#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -0700119
tomhudsond968a6f2015-03-26 11:28:06 -0700120bool Target::init(SkImageInfo info, Benchmark* bench) {
121 if (Benchmark::kRaster_Backend == config.backend) {
122 this->surface.reset(SkSurface::NewRaster(info));
123 if (!this->surface.get()) {
124 return false;
125 }
126 }
127 return true;
128}
129bool Target::capturePixels(SkBitmap* bmp) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700130 SkCanvas* canvas = this->getCanvas();
tomhudsond968a6f2015-03-26 11:28:06 -0700131 if (!canvas) {
132 return false;
133 }
134 bmp->setInfo(canvas->imageInfo());
135 if (!canvas->readPixels(bmp, 0, 0)) {
136 SkDebugf("Can't read canvas pixels.\n");
137 return false;
138 }
139 return true;
140}
141
142#if SK_SUPPORT_GPU
143struct GPUTarget : public Target {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 explicit GPUTarget(const Config& c) : Target(c), gl(nullptr) { }
tomhudsond968a6f2015-03-26 11:28:06 -0700145 SkGLContext* gl;
146
147 void setup() override {
148 this->gl->makeCurrent();
149 // Make sure we're done with whatever came before.
150 SK_GL(*this->gl, Finish());
151 }
152 void endTiming() override {
153 if (this->gl) {
154 SK_GL(*this->gl, Flush());
155 this->gl->swapBuffers();
156 }
157 }
158 void fence() override {
159 SK_GL(*this->gl, Finish());
160 }
mtkleind75c4662015-04-30 07:11:22 -0700161
cdaltond416a5b2015-06-23 13:23:44 -0700162 bool needsFrameTiming(int* maxFrameLag) const override {
163 if (!this->gl->getMaxGpuFrameLag(maxFrameLag)) {
164 // Frame lag is unknown.
165 *maxFrameLag = FLAGS_gpuFrameLag;
166 }
167 return true;
168 }
tomhudsond968a6f2015-03-26 11:28:06 -0700169 bool init(SkImageInfo info, Benchmark* bench) override {
bsalomonafcd7cd2015-08-31 12:39:41 -0700170 uint32_t flags = this->config.useDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag :
171 0;
tomhudsond968a6f2015-03-26 11:28:06 -0700172 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
kkinnunen5219fd92015-12-10 06:28:13 -0800173 this->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800174 this->config.ctxOptions),
tomhudsond968a6f2015-03-26 11:28:06 -0700175 SkSurface::kNo_Budgeted, info,
176 this->config.samples, &props));
kkinnunen3e980c32015-12-23 01:33:00 -0800177 this->gl = gGrFactory->getContextInfo(this->config.ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800178 this->config.ctxOptions)->fGLContext;
tomhudsond968a6f2015-03-26 11:28:06 -0700179 if (!this->surface.get()) {
180 return false;
181 }
cdaltond416a5b2015-06-23 13:23:44 -0700182 if (!this->gl->fenceSyncSupport()) {
183 SkDebugf("WARNING: GL context for config \"%s\" does not support fence sync. "
184 "Timings might not be accurate.\n", this->config.name);
185 }
tomhudsond968a6f2015-03-26 11:28:06 -0700186 return true;
187 }
188 void fillOptions(ResultsWriter* log) override {
189 const GrGLubyte* version;
190 SK_GL_RET(*this->gl, version, GetString(GR_GL_VERSION));
191 log->configOption("GL_VERSION", (const char*)(version));
192
193 SK_GL_RET(*this->gl, version, GetString(GR_GL_RENDERER));
194 log->configOption("GL_RENDERER", (const char*) version);
195
196 SK_GL_RET(*this->gl, version, GetString(GR_GL_VENDOR));
197 log->configOption("GL_VENDOR", (const char*) version);
198
199 SK_GL_RET(*this->gl, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
200 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
201 }
202};
mtkleind75c4662015-04-30 07:11:22 -0700203
tomhudsond968a6f2015-03-26 11:28:06 -0700204#endif
205
tomhudson75a0ebb2015-03-27 12:11:44 -0700206static double time(int loops, Benchmark* bench, Target* target) {
207 SkCanvas* canvas = target->getCanvas();
bsalomon6eb03cc2014-08-07 14:28:50 -0700208 if (canvas) {
209 canvas->clear(SK_ColorWHITE);
210 }
joshualitt8a6697a2015-09-30 12:11:07 -0700211 bench->preDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700212 double start = now_ms();
tomhudson75a0ebb2015-03-27 12:11:44 -0700213 canvas = target->beginTiming(canvas);
214 bench->draw(loops, canvas);
mtkleinbb6a0282014-07-01 08:43:42 -0700215 if (canvas) {
216 canvas->flush();
217 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700218 target->endTiming();
mtkleinbbba1682015-10-28 11:36:30 -0700219 double elapsed = now_ms() - start;
joshualitt8a6697a2015-09-30 12:11:07 -0700220 bench->postDraw(canvas);
mtkleinbbba1682015-10-28 11:36:30 -0700221 return elapsed;
mtkleinbb6a0282014-07-01 08:43:42 -0700222}
223
mtkleinf3723212014-06-25 14:08:00 -0700224static double estimate_timer_overhead() {
225 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700226 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbbba1682015-10-28 11:36:30 -0700227 double start = now_ms();
228 overhead += now_ms() - start;
mtkleinf3723212014-06-25 14:08:00 -0700229 }
230 return overhead / FLAGS_overheadLoops;
231}
232
reed53249782014-10-10 09:09:52 -0700233static int detect_forever_loops(int loops) {
234 // look for a magic run-forever value
235 if (loops < 0) {
236 loops = SK_MaxS32;
237 }
238 return loops;
239}
240
mtklein55b0ffc2014-07-17 08:38:23 -0700241static int clamp_loops(int loops) {
242 if (loops < 1) {
mtklein527930f2014-11-06 08:04:34 -0800243 SkDebugf("ERROR: clamping loops from %d to 1. "
244 "There's probably something wrong with the bench.\n", loops);
mtklein55b0ffc2014-07-17 08:38:23 -0700245 return 1;
246 }
247 if (loops > FLAGS_maxLoops) {
248 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
249 return FLAGS_maxLoops;
250 }
251 return loops;
252}
253
tomhudsond968a6f2015-03-26 11:28:06 -0700254static bool write_canvas_png(Target* target, const SkString& filename) {
255
bsalomon6eb03cc2014-08-07 14:28:50 -0700256 if (filename.isEmpty()) {
257 return false;
258 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700259 if (target->getCanvas() &&
260 kUnknown_SkColorType == target->getCanvas()->imageInfo().colorType()) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700261 return false;
262 }
tomhudsond968a6f2015-03-26 11:28:06 -0700263
bsalomon6eb03cc2014-08-07 14:28:50 -0700264 SkBitmap bmp;
tomhudsond968a6f2015-03-26 11:28:06 -0700265
266 if (!target->capturePixels(&bmp)) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700267 return false;
268 }
tomhudsond968a6f2015-03-26 11:28:06 -0700269
bsalomon6eb03cc2014-08-07 14:28:50 -0700270 SkString dir = SkOSPath::Dirname(filename.c_str());
271 if (!sk_mkdir(dir.c_str())) {
272 SkDebugf("Can't make dir %s.\n", dir.c_str());
273 return false;
274 }
275 SkFILEWStream stream(filename.c_str());
276 if (!stream.isValid()) {
277 SkDebugf("Can't write %s.\n", filename.c_str());
278 return false;
279 }
280 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
281 SkDebugf("Can't encode a PNG.\n");
282 return false;
283 }
284 return true;
285}
286
287static int kFailedLoops = -2;
cdaltone1b89582015-06-25 19:17:08 -0700288static int setup_cpu_bench(const double overhead, Target* target, Benchmark* bench) {
mtkleinbb6a0282014-07-01 08:43:42 -0700289 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700290 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700291 int round = 0;
cdaltonb4022962015-06-25 10:51:56 -0700292 int loops = bench->calculateLoops(FLAGS_loops);
293 if (kAutoTuneLoops == loops) {
bsalomon6eb03cc2014-08-07 14:28:50 -0700294 while (bench_plus_overhead < overhead) {
295 if (round++ == FLAGS_maxCalibrationAttempts) {
296 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
mtklein96289052014-09-10 12:05:59 -0700297 bench->getUniqueName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
bsalomon6eb03cc2014-08-07 14:28:50 -0700298 return kFailedLoops;
299 }
tomhudson75a0ebb2015-03-27 12:11:44 -0700300 bench_plus_overhead = time(1, bench, target);
mtklein55b0ffc2014-07-17 08:38:23 -0700301 }
mtklein2069e222014-08-04 13:57:39 -0700302 }
mtkleinf3723212014-06-25 14:08:00 -0700303
mtkleinbb6a0282014-07-01 08:43:42 -0700304 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700305 // We'll pick N to make timer overhead negligible:
306 //
mtkleinbb6a0282014-07-01 08:43:42 -0700307 // overhead
308 // ------------------------- < FLAGS_overheadGoal
309 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700310 //
mtkleinbb6a0282014-07-01 08:43:42 -0700311 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700312 //
313 // Doing some math, we get:
314 //
mtkleinbb6a0282014-07-01 08:43:42 -0700315 // (overhead / FLAGS_overheadGoal) - overhead
316 // ------------------------------------------ < N
317 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700318 //
319 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700320 if (kAutoTuneLoops == loops) {
321 const double numer = overhead / FLAGS_overheadGoal - overhead;
322 const double denom = bench_plus_overhead - overhead;
323 loops = (int)ceil(numer / denom);
reed53249782014-10-10 09:09:52 -0700324 loops = clamp_loops(loops);
325 } else {
326 loops = detect_forever_loops(loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700327 }
mtkleinbb6a0282014-07-01 08:43:42 -0700328
mtkleinbb6a0282014-07-01 08:43:42 -0700329 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700330}
331
cdaltone1b89582015-06-25 19:17:08 -0700332static int setup_gpu_bench(Target* target, Benchmark* bench, int maxGpuFrameLag) {
mtkleinbb6a0282014-07-01 08:43:42 -0700333 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
cdaltonb4022962015-06-25 10:51:56 -0700334 int loops = bench->calculateLoops(FLAGS_loops);
bsalomon6eb03cc2014-08-07 14:28:50 -0700335 if (kAutoTuneLoops == loops) {
336 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700337 double elapsed = 0;
338 do {
mtklein527930f2014-11-06 08:04:34 -0800339 if (1<<30 == loops) {
340 // We're about to wrap. Something's wrong with the bench.
341 loops = 0;
342 break;
343 }
mtkleina189ccd2014-07-14 12:28:47 -0700344 loops *= 2;
345 // If the GPU lets frames lag at all, we need to make sure we're timing
cdaltond416a5b2015-06-23 13:23:44 -0700346 // _this_ round, not still timing last round.
347 for (int i = 0; i < maxGpuFrameLag; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700348 elapsed = time(loops, bench, target);
mtkleina189ccd2014-07-14 12:28:47 -0700349 }
350 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700351
mtkleina189ccd2014-07-14 12:28:47 -0700352 // We've overshot at least a little. Scale back linearly.
353 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
reed53249782014-10-10 09:09:52 -0700354 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700355
tomhudsond968a6f2015-03-26 11:28:06 -0700356 // Make sure we're not still timing our calibration.
357 target->fence();
reed53249782014-10-10 09:09:52 -0700358 } else {
359 loops = detect_forever_loops(loops);
mtkleina189ccd2014-07-14 12:28:47 -0700360 }
mtkleinbb6a0282014-07-01 08:43:42 -0700361
362 // Pretty much the same deal as the calibration: do some warmup to make
363 // sure we're timing steady-state pipelined frames.
cdaltond416a5b2015-06-23 13:23:44 -0700364 for (int i = 0; i < maxGpuFrameLag - 1; i++) {
tomhudson75a0ebb2015-03-27 12:11:44 -0700365 time(loops, bench, target);
mtkleinf3723212014-06-25 14:08:00 -0700366 }
mtkleinbb6a0282014-07-01 08:43:42 -0700367
mtkleinbb6a0282014-07-01 08:43:42 -0700368 return loops;
369}
mtkleinbb6a0282014-07-01 08:43:42 -0700370
371static SkString to_lower(const char* str) {
372 SkString lower(str);
373 for (size_t i = 0; i < lower.size(); i++) {
374 lower[i] = tolower(lower[i]);
375 }
376 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700377}
378
bsalomonc2553372014-07-22 13:09:05 -0700379static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700380 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700381 if (to_lower(FLAGS_config[i]).equals(name)) {
382 return true;
mtkleinf3723212014-06-25 14:08:00 -0700383 }
384 }
bsalomonc2553372014-07-22 13:09:05 -0700385 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700386}
387
bsalomonc2553372014-07-22 13:09:05 -0700388#if SK_SUPPORT_GPU
389static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
kkinnunen5219fd92015-12-10 06:28:13 -0800390 GrContextFactory::GLContextOptions ctxOptions,
bsalomonc2553372014-07-22 13:09:05 -0700391 int sampleCnt) {
392 if (!is_cpu_config_allowed(name)) {
393 return false;
394 }
kkinnunen3e980c32015-12-23 01:33:00 -0800395 if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
bsalomon76228632015-05-29 08:02:10 -0700396 return sampleCnt <= ctx->caps()->maxSampleCount();
bsalomonc2553372014-07-22 13:09:05 -0700397 }
398 return false;
399}
400#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700401
bsalomonc2553372014-07-22 13:09:05 -0700402#if SK_SUPPORT_GPU
403#define kBogusGLContextType GrContextFactory::kNative_GLContextType
kkinnunen5219fd92015-12-10 06:28:13 -0800404#define kBogusGLContextOptions GrContextFactory::kNone_GLContextOptions
bsalomonc2553372014-07-22 13:09:05 -0700405#else
406#define kBogusGLContextType 0
kkinnunen5219fd92015-12-10 06:28:13 -0800407#define kBogusGLContextOptions 0
mtkleine714e752014-07-31 12:13:48 -0700408#endif
bsalomonc2553372014-07-22 13:09:05 -0700409
410// Append all configs that are enabled and supported.
411static void create_configs(SkTDArray<Config>* configs) {
kkinnunen5219fd92015-12-10 06:28:13 -0800412 #define CPU_CONFIG(name, backend, color, alpha) \
413 if (is_cpu_config_allowed(#name)) { \
414 Config config = { #name, Benchmark::backend, color, alpha, 0, \
415 kBogusGLContextType, kBogusGLContextOptions, \
416 false }; \
417 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700418 }
mtkleine714e752014-07-31 12:13:48 -0700419
mtklein40b32be2014-07-09 08:46:49 -0700420 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700421 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
422 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
423 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700424 }
mtkleinbb6a0282014-07-01 08:43:42 -0700425
426#if SK_SUPPORT_GPU
kkinnunen5219fd92015-12-10 06:28:13 -0800427 #define GPU_CONFIG(name, ctxType, ctxOptions, samples, useDFText) \
428 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, \
429 GrContextFactory::ctxOptions, samples)) { \
bsalomonc2553372014-07-22 13:09:05 -0700430 Config config = { \
431 #name, \
432 Benchmark::kGPU_Backend, \
433 kN32_SkColorType, \
434 kPremul_SkAlphaType, \
435 samples, \
jvanverth4736e142014-11-07 07:12:46 -0800436 GrContextFactory::ctxType, \
kkinnunen5219fd92015-12-10 06:28:13 -0800437 GrContextFactory::ctxOptions, \
jvanverth4736e142014-11-07 07:12:46 -0800438 useDFText }; \
bsalomonc2553372014-07-22 13:09:05 -0700439 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700440 }
mtkleine714e752014-07-31 12:13:48 -0700441
mtklein40b32be2014-07-09 08:46:49 -0700442 if (FLAGS_gpu) {
kkinnunen5219fd92015-12-10 06:28:13 -0800443 GPU_CONFIG(gpu, kNative_GLContextType, kNone_GLContextOptions, 0, false)
444 GPU_CONFIG(msaa4, kNative_GLContextType, kNone_GLContextOptions, 4, false)
445 GPU_CONFIG(msaa16, kNative_GLContextType, kNone_GLContextOptions, 16, false)
446 GPU_CONFIG(nvprmsaa4, kNative_GLContextType, kEnableNVPR_GLContextOptions, 4, false)
447 GPU_CONFIG(nvprmsaa16, kNative_GLContextType, kEnableNVPR_GLContextOptions, 16, false)
448 GPU_CONFIG(gpudft, kNative_GLContextType, kNone_GLContextOptions, 0, true)
449 GPU_CONFIG(debug, kDebug_GLContextType, kNone_GLContextOptions, 0, false)
450 GPU_CONFIG(nullgpu, kNull_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800451#if SK_ANGLE
452#ifdef SK_BUILD_FOR_WIN
kkinnunen5219fd92015-12-10 06:28:13 -0800453 GPU_CONFIG(angle, kANGLE_GLContextType, kNone_GLContextOptions, 0, false)
kkinnunen3e980c32015-12-23 01:33:00 -0800454#endif
kkinnunen5219fd92015-12-10 06:28:13 -0800455 GPU_CONFIG(angle-gl, kANGLE_GL_GLContextType, kNone_GLContextOptions, 0, false)
bsalomon3b4d0772014-08-06 10:52:33 -0700456#endif
kkinnunen3e980c32015-12-23 01:33:00 -0800457#if SK_COMMAND_BUFFER
kkinnunen5219fd92015-12-10 06:28:13 -0800458 GPU_CONFIG(commandbuffer, kCommandBuffer_GLContextType, kNone_GLContextOptions, 0, false)
hendrikw885bf092015-08-27 10:38:39 -0700459#endif
cdaltond416a5b2015-06-23 13:23:44 -0700460#if SK_MESA
kkinnunen5219fd92015-12-10 06:28:13 -0800461 GPU_CONFIG(mesa, kMESA_GLContextType, kNone_GLContextOptions, 0, false)
cdaltond416a5b2015-06-23 13:23:44 -0700462#endif
mtklein40b32be2014-07-09 08:46:49 -0700463 }
mtkleinbb6a0282014-07-01 08:43:42 -0700464#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700465
466#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
467 if (is_cpu_config_allowed("hwui")) {
468 Config config = { "hwui", Benchmark::kHWUI_Backend, kRGBA_8888_SkColorType,
kkinnunen5219fd92015-12-10 06:28:13 -0800469 kPremul_SkAlphaType, 0, kBogusGLContextType, kBogusGLContextOptions,
470 false };
tomhudsond968a6f2015-03-26 11:28:06 -0700471 configs->push(config);
472 }
473#endif
mtkleinf3723212014-06-25 14:08:00 -0700474}
475
halcanary96fcdcc2015-08-27 07:41:13 -0700476// If bench is enabled for config, returns a Target* for it, otherwise nullptr.
bsalomonc2553372014-07-22 13:09:05 -0700477static Target* is_enabled(Benchmark* bench, const Config& config) {
478 if (!bench->isSuitableFor(config.backend)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700479 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700480 }
481
reede5ea5002014-09-03 11:54:58 -0700482 SkImageInfo info = SkImageInfo::Make(bench->getSize().fX, bench->getSize().fY,
483 config.color, config.alpha);
bsalomonc2553372014-07-22 13:09:05 -0700484
halcanary96fcdcc2015-08-27 07:41:13 -0700485 Target* target = nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700486
tomhudsond968a6f2015-03-26 11:28:06 -0700487 switch (config.backend) {
bsalomonc2553372014-07-22 13:09:05 -0700488#if SK_SUPPORT_GPU
tomhudsond968a6f2015-03-26 11:28:06 -0700489 case Benchmark::kGPU_Backend:
490 target = new GPUTarget(config);
491 break;
bsalomonc2553372014-07-22 13:09:05 -0700492#endif
tomhudsond968a6f2015-03-26 11:28:06 -0700493#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
494 case Benchmark::kHWUI_Backend:
495 target = new HWUITarget(config, bench);
496 break;
497#endif
498 default:
499 target = new Target(config);
500 break;
501 }
bsalomonc2553372014-07-22 13:09:05 -0700502
tomhudsond968a6f2015-03-26 11:28:06 -0700503 if (!target->init(info, bench)) {
bsalomonc2553372014-07-22 13:09:05 -0700504 delete target;
halcanary96fcdcc2015-08-27 07:41:13 -0700505 return nullptr;
bsalomonc2553372014-07-22 13:09:05 -0700506 }
507 return target;
508}
509
msarett5cb48852015-11-06 08:56:32 -0800510static bool valid_brd_bench(SkData* encoded, SkBitmapRegionDecoder::Strategy strategy,
msarett7f691442015-09-22 11:56:16 -0700511 SkColorType colorType, uint32_t sampleSize, uint32_t minOutputSize, int* width,
512 int* height) {
msarett5cb48852015-11-06 08:56:32 -0800513 SkAutoTDelete<SkBitmapRegionDecoder> brd(
514 SkBitmapRegionDecoder::Create(encoded, strategy));
msarett7f691442015-09-22 11:56:16 -0700515 if (nullptr == brd.get()) {
516 // This is indicates that subset decoding is not supported for a particular image format.
517 return false;
518 }
519
msarett35e5d1b2015-10-27 12:50:25 -0700520 SkBitmap bitmap;
521 if (!brd->decodeRegion(&bitmap, nullptr, SkIRect::MakeXYWH(0, 0, brd->width(), brd->height()),
522 1, colorType, false)) {
523 return false;
524 }
msarett7f691442015-09-22 11:56:16 -0700525
526 if (sampleSize * minOutputSize > (uint32_t) brd->width() || sampleSize * minOutputSize >
527 (uint32_t) brd->height()) {
528 // This indicates that the image is not large enough to decode a
529 // minOutputSize x minOutputSize subset at the given sampleSize.
530 return false;
531 }
532
533 // Set the image width and height. The calling code will use this to choose subsets to decode.
534 *width = brd->width();
535 *height = brd->height();
536 return true;
537}
538
egdaniel3bf92062015-06-26 08:12:46 -0700539static void cleanup_run(Target* target) {
halcanary385fe4d2015-08-26 13:07:48 -0700540 delete target;
egdaniel3bf92062015-06-26 08:12:46 -0700541#if SK_SUPPORT_GPU
542 if (FLAGS_abandonGpuContext) {
543 gGrFactory->abandonContexts();
544 }
545 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
546 gGrFactory->destroyContexts();
547 }
548#endif
549}
550
mtkleine714e752014-07-31 12:13:48 -0700551class BenchmarkStream {
552public:
mtklein92007582014-08-01 07:46:52 -0700553 BenchmarkStream() : fBenches(BenchRegistry::Head())
554 , fGMs(skiagm::GMRegistry::Head())
mtkleinfd731ce2014-09-10 12:19:30 -0700555 , fCurrentRecording(0)
mtklein92007582014-08-01 07:46:52 -0700556 , fCurrentScale(0)
robertphillips5b693772014-11-21 06:19:36 -0800557 , fCurrentSKP(0)
msarett95f192d2015-02-13 09:05:41 -0800558 , fCurrentUseMPD(0)
scroggo60869a42015-04-01 12:09:17 -0700559 , fCurrentCodec(0)
msarett95f192d2015-02-13 09:05:41 -0800560 , fCurrentImage(0)
msarett7f691442015-09-22 11:56:16 -0700561 , fCurrentBRDImage(0)
msarett95f192d2015-02-13 09:05:41 -0800562 , fCurrentColorType(0)
msarettb23e6aa2015-06-09 13:56:10 -0700563 , fCurrentSubsetType(0)
msarett7f691442015-09-22 11:56:16 -0700564 , fCurrentBRDStrategy(0)
565 , fCurrentBRDSampleSize(0)
msarettb23e6aa2015-06-09 13:56:10 -0700566 , fCurrentAnimSKP(0) {
mtklein92007582014-08-01 07:46:52 -0700567 for (int i = 0; i < FLAGS_skps.count(); i++) {
568 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
569 fSKPs.push_back() = FLAGS_skps[i];
570 } else {
571 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
572 SkString path;
573 while (it.next(&path)) {
574 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
575 }
576 }
577 }
mtkleine714e752014-07-31 12:13:48 -0700578
mtklein92007582014-08-01 07:46:52 -0700579 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
580 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
581 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
582 exit(1);
583 }
584
585 for (int i = 0; i < FLAGS_scales.count(); i++) {
586 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
587 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
588 exit(1);
589 }
590 }
robertphillips5b693772014-11-21 06:19:36 -0800591
cdalton63a82852015-06-29 14:06:10 -0700592 if (2 != sscanf(FLAGS_zoom[0], "%f,%lf", &fZoomMax, &fZoomPeriodMs)) {
593 SkDebugf("Can't parse %s from --zoom as a zoomMax,zoomPeriodMs.\n", FLAGS_zoom[0]);
joshualitt261c3ad2015-04-27 09:16:57 -0700594 exit(1);
595 }
596
robertphillips5b693772014-11-21 06:19:36 -0800597 if (FLAGS_mpd) {
598 fUseMPDs.push_back() = true;
599 }
mtkleinc751ecb2015-06-15 08:56:38 -0700600 fUseMPDs.push_back() = false;
mtklein95553d92015-03-12 08:24:21 -0700601
msarett95f192d2015-02-13 09:05:41 -0800602 // Prepare the images for decoding
603 for (int i = 0; i < FLAGS_images.count(); i++) {
604 const char* flag = FLAGS_images[i];
605 if (sk_isdir(flag)) {
606 // If the value passed in is a directory, add all the images
607 SkOSFile::Iter it(flag);
608 SkString file;
609 while (it.next(&file)) {
610 fImages.push_back() = SkOSPath::Join(flag, file.c_str());
611 }
612 } else if (sk_exists(flag)) {
613 // Also add the value if it is a single image
614 fImages.push_back() = flag;
615 }
616 }
mtklein95553d92015-03-12 08:24:21 -0700617
msarett95f192d2015-02-13 09:05:41 -0800618 // Choose the candidate color types for image decoding
619 const SkColorType colorTypes[] =
msarettb23e6aa2015-06-09 13:56:10 -0700620 { kN32_SkColorType,
621 kRGB_565_SkColorType,
622 kAlpha_8_SkColorType,
623 kIndex_8_SkColorType,
624 kGray_8_SkColorType };
msarett74deb982015-10-20 16:45:56 -0700625 fColorTypes.reset(colorTypes, SK_ARRAY_COUNT(colorTypes));
mtklein92007582014-08-01 07:46:52 -0700626 }
627
mtkleinfd731ce2014-09-10 12:19:30 -0700628 static bool ReadPicture(const char* path, SkAutoTUnref<SkPicture>* pic) {
629 // Not strictly necessary, as it will be checked again later,
630 // but helps to avoid a lot of pointless work if we're going to skip it.
631 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path)) {
632 return false;
633 }
634
scroggoa1193e42015-01-21 12:09:53 -0800635 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -0700636 if (stream.get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700637 SkDebugf("Could not read %s.\n", path);
638 return false;
639 }
640
mtklein57f27bd2015-02-09 11:58:41 -0800641 pic->reset(SkPicture::CreateFromStream(stream.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700642 if (pic->get() == nullptr) {
mtkleinfd731ce2014-09-10 12:19:30 -0700643 SkDebugf("Could not read %s as an SkPicture.\n", path);
644 return false;
645 }
646 return true;
647 }
648
mtklein92007582014-08-01 07:46:52 -0700649 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700650 if (fBenches) {
halcanary96fcdcc2015-08-27 07:41:13 -0700651 Benchmark* bench = fBenches->factory()(nullptr);
mtkleine714e752014-07-31 12:13:48 -0700652 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700653 fSourceType = "bench";
mtkleinfd731ce2014-09-10 12:19:30 -0700654 fBenchType = "micro";
mtkleine714e752014-07-31 12:13:48 -0700655 return bench;
656 }
mtklein92007582014-08-01 07:46:52 -0700657
mtkleine714e752014-07-31 12:13:48 -0700658 while (fGMs) {
halcanary96fcdcc2015-08-27 07:41:13 -0700659 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(nullptr));
mtkleine714e752014-07-31 12:13:48 -0700660 fGMs = fGMs->next();
mtkleincf5d9c92015-01-23 10:31:45 -0800661 if (gm->runAsBench()) {
mtklein92007582014-08-01 07:46:52 -0700662 fSourceType = "gm";
mtkleinfd731ce2014-09-10 12:19:30 -0700663 fBenchType = "micro";
halcanary385fe4d2015-08-26 13:07:48 -0700664 return new GMBench(gm.detach());
mtkleine714e752014-07-31 12:13:48 -0700665 }
666 }
mtklein92007582014-08-01 07:46:52 -0700667
mtkleinfd731ce2014-09-10 12:19:30 -0700668 // First add all .skps as RecordingBenches.
669 while (fCurrentRecording < fSKPs.count()) {
670 const SkString& path = fSKPs[fCurrentRecording++];
671 SkAutoTUnref<SkPicture> pic;
672 if (!ReadPicture(path.c_str(), &pic)) {
673 continue;
674 }
675 SkString name = SkOSPath::Basename(path.c_str());
676 fSourceType = "skp";
677 fBenchType = "recording";
bsalomon0aa5cea2014-12-15 09:13:35 -0800678 fSKPBytes = static_cast<double>(SkPictureUtils::ApproximateBytesUsed(pic));
mtklein051e56d2014-12-04 08:46:51 -0800679 fSKPOps = pic->approximateOpCount();
halcanary385fe4d2015-08-26 13:07:48 -0700680 return new RecordingBench(name.c_str(), pic.get(), FLAGS_bbh);
mtkleinfd731ce2014-09-10 12:19:30 -0700681 }
682
683 // Then once each for each scale as SKPBenches (playback).
mtklein92007582014-08-01 07:46:52 -0700684 while (fCurrentScale < fScales.count()) {
685 while (fCurrentSKP < fSKPs.count()) {
robertphillips5b693772014-11-21 06:19:36 -0800686 const SkString& path = fSKPs[fCurrentSKP];
mtkleinfd731ce2014-09-10 12:19:30 -0700687 SkAutoTUnref<SkPicture> pic;
688 if (!ReadPicture(path.c_str(), &pic)) {
robertphillips5b693772014-11-21 06:19:36 -0800689 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700690 continue;
691 }
robertphillips5b693772014-11-21 06:19:36 -0800692
693 while (fCurrentUseMPD < fUseMPDs.count()) {
694 if (FLAGS_bbh) {
695 // The SKP we read off disk doesn't have a BBH. Re-record so it grows one.
696 SkRTreeFactory factory;
697 SkPictureRecorder recorder;
698 static const int kFlags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag;
699 pic->playback(recorder.beginRecording(pic->cullRect().width(),
700 pic->cullRect().height(),
mtklein748ca3b2015-01-15 10:56:12 -0800701 &factory,
robertphillipse451c4d2014-12-09 10:28:00 -0800702 fUseMPDs[fCurrentUseMPD] ? kFlags : 0));
robertphillips5b693772014-11-21 06:19:36 -0800703 pic.reset(recorder.endRecording());
704 }
705 SkString name = SkOSPath::Basename(path.c_str());
706 fSourceType = "skp";
707 fBenchType = "playback";
halcanary385fe4d2015-08-26 13:07:48 -0700708 return new SKPBench(name.c_str(), pic.get(), fClip, fScales[fCurrentScale],
709 fUseMPDs[fCurrentUseMPD++], FLAGS_loopSKP);
mtklein20840502014-08-21 15:51:22 -0700710 }
robertphillips5b693772014-11-21 06:19:36 -0800711 fCurrentUseMPD = 0;
712 fCurrentSKP++;
mtklein92007582014-08-01 07:46:52 -0700713 }
714 fCurrentSKP = 0;
715 fCurrentScale++;
716 }
717
joshualitt261c3ad2015-04-27 09:16:57 -0700718 // Now loop over each skp again if we have an animation
cdalton63a82852015-06-29 14:06:10 -0700719 if (fZoomMax != 1.0f && fZoomPeriodMs > 0) {
joshualitt261c3ad2015-04-27 09:16:57 -0700720 while (fCurrentAnimSKP < fSKPs.count()) {
721 const SkString& path = fSKPs[fCurrentAnimSKP];
722 SkAutoTUnref<SkPicture> pic;
723 if (!ReadPicture(path.c_str(), &pic)) {
724 fCurrentAnimSKP++;
725 continue;
726 }
727
728 fCurrentAnimSKP++;
729 SkString name = SkOSPath::Basename(path.c_str());
cdalton63a82852015-06-29 14:06:10 -0700730 SkAutoTUnref<SKPAnimationBench::Animation> animation(
731 SKPAnimationBench::CreateZoomAnimation(fZoomMax, fZoomPeriodMs));
halcanary385fe4d2015-08-26 13:07:48 -0700732 return new SKPAnimationBench(name.c_str(), pic.get(), fClip, animation,
733 FLAGS_loopSKP);
joshualitt261c3ad2015-04-27 09:16:57 -0700734 }
735 }
736
scroggo60869a42015-04-01 12:09:17 -0700737 for (; fCurrentCodec < fImages.count(); fCurrentCodec++) {
scroggo303fa352015-10-05 11:03:34 -0700738 fSourceType = "image";
739 fBenchType = "skcodec";
scroggo60869a42015-04-01 12:09:17 -0700740 const SkString& path = fImages[fCurrentCodec];
741 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
742 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
scroggo60869a42015-04-01 12:09:17 -0700743 if (!codec) {
744 // Nothing to time.
msarett9d9725c2015-04-24 11:41:55 -0700745 SkDebugf("Cannot find codec for %s\n", path.c_str());
scroggo60869a42015-04-01 12:09:17 -0700746 continue;
747 }
scroggo21027992015-04-02 13:22:38 -0700748
scroggo60869a42015-04-01 12:09:17 -0700749 while (fCurrentColorType < fColorTypes.count()) {
scroggo21027992015-04-02 13:22:38 -0700750 const SkColorType colorType = fColorTypes[fCurrentColorType];
scroggo60869a42015-04-01 12:09:17 -0700751 fCurrentColorType++;
scroggo21027992015-04-02 13:22:38 -0700752
scroggo60869a42015-04-01 12:09:17 -0700753 // Make sure we can decode to this color type.
scroggo60869a42015-04-01 12:09:17 -0700754 SkImageInfo info = codec->getInfo().makeColorType(colorType);
scroggo21027992015-04-02 13:22:38 -0700755 SkAlphaType alphaType;
756 if (!SkColorTypeValidateAlphaType(colorType, info.alphaType(),
757 &alphaType)) {
758 continue;
759 }
760 if (alphaType != info.alphaType()) {
761 info = info.makeAlphaType(alphaType);
762 }
763
764 const size_t rowBytes = info.minRowBytes();
765 SkAutoMalloc storage(info.getSafeSize(rowBytes));
766
767 // Used if fCurrentColorType is kIndex_8_SkColorType
768 int colorCount = 256;
769 SkPMColor colors[256];
770
scroggoeb602a52015-07-09 08:16:03 -0700771 const SkCodec::Result result = codec->getPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700772 info, storage.get(), rowBytes, nullptr, colors,
scroggo21027992015-04-02 13:22:38 -0700773 &colorCount);
scroggo60869a42015-04-01 12:09:17 -0700774 switch (result) {
scroggoeb602a52015-07-09 08:16:03 -0700775 case SkCodec::kSuccess:
776 case SkCodec::kIncompleteInput:
scroggo60869a42015-04-01 12:09:17 -0700777 return new CodecBench(SkOSPath::Basename(path.c_str()),
778 encoded, colorType);
scroggoeb602a52015-07-09 08:16:03 -0700779 case SkCodec::kInvalidConversion:
scroggo60869a42015-04-01 12:09:17 -0700780 // This is okay. Not all conversions are valid.
781 break;
scroggo60869a42015-04-01 12:09:17 -0700782 default:
783 // This represents some sort of failure.
784 SkASSERT(false);
785 break;
786 }
787 }
788 fCurrentColorType = 0;
789 }
790
msarett95f192d2015-02-13 09:05:41 -0800791 // Run the DecodingBenches
792 while (fCurrentImage < fImages.count()) {
scroggo303fa352015-10-05 11:03:34 -0700793 fSourceType = "image";
794 fBenchType = "skimagedecoder";
msarett95f192d2015-02-13 09:05:41 -0800795 while (fCurrentColorType < fColorTypes.count()) {
796 const SkString& path = fImages[fCurrentImage];
797 SkColorType colorType = fColorTypes[fCurrentColorType];
798 fCurrentColorType++;
scroggo60869a42015-04-01 12:09:17 -0700799 // Check if the image decodes to the right color type
800 // before creating the benchmark
msarett95f192d2015-02-13 09:05:41 -0800801 SkBitmap bitmap;
802 if (SkImageDecoder::DecodeFile(path.c_str(), &bitmap,
scroggo60869a42015-04-01 12:09:17 -0700803 colorType, SkImageDecoder::kDecodePixels_Mode)
804 && bitmap.colorType() == colorType) {
msarett95f192d2015-02-13 09:05:41 -0800805 return new DecodingBench(path, colorType);
806 }
807 }
808 fCurrentColorType = 0;
809 fCurrentImage++;
810 }
811
msarett7f691442015-09-22 11:56:16 -0700812 // Run the BRDBenches
813 // We will benchmark multiple BRD strategies.
scroggo303fa352015-10-05 11:03:34 -0700814 static const struct {
msarett5cb48852015-11-06 08:56:32 -0800815 SkBitmapRegionDecoder::Strategy fStrategy;
msarett0459c942015-11-10 14:52:13 -0800816 const char* fName;
scroggo303fa352015-10-05 11:03:34 -0700817 } strategies[] = {
msarett0459c942015-11-10 14:52:13 -0800818 { SkBitmapRegionDecoder::kCanvas_Strategy, "BRD_canvas" },
msarett5cb48852015-11-06 08:56:32 -0800819 { SkBitmapRegionDecoder::kAndroidCodec_Strategy, "BRD_android_codec" },
msarett7f691442015-09-22 11:56:16 -0700820 };
821
822 // We intend to create benchmarks that model the use cases in
823 // android/libraries/social/tiledimage. In this library, an image is decoded in 512x512
824 // tiles. The image can be translated freely, so the location of a tile may be anywhere in
825 // the image. For that reason, we will benchmark decodes in five representative locations
826 // in the image. Additionally, this use case utilizes power of two scaling, so we will
827 // test on power of two sample sizes. The output tile is always 512x512, so, when a
828 // sampleSize is used, the size of the subset that is decoded is always
829 // (sampleSize*512)x(sampleSize*512).
830 // There are a few good reasons to only test on power of two sample sizes at this time:
831 // JPEG decodes using kOriginal_Strategy are broken for non-powers of two.
halcanary6950de62015-11-07 05:29:00 -0800832 // https://bug.skia.org/4319
msarett7f691442015-09-22 11:56:16 -0700833 // All use cases we are aware of only scale by powers of two.
834 // PNG decodes use the indicated sampling strategy regardless of the sample size, so
835 // these tests are sufficient to provide good coverage of our scaling options.
scroggo501b7342015-11-03 07:55:11 -0800836 const uint32_t sampleSizes[] = { 1, 2, 4, 8, 16, 32, 64 };
msarett7f691442015-09-22 11:56:16 -0700837 const uint32_t minOutputSize = 512;
838 while (fCurrentBRDImage < fImages.count()) {
839 while (fCurrentBRDStrategy < (int) SK_ARRAY_COUNT(strategies)) {
scroggo303fa352015-10-05 11:03:34 -0700840 fSourceType = "image";
841 fBenchType = strategies[fCurrentBRDStrategy].fName;
scroggo860e8a62015-10-15 07:51:28 -0700842
843 const SkString& path = fImages[fCurrentBRDImage];
msarett5cb48852015-11-06 08:56:32 -0800844 const SkBitmapRegionDecoder::Strategy strategy =
scroggo860e8a62015-10-15 07:51:28 -0700845 strategies[fCurrentBRDStrategy].fStrategy;
846
msarett7f691442015-09-22 11:56:16 -0700847 while (fCurrentColorType < fColorTypes.count()) {
848 while (fCurrentBRDSampleSize < (int) SK_ARRAY_COUNT(sampleSizes)) {
849 while (fCurrentSubsetType <= kLastSingle_SubsetType) {
scroggo860e8a62015-10-15 07:51:28 -0700850
851
msarett7f691442015-09-22 11:56:16 -0700852 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
853 const SkColorType colorType = fColorTypes[fCurrentColorType];
854 uint32_t sampleSize = sampleSizes[fCurrentBRDSampleSize];
855 int currentSubsetType = fCurrentSubsetType++;
856
857 int width = 0;
858 int height = 0;
859 if (!valid_brd_bench(encoded.get(), strategy, colorType, sampleSize,
860 minOutputSize, &width, &height)) {
861 break;
862 }
863
864 SkString basename = SkOSPath::Basename(path.c_str());
865 SkIRect subset;
866 const uint32_t subsetSize = sampleSize * minOutputSize;
867 switch (currentSubsetType) {
868 case kTopLeft_SubsetType:
869 basename.append("_TopLeft");
870 subset = SkIRect::MakeXYWH(0, 0, subsetSize, subsetSize);
871 break;
872 case kTopRight_SubsetType:
873 basename.append("_TopRight");
874 subset = SkIRect::MakeXYWH(width - subsetSize, 0, subsetSize,
875 subsetSize);
876 break;
877 case kMiddle_SubsetType:
878 basename.append("_Middle");
879 subset = SkIRect::MakeXYWH((width - subsetSize) / 2,
880 (height - subsetSize) / 2, subsetSize, subsetSize);
881 break;
882 case kBottomLeft_SubsetType:
883 basename.append("_BottomLeft");
884 subset = SkIRect::MakeXYWH(0, height - subsetSize, subsetSize,
885 subsetSize);
886 break;
887 case kBottomRight_SubsetType:
888 basename.append("_BottomRight");
889 subset = SkIRect::MakeXYWH(width - subsetSize,
890 height - subsetSize, subsetSize, subsetSize);
891 break;
892 default:
893 SkASSERT(false);
894 }
895
896 return new BitmapRegionDecoderBench(basename.c_str(), encoded.get(),
897 strategy, colorType, sampleSize, subset);
898 }
899 fCurrentSubsetType = 0;
900 fCurrentBRDSampleSize++;
901 }
902 fCurrentBRDSampleSize = 0;
903 fCurrentColorType++;
904 }
905 fCurrentColorType = 0;
906 fCurrentBRDStrategy++;
907 }
908 fCurrentBRDStrategy = 0;
909 fCurrentBRDImage++;
910 }
911
halcanary96fcdcc2015-08-27 07:41:13 -0700912 return nullptr;
mtkleine714e752014-07-31 12:13:48 -0700913 }
mtklein92007582014-08-01 07:46:52 -0700914
915 void fillCurrentOptions(ResultsWriter* log) const {
916 log->configOption("source_type", fSourceType);
mtkleinfd731ce2014-09-10 12:19:30 -0700917 log->configOption("bench_type", fBenchType);
mtklein92007582014-08-01 07:46:52 -0700918 if (0 == strcmp(fSourceType, "skp")) {
919 log->configOption("clip",
920 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
921 fClip.fRight, fClip.fBottom).c_str());
mtklein4dfdbb12015-10-20 07:45:29 -0700922 SK_ALWAYSBREAK(fCurrentScale < fScales.count()); // debugging paranoia
mtklein92007582014-08-01 07:46:52 -0700923 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
robertphillips5b693772014-11-21 06:19:36 -0800924 if (fCurrentUseMPD > 0) {
925 SkASSERT(1 == fCurrentUseMPD || 2 == fCurrentUseMPD);
926 log->configOption("multi_picture_draw", fUseMPDs[fCurrentUseMPD-1] ? "true" : "false");
927 }
mtklein92007582014-08-01 07:46:52 -0700928 }
mtklein051e56d2014-12-04 08:46:51 -0800929 if (0 == strcmp(fBenchType, "recording")) {
930 log->metric("bytes", fSKPBytes);
931 log->metric("ops", fSKPOps);
932 }
mtklein92007582014-08-01 07:46:52 -0700933 }
934
mtkleine714e752014-07-31 12:13:48 -0700935private:
msarettb23e6aa2015-06-09 13:56:10 -0700936 enum SubsetType {
937 kTopLeft_SubsetType = 0,
938 kTopRight_SubsetType = 1,
msarettab80e352015-06-17 10:28:22 -0700939 kMiddle_SubsetType = 2,
940 kBottomLeft_SubsetType = 3,
941 kBottomRight_SubsetType = 4,
942 kTranslate_SubsetType = 5,
943 kZoom_SubsetType = 6,
msarett7f691442015-09-22 11:56:16 -0700944 kLast_SubsetType = kZoom_SubsetType,
945 kLastSingle_SubsetType = kBottomRight_SubsetType,
msarettb23e6aa2015-06-09 13:56:10 -0700946 };
947
mtkleine714e752014-07-31 12:13:48 -0700948 const BenchRegistry* fBenches;
949 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700950 SkIRect fClip;
951 SkTArray<SkScalar> fScales;
952 SkTArray<SkString> fSKPs;
robertphillips5b693772014-11-21 06:19:36 -0800953 SkTArray<bool> fUseMPDs;
msarett95f192d2015-02-13 09:05:41 -0800954 SkTArray<SkString> fImages;
msarett74deb982015-10-20 16:45:56 -0700955 SkTArray<SkColorType, true> fColorTypes;
cdalton63a82852015-06-29 14:06:10 -0700956 SkScalar fZoomMax;
957 double fZoomPeriodMs;
mtklein92007582014-08-01 07:46:52 -0700958
mtklein051e56d2014-12-04 08:46:51 -0800959 double fSKPBytes, fSKPOps;
960
mtkleinfd731ce2014-09-10 12:19:30 -0700961 const char* fSourceType; // What we're benching: bench, GM, SKP, ...
962 const char* fBenchType; // How we bench it: micro, recording, playback, ...
963 int fCurrentRecording;
mtklein92007582014-08-01 07:46:52 -0700964 int fCurrentScale;
965 int fCurrentSKP;
robertphillips5b693772014-11-21 06:19:36 -0800966 int fCurrentUseMPD;
scroggo60869a42015-04-01 12:09:17 -0700967 int fCurrentCodec;
msarett95f192d2015-02-13 09:05:41 -0800968 int fCurrentImage;
msarett7f691442015-09-22 11:56:16 -0700969 int fCurrentBRDImage;
msarett95f192d2015-02-13 09:05:41 -0800970 int fCurrentColorType;
msarettb23e6aa2015-06-09 13:56:10 -0700971 int fCurrentSubsetType;
msarett7f691442015-09-22 11:56:16 -0700972 int fCurrentBRDStrategy;
973 int fCurrentBRDSampleSize;
joshualitt261c3ad2015-04-27 09:16:57 -0700974 int fCurrentAnimSKP;
mtkleine714e752014-07-31 12:13:48 -0700975};
976
msarettc149f0e2016-01-04 11:35:43 -0800977// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
978// This prints something every once in a while so that it knows we're still working.
979static void start_keepalive() {
980 struct Loop {
981 static void forever(void*) {
982 for (;;) {
983 static const int kSec = 1200;
984 #if defined(SK_BUILD_FOR_WIN)
985 Sleep(kSec * 1000);
986 #else
987 sleep(kSec);
988 #endif
989 SkDebugf("\nBenchmarks still running...\n");
990 }
991 }
992 };
993 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
994 intentionallyLeaked->start();
995}
996
jcgregorio3b27ade2014-11-13 08:06:40 -0800997int nanobench_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700998int nanobench_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800999 SetupCrashHandler();
mtkleinf3723212014-06-25 14:08:00 -07001000 SkAutoGraphics ag;
mtkleincc29d262015-07-09 10:04:56 -07001001 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleinf3723212014-06-25 14:08:00 -07001002
krajcevski69a55602014-08-13 10:46:31 -07001003#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -07001004 GrContextOptions grContextOpts;
krajcevski12b35442014-08-13 12:06:26 -07001005 grContextOpts.fDrawPathToCompressedTexture = FLAGS_gpuCompressAlphaMasks;
halcanary385fe4d2015-08-26 13:07:48 -07001006 gGrFactory.reset(new GrContextFactory(grContextOpts));
krajcevski69a55602014-08-13 10:46:31 -07001007#endif
1008
bsalomon06cddec2014-10-24 10:40:50 -07001009 if (FLAGS_veryVerbose) {
1010 FLAGS_verbose = true;
1011 }
1012
bsalomon6eb03cc2014-08-07 14:28:50 -07001013 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -07001014 FLAGS_samples = 1;
1015 FLAGS_gpuFrameLag = 0;
1016 }
1017
bsalomon6eb03cc2014-08-07 14:28:50 -07001018 if (!FLAGS_writePath.isEmpty()) {
1019 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
1020 if (!sk_mkdir(FLAGS_writePath[0])) {
1021 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
halcanary96fcdcc2015-08-27 07:41:13 -07001022 FLAGS_writePath.set(0, nullptr);
bsalomon6eb03cc2014-08-07 14:28:50 -07001023 }
1024 }
1025
halcanary385fe4d2015-08-26 13:07:48 -07001026 SkAutoTDelete<ResultsWriter> log(new ResultsWriter);
mtklein60317d0f2014-07-14 11:30:37 -07001027 if (!FLAGS_outResultsFile.isEmpty()) {
halcanary385fe4d2015-08-26 13:07:48 -07001028 log.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
mtklein60317d0f2014-07-14 11:30:37 -07001029 }
mtklein1915b622014-08-20 11:45:00 -07001030
1031 if (1 == FLAGS_properties.count() % 2) {
1032 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
1033 return 1;
1034 }
1035 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
1036 log->property(FLAGS_properties[i-1], FLAGS_properties[i]);
1037 }
jcgregoriobf5e5232014-07-17 13:14:16 -07001038
1039 if (1 == FLAGS_key.count() % 2) {
1040 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
1041 return 1;
1042 }
1043 for (int i = 1; i < FLAGS_key.count(); i += 2) {
mtklein1915b622014-08-20 11:45:00 -07001044 log->key(FLAGS_key[i-1], FLAGS_key[i]);
mtklein94e51562014-08-19 12:41:53 -07001045 }
mtklein60317d0f2014-07-14 11:30:37 -07001046
mtkleinf3723212014-06-25 14:08:00 -07001047 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -07001048 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -04001049
cdaltone1b89582015-06-25 19:17:08 -07001050 SkTArray<double> samples;
mtkleinbb6a0282014-07-01 08:43:42 -07001051
bsalomon6eb03cc2014-08-07 14:28:50 -07001052 if (kAutoTuneLoops != FLAGS_loops) {
1053 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleinf3723212014-06-25 14:08:00 -07001054 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001055 SkDebugf("! -> high variance, ? -> moderate variance\n");
1056 SkDebugf(" micros \tbench\n");
mtkleinbbba1682015-10-28 11:36:30 -07001057 } else if (FLAGS_ms) {
cdaltone1b89582015-06-25 19:17:08 -07001058 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -07001059 } else {
mtkleind75c4662015-04-30 07:11:22 -07001060 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
qiankun.miao8247ec32014-09-09 19:24:36 -07001061 FLAGS_samples, "samples");
mtkleinf3723212014-06-25 14:08:00 -07001062 }
1063
bsalomonc2553372014-07-22 13:09:05 -07001064 SkTDArray<Config> configs;
1065 create_configs(&configs);
1066
msarettc149f0e2016-01-04 11:35:43 -08001067 if (FLAGS_keepAlive) {
1068 start_keepalive();
1069 }
1070
mtkleine070c2b2014-10-14 08:40:43 -07001071 int runs = 0;
mtklein92007582014-08-01 07:46:52 -07001072 BenchmarkStream benchStream;
1073 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -07001074 SkAutoTDelete<Benchmark> bench(b);
mtklein96289052014-09-10 12:05:59 -07001075 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getUniqueName())) {
mtkleinf3723212014-06-25 14:08:00 -07001076 continue;
1077 }
1078
egdaniel3bf92062015-06-26 08:12:46 -07001079 if (!configs.isEmpty()) {
mtklein96289052014-09-10 12:05:59 -07001080 log->bench(bench->getUniqueName(), bench->getSize().fX, bench->getSize().fY);
joshualitt8a6697a2015-09-30 12:11:07 -07001081 bench->delayedSetup();
jcgregoriobf5e5232014-07-17 13:14:16 -07001082 }
egdaniel3bf92062015-06-26 08:12:46 -07001083 for (int i = 0; i < configs.count(); ++i) {
1084 Target* target = is_enabled(b, configs[i]);
1085 if (!target) {
1086 continue;
1087 }
mtkleinf3723212014-06-25 14:08:00 -07001088
halcanary96fcdcc2015-08-27 07:41:13 -07001089 // During HWUI output this canvas may be nullptr.
egdaniel3bf92062015-06-26 08:12:46 -07001090 SkCanvas* canvas = target->getCanvas();
1091 const char* config = target->config.name;
1092
1093 target->setup();
robertphillips5b693772014-11-21 06:19:36 -08001094 bench->perCanvasPreDraw(canvas);
1095
cdaltone1b89582015-06-25 19:17:08 -07001096 int maxFrameLag;
mtkleina1ebeb22015-10-01 09:43:39 -07001097 int loops = target->needsFrameTiming(&maxFrameLag)
egdaniel3bf92062015-06-26 08:12:46 -07001098 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
1099 : setup_cpu_bench(overhead, target, bench.get());
cdaltone1b89582015-06-25 19:17:08 -07001100
mtkleinbbba1682015-10-28 11:36:30 -07001101 if (FLAGS_ms) {
1102 samples.reset();
1103 auto stop = now_ms() + FLAGS_ms;
1104 do {
1105 samples.push_back(time(loops, bench, target) / loops);
1106 } while (now_ms() < stop);
1107 } else {
cdaltone1b89582015-06-25 19:17:08 -07001108 samples.reset(FLAGS_samples);
1109 for (int s = 0; s < FLAGS_samples; s++) {
egdaniel3bf92062015-06-26 08:12:46 -07001110 samples[s] = time(loops, bench, target) / loops;
cdaltone1b89582015-06-25 19:17:08 -07001111 }
cdaltone1b89582015-06-25 19:17:08 -07001112 }
mtkleinf3723212014-06-25 14:08:00 -07001113
joshualitte45c81c2015-12-02 09:05:37 -08001114#if SK_SUPPORT_GPU
1115 SkTArray<SkString> keys;
1116 SkTArray<double> values;
1117 bool gpuStatsDump = FLAGS_gpuStatsDump && Benchmark::kGPU_Backend == configs[i].backend;
1118 if (gpuStatsDump) {
1119 // TODO cache stats
1120 bench->getGpuStats(canvas, &keys, &values);
1121 }
1122#endif
1123
robertphillips5b693772014-11-21 06:19:36 -08001124 bench->perCanvasPostDraw(canvas);
1125
egdaniel3bf92062015-06-26 08:12:46 -07001126 if (Benchmark::kNonRendering_Backend != target->config.backend &&
tomhudsond968a6f2015-03-26 11:28:06 -07001127 !FLAGS_writePath.isEmpty() && FLAGS_writePath[0]) {
bsalomon6eb03cc2014-08-07 14:28:50 -07001128 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
mtklein96289052014-09-10 12:05:59 -07001129 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getUniqueName());
bsalomon6eb03cc2014-08-07 14:28:50 -07001130 pngFilename.append(".png");
egdaniel3bf92062015-06-26 08:12:46 -07001131 write_canvas_png(target, pngFilename);
bsalomon6eb03cc2014-08-07 14:28:50 -07001132 }
1133
1134 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -07001135 // Can't be timed. A warning note has already been printed.
egdaniel3bf92062015-06-26 08:12:46 -07001136 cleanup_run(target);
Mike Kleine3631362014-07-15 17:56:37 -04001137 continue;
1138 }
1139
cdaltone1b89582015-06-25 19:17:08 -07001140 Stats stats(samples);
mtklein1915b622014-08-20 11:45:00 -07001141 log->config(config);
mtklein96289052014-09-10 12:05:59 -07001142 log->configOption("name", bench->getName());
mtklein1915b622014-08-20 11:45:00 -07001143 benchStream.fillCurrentOptions(log.get());
egdaniel3bf92062015-06-26 08:12:46 -07001144 target->fillOptions(log.get());
mtklein051e56d2014-12-04 08:46:51 -08001145 log->metric("min_ms", stats.min);
joshualitte45c81c2015-12-02 09:05:37 -08001146#if SK_SUPPORT_GPU
1147 if (gpuStatsDump) {
1148 // dump to json, only SKPBench currently returns valid keys / values
1149 SkASSERT(keys.count() == values.count());
1150 for (int i = 0; i < keys.count(); i++) {
1151 log->metric(keys[i].c_str(), values[i]);
1152 }
1153 }
1154#endif
1155
mtkleine070c2b2014-10-14 08:40:43 -07001156 if (runs++ % FLAGS_flushEvery == 0) {
1157 log->flush();
1158 }
mtklein60317d0f2014-07-14 11:30:37 -07001159
bsalomon6eb03cc2014-08-07 14:28:50 -07001160 if (kAutoTuneLoops != FLAGS_loops) {
egdaniel3bf92062015-06-26 08:12:46 -07001161 if (configs.count() == 1) {
mtkleina189ccd2014-07-14 12:28:47 -07001162 config = ""; // Only print the config if we run the same bench on more than one.
1163 }
mtkleind75c4662015-04-30 07:11:22 -07001164 SkDebugf("%4d/%-4dMB\t%s\t%s\n"
1165 , sk_tools::getCurrResidentSetSizeMB()
1166 , sk_tools::getMaxResidentSetSizeMB()
mtklein53d25622014-09-18 07:39:42 -07001167 , bench->getUniqueName()
1168 , config);
mtkleinf3723212014-06-25 14:08:00 -07001169 } else if (FLAGS_quiet) {
mtklein66cfcff2015-12-04 06:35:30 -08001170 const char* mark = " ";
1171 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
1172 if (stddev_percent > 5) mark = "?";
1173 if (stddev_percent > 10) mark = "!";
1174
1175 SkDebugf("%10.2f %s\t%s\t%s\n",
1176 stats.median*1e3, mark, bench->getUniqueName(), config);
mtkleinf3723212014-06-25 14:08:00 -07001177 } else {
1178 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtkleind75c4662015-04-30 07:11:22 -07001179 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
1180 , sk_tools::getCurrResidentSetSizeMB()
1181 , sk_tools::getMaxResidentSetSizeMB()
mtkleinf3723212014-06-25 14:08:00 -07001182 , loops
mtklein55b0ffc2014-07-17 08:38:23 -07001183 , HUMANIZE(stats.min)
1184 , HUMANIZE(stats.median)
1185 , HUMANIZE(stats.mean)
1186 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -07001187 , stddev_percent
mtkleinbbba1682015-10-28 11:36:30 -07001188 , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -07001189 , config
mtklein96289052014-09-10 12:05:59 -07001190 , bench->getUniqueName()
mtkleinf3723212014-06-25 14:08:00 -07001191 );
1192 }
joshualitte45c81c2015-12-02 09:05:37 -08001193
bsalomonb12ea412015-02-02 21:19:50 -08001194#if SK_SUPPORT_GPU
joshualitte45c81c2015-12-02 09:05:37 -08001195 if (FLAGS_gpuStats && Benchmark::kGPU_Backend == configs[i].backend) {
kkinnunen5219fd92015-12-10 06:28:13 -08001196 GrContext* context = gGrFactory->get(configs[i].ctxType,
kkinnunen3e980c32015-12-23 01:33:00 -08001197 configs[i].ctxOptions);
kkinnunen5219fd92015-12-10 06:28:13 -08001198 context->printCacheStats();
1199 context->printGpuStats();
bsalomon06cddec2014-10-24 10:40:50 -07001200 }
1201#endif
joshualitte45c81c2015-12-02 09:05:37 -08001202
cdalton2c56ba52015-06-26 13:32:53 -07001203 if (FLAGS_verbose) {
1204 SkDebugf("Samples: ");
1205 for (int i = 0; i < samples.count(); i++) {
1206 SkDebugf("%s ", HUMANIZE(samples[i]));
1207 }
1208 SkDebugf("%s\n", bench->getUniqueName());
1209 }
egdaniel3bf92062015-06-26 08:12:46 -07001210 cleanup_run(target);
mtkleinf3723212014-06-25 14:08:00 -07001211 }
mtkleinf3723212014-06-25 14:08:00 -07001212 }
1213
mtkleine1091452014-12-04 10:47:02 -08001214 log->bench("memory_usage", 0,0);
1215 log->config("meta");
1216 log->metric("max_rss_mb", sk_tools::getMaxResidentSetSizeMB());
1217
joshualitte0b19d42015-03-26 10:41:02 -07001218#if SK_SUPPORT_GPU
1219 // Make sure we clean up the global GrContextFactory here, otherwise we might race with the
1220 // SkEventTracer destructor
halcanary96fcdcc2015-08-27 07:41:13 -07001221 gGrFactory.reset(nullptr);
joshualitte0b19d42015-03-26 10:41:02 -07001222#endif
1223
mtkleinf3723212014-06-25 14:08:00 -07001224 return 0;
1225}
1226
jcgregorio3b27ade2014-11-13 08:06:40 -08001227#if !defined SK_BUILD_FOR_IOS
1228int main(int argc, char** argv) {
1229 SkCommandLineFlags::Parse(argc, argv);
1230 return nanobench_main();
1231}
1232#endif